GNU libmicrohttpd  0.9.29
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
basicauth.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  (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 <limits.h>
27 #include "internal.h"
28 #include "base64.h"
29 
33 #define _BASIC_BASE "Basic "
34 
35 
45 char *
47  char** password)
48 {
49  const char *header;
50  char *decode;
51  const char *separator;
52  char *user;
53 
54  if ( (NULL == (header = MHD_lookup_connection_value (connection,
57  (0 != strncmp (header, _BASIC_BASE, strlen(_BASIC_BASE))) )
58  return NULL;
59  header += strlen (_BASIC_BASE);
60  if (NULL == (decode = BASE64Decode (header)))
61  {
62 #if HAVE_MESSAGES
63  MHD_DLOG (connection->daemon,
64  "Error decoding basic authentication\n");
65 #endif
66  return NULL;
67  }
68  /* Find user:password pattern */
69  if (NULL == (separator = strchr (decode, ':')))
70  {
71 #if HAVE_MESSAGES
72  MHD_DLOG(connection->daemon,
73  "Basic authentication doesn't contain ':' separator\n");
74 #endif
75  free (decode);
76  return NULL;
77  }
78  if (NULL == (user = strdup (decode)))
79  {
80  free (decode);
81  return NULL;
82  }
83  user[separator - decode] = '\0'; /* cut off at ':' */
84  if (NULL != password)
85  {
86  *password = strdup (separator + 1);
87  if (NULL == *password)
88  {
89 #if HAVE_MESSAGES
90  MHD_DLOG(connection->daemon,
91  "Failed to allocate memory for password\n");
92 #endif
93  free (decode);
94  free (user);
95  return NULL;
96  }
97  }
98  free (decode);
99  return user;
100 }
101 
102 
115 int
117  const char *realm,
118  struct MHD_Response *response)
119 {
120  int ret;
121  size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1;
122  char *header;
123 
124  header = (char*)malloc(hlen);
125  if (NULL == header)
126  {
127 #if HAVE_MESSAGES
128  MHD_DLOG(connection->daemon,
129  "Failed to allocate memory for auth header\n");
130 #endif /* HAVE_MESSAGES */
131  return MHD_NO;
132  }
133  MHD_snprintf_ (header,
134  hlen,
135  "Basic realm=\"%s\"",
136  realm);
137  ret = MHD_add_response_header (response,
139  header);
140  free(header);
141  if (MHD_YES == ret)
142  ret = MHD_queue_response (connection,
144  response);
145  return ret;
146 }
147 
148 /* 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:217
_MHD_EXTERN int MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
Definition: response.c:100
#define NULL
Definition: reason_phrase.c:31
#define MHD_YES
Definition: microhttpd.h:138
_MHD_EXTERN int MHD_queue_basic_auth_fail_response(struct MHD_Connection *connection, const char *realm, struct MHD_Response *response)
Definition: basicauth.c:116
platform-specific includes for libmicrohttpd
#define _BASIC_BASE
Definition: basicauth.c:33
struct MHD_Daemon * daemon
Definition: internal.h:551
_MHD_EXTERN char * MHD_basic_auth_get_username_password(struct MHD_Connection *connection, char **password)
Definition: basicauth.c:46
#define MHD_HTTP_HEADER_AUTHORIZATION
Definition: microhttpd.h:312
internal shared structures
#define MHD_HTTP_HEADER_WWW_AUTHENTICATE
Definition: microhttpd.h:355
#define MHD_snprintf_
_MHD_EXTERN int MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
Definition: connection.c:2907
#define MHD_HTTP_UNAUTHORIZED
Definition: microhttpd.h:249
char * BASE64Decode(const char *src)
Definition: base64.c:30
#define MHD_NO
Definition: microhttpd.h:143