GNU libmicrohttpd 0.9.77
Loading...
Searching...
No Matches
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 "mhd_str.h"
29#include "mhd_compat.h"
30
34#define _BASIC_BASE "Basic "
35
36
46char *
48 char **password)
49{
50 const char *header;
51 size_t enc_size;
52 size_t value_size;
53 size_t dec_size;
54 char *decode;
55 char *separator;
56
57 if (NULL != password)
58 *password = NULL;
59
60 if (MHD_NO ==
66 &header,
67 &value_size))
68 return NULL;
69
70 if (0 != strncmp (header,
73 return NULL;
74
76 enc_size = value_size - MHD_STATICSTR_LEN_ (_BASIC_BASE);
77 if (0 != (enc_size % 4))
78 {
79#ifdef HAVE_MESSAGES
80 MHD_DLOG (connection->daemon,
81 _ ("Bad length of basic authentication value.\n"));
82#endif
83 return NULL;
84 }
85 dec_size = MHD_base64_max_dec_size_ (enc_size);
86 decode = (char *) malloc (dec_size + 1);
87 if (NULL == decode)
88 {
89#ifdef HAVE_MESSAGES
90 MHD_DLOG (connection->daemon,
91 _ ("Failed to allocate memory.\n"));
92#endif
93 return NULL;
94 }
95 dec_size = MHD_base64_to_bin_n (header, enc_size, decode, dec_size);
96 if (0 != dec_size)
97 {
98 decode[dec_size] = 0; /* Zero-terminate */
99 /* Find user:password pattern */
100 separator = memchr (decode, ':', dec_size);
101 if (NULL != separator)
102 {
103 *separator = 0; /* Zero-terminate 'username' */
104 if (NULL == password)
105 return decode; /* Success exit point */
106 else
107 {
108 *password = strdup (separator + 1);
109 if (NULL != *password)
110 return decode; /* Success exit point */
111#ifdef HAVE_MESSAGES
112 else
113 {
114 MHD_DLOG (connection->daemon,
115 _ ("Failed to allocate memory for password.\n"));
116 }
117#endif /* HAVE_MESSAGES */
118 }
119 }
120#ifdef HAVE_MESSAGES
121 else
122 {
123 MHD_DLOG (connection->daemon,
124 _ ("Basic authentication doesn't contain ':' separator.\n"));
125 }
126#endif /* HAVE_MESSAGES */
127 }
128#ifdef HAVE_MESSAGES
129 else
130 {
131 MHD_DLOG (connection->daemon,
132 _ ("Error decoding basic authentication.\n"));
133 }
134#endif /* HAVE_MESSAGES */
135 free (decode);
136 return NULL; /* Failure exit point */
137}
138
139
152enum MHD_Result
154 const char *realm,
155 struct MHD_Response *response)
156{
157 enum MHD_Result ret;
158 int res;
159 size_t hlen = strlen (realm) + strlen ("Basic realm=\"\"") + 1;
160 char *header;
161
162 header = (char *) malloc (hlen);
163 if (NULL == header)
164 {
165#ifdef HAVE_MESSAGES
166 MHD_DLOG (connection->daemon,
167 "Failed to allocate memory for auth header.\n");
168#endif /* HAVE_MESSAGES */
169 return MHD_NO;
170 }
171 res = MHD_snprintf_ (header,
172 hlen,
173 "Basic realm=\"%s\"",
174 realm);
175 if ((res > 0) && ((size_t) res < hlen))
176 ret = MHD_add_response_header (response,
178 header);
179 else
180 ret = MHD_NO;
181
182 free (header);
183 if (MHD_NO != ret)
184 {
185 ret = MHD_queue_response (connection,
187 response);
188 }
189 else
190 {
191#ifdef HAVE_MESSAGES
192 MHD_DLOG (connection->daemon,
193 _ ("Failed to add Basic auth header.\n"));
194#endif /* HAVE_MESSAGES */
195 }
196 return ret;
197}
198
199
200/* end of basicauth.c */
#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:153
#define MHD_HTTP_HEADER_AUTHORIZATION
Definition microhttpd.h:598
#define MHD_HTTP_HEADER_WWW_AUTHENTICATE
Definition microhttpd.h:686
#define MHD_HTTP_UNAUTHORIZED
Definition microhttpd.h:399
_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:649
_MHD_EXTERN enum MHD_Result MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
_MHD_EXTERN enum MHD_Result MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
Definition response.c:493
#define MHD_STATICSTR_LEN_(macro)
Definition mhd_str.h:45
#define NULL
#define _(String)
Definition mhd_options.h:42
MHD internal shared structures.
Header for platform missing functions.
limits values definitions
Header for string manipulating helpers.
MHD_Result
Definition microhttpd.h:158
@ MHD_NO
Definition microhttpd.h:162
@ MHD_HEADER_KIND
platform-specific includes for libmicrohttpd
struct MHD_Daemon * daemon
Definition internal.h:675