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