Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 287 lines (225 sloc) 6.891 kb
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include "common-internal.h"
#include "validator_htdigest.h"
#include "plugin_loader.h"
#include "connection-protected.h"
#include "thread.h"

/* Plug-in initialization
*/
PLUGIN_INFO_VALIDATOR_EASIEST_INIT (htdigest, http_auth_basic | http_auth_digest);


static ret_t
props_free (cherokee_validator_htdigest_props_t *props)
{
return cherokee_validator_file_props_free_base (PROP_VFILE(props));
}


ret_t
cherokee_validator_htdigest_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
{
cherokee_validator_htdigest_props_t *props;

UNUSED(srv);

if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, validator_htdigest_props);

cherokee_validator_file_props_init_base (PROP_VFILE(n),
MODULE_PROPS_FREE(props_free));
*_props = MODULE_PROPS(n);
}

props = PROP_HTDIGEST(*_props);

/* Call the file based validator configure
*/
return cherokee_validator_file_configure (conf, srv, _props);
}


ret_t
cherokee_validator_htdigest_new (cherokee_validator_htdigest_t **htdigest, cherokee_module_props_t *props)
{
CHEROKEE_NEW_STRUCT(n,validator_htdigest);

/* Init
*/
cherokee_validator_file_init_base (VFILE(n),
PROP_VFILE(props),
PLUGIN_INFO_VALIDATOR_PTR(htdigest));

VALIDATOR(n)->support = http_auth_basic | http_auth_digest;

MODULE(n)->free = (module_func_free_t) cherokee_validator_htdigest_free;
VALIDATOR(n)->check = (validator_func_check_t) cherokee_validator_htdigest_check;
VALIDATOR(n)->add_headers = (validator_func_add_headers_t) cherokee_validator_htdigest_add_headers;

/* Return obj
*/
*htdigest = n;
return ret_ok;
}


ret_t
cherokee_validator_htdigest_free (cherokee_validator_htdigest_t *htdigest)
{
return cherokee_validator_file_free_base (VFILE(htdigest));
}


static ret_t
build_HA1 (cherokee_connection_t *conn, cherokee_buffer_t *buf)
{
cherokee_buffer_add_va (buf, "%s:%s:%s", conn->validator->user.buf, conn->config_entry.auth_realm->buf, conn->validator->passwd.buf);
cherokee_buffer_encode_md5_digest (buf);
return ret_ok;
}


static ret_t
extract_user_entry (cherokee_buffer_t *file, char *user_, char **user, char **realm, char **passwd)
{
char *end = file->buf + file->len;
char *pos = file->buf;
int user_len = strlen (user_);

while (pos < end) {
char *eol;

/* Limit the line
*/
eol = strchr (pos, CHR_LF);
if (eol != NULL)
*eol = '\0';

/* Check the user
*/
if ((pos[user_len] == ':') &&
(strncmp (pos, user_, user_len) == 0)) {
char *tmp;

*user = pos;

tmp = strchr(pos, ':');
if (!tmp)
return ret_error;
*tmp = '\0';
*realm = tmp + 1;

tmp = strchr (*realm, ':');
if (!tmp)
return ret_error;
*tmp = '\0';
*passwd = tmp + 1;

return ret_ok;
}

/* Look for the next line, or exit
*/
if (eol == NULL)
break;

pos = eol;
*eol = CHR_LF;
while ((*pos == CHR_CR) || (*pos == CHR_LF)) pos++;
}

return ret_not_found;
}


static ret_t
validate_basic (cherokee_validator_htdigest_t *htdigest, cherokee_connection_t *conn, cherokee_buffer_t *file)
{
ret_t ret;
cherokee_boolean_t equal;
char *user = NULL;
char *realm = NULL;
char *passwd = NULL;
cherokee_buffer_t ha1 = CHEROKEE_BUF_INIT;

UNUSED(htdigest);

/* Extact the right entry information
*/
ret = extract_user_entry (file, conn->validator->user.buf, &user, &realm, &passwd);
if (ret != ret_ok)
return ret;

/* Build the hash
*/
build_HA1 (conn, &ha1);

/* Compare it with the stored hash, clean, and return
*/
equal = (strncmp(ha1.buf, passwd, ha1.len) == 0);
cherokee_buffer_mrproper (&ha1);

return (equal) ? ret_ok : ret_not_found;
}


static ret_t
validate_digest (cherokee_validator_htdigest_t *htdigest, cherokee_connection_t *conn, cherokee_buffer_t *file)
{
ret_t ret;
int re = -1;
char *user = NULL;
char *realm = NULL;
char *passwd = NULL;
cherokee_buffer_t buf = CHEROKEE_BUF_INIT;

/* Sanity check
*/
if (cherokee_buffer_is_empty (&conn->validator->response))
return ret_error;

/* Extact the right entry information
*/
ret = extract_user_entry (file, conn->validator->user.buf, &user, &realm, &passwd);
if (unlikely(ret != ret_ok))
return ret;

/* Build the hash:
* In this case passwd is the HA1 hash: md5(user:realm:passwd)
*/
ret = cherokee_validator_digest_response (VALIDATOR(htdigest), passwd, &buf, conn);
if (unlikely(ret != ret_ok))
goto go_out;

/* Compare and return
*/
re = cherokee_buffer_cmp_buf (&conn->validator->response, &buf);

go_out:
cherokee_buffer_mrproper (&buf);
return (re == 0) ? ret_ok : ret_deny;
}


ret_t
cherokee_validator_htdigest_check (cherokee_validator_htdigest_t *htdigest,
cherokee_connection_t *conn)
{
ret_t ret;
cherokee_buffer_t *fpass;
cherokee_buffer_t file = CHEROKEE_BUF_INIT;

/* Ensure that we have all what we need
*/
if ((conn->validator == NULL) ||
cherokee_buffer_is_empty (&conn->validator->user))
return ret_error;

/* Get the full path to the file
*/
ret = cherokee_validator_file_get_full_path (VFILE(htdigest), conn, &fpass,
&CONN_THREAD(conn)->tmp_buf1);
if (ret != ret_ok) {
ret = ret_error;
goto out;
}

/* Read the whole file
*/
ret = cherokee_buffer_read_file (&file, fpass->buf);
if (ret != ret_ok) {
ret = ret_error;
goto out;
}

/* Authenticate
*/
if (conn->req_auth_type & http_auth_basic) {
ret = validate_basic (htdigest, conn, &file);

} else if (conn->req_auth_type & http_auth_digest) {
ret = validate_digest (htdigest, conn, &file);

} else {
SHOULDNT_HAPPEN;
}

out:
cherokee_buffer_mrproper (&file);
return ret;
}


ret_t
cherokee_validator_htdigest_add_headers (cherokee_validator_htdigest_t *htdigest, cherokee_connection_t *conn, cherokee_buffer_t *buf)
{
UNUSED(htdigest);
UNUSED(conn);
UNUSED(buf);

return ret_ok;
}

Something went wrong with that request. Please try again.