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];
60 int a,b,cpos,tend,tval;
67 tmbuf = (struct tm *)localtime(&now);
69 /* get rid of + or - timezone mods */
70 for (a=0; a<strlen(dbuf); ++a)
71 if ((dbuf[a]=='+')||(dbuf[a]=='-'))
73 strcpy(&dbuf[a],&dbuf[a+1]);
74 } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
76 /* try and extract the time by looking for colons */
78 for (a=strlen(dbuf); a>=0; --a)
79 if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
83 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
85 tmbuf->tm_hour = atoi(&dbuf[cpos]);
86 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
87 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
90 strcpy(&dbuf[cpos],&dbuf[cpos+1]);
91 } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
94 /* next try to extract a month */
97 for (a=0; a<strlen(dbuf); ++a)
99 if (!strncmp(&dbuf[a],monthdesc[b],3)) {
104 tmbuf->tm_mon = tval;
105 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
110 for (a=0; a<strlen(dbuf); ++a)
111 if ((atoi(&dbuf[a])>=1900) && (dbuf[a]!=32)) {
112 tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
113 strcpy(&dbuf[a],&dbuf[a+4]);
116 /* whatever's left is the mday (hopefully) */
118 for (a=0; a<strlen(dbuf); ++a)
119 if ((dbuf[a]!=32)&&(atoi(&dbuf[a])>=1)&&(atoi(&dbuf[a])<=31)
120 && ( (a==0)||(dbuf[a-1]==' ') ) ) {
121 tmbuf->tm_mday = atoi(&dbuf[a]);
122 strcpy(&dbuf[a],&dbuf[a+2]);
125 return((long)mktime(tmbuf));
131 * replacement strerror() for systems that don't have it
137 sprintf(buf,"errno = %d",e);
147 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
151 void strip_trailing_whitespace(buf)
153 while(isspace(buf[strlen(buf)-1]))
154 buf[strlen(buf)-1]=0;
157 /* strip leading and trailing spaces */
160 while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
161 while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
166 * Check to see if a given FQDN really maps to a Citadel network node
168 void host_alias(char host[]) {
172 /* What name is the local host known by? */
173 /* if (!strcasecmp(host, config.c_fqdn)) { */
174 if (IsHostLocal(host)) {
175 strcpy(host, config.c_nodename);
179 /* Other hosts in the gateway domain? */
180 for (a=0; a<strlen(host); ++a) {
181 if ((host[a]=='.') && (!strcasecmp(&host[a+1], GW_DOMAIN))) {
183 for (a=0; a<strlen(host); ++a) {
184 if (host[a]=='.') host[a] = 0;
190 /* Otherwise, do nothing... */
196 * Split an RFC822-style address into userid, host, and full name
198 void process_rfc822_addr(rfc822,user,node,name)
205 /* extract full name - first, it's From minus <userid> */
207 for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
209 strcpy(&name[a],&name[a+1]);
210 } while ( (strlen(name) > 0) && (name[a]!='>') );
211 strcpy(&name[a],&name[a+1]);
214 /* strip anything to the left of a bang */
215 while ( (strlen(name)>0) && (haschar(name,'!')>0) )
216 strcpy(name,&name[1]);
218 /* and anything to the right of a @ or % */
219 for (a=0; a<strlen(name); ++a) {
220 if (name[a]=='@') name[a]=0;
221 if (name[a]=='%') name[a]=0;
224 /* but if there are parentheses, that changes the rules... */
225 if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
227 while ( (strlen(name) > 0) && (name[0]!='(') ) {
228 strcpy(&name[0],&name[1]);
230 strcpy(&name[0],&name[1]);
231 for (a=0; a<strlen(name); ++a)
232 if (name[a]==')') name[a]=0;
235 /* but if there are a set of quotes, that supersedes everything */
236 if (haschar(rfc822,34)==2) {
238 while ( (strlen(name) > 0) && (name[0]!=34) ) {
239 strcpy(&name[0],&name[1]);
241 strcpy(&name[0],&name[1]);
242 for (a=0; a<strlen(name); ++a)
243 if (name[a]==34) name[a]=0;
246 /* extract user id */
249 /* first get rid of anything in parens */
250 for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
252 strcpy(&user[a],&user[a+1]);
253 } while ( (strlen(user) > 0) && (user[a]!=')') );
254 strcpy(&user[a],&user[a+1]);
257 /* if there's a set of angle brackets, strip it down to that */
258 if ( (haschar(user,'<') == 1) && (haschar(user,'>') == 1) ) {
259 while ( (strlen(user) > 0) && (user[0]!='<') ) {
260 strcpy(&user[0],&user[1]);
262 strcpy(&user[0],&user[1]);
263 for (a=0; a<strlen(user); ++a)
264 if (user[a]=='>') user[a]=0;
267 /* strip anything to the left of a bang */
268 while ( (strlen(user)>0) && (haschar(user,'!')>0) )
269 strcpy(user,&user[1]);
271 /* and anything to the right of a @ or % */
272 for (a=0; a<strlen(user); ++a) {
273 if (user[a]=='@') user[a]=0;
274 if (user[a]=='%') user[a]=0;
279 /* extract node name */
280 strcpy(node, rfc822);
282 /* first get rid of anything in parens */
283 for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
285 strcpy(&node[a],&node[a+1]);
286 } while ( (strlen(node) > 0) && (node[a]!=')') );
287 strcpy(&node[a],&node[a+1]);
290 /* if there's a set of angle brackets, strip it down to that */
291 if ( (haschar(node,'<') == 1) && (haschar(node,'>') == 1) ) {
292 while ( (strlen(node) > 0) && (node[0]!='<') ) {
293 strcpy(&node[0],&node[1]);
295 strcpy(&node[0],&node[1]);
296 for (a=0; a<strlen(node); ++a)
297 if (node[a]=='>') node[a]=0;
300 /* strip anything to the left of a @ */
301 while ( (strlen(node)>0) && (haschar(node,'@')>0) )
302 strcpy(node,&node[1]);
304 /* strip anything to the left of a % */
305 while ( (strlen(node)>0) && (haschar(node,'%')>0) )
306 strcpy(node,&node[1]);
308 /* reduce multiple system bang paths to node!user */
309 while ( (strlen(node)>0) && (haschar(node,'!')>1) )
310 strcpy(node,&node[1]);
312 /* now get rid of the user portion of a node!user string */
313 for (a=0; a<strlen(node); ++a) if (node[a]=='!') node[a]=0;
317 /* strip leading and trailing spaces in all strings */
324 * Copy line by line, ending at EOF or a "."
326 void loopcopy(FILE *to, FILE *from) {
331 r = fgets(buf, sizeof(buf), from);
332 if (r == NULL) return;
333 strip_trailing_whitespace(buf);
334 if (!strcmp(buf, ".")) return;
335 fprintf(to, "%s\n", buf);
341 * pipe message through netproc
343 void do_citmail(char recp[], int dtype) {
359 /* get the Citadel node name out of the path */
360 strncpy(destsys, recp, sizeof(destsys) );
361 for (a=0; a<strlen(destsys); ++a) {
362 if ((destsys[a]=='!')||(destsys[a]=='.')) {
367 /* chop the system name out, so we're left with a user */
368 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
370 /* now convert underscores to spaces */
371 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
376 sprintf(from, "postmaster@%s", config.c_nodename);
378 sprintf(buf, "./network/spoolin/citmail.%d", getpid());
379 temp = fopen(buf,"w");
381 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
383 strcpy(nodebuf, config.c_nodename);
385 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
386 strip_trailing_whitespace(buf);
388 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
389 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
390 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
391 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
393 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
395 /* now convert it to Citadel format */
396 fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
397 fprintf(temp,"E%s%c", userbuf, 0);
398 fprintf(temp,"T%ld%c", now, 0);
399 fprintf(temp,"A%s%c", frombuf, 0);
400 fprintf(temp,"OMail%c", 0);
401 fprintf(temp,"N%s%c", nodebuf, 0);
403 fprintf(temp,"D%s%c", destsys, 0);
405 fprintf(temp,"R%s%c", recp, 0);
406 if (strlen(subject)>0) {
407 fprintf(temp,"U%s%c", subject, 0);
410 if (strcmp(buf, ".")) loopcopy(temp, stdin);
415 void do_roommail(recp) /* pipe public message through netproc */
420 char buf[128],userbuf[128],frombuf[128],nodebuf[128];
421 char subject[128], from[256];
424 sprintf(from, "postmaster@%s", config.c_nodename);
425 strcpy(recp,&recp[5]);
426 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=32;
430 sprintf(buf,"./network/spoolin/citmail.%d",getpid());
431 temp = fopen(buf,"w");
433 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
434 strcpy(frombuf,"Internet Mail Gateway");
435 strcpy(nodebuf, config.c_nodename);
437 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
438 strip_trailing_whitespace(buf);
440 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
441 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
442 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
443 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
445 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
447 fprintf(temp,"P%s@%s",userbuf,nodebuf); putc(0,temp);
448 fprintf(temp,"T%ld",now); putc(0,temp);
449 fprintf(temp,"A%s",frombuf); putc(0,temp);
450 fprintf(temp,"O%s",recp); putc(0,temp);
451 fprintf(temp,"N%s",nodebuf); putc(0,temp);
452 if (strlen(subject)>0) {
453 fprintf(temp,"U%s",subject); putc(0,temp);
456 if (strcmp(buf, ".")) loopcopy(temp, stdin);
461 void do_uudecode(target)
463 static char buf[1024];
466 sprintf(buf,"cd %s; uudecode",target);
469 if (fp==NULL) return;
470 while (fgets(buf,1024,stdin)!=NULL) {
471 fprintf(fp,"%s",buf);
477 void do_fallback(recp)
479 static char buf[1024];
482 sprintf(buf, FALLBACK, recp);
484 if (fp==NULL) fp = popen("cat >/dev/null", "w");
495 fp=fopen(ALIASES,"r");
497 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
501 while (fgets(abuf,256,fp)!=NULL) {
502 strip_trailing_whitespace(abuf);
503 for (a=0; a<strlen(abuf); ++a) {
506 if (!strcasecmp(name,abuf)) {
507 strcpy(name,&abuf[a+1]);
517 void deliver(char recp[], int is_test, int deliver_to_ignet) {
519 /* various ways we can deliver mail... */
521 if (deliver_to_ignet) {
522 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
523 if (is_test == 0) do_citmail(recp, REMOTE);
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);
531 else if (!strcmp(recp,"cit86net")) {
532 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
534 do_uudecode(CIT86NET);
535 system("exec ./BatchTranslate86");
539 else if (!strcmp(recp,"null")) {
540 syslog(LOG_NOTICE,"zapping nulled message");
543 else if (!strncasecmp(recp,"room_",5)) {
544 syslog(LOG_NOTICE,"to room %s",recp);
545 if (is_test == 0) do_roommail(recp);
549 /* Otherwise, we treat the recipient as the name of a local
550 * Citadel user. If the username doesn't exist, we still
551 * deliver it to Citadel -- netproc will handle the bounce.
553 syslog(LOG_NOTICE,"to Citadel user %s",recp);
554 if (is_test == 0) do_citmail(recp, LOCAL);
565 int deliver_to_ignet = 0;
567 static char recp[1024], buf[1024];
568 static char user[1024], node[1024], name[1024];
571 openlog("citmail", LOG_PID, LOG_USER);
573 LoadInternetConfig();
575 if (!strcmp(argv[1],"-t")) {
577 syslog(LOG_NOTICE,"test mode - will not deliver");
579 if (!strcmp(argv[1], "-i")) {
581 syslog(LOG_NOTICE,"started as an SMTP daemon");
588 else if (is_test == 0) {
589 strcpy(recp,argv[1]);
592 strcpy(recp,argv[2]);
597 /*** SMTP delivery mode ***/
599 printf("200 Citadel/UX SMTP gateway ready.\n");
604 fgets(buf, 1024, stdin);
605 while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
606 buf[strlen(buf)-1] = 0;
609 /* null-pad to allow some lazy compares */
610 buf[strlen(buf)+1] = 0;
611 buf[strlen(buf)+2] = 0;
612 buf[strlen(buf)+3] = 0;
614 if (!strncasecmp(buf, "QUIT", 4)) {
615 printf("221 Later, dude.\n");
617 else if (!strncasecmp(buf, "HELP", 4)) {
618 printf("214 You think _you_ need help?\n");
620 else if (!strncasecmp(buf, "HELO", 4)) {
621 printf("250 Howdy ho, Mr. Hankey!\n");
623 else if (!strncasecmp(buf, "MAIL", 4)) {
624 printf("250 Sure, whatever...\n");
628 else if (!strncasecmp(buf, "RCPT To: ", 9)) {
629 if (strlen(recp) > 0) {
630 printf("571 Multiple recipients not supported.\n");
633 strcpy(recp, &buf[9]);
634 if (haschar(recp, ',')) {
635 printf("571 Multiple recipients not supported.\n");
639 syslog(LOG_NOTICE,"recp: %s",recp);
640 for (a=0; a<2; ++a) {
644 /* did we alias it back to a remote address? */
645 if ( (haschar(recp,'%'))
646 || (haschar(recp,'@'))
647 || (haschar(recp,'!')) ) {
649 process_rfc822_addr(recp, user, node, name);
652 /* If there are dots, it's an Internet host, so feed it
653 * back to an external mail transport agent such as sendmail.
655 if (haschar(node, '.')) {
656 printf("571 Away with thee, spammer!\n");
660 /* Otherwise, we're dealing with Citadel mail. */
662 sprintf(recp, "%s!%s", node, user);
663 deliver_to_ignet = 1;
664 printf("250 IGnet recipient.\n");
668 printf("250 Local recipient.\n");
678 else if (!strncasecmp(buf, "RCPT", 4)) {
679 printf("501 Only 'To:' commands are supported.\n");
681 else if (!strncasecmp(buf, "DATA", 4)) {
682 if (strlen(recp) > 0) {
683 printf("354 Sock it to me, baby...\n");
685 deliver(recp, is_test, deliver_to_ignet);
686 printf("250 Cool beans!\n");
689 printf("503 No recipient has been specified.\n");
693 printf("500 Huh?\n");
696 } while (strncasecmp(buf,"QUIT",4));
700 /*** Non-SMTP delivery mode ***/
701 syslog(LOG_NOTICE,"recp: %s",recp);
702 for (a=0; a<2; ++a) {
706 /* did we alias it back to a remote address? */
707 if ( (haschar(recp,'%'))
708 || (haschar(recp,'@'))
709 || (haschar(recp,'!')) ) {
711 process_rfc822_addr(recp, user, node, name);
714 /* If there are dots, it's an Internet host, so feed it
715 * back to an external mail transport agent such as sendmail.
717 if (haschar(node, '.')) {
718 sprintf(buf, SENDMAIL, recp);
723 /* Otherwise, we're dealing with Citadel mail. */
725 sprintf(recp, "%s!%s", node, user);
726 deliver_to_ignet = 1;
731 deliver(recp, is_test, deliver_to_ignet);
735 if (RUN_NETPROC) execlp("./netproc","netproc",NULL);