* Hmmph. Do the session cookie as hex instead of base64. There really
[citadel.git] / webcit / cookie_conversion.c
1
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/socket.h>
11 #include <sys/time.h>
12 #include <limits.h>
13 #include <netinet/in.h>
14 #include <netdb.h>
15 #include <string.h>
16 #include <pwd.h>
17 #include <errno.h>
18 #include <stdarg.h>
19 #include <pthread.h>
20 #include <signal.h>
21 #include "webcit.h"
22
23 #define TRUE  1
24 #define FALSE 0
25
26 typedef unsigned char byte;           /* Byte type */
27
28 /*
29  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
30  */
31 void stuff_to_cookie(char *cookie, int session,
32                 char *user, char *pass, char *room)
33 {
34         char buf[SIZ];
35         int i;
36
37         sprintf(buf, "%d|%s|%s|%s|", session, user, pass, room);
38         strcpy(cookie, "");
39         for (i=0; i<strlen(buf); ++i) {
40                 sprintf(&cookie[i*2], "%02X", buf[i]);
41         }
42 }
43
44 int xtoi(char *in, size_t len)
45 {
46     int val = 0;
47     while (isxdigit((byte) *in) && (len-- > 0)) {
48         char c = *in++;
49         val <<= 4;
50         val += isdigit((unsigned char)c)
51             ? (c - '0')
52             : (tolower((unsigned char)c) - 'a' + 10);
53     }
54     return val;
55 }
56
57 /*
58  * Extract all that fun stuff out of the cookie.
59  */
60 void cookie_to_stuff(char *cookie, int *session, char *user, char *pass, char *room)
61 {
62         char buf[SIZ];
63         int i, len;
64
65         strcpy(buf, "");
66         len = strlen(cookie) * 2 ;
67         for (i=0; i<len; ++i) {
68                 buf[i] = xtoi(&cookie[i*2], 2);
69                 buf[i+1] = 0;
70         }
71
72         if (session != NULL)
73                 *session = extract_int(buf, 0);
74         if (user != NULL)
75                 extract(user, buf, 1);
76         if (pass != NULL)
77                 extract(pass, buf, 2);
78         if (room != NULL)
79                 extract(room, buf, 3);
80 }