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];
61 int a,b,cpos,tend,tval;
68 tmbuf = (struct tm *)localtime(&now);
70 /* get rid of + or - timezone mods */
71 for (a=0; a<strlen(dbuf); ++a)
72 if ((dbuf[a]=='+')||(dbuf[a]=='-'))
74 strcpy(&dbuf[a],&dbuf[a+1]);
75 } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
77 /* try and extract the time by looking for colons */
79 for (a=strlen(dbuf); a>=0; --a)
80 if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
84 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
86 tmbuf->tm_hour = atoi(&dbuf[cpos]);
87 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
88 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
91 strcpy(&dbuf[cpos],&dbuf[cpos+1]);
92 } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
95 /* next try to extract a month */
98 for (a=0; a<strlen(dbuf); ++a)
100 if (!strncmp(&dbuf[a],monthdesc[b],3)) {
105 tmbuf->tm_mon = tval;
106 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
111 for (a=0; a<strlen(dbuf); ++a)
112 if ((atoi(&dbuf[a])>=1900) && (dbuf[a]!=32)) {
113 tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
114 strcpy(&dbuf[a],&dbuf[a+4]);
117 /* whatever's left is the mday (hopefully) */
119 for (a=0; a<strlen(dbuf); ++a)
120 if ((dbuf[a]!=32)&&(atoi(&dbuf[a])>=1)&&(atoi(&dbuf[a])<=31)
121 && ( (a==0)||(dbuf[a-1]==' ') ) ) {
122 tmbuf->tm_mday = atoi(&dbuf[a]);
123 strcpy(&dbuf[a],&dbuf[a+2]);
126 return((long)mktime(tmbuf));
132 * replacement strerror() for systems that don't have it
138 sprintf(buf,"errno = %d",e);
148 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
152 void strip_trailing_whitespace(buf)
154 while(isspace(buf[strlen(buf)-1]))
155 buf[strlen(buf)-1]=0;
158 /* strip leading and trailing spaces */
161 while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
162 while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
167 * Check to see if a given FQDN really maps to a Citadel network node
169 void host_alias(char host[]) {
173 /* What name is the local host known by? */
174 /* if (!strcasecmp(host, config.c_fqdn)) { */
175 if (IsHostLocal(host)) {
176 strcpy(host, config.c_nodename);
180 /* Other hosts in the gateway domain? */
181 for (a=0; a<strlen(host); ++a) {
182 if ((host[a]=='.') && (!strcasecmp(&host[a+1], GW_DOMAIN))) {
184 for (a=0; a<strlen(host); ++a) {
185 if (host[a]=='.') host[a] = 0;
191 /* Otherwise, do nothing... */
197 * Split an RFC822-style address into userid, host, and full name
199 void process_rfc822_addr(rfc822,user,node,name)
206 /* extract full name - first, it's From minus <userid> */
208 for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
210 strcpy(&name[a],&name[a+1]);
211 } while ( (strlen(name) > 0) && (name[a]!='>') );
212 strcpy(&name[a],&name[a+1]);
215 /* strip anything to the left of a bang */
216 while ( (strlen(name)>0) && (haschar(name,'!')>0) )
217 strcpy(name,&name[1]);
219 /* and anything to the right of a @ or % */
220 for (a=0; a<strlen(name); ++a) {
221 if (name[a]=='@') name[a]=0;
222 if (name[a]=='%') name[a]=0;
225 /* but if there are parentheses, that changes the rules... */
226 if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
228 while ( (strlen(name) > 0) && (name[0]!='(') ) {
229 strcpy(&name[0],&name[1]);
231 strcpy(&name[0],&name[1]);
232 for (a=0; a<strlen(name); ++a)
233 if (name[a]==')') name[a]=0;
236 /* but if there are a set of quotes, that supersedes everything */
237 if (haschar(rfc822,34)==2) {
239 while ( (strlen(name) > 0) && (name[0]!=34) ) {
240 strcpy(&name[0],&name[1]);
242 strcpy(&name[0],&name[1]);
243 for (a=0; a<strlen(name); ++a)
244 if (name[a]==34) name[a]=0;
247 /* extract user id */
250 /* first get rid of anything in parens */
251 for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
253 strcpy(&user[a],&user[a+1]);
254 } while ( (strlen(user) > 0) && (user[a]!=')') );
255 strcpy(&user[a],&user[a+1]);
258 /* if there's a set of angle brackets, strip it down to that */
259 if ( (haschar(user,'<') == 1) && (haschar(user,'>') == 1) ) {
260 while ( (strlen(user) > 0) && (user[0]!='<') ) {
261 strcpy(&user[0],&user[1]);
263 strcpy(&user[0],&user[1]);
264 for (a=0; a<strlen(user); ++a)
265 if (user[a]=='>') user[a]=0;
268 /* strip anything to the left of a bang */
269 while ( (strlen(user)>0) && (haschar(user,'!')>0) )
270 strcpy(user,&user[1]);
272 /* and anything to the right of a @ or % */
273 for (a=0; a<strlen(user); ++a) {
274 if (user[a]=='@') user[a]=0;
275 if (user[a]=='%') user[a]=0;
280 /* extract node name */
281 strcpy(node, rfc822);
283 /* first get rid of anything in parens */
284 for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
286 strcpy(&node[a],&node[a+1]);
287 } while ( (strlen(node) > 0) && (node[a]!=')') );
288 strcpy(&node[a],&node[a+1]);
291 /* if there's a set of angle brackets, strip it down to that */
292 if ( (haschar(node,'<') == 1) && (haschar(node,'>') == 1) ) {
293 while ( (strlen(node) > 0) && (node[0]!='<') ) {
294 strcpy(&node[0],&node[1]);
296 strcpy(&node[0],&node[1]);
297 for (a=0; a<strlen(node); ++a)
298 if (node[a]=='>') node[a]=0;
301 /* strip anything to the left of a @ */
302 while ( (strlen(node)>0) && (haschar(node,'@')>0) )
303 strcpy(node,&node[1]);
305 /* strip anything to the left of a % */
306 while ( (strlen(node)>0) && (haschar(node,'%')>0) )
307 strcpy(node,&node[1]);
309 /* reduce multiple system bang paths to node!user */
310 while ( (strlen(node)>0) && (haschar(node,'!')>1) )
311 strcpy(node,&node[1]);
313 /* now get rid of the user portion of a node!user string */
314 for (a=0; a<strlen(node); ++a) if (node[a]=='!') node[a]=0;
318 /* strip leading and trailing spaces in all strings */
325 * Copy line by line, ending at EOF or a "."
327 void loopcopy(FILE *to, FILE *from) {
332 r = fgets(buf, sizeof(buf), from);
333 if (r == NULL) return;
334 strip_trailing_whitespace(buf);
335 if (!strcmp(buf, ".")) return;
336 fprintf(to, "%s\n", buf);
342 * pipe message through netproc
344 void do_citmail(char recp[], int dtype) {
356 char targetroom[256];
361 /* get the Citadel node name out of the path */
362 strncpy(destsys, recp, sizeof(destsys) );
363 for (a=0; a<strlen(destsys); ++a) {
364 if ((destsys[a]=='!')||(destsys[a]=='.')) {
369 /* chop the system name out, so we're left with a user */
370 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
373 /* Convert underscores to spaces */
374 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
376 /* Are we delivering to a room instead of a user? */
377 if (!strncasecmp(recp, "room ", 5)) {
378 strcpy(targetroom, &recp[5]);
382 strcpy(targetroom, "Mail");
386 sprintf(from, "postmaster@%s", config.c_nodename);
388 sprintf(buf, "./network/spoolin/citmail.%d", getpid());
389 temp = fopen(buf,"w");
391 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
393 strcpy(nodebuf, config.c_nodename);
395 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
396 strip_trailing_whitespace(buf);
398 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
399 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
400 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
401 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
403 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
405 /* now convert it to Citadel format */
406 fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
407 fprintf(temp,"T%ld%c", now, 0);
408 fprintf(temp,"A%s%c", userbuf, 0);
410 if (strlen(targetroom) > 0) {
411 fprintf(temp, "O%s%c", targetroom, 0);
414 fprintf(temp, "OMail%c", 0);
417 fprintf(temp,"N%s%c", nodebuf, 0);
418 fprintf(temp,"H%s%c", frombuf, 0);
420 fprintf(temp,"D%s%c", destsys, 0);
423 if (strlen(recp) > 0) {
424 fprintf(temp,"R%s%c", recp, 0);
427 if (strlen(subject)>0) {
428 fprintf(temp,"U%s%c", subject, 0);
431 if (strcmp(buf, ".")) loopcopy(temp, stdin);
437 void do_uudecode(target)
439 static char buf[1024];
442 sprintf(buf,"cd %s; uudecode",target);
445 if (fp==NULL) return;
446 while (fgets(buf,1024,stdin)!=NULL) {
447 fprintf(fp,"%s",buf);
459 fp=fopen(ALIASES,"r");
461 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
465 while (fgets(abuf,256,fp)!=NULL) {
466 strip_trailing_whitespace(abuf);
467 for (a=0; a<strlen(abuf); ++a) {
470 if (!strcasecmp(name,abuf)) {
471 strcpy(name,&abuf[a+1]);
481 void deliver(char recp[], int is_test, int deliver_to_ignet) {
483 /* various ways we can deliver mail... */
485 if (deliver_to_ignet) {
486 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
487 if (is_test == 0) do_citmail(recp, REMOTE);
490 else if (!strcmp(recp,"uudecode")) {
491 syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
492 if (is_test == 0) do_uudecode(config.c_bucket_dir);
495 else if (!strcmp(recp,"cit86net")) {
496 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
498 do_uudecode(CIT86NET);
499 system("exec ./BatchTranslate86");
503 else if (!strcmp(recp,"null")) {
504 syslog(LOG_NOTICE,"zapping nulled message");
508 /* Otherwise, the user is local (or an unknown name was
509 * specified, in which case we let netproc handle the bounce)
511 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
512 if (is_test == 0) do_citmail(recp, LOCAL);
523 int deliver_to_ignet = 0;
525 static char recp[1024], buf[1024];
526 static char user[1024], node[1024], name[1024];
529 openlog("citmail", LOG_PID, LOG_USER);
531 LoadInternetConfig();
533 if (!strcmp(argv[1],"-t")) {
535 syslog(LOG_NOTICE,"test mode - will not deliver");
537 if (!strcmp(argv[1], "-i")) {
539 syslog(LOG_NOTICE,"started as an SMTP daemon");
546 else if (is_test == 0) {
547 strcpy(recp,argv[1]);
550 strcpy(recp,argv[2]);
555 /*** SMTP delivery mode ***/
557 printf("200 Citadel/UX SMTP gateway ready.\n");
562 fgets(buf, 1024, stdin);
563 while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
564 buf[strlen(buf)-1] = 0;
567 /* null-pad to allow some lazy compares */
568 buf[strlen(buf)+1] = 0;
569 buf[strlen(buf)+2] = 0;
570 buf[strlen(buf)+3] = 0;
572 if (!strncasecmp(buf, "QUIT", 4)) {
573 printf("221 Later, dude.\n");
575 else if (!strncasecmp(buf, "HELP", 4)) {
576 printf("214 You think _you_ need help?\n");
578 else if (!strncasecmp(buf, "HELO", 4)) {
579 printf("250 Howdy ho, Mr. Hankey!\n");
581 else if (!strncasecmp(buf, "MAIL", 4)) {
582 printf("250 Sure, whatever...\n");
586 else if (!strncasecmp(buf, "RCPT To: ", 9)) {
587 if (strlen(recp) > 0) {
588 printf("571 Multiple recipients not supported.\n");
591 strcpy(recp, &buf[9]);
592 if (haschar(recp, ',')) {
593 printf("571 Multiple recipients not supported.\n");
597 syslog(LOG_NOTICE,"recp: %s",recp);
598 for (a=0; a<2; ++a) {
602 /* did we alias it back to a remote address? */
603 if ( (haschar(recp,'%'))
604 || (haschar(recp,'@'))
605 || (haschar(recp,'!')) ) {
607 process_rfc822_addr(recp, user, node, name);
610 /* If there are dots, it's an Internet host, so feed it
611 * back to an external mail transport agent such as sendmail.
613 if (haschar(node, '.')) {
614 printf("571 Away with thee, spammer!\n");
618 /* Otherwise, we're dealing with Citadel mail. */
620 sprintf(recp, "%s!%s", node, user);
621 deliver_to_ignet = 1;
622 printf("250 IGnet recipient.\n");
626 printf("250 Local recipient.\n");
636 else if (!strncasecmp(buf, "RCPT", 4)) {
637 printf("501 Only 'To:' commands are supported.\n");
639 else if (!strncasecmp(buf, "DATA", 4)) {
640 if (strlen(recp) > 0) {
641 printf("354 Sock it to me, baby...\n");
643 deliver(recp, is_test, deliver_to_ignet);
644 printf("250 Cool beans!\n");
647 printf("503 No recipient has been specified.\n");
651 printf("500 Huh?\n");
654 } while (strncasecmp(buf,"QUIT",4));
658 /*** Non-SMTP delivery mode ***/
659 syslog(LOG_NOTICE,"recp: %s",recp);
660 for (a=0; a<2; ++a) {
664 /* did we alias it back to a remote address? */
665 if ( (haschar(recp,'%'))
666 || (haschar(recp,'@'))
667 || (haschar(recp,'!')) ) {
669 process_rfc822_addr(recp, user, node, name);
672 /* If there are dots, it's an Internet host, so feed it
673 * back to an external mail transport agent such as sendmail.
675 if (haschar(node, '.')) {
676 sprintf(buf, SENDMAIL, recp);
681 /* Otherwise, we're dealing with Citadel mail. */
683 sprintf(recp, "%s!%s", node, user);
684 deliver_to_ignet = 1;
689 deliver(recp, is_test, deliver_to_ignet);
693 if (RUN_NETPROC) execlp("./netproc","netproc",NULL);