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