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.
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)
37 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
39 char *monthdesc[] = { "Jan","Feb","Mar","Apr","May","Jun",
40 "Jul","Aug","Sep","Oct","Nov","Dec" };
44 void LoadInternetConfig();
55 char OUTGOING_FQDN[128];
58 int struncmp(lstr,rstr,l)
63 if (pos==l) return(0);
64 lc=tolower(lstr[pos]);
65 rc=tolower(rstr[pos]);
66 if ((lc==0)&&(rc==0)) return(0);
67 if (lc<rc) return(-1);
75 int a,b,cpos,tend,tval;
82 tmbuf = (struct tm *)localtime(&now);
84 /* get rid of + or - timezone mods */
85 for (a=0; a<strlen(dbuf); ++a)
86 if ((dbuf[a]=='+')||(dbuf[a]=='-'))
88 strcpy(&dbuf[a],&dbuf[a+1]);
89 } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
91 /* try and extract the time by looking for colons */
93 for (a=strlen(dbuf); a>=0; --a)
94 if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
98 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
100 tmbuf->tm_hour = atoi(&dbuf[cpos]);
101 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
102 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
105 strcpy(&dbuf[cpos],&dbuf[cpos+1]);
106 } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
109 /* next try to extract a month */
112 for (a=0; a<strlen(dbuf); ++a)
114 if (!strncmp(&dbuf[a],monthdesc[b],3)) {
119 tmbuf->tm_mon = tval;
120 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
125 for (a=0; a<strlen(dbuf); ++a)
126 if ((atoi(&dbuf[a])>=1900) && (dbuf[a]!=32)) {
127 tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
128 strcpy(&dbuf[a],&dbuf[a+4]);
131 /* whatever's left is the mday (hopefully) */
133 for (a=0; a<strlen(dbuf); ++a)
134 if ((dbuf[a]!=32)&&(atoi(&dbuf[a])>=1)&&(atoi(&dbuf[a])<=31)
135 && ( (a==0)||(dbuf[a-1]==' ') ) ) {
136 tmbuf->tm_mday = atoi(&dbuf[a]);
137 strcpy(&dbuf[a],&dbuf[a+2]);
140 return((long)mktime(tmbuf));
146 * replacement strerror() for systems that don't have it
152 sprintf(buf,"errno = %d",e);
162 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
166 void strip_trailing_whitespace(buf)
168 while(isspace(buf[strlen(buf)-1]))
169 buf[strlen(buf)-1]=0;
172 int islocalok(char recp[]) {
177 int found_closest_match = 0;
180 for (a=0; a<strlen(a_recp); ++a)
181 if (a_recp[a]=='_') a_recp[a]=32;
182 lookfor = (-1L); if (!struncmp(recp,"cit",3)) lookfor=atol(&recp[3]);
183 us=open("/appl/citadel/usersupp",O_RDONLY);
185 while(read(us,&ust,sizeof(struct usersupp))>0) {
186 if (lookfor == ust.usernum) {
187 strcpy(recp,ust.fullname);
191 if (!strucmp(ust.fullname,a_recp)) {
192 strcpy(recp,ust.fullname);
196 if (!struncmp(ust.fullname,a_recp,strlen(a_recp))) {
197 strcpy(recp,ust.fullname);
198 found_closest_match = 1;
203 if (getpwnam(recp)!=NULL) return(1);
204 if (found_closest_match) return(3);
208 /* strip leading and trailing spaces */
211 while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
212 while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
217 * Check to see if a given FQDN really maps to a Citadel network node
219 void host_alias(char host[]) {
223 /* What name is the local host known by? */
224 /* if (!strucmp(host, config.c_fqdn)) { */
225 if (IsHostLocal(host)) {
226 strcpy(host, config.c_nodename);
230 /* Other hosts in the gateway domain? */
231 for (a=0; a<strlen(host); ++a) {
232 if ((host[a]=='.') && (!strucmp(&host[a+1], GW_DOMAIN))) {
234 for (a=0; a<strlen(host); ++a) {
235 if (host[a]=='.') host[a] = 0;
241 /* Otherwise, do nothing... */
247 * Split an RFC822-style address into userid, host, and full name
249 void process_rfc822_addr(rfc822,user,node,name)
256 /* extract full name - first, it's From minus <userid> */
258 for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
260 strcpy(&name[a],&name[a+1]);
261 } while ( (strlen(name) > 0) && (name[a]!='>') );
262 strcpy(&name[a],&name[a+1]);
265 /* strip anything to the left of a bang */
266 while ( (strlen(name)>0) && (haschar(name,'!')>0) )
267 strcpy(name,&name[1]);
269 /* and anything to the right of a @ or % */
270 for (a=0; a<strlen(name); ++a) {
271 if (name[a]=='@') name[a]=0;
272 if (name[a]=='%') name[a]=0;
275 /* but if there are parentheses, that changes the rules... */
276 if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
278 while ( (strlen(name) > 0) && (name[0]!='(') ) {
279 strcpy(&name[0],&name[1]);
281 strcpy(&name[0],&name[1]);
282 for (a=0; a<strlen(name); ++a)
283 if (name[a]==')') name[a]=0;
286 /* but if there are a set of quotes, that supersedes everything */
287 if (haschar(rfc822,34)==2) {
289 while ( (strlen(name) > 0) && (name[0]!=34) ) {
290 strcpy(&name[0],&name[1]);
292 strcpy(&name[0],&name[1]);
293 for (a=0; a<strlen(name); ++a)
294 if (name[a]==34) name[a]=0;
297 /* extract user id */
300 /* first get rid of anything in parens */
301 for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
303 strcpy(&user[a],&user[a+1]);
304 } while ( (strlen(user) > 0) && (user[a]!=')') );
305 strcpy(&user[a],&user[a+1]);
308 /* if there's a set of angle brackets, strip it down to that */
309 if ( (haschar(user,'<') == 1) && (haschar(user,'>') == 1) ) {
310 while ( (strlen(user) > 0) && (user[0]!='<') ) {
311 strcpy(&user[0],&user[1]);
313 strcpy(&user[0],&user[1]);
314 for (a=0; a<strlen(user); ++a)
315 if (user[a]=='>') user[a]=0;
318 /* strip anything to the left of a bang */
319 while ( (strlen(user)>0) && (haschar(user,'!')>0) )
320 strcpy(user,&user[1]);
322 /* and anything to the right of a @ or % */
323 for (a=0; a<strlen(user); ++a) {
324 if (user[a]=='@') user[a]=0;
325 if (user[a]=='%') user[a]=0;
330 /* extract node name */
331 strcpy(node, rfc822);
333 /* first get rid of anything in parens */
334 for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
336 strcpy(&node[a],&node[a+1]);
337 } while ( (strlen(node) > 0) && (node[a]!=')') );
338 strcpy(&node[a],&node[a+1]);
341 /* if there's a set of angle brackets, strip it down to that */
342 if ( (haschar(node,'<') == 1) && (haschar(node,'>') == 1) ) {
343 while ( (strlen(node) > 0) && (node[0]!='<') ) {
344 strcpy(&node[0],&node[1]);
346 strcpy(&node[0],&node[1]);
347 for (a=0; a<strlen(node); ++a)
348 if (node[a]=='>') node[a]=0;
351 /* strip anything to the left of a @ */
352 while ( (strlen(node)>0) && (haschar(node,'@')>0) )
353 strcpy(node,&node[1]);
355 /* strip anything to the left of a % */
356 while ( (strlen(node)>0) && (haschar(node,'%')>0) )
357 strcpy(node,&node[1]);
359 /* reduce multiple system bang paths to node!user */
360 while ( (strlen(node)>0) && (haschar(node,'!')>1) )
361 strcpy(node,&node[1]);
363 /* now get rid of the user portion of a node!user string */
364 for (a=0; a<strlen(node); ++a) if (node[a]=='!') node[a]=0;
368 /* strip leading and trailing spaces in all strings */
375 * Copy line by line, ending at EOF or a "."
377 void loopcopy(FILE *to, FILE *from) {
382 r = fgets(buf, sizeof(buf), from);
383 if (r == NULL) return;
384 strip_trailing_whitespace(buf);
385 if (!strcmp(buf, ".")) return;
386 fprintf(to, "%s\n", buf);
392 * pipe message through netproc
394 void do_citmail(char recp[], int dtype) {
410 /* get the Citadel node name out of the path */
411 strncpy(destsys, recp, sizeof(destsys) );
412 for (a=0; a<strlen(destsys); ++a) {
413 if ((destsys[a]=='!')||(destsys[a]=='.')) {
418 /* chop the system name out, so we're left with a user */
419 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
421 /* now convert underscores to spaces */
422 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
427 sprintf(from, "postmaster@%s", config.c_nodename);
429 sprintf(buf, "./network/spoolin/citmail.%d", getpid());
430 temp = fopen(buf,"w");
432 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
434 strcpy(nodebuf, config.c_nodename);
436 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
437 strip_trailing_whitespace(buf);
439 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
440 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
441 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
442 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
444 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
446 /* now convert it to Citadel format */
447 fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
448 fprintf(temp,"E%s%c", userbuf, 0);
449 fprintf(temp,"T%ld%c", now, 0);
450 fprintf(temp,"A%s%c", frombuf, 0);
451 fprintf(temp,"OMail%c", 0);
452 fprintf(temp,"N%s%c", nodebuf, 0);
454 fprintf(temp,"D%s%c", destsys, 0);
456 fprintf(temp,"R%s%c", recp, 0);
457 if (strlen(subject)>0) {
458 fprintf(temp,"U%s%c", subject, 0);
461 if (strcmp(buf, ".")) loopcopy(temp, stdin);
466 void do_roommail(recp) /* pipe public message through netproc */
471 char buf[128],userbuf[128],frombuf[128],nodebuf[128];
472 char subject[128], from[256];
475 sprintf(from, "postmaster@%s", config.c_nodename);
476 strcpy(recp,&recp[5]);
477 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=32;
481 sprintf(buf,"./network/spoolin/citmail.%d",getpid());
482 temp = fopen(buf,"w");
484 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
485 strcpy(frombuf,"Internet Mail Gateway");
486 strcpy(nodebuf, config.c_nodename);
488 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
489 strip_trailing_whitespace(buf);
491 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
492 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
493 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
494 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
496 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
498 fprintf(temp,"P%s@%s",userbuf,nodebuf); putc(0,temp);
499 fprintf(temp,"T%ld",now); putc(0,temp);
500 fprintf(temp,"A%s",frombuf); putc(0,temp);
501 fprintf(temp,"O%s",recp); putc(0,temp);
502 fprintf(temp,"N%s",nodebuf); putc(0,temp);
503 if (strlen(subject)>0) {
504 fprintf(temp,"U%s",subject); putc(0,temp);
507 if (strcmp(buf, ".")) loopcopy(temp, stdin);
512 void do_uudecode(target)
514 static char buf[1024];
517 sprintf(buf,"cd %s; uudecode",target);
520 if (fp==NULL) return;
521 while (fgets(buf,1024,stdin)!=NULL) {
522 fprintf(fp,"%s",buf);
528 void do_fallback(recp)
530 static char buf[1024];
533 sprintf(buf, FALLBACK, recp);
535 if (fp==NULL) fp = popen("cat >/dev/null", "w");
546 fp=fopen(ALIASES,"r");
548 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
552 while (fgets(abuf,256,fp)!=NULL) {
553 strip_trailing_whitespace(abuf);
554 for (a=0; a<strlen(abuf); ++a) {
557 if (!strucmp(name,abuf)) {
558 strcpy(name,&abuf[a+1]);
568 void deliver(char recp[], int is_test, int deliver_to_ignet) {
573 /* various ways we can deliver mail... */
575 if (deliver_to_ignet) {
576 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
577 if (is_test == 0) do_citmail(recp, REMOTE);
580 else if (!strcmp(recp,"uudecode")) {
581 syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
582 if (is_test == 0) do_uudecode(config.c_bucket_dir);
585 else if (!strcmp(recp,"cit86net")) {
586 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
588 do_uudecode(CIT86NET);
589 system("exec ./BatchTranslate86");
593 else if (!strcmp(recp,"null")) {
594 syslog(LOG_NOTICE,"zapping nulled message");
597 else if (!struncmp(recp,"room_",5)) {
598 syslog(LOG_NOTICE,"to room %s",recp);
599 if (is_test == 0) do_roommail(recp);
603 syslog(LOG_NOTICE,"fallback mailer to user %s",recp);
604 if (is_test == 0) do_fallback(recp);
608 /* Otherwise, the user is local (or an unknown name was specified, in
609 * which case we let netproc handle the bounce)
611 syslog(LOG_NOTICE,"to Citadel user %s",recp);
612 if (is_test == 0) do_citmail(recp, LOCAL);
623 int deliver_to_ignet = 0;
625 static char recp[1024], buf[1024];
626 static char user[1024], node[1024], name[1024];
629 openlog("citmail", LOG_PID, LOG_USER);
631 LoadInternetConfig();
633 if (!strcmp(argv[1],"-t")) {
635 syslog(LOG_NOTICE,"test mode - will not deliver");
637 if (!strcmp(argv[1], "-i")) {
639 syslog(LOG_NOTICE,"started as an SMTP daemon");
646 else if (is_test == 0) {
647 strcpy(recp,argv[1]);
650 strcpy(recp,argv[2]);
655 /*** SMTP delivery mode ***/
657 printf("200 Citadel/UX SMTP gateway ready.\n");
662 fgets(buf, 1024, stdin);
663 while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
664 buf[strlen(buf)-1] = 0;
667 /* null-pad to allow some lazy compares */
668 buf[strlen(buf)+1] = 0;
669 buf[strlen(buf)+2] = 0;
670 buf[strlen(buf)+3] = 0;
672 if (!struncmp(buf, "QUIT", 4)) {
673 printf("221 Later, dude.\n");
675 else if (!struncmp(buf, "HELP", 4)) {
676 printf("214 You think _you_ need help?\n");
678 else if (!struncmp(buf, "HELO", 4)) {
679 printf("250 Howdy ho, Mr. Hankey!\n");
681 else if (!struncmp(buf, "MAIL", 4)) {
682 printf("250 Sure, whatever...\n");
686 else if (!struncmp(buf, "RCPT To: ", 9)) {
687 if (strlen(recp) > 0) {
688 printf("571 Multiple recipients not supported.\n");
691 strcpy(recp, &buf[9]);
692 if (haschar(recp, ',')) {
693 printf("571 Multiple recipients not supported.\n");
697 syslog(LOG_NOTICE,"recp: %s",recp);
698 for (a=0; a<2; ++a) {
702 /* did we alias it back to a remote address? */
703 if ( (haschar(recp,'%'))
704 || (haschar(recp,'@'))
705 || (haschar(recp,'!')) ) {
707 process_rfc822_addr(recp, user, node, name);
710 /* If there are dots, it's an Internet host, so feed it
711 * back to an external mail transport agent such as sendmail.
713 if (haschar(node, '.')) {
714 printf("571 Away with thee, spammer!\n");
718 /* Otherwise, we're dealing with Citadel mail. */
720 sprintf(recp, "%s!%s", node, user);
721 deliver_to_ignet = 1;
722 printf("250 IGnet recipient.\n");
726 printf("250 Local recipient.\n");
736 else if (!struncmp(buf, "RCPT", 4)) {
737 printf("501 Only 'To:' commands are supported.\n");
739 else if (!struncmp(buf, "DATA", 4)) {
740 if (strlen(recp) > 0) {
741 printf("354 Sock it to me, baby...\n");
743 deliver(recp, is_test, deliver_to_ignet);
744 printf("250 Cool beans!\n");
747 printf("503 No recipient has been specified.\n");
751 printf("500 Huh?\n");
754 } while (struncmp(buf,"QUIT",4));
758 /*** Non-SMTP delivery mode ***/
759 syslog(LOG_NOTICE,"recp: %s",recp);
760 for (a=0; a<2; ++a) {
764 /* did we alias it back to a remote address? */
765 if ( (haschar(recp,'%'))
766 || (haschar(recp,'@'))
767 || (haschar(recp,'!')) ) {
769 process_rfc822_addr(recp, user, node, name);
772 /* If there are dots, it's an Internet host, so feed it
773 * back to an external mail transport agent such as sendmail.
775 if (haschar(node, '.')) {
776 sprintf(buf, SENDMAIL, recp);
781 /* Otherwise, we're dealing with Citadel mail. */
783 sprintf(recp, "%s!%s", node, user);
784 deliver_to_ignet = 1;
789 deliver(recp, is_test, deliver_to_ignet);
793 if (RUN_NETPROC) execlp("./netproc","netproc",NULL);