aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/basicauth.c
blob: f1e38af4f1b706643bdd15bab53bf1728b1f12ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
     This file is part of libmicrohttpd
     Copyright (C) 2010, 2011, 2012 Daniel Pittman and Christian Grothoff

     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later version.

     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.

     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * @file basicauth.c
 * @brief Implements HTTP basic authentication methods
 * @author Amr Ali
 * @author Matthieu Speder
 */
#include "platform.h"
#include "mhd_limits.h"
#include "internal.h"
#include "base64.h"
#include "mhd_compat.h"

/**
 * Beginning string for any valid Basic authentication header.
 */
#define _BASIC_BASE   "Basic "


/**
 * Get the username and password from the basic authorization header sent by the client
 *
 * @param connection The MHD connection structure
 * @param password a pointer for the password
 * @return NULL if no username could be found, a pointer
 *      to the username if found
 * @ingroup authentication
 */
char *
MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
                                      char**password)
{
  const char *header;
  char *decode;
  const char *separator;
  char *user;

  if ( (MHD_NO == MHD_lookup_connection_value_n (connection,
                                                 MHD_HEADER_KIND,
                                                 MHD_HTTP_HEADER_AUTHORIZATION,
                                                 MHD_STATICSTR_LEN_ (
                                                   MHD_HTTP_HEADER_AUTHORIZATION),
                                                 &header,
                                                 NULL)) ||
       (0 != strncmp (header,
                      _BASIC_BASE,
                      MHD_STATICSTR_LEN_ (_BASIC_BASE))) )
    return NULL;
  header += MHD_STATICSTR_LEN_ (_BASIC_BASE);
  if (NULL == (decode = BASE64Decode (header)))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (connection->daemon,
              _ ("Error decoding basic authentication\n"));
#endif
    return NULL;
  }
  /* Find user:password pattern */
  if (NULL == (separator = strchr (decode,
                                   ':')))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (connection->daemon,
              _ ("Basic authentication doesn't contain ':' separator\n"));
#endif
    free (decode);
    return NULL;
  }
  if (NULL == (user = strdup (decode)))
  {
    free (decode);
    return NULL;
  }
  user[separator - decode] = '\0'; /* cut off at ':' */
  if (NULL != password)
  {
    *password = strdup (separator + 1);
    if (NULL == *password)
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (connection->daemon,
                _ ("Failed to allocate memory for password\n"));
#endif
      free (decode);
      free (user);
      return NULL;
    }
  }
  free (decode);
  return user;
}


/**
 * Queues a response to request basic authentication from the client.
 * The given response object is expected to include the payload for
 * the response; the "WWW-Authenticate" header will be added and the
 * response queued with the 'UNAUTHORIZED' status code.
 *
 * @param connection The MHD connection structure
 * @param realm the realm presented to the client
 * @param response response object to modify and queue
 * @return #MHD_YES on success, #MHD_NO otherwise
 * @ingroup authentication
 */
int
MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
                                    const char *realm,
                                    struct MHD_Response *response)
{
  int ret;
  int res;
  size_t hlen = strlen (realm) + strlen ("Basic realm=\"\"") + 1;
  char *header;

  header = (char *) malloc (hlen);
  if (NULL == header)
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (connection->daemon,
              "Failed to allocate memory for auth header\n");
#endif /* HAVE_MESSAGES */
    return MHD_NO;
  }
  res = MHD_snprintf_ (header,
                       hlen,
                       "Basic realm=\"%s\"",
                       realm);
  if ((res > 0)&&((size_t) res < hlen))
    ret = MHD_add_response_header (response,
                                   MHD_HTTP_HEADER_WWW_AUTHENTICATE,
                                   header);
  else
    ret = MHD_NO;

  free (header);
  if (MHD_YES == ret)
  {
    ret = MHD_queue_response (connection,
                              MHD_HTTP_UNAUTHORIZED,
                              response);
  }
  else
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (connection->daemon,
              _ ("Failed to add Basic auth header\n"));
#endif /* HAVE_MESSAGES */
  }
  return ret;
}

/* end of basicauth.c */