* fix server cookie feature
[citadel.git] / webcit / cookie_conversion.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6
7 /*
8  * String to unset the cookie.
9  * Any date "in the past" will work, so I chose my birthday, right down to
10  * the exact minute.  :)
11  */
12 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
13 typedef unsigned char byte;           /* Byte type used by cookie_to_stuff() */
14
15 /*
16  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
17  */
18 void stuff_to_cookie(int unset_cookies)
19 {
20         wcsession *WCC = WC;
21         char buf[SIZ];
22
23         if (unset_cookies) {
24                 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
25         }
26         else
27         {
28                 StrBufAppendPrintf(WCC->HBuf, "Set-cookie: webcit=");
29                 snprintf(buf, sizeof(buf), "%d", WCC->wc_session);
30                 StrBufHexescAppend(WCC->HBuf, NULL, buf);
31                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
32                 StrBufHexescAppend(WCC->HBuf, WCC->wc_username, NULL);
33                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
34                 StrBufHexescAppend(WCC->HBuf, WCC->wc_password, NULL);
35                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
36                 StrBufHexescAppend(WCC->HBuf, WCC->wc_roomname, NULL);
37                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
38                 StrBufHexescAppend(WCC->HBuf, NULL, get_selected_language());
39                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
40
41                 if (server_cookie != NULL) {
42                         StrBufAppendPrintf(WCC->HBuf, 
43                                            ";path=/ \r\n%s\r\n", 
44                                            server_cookie);
45                 }
46                 else {
47                         StrBufAppendBufPlain(WCC->HBuf,
48                                              HKEY("; path=/\r\n"), 0);
49                 }
50         }
51 }
52
53 /*
54  * Convert unpacked hex string to an integer
55  */
56 int xtoi(const char *in, size_t len)
57 {
58         int val = 0;
59         char c = 0;
60         while (!IsEmptyStr(in) && isxdigit((byte) *in) && (len-- > 0))
61         {
62                 c = *in++;
63                 val <<= 4;
64                 if (!isdigit((unsigned char)c)) {
65                         c = tolower((unsigned char) c);
66                         if ((c < 'a') || (c > 'f'))
67                                 return 0;
68                         val += c  - 'a' + 10 ;
69                 }
70                 else
71                         val += c - '0';
72         }
73         return val;
74 }
75
76 /*
77  * Extract all that fun stuff out of the cookie.
78  */
79 void cookie_to_stuff(StrBuf *cookie,
80                         int *session,
81                         StrBuf *user,
82                         StrBuf *pass,
83                         StrBuf *room,
84                         StrBuf *language)
85 {
86         if (session != NULL) {
87                 *session = StrBufExtract_int(cookie, 0, '|');
88         }
89         if (user != NULL) {
90                 StrBufExtract_token(user, cookie, 1, '|');
91         }
92         if (pass != NULL) {
93                 StrBufExtract_token(pass, cookie, 2, '|');
94         }
95         if (room != NULL) {
96                 StrBufExtract_token(room, cookie, 3, '|');
97         }
98         if (language != NULL) {
99                 StrBufExtract_token(language, cookie, 4, '|');
100         }
101 }