b5b4a8cbca4a0501713543148beafa2fcead63ef
[citadel.git] / citadel / citmail.c
1 /*
2  * citmail.c v4.2
3  * $Id$
4  *
5  * This program may be used as a local mail delivery agent, which will allow
6  * all Citadel users to receive Internet e-mail.  To enable this functionality,
7  * you must tell sendmail, smail, or whatever mailer you are using, that this
8  * program is your local mail delivery agent.  This program is a direct
9  * replacement for lmail, deliver, or whatever.
10  *
11  * Usage:
12  *
13  * citmail <recipient>       - Deliver a message
14  * citmail -t <recipient>    - Address test mode (will not deliver)
15  *
16  */
17
18 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <time.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <syslog.h>
29 #include <limits.h>
30 #include "citadel.h"
31 #include "config.h"
32 #include "internetmail.h"
33
34 /* message delivery classes */
35 enum {
36         DELIVER_LOCAL,
37         DELIVER_REMOTE,
38         DELIVER_INTERNET,
39         DELIVER_CCITADEL
40 };
41         
42
43 #undef tolower
44 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
45
46 char *monthdesc[] =
47 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
48  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
49
50 char ALIASES[128];
51 char CIT86NET[128];
52 char SENDMAIL[128];
53 char FALLBACK[128];
54 char GW_DOMAIN[128];
55 char TABLEFILE[128];
56 char OUTGOING_FQDN[128];
57 int RUN_NETPROC = 1;
58
59
60 long conv_date(char *sdbuf)
61 {
62         int a, b, cpos, tend, tval;
63         time_t now;
64         struct tm *tmbuf;
65         char dbuf[128];
66
67         strcpy(dbuf, sdbuf);
68         time(&now);
69         tmbuf = (struct tm *) localtime(&now);
70
71         /* get rid of + or - timezone mods */
72         for (a = 0; a < strlen(dbuf); ++a)
73                 if ((dbuf[a] == '+') || (dbuf[a] == '-'))
74                         do {
75                                 strcpy(&dbuf[a], &dbuf[a + 1]);
76                         } while ((dbuf[a] != 32) && (dbuf[a] != 0));
77
78         /* try and extract the time by looking for colons */
79         cpos = (-1);
80         for (a = strlen(dbuf); a >= 0; --a)
81                 if ((dbuf[a] == ':') && (atoi(&dbuf[a - 1]) != 0))
82                         cpos = a;
83         if (cpos >= 0) {
84                 cpos = cpos - 2;
85                 tend = strlen(dbuf);
86                 for (a = tend; a >= cpos; --a)
87                         if (dbuf[a] == ' ')
88                                 tend = a;
89
90                 tmbuf->tm_hour = atoi(&dbuf[cpos]);
91                 tmbuf->tm_min = atoi(&dbuf[cpos + 3]);
92                 tmbuf->tm_sec = atoi(&dbuf[cpos + 6]);
93
94                 do {
95                         strcpy(&dbuf[cpos], &dbuf[cpos + 1]);
96                 } while ((dbuf[cpos] != 32) && (dbuf[cpos] != 0));
97         }
98         /* next try to extract a month */
99
100         tval = (-1);
101         for (a = 0; a < strlen(dbuf); ++a)
102                 for (b = 0; b < 12; ++b)
103                         if (!strncmp(&dbuf[a], monthdesc[b], 3)) {
104                                 tval = b;
105                                 cpos = a;
106                         }
107         if (tval >= 0) {
108                 tmbuf->tm_mon = tval;
109                 strcpy(&dbuf[cpos], &dbuf[cpos + 3]);
110         }
111         /* now the year */
112
113         for (a = 0; a < strlen(dbuf); ++a)
114                 if ((atoi(&dbuf[a]) >= 1900) && (dbuf[a] != 32)) {
115                         tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
116                         strcpy(&dbuf[a], &dbuf[a + 4]);
117                 }
118         /* whatever's left is the mday (hopefully) */
119
120         for (a = 0; a < strlen(dbuf); ++a)
121                 if ((dbuf[a] != 32) && (atoi(&dbuf[a]) >= 1) && (atoi(&dbuf[a]) <= 31)
122                     && ((a == 0) || (dbuf[a - 1] == ' '))) {
123                         tmbuf->tm_mday = atoi(&dbuf[a]);
124                         strcpy(&dbuf[a], &dbuf[a + 2]);
125                 }
126         return ((long) mktime(tmbuf));
127 }
128
129
130 #ifndef HAVE_STRERROR
131 /*
132  * replacement strerror() for systems that don't have it
133  */
134 char *strerror(int e)
135 {
136         static char buf[32];
137
138         snprintf(buf, sizeof buf, "errno = %d", e);
139         return (buf);
140 }
141 #endif
142
143 int haschar(char *st, int ch)
144 {
145         int a, b;
146         b = 0;
147         for (a = 0; a < strlen(st); ++a)
148                 if (st[a] == ch)
149                         ++b;
150         return (b);
151 }
152
153 void strip_trailing_whitespace(char *buf)
154 {
155         while (isspace(buf[strlen(buf) - 1]))
156                 buf[strlen(buf) - 1] = 0;
157 }
158
159 /* strip leading and trailing spaces */
160 void striplt(char *buf)
161 {
162         while ((strlen(buf) > 0) && (buf[0] == 32))
163                 strcpy(buf, &buf[1]);
164         while (buf[strlen(buf) - 1] == 32)
165                 buf[strlen(buf) - 1] = 0;
166 }
167
168
169 /*
170  * Check to see if a given FQDN really maps to a Citadel network node
171  */
172 void host_alias(char host[])
173 {
174
175         int a;
176
177         /* What name is the local host known by? */
178         /* if (!strcasecmp(host, config.c_fqdn)) { */
179         if (IsHostLocal(host)) {
180                 strcpy(host, config.c_nodename);
181                 return;
182         }
183         /* Other hosts in the gateway domain? */
184         for (a = 0; a < strlen(host); ++a) {
185                 if ((host[a] == '.') && (!strcasecmp(&host[a + 1], GW_DOMAIN))) {
186                         host[a] = 0;
187                         for (a = 0; a < strlen(host); ++a) {
188                                 if (host[a] == '.')
189                                         host[a] = 0;
190                         }
191                         return;
192                 }
193         }
194
195         /* Otherwise, do nothing... */
196 }
197
198
199
200 /*
201  * Split an RFC822-style address into userid, host, and full name
202  */
203 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
204 {
205         int a;
206
207         /* extract full name - first, it's From minus <userid> */
208         strcpy(name, rfc822);
209         for (a = 0; a < strlen(name); ++a)
210                 if (name[a] == '<') {
211                         do {
212                                 strcpy(&name[a], &name[a + 1]);
213                         } while ((strlen(name) > 0) && (name[a] != '>'));
214                         strcpy(&name[a], &name[a + 1]);
215                 }
216         /* strip anything to the left of a bang */
217         while ((strlen(name) > 0) && (haschar(name, '!') > 0))
218                 strcpy(name, &name[1]);
219
220         /* and anything to the right of a @ or % */
221         for (a = 0; a < strlen(name); ++a) {
222                 if (name[a] == '@')
223                         name[a] = 0;
224                 if (name[a] == '%')
225                         name[a] = 0;
226         }
227
228         /* but if there are parentheses, that changes the rules... */
229         if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
230                 strcpy(name, rfc822);
231                 while ((strlen(name) > 0) && (name[0] != '(')) {
232                         strcpy(&name[0], &name[1]);
233                 }
234                 strcpy(&name[0], &name[1]);
235                 for (a = 0; a < strlen(name); ++a)
236                         if (name[a] == ')')
237                                 name[a] = 0;
238         }
239         /* but if there are a set of quotes, that supersedes everything */
240         if (haschar(rfc822, 34) == 2) {
241                 strcpy(name, rfc822);
242                 while ((strlen(name) > 0) && (name[0] != 34)) {
243                         strcpy(&name[0], &name[1]);
244                 }
245                 strcpy(&name[0], &name[1]);
246                 for (a = 0; a < strlen(name); ++a)
247                         if (name[a] == 34)
248                                 name[a] = 0;
249         }
250         /* extract user id */
251         strcpy(user, rfc822);
252
253         /* first get rid of anything in parens */
254         for (a = 0; a < strlen(user); ++a)
255                 if (user[a] == '(') {
256                         do {
257                                 strcpy(&user[a], &user[a + 1]);
258                         } while ((strlen(user) > 0) && (user[a] != ')'));
259                         strcpy(&user[a], &user[a + 1]);
260                 }
261         /* if there's a set of angle brackets, strip it down to that */
262         if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
263                 while ((strlen(user) > 0) && (user[0] != '<')) {
264                         strcpy(&user[0], &user[1]);
265                 }
266                 strcpy(&user[0], &user[1]);
267                 for (a = 0; a < strlen(user); ++a)
268                         if (user[a] == '>')
269                                 user[a] = 0;
270         }
271         /* strip anything to the left of a bang */
272         while ((strlen(user) > 0) && (haschar(user, '!') > 0))
273                 strcpy(user, &user[1]);
274
275         /* and anything to the right of a @ or % */
276         for (a = 0; a < strlen(user); ++a) {
277                 if (user[a] == '@')
278                         user[a] = 0;
279                 if (user[a] == '%')
280                         user[a] = 0;
281         }
282
283
284
285         /* extract node name */
286         strcpy(node, rfc822);
287
288         /* first get rid of anything in parens */
289         for (a = 0; a < strlen(node); ++a)
290                 if (node[a] == '(') {
291                         do {
292                                 strcpy(&node[a], &node[a + 1]);
293                         } while ((strlen(node) > 0) && (node[a] != ')'));
294                         strcpy(&node[a], &node[a + 1]);
295                 }
296         /* if there's a set of angle brackets, strip it down to that */
297         if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
298                 while ((strlen(node) > 0) && (node[0] != '<')) {
299                         strcpy(&node[0], &node[1]);
300                 }
301                 strcpy(&node[0], &node[1]);
302                 for (a = 0; a < strlen(node); ++a)
303                         if (node[a] == '>')
304                                 node[a] = 0;
305         }
306         /* strip anything to the left of a @ */
307         while ((strlen(node) > 0) && (haschar(node, '@') > 0))
308                 strcpy(node, &node[1]);
309
310         /* strip anything to the left of a % */
311         while ((strlen(node) > 0) && (haschar(node, '%') > 0))
312                 strcpy(node, &node[1]);
313
314         /* reduce multiple system bang paths to node!user */
315         while ((strlen(node) > 0) && (haschar(node, '!') > 1))
316                 strcpy(node, &node[1]);
317
318         /* now get rid of the user portion of a node!user string */
319         for (a = 0; a < strlen(node); ++a)
320                 if (node[a] == '!')
321                         node[a] = 0;
322
323
324
325         /* strip leading and trailing spaces in all strings */
326         striplt(user);
327         striplt(node);
328         striplt(name);
329 }
330
331 /*
332  * Copy line by line, ending at EOF or a "." 
333  */
334 void loopcopy(FILE * to, FILE * from)
335 {
336         char buf[1024];
337         char *r;
338
339         while (1) {
340                 r = fgets(buf, sizeof(buf), from);
341                 if (r == NULL)
342                         return;
343                 strip_trailing_whitespace(buf);
344                 if (!strcmp(buf, "."))
345                         return;
346                 fprintf(to, "%s\n", buf);
347         }
348 }
349
350
351
352 /*
353  * pipe message through netproc
354  */
355 void do_citmail(char recp[], int dtype)
356 {
357
358         time_t now;
359         FILE *temp;
360         int a;
361         int format_type = 0;
362         char buf[128];
363         char from[512];
364         char userbuf[256];
365         char frombuf[256];
366         char nodebuf[256];
367         char destsys[256];
368         char subject[256];
369         long message_id = 0L;
370         char targetroom[256];
371         char content_type[256];
372         char *extra_headers = NULL;
373
374
375         if (dtype == DELIVER_REMOTE) {
376
377                 /* get the Citadel node name out of the path */
378                 strncpy(destsys, recp, sizeof(destsys));
379                 for (a = 0; a < strlen(destsys); ++a) {
380                         if ((destsys[a] == '!') || (destsys[a] == '.')) {
381                                 destsys[a] = 0;
382                         }
383                 }
384
385                 /* chop the system name out, so we're left with a user */
386                 while (haschar(recp, '!'))
387                         strcpy(recp, &recp[1]);
388         }
389         /* Convert underscores to spaces */
390         for (a = 0; a < strlen(recp); ++a)
391                 if (recp[a] == '_')
392                         recp[a] = ' ';
393
394         /* Are we delivering to a room instead of a user? */
395         if (!strncasecmp(recp, "room ", 5)) {
396                 strcpy(targetroom, &recp[5]);
397                 strcpy(recp, "");
398         } else {
399                 strcpy(targetroom, MAILROOM);
400         }
401
402         time(&now);
403         snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
404
405         snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
406         temp = fopen(buf, "w");
407
408         strcpy(subject, "");
409         strcpy(nodebuf, config.c_nodename);
410         strcpy(content_type, "text/plain");
411
412         do {
413                 if (fgets(buf, 128, stdin) == NULL)
414                         strcpy(buf, ".");
415                 strip_trailing_whitespace(buf);
416
417                 if (!strncasecmp(buf, "Subject: ", 9))
418                         strcpy(subject, &buf[9]);
419                 else if (!strncasecmp(buf, "Date: ", 6))
420                         now = conv_date(&buf[6]);
421                 else if (!strncasecmp(buf, "From: ", 6))
422                         strcpy(from, &buf[6]);
423                 else if (!strncasecmp(buf, "Content-type: ", 14))
424                         strcpy(content_type, &buf[14]);
425                 else if (!strncasecmp(buf, "From ", 5)) {       /* ignore */
426                 } else {
427                         if (extra_headers == NULL) {
428                                 extra_headers = malloc(strlen(buf) + 2);
429                                 strcpy(extra_headers, "");
430                         } else {
431                                 extra_headers = realloc(extra_headers,
432                                                         (strlen(extra_headers) + strlen(buf) + 2));
433                         }
434                         strcat(extra_headers, buf);
435                         strcat(extra_headers, "\n");
436                 }
437         } while ((strcmp(buf, ".")) && (strcmp(buf, "")));
438
439         process_rfc822_addr(from, userbuf, nodebuf, frombuf);
440
441         if (!strncasecmp(content_type, "text/plain", 10))
442                 format_type = 1;        /* plain ASCII message */
443         else
444                 format_type = 4;        /* MIME message */
445
446         /* now convert it to Citadel format */
447
448         /* Header bytes */
449         putc(255, temp);        /* 0xFF = start-of-message byte */
450         putc(MES_NORMAL, temp); /* Non-anonymous message */
451         putc(format_type, temp);        /* Format type */
452
453         /* Origination */
454         fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0);
455         if (message_id)
456                 fprintf(temp, "I%ld%c", message_id, 0);
457         fprintf(temp, "T%ld%c", (long)now, 0);
458         fprintf(temp, "A%s%c", userbuf, 0);
459
460         /* Destination */
461         if (strlen(targetroom) > 0) {
462                 fprintf(temp, "O%s%c", targetroom, 0);
463         } else {
464                 fprintf(temp, "O%s%c", MAILROOM, 0);
465         }
466
467         fprintf(temp, "N%s%c", nodebuf, 0);
468         fprintf(temp, "H%s%c", frombuf, 0);
469         if (dtype == DELIVER_REMOTE) {
470                 fprintf(temp, "D%s%c", destsys, 0);
471         }
472         if (strlen(recp) > 0) {
473                 fprintf(temp, "R%s%c", recp, 0);
474         }
475         /* Subject and text */
476         if (strlen(subject) > 0) {
477                 fprintf(temp, "U%s%c", subject, 0);
478         }
479         putc('M', temp);
480         if (format_type == 4) {
481                 fprintf(temp, "Content-type: %s\n", content_type);
482                 if (extra_headers != NULL)
483                         fprintf(temp, "%s", extra_headers);
484                 fprintf(temp, "\n");
485         }
486         if (extra_headers != NULL)
487                 free(extra_headers);
488         if (strcmp(buf, "."))
489                 loopcopy(temp, stdin);
490         putc(0, temp);
491         fclose(temp);
492 }
493
494
495 void do_uudecode(char *target)
496 {
497         static char buf[1024];
498         FILE *fp;
499
500         snprintf(buf, sizeof buf, "cd %s; uudecode", target);
501
502         fp = popen(buf, "w");
503         if (fp == NULL)
504                 return;
505         while (fgets(buf, 1024, stdin) != NULL) {
506                 fprintf(fp, "%s", buf);
507         }
508         pclose(fp);
509
510 }
511
512 int alias(char *name)
513 {
514         FILE *fp;
515         int a;
516         char abuf[256];
517
518         fp = fopen(ALIASES, "r");
519         if (fp == NULL) {
520                 syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno));
521                 return (2);
522         }
523         while (fgets(abuf, 256, fp) != NULL) {
524                 strip_trailing_whitespace(abuf);
525                 for (a = 0; a < strlen(abuf); ++a) {
526                         if (abuf[a] == ',') {
527                                 abuf[a] = 0;
528                                 if (!strcasecmp(name, abuf)) {
529                                         strcpy(name, &abuf[a + 1]);
530                                 }
531                         }
532                 }
533         }
534         fclose(fp);
535         return (0);
536 }
537
538
539 void deliver(char recp[], int is_test, int deliver_to_ignet)
540 {
541
542         /* various ways we can deliver mail... */
543
544         if (deliver_to_ignet) {
545                 syslog(LOG_NOTICE, "to Citadel network user %s", recp);
546                 if (is_test == 0)
547                         do_citmail(recp, DELIVER_REMOTE);
548         } else if (!strcmp(recp, "uudecode")) {
549                 syslog(LOG_NOTICE, "uudecoding to bit bucket directory");
550                 if (is_test == 0)
551                         do_uudecode(config.c_bucket_dir);
552         } else if (!strcmp(recp, "cit86net")) {
553                 syslog(LOG_NOTICE, "uudecoding to Cit86net spool");
554                 if (is_test == 0) {
555                         do_uudecode(CIT86NET);
556                         system("exec ./BatchTranslate86");
557                 }
558         } else if (!strcmp(recp, "null")) {
559                 syslog(LOG_NOTICE, "zapping nulled message");
560         } else {
561                 /* Otherwise, the user is local (or an unknown name was
562                  * specified, in which case we let netproc handle the bounce)
563                  */
564                 syslog(LOG_NOTICE, "to Citadel recipient %s", recp);
565                 if (is_test == 0)
566                         do_citmail(recp, DELIVER_LOCAL);
567         }
568
569 }
570
571
572
573 int main(int argc, char **argv)
574 {
575         int is_test = 0;
576         int deliver_to_ignet = 0;
577         static char recp[1024], buf[1024];
578         static char user[1024], node[1024], name[1024];
579         int a;
580
581         openlog("citmail", LOG_PID, LOG_USER);
582         get_config();
583         LoadInternetConfig();
584
585         if (!strcmp(argv[1], "-t")) {
586                 is_test = 1;
587                 syslog(LOG_NOTICE, "test mode - will not deliver");
588         }
589         if (is_test == 0) {
590                 strcpy(recp, argv[1]);
591         } else {
592                 strcpy(recp, argv[2]);
593         }
594
595 /*** Non-SMTP delivery mode ***/
596         syslog(LOG_NOTICE, "recp: %s", recp);
597         for (a = 0; a < 2; ++a) {
598                 alias(recp);
599         }
600
601         /* did we alias it back to a remote address? */
602         if ((haschar(recp, '%'))
603             || (haschar(recp, '@'))
604             || (haschar(recp, '!'))) {
605
606                 process_rfc822_addr(recp, user, node, name);
607                 host_alias(node);
608
609                 /* If there are dots, it's an Internet host, so feed it
610                  * back to an external mail transport agent such as sendmail.
611                  */
612                 if (haschar(node, '.')) {
613                         snprintf(buf, sizeof buf, SENDMAIL, recp);
614                         system(buf);
615                         exit(0);
616                 }
617                 /* Otherwise, we're dealing with Citadel mail. */
618                 else {
619                         snprintf(recp, sizeof recp, "%s!%s", node, user);
620                         deliver_to_ignet = 1;
621                 }
622
623         }
624         deliver(recp, is_test, deliver_to_ignet);
625
626         if (RUN_NETPROC) {
627                 syslog(LOG_NOTICE, "running netproc");
628                 if (system("/bin/true") != 0) {
629                         syslog(LOG_ERR, "netproc failed: %s", strerror(errno));
630                 }
631         } else {
632                 syslog(LOG_NOTICE, "skipping netproc");
633         }
634         exit(0);
635 }