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