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 * pipe message through netproc
355 void do_citmail(char recp[], int dtype)
369 long message_id = 0L;
370 char targetroom[256];
371 char content_type[256];
372 char *extra_headers = NULL;
375 if (dtype == DELIVER_REMOTE) {
377 /* get the Citadel node name out of the path */
378 strncpy(destsys, recp, sizeof(destsys));
379 for (a = 0; a < strlen(destsys); ++a) {
380 if ((destsys[a] == '!') || (destsys[a] == '.')) {
385 /* chop the system name out, so we're left with a user */
386 while (haschar(recp, '!'))
387 strcpy(recp, &recp[1]);
389 /* Convert underscores to spaces */
390 for (a = 0; a < strlen(recp); ++a)
394 /* Are we delivering to a room instead of a user? */
395 if (!strncasecmp(recp, "room ", 5)) {
396 strcpy(targetroom, &recp[5]);
399 strcpy(targetroom, MAILROOM);
403 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
405 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
406 temp = fopen(buf, "w");
409 strcpy(nodebuf, config.c_nodename);
410 strcpy(content_type, "text/plain");
413 if (fgets(buf, 128, stdin) == NULL)
415 strip_trailing_whitespace(buf);
417 if (!strncasecmp(buf, "Subject: ", 9))
418 strcpy(subject, &buf[9]);
419 else if (!strncasecmp(buf, "Date: ", 6))
420 now = conv_date(&buf[6]);
421 else if (!strncasecmp(buf, "From: ", 6))
422 strcpy(from, &buf[6]);
423 else if (!strncasecmp(buf, "Content-type: ", 14))
424 strcpy(content_type, &buf[14]);
425 else if (!strncasecmp(buf, "From ", 5)) { /* ignore */
427 if (extra_headers == NULL) {
428 extra_headers = malloc(strlen(buf) + 2);
429 strcpy(extra_headers, "");
431 extra_headers = realloc(extra_headers,
432 (strlen(extra_headers) + strlen(buf) + 2));
434 strcat(extra_headers, buf);
435 strcat(extra_headers, "\n");
437 } while ((strcmp(buf, ".")) && (strcmp(buf, "")));
439 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
441 if (!strncasecmp(content_type, "text/plain", 10))
442 format_type = 1; /* plain ASCII message */
444 format_type = 4; /* MIME message */
446 /* now convert it to Citadel format */
449 putc(255, temp); /* 0xFF = start-of-message byte */
450 putc(MES_NORMAL, temp); /* Non-anonymous message */
451 putc(format_type, temp); /* Format type */
454 fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0);
456 fprintf(temp, "I%ld%c", message_id, 0);
457 fprintf(temp, "T%ld%c", (long)now, 0);
458 fprintf(temp, "A%s%c", userbuf, 0);
461 if (strlen(targetroom) > 0) {
462 fprintf(temp, "O%s%c", targetroom, 0);
464 fprintf(temp, "O%s%c", MAILROOM, 0);
467 fprintf(temp, "N%s%c", nodebuf, 0);
468 fprintf(temp, "H%s%c", frombuf, 0);
469 if (dtype == DELIVER_REMOTE) {
470 fprintf(temp, "D%s%c", destsys, 0);
472 if (strlen(recp) > 0) {
473 fprintf(temp, "R%s%c", recp, 0);
475 /* Subject and text */
476 if (strlen(subject) > 0) {
477 fprintf(temp, "U%s%c", subject, 0);
480 if (format_type == 4) {
481 fprintf(temp, "Content-type: %s\n", content_type);
482 if (extra_headers != NULL)
483 fprintf(temp, "%s", extra_headers);
486 if (extra_headers != NULL)
488 if (strcmp(buf, "."))
489 loopcopy(temp, stdin);
495 void do_uudecode(char *target)
497 static char buf[1024];
500 snprintf(buf, sizeof buf, "cd %s; uudecode", target);
502 fp = popen(buf, "w");
505 while (fgets(buf, 1024, stdin) != NULL) {
506 fprintf(fp, "%s", buf);
512 int alias(char *name)
518 fp = fopen(ALIASES, "r");
520 syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno));
523 while (fgets(abuf, 256, fp) != NULL) {
524 strip_trailing_whitespace(abuf);
525 for (a = 0; a < strlen(abuf); ++a) {
526 if (abuf[a] == ',') {
528 if (!strcasecmp(name, abuf)) {
529 strcpy(name, &abuf[a + 1]);
539 void deliver(char recp[], int is_test, int deliver_to_ignet)
542 /* various ways we can deliver mail... */
544 if (deliver_to_ignet) {
545 syslog(LOG_NOTICE, "to Citadel network user %s", recp);
547 do_citmail(recp, DELIVER_REMOTE);
548 } else if (!strcmp(recp, "uudecode")) {
549 syslog(LOG_NOTICE, "uudecoding to bit bucket directory");
551 do_uudecode(config.c_bucket_dir);
552 } else if (!strcmp(recp, "cit86net")) {
553 syslog(LOG_NOTICE, "uudecoding to Cit86net spool");
555 do_uudecode(CIT86NET);
556 system("exec ./BatchTranslate86");
558 } else if (!strcmp(recp, "null")) {
559 syslog(LOG_NOTICE, "zapping nulled message");
561 /* Otherwise, the user is local (or an unknown name was
562 * specified, in which case we let netproc handle the bounce)
564 syslog(LOG_NOTICE, "to Citadel recipient %s", recp);
566 do_citmail(recp, DELIVER_LOCAL);
573 int main(int argc, char **argv)
576 int deliver_to_ignet = 0;
577 static char recp[1024], buf[1024];
578 static char user[1024], node[1024], name[1024];
581 openlog("citmail", LOG_PID, LOG_USER);
583 LoadInternetConfig();
585 if (!strcmp(argv[1], "-t")) {
587 syslog(LOG_NOTICE, "test mode - will not deliver");
590 strcpy(recp, argv[1]);
592 strcpy(recp, argv[2]);
595 /*** Non-SMTP delivery mode ***/
596 syslog(LOG_NOTICE, "recp: %s", recp);
597 for (a = 0; a < 2; ++a) {
601 /* did we alias it back to a remote address? */
602 if ((haschar(recp, '%'))
603 || (haschar(recp, '@'))
604 || (haschar(recp, '!'))) {
606 process_rfc822_addr(recp, user, node, name);
609 /* If there are dots, it's an Internet host, so feed it
610 * back to an external mail transport agent such as sendmail.
612 if (haschar(node, '.')) {
613 snprintf(buf, sizeof buf, SENDMAIL, recp);
617 /* Otherwise, we're dealing with Citadel mail. */
619 snprintf(recp, sizeof recp, "%s!%s", node, user);
620 deliver_to_ignet = 1;
624 deliver(recp, is_test, deliver_to_ignet);
627 syslog(LOG_NOTICE, "running netproc");
628 if (system("/bin/true") != 0) {
629 syslog(LOG_ERR, "netproc failed: %s", strerror(errno));
632 syslog(LOG_NOTICE, "skipping netproc");