Changes to support MIME
[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 if (!strncasecmp(buf,"From ", 5))  /* ignore */
403                         { }
404                 else {
405                         if (extra_headers == NULL) {
406                                 extra_headers = malloc(strlen(buf)+2);
407                                 strcpy(extra_headers, "");
408                                 }
409                         else {
410                                 extra_headers = realloc(extra_headers,
411                                         (strlen(extra_headers)+strlen(buf)+2));
412                                 }
413                         strcat(extra_headers, buf);
414                         strcat(extra_headers, "\n");
415                         }
416                 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
417
418         process_rfc822_addr(from, userbuf, nodebuf, frombuf);
419
420         if (!strncasecmp(content_type, "text/plain", 10))
421                 format_type = 1;        /* plain ASCII message */
422         else
423                 format_type = 4;        /* MIME message */
424
425         /* now convert it to Citadel format */
426
427         /* Header bytes */
428         putc(255, temp);                /* 0xFF = start-of-message byte */
429         putc(MES_NORMAL, temp);         /* Non-anonymous message */
430         putc(format_type, temp);        /* Format type */
431
432         /* Origination */
433         fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
434         fprintf(temp,"T%ld%c", now, 0);
435         fprintf(temp,"A%s%c", userbuf, 0);
436
437         /* Destination */
438         if (strlen(targetroom) > 0) {
439                 fprintf(temp, "O%s%c", targetroom, 0);
440                 }
441         else {
442                 fprintf(temp, "O%s%c", MAILROOM, 0);
443                 }
444
445         fprintf(temp,"N%s%c", nodebuf, 0);
446         fprintf(temp,"H%s%c", frombuf, 0);
447         if (dtype==REMOTE) {
448                 fprintf(temp,"D%s%c", destsys, 0);
449                 }
450
451         if (strlen(recp) > 0) {
452                 fprintf(temp,"R%s%c", recp, 0);
453                 }
454
455         /* Subject and text */
456         if (strlen(subject)>0) {
457                 fprintf(temp,"U%s%c", subject, 0);
458                 }
459         putc('M',temp);
460         if (format_type == 4) {
461                 fprintf(temp, "Content-type: %s\n", content_type);
462                 if (extra_headers != NULL)
463                         fprintf(temp, "%s", extra_headers);
464                 fprintf(temp, "\n");
465                 }
466         if (extra_headers != NULL) free(extra_headers);
467         if (strcmp(buf, ".")) loopcopy(temp, stdin);
468         putc(0,temp);
469         fclose(temp);
470         }
471
472
473 void do_uudecode(char *target)
474 {
475         static char buf[1024];
476         FILE *fp;
477         
478         snprintf(buf,sizeof buf,"cd %s; uudecode",target);
479
480         fp=popen(buf,"w");
481         if (fp==NULL) return;
482         while (fgets(buf,1024,stdin)!=NULL) {
483                 fprintf(fp,"%s",buf);
484                 }
485         pclose(fp);
486
487         }
488
489 int alias(char *name)
490 {
491         FILE *fp;
492         int a;
493         char abuf[256];
494         
495         fp=fopen(ALIASES,"r");
496         if (fp==NULL) {
497                 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
498                 return(2);
499                 }
500
501         while (fgets(abuf,256,fp)!=NULL) {
502                 strip_trailing_whitespace(abuf);
503                 for (a=0; a<strlen(abuf); ++a) {
504                         if (abuf[a]==',') {
505                                 abuf[a]=0;
506                                 if (!strcasecmp(name,abuf)) {
507                                         strcpy(name,&abuf[a+1]);
508                                         }
509                                 }
510                         }
511                 }
512         fclose(fp);
513         return(0);
514         }
515
516
517 void deliver(char recp[], int is_test, int deliver_to_ignet) {
518
519         /* various ways we can deliver mail... */
520
521         if (deliver_to_ignet) {
522                 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
523                 if (is_test == 0) do_citmail(recp, REMOTE);
524                 }
525
526         else if (!strcmp(recp,"uudecode")) {
527                 syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
528                 if (is_test == 0) do_uudecode(config.c_bucket_dir);
529                 }
530
531         else if (!strcmp(recp,"cit86net")) {
532                 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
533                 if (is_test == 0) {
534                         do_uudecode(CIT86NET);
535                         system("exec ./BatchTranslate86");
536                         }
537                 }
538
539         else if (!strcmp(recp,"null")) {
540                 syslog(LOG_NOTICE,"zapping nulled message");
541                 }
542
543         else {
544                 /* Otherwise, the user is local (or an unknown name was
545                  * specified, in which case we let netproc handle the bounce)
546                  */
547                 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
548                 if (is_test == 0) do_citmail(recp, LOCAL);
549                 }
550
551         }
552
553
554
555 int main(int argc, char **argv)
556 {
557         int is_test = 0;
558         int deliver_to_ignet = 0;
559         static char recp[1024], buf[1024];
560         static char user[1024], node[1024], name[1024];
561         int a;
562
563         openlog("citmail", LOG_PID, LOG_USER);
564         get_config();
565         LoadInternetConfig();
566
567         if (!strcmp(argv[1],"-t")) {
568                 is_test = 1;
569                 syslog(LOG_NOTICE,"test mode - will not deliver");
570                 }
571
572         if (is_test == 0) {
573                 strcpy(recp,argv[1]);
574                 }
575         else {
576                 strcpy(recp,argv[2]);
577                 }
578
579         /*** Non-SMTP delivery mode ***/
580         syslog(LOG_NOTICE,"recp: %s",recp);
581         for (a=0; a<2; ++a) {
582                 alias(recp);
583                 }
584         
585         /* did we alias it back to a remote address? */
586         if (    (haschar(recp,'%'))
587         ||      (haschar(recp,'@'))
588         ||      (haschar(recp,'!')) ) {
589
590                 process_rfc822_addr(recp, user, node, name);
591                 host_alias(node);
592         
593                 /* If there are dots, it's an Internet host, so feed it
594                 * back to an external mail transport agent such as sendmail.
595                 */
596                 if (haschar(node, '.')) {
597                         snprintf(buf, sizeof buf, SENDMAIL, recp);
598                         system(buf);
599                         exit(0);
600                         }
601
602                 /* Otherwise, we're dealing with Citadel mail. */
603                 else {
604                         snprintf(recp, sizeof recp, "%s!%s", node, user);
605                         deliver_to_ignet = 1;
606                         }
607
608                 }
609
610         deliver(recp, is_test, deliver_to_ignet);
611
612         if (RUN_NETPROC) execlp("./netproc", "netproc", "-i", NULL);
613         exit(0);
614         }
615