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