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)
32 #include "internetmail.h"
40 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
42 char *monthdesc[] = { "Jan","Feb","Mar","Apr","May","Jun",
43 "Jul","Aug","Sep","Oct","Nov","Dec" };
51 char OUTGOING_FQDN[128];
55 long conv_date(char *sdbuf)
57 int a,b,cpos,tend,tval;
64 tmbuf = (struct tm *)localtime(&now);
66 /* get rid of + or - timezone mods */
67 for (a=0; a<strlen(dbuf); ++a)
68 if ((dbuf[a]=='+')||(dbuf[a]=='-'))
70 strcpy(&dbuf[a],&dbuf[a+1]);
71 } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
73 /* try and extract the time by looking for colons */
75 for (a=strlen(dbuf); a>=0; --a)
76 if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
80 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
82 tmbuf->tm_hour = atoi(&dbuf[cpos]);
83 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
84 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
87 strcpy(&dbuf[cpos],&dbuf[cpos+1]);
88 } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
91 /* next try to extract a month */
94 for (a=0; a<strlen(dbuf); ++a)
96 if (!strncmp(&dbuf[a],monthdesc[b],3)) {
101 tmbuf->tm_mon = tval;
102 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
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]);
113 /* whatever's left is the mday (hopefully) */
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]);
122 return((long)mktime(tmbuf));
126 #ifndef HAVE_STRERROR
128 * replacement strerror() for systems that don't have it
130 char *strerror(int e)
134 sprintf(buf,"errno = %d",e);
139 int haschar(char *st, int ch)
143 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
147 void strip_trailing_whitespace(char *buf)
149 while(isspace(buf[strlen(buf)-1]))
150 buf[strlen(buf)-1]=0;
153 /* strip leading and trailing spaces */
154 void striplt(char *buf)
156 while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
157 while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
162 * Check to see if a given FQDN really maps to a Citadel network node
164 void host_alias(char host[]) {
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);
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))) {
179 for (a=0; a<strlen(host); ++a) {
180 if (host[a]=='.') host[a] = 0;
186 /* Otherwise, do nothing... */
192 * Split an RFC822-style address into userid, host, and full name
194 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
198 /* extract full name - first, it's From minus <userid> */
200 for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
202 strcpy(&name[a],&name[a+1]);
203 } while ( (strlen(name) > 0) && (name[a]!='>') );
204 strcpy(&name[a],&name[a+1]);
207 /* strip anything to the left of a bang */
208 while ( (strlen(name)>0) && (haschar(name,'!')>0) )
209 strcpy(name,&name[1]);
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;
217 /* but if there are parentheses, that changes the rules... */
218 if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
220 while ( (strlen(name) > 0) && (name[0]!='(') ) {
221 strcpy(&name[0],&name[1]);
223 strcpy(&name[0],&name[1]);
224 for (a=0; a<strlen(name); ++a)
225 if (name[a]==')') name[a]=0;
228 /* but if there are a set of quotes, that supersedes everything */
229 if (haschar(rfc822,34)==2) {
231 while ( (strlen(name) > 0) && (name[0]!=34) ) {
232 strcpy(&name[0],&name[1]);
234 strcpy(&name[0],&name[1]);
235 for (a=0; a<strlen(name); ++a)
236 if (name[a]==34) name[a]=0;
239 /* extract user id */
242 /* first get rid of anything in parens */
243 for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
245 strcpy(&user[a],&user[a+1]);
246 } while ( (strlen(user) > 0) && (user[a]!=')') );
247 strcpy(&user[a],&user[a+1]);
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]);
255 strcpy(&user[0],&user[1]);
256 for (a=0; a<strlen(user); ++a)
257 if (user[a]=='>') user[a]=0;
260 /* strip anything to the left of a bang */
261 while ( (strlen(user)>0) && (haschar(user,'!')>0) )
262 strcpy(user,&user[1]);
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;
272 /* extract node name */
273 strcpy(node, rfc822);
275 /* first get rid of anything in parens */
276 for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
278 strcpy(&node[a],&node[a+1]);
279 } while ( (strlen(node) > 0) && (node[a]!=')') );
280 strcpy(&node[a],&node[a+1]);
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]);
288 strcpy(&node[0],&node[1]);
289 for (a=0; a<strlen(node); ++a)
290 if (node[a]=='>') node[a]=0;
293 /* strip anything to the left of a @ */
294 while ( (strlen(node)>0) && (haschar(node,'@')>0) )
295 strcpy(node,&node[1]);
297 /* strip anything to the left of a % */
298 while ( (strlen(node)>0) && (haschar(node,'%')>0) )
299 strcpy(node,&node[1]);
301 /* reduce multiple system bang paths to node!user */
302 while ( (strlen(node)>0) && (haschar(node,'!')>1) )
303 strcpy(node,&node[1]);
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;
310 /* strip leading and trailing spaces in all strings */
317 * Copy line by line, ending at EOF or a "."
319 void loopcopy(FILE *to, FILE *from) {
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);
334 * pipe message through netproc
336 void do_citmail(char recp[], int dtype) {
348 char targetroom[256];
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]=='.')) {
361 /* chop the system name out, so we're left with a user */
362 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
365 /* Convert underscores to spaces */
366 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
368 /* Are we delivering to a room instead of a user? */
369 if (!strncasecmp(recp, "room ", 5)) {
370 strcpy(targetroom, &recp[5]);
374 strcpy(targetroom, "Mail");
378 sprintf(from, "postmaster@%s", config.c_nodename);
380 sprintf(buf, "./network/spoolin/citmail.%d", getpid());
381 temp = fopen(buf,"w");
383 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
385 strcpy(nodebuf, config.c_nodename);
387 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
388 strip_trailing_whitespace(buf);
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, "")) );
395 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
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);
402 if (strlen(targetroom) > 0) {
403 fprintf(temp, "O%s%c", targetroom, 0);
406 fprintf(temp, "OMail%c", 0);
409 fprintf(temp,"N%s%c", nodebuf, 0);
410 fprintf(temp,"H%s%c", frombuf, 0);
412 fprintf(temp,"D%s%c", destsys, 0);
415 if (strlen(recp) > 0) {
416 fprintf(temp,"R%s%c", recp, 0);
419 if (strlen(subject)>0) {
420 fprintf(temp,"U%s%c", subject, 0);
423 if (strcmp(buf, ".")) loopcopy(temp, stdin);
429 void do_uudecode(char *target)
431 static char buf[1024];
434 sprintf(buf,"cd %s; uudecode",target);
437 if (fp==NULL) return;
438 while (fgets(buf,1024,stdin)!=NULL) {
439 fprintf(fp,"%s",buf);
445 int alias(char *name)
451 fp=fopen(ALIASES,"r");
453 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
457 while (fgets(abuf,256,fp)!=NULL) {
458 strip_trailing_whitespace(abuf);
459 for (a=0; a<strlen(abuf); ++a) {
462 if (!strcasecmp(name,abuf)) {
463 strcpy(name,&abuf[a+1]);
473 void deliver(char recp[], int is_test, int deliver_to_ignet) {
475 /* various ways we can deliver mail... */
477 if (deliver_to_ignet) {
478 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
479 if (is_test == 0) do_citmail(recp, REMOTE);
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);
487 else if (!strcmp(recp,"cit86net")) {
488 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
490 do_uudecode(CIT86NET);
491 system("exec ./BatchTranslate86");
495 else if (!strcmp(recp,"null")) {
496 syslog(LOG_NOTICE,"zapping nulled message");
500 /* Otherwise, the user is local (or an unknown name was
501 * specified, in which case we let netproc handle the bounce)
503 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
504 if (is_test == 0) do_citmail(recp, LOCAL);
511 int main(int argc, char **argv)
514 int deliver_to_ignet = 0;
516 static char recp[1024], buf[1024];
517 static char user[1024], node[1024], name[1024];
520 openlog("citmail", LOG_PID, LOG_USER);
522 LoadInternetConfig();
524 if (!strcmp(argv[1],"-t")) {
526 syslog(LOG_NOTICE,"test mode - will not deliver");
528 if (!strcmp(argv[1], "-i")) {
530 syslog(LOG_NOTICE,"started as an SMTP daemon");
537 else if (is_test == 0) {
538 strcpy(recp,argv[1]);
541 strcpy(recp,argv[2]);
546 /*** SMTP delivery mode ***/
548 printf("200 Citadel/UX SMTP gateway ready.\n");
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;
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;
563 if (!strncasecmp(buf, "QUIT", 4)) {
564 printf("221 Later, dude.\n");
566 else if (!strncasecmp(buf, "HELP", 4)) {
567 printf("214 You think _you_ need help?\n");
569 else if (!strncasecmp(buf, "HELO", 4)) {
570 printf("250 Howdy ho, Mr. Hankey!\n");
572 else if (!strncasecmp(buf, "MAIL", 4)) {
573 printf("250 Sure, whatever...\n");
577 else if (!strncasecmp(buf, "RCPT To: ", 9)) {
578 if (strlen(recp) > 0) {
579 printf("571 Multiple recipients not supported.\n");
582 strcpy(recp, &buf[9]);
583 if (haschar(recp, ',')) {
584 printf("571 Multiple recipients not supported.\n");
588 syslog(LOG_NOTICE,"recp: %s",recp);
589 for (a=0; a<2; ++a) {
593 /* did we alias it back to a remote address? */
594 if ( (haschar(recp,'%'))
595 || (haschar(recp,'@'))
596 || (haschar(recp,'!')) ) {
598 process_rfc822_addr(recp, user, node, name);
601 /* If there are dots, it's an Internet host, so feed it
602 * back to an external mail transport agent such as sendmail.
604 if (haschar(node, '.')) {
605 printf("571 Away with thee, spammer!\n");
609 /* Otherwise, we're dealing with Citadel mail. */
611 sprintf(recp, "%s!%s", node, user);
612 deliver_to_ignet = 1;
613 printf("250 IGnet recipient.\n");
617 printf("250 Local recipient.\n");
627 else if (!strncasecmp(buf, "RCPT", 4)) {
628 printf("501 Only 'To:' commands are supported.\n");
630 else if (!strncasecmp(buf, "DATA", 4)) {
631 if (strlen(recp) > 0) {
632 printf("354 Sock it to me, baby...\n");
634 deliver(recp, is_test, deliver_to_ignet);
635 printf("250 Cool beans!\n");
638 printf("503 No recipient has been specified.\n");
642 printf("500 Huh?\n");
645 } while (strncasecmp(buf,"QUIT",4));
649 /*** Non-SMTP delivery mode ***/
650 syslog(LOG_NOTICE,"recp: %s",recp);
651 for (a=0; a<2; ++a) {
655 /* did we alias it back to a remote address? */
656 if ( (haschar(recp,'%'))
657 || (haschar(recp,'@'))
658 || (haschar(recp,'!')) ) {
660 process_rfc822_addr(recp, user, node, name);
663 /* If there are dots, it's an Internet host, so feed it
664 * back to an external mail transport agent such as sendmail.
666 if (haschar(node, '.')) {
667 sprintf(buf, SENDMAIL, recp);
672 /* Otherwise, we're dealing with Citadel mail. */
674 sprintf(recp, "%s!%s", node, user);
675 deliver_to_ignet = 1;
680 deliver(recp, is_test, deliver_to_ignet);
684 if (RUN_NETPROC) execlp("./netproc","netproc",NULL);