00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #include "platform.h"
00026 #include <limits.h>
00027 #include "internal.h"
00028 #include "base64.h"
00029
00033 #define _BASIC_BASE "Basic "
00034
00035
00045 char *
00046 MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
00047 char** password)
00048 {
00049 const char *header;
00050 char *decode;
00051 const char *separator;
00052 char *user;
00053
00054 if ( (NULL == (header = MHD_lookup_connection_value (connection,
00055 MHD_HEADER_KIND,
00056 MHD_HTTP_HEADER_AUTHORIZATION))) ||
00057 (0 != strncmp (header, _BASIC_BASE, strlen(_BASIC_BASE))) )
00058 return NULL;
00059 header += strlen (_BASIC_BASE);
00060 if (NULL == (decode = BASE64Decode (header)))
00061 {
00062 #if HAVE_MESSAGES
00063 MHD_DLOG (connection->daemon,
00064 "Error decoding basic authentication\n");
00065 #endif
00066 return NULL;
00067 }
00068
00069 if (NULL == (separator = strchr (decode, ':')))
00070 {
00071 #if HAVE_MESSAGES
00072 MHD_DLOG(connection->daemon,
00073 "Basic authentication doesn't contain ':' separator\n");
00074 #endif
00075 free (decode);
00076 return NULL;
00077 }
00078 if (NULL == (user = strdup (decode)))
00079 {
00080 free (decode);
00081 return NULL;
00082 }
00083 user[separator - decode] = '\0';
00084 if (NULL != password)
00085 {
00086 *password = strdup (separator + 1);
00087 if (NULL == *password)
00088 {
00089 #if HAVE_MESSAGES
00090 MHD_DLOG(connection->daemon,
00091 "Failed to allocate memory for password\n");
00092 #endif
00093 free (decode);
00094 free (user);
00095 return NULL;
00096 }
00097 }
00098 free (decode);
00099 return user;
00100 }
00101
00102
00115 int
00116 MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
00117 const char *realm,
00118 struct MHD_Response *response)
00119 {
00120 int ret;
00121 size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1;
00122 char header[hlen];
00123
00124 snprintf (header,
00125 sizeof (header),
00126 "Basic realm=\"%s\"",
00127 realm);
00128 ret = MHD_add_response_header (response,
00129 MHD_HTTP_HEADER_WWW_AUTHENTICATE,
00130 header);
00131 if (MHD_YES == ret)
00132 ret = MHD_queue_response (connection,
00133 MHD_HTTP_UNAUTHORIZED,
00134 response);
00135 return ret;
00136 }
00137
00138