GNU libmicrohttpd  0.9.59
basicauth.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2010, 2011, 2012 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
25 #include "platform.h"
26 #include "mhd_limits.h"
27 #include "internal.h"
28 #include "base64.h"
29 #include "mhd_compat.h"
30 
34 #define _BASIC_BASE "Basic "
35 
36 
46 char *
48  char** password)
49 {
50  const char *header;
51  char *decode;
52  const char *separator;
53  char *user;
54 
55  if ( (NULL == (header = MHD_lookup_connection_value (connection,
58  (0 != strncmp (header,
61  return NULL;
62  header += MHD_STATICSTR_LEN_ (_BASIC_BASE);
63  if (NULL == (decode = BASE64Decode (header)))
64  {
65 #ifdef HAVE_MESSAGES
66  MHD_DLOG (connection->daemon,
67  _("Error decoding basic authentication\n"));
68 #endif
69  return NULL;
70  }
71  /* Find user:password pattern */
72  if (NULL == (separator = strchr (decode,
73  ':')))
74  {
75 #ifdef HAVE_MESSAGES
76  MHD_DLOG(connection->daemon,
77  _("Basic authentication doesn't contain ':' separator\n"));
78 #endif
79  free (decode);
80  return NULL;
81  }
82  if (NULL == (user = strdup (decode)))
83  {
84  free (decode);
85  return NULL;
86  }
87  user[separator - decode] = '\0'; /* cut off at ':' */
88  if (NULL != password)
89  {
90  *password = strdup (separator + 1);
91  if (NULL == *password)
92  {
93 #ifdef HAVE_MESSAGES
94  MHD_DLOG(connection->daemon,
95  _("Failed to allocate memory for password\n"));
96 #endif
97  free (decode);
98  free (user);
99  return NULL;
100  }
101  }
102  free (decode);
103  return user;
104 }
105 
106 
119 int
121  const char *realm,
122  struct MHD_Response *response)
123 {
124  int ret;
125  int res;
126  size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1;
127  char *header;
128 
129  header = (char *) malloc(hlen);
130  if (NULL == header)
131  {
132 #ifdef HAVE_MESSAGES
133  MHD_DLOG(connection->daemon,
134  "Failed to allocate memory for auth header\n");
135 #endif /* HAVE_MESSAGES */
136  return MHD_NO;
137  }
138  res = MHD_snprintf_ (header,
139  hlen,
140  "Basic realm=\"%s\"",
141  realm);
142  if (res > 0 && (size_t)res < hlen)
143  ret = MHD_add_response_header (response,
145  header);
146  else
147  ret = MHD_NO;
148 
149  free(header);
150  if (MHD_YES == ret)
151  ret = MHD_queue_response (connection,
153  response);
154  else
155  {
156 #ifdef HAVE_MESSAGES
157  MHD_DLOG (connection->daemon,
158  _("Failed to add Basic auth header\n"));
159 #endif /* HAVE_MESSAGES */
160  }
161  return ret;
162 }
163 
164 /* end of basicauth.c */
_MHD_EXTERN const char * MHD_lookup_connection_value(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
Definition: connection.c:794
_MHD_EXTERN int MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
Definition: response.c:118
#define NULL
Definition: reason_phrase.c:31
#define MHD_YES
Definition: microhttpd.h:134
_MHD_EXTERN int MHD_queue_basic_auth_fail_response(struct MHD_Connection *connection, const char *realm, struct MHD_Response *response)
Definition: basicauth.c:120
Header for platform missing functions.
platform-specific includes for libmicrohttpd
#define _BASIC_BASE
Definition: basicauth.c:34
struct MHD_Daemon * daemon
Definition: internal.h:650
#define MHD_STATICSTR_LEN_(macro)
Definition: internal.h:126
_MHD_EXTERN char * MHD_basic_auth_get_username_password(struct MHD_Connection *connection, char **password)
Definition: basicauth.c:47
#define MHD_HTTP_HEADER_AUTHORIZATION
Definition: microhttpd.h:434
#define MHD_HTTP_HEADER_WWW_AUTHENTICATE
Definition: microhttpd.h:512
limits values definitions
internal shared structures
_MHD_EXTERN int MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
Definition: connection.c:3931
#define MHD_HTTP_UNAUTHORIZED
Definition: microhttpd.h:330
char * BASE64Decode(const char *src)
Definition: base64.c:27
#define _(String)
Definition: mhd_options.h:42
#define MHD_NO
Definition: microhttpd.h:139