basicauth.c

Go to the documentation of this file.
00001 /*
00002      This file is part of libmicrohttpd
00003      (C) 2010, 2011, 2012 Daniel Pittman and Christian Grothoff
00004 
00005      This library is free software; you can redistribute it and/or
00006      modify it under the terms of the GNU Lesser General Public
00007      License as published by the Free Software Foundation; either
00008      version 2.1 of the License, or (at your option) any later version.
00009 
00010      This library is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013      Lesser General Public License for more details.
00014 
00015      You should have received a copy of the GNU Lesser General Public
00016      License along with this library; if not, write to the Free Software
00017      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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   /* Find user:password pattern */
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'; /* cut off at ':' */
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 /* end of basicauth.c */

Generated on Thu Sep 27 17:55:14 2012 for GNU libmicrohttpd by  doxygen 1.4.7