* Remove leading '|' from references if present
[citadel.git] / webcit / cookie_conversion.c
blob172536fc4f6409376eacefbbfd76c03ba3ac4103
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.
9 * \ingroup WebcitHttpServer
11 /*@{*/
12 #include "webcit.h"
15 #define TRUE 1 /**< for sure? */
16 #define FALSE 0 /**< nope. */
18 typedef unsigned char byte; /**< Byte type */
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
29 void stuff_to_cookie(char *cookie, size_t clen, int session,
30 StrBuf *user, StrBuf *pass, StrBuf *room)
32 char buf[SIZ];
33 int i;
34 int len;
36 len = snprintf(buf, SIZ, "%d|%s|%s|%s|",
37 session,
38 ChrPtr(user),
39 ChrPtr(pass),
40 ChrPtr(room));
42 strcpy(cookie, "");
43 for (i=0; (i < len) && (i * 2 < clen); ++i) {
44 snprintf(&cookie[i*2], clen - i * 2, "%02X", buf[i]);
48 /**
49 * \brief Convert unpacked hex string to an integer
50 * \param in Input hex string
51 * \param len the length of the string
52 * \return the corrosponding integer value
54 int xtoi(const char *in, size_t len)
56 int val = 0;
57 char c = 0;
58 while (!IsEmptyStr(in) && isxdigit((byte) *in) && (len-- > 0))
60 c = *in++;
61 val <<= 4;
62 if (!isdigit((unsigned char)c)) {
63 c = tolower((unsigned char) c);
64 if ((c < 'a') || (c > 'f'))
65 return 0;
66 val += c - 'a' + 10 ;
68 else
69 val += c - '0';
71 return val;
74 /**
75 * \brief Extract all that fun stuff out of the cookie.
76 * \param cookie the cookie string
77 * \param session the corrosponding session to return
78 * \param user the user string
79 * \param user_len the user stringlength
80 * \param pass the passphrase
81 * \param pass_len length of the passphrase string
82 * \param room the room he is in
83 * \param room_len the length of the room string
85 void cookie_to_stuff(StrBuf *cookie, int *session,
86 StrBuf *user,
87 StrBuf *pass,
88 StrBuf *room)
90 const char *pch;
91 char buf[SIZ];
92 StrBuf *Buf;
93 int i, len;
95 pch = strstr(ChrPtr(cookie), "webcit=");
97 if (pch != NULL)
98 StrBufCutLeft(cookie, (pch - ChrPtr(cookie)) + 7);
100 strcpy(buf, "");
101 len = StrLength(cookie) / 2;
102 pch = ChrPtr(cookie);
103 for (i=0; i<len; ++i) {
104 buf[i] = xtoi(&pch[i*2], 2);
105 buf[i+1] = 0;
107 Buf = NewStrBufPlain(buf, i);
109 /* debug
110 char t[256];
111 extract_token(t, buf, 0, '|', sizeof t);
112 lprintf(9, "SESS: %s\n", t);
113 extract_token(t, buf, 1, '|', sizeof t);
114 lprintf(9, "USER: %s\n", t);
115 extract_token(t, buf, 2, '|', sizeof t);
116 lprintf(9, "PASS: %s\n", t);
117 extract_token(t, buf, 3, '|', sizeof t);
118 lprintf(9, "ROOM: %s\n", t);
119 debug */
121 if (session != NULL)
122 *session = StrBufExtract_int(Buf, 0, '|');
123 if (user != NULL)
124 StrBufExtract_token(user, Buf, 1, '|');
125 if (pass != NULL)
126 StrBufExtract_token(pass, Buf, 2, '|');
127 if (room != NULL)
128 StrBufExtract_token(room, Buf, 3, '|');
129 FreeStrBuf(&Buf);
131 /*@}*/