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
43 {"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))
82 for (a = tend; a >= cpos; --a)
86 tmbuf->tm_hour = atoi(&dbuf[cpos]);
87 tmbuf->tm_min = atoi(&dbuf[cpos + 3]);
88 tmbuf->tm_sec = atoi(&dbuf[cpos + 6]);
91 strcpy(&dbuf[cpos], &dbuf[cpos + 1]);
92 } while ((dbuf[cpos] != 32) && (dbuf[cpos] != 0));
94 /* next try to extract a month */
97 for (a = 0; a < strlen(dbuf); ++a)
98 for (b = 0; b < 12; ++b)
99 if (!strncmp(&dbuf[a], monthdesc[b], 3)) {
104 tmbuf->tm_mon = tval;
105 strcpy(&dbuf[cpos], &dbuf[cpos + 3]);
109 for (a = 0; a < strlen(dbuf); ++a)
110 if ((atoi(&dbuf[a]) >= 1900) && (dbuf[a] != 32)) {
111 tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
112 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]);
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)
149 void strip_trailing_whitespace(char *buf)
151 while (isspace(buf[strlen(buf) - 1]))
152 buf[strlen(buf) - 1] = 0;
155 /* strip leading and trailing spaces */
156 void striplt(char *buf)
158 while ((strlen(buf) > 0) && (buf[0] == 32))
159 strcpy(buf, &buf[1]);
160 while (buf[strlen(buf) - 1] == 32)
161 buf[strlen(buf) - 1] = 0;
166 * Check to see if a given FQDN really maps to a Citadel network node
168 void host_alias(char host[])
173 /* What name is the local host known by? */
174 /* if (!strcasecmp(host, config.c_fqdn)) { */
175 if (IsHostLocal(host)) {
176 strcpy(host, config.c_nodename);
179 /* Other hosts in the gateway domain? */
180 for (a = 0; a < strlen(host); ++a) {
181 if ((host[a] == '.') && (!strcasecmp(&host[a + 1], GW_DOMAIN))) {
183 for (a = 0; a < strlen(host); ++a) {
191 /* Otherwise, do nothing... */
197 * Split an RFC822-style address into userid, host, and full name
199 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
203 /* extract full name - first, it's From minus <userid> */
204 strcpy(name, rfc822);
205 for (a = 0; a < strlen(name); ++a)
206 if (name[a] == '<') {
208 strcpy(&name[a], &name[a + 1]);
209 } while ((strlen(name) > 0) && (name[a] != '>'));
210 strcpy(&name[a], &name[a + 1]);
212 /* strip anything to the left of a bang */
213 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
214 strcpy(name, &name[1]);
216 /* and anything to the right of a @ or % */
217 for (a = 0; a < strlen(name); ++a) {
224 /* but if there are parentheses, that changes the rules... */
225 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
226 strcpy(name, rfc822);
227 while ((strlen(name) > 0) && (name[0] != '(')) {
228 strcpy(&name[0], &name[1]);
230 strcpy(&name[0], &name[1]);
231 for (a = 0; a < strlen(name); ++a)
235 /* but if there are a set of quotes, that supersedes everything */
236 if (haschar(rfc822, 34) == 2) {
237 strcpy(name, rfc822);
238 while ((strlen(name) > 0) && (name[0] != 34)) {
239 strcpy(&name[0], &name[1]);
241 strcpy(&name[0], &name[1]);
242 for (a = 0; a < strlen(name); ++a)
246 /* extract user id */
247 strcpy(user, rfc822);
249 /* first get rid of anything in parens */
250 for (a = 0; a < strlen(user); ++a)
251 if (user[a] == '(') {
253 strcpy(&user[a], &user[a + 1]);
254 } while ((strlen(user) > 0) && (user[a] != ')'));
255 strcpy(&user[a], &user[a + 1]);
257 /* if there's a set of angle brackets, strip it down to that */
258 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
259 while ((strlen(user) > 0) && (user[0] != '<')) {
260 strcpy(&user[0], &user[1]);
262 strcpy(&user[0], &user[1]);
263 for (a = 0; a < strlen(user); ++a)
267 /* strip anything to the left of a bang */
268 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
269 strcpy(user, &user[1]);
271 /* and anything to the right of a @ or % */
272 for (a = 0; a < strlen(user); ++a) {
281 /* extract node name */
282 strcpy(node, rfc822);
284 /* first get rid of anything in parens */
285 for (a = 0; a < strlen(node); ++a)
286 if (node[a] == '(') {
288 strcpy(&node[a], &node[a + 1]);
289 } while ((strlen(node) > 0) && (node[a] != ')'));
290 strcpy(&node[a], &node[a + 1]);
292 /* if there's a set of angle brackets, strip it down to that */
293 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
294 while ((strlen(node) > 0) && (node[0] != '<')) {
295 strcpy(&node[0], &node[1]);
297 strcpy(&node[0], &node[1]);
298 for (a = 0; a < strlen(node); ++a)
302 /* strip anything to the left of a @ */
303 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
304 strcpy(node, &node[1]);
306 /* strip anything to the left of a % */
307 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
308 strcpy(node, &node[1]);
310 /* reduce multiple system bang paths to node!user */
311 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
312 strcpy(node, &node[1]);
314 /* now get rid of the user portion of a node!user string */
315 for (a = 0; a < strlen(node); ++a)
321 /* strip leading and trailing spaces in all strings */
328 * Copy line by line, ending at EOF or a "."
330 void loopcopy(FILE * to, FILE * from)
336 r = fgets(buf, sizeof(buf), from);
339 strip_trailing_whitespace(buf);
340 if (!strcmp(buf, "."))
342 fprintf(to, "%s\n", buf);
348 * pipe message through netproc
350 void do_citmail(char recp[], int dtype)
364 char targetroom[256];
365 char content_type[256];
366 char *extra_headers = NULL;
369 if (dtype == REMOTE) {
371 /* get the Citadel node name out of the path */
372 strncpy(destsys, recp, sizeof(destsys));
373 for (a = 0; a < strlen(destsys); ++a) {
374 if ((destsys[a] == '!') || (destsys[a] == '.')) {
379 /* chop the system name out, so we're left with a user */
380 while (haschar(recp, '!'))
381 strcpy(recp, &recp[1]);
383 /* Convert underscores to spaces */
384 for (a = 0; a < strlen(recp); ++a)
388 /* Are we delivering to a room instead of a user? */
389 if (!strncasecmp(recp, "room ", 5)) {
390 strcpy(targetroom, &recp[5]);
393 strcpy(targetroom, MAILROOM);
397 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
399 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
400 temp = fopen(buf, "w");
403 strcpy(nodebuf, config.c_nodename);
404 strcpy(content_type, "text/plain");
407 if (fgets(buf, 128, stdin) == NULL)
409 strip_trailing_whitespace(buf);
411 if (!strncasecmp(buf, "Subject: ", 9))
412 strcpy(subject, &buf[9]);
413 else if (!strncasecmp(buf, "Date: ", 6))
414 now = conv_date(&buf[6]);
415 else if (!strncasecmp(buf, "From: ", 6))
416 strcpy(from, &buf[6]);
417 else if (!strncasecmp(buf, "Content-type: ", 14))
418 strcpy(content_type, &buf[14]);
419 else if (!strncasecmp(buf, "From ", 5)) { /* ignore */
421 if (extra_headers == NULL) {
422 extra_headers = malloc(strlen(buf) + 2);
423 strcpy(extra_headers, "");
425 extra_headers = realloc(extra_headers,
426 (strlen(extra_headers) + strlen(buf) + 2));
428 strcat(extra_headers, buf);
429 strcat(extra_headers, "\n");
431 } while ((strcmp(buf, ".")) && (strcmp(buf, "")));
433 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
435 if (!strncasecmp(content_type, "text/plain", 10))
436 format_type = 1; /* plain ASCII message */
438 format_type = 4; /* MIME message */
440 /* now convert it to Citadel format */
443 putc(255, temp); /* 0xFF = start-of-message byte */
444 putc(MES_NORMAL, temp); /* Non-anonymous message */
445 putc(format_type, temp); /* Format type */
448 fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0);
449 fprintf(temp, "T%ld%c", now, 0);
450 fprintf(temp, "A%s%c", userbuf, 0);
453 if (strlen(targetroom) > 0) {
454 fprintf(temp, "O%s%c", targetroom, 0);
456 fprintf(temp, "O%s%c", MAILROOM, 0);
459 fprintf(temp, "N%s%c", nodebuf, 0);
460 fprintf(temp, "H%s%c", frombuf, 0);
461 if (dtype == REMOTE) {
462 fprintf(temp, "D%s%c", destsys, 0);
464 if (strlen(recp) > 0) {
465 fprintf(temp, "R%s%c", recp, 0);
467 /* Subject and text */
468 if (strlen(subject) > 0) {
469 fprintf(temp, "U%s%c", subject, 0);
472 if (format_type == 4) {
473 fprintf(temp, "Content-type: %s\n", content_type);
474 if (extra_headers != NULL)
475 fprintf(temp, "%s", extra_headers);
478 if (extra_headers != NULL)
480 if (strcmp(buf, "."))
481 loopcopy(temp, stdin);
487 void do_uudecode(char *target)
489 static char buf[1024];
492 snprintf(buf, sizeof buf, "cd %s; uudecode", target);
494 fp = popen(buf, "w");
497 while (fgets(buf, 1024, stdin) != NULL) {
498 fprintf(fp, "%s", buf);
504 int alias(char *name)
510 fp = fopen(ALIASES, "r");
512 syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno));
515 while (fgets(abuf, 256, fp) != NULL) {
516 strip_trailing_whitespace(abuf);
517 for (a = 0; a < strlen(abuf); ++a) {
518 if (abuf[a] == ',') {
520 if (!strcasecmp(name, abuf)) {
521 strcpy(name, &abuf[a + 1]);
531 void deliver(char recp[], int is_test, int deliver_to_ignet)
534 /* various ways we can deliver mail... */
536 if (deliver_to_ignet) {
537 syslog(LOG_NOTICE, "to Citadel network user %s", recp);
539 do_citmail(recp, REMOTE);
540 } else if (!strcmp(recp, "uudecode")) {
541 syslog(LOG_NOTICE, "uudecoding to bit bucket directory");
543 do_uudecode(config.c_bucket_dir);
544 } else if (!strcmp(recp, "cit86net")) {
545 syslog(LOG_NOTICE, "uudecoding to Cit86net spool");
547 do_uudecode(CIT86NET);
548 system("exec ./BatchTranslate86");
550 } else if (!strcmp(recp, "null")) {
551 syslog(LOG_NOTICE, "zapping nulled message");
553 /* Otherwise, the user is local (or an unknown name was
554 * specified, in which case we let netproc handle the bounce)
556 syslog(LOG_NOTICE, "to Citadel recipient %s", recp);
558 do_citmail(recp, LOCAL);
565 int main(int argc, char **argv)
568 int deliver_to_ignet = 0;
569 static char recp[1024], buf[1024];
570 static char user[1024], node[1024], name[1024];
573 openlog("citmail", LOG_PID, LOG_USER);
575 LoadInternetConfig();
577 if (!strcmp(argv[1], "-t")) {
579 syslog(LOG_NOTICE, "test mode - will not deliver");
582 strcpy(recp, argv[1]);
584 strcpy(recp, argv[2]);
587 /*** Non-SMTP delivery mode ***/
588 syslog(LOG_NOTICE, "recp: %s", recp);
589 for (a = 0; a < 2; ++a) {
593 /* did we alias it back to a remote address? */
594 if ((haschar(recp, '%'))
595 || (haschar(recp, '@'))
596 || (haschar(recp, '!'))) {
598 process_rfc822_addr(recp, user, node, name);
601 /* If there are dots, it's an Internet host, so feed it
602 * back to an external mail transport agent such as sendmail.
604 if (haschar(node, '.')) {
605 snprintf(buf, sizeof buf, SENDMAIL, recp);
609 /* Otherwise, we're dealing with Citadel mail. */
611 snprintf(recp, sizeof recp, "%s!%s", node, user);
612 deliver_to_ignet = 1;
616 deliver(recp, is_test, deliver_to_ignet);
619 syslog(LOG_NOTICE, "running netproc");
620 if (system("/bin/true") != 0) {
621 syslog(LOG_ERR, "netproc failed: %s", strerror(errno));
624 syslog(LOG_NOTICE, "skipping netproc");