* Added the ability to compose messages with file attachments uploaded from
[citadel.git] / webcit / tools.c
1 /*
2  * tools.c -- Miscellaneous routines 
3  */
4
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <sys/socket.h>
14 #include <sys/time.h>
15 #include <limits.h>
16 #include <netinet/in.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <sys/time.h>
25 #include "webcit.h"
26 #include "webserver.h"
27
28 typedef unsigned char byte;
29
30 #define FALSE 0
31 #define TRUE 1
32
33 char *ascmonths[] = {
34         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
36 };
37
38 char *ascdays[] = {
39         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
40 };
41
42 static byte dtable[256];        /* base64 encode / decode table */
43
44 char *safestrncpy(char *dest, const char *src, size_t n)
45 {
46         if (dest == NULL || src == NULL) {
47                 lprintf(1, "safestrncpy: NULL argument\n");
48                 abort();
49         }
50         strncpy(dest, src, n);
51         dest[n - 1] = 0;
52         return dest;
53 }
54
55
56
57 /*
58  * num_tokens()  -  discover number of parameters/tokens in a string
59  */
60 int num_tokens(char *source, char tok)
61 {
62         int a;
63         int count = 1;
64
65         if (source == NULL)
66                 return (0);
67         for (a = 0; a < strlen(source); ++a) {
68                 if (source[a] == tok)
69                         ++count;
70         }
71         return (count);
72 }
73
74 /*
75  * extract_token()  -  a smarter string tokenizer
76  */
77 void extract_token(char *dest, char *source, int parmnum, char separator)
78 {
79         int i;
80         int len;
81         int curr_parm;
82
83         strcpy(dest, "");
84         len = 0;
85         curr_parm = 0;
86
87         if (strlen(source) == 0) {
88                 return;
89         }
90
91         for (i = 0; i < strlen(source); ++i) {
92                 if (source[i] == separator) {
93                         ++curr_parm;
94                 } else if (curr_parm == parmnum) {
95                         dest[len + 1] = 0;
96                         dest[len++] = source[i];
97                 }
98         }
99 }
100
101
102
103 /*
104  * remove_token()  -  a tokenizer that kills, maims, and destroys
105  */
106 void remove_token(char *source, int parmnum, char separator)
107 {
108         int i;
109         int len;
110         int curr_parm;
111         int start, end;
112
113         len = 0;
114         curr_parm = 0;
115         start = (-1);
116         end = (-1);
117
118         if (strlen(source) == 0) {
119                 return;
120         }
121
122         for (i = 0; i < strlen(source); ++i) {
123                 if ((start < 0) && (curr_parm == parmnum)) {
124                         start = i;
125                 }
126
127                 if ((end < 0) && (curr_parm == (parmnum + 1))) {
128                         end = i;
129                 }
130
131                 if (source[i] == separator) {
132                         ++curr_parm;
133                 }
134         }
135
136         if (end < 0)
137                 end = strlen(source);
138
139         strcpy(&source[start], &source[end]);
140 }
141
142
143
144
145 /*
146  * extract_int()  -  extract an int parm w/o supplying a buffer
147  */
148 int extract_int(char *source, int parmnum)
149 {
150         char buf[SIZ];
151
152         extract_token(buf, source, parmnum, '|');
153         return (atoi(buf));
154 }
155
156 /*
157  * extract_long()  -  extract an long parm w/o supplying a buffer
158  */
159 long extract_long(char *source, long int parmnum)
160 {
161         char buf[SIZ];
162
163         extract_token(buf, source, parmnum, '|');
164         return (atol(buf));
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 /*
182  * check for the presence of a character within a string (returns count)
183  */
184 int haschar(st, ch)
185 char st[];
186 char ch;
187 {
188         int a, b;
189         b = 0;
190         for (a = 0; a < strlen(st); ++a)
191                 if (st[a] == ch)
192                         ++b;
193         return (b);
194 }
195
196
197 /*
198  * Format a date/time stamp for output 
199  */
200 void fmt_date(char *buf, time_t thetime)
201 {
202         struct tm *tm;
203         int hour;
204
205         strcpy(buf, "");
206         tm = localtime(&thetime);
207         hour = tm->tm_hour;
208         if (hour == 0)
209                 hour = 12;
210         else if (hour > 12)
211                 hour = hour - 12;
212
213         sprintf(buf, "%s %d %d %2d:%02d%s",
214                 ascmonths[tm->tm_mon],
215                 tm->tm_mday,
216                 tm->tm_year + 1900,
217                 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
218             );
219 }
220
221
222
223 /*
224  * Format TIME ONLY for output 
225  */
226 void fmt_time(char *buf, time_t thetime)
227 {
228         struct tm *tm;
229         int hour;
230
231         strcpy(buf, "");
232         tm = localtime(&thetime);
233         hour = tm->tm_hour;
234         if (hour == 0)
235                 hour = 12;
236         else if (hour > 12)
237                 hour = hour - 12;
238
239         sprintf(buf, "%d:%02d%s",
240                 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
241             );
242 }
243
244
245
246
247 /*
248  * Format a date/time stamp to the format used in HTTP headers
249  */
250 void httpdate(char *buf, time_t thetime)
251 {
252         struct tm *tm;
253
254         strcpy(buf, "");
255         tm = localtime(&thetime);
256
257         sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
258                 ascdays[tm->tm_wday],
259                 tm->tm_mday,
260                 ascmonths[tm->tm_mon],
261                 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
262 }
263
264
265
266
267
268 /*
269  * Utility function to "readline" from memory
270  * (returns new pointer)
271  */
272 char *memreadline(char *start, char *buf, int maxlen)
273 {
274         char ch;
275         char *ptr;
276         int len = 0;            /* tally our own length to avoid strlen() delays */
277
278         ptr = start;
279         memset(buf, 0, maxlen);
280
281         while (1) {
282                 ch = *ptr++;
283                 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
284                         buf[strlen(buf) + 1] = 0;
285                         buf[strlen(buf)] = ch;
286                         ++len;
287                 }
288                 if ((ch == 10) || (ch == 0)) {
289                         return ptr;
290                 }
291         }
292 }
293
294
295
296 /*
297  * pattern2()  -  searches for patn within search string, returns pos
298  */
299 int pattern2(char *search, char *patn)
300 {
301         int a;
302         for (a = 0; a < strlen(search); ++a) {
303                 if (!strncasecmp(&search[a], patn, strlen(patn)))
304                         return (a);
305         }
306         return (-1);
307 }
308
309
310 /*
311  * Strip leading and trailing spaces from a string
312  */
313 void striplt(char *buf)
314 {
315         while ((strlen(buf) > 0) && (isspace(buf[0])))
316                 strcpy(buf, &buf[1]);
317         while (isspace(buf[strlen(buf) - 1]))
318                 buf[strlen(buf) - 1] = 0;
319 }
320
321
322 /*
323  * Determine whether the specified message number is contained within the
324  * specified set.
325  */
326 int is_msg_in_mset(char *mset, long msgnum)
327 {
328         int num_sets;
329         int s;
330         char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
331         long lo, hi;
332
333         /*
334          * Now set it for all specified messages.
335          */
336         num_sets = num_tokens(mset, ',');
337         for (s = 0; s < num_sets; ++s) {
338                 extract_token(setstr, mset, s, ',');
339
340                 extract_token(lostr, setstr, 0, ':');
341                 if (num_tokens(setstr, ':') >= 2) {
342                         extract_token(histr, setstr, 1, ':');
343                         if (!strcmp(histr, "*")) {
344                                 snprintf(histr, sizeof histr, "%ld",
345                                          LONG_MAX);
346                         }
347                 } else {
348                         strcpy(histr, lostr);
349                 }
350                 lo = atol(lostr);
351                 hi = atol(histr);
352
353                 if ((msgnum >= lo) && (msgnum <= hi))
354                         return (1);
355         }
356
357         return (0);
358 }
359
360
361 /*
362  * Strip a boundarized substring out of a string (for example, remove
363  * parentheses and anything inside them).
364  *
365  * This improved version can strip out *multiple* boundarized substrings.
366  */
367 void stripout(char *str, char leftboundary, char rightboundary)
368 {
369         int a;
370         int lb = (-1);
371         int rb = (-1);
372
373         do {
374                 lb = (-1);
375                 rb = (-1);
376
377                 for (a = 0; a < strlen(str); ++a) {
378                         if (str[a] == leftboundary)
379                                 lb = a;
380                         if (str[a] == rightboundary)
381                                 rb = a;
382                 }
383
384                 if ((lb > 0) && (rb > lb)) {
385                         strcpy(&str[lb - 1], &str[rb + 1]);
386                 }
387
388         } while ((lb > 0) && (rb > lb));
389
390 }
391
392
393
394 /*
395  * Replacement for sleep() that uses select() in order to avoid SIGALRM
396  */
397 void sleeeeeeeeeep(int seconds)
398 {
399         struct timeval tv;
400
401         tv.tv_sec = seconds;
402         tv.tv_usec = 0;
403         select(0, NULL, NULL, NULL, &tv);
404 }
405
406
407
408 /*
409  * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
410  * John Walker, copied over from the Citadel server.
411  */
412
413 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
414 {
415         int i, hiteof = FALSE;
416         int spos = 0;
417         int dpos = 0;
418         int thisline = 0;
419
420         /*  Fill dtable with character encodings.  */
421
422         for (i = 0; i < 26; i++) {
423                 dtable[i] = 'A' + i;
424                 dtable[26 + i] = 'a' + i;
425         }
426         for (i = 0; i < 10; i++) {
427                 dtable[52 + i] = '0' + i;
428         }
429         dtable[62] = '+';
430         dtable[63] = '/';
431
432         while (!hiteof) {
433                 byte igroup[3], ogroup[4];
434                 int c, n;
435
436                 igroup[0] = igroup[1] = igroup[2] = 0;
437                 for (n = 0; n < 3; n++) {
438                         if (spos >= sourcelen) {
439                                 hiteof = TRUE;
440                                 break;
441                         }
442                         c = source[spos++];
443                         igroup[n] = (byte) c;
444                 }
445                 if (n > 0) {
446                         ogroup[0] = dtable[igroup[0] >> 2];
447                         ogroup[1] =
448                             dtable[((igroup[0] & 3) << 4) |
449                                    (igroup[1] >> 4)];
450                         ogroup[2] =
451                             dtable[((igroup[1] & 0xF) << 2) |
452                                    (igroup[2] >> 6)];
453                         ogroup[3] = dtable[igroup[2] & 0x3F];
454
455                         /* Replace characters in output stream with "=" pad
456                            characters if fewer than three characters were
457                            read from the end of the input stream. */
458
459                         if (n < 3) {
460                                 ogroup[3] = '=';
461                                 if (n < 2) {
462                                         ogroup[2] = '=';
463                                 }
464                         }
465                         for (i = 0; i < 4; i++) {
466                                 dest[dpos++] = ogroup[i];
467                                 dest[dpos] = 0;
468                         }
469                         thisline += 4;
470                         if (thisline > 70) {
471                                 dest[dpos++] = '\r';
472                                 dest[dpos++] = '\n';
473                                 dest[dpos] = 0;
474                                 thisline = 0;
475                         }
476                 }
477         }
478         if (thisline > 70) {
479                 dest[dpos++] = '\r';
480                 dest[dpos++] = '\n';
481                 dest[dpos] = 0;
482                 thisline = 0;
483         }
484 }
485
486
487 /* 
488  * Convert base64-encoded to binary.  Returns the length of the decoded data.
489  * It will stop after reading 'length' bytes.
490  */
491 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
492 {
493         int i, c;
494         int dpos = 0;
495         int spos = 0;
496
497         for (i = 0; i < 255; i++) {
498                 dtable[i] = 0x80;
499         }
500         for (i = 'A'; i <= 'Z'; i++) {
501                 dtable[i] = 0 + (i - 'A');
502         }
503         for (i = 'a'; i <= 'z'; i++) {
504                 dtable[i] = 26 + (i - 'a');
505         }
506         for (i = '0'; i <= '9'; i++) {
507                 dtable[i] = 52 + (i - '0');
508         }
509         dtable['+'] = 62;
510         dtable['/'] = 63;
511         dtable['='] = 0;
512
513          /*CONSTANTCONDITION*/ while (TRUE) {
514                 byte a[4], b[4], o[3];
515
516                 for (i = 0; i < 4; i++) {
517                         if (spos >= length) {
518                                 return (dpos);
519                         }
520                         c = source[spos++];
521
522                         if (c == 0) {
523                                 if (i > 0) {
524                                         return (dpos);
525                                 }
526                                 return (dpos);
527                         }
528                         if (dtable[c] & 0x80) {
529                                 /* Ignoring errors: discard invalid character. */
530                                 i--;
531                                 continue;
532                         }
533                         a[i] = (byte) c;
534                         b[i] = (byte) dtable[c];
535                 }
536                 o[0] = (b[0] << 2) | (b[1] >> 4);
537                 o[1] = (b[1] << 4) | (b[2] >> 2);
538                 o[2] = (b[2] << 6) | b[3];
539                 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
540                 if (i >= 1)
541                         dest[dpos++] = o[0];
542                 if (i >= 2)
543                         dest[dpos++] = o[1];
544                 if (i >= 3)
545                         dest[dpos++] = o[2];
546                 dest[dpos] = 0;
547                 if (i < 3) {
548                         return (dpos);
549                 }
550         }
551 }