* citserver.c, msgbase.c, user_ops.c: hide the owner-prefix of mail
[citadel.git] / citadel / citmail.c
1 /*
2  * citmail.c v4.1
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  * citmail -i                - Run as an SMTP daemon (typically from inetd)
16  *
17  */
18
19 #include "sysdep.h"
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <time.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <syslog.h>
30 #include <limits.h>
31 #include "citadel.h"
32 #include "config.h"
33 #include "internetmail.h"
34
35 #define LOCAL 0
36 #define REMOTE 1
37 #define UUCP 2
38 #define CCITADEL 3
39
40 #undef tolower
41 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
42
43 char *monthdesc[] = {   "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)) cpos=a;
78         if (cpos>=0) {
79                 cpos = cpos - 2;
80                 tend = strlen(dbuf);
81                 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
82
83                 tmbuf->tm_hour = atoi(&dbuf[cpos]);
84                 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
85                 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
86
87                 do {
88                         strcpy(&dbuf[cpos],&dbuf[cpos+1]);
89                         } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
90                 }
91
92         /* next try to extract a month */
93         
94         tval = (-1);
95         for (a=0; a<strlen(dbuf); ++a)
96                 for (b=0; b<12; ++b)
97                         if (!strncmp(&dbuf[a],monthdesc[b],3)) {
98                                 tval = b;
99                                 cpos = a;
100                                 }
101         if (tval >= 0) {
102                 tmbuf->tm_mon = tval;
103                 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
104                 }
105
106         /* now the year */
107
108         for (a=0; a<strlen(dbuf); ++a)
109                 if ((atoi(&dbuf[a])>=1900) && (dbuf[a]!=32)) {
110                         tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
111                         strcpy(&dbuf[a],&dbuf[a+4]);
112                         }
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
123         return((long)mktime(tmbuf));
124         }
125
126
127 #ifndef HAVE_STRERROR
128 /*
129  * replacement strerror() for systems that don't have it
130  */
131 char *strerror(int e)
132 {
133         static char buf[32];
134
135         snprintf(buf,sizeof buf,"errno = %d",e);
136         return(buf);
137         }
138 #endif
139
140 int haschar(char *st, int ch)
141 {
142         int a,b;
143         b=0;
144         for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
145         return(b);
146         }
147
148 void strip_trailing_whitespace(char *buf)
149 {
150         while(isspace(buf[strlen(buf)-1]))
151                 buf[strlen(buf)-1]=0;
152         }
153
154 /* strip leading and trailing spaces */
155 void striplt(char *buf)
156 {
157         while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
158         while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
159         }
160
161
162 /*
163  * Check to see if a given FQDN really maps to a Citadel network node
164  */
165 void host_alias(char host[]) {
166
167         int a;
168
169         /* What name is the local host known by? */
170         /* if (!strcasecmp(host, config.c_fqdn)) { */
171         if (IsHostLocal(host)) {
172                 strcpy(host, config.c_nodename);
173                 return;
174                 }
175
176         /* Other hosts in the gateway domain? */
177         for (a=0; a<strlen(host); ++a) {
178                 if ((host[a]=='.') && (!strcasecmp(&host[a+1], GW_DOMAIN))) {
179                         host[a] = 0;
180                         for (a=0; a<strlen(host); ++a) {
181                                 if (host[a]=='.') host[a] = 0;
182                                 }
183                         return;
184                         }
185                 }
186
187         /* Otherwise, do nothing... */
188         }
189
190
191
192 /*
193  * Split an RFC822-style address into userid, host, and full name
194  */
195 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
196 {
197         int a;
198
199         /* extract full name - first, it's From minus <userid> */
200         strcpy(name,rfc822);
201         for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
202                 do {
203                         strcpy(&name[a],&name[a+1]);
204                         } while ( (strlen(name) > 0) && (name[a]!='>') );
205                 strcpy(&name[a],&name[a+1]);
206                 }
207
208         /* strip anything to the left of a bang */
209         while ( (strlen(name)>0) && (haschar(name,'!')>0) ) 
210                 strcpy(name,&name[1]);
211
212         /* and anything to the right of a @ or % */
213         for (a=0; a<strlen(name); ++a) {
214                 if (name[a]=='@') name[a]=0;
215                 if (name[a]=='%') name[a]=0;
216                 }
217
218         /* but if there are parentheses, that changes the rules... */
219         if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
220                 strcpy(name,rfc822);
221                 while ( (strlen(name) > 0) && (name[0]!='(') ) {
222                         strcpy(&name[0],&name[1]);
223                         }
224                 strcpy(&name[0],&name[1]);
225                 for (a=0; a<strlen(name); ++a)
226                          if (name[a]==')') name[a]=0;
227                 }
228
229         /* but if there are a set of quotes, that supersedes everything */
230         if (haschar(rfc822,34)==2) {
231                 strcpy(name,rfc822);
232                 while ( (strlen(name) > 0) && (name[0]!=34) ) {
233                         strcpy(&name[0],&name[1]);
234                         }
235                 strcpy(&name[0],&name[1]);
236                 for (a=0; a<strlen(name); ++a)
237                         if (name[a]==34) name[a]=0;
238                 }
239
240         /* extract user id */
241         strcpy(user,rfc822);
242
243         /* first get rid of anything in parens */
244         for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
245                 do {
246                         strcpy(&user[a],&user[a+1]);
247                         } while ( (strlen(user) > 0) && (user[a]!=')') );
248                 strcpy(&user[a],&user[a+1]);
249                 }
250
251         /* if there's a set of angle brackets, strip it down to that */
252         if ( (haschar(user,'<') == 1) && (haschar(user,'>') == 1) ) {
253                 while ( (strlen(user) > 0) && (user[0]!='<') ) {
254                         strcpy(&user[0],&user[1]);
255                         }
256                 strcpy(&user[0],&user[1]);
257                 for (a=0; a<strlen(user); ++a)
258                          if (user[a]=='>') user[a]=0;
259                 }
260
261         /* strip anything to the left of a bang */
262         while ( (strlen(user)>0) && (haschar(user,'!')>0) ) 
263                 strcpy(user,&user[1]);
264
265         /* and anything to the right of a @ or % */
266         for (a=0; a<strlen(user); ++a) {
267                 if (user[a]=='@') user[a]=0;
268                 if (user[a]=='%') user[a]=0;
269                 }
270
271
272         
273         /* extract node name */
274         strcpy(node, rfc822);
275
276         /* first get rid of anything in parens */
277         for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
278                 do {
279                         strcpy(&node[a],&node[a+1]);
280                         } while ( (strlen(node) > 0) && (node[a]!=')') );
281                 strcpy(&node[a],&node[a+1]);
282                 }
283
284         /* if there's a set of angle brackets, strip it down to that */
285         if ( (haschar(node,'<') == 1) && (haschar(node,'>') == 1) ) {
286                 while ( (strlen(node) > 0) && (node[0]!='<') ) {
287                         strcpy(&node[0],&node[1]);
288                         }
289                 strcpy(&node[0],&node[1]);
290                 for (a=0; a<strlen(node); ++a)
291                          if (node[a]=='>') node[a]=0;
292                 }
293
294         /* strip anything to the left of a @ */
295         while ( (strlen(node)>0) && (haschar(node,'@')>0) ) 
296                 strcpy(node,&node[1]);
297
298         /* strip anything to the left of a % */
299         while ( (strlen(node)>0) && (haschar(node,'%')>0) ) 
300                 strcpy(node,&node[1]);
301
302         /* reduce multiple system bang paths to node!user */
303         while ( (strlen(node)>0) && (haschar(node,'!')>1) ) 
304                 strcpy(node,&node[1]);
305
306         /* now get rid of the user portion of a node!user string */
307         for (a=0; a<strlen(node); ++a) if (node[a]=='!') node[a]=0;
308
309
310
311         /* strip leading and trailing spaces in all strings */
312         striplt(user);
313         striplt(node);
314         striplt(name);
315         }
316
317 /*
318  * Copy line by line, ending at EOF or a "." 
319  */
320 void loopcopy(FILE *to, FILE *from) {
321         char buf[1024];
322         char *r;
323         
324         while (1) {
325                 r = fgets(buf, sizeof(buf), from);
326                 if (r == NULL) return;
327                 strip_trailing_whitespace(buf);
328                 if (!strcmp(buf, ".")) return;
329                 fprintf(to, "%s\n", buf);
330                 }
331         }
332
333
334 /*
335  * pipe message through netproc
336  */
337 void do_citmail(char recp[], int dtype) {
338
339         time_t now;
340         FILE *temp;
341         int a;
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
351
352         if (dtype==REMOTE) {
353
354                 /* get the Citadel node name out of the path */
355                 strncpy(destsys, recp, sizeof(destsys) );
356                 for (a=0; a<strlen(destsys); ++a) {
357                         if ((destsys[a]=='!')||(destsys[a]=='.')) {
358                                 destsys[a]=0;
359                                 }
360                         }
361
362                 /* chop the system name out, so we're left with a user */
363                 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
364                 }
365
366         /* Convert underscores to spaces */
367         for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
368
369         /* Are we delivering to a room instead of a user? */
370         if (!strncasecmp(recp, "room ", 5)) {
371                 strcpy(targetroom, &recp[5]);
372                 strcpy(recp, "");
373                 }
374         else {
375                 strcpy(targetroom, MAILROOM);
376                 }
377
378         time(&now);
379         snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
380
381         snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
382         temp = fopen(buf,"w");
383
384         putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
385         strcpy(subject,"");
386         strcpy(nodebuf, config.c_nodename);
387         do {
388                 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
389                 strip_trailing_whitespace(buf);
390
391                 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
392                 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
393                 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
394                 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
395
396         process_rfc822_addr(from, userbuf, nodebuf, frombuf);
397
398         /* now convert it to Citadel format */
399         fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
400         fprintf(temp,"T%ld%c", now, 0);
401         fprintf(temp,"A%s%c", userbuf, 0);
402
403         if (strlen(targetroom) > 0) {
404                 fprintf(temp, "O%s%c", targetroom, 0);
405                 }
406         else {
407                 fprintf(temp, "O%s%c", MAILROOM, 0);
408                 }
409
410         fprintf(temp,"N%s%c", nodebuf, 0);
411         fprintf(temp,"H%s%c", frombuf, 0);
412         if (dtype==REMOTE) {
413                 fprintf(temp,"D%s%c", destsys, 0);
414                 }
415
416         if (strlen(recp) > 0) {
417                 fprintf(temp,"R%s%c", recp, 0);
418                 }
419
420         if (strlen(subject)>0) {
421                 fprintf(temp,"U%s%c", subject, 0);
422                 }
423         putc('M',temp);
424         if (strcmp(buf, ".")) loopcopy(temp, stdin);
425         putc(0,temp);
426         fclose(temp);
427         }
428
429
430 void do_uudecode(char *target)
431 {
432         static char buf[1024];
433         FILE *fp;
434         
435         snprintf(buf,sizeof buf,"cd %s; uudecode",target);
436
437         fp=popen(buf,"w");
438         if (fp==NULL) return;
439         while (fgets(buf,1024,stdin)!=NULL) {
440                 fprintf(fp,"%s",buf);
441                 }
442         pclose(fp);
443
444         }
445
446 int alias(char *name)
447 {
448         FILE *fp;
449         int a;
450         char abuf[256];
451         
452         fp=fopen(ALIASES,"r");
453         if (fp==NULL) {
454                 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
455                 return(2);
456                 }
457
458         while (fgets(abuf,256,fp)!=NULL) {
459                 strip_trailing_whitespace(abuf);
460                 for (a=0; a<strlen(abuf); ++a) {
461                         if (abuf[a]==',') {
462                                 abuf[a]=0;
463                                 if (!strcasecmp(name,abuf)) {
464                                         strcpy(name,&abuf[a+1]);
465                                         }
466                                 }
467                         }
468                 }
469         fclose(fp);
470         return(0);
471         }
472
473
474 void deliver(char recp[], int is_test, int deliver_to_ignet) {
475
476         /* various ways we can deliver mail... */
477
478         if (deliver_to_ignet) {
479                 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
480                 if (is_test == 0) do_citmail(recp, REMOTE);
481                 }
482
483         else if (!strcmp(recp,"uudecode")) {
484                 syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
485                 if (is_test == 0) do_uudecode(config.c_bucket_dir);
486                 }
487
488         else if (!strcmp(recp,"cit86net")) {
489                 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
490                 if (is_test == 0) {
491                         do_uudecode(CIT86NET);
492                         system("exec ./BatchTranslate86");
493                         }
494                 }
495
496         else if (!strcmp(recp,"null")) {
497                 syslog(LOG_NOTICE,"zapping nulled message");
498                 }
499
500         else {
501                 /* Otherwise, the user is local (or an unknown name was
502                  * specified, in which case we let netproc handle the bounce)
503                  */
504                 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
505                 if (is_test == 0) do_citmail(recp, LOCAL);
506                 }
507
508         }
509
510
511
512 int main(int argc, char **argv)
513 {
514         int is_test = 0;
515         int deliver_to_ignet = 0;
516         int smtp = 0;
517         static char recp[1024], buf[1024];
518         static char user[1024], node[1024], name[1024];
519         int a;
520
521         openlog("citmail", LOG_PID, LOG_USER);
522         get_config();
523         LoadInternetConfig();
524
525         if (!strcmp(argv[1],"-t")) {
526                 is_test = 1;
527                 syslog(LOG_NOTICE,"test mode - will not deliver");
528                 }
529         if (!strcmp(argv[1], "-i")) {
530                 smtp = 1;
531                 syslog(LOG_NOTICE,"started as an SMTP daemon");
532                 }
533
534
535         if (smtp) {
536                 strcpy(recp, "");
537                 }
538         else if (is_test == 0) {
539                 strcpy(recp,argv[1]);
540                 }
541         else {
542                 strcpy(recp,argv[2]);
543                 }
544
545
546         if (smtp) {
547                 /*** SMTP delivery mode ***/
548
549                 printf("200 Citadel/UX SMTP gateway ready.\n");
550         
551                 do {
552                         fflush(stdout);
553                         fflush(stderr);
554                         fgets(buf, 1024, stdin);
555                         while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
556                                 buf[strlen(buf)-1] = 0;
557                                 }
558
559                         /* null-pad to allow some lazy compares */
560                         buf[strlen(buf)+1] = 0;
561                         buf[strlen(buf)+2] = 0;
562                         buf[strlen(buf)+3] = 0;
563         
564                         if (!strncasecmp(buf, "QUIT", 4)) {
565                                 printf("221 Later, dude.\n");
566                                 }
567                         else if (!strncasecmp(buf, "HELP", 4)) {
568                                 printf("214 You think _you_ need help?\n");
569                                 }
570                         else if (!strncasecmp(buf, "HELO", 4)) {
571                                 printf("250 Howdy ho, Mr. Hankey!\n");
572                                 }
573                         else if (!strncasecmp(buf, "MAIL", 4)) {
574                                 printf("250 Sure, whatever...\n");
575                                 }
576
577
578                         else if (!strncasecmp(buf, "RCPT To: ", 9)) {
579                                 if (strlen(recp) > 0) {
580                                         printf("571 Multiple recipients not supported.\n");
581                                         }
582                                 else {
583                                         strcpy(recp, &buf[9]);
584                                         if (haschar(recp, ',')) {
585                                                 printf("571 Multiple recipients not supported.\n");
586                                                 strcpy(recp, "");
587                                                 }
588                                         else {
589                                                 syslog(LOG_NOTICE,"recp: %s",recp);
590                                                 for (a=0; a<2; ++a) {
591                                                         alias(recp);
592                                                         }
593
594                                                 /* did we alias it back to a remote address? */
595                                                 if (    (haschar(recp,'%'))
596                                                 ||      (haschar(recp,'@'))
597                                                 ||      (haschar(recp,'!')) ) {
598         
599                                                         process_rfc822_addr(recp, user, node, name);
600                                                         host_alias(node);
601                 
602                                                         /* If there are dots, it's an Internet host, so feed it
603                                                         * back to an external mail transport agent such as sendmail.
604                                                         */
605                                                         if (haschar(node, '.')) {
606                                                                 printf("571 Away with thee, spammer!\n");
607                                                                 strcpy(recp, "");
608                                                                 }
609                 
610                                                         /* Otherwise, we're dealing with Citadel mail. */
611                                                         else {
612                                                                 snprintf(recp, sizeof recp, "%s!%s", node, user);
613                                                                 deliver_to_ignet = 1;
614                                                                 printf("250 IGnet recipient.\n");
615                                                                 }
616                                                         }
617                                                 else {
618                                                         printf("250 Local recipient.\n");
619                                                         }
620         
621                                                 }
622         
623                                         }
624                                 }
625
626
627
628                         else if (!strncasecmp(buf, "RCPT", 4)) {
629                                 printf("501 Only 'To:' commands are supported.\n");
630                                 }
631                         else if (!strncasecmp(buf, "DATA", 4)) {
632                                 if (strlen(recp) > 0) {
633                                         printf("354 Sock it to me, baby...\n");
634                                         fflush(stdout);
635                                         deliver(recp, is_test, deliver_to_ignet);
636                                         printf("250 Cool beans!\n");
637                                         }
638                                 else {
639                                         printf("503 No recipient has been specified.\n");
640                                         }
641                                 }
642                         else {
643                                 printf("500 Huh?\n");
644                                 }
645         
646                         } while (strncasecmp(buf,"QUIT",4));
647                 }
648
649         else {
650                 /*** Non-SMTP delivery mode ***/
651                 syslog(LOG_NOTICE,"recp: %s",recp);
652                 for (a=0; a<2; ++a) {
653                         alias(recp);
654                         }
655         
656                 /* did we alias it back to a remote address? */
657                 if (    (haschar(recp,'%'))
658                 ||      (haschar(recp,'@'))
659                 ||      (haschar(recp,'!')) ) {
660         
661                         process_rfc822_addr(recp, user, node, name);
662                         host_alias(node);
663                 
664                         /* If there are dots, it's an Internet host, so feed it
665                         * back to an external mail transport agent such as sendmail.
666                         */
667                         if (haschar(node, '.')) {
668                                 snprintf(buf, sizeof buf, SENDMAIL, recp);
669                                 system(buf);
670                                 exit(0);
671                                 }
672         
673                         /* Otherwise, we're dealing with Citadel mail. */
674                         else {
675                                 snprintf(recp, sizeof recp, "%s!%s", node, user);
676                                 deliver_to_ignet = 1;
677                                 }
678         
679                         }
680         
681                 deliver(recp, is_test, deliver_to_ignet);
682                 }
683         
684         closelog();
685         if (RUN_NETPROC) execlp("./netproc","netproc",NULL);
686         exit(0);
687         }
688