chiark / gitweb /
Web interface starts to reflect user rights properly:
[disorder] / lib / cookies.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/cookies.c
21  * @brief Cookie support
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <time.h>
32 #include <gcrypt.h>
33 #include <pcre.h>
34
35 #include "rights.h"
36 #include "cookies.h"
37 #include "hash.h"
38 #include "mem.h"
39 #include "log.h"
40 #include "printf.h"
41 #include "base64.h"
42 #include "configuration.h"
43 #include "kvp.h"
44 #include "rights.h"
45 #include "trackdb.h"
46
47 /** @brief Hash function used in signing HMAC */
48 #define ALGO GCRY_MD_SHA1
49
50 /** @brief Size of key to use */
51 #define HASHSIZE 20
52
53 /** @brief Signing key */
54 static uint8_t signing_key[HASHSIZE];
55
56 /** @brief Previous signing key */
57 static uint8_t old_signing_key[HASHSIZE];
58
59 /** @brief Signing key validity limit or 0 if none */
60 static time_t signing_key_validity_limit;
61
62 /** @brief Hash of revoked cookies */
63 static hash *revoked;
64
65 /** @brief Callback to expire revocation list */
66 static int revoked_cleanup_callback(const char *key, void *value,
67                                     void *u) {
68   if(*(time_t *)value < *(time_t *)u)
69     hash_remove(revoked, key);
70   return 0;
71 }
72
73 /** @brief Generate a new key */
74 static void newkey(void) {
75   time_t now;
76
77   time(&now);
78   memcpy(old_signing_key, signing_key, HASHSIZE);
79   gcry_randomize(signing_key, HASHSIZE, GCRY_STRONG_RANDOM);
80   signing_key_validity_limit = now + config->cookie_key_lifetime;
81   /* Now is a good time to clean up the revocation list... */
82   if(revoked)
83     hash_foreach(revoked, revoked_cleanup_callback, &now);
84 }
85
86 /** @brief Sign @p subject with @p key and return the base64 of the result
87  * @param key Key to sign with (@ref HASHSIZE bytes)
88  * @param subject Subject string
89  * @return Base64-encoded signature or NULL
90  */
91 static char *sign(const uint8_t *key,
92                   const char *subject) {
93   gcry_error_t e;
94   gcry_md_hd_t h;
95   uint8_t *sig;
96   char *sig64;
97
98   if((e = gcry_md_open(&h, ALGO, GCRY_MD_FLAG_HMAC))) {
99     error(0, "gcry_md_open: %s", gcry_strerror(e));
100     return 0;
101   }
102   if((e = gcry_md_setkey(h, key, HASHSIZE))) {
103     error(0, "gcry_md_setkey: %s", gcry_strerror(e));
104     gcry_md_close(h);
105     return 0;
106   }
107   gcry_md_write(h, subject, strlen(subject));
108   sig = gcry_md_read(h, ALGO);
109   sig64 = mime_to_base64(sig, HASHSIZE);
110   gcry_md_close(h);
111   return sig64;
112 }
113
114 /** @brief Create a login cookie
115  * @param user Username
116  * @return Cookie or NULL
117  */
118 char *make_cookie(const char *user) {
119   const char *password;
120   time_t now;
121   char *b, *bp, *c, *g;
122
123   /* semicolons aren't allowed in usernames */
124   if(strchr(user, ';')) {
125     error(0, "make_cookie for username with semicolon");
126     return 0;
127   }
128   /* look up the password */
129   password = trackdb_get_password(user);
130   if(!password) {
131     error(0, "make_cookie for nonexistent user");
132     return 0;
133   }
134   /* make sure we have a valid signing key */
135   time(&now);
136   if(now >= signing_key_validity_limit)
137     newkey();
138   /* construct the subject */
139   byte_xasprintf(&b, "%jx;%s;", (intmax_t)now + config->cookie_login_lifetime,
140                  urlencodestring(user));
141   byte_xasprintf(&bp, "%s%s", b, password);
142   /* sign it */
143   if(!(g = sign(signing_key, bp)))
144     return 0;
145   /* put together the final cookie */
146   byte_xasprintf(&c, "%s%s", b, g);
147   return c;
148 }
149
150 /** @brief Verify a cookie
151  * @param cookie Cookie to verify
152  * @param rights Where to store rights value
153  * @return Verified user or NULL
154  */
155 char *verify_cookie(const char *cookie, rights_type *rights) {
156   char *c1, *c2;
157   intmax_t t;
158   time_t now;
159   char *user, *bp, *sig;
160   const char *password;
161   struct kvp *k;
162
163   /* check the revocation list */
164   if(revoked && hash_find(revoked, cookie)) {
165     error(0, "attempt to log in with revoked cookie");
166     return 0;
167   }
168   /* parse the cookie */
169   errno = 0;
170   t = strtoimax(cookie, &c1, 16);
171   if(errno) {
172     error(errno, "error parsing cookie timestamp");
173     return 0;
174   }
175   if(*c1 != ';') {
176     error(0, "invalid cookie timestamp");
177     return 0;
178   }
179   /* There'd better be two semicolons */
180   c2 = strchr(c1 + 1, ';');
181   if(c2 == 0) {
182     error(0, "invalid cookie syntax");
183     return 0;
184   }
185   /* Extract the username */
186   user = xstrndup(c1 + 1, c2 - (c1 + 1));
187   /* check expiry */
188   time(&now);
189   if(now >= t) {
190     error(0, "cookie has expired");
191     return 0;
192   }
193   /* look up the password */
194   k = trackdb_getuserinfo(user);
195   if(!k) {
196     error(0, "verify_cookie for nonexistent user");
197     return 0;
198   }
199   password = kvp_get(k, "password");
200   if(!password) password = "";
201   if(parse_rights(kvp_get(k, "rights"), rights, 1))
202     return 0;
203   /* construct the expected subject.  We re-encode the timestamp and the
204    * password. */
205   byte_xasprintf(&bp, "%jx;%s;%s", t, urlencodestring(user), password);
206   /* Compute the expected signature.  NB we base64 the expected signature and
207    * compare that rather than exposing our base64 parser to the cookie. */
208   if(!(sig = sign(signing_key, bp)))
209     return 0;
210   if(!strcmp(sig, c2 + 1))
211     return user;
212   /* that didn't match, try the old key */
213   if(!(sig = sign(old_signing_key, bp)))
214     return 0;
215   if(!strcmp(sig, c2 + 1))
216     return user;
217   /* that didn't match either */
218   error(0, "cookie signature does not match");
219   return 0;
220 }
221
222 /** @brief Revoke a cookie
223  * @param cookie Cookie to revoke
224  *
225  * Further attempts to log in with @p cookie will fail.
226  */
227 void revoke_cookie(const char *cookie) {
228   time_t when;
229   char *ptr;
230
231   /* find the cookie's expiry time */
232   errno = 0;
233   when = (time_t)strtoimax(cookie, &ptr, 16);
234   /* reject bogus cookies */
235   if(errno)
236     return;
237   if(*ptr != ';')
238     return;
239   /* make sure the revocation list exists */
240   if(!revoked)
241     revoked = hash_new(sizeof(time_t));
242   /* add the cookie to it; its value is the expiry time */
243   hash_add(revoked, cookie, &when, HASH_INSERT);
244 }
245
246 /*
247 Local Variables:
248 c-basic-offset:2
249 comment-column:40
250 fill-column:79
251 indent-tabs-mode:nil
252 End:
253 */