Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 197 lines (155 sloc) 4.534 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
/* -*- 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 "nonce.h"
#include "avl.h"
#include "connection-protected.h"
#include "server-protected.h"
#include "bogotime.h"

struct cherokee_nonce_table {
cherokee_avl_t table;
cherokee_list_t list;
CHEROKEE_MUTEX_T (access);
};

typedef struct {
cherokee_list_t listed;
cherokee_buffer_t nonce;
time_t accessed;
} entry_t;


static entry_t *
entry_new (void)
{
entry_t *entry;

entry = (entry_t *) malloc(sizeof(entry_t));
if (unlikely (entry == NULL))
return NULL;

entry->accessed = cherokee_bogonow_now;
INIT_LIST_HEAD (&entry->listed);
cherokee_buffer_init (&entry->nonce);

return entry;
}

static void
entry_free (cherokee_nonce_table_t *nonces,
entry_t *entry)
{
cherokee_list_del (&entry->listed);
cherokee_avl_del (&nonces->table, &entry->nonce, NULL);
cherokee_buffer_mrproper (&entry->nonce);
free (entry);
}


ret_t
cherokee_nonce_table_new (cherokee_nonce_table_t **nonces)
{
CHEROKEE_NEW_STRUCT(n, nonce_table);

cherokee_avl_init (&n->table);
CHEROKEE_MUTEX_INIT (&n->access, NULL);
INIT_LIST_HEAD (&n->list);

*nonces = n;
return ret_ok;
}


ret_t
cherokee_nonce_table_free (cherokee_nonce_table_t *nonces)
{
cherokee_list_t *i, *j;

list_for_each_safe (i, j, &nonces->list) {
entry_free (nonces, (entry_t *)i);
}

cherokee_avl_mrproper (&nonces->table, NULL);
CHEROKEE_MUTEX_DESTROY (&nonces->access);

free (nonces);
return ret_ok;
}


ret_t
cherokee_nonce_table_remove (cherokee_nonce_table_t *nonces,
cherokee_buffer_t *nonce)
{
ret_t ret;
entry_t *entry = NULL;

CHEROKEE_MUTEX_LOCK (&nonces->access);
ret = cherokee_avl_get (&nonces->table, nonce, (void **)&entry);
if (ret == ret_ok) {
entry_free (nonces, entry);
}
CHEROKEE_MUTEX_UNLOCK (&nonces->access);

if (ret != ret_ok) return ret_not_found;
return ret_ok;
}


ret_t
cherokee_nonce_table_check (cherokee_nonce_table_t *nonces,
cherokee_buffer_t *nonce)
{
ret_t ret;
entry_t *entry = NULL;

if (cherokee_buffer_is_empty (nonce))
return ret_not_found;

CHEROKEE_MUTEX_LOCK (&nonces->access);
ret = cherokee_avl_get (&nonces->table, nonce, (void **)&entry);
CHEROKEE_MUTEX_UNLOCK (&nonces->access);

if ((ret != ret_ok) || (entry == NULL))
return ret_not_found;

entry->accessed = cherokee_bogonow_now;
return ret_ok;
}


ret_t
cherokee_nonce_table_generate (cherokee_nonce_table_t *nonces,
cherokee_connection_t *conn,
cherokee_buffer_t *nonce)
{
entry_t *entry;

/* Generate nonce string
*/
cherokee_buffer_add_ullong16(nonce, (cullong_t) cherokee_bogonow_now);
cherokee_buffer_add_ulong16 (nonce, (culong_t) rand());
cherokee_buffer_add_ulong16 (nonce, (culong_t) POINTER_TO_INT(conn));

/* Compute MD5 and overwrite buffer content without reallocating it !
*/
cherokee_buffer_encode_md5_digest (nonce);

/* Copy the nonce and add to the table
*/
entry = entry_new();
if (unlikely (entry == NULL))
return ret_nomem;

cherokee_buffer_add_buffer (&entry->nonce, nonce);

CHEROKEE_MUTEX_LOCK (&nonces->access);
cherokee_avl_add (&nonces->table, nonce, entry);
cherokee_list_add (&entry->listed, &nonces->list);
CHEROKEE_MUTEX_UNLOCK (&nonces->access);

/* Return
*/
return ret_ok;
}


ret_t
cherokee_nonce_table_cleanup (cherokee_nonce_table_t *nonces)
{
cherokee_list_t *i, *j;
entry_t *entry;

CHEROKEE_MUTEX_LOCK (&nonces->access);
list_for_each_safe (i, j, &nonces->list) {
entry = (entry_t *)i;
if (entry->accessed + NONCE_EXPIRATION < cherokee_bogonow_now) {
entry_free (nonces, entry);
}
}
CHEROKEE_MUTEX_UNLOCK (&nonces->access);

return ret_ok;
}
Something went wrong with that request. Please try again.