844798ea06bf94673561d6e1b7ca736e9e21ccf4
[citadel.git] / webcit / cookie_conversion.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup CookieConversion Grep Cookies
6  * Utility functions which convert the HTTP cookie format we use to and
7  * from user/password/room strings.
8  *
9  * \ingroup WebcitHttpServer 
10  */
11 /*@{*/
12 #include "webcit.h"
13
14
15 #define TRUE  1    /**< for sure? */
16 #define FALSE 0    /**< nope. */
17
18 typedef unsigned char byte;           /**< Byte type */
19
20 /**
21  * \brief find cookie
22  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
23  * \param cookie cookie string to create???
24  * \param session the session we want to convert into a cookie
25  * \param user the user to be associated with the cookie
26  * \param pass his passphrase
27  * \param room the room he wants to enter
28  */
29 void stuff_to_cookie(char *cookie, size_t clen, int session,
30                 char *user, char *pass, char *room)
31 {
32         char buf[SIZ];
33         int i;
34         int len;
35
36         len = snprintf(buf, SIZ, "%d|%s|%s|%s|", session, user, pass, room);
37         strcpy(cookie, "");
38         for (i=0; (i < len) && (i * 2 < clen); ++i) {
39                 snprintf(&cookie[i*2], clen - i * 2, "%02X", buf[i]);
40         }
41 }
42
43 /**
44  * \brief       Convert unpacked hex string to an integer
45  * \param       in      Input hex string
46  * \param       len     the length of the string
47  * \return      the corrosponding integer value
48  */
49 int xtoi(const char *in, size_t len)
50 {
51         int val = 0;
52         char c = 0;
53         while (!IsEmptyStr(in) && isxdigit((byte) *in) && (len-- > 0))
54         {
55                 c = *in++;
56                 val <<= 4;
57                 if (!isdigit((unsigned char)c)) {
58                         c = tolower((unsigned char) c);
59                         if ((c < 'a') || (c > 'f'))
60                                 return 0;
61                         val += c  - 'a' + 10 ;
62                 }
63                 else
64                         val += c - '0';
65         }
66         return val;
67 }
68
69 /**
70  * \brief Extract all that fun stuff out of the cookie.
71  * \param cookie the cookie string
72  * \param session the corrosponding session to return
73  * \param user the user string
74  * \param user_len the user stringlength
75  * \param pass the passphrase
76  * \param pass_len length of the passphrase string 
77  * \param room the room he is in
78  * \param room_len the length of the room string
79  */
80 void cookie_to_stuff(StrBuf *cookie, int *session,
81                 char *user, size_t user_len,
82                 char *pass, size_t pass_len,
83                 char *room, size_t room_len)
84 {
85         const char *pch;
86         char buf[SIZ];
87         int i, len;
88
89         pch = strstr(ChrPtr(cookie), "webcit=");
90         
91         if (pch != NULL)
92                 StrBufCutLeft(cookie, (pch - ChrPtr(cookie)) + 7);
93
94         strcpy(buf, "");
95         len = StrLength(cookie) / 2;
96         pch = ChrPtr(cookie);
97         for (i=0; i<len; ++i) {
98                 buf[i] = xtoi(&pch[i*2], 2);
99                 buf[i+1] = 0;
100         }
101
102 /* debug
103         char t[256];
104         extract_token(t, buf, 0, '|', sizeof t);
105         lprintf(9, "SESS: %s\n", t);
106         extract_token(t, buf, 1, '|', sizeof t);
107         lprintf(9, "USER: %s\n", t);
108         extract_token(t, buf, 2, '|', sizeof t);
109         lprintf(9, "PASS: %s\n", t);
110         extract_token(t, buf, 3, '|', sizeof t);
111         lprintf(9, "ROOM: %s\n", t);
112  debug */
113
114         if (session != NULL)
115                 *session = extract_int(buf, 0);
116         if (user != NULL)
117                 extract_token(user, buf, 1, '|', user_len);
118         if (pass != NULL)
119                 extract_token(pass, buf, 2, '|', pass_len);
120         if (room != NULL)
121                 extract_token(room, buf, 3, '|', room_len);
122 }
123 /*@}*/