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