* Changed decode_base64() to CtdlDecodeBase64() to avoid conflict with
[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  * CtdlDecodeBase64() 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  * Convert base64-encoded to binary.  Returns the length of the decoded data.
95  * It will stop after reading 'length' bytes.
96  */
97 int CtdlDecodeBase64(char *dest, char *source, size_t length)
98 {
99     int i, c;
100     int dpos = 0;
101     int spos = 0;
102
103     for (i = 0; i < 255; i++) {
104         dtable[i] = 0x80;
105     }
106     for (i = 'A'; i <= 'Z'; i++) {
107         dtable[i] = 0 + (i - 'A');
108     }
109     for (i = 'a'; i <= 'z'; i++) {
110         dtable[i] = 26 + (i - 'a');
111     }
112     for (i = '0'; i <= '9'; i++) {
113         dtable[i] = 52 + (i - '0');
114     }
115     dtable['+'] = 62;
116     dtable['/'] = 63;
117     dtable['='] = 0;
118
119     /*CONSTANTCONDITION*/
120     while (TRUE) {
121         byte a[4], b[4], o[3];
122
123         for (i = 0; i < 4; i++) {
124             if (spos >= length) {
125                 return(dpos);
126             }
127             c = source[spos++];
128
129             if (c == 0) {
130                 if (i > 0) {
131                     return(dpos);
132                 }
133                 return(dpos);
134             }
135             if (dtable[c] & 0x80) {
136                 /* Ignoring errors: discard invalid character. */
137                 i--;
138                 continue;
139             }
140             a[i] = (byte) c;
141             b[i] = (byte) dtable[c];
142         }
143         o[0] = (b[0] << 2) | (b[1] >> 4);
144         o[1] = (b[1] << 4) | (b[2] >> 2);
145         o[2] = (b[2] << 6) | b[3];
146         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
147         if (i>=1) dest[dpos++] = o[0];
148         if (i>=2) dest[dpos++] = o[1];
149         if (i>=3) dest[dpos++] = o[2];
150         dest[dpos] = 0;
151         if (i < 3) {
152             return(dpos);
153         }
154     }
155 }
156
157
158
159 /*
160  * Pack all session info into one easy-to-digest cookie.  Healthy and delicious!
161  */
162 void stuff_to_cookie(char *cookie, int session, char *user, char *pass, char *room)
163 {
164         char buf[SIZ];
165
166         sprintf(buf, "%d|%s|%s|%s", session, user, pass, room);
167         encode_base64(cookie, buf);
168 }
169
170
171 /*
172  * Extract all that fun stuff out of the cookie.
173  */
174 void cookie_to_stuff(char *cookie, int *session, char *user, char *pass, char *room)
175 {
176         char buf[SIZ];
177
178         CtdlDecodeBase64(buf, cookie, strlen(cookie));
179
180         if (session != NULL)
181                 *session = extract_int(buf, 0);
182         if (user != NULL)
183                 extract(user, buf, 1);
184         if (pass != NULL)
185                 extract(pass, buf, 2);
186         if (room != NULL)
187                 extract(room, buf, 3);
188 }