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
00044 char *
00045 MHD_basic_auth_get_username_password(struct MHD_Connection *connection,
00046 char** password)
00047 {
00048 const char *header;
00049 char *decode;
00050 const char *separator;
00051 char *user;
00052
00053 if ( (NULL == (header = MHD_lookup_connection_value (connection,
00054 MHD_HEADER_KIND,
00055 MHD_HTTP_HEADER_AUTHORIZATION))) ||
00056 (0 != strncmp (header, _BASIC_BASE, strlen(_BASIC_BASE))) )
00057 return NULL;
00058 header += strlen (_BASIC_BASE);
00059 if (NULL == (decode = BASE64Decode (header)))
00060 {
00061 #if HAVE_MESSAGES
00062 MHD_DLOG (connection->daemon,
00063 "Error decoding basic authentication\n");
00064 #endif
00065 return NULL;
00066 }
00067
00068 if (NULL == (separator = strchr (decode, ':')))
00069 {
00070 #if HAVE_MESSAGES
00071 MHD_DLOG(connection->daemon,
00072 "Basic authentication doesn't contain ':' separator\n");
00073 #endif
00074 free (decode);
00075 return NULL;
00076 }
00077 if (NULL == (user = strdup (decode)))
00078 {
00079 free (decode);
00080 return NULL;
00081 }
00082 user[separator - decode] = '\0';
00083 if (NULL != password)
00084 {
00085 *password = strdup (separator + 1);
00086 if (NULL == *password)
00087 {
00088 #if HAVE_MESSAGES
00089 MHD_DLOG(connection->daemon,
00090 "Failed to allocate memory for password\n");
00091 #endif
00092 free (decode);
00093 free (user);
00094 return NULL;
00095 }
00096 }
00097 free (decode);
00098 return user;
00099 }
00100
00101
00109 int
00110 MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
00111 const char *realm,
00112 struct MHD_Response *response)
00113 {
00114 int ret;
00115 size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1;
00116 char header[hlen];
00117
00118 snprintf (header,
00119 sizeof (header),
00120 "Basic realm=\"%s\"",
00121 realm);
00122 ret = MHD_add_response_header (response,
00123 MHD_HTTP_HEADER_WWW_AUTHENTICATE,
00124 header);
00125 if (MHD_YES == ret)
00126 ret = MHD_queue_response (connection,
00127 MHD_HTTP_UNAUTHORIZED,
00128 response);
00129 return ret;
00130 }
00131
00132