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.
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)
33 #include "internetmail.h"
41 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
43 char *monthdesc[] = { "Jan","Feb","Mar","Apr","May","Jun",
44 "Jul","Aug","Sep","Oct","Nov","Dec" };
52 char OUTGOING_FQDN[128];
56 long conv_date(char *sdbuf)
58 int a,b,cpos,tend,tval;
65 tmbuf = (struct tm *)localtime(&now);
67 /* get rid of + or - timezone mods */
68 for (a=0; a<strlen(dbuf); ++a)
69 if ((dbuf[a]=='+')||(dbuf[a]=='-'))
71 strcpy(&dbuf[a],&dbuf[a+1]);
72 } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
74 /* try and extract the time by looking for colons */
76 for (a=strlen(dbuf); a>=0; --a)
77 if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
81 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
83 tmbuf->tm_hour = atoi(&dbuf[cpos]);
84 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
85 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
88 strcpy(&dbuf[cpos],&dbuf[cpos+1]);
89 } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
92 /* next try to extract a month */
95 for (a=0; a<strlen(dbuf); ++a)
97 if (!strncmp(&dbuf[a],monthdesc[b],3)) {
102 tmbuf->tm_mon = tval;
103 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
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]);
114 /* whatever's left is the mday (hopefully) */
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]);
123 return((long)mktime(tmbuf));
127 #ifndef HAVE_STRERROR
129 * replacement strerror() for systems that don't have it
131 char *strerror(int e)
135 snprintf(buf,sizeof buf,"errno = %d",e);
140 int haschar(char *st, int ch)
144 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
148 void strip_trailing_whitespace(char *buf)
150 while(isspace(buf[strlen(buf)-1]))
151 buf[strlen(buf)-1]=0;
154 /* strip leading and trailing spaces */
155 void striplt(char *buf)
157 while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
158 while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
163 * Check to see if a given FQDN really maps to a Citadel network node
165 void host_alias(char host[]) {
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);
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))) {
180 for (a=0; a<strlen(host); ++a) {
181 if (host[a]=='.') host[a] = 0;
187 /* Otherwise, do nothing... */
193 * Split an RFC822-style address into userid, host, and full name
195 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
199 /* extract full name - first, it's From minus <userid> */
201 for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
203 strcpy(&name[a],&name[a+1]);
204 } while ( (strlen(name) > 0) && (name[a]!='>') );
205 strcpy(&name[a],&name[a+1]);
208 /* strip anything to the left of a bang */
209 while ( (strlen(name)>0) && (haschar(name,'!')>0) )
210 strcpy(name,&name[1]);
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;
218 /* but if there are parentheses, that changes the rules... */
219 if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
221 while ( (strlen(name) > 0) && (name[0]!='(') ) {
222 strcpy(&name[0],&name[1]);
224 strcpy(&name[0],&name[1]);
225 for (a=0; a<strlen(name); ++a)
226 if (name[a]==')') name[a]=0;
229 /* but if there are a set of quotes, that supersedes everything */
230 if (haschar(rfc822,34)==2) {
232 while ( (strlen(name) > 0) && (name[0]!=34) ) {
233 strcpy(&name[0],&name[1]);
235 strcpy(&name[0],&name[1]);
236 for (a=0; a<strlen(name); ++a)
237 if (name[a]==34) name[a]=0;
240 /* extract user id */
243 /* first get rid of anything in parens */
244 for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
246 strcpy(&user[a],&user[a+1]);
247 } while ( (strlen(user) > 0) && (user[a]!=')') );
248 strcpy(&user[a],&user[a+1]);
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]);
256 strcpy(&user[0],&user[1]);
257 for (a=0; a<strlen(user); ++a)
258 if (user[a]=='>') user[a]=0;
261 /* strip anything to the left of a bang */
262 while ( (strlen(user)>0) && (haschar(user,'!')>0) )
263 strcpy(user,&user[1]);
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;
273 /* extract node name */
274 strcpy(node, rfc822);
276 /* first get rid of anything in parens */
277 for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
279 strcpy(&node[a],&node[a+1]);
280 } while ( (strlen(node) > 0) && (node[a]!=')') );
281 strcpy(&node[a],&node[a+1]);
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]);
289 strcpy(&node[0],&node[1]);
290 for (a=0; a<strlen(node); ++a)
291 if (node[a]=='>') node[a]=0;
294 /* strip anything to the left of a @ */
295 while ( (strlen(node)>0) && (haschar(node,'@')>0) )
296 strcpy(node,&node[1]);
298 /* strip anything to the left of a % */
299 while ( (strlen(node)>0) && (haschar(node,'%')>0) )
300 strcpy(node,&node[1]);
302 /* reduce multiple system bang paths to node!user */
303 while ( (strlen(node)>0) && (haschar(node,'!')>1) )
304 strcpy(node,&node[1]);
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;
311 /* strip leading and trailing spaces in all strings */
318 * Copy line by line, ending at EOF or a "."
320 void loopcopy(FILE *to, FILE *from) {
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);
335 * pipe message through netproc
337 void do_citmail(char recp[], int dtype) {
349 char targetroom[256];
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]=='.')) {
362 /* chop the system name out, so we're left with a user */
363 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
366 /* Convert underscores to spaces */
367 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
369 /* Are we delivering to a room instead of a user? */
370 if (!strncasecmp(recp, "room ", 5)) {
371 strcpy(targetroom, &recp[5]);
375 strcpy(targetroom, MAILROOM);
379 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
381 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
382 temp = fopen(buf,"w");
384 putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
386 strcpy(nodebuf, config.c_nodename);
388 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
389 strip_trailing_whitespace(buf);
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, "")) );
396 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
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);
403 if (strlen(targetroom) > 0) {
404 fprintf(temp, "O%s%c", targetroom, 0);
407 fprintf(temp, "O%s%c", MAILROOM, 0);
410 fprintf(temp,"N%s%c", nodebuf, 0);
411 fprintf(temp,"H%s%c", frombuf, 0);
413 fprintf(temp,"D%s%c", destsys, 0);
416 if (strlen(recp) > 0) {
417 fprintf(temp,"R%s%c", recp, 0);
420 if (strlen(subject)>0) {
421 fprintf(temp,"U%s%c", subject, 0);
424 if (strcmp(buf, ".")) loopcopy(temp, stdin);
430 void do_uudecode(char *target)
432 static char buf[1024];
435 snprintf(buf,sizeof buf,"cd %s; uudecode",target);
438 if (fp==NULL) return;
439 while (fgets(buf,1024,stdin)!=NULL) {
440 fprintf(fp,"%s",buf);
446 int alias(char *name)
452 fp=fopen(ALIASES,"r");
454 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
458 while (fgets(abuf,256,fp)!=NULL) {
459 strip_trailing_whitespace(abuf);
460 for (a=0; a<strlen(abuf); ++a) {
463 if (!strcasecmp(name,abuf)) {
464 strcpy(name,&abuf[a+1]);
474 void deliver(char recp[], int is_test, int deliver_to_ignet) {
476 /* various ways we can deliver mail... */
478 if (deliver_to_ignet) {
479 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
480 if (is_test == 0) do_citmail(recp, REMOTE);
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);
488 else if (!strcmp(recp,"cit86net")) {
489 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
491 do_uudecode(CIT86NET);
492 system("exec ./BatchTranslate86");
496 else if (!strcmp(recp,"null")) {
497 syslog(LOG_NOTICE,"zapping nulled message");
501 /* Otherwise, the user is local (or an unknown name was
502 * specified, in which case we let netproc handle the bounce)
504 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
505 if (is_test == 0) do_citmail(recp, LOCAL);
512 int main(int argc, char **argv)
515 int deliver_to_ignet = 0;
517 static char recp[1024], buf[1024];
518 static char user[1024], node[1024], name[1024];
521 openlog("citmail", LOG_PID, LOG_USER);
523 LoadInternetConfig();
525 if (!strcmp(argv[1],"-t")) {
527 syslog(LOG_NOTICE,"test mode - will not deliver");
529 if (!strcmp(argv[1], "-i")) {
531 syslog(LOG_NOTICE,"started as an SMTP daemon");
538 else if (is_test == 0) {
539 strcpy(recp,argv[1]);
542 strcpy(recp,argv[2]);
547 /*** SMTP delivery mode ***/
549 printf("200 Citadel/UX SMTP gateway ready.\n");
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;
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;
564 if (!strncasecmp(buf, "QUIT", 4)) {
565 printf("221 Later, dude.\n");
567 else if (!strncasecmp(buf, "HELP", 4)) {
568 printf("214 You think _you_ need help?\n");
570 else if (!strncasecmp(buf, "HELO", 4)) {
571 printf("250 Howdy ho, Mr. Hankey!\n");
573 else if (!strncasecmp(buf, "MAIL", 4)) {
574 printf("250 Sure, whatever...\n");
578 else if (!strncasecmp(buf, "RCPT To: ", 9)) {
579 if (strlen(recp) > 0) {
580 printf("571 Multiple recipients not supported.\n");
583 strcpy(recp, &buf[9]);
584 if (haschar(recp, ',')) {
585 printf("571 Multiple recipients not supported.\n");
589 syslog(LOG_NOTICE,"recp: %s",recp);
590 for (a=0; a<2; ++a) {
594 /* did we alias it back to a remote address? */
595 if ( (haschar(recp,'%'))
596 || (haschar(recp,'@'))
597 || (haschar(recp,'!')) ) {
599 process_rfc822_addr(recp, user, node, name);
602 /* If there are dots, it's an Internet host, so feed it
603 * back to an external mail transport agent such as sendmail.
605 if (haschar(node, '.')) {
606 printf("571 Away with thee, spammer!\n");
610 /* Otherwise, we're dealing with Citadel mail. */
612 snprintf(recp, sizeof recp, "%s!%s", node, user);
613 deliver_to_ignet = 1;
614 printf("250 IGnet recipient.\n");
618 printf("250 Local recipient.\n");
628 else if (!strncasecmp(buf, "RCPT", 4)) {
629 printf("501 Only 'To:' commands are supported.\n");
631 else if (!strncasecmp(buf, "DATA", 4)) {
632 if (strlen(recp) > 0) {
633 printf("354 Sock it to me, baby...\n");
635 deliver(recp, is_test, deliver_to_ignet);
636 printf("250 Cool beans!\n");
639 printf("503 No recipient has been specified.\n");
643 printf("500 Huh?\n");
646 } while (strncasecmp(buf,"QUIT",4));
650 /*** Non-SMTP delivery mode ***/
651 syslog(LOG_NOTICE,"recp: %s",recp);
652 for (a=0; a<2; ++a) {
656 /* did we alias it back to a remote address? */
657 if ( (haschar(recp,'%'))
658 || (haschar(recp,'@'))
659 || (haschar(recp,'!')) ) {
661 process_rfc822_addr(recp, user, node, name);
664 /* If there are dots, it's an Internet host, so feed it
665 * back to an external mail transport agent such as sendmail.
667 if (haschar(node, '.')) {
668 snprintf(buf, sizeof buf, SENDMAIL, recp);
673 /* Otherwise, we're dealing with Citadel mail. */
675 snprintf(recp, sizeof recp, "%s!%s", node, user);
676 deliver_to_ignet = 1;
681 deliver(recp, is_test, deliver_to_ignet);
685 if (RUN_NETPROC) execlp("./netproc","netproc",NULL);