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"
34 /* message delivery classes */
44 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
47 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
48 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
56 char OUTGOING_FQDN[128];
60 long conv_date(char *sdbuf)
62 int a, b, cpos, tend, tval;
69 tmbuf = (struct tm *) localtime(&now);
71 /* get rid of + or - timezone mods */
72 for (a = 0; a < strlen(dbuf); ++a)
73 if ((dbuf[a] == '+') || (dbuf[a] == '-'))
75 strcpy(&dbuf[a], &dbuf[a + 1]);
76 } while ((dbuf[a] != 32) && (dbuf[a] != 0));
78 /* try and extract the time by looking for colons */
80 for (a = strlen(dbuf); a >= 0; --a)
81 if ((dbuf[a] == ':') && (atoi(&dbuf[a - 1]) != 0))
86 for (a = tend; a >= cpos; --a)
90 tmbuf->tm_hour = atoi(&dbuf[cpos]);
91 tmbuf->tm_min = atoi(&dbuf[cpos + 3]);
92 tmbuf->tm_sec = atoi(&dbuf[cpos + 6]);
95 strcpy(&dbuf[cpos], &dbuf[cpos + 1]);
96 } while ((dbuf[cpos] != 32) && (dbuf[cpos] != 0));
98 /* next try to extract a month */
101 for (a = 0; a < strlen(dbuf); ++a)
102 for (b = 0; b < 12; ++b)
103 if (!strncmp(&dbuf[a], monthdesc[b], 3)) {
108 tmbuf->tm_mon = tval;
109 strcpy(&dbuf[cpos], &dbuf[cpos + 3]);
113 for (a = 0; a < strlen(dbuf); ++a)
114 if ((atoi(&dbuf[a]) >= 1900) && (dbuf[a] != 32)) {
115 tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
116 strcpy(&dbuf[a], &dbuf[a + 4]);
118 /* whatever's left is the mday (hopefully) */
120 for (a = 0; a < strlen(dbuf); ++a)
121 if ((dbuf[a] != 32) && (atoi(&dbuf[a]) >= 1) && (atoi(&dbuf[a]) <= 31)
122 && ((a == 0) || (dbuf[a - 1] == ' '))) {
123 tmbuf->tm_mday = atoi(&dbuf[a]);
124 strcpy(&dbuf[a], &dbuf[a + 2]);
126 return ((long) mktime(tmbuf));
130 #ifndef HAVE_STRERROR
132 * replacement strerror() for systems that don't have it
134 char *strerror(int e)
138 snprintf(buf, sizeof buf, "errno = %d", e);
143 int haschar(char *st, int ch)
147 for (a = 0; a < strlen(st); ++a)
153 void strip_trailing_whitespace(char *buf)
155 while (isspace(buf[strlen(buf) - 1]))
156 buf[strlen(buf) - 1] = 0;
159 /* strip leading and trailing spaces */
160 void striplt(char *buf)
162 while ((strlen(buf) > 0) && (buf[0] == 32))
163 strcpy(buf, &buf[1]);
164 while (buf[strlen(buf) - 1] == 32)
165 buf[strlen(buf) - 1] = 0;
170 * Check to see if a given FQDN really maps to a Citadel network node
172 void host_alias(char host[])
177 /* What name is the local host known by? */
178 /* if (!strcasecmp(host, config.c_fqdn)) { */
179 if (IsHostLocal(host)) {
180 strcpy(host, config.c_nodename);
183 /* Other hosts in the gateway domain? */
184 for (a = 0; a < strlen(host); ++a) {
185 if ((host[a] == '.') && (!strcasecmp(&host[a + 1], GW_DOMAIN))) {
187 for (a = 0; a < strlen(host); ++a) {
195 /* Otherwise, do nothing... */
201 * Split an RFC822-style address into userid, host, and full name
203 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
207 /* extract full name - first, it's From minus <userid> */
208 strcpy(name, rfc822);
209 for (a = 0; a < strlen(name); ++a)
210 if (name[a] == '<') {
212 strcpy(&name[a], &name[a + 1]);
213 } while ((strlen(name) > 0) && (name[a] != '>'));
214 strcpy(&name[a], &name[a + 1]);
216 /* strip anything to the left of a bang */
217 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
218 strcpy(name, &name[1]);
220 /* and anything to the right of a @ or % */
221 for (a = 0; a < strlen(name); ++a) {
228 /* but if there are parentheses, that changes the rules... */
229 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
230 strcpy(name, rfc822);
231 while ((strlen(name) > 0) && (name[0] != '(')) {
232 strcpy(&name[0], &name[1]);
234 strcpy(&name[0], &name[1]);
235 for (a = 0; a < strlen(name); ++a)
239 /* but if there are a set of quotes, that supersedes everything */
240 if (haschar(rfc822, 34) == 2) {
241 strcpy(name, rfc822);
242 while ((strlen(name) > 0) && (name[0] != 34)) {
243 strcpy(&name[0], &name[1]);
245 strcpy(&name[0], &name[1]);
246 for (a = 0; a < strlen(name); ++a)
250 /* extract user id */
251 strcpy(user, rfc822);
253 /* first get rid of anything in parens */
254 for (a = 0; a < strlen(user); ++a)
255 if (user[a] == '(') {
257 strcpy(&user[a], &user[a + 1]);
258 } while ((strlen(user) > 0) && (user[a] != ')'));
259 strcpy(&user[a], &user[a + 1]);
261 /* if there's a set of angle brackets, strip it down to that */
262 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
263 while ((strlen(user) > 0) && (user[0] != '<')) {
264 strcpy(&user[0], &user[1]);
266 strcpy(&user[0], &user[1]);
267 for (a = 0; a < strlen(user); ++a)
271 /* strip anything to the left of a bang */
272 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
273 strcpy(user, &user[1]);
275 /* and anything to the right of a @ or % */
276 for (a = 0; a < strlen(user); ++a) {
285 /* extract node name */
286 strcpy(node, rfc822);
288 /* first get rid of anything in parens */
289 for (a = 0; a < strlen(node); ++a)
290 if (node[a] == '(') {
292 strcpy(&node[a], &node[a + 1]);
293 } while ((strlen(node) > 0) && (node[a] != ')'));
294 strcpy(&node[a], &node[a + 1]);
296 /* if there's a set of angle brackets, strip it down to that */
297 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
298 while ((strlen(node) > 0) && (node[0] != '<')) {
299 strcpy(&node[0], &node[1]);
301 strcpy(&node[0], &node[1]);
302 for (a = 0; a < strlen(node); ++a)
306 /* strip anything to the left of a @ */
307 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
308 strcpy(node, &node[1]);
310 /* strip anything to the left of a % */
311 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
312 strcpy(node, &node[1]);
314 /* reduce multiple system bang paths to node!user */
315 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
316 strcpy(node, &node[1]);
318 /* now get rid of the user portion of a node!user string */
319 for (a = 0; a < strlen(node); ++a)
325 /* strip leading and trailing spaces in all strings */
332 * Copy line by line, ending at EOF or a "."
334 void loopcopy(FILE * to, FILE * from)
340 r = fgets(buf, sizeof(buf), from);
343 strip_trailing_whitespace(buf);
344 if (!strcmp(buf, "."))
346 fprintf(to, "%s\n", buf);
353 * Try to extract a numeric message ID
355 long extract_msg_id(char *id_string) {
360 strncpy(buf, id_string, sizeof buf);
363 for (i=0; i<strlen(buf); ++i) {
365 strcpy(buf, &buf[i]);
366 for (j=0; j<strlen(buf); ++j)
367 if (buf[j]=='>') buf[j]=0;
372 if (msgid) return(msgid);
374 for (i=0; i<strlen(buf); ++i) {
375 if (!isdigit(buf[i])) {
376 strcpy(&buf[i], &buf[i+1]);
387 * pipe message through netproc
389 void do_citmail(char recp[], int dtype)
403 long message_id = 0L;
404 char targetroom[256];
405 char content_type[256];
406 char *extra_headers = NULL;
409 if (dtype == DELIVER_REMOTE) {
411 /* get the Citadel node name out of the path */
412 strncpy(destsys, recp, sizeof(destsys));
413 for (a = 0; a < strlen(destsys); ++a) {
414 if ((destsys[a] == '!') || (destsys[a] == '.')) {
419 /* chop the system name out, so we're left with a user */
420 while (haschar(recp, '!'))
421 strcpy(recp, &recp[1]);
423 /* Convert underscores to spaces */
424 for (a = 0; a < strlen(recp); ++a)
428 /* Are we delivering to a room instead of a user? */
429 if (!strncasecmp(recp, "room ", 5)) {
430 strcpy(targetroom, &recp[5]);
433 strcpy(targetroom, MAILROOM);
437 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
439 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
440 temp = fopen(buf, "w");
443 strcpy(nodebuf, config.c_nodename);
444 strcpy(content_type, "text/plain");
447 if (fgets(buf, 128, stdin) == NULL)
449 strip_trailing_whitespace(buf);
451 if (!strncasecmp(buf, "Subject: ", 9))
452 strcpy(subject, &buf[9]);
453 else if (!strncasecmp(buf, "Date: ", 6))
454 now = conv_date(&buf[6]);
455 else if (!strncasecmp(buf, "From: ", 6))
456 strcpy(from, &buf[6]);
457 else if (!strncasecmp(buf, "Message-ID: ", 12))
458 message_id = extract_msg_id(&buf[12]);
459 else if (!strncasecmp(buf, "Content-type: ", 14))
460 strcpy(content_type, &buf[14]);
461 else if (!strncasecmp(buf, "From ", 5)) { /* ignore */
463 if (extra_headers == NULL) {
464 extra_headers = malloc(strlen(buf) + 2);
465 strcpy(extra_headers, "");
467 extra_headers = realloc(extra_headers,
468 (strlen(extra_headers) + strlen(buf) + 2));
470 strcat(extra_headers, buf);
471 strcat(extra_headers, "\n");
473 } while ((strcmp(buf, ".")) && (strcmp(buf, "")));
475 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
477 if (!strncasecmp(content_type, "text/plain", 10))
478 format_type = 1; /* plain ASCII message */
480 format_type = 4; /* MIME message */
482 /* now convert it to Citadel format */
485 putc(255, temp); /* 0xFF = start-of-message byte */
486 putc(MES_NORMAL, temp); /* Non-anonymous message */
487 putc(format_type, temp); /* Format type */
490 fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0);
492 fprintf(temp, "I%ld%c", message_id, 0);
493 fprintf(temp, "T%ld%c", (long)now, 0);
494 fprintf(temp, "A%s%c", userbuf, 0);
497 if (strlen(targetroom) > 0) {
498 fprintf(temp, "O%s%c", targetroom, 0);
500 fprintf(temp, "O%s%c", MAILROOM, 0);
503 fprintf(temp, "N%s%c", nodebuf, 0);
504 fprintf(temp, "H%s%c", frombuf, 0);
505 if (dtype == DELIVER_REMOTE) {
506 fprintf(temp, "D%s%c", destsys, 0);
508 if (strlen(recp) > 0) {
509 fprintf(temp, "R%s%c", recp, 0);
511 /* Subject and text */
512 if (strlen(subject) > 0) {
513 fprintf(temp, "U%s%c", subject, 0);
516 if (format_type == 4) {
517 fprintf(temp, "Content-type: %s\n", content_type);
518 if (extra_headers != NULL)
519 fprintf(temp, "%s", extra_headers);
522 if (extra_headers != NULL)
524 if (strcmp(buf, "."))
525 loopcopy(temp, stdin);
531 void do_uudecode(char *target)
533 static char buf[1024];
536 snprintf(buf, sizeof buf, "cd %s; uudecode", target);
538 fp = popen(buf, "w");
541 while (fgets(buf, 1024, stdin) != NULL) {
542 fprintf(fp, "%s", buf);
548 int alias(char *name)
554 fp = fopen(ALIASES, "r");
556 syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno));
559 while (fgets(abuf, 256, fp) != NULL) {
560 strip_trailing_whitespace(abuf);
561 for (a = 0; a < strlen(abuf); ++a) {
562 if (abuf[a] == ',') {
564 if (!strcasecmp(name, abuf)) {
565 strcpy(name, &abuf[a + 1]);
575 void deliver(char recp[], int is_test, int deliver_to_ignet)
578 /* various ways we can deliver mail... */
580 if (deliver_to_ignet) {
581 syslog(LOG_NOTICE, "to Citadel network user %s", recp);
583 do_citmail(recp, DELIVER_REMOTE);
584 } else if (!strcmp(recp, "uudecode")) {
585 syslog(LOG_NOTICE, "uudecoding to bit bucket directory");
587 do_uudecode(config.c_bucket_dir);
588 } else if (!strcmp(recp, "cit86net")) {
589 syslog(LOG_NOTICE, "uudecoding to Cit86net spool");
591 do_uudecode(CIT86NET);
592 system("exec ./BatchTranslate86");
594 } else if (!strcmp(recp, "null")) {
595 syslog(LOG_NOTICE, "zapping nulled message");
597 /* Otherwise, the user is local (or an unknown name was
598 * specified, in which case we let netproc handle the bounce)
600 syslog(LOG_NOTICE, "to Citadel recipient %s", recp);
602 do_citmail(recp, DELIVER_LOCAL);
609 int main(int argc, char **argv)
612 int deliver_to_ignet = 0;
613 static char recp[1024], buf[1024];
614 static char user[1024], node[1024], name[1024];
617 openlog("citmail", LOG_PID, LOG_USER);
619 LoadInternetConfig();
621 if (!strcmp(argv[1], "-t")) {
623 syslog(LOG_NOTICE, "test mode - will not deliver");
626 strcpy(recp, argv[1]);
628 strcpy(recp, argv[2]);
631 /*** Non-SMTP delivery mode ***/
632 syslog(LOG_NOTICE, "recp: %s", recp);
633 for (a = 0; a < 2; ++a) {
637 /* did we alias it back to a remote address? */
638 if ((haschar(recp, '%'))
639 || (haschar(recp, '@'))
640 || (haschar(recp, '!'))) {
642 process_rfc822_addr(recp, user, node, name);
645 /* If there are dots, it's an Internet host, so feed it
646 * back to an external mail transport agent such as sendmail.
648 if (haschar(node, '.')) {
649 snprintf(buf, sizeof buf, SENDMAIL, recp);
653 /* Otherwise, we're dealing with Citadel mail. */
655 snprintf(recp, sizeof recp, "%s!%s", node, user);
656 deliver_to_ignet = 1;
660 deliver(recp, is_test, deliver_to_ignet);
663 syslog(LOG_NOTICE, "running netproc");
664 if (system("/bin/true") != 0) {
665 syslog(LOG_ERR, "netproc failed: %s", strerror(errno));
668 syslog(LOG_NOTICE, "skipping netproc");