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);
352 * pipe message through netproc
354 void do_citmail(char recp[], int dtype)
368 char targetroom[256];
369 char content_type[256];
370 char *extra_headers = NULL;
373 if (dtype == DELIVER_REMOTE) {
375 /* get the Citadel node name out of the path */
376 strncpy(destsys, recp, sizeof(destsys));
377 for (a = 0; a < strlen(destsys); ++a) {
378 if ((destsys[a] == '!') || (destsys[a] == '.')) {
383 /* chop the system name out, so we're left with a user */
384 while (haschar(recp, '!'))
385 strcpy(recp, &recp[1]);
387 /* Convert underscores to spaces */
388 for (a = 0; a < strlen(recp); ++a)
392 /* Are we delivering to a room instead of a user? */
393 if (!strncasecmp(recp, "room ", 5)) {
394 strcpy(targetroom, &recp[5]);
397 strcpy(targetroom, MAILROOM);
401 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
403 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
404 temp = fopen(buf, "w");
407 strcpy(nodebuf, config.c_nodename);
408 strcpy(content_type, "text/plain");
411 if (fgets(buf, 128, stdin) == NULL)
413 strip_trailing_whitespace(buf);
415 if (!strncasecmp(buf, "Subject: ", 9))
416 strcpy(subject, &buf[9]);
417 else if (!strncasecmp(buf, "Date: ", 6))
418 now = conv_date(&buf[6]);
419 else if (!strncasecmp(buf, "From: ", 6))
420 strcpy(from, &buf[6]);
421 else if (!strncasecmp(buf, "Content-type: ", 14))
422 strcpy(content_type, &buf[14]);
423 else if (!strncasecmp(buf, "From ", 5)) { /* ignore */
425 if (extra_headers == NULL) {
426 extra_headers = malloc(strlen(buf) + 2);
427 strcpy(extra_headers, "");
429 extra_headers = realloc(extra_headers,
430 (strlen(extra_headers) + strlen(buf) + 2));
432 strcat(extra_headers, buf);
433 strcat(extra_headers, "\n");
435 } while ((strcmp(buf, ".")) && (strcmp(buf, "")));
437 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
439 if (!strncasecmp(content_type, "text/plain", 10))
440 format_type = 1; /* plain ASCII message */
442 format_type = 4; /* MIME message */
444 /* now convert it to Citadel format */
447 putc(255, temp); /* 0xFF = start-of-message byte */
448 putc(MES_NORMAL, temp); /* Non-anonymous message */
449 putc(format_type, temp); /* Format type */
452 fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0);
453 fprintf(temp, "T%ld%c", (long)now, 0);
454 fprintf(temp, "A%s%c", userbuf, 0);
457 if (strlen(targetroom) > 0) {
458 fprintf(temp, "O%s%c", targetroom, 0);
460 fprintf(temp, "O%s%c", MAILROOM, 0);
463 fprintf(temp, "N%s%c", nodebuf, 0);
464 fprintf(temp, "H%s%c", frombuf, 0);
465 if (dtype == DELIVER_REMOTE) {
466 fprintf(temp, "D%s%c", destsys, 0);
468 if (strlen(recp) > 0) {
469 fprintf(temp, "R%s%c", recp, 0);
471 /* Subject and text */
472 if (strlen(subject) > 0) {
473 fprintf(temp, "U%s%c", subject, 0);
476 if (format_type == 4) {
477 fprintf(temp, "Content-type: %s\n", content_type);
478 if (extra_headers != NULL)
479 fprintf(temp, "%s", extra_headers);
482 if (extra_headers != NULL)
484 if (strcmp(buf, "."))
485 loopcopy(temp, stdin);
491 void do_uudecode(char *target)
493 static char buf[1024];
496 snprintf(buf, sizeof buf, "cd %s; uudecode", target);
498 fp = popen(buf, "w");
501 while (fgets(buf, 1024, stdin) != NULL) {
502 fprintf(fp, "%s", buf);
508 int alias(char *name)
514 fp = fopen(ALIASES, "r");
516 syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno));
519 while (fgets(abuf, 256, fp) != NULL) {
520 strip_trailing_whitespace(abuf);
521 for (a = 0; a < strlen(abuf); ++a) {
522 if (abuf[a] == ',') {
524 if (!strcasecmp(name, abuf)) {
525 strcpy(name, &abuf[a + 1]);
535 void deliver(char recp[], int is_test, int deliver_to_ignet)
538 /* various ways we can deliver mail... */
540 if (deliver_to_ignet) {
541 syslog(LOG_NOTICE, "to Citadel network user %s", recp);
543 do_citmail(recp, DELIVER_REMOTE);
544 } else if (!strcmp(recp, "uudecode")) {
545 syslog(LOG_NOTICE, "uudecoding to bit bucket directory");
547 do_uudecode(config.c_bucket_dir);
548 } else if (!strcmp(recp, "cit86net")) {
549 syslog(LOG_NOTICE, "uudecoding to Cit86net spool");
551 do_uudecode(CIT86NET);
552 system("exec ./BatchTranslate86");
554 } else if (!strcmp(recp, "null")) {
555 syslog(LOG_NOTICE, "zapping nulled message");
557 /* Otherwise, the user is local (or an unknown name was
558 * specified, in which case we let netproc handle the bounce)
560 syslog(LOG_NOTICE, "to Citadel recipient %s", recp);
562 do_citmail(recp, DELIVER_LOCAL);
569 int main(int argc, char **argv)
572 int deliver_to_ignet = 0;
573 static char recp[1024], buf[1024];
574 static char user[1024], node[1024], name[1024];
577 openlog("citmail", LOG_PID, LOG_USER);
579 LoadInternetConfig();
581 if (!strcmp(argv[1], "-t")) {
583 syslog(LOG_NOTICE, "test mode - will not deliver");
586 strcpy(recp, argv[1]);
588 strcpy(recp, argv[2]);
591 /*** Non-SMTP delivery mode ***/
592 syslog(LOG_NOTICE, "recp: %s", recp);
593 for (a = 0; a < 2; ++a) {
597 /* did we alias it back to a remote address? */
598 if ((haschar(recp, '%'))
599 || (haschar(recp, '@'))
600 || (haschar(recp, '!'))) {
602 process_rfc822_addr(recp, user, node, name);
605 /* If there are dots, it's an Internet host, so feed it
606 * back to an external mail transport agent such as sendmail.
608 if (haschar(node, '.')) {
609 snprintf(buf, sizeof buf, SENDMAIL, recp);
613 /* Otherwise, we're dealing with Citadel mail. */
615 snprintf(recp, sizeof recp, "%s!%s", node, user);
616 deliver_to_ignet = 1;
620 deliver(recp, is_test, deliver_to_ignet);
623 syslog(LOG_NOTICE, "running netproc");
624 if (system("/bin/true") != 0) {
625 syslog(LOG_ERR, "netproc failed: %s", strerror(errno));
628 syslog(LOG_NOTICE, "skipping netproc");