* Brought over the newer string tokenizer from Citadel
[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 static byte dtable[SIZ];              /* base64 encode / decode table */
28
29 /*
30  * decode_base64() and encode_base64() are adaptations of code by
31  * John Walker, found in full in the file "base64.c" included with the Citadel
32  * server.  The difference between those functions and these is that
33  * these are intended to encode/decode small string buffers, and those are
34  * intended to encode/decode entire MIME parts.
35  */
36
37 void encode_base64(char *dest, char *source)
38 {
39     int i, hiteof = FALSE;
40     int spos = 0;
41     int dpos = 0;
42
43     /*  Fill dtable with character encodings.  */
44
45     for (i = 0; i < 26; i++) {
46         dtable[i] = 'A' + i;
47         dtable[26 + i] = 'a' + i;
48     }
49     for (i = 0; i < 10; i++) {
50         dtable[52 + i] = '0' + i;
51     }
52     dtable[62] = '+';
53     dtable[63] = '/';
54
55     while (!hiteof) {
56         byte igroup[3], ogroup[4];
57         int c, n;
58
59         igroup[0] = igroup[1] = igroup[2] = 0;
60         for (n = 0; n < 3; n++) {
61             c = source[spos++];
62             if (c == 0) {
63                 hiteof = TRUE;
64                 break;
65             }
66             igroup[n] = (byte) c;
67         }
68         if (n > 0) {
69             ogroup[0] = dtable[igroup[0] >> 2];
70             ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
71             ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
72             ogroup[3] = dtable[igroup[2] & 0x3F];
73
74             /* Replace characters in output stream with "=" pad
75                characters if fewer than three characters were
76                read from the end of the input stream. */
77
78             if (n < 3) {
79                 ogroup[3] = '=';
80                 if (n < 2) {
81                     ogroup[2] = '=';
82                 }
83             }
84             for (i = 0; i < 4; i++) {
85                 dest[dpos++] = ogroup[i];
86                 dest[dpos] = 0;
87             }
88         }
89     }
90 }
91
92
93
94 void decode_base64(char *dest, char *source)
95 {
96     int i;
97     int dpos = 0;
98     int spos = 0;
99
100     for (i = 0; i < 255; i++) {
101         dtable[i] = 0x80;
102     }
103     for (i = 'A'; i <= 'Z'; i++) {
104         dtable[i] = 0 + (i - 'A');
105     }
106     for (i = 'a'; i <= 'z'; i++) {
107         dtable[i] = 26 + (i - 'a');
108     }
109     for (i = '0'; i <= '9'; i++) {
110         dtable[i] = 52 + (i - '0');
111     }
112     dtable['+'] = 62;
113     dtable['/'] = 63;
114     dtable['='] = 0;
115
116     /*CONSTANTCONDITION*/
117     while (TRUE) {
118         byte a[4], b[4], o[3];
119
120         for (i = 0; i < 4; i++) {
121             int c = source[spos++];
122
123             if (c == 0) {
124                 if (i > 0) {
125                     return;
126                 }
127                 return;
128             }
129             if (dtable[c] & 0x80) {
130                 /* Ignoring errors: discard invalid character. */
131                 i--;
132                 continue;
133             }
134             a[i] = (byte) c;
135             b[i] = (byte) dtable[c];
136         }
137         o[0] = (b[0] << 2) | (b[1] >> 4);
138         o[1] = (b[1] << 4) | (b[2] >> 2);
139         o[2] = (b[2] << 6) | b[3];
140         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
141         if (i>=1) dest[dpos++] = o[0];
142         if (i>=2) dest[dpos++] = o[1];
143         if (i>=3) dest[dpos++] = o[2];
144         dest[dpos] = 0;
145         if (i < 3) {
146             return;
147         }
148     }
149 }
150
151
152
153
154
155 /*
156  * Pack all session info into one easy-to-digest cookie.  Healthy and delicious!
157  */
158 void stuff_to_cookie(char *cookie, int session, char *user, char *pass, char *room)
159 {
160         char buf[SIZ];
161
162         sprintf(buf, "%d|%s|%s|%s", session, user, pass, room);
163         encode_base64(cookie, buf);
164 }
165
166
167 /*
168  * Extract all that fun stuff out of the cookie.
169  */
170 void cookie_to_stuff(char *cookie, int *session, char *user, char *pass, char *room)
171 {
172         char buf[SIZ];
173
174         decode_base64(buf, cookie);
175
176         if (session != NULL)
177                 *session = extract_int(buf, 0);
178         if (user != NULL)
179                 extract(user, buf, 1);
180         if (pass != NULL)
181                 extract(pass, buf, 2);
182         if (room != NULL)
183                 extract(room, buf, 3);
184 }