Tue Aug 18 00:42:33 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[citadel.git] / citadel / citmail.c
1 /*
2  * citmail.c v4.1
3  *
4  * This program may be used as a local mail delivery agent, which will allow
5  * all Citadel users to receive Internet e-mail.  To enable this functionality,
6  * you must tell sendmail, smail, or whatever mailer you are using, that this
7  * program is your local mail delivery agent.  This program is a direct
8  * replacement for lmail, deliver, or whatever.
9  *
10  * Usage:
11  *
12  * citmail <recipient>       - Deliver a message
13  * citmail -t <recipient>    - Address test mode (will not deliver)
14  * citmail -i                - Run as an SMTP daemon (typically from inetd)
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         long 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 #ifdef NO_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         sprintf(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         long now;
339         FILE *temp;
340         int a;
341         char buf[128];
342         char from[512];
343         char userbuf[256];
344         char frombuf[256];
345         char nodebuf[256];
346         char destsys[256];
347         char subject[256];
348         char targetroom[256];
349
350
351         if (dtype==REMOTE) {
352
353                 /* get the Citadel node name out of the path */
354                 strncpy(destsys, recp, sizeof(destsys) );
355                 for (a=0; a<strlen(destsys); ++a) {
356                         if ((destsys[a]=='!')||(destsys[a]=='.')) {
357                                 destsys[a]=0;
358                                 }
359                         }
360
361                 /* chop the system name out, so we're left with a user */
362                 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
363                 }
364
365         /* Convert underscores to spaces */
366         for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
367
368         /* Are we delivering to a room instead of a user? */
369         if (!strncasecmp(recp, "room ", 5)) {
370                 strcpy(targetroom, &recp[5]);
371                 strcpy(recp, "");
372                 }
373         else {
374                 strcpy(targetroom, "Mail");
375                 }
376
377         time(&now);
378         sprintf(from, "postmaster@%s", config.c_nodename);
379
380         sprintf(buf, "./network/spoolin/citmail.%d", getpid());
381         temp = fopen(buf,"w");
382
383         putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
384         strcpy(subject,"");
385         strcpy(nodebuf, config.c_nodename);
386         do {
387                 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
388                 strip_trailing_whitespace(buf);
389
390                 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
391                 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
392                 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
393                 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
394
395         process_rfc822_addr(from, userbuf, nodebuf, frombuf);
396
397         /* now convert it to Citadel format */
398         fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
399         fprintf(temp,"T%ld%c", now, 0);
400         fprintf(temp,"A%s%c", userbuf, 0);
401
402         if (strlen(targetroom) > 0) {
403                 fprintf(temp, "O%s%c", targetroom, 0);
404                 }
405         else {
406                 fprintf(temp, "OMail%c", 0);
407                 }
408
409         fprintf(temp,"N%s%c", nodebuf, 0);
410         fprintf(temp,"H%s%c", frombuf, 0);
411         if (dtype==REMOTE) {
412                 fprintf(temp,"D%s%c", destsys, 0);
413                 }
414
415         if (strlen(recp) > 0) {
416                 fprintf(temp,"R%s%c", recp, 0);
417                 }
418
419         if (strlen(subject)>0) {
420                 fprintf(temp,"U%s%c", subject, 0);
421                 }
422         putc('M',temp);
423         if (strcmp(buf, ".")) loopcopy(temp, stdin);
424         putc(0,temp);
425         fclose(temp);
426         }
427
428
429 void do_uudecode(char *target)
430 {
431         static char buf[1024];
432         FILE *fp;
433         
434         sprintf(buf,"cd %s; uudecode",target);
435
436         fp=popen(buf,"w");
437         if (fp==NULL) return;
438         while (fgets(buf,1024,stdin)!=NULL) {
439                 fprintf(fp,"%s",buf);
440                 }
441         pclose(fp);
442
443         }
444
445 int alias(char *name)
446 {
447         FILE *fp;
448         int a;
449         char abuf[256];
450         
451         fp=fopen(ALIASES,"r");
452         if (fp==NULL) {
453                 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
454                 return(2);
455                 }
456
457         while (fgets(abuf,256,fp)!=NULL) {
458                 strip_trailing_whitespace(abuf);
459                 for (a=0; a<strlen(abuf); ++a) {
460                         if (abuf[a]==',') {
461                                 abuf[a]=0;
462                                 if (!strcasecmp(name,abuf)) {
463                                         strcpy(name,&abuf[a+1]);
464                                         }
465                                 }
466                         }
467                 }
468         fclose(fp);
469         return(0);
470         }
471
472
473 void deliver(char recp[], int is_test, int deliver_to_ignet) {
474
475         /* various ways we can deliver mail... */
476
477         if (deliver_to_ignet) {
478                 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
479                 if (is_test == 0) do_citmail(recp, REMOTE);
480                 }
481
482         else if (!strcmp(recp,"uudecode")) {
483                 syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
484                 if (is_test == 0) do_uudecode(config.c_bucket_dir);
485                 }
486
487         else if (!strcmp(recp,"cit86net")) {
488                 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
489                 if (is_test == 0) {
490                         do_uudecode(CIT86NET);
491                         system("exec ./BatchTranslate86");
492                         }
493                 }
494
495         else if (!strcmp(recp,"null")) {
496                 syslog(LOG_NOTICE,"zapping nulled message");
497                 }
498
499         else {
500                 /* Otherwise, the user is local (or an unknown name was
501                  * specified, in which case we let netproc handle the bounce)
502                  */
503                 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
504                 if (is_test == 0) do_citmail(recp, LOCAL);
505                 }
506
507         }
508
509
510
511 void main(int argc, char **argv)
512 {
513         int is_test = 0;
514         int deliver_to_ignet = 0;
515         int smtp = 0;
516         static char recp[1024], buf[1024];
517         static char user[1024], node[1024], name[1024];
518         int a;
519
520         openlog("citmail", LOG_PID, LOG_USER);
521         get_config();
522         LoadInternetConfig();
523
524         if (!strcmp(argv[1],"-t")) {
525                 is_test = 1;
526                 syslog(LOG_NOTICE,"test mode - will not deliver");
527                 }
528         if (!strcmp(argv[1], "-i")) {
529                 smtp = 1;
530                 syslog(LOG_NOTICE,"started as an SMTP daemon");
531                 }
532
533
534         if (smtp) {
535                 strcpy(recp, "");
536                 }
537         else if (is_test == 0) {
538                 strcpy(recp,argv[1]);
539                 }
540         else {
541                 strcpy(recp,argv[2]);
542                 }
543
544
545         if (smtp) {
546                 /*** SMTP delivery mode ***/
547
548                 printf("200 Citadel/UX SMTP gateway ready.\n");
549         
550                 do {
551                         fflush(stdout);
552                         fflush(stderr);
553                         fgets(buf, 1024, stdin);
554                         while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
555                                 buf[strlen(buf)-1] = 0;
556                                 }
557
558                         /* null-pad to allow some lazy compares */
559                         buf[strlen(buf)+1] = 0;
560                         buf[strlen(buf)+2] = 0;
561                         buf[strlen(buf)+3] = 0;
562         
563                         if (!strncasecmp(buf, "QUIT", 4)) {
564                                 printf("221 Later, dude.\n");
565                                 }
566                         else if (!strncasecmp(buf, "HELP", 4)) {
567                                 printf("214 You think _you_ need help?\n");
568                                 }
569                         else if (!strncasecmp(buf, "HELO", 4)) {
570                                 printf("250 Howdy ho, Mr. Hankey!\n");
571                                 }
572                         else if (!strncasecmp(buf, "MAIL", 4)) {
573                                 printf("250 Sure, whatever...\n");
574                                 }
575
576
577                         else if (!strncasecmp(buf, "RCPT To: ", 9)) {
578                                 if (strlen(recp) > 0) {
579                                         printf("571 Multiple recipients not supported.\n");
580                                         }
581                                 else {
582                                         strcpy(recp, &buf[9]);
583                                         if (haschar(recp, ',')) {
584                                                 printf("571 Multiple recipients not supported.\n");
585                                                 strcpy(recp, "");
586                                                 }
587                                         else {
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                                                                 printf("571 Away with thee, spammer!\n");
606                                                                 strcpy(recp, "");
607                                                                 }
608                 
609                                                         /* Otherwise, we're dealing with Citadel mail. */
610                                                         else {
611                                                                 sprintf(recp, "%s!%s", node, user);
612                                                                 deliver_to_ignet = 1;
613                                                                 printf("250 IGnet recipient.\n");
614                                                                 }
615                                                         }
616                                                 else {
617                                                         printf("250 Local recipient.\n");
618                                                         }
619         
620                                                 }
621         
622                                         }
623                                 }
624
625
626
627                         else if (!strncasecmp(buf, "RCPT", 4)) {
628                                 printf("501 Only 'To:' commands are supported.\n");
629                                 }
630                         else if (!strncasecmp(buf, "DATA", 4)) {
631                                 if (strlen(recp) > 0) {
632                                         printf("354 Sock it to me, baby...\n");
633                                         fflush(stdout);
634                                         deliver(recp, is_test, deliver_to_ignet);
635                                         printf("250 Cool beans!\n");
636                                         }
637                                 else {
638                                         printf("503 No recipient has been specified.\n");
639                                         }
640                                 }
641                         else {
642                                 printf("500 Huh?\n");
643                                 }
644         
645                         } while (strncasecmp(buf,"QUIT",4));
646                 }
647
648         else {
649                 /*** Non-SMTP delivery mode ***/
650                 syslog(LOG_NOTICE,"recp: %s",recp);
651                 for (a=0; a<2; ++a) {
652                         alias(recp);
653                         }
654         
655                 /* did we alias it back to a remote address? */
656                 if (    (haschar(recp,'%'))
657                 ||      (haschar(recp,'@'))
658                 ||      (haschar(recp,'!')) ) {
659         
660                         process_rfc822_addr(recp, user, node, name);
661                         host_alias(node);
662                 
663                         /* If there are dots, it's an Internet host, so feed it
664                         * back to an external mail transport agent such as sendmail.
665                         */
666                         if (haschar(node, '.')) {
667                                 sprintf(buf, SENDMAIL, recp);
668                                 system(buf);
669                                 exit(0);
670                                 }
671         
672                         /* Otherwise, we're dealing with Citadel mail. */
673                         else {
674                                 sprintf(recp, "%s!%s", node, user);
675                                 deliver_to_ignet = 1;
676                                 }
677         
678                         }
679         
680                 deliver(recp, is_test, deliver_to_ignet);
681                 }
682         
683         closelog();
684         if (RUN_NETPROC) execlp("./netproc","netproc",NULL);
685         exit(0);
686         }
687