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)
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 snprintf(buf,sizeof 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) {
349 char targetroom[256];
350 char content_type[256];
351 char *extra_headers = NULL;
356 /* get the Citadel node name out of the path */
357 strncpy(destsys, recp, sizeof(destsys) );
358 for (a=0; a<strlen(destsys); ++a) {
359 if ((destsys[a]=='!')||(destsys[a]=='.')) {
364 /* chop the system name out, so we're left with a user */
365 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
368 /* Convert underscores to spaces */
369 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
371 /* Are we delivering to a room instead of a user? */
372 if (!strncasecmp(recp, "room ", 5)) {
373 strcpy(targetroom, &recp[5]);
377 strcpy(targetroom, MAILROOM);
381 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
383 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
384 temp = fopen(buf,"w");
387 strcpy(nodebuf, config.c_nodename);
388 strcpy(content_type, "text/plain");
391 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
392 strip_trailing_whitespace(buf);
394 if (!strncasecmp(buf,"Subject: ",9))
395 strcpy(subject,&buf[9]);
396 else if (!strncasecmp(buf,"Date: ",6))
397 now = conv_date(&buf[6]);
398 else if (!strncasecmp(buf,"From: ",6))
399 strcpy(from, &buf[6]);
400 else if (!strncasecmp(buf,"Content-type: ",14))
401 strcpy(content_type, &buf[14]);
402 else if (!strncasecmp(buf,"From ", 5)) /* ignore */
405 if (extra_headers == NULL) {
406 extra_headers = malloc(strlen(buf)+2);
407 strcpy(extra_headers, "");
410 extra_headers = realloc(extra_headers,
411 (strlen(extra_headers)+strlen(buf)+2));
413 strcat(extra_headers, buf);
414 strcat(extra_headers, "\n");
416 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
418 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
420 if (!strncasecmp(content_type, "text/plain", 10))
421 format_type = 1; /* plain ASCII message */
423 format_type = 4; /* MIME message */
425 /* now convert it to Citadel format */
428 putc(255, temp); /* 0xFF = start-of-message byte */
429 putc(MES_NORMAL, temp); /* Non-anonymous message */
430 putc(format_type, temp); /* Format type */
433 fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
434 fprintf(temp,"T%ld%c", now, 0);
435 fprintf(temp,"A%s%c", userbuf, 0);
438 if (strlen(targetroom) > 0) {
439 fprintf(temp, "O%s%c", targetroom, 0);
442 fprintf(temp, "O%s%c", MAILROOM, 0);
445 fprintf(temp,"N%s%c", nodebuf, 0);
446 fprintf(temp,"H%s%c", frombuf, 0);
448 fprintf(temp,"D%s%c", destsys, 0);
451 if (strlen(recp) > 0) {
452 fprintf(temp,"R%s%c", recp, 0);
455 /* Subject and text */
456 if (strlen(subject)>0) {
457 fprintf(temp,"U%s%c", subject, 0);
460 if (format_type == 4) {
461 fprintf(temp, "Content-type: %s\n", content_type);
462 if (extra_headers != NULL)
463 fprintf(temp, "%s", extra_headers);
466 if (extra_headers != NULL) free(extra_headers);
467 if (strcmp(buf, ".")) loopcopy(temp, stdin);
473 void do_uudecode(char *target)
475 static char buf[1024];
478 snprintf(buf,sizeof buf,"cd %s; uudecode",target);
481 if (fp==NULL) return;
482 while (fgets(buf,1024,stdin)!=NULL) {
483 fprintf(fp,"%s",buf);
489 int alias(char *name)
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");
544 /* Otherwise, the user is local (or an unknown name was
545 * specified, in which case we let netproc handle the bounce)
547 syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
548 if (is_test == 0) do_citmail(recp, LOCAL);
555 int main(int argc, char **argv)
558 int deliver_to_ignet = 0;
559 static char recp[1024], buf[1024];
560 static char user[1024], node[1024], name[1024];
563 openlog("citmail", LOG_PID, LOG_USER);
565 LoadInternetConfig();
567 if (!strcmp(argv[1],"-t")) {
569 syslog(LOG_NOTICE,"test mode - will not deliver");
573 strcpy(recp,argv[1]);
576 strcpy(recp,argv[2]);
579 /*** Non-SMTP delivery mode ***/
580 syslog(LOG_NOTICE,"recp: %s",recp);
581 for (a=0; a<2; ++a) {
585 /* did we alias it back to a remote address? */
586 if ( (haschar(recp,'%'))
587 || (haschar(recp,'@'))
588 || (haschar(recp,'!')) ) {
590 process_rfc822_addr(recp, user, node, name);
593 /* If there are dots, it's an Internet host, so feed it
594 * back to an external mail transport agent such as sendmail.
596 if (haschar(node, '.')) {
597 snprintf(buf, sizeof buf, SENDMAIL, recp);
602 /* Otherwise, we're dealing with Citadel mail. */
604 snprintf(recp, sizeof recp, "%s!%s", node, user);
605 deliver_to_ignet = 1;
610 deliver(recp, is_test, deliver_to_ignet);
612 if (RUN_NETPROC) execlp("./netproc", "netproc", "-i", NULL);