GNU libmicrohttpd  0.9.65
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 ( (MHD_NO == MHD_lookup_connection_value_n (connection,
59  &header,
60  NULL)) ||
61  (0 != strncmp (header,
64  return NULL;
65  header += MHD_STATICSTR_LEN_ (_BASIC_BASE);
66  if (NULL == (decode = BASE64Decode (header)))
67  {
68 #ifdef HAVE_MESSAGES
69  MHD_DLOG (connection->daemon,
70  _("Error decoding basic authentication\n"));
71 #endif
72  return NULL;
73  }
74  /* Find user:password pattern */
75  if (NULL == (separator = strchr (decode,
76  ':')))
77  {
78 #ifdef HAVE_MESSAGES
79  MHD_DLOG(connection->daemon,
80  _("Basic authentication doesn't contain ':' separator\n"));
81 #endif
82  free (decode);
83  return NULL;
84  }
85  if (NULL == (user = strdup (decode)))
86  {
87  free (decode);
88  return NULL;
89  }
90  user[separator - decode] = '\0'; /* cut off at ':' */
91  if (NULL != password)
92  {
93  *password = strdup (separator + 1);
94  if (NULL == *password)
95  {
96 #ifdef HAVE_MESSAGES
97  MHD_DLOG(connection->daemon,
98  _("Failed to allocate memory for password\n"));
99 #endif
100  free (decode);
101  free (user);
102  return NULL;
103  }
104  }
105  free (decode);
106  return user;
107 }
108 
109 
122 int
124  const char *realm,
125  struct MHD_Response *response)
126 {
127  int ret;
128  int res;
129  size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1;
130  char *header;
131 
132  header = (char *) malloc(hlen);
133  if (NULL == header)
134  {
135 #ifdef HAVE_MESSAGES
136  MHD_DLOG(connection->daemon,
137  "Failed to allocate memory for auth header\n");
138 #endif /* HAVE_MESSAGES */
139  return MHD_NO;
140  }
141  res = MHD_snprintf_ (header,
142  hlen,
143  "Basic realm=\"%s\"",
144  realm);
145  if (res > 0 && (size_t)res < hlen)
146  ret = MHD_add_response_header (response,
148  header);
149  else
150  ret = MHD_NO;
151 
152  free(header);
153  if (MHD_YES == ret)
154  ret = MHD_queue_response (connection,
156  response);
157  else
158  {
159 #ifdef HAVE_MESSAGES
160  MHD_DLOG (connection->daemon,
161  _("Failed to add Basic auth header\n"));
162 #endif /* HAVE_MESSAGES */
163  }
164  return ret;
165 }
166 
167 /* end of basicauth.c */
Header for platform missing functions.
_MHD_EXTERN int MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
Definition: response.c:133
#define MHD_YES
Definition: microhttpd.h:140
_MHD_EXTERN int MHD_queue_basic_auth_fail_response(struct MHD_Connection *connection, const char *realm, struct MHD_Response *response)
Definition: basicauth.c:123
platform-specific includes for libmicrohttpd
#define _BASIC_BASE
Definition: basicauth.c:34
struct MHD_Daemon * daemon
Definition: internal.h:672
_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:533
internal shared structures
#define MHD_HTTP_HEADER_WWW_AUTHENTICATE
Definition: microhttpd.h:611
#define NULL
Definition: reason_phrase.c:30
#define MHD_STATICSTR_LEN_(macro)
Definition: mhd_str.h:45
_MHD_EXTERN int MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
Definition: connection.c:4291
#define MHD_HTTP_UNAUTHORIZED
Definition: microhttpd.h:369
char * BASE64Decode(const char *src)
Definition: base64.c:27
#define _(String)
Definition: mhd_options.h:42
_MHD_EXTERN int MHD_lookup_connection_value_n(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key, size_t key_size, const char **value_ptr, size_t *value_size_ptr)
Definition: connection.c:957
#define MHD_NO
Definition: microhttpd.h:145
limits values definitions