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"
35 extern time_t parsedate(char *p);
38 /* message delivery classes */
48 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
51 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
52 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
60 char OUTGOING_FQDN[128];
67 * replacement strerror() for systems that don't have it
73 snprintf(buf, sizeof buf, "errno = %d", e);
78 int haschar(char *st, int ch)
82 for (a = 0; a < strlen(st); ++a)
88 void strip_trailing_whitespace(char *buf)
90 while (isspace(buf[strlen(buf) - 1]))
91 buf[strlen(buf) - 1] = 0;
94 /* strip leading and trailing spaces */
95 void striplt(char *buf)
97 while ((strlen(buf) > 0) && (buf[0] == 32))
99 while (buf[strlen(buf) - 1] == 32)
100 buf[strlen(buf) - 1] = 0;
105 * Check to see if a given FQDN really maps to a Citadel network node
107 void host_alias(char host[])
112 /* What name is the local host known by? */
113 /* if (!strcasecmp(host, config.c_fqdn)) { */
114 if (IsHostLocal(host)) {
115 strcpy(host, config.c_nodename);
118 /* Other hosts in the gateway domain? */
119 for (a = 0; a < strlen(host); ++a) {
120 if ((host[a] == '.') && (!strcasecmp(&host[a + 1], GW_DOMAIN))) {
122 for (a = 0; a < strlen(host); ++a) {
130 /* Otherwise, do nothing... */
136 * Split an RFC822-style address into userid, host, and full name
138 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
142 /* extract full name - first, it's From minus <userid> */
143 strcpy(name, rfc822);
144 for (a = 0; a < strlen(name); ++a)
145 if (name[a] == '<') {
147 strcpy(&name[a], &name[a + 1]);
148 } while ((strlen(name) > 0) && (name[a] != '>'));
149 strcpy(&name[a], &name[a + 1]);
151 /* strip anything to the left of a bang */
152 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
153 strcpy(name, &name[1]);
155 /* and anything to the right of a @ or % */
156 for (a = 0; a < strlen(name); ++a) {
163 /* but if there are parentheses, that changes the rules... */
164 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
165 strcpy(name, rfc822);
166 while ((strlen(name) > 0) && (name[0] != '(')) {
167 strcpy(&name[0], &name[1]);
169 strcpy(&name[0], &name[1]);
170 for (a = 0; a < strlen(name); ++a)
174 /* but if there are a set of quotes, that supersedes everything */
175 if (haschar(rfc822, 34) == 2) {
176 strcpy(name, rfc822);
177 while ((strlen(name) > 0) && (name[0] != 34)) {
178 strcpy(&name[0], &name[1]);
180 strcpy(&name[0], &name[1]);
181 for (a = 0; a < strlen(name); ++a)
185 /* extract user id */
186 strcpy(user, rfc822);
188 /* first get rid of anything in parens */
189 for (a = 0; a < strlen(user); ++a)
190 if (user[a] == '(') {
192 strcpy(&user[a], &user[a + 1]);
193 } while ((strlen(user) > 0) && (user[a] != ')'));
194 strcpy(&user[a], &user[a + 1]);
196 /* if there's a set of angle brackets, strip it down to that */
197 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
198 while ((strlen(user) > 0) && (user[0] != '<')) {
199 strcpy(&user[0], &user[1]);
201 strcpy(&user[0], &user[1]);
202 for (a = 0; a < strlen(user); ++a)
206 /* strip anything to the left of a bang */
207 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
208 strcpy(user, &user[1]);
210 /* and anything to the right of a @ or % */
211 for (a = 0; a < strlen(user); ++a) {
220 /* extract node name */
221 strcpy(node, rfc822);
223 /* first get rid of anything in parens */
224 for (a = 0; a < strlen(node); ++a)
225 if (node[a] == '(') {
227 strcpy(&node[a], &node[a + 1]);
228 } while ((strlen(node) > 0) && (node[a] != ')'));
229 strcpy(&node[a], &node[a + 1]);
231 /* if there's a set of angle brackets, strip it down to that */
232 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
233 while ((strlen(node) > 0) && (node[0] != '<')) {
234 strcpy(&node[0], &node[1]);
236 strcpy(&node[0], &node[1]);
237 for (a = 0; a < strlen(node); ++a)
241 /* strip anything to the left of a @ */
242 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
243 strcpy(node, &node[1]);
245 /* strip anything to the left of a % */
246 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
247 strcpy(node, &node[1]);
249 /* reduce multiple system bang paths to node!user */
250 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
251 strcpy(node, &node[1]);
253 /* now get rid of the user portion of a node!user string */
254 for (a = 0; a < strlen(node); ++a)
260 /* strip leading and trailing spaces in all strings */
267 * Copy line by line, ending at EOF or a "."
269 void loopcopy(FILE * to, FILE * from)
275 r = fgets(buf, sizeof(buf), from);
278 strip_trailing_whitespace(buf);
279 if (!strcmp(buf, "."))
281 fprintf(to, "%s\n", buf);
288 * pipe message through netproc
290 void do_citmail(char recp[], int dtype)
304 long message_id = 0L;
305 char targetroom[256];
306 char content_type[256];
307 char *extra_headers = NULL;
310 if (dtype == DELIVER_REMOTE) {
312 /* get the Citadel node name out of the path */
313 strncpy(destsys, recp, sizeof(destsys));
314 for (a = 0; a < strlen(destsys); ++a) {
315 if ((destsys[a] == '!') || (destsys[a] == '.')) {
320 /* chop the system name out, so we're left with a user */
321 while (haschar(recp, '!'))
322 strcpy(recp, &recp[1]);
324 /* Convert underscores to spaces */
325 for (a = 0; a < strlen(recp); ++a)
329 /* Are we delivering to a room instead of a user? */
330 if (!strncasecmp(recp, "room ", 5)) {
331 strcpy(targetroom, &recp[5]);
334 strcpy(targetroom, MAILROOM);
338 snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
340 snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
341 temp = fopen(buf, "w");
344 strcpy(nodebuf, config.c_nodename);
345 strcpy(content_type, "text/plain");
348 if (fgets(buf, 128, stdin) == NULL)
350 strip_trailing_whitespace(buf);
352 if (!strncasecmp(buf, "Subject: ", 9))
353 strcpy(subject, &buf[9]);
354 else if (!strncasecmp(buf, "Date: ", 6)) {
355 now = parsedate(&buf[6]);
356 if (now < 0L) now = time(NULL);
358 else if (!strncasecmp(buf, "From: ", 6))
359 strcpy(from, &buf[6]);
360 else if (!strncasecmp(buf, "Content-type: ", 14))
361 strcpy(content_type, &buf[14]);
362 else if (!strncasecmp(buf, "From ", 5)) { /* ignore */
364 if (extra_headers == NULL) {
365 extra_headers = malloc(strlen(buf) + 2);
366 strcpy(extra_headers, "");
368 extra_headers = realloc(extra_headers,
369 (strlen(extra_headers) + strlen(buf) + 2));
371 strcat(extra_headers, buf);
372 strcat(extra_headers, "\n");
374 } while ((strcmp(buf, ".")) && (strcmp(buf, "")));
376 process_rfc822_addr(from, userbuf, nodebuf, frombuf);
378 if (!strncasecmp(content_type, "text/plain", 10))
379 format_type = 1; /* plain ASCII message */
381 format_type = 4; /* MIME message */
383 /* now convert it to Citadel format */
386 putc(255, temp); /* 0xFF = start-of-message byte */
387 putc(MES_NORMAL, temp); /* Non-anonymous message */
388 putc(format_type, temp); /* Format type */
391 fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0);
393 fprintf(temp, "I%ld%c", message_id, 0);
394 fprintf(temp, "T%ld%c", (long)now, 0);
395 fprintf(temp, "A%s%c", userbuf, 0);
398 if (strlen(targetroom) > 0) {
399 fprintf(temp, "O%s%c", targetroom, 0);
401 fprintf(temp, "O%s%c", MAILROOM, 0);
404 fprintf(temp, "N%s%c", nodebuf, 0);
405 fprintf(temp, "H%s%c", frombuf, 0);
406 if (dtype == DELIVER_REMOTE) {
407 fprintf(temp, "D%s%c", destsys, 0);
409 if (strlen(recp) > 0) {
410 fprintf(temp, "R%s%c", recp, 0);
412 /* Subject and text */
413 if (strlen(subject) > 0) {
414 fprintf(temp, "U%s%c", subject, 0);
417 if (format_type == 4) {
418 fprintf(temp, "Content-type: %s\n", content_type);
419 if (extra_headers != NULL)
420 fprintf(temp, "%s", extra_headers);
423 if (extra_headers != NULL)
425 if (strcmp(buf, "."))
426 loopcopy(temp, stdin);
432 void do_uudecode(char *target)
434 static char buf[1024];
437 snprintf(buf, sizeof buf, "cd %s; uudecode", target);
439 fp = popen(buf, "w");
442 while (fgets(buf, 1024, stdin) != NULL) {
443 fprintf(fp, "%s", buf);
449 int alias(char *name)
455 fp = fopen(ALIASES, "r");
457 syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno));
460 while (fgets(abuf, 256, fp) != NULL) {
461 strip_trailing_whitespace(abuf);
462 for (a = 0; a < strlen(abuf); ++a) {
463 if (abuf[a] == ',') {
465 if (!strcasecmp(name, abuf)) {
466 strcpy(name, &abuf[a + 1]);
476 void deliver(char recp[], int is_test, int deliver_to_ignet)
479 /* various ways we can deliver mail... */
481 if (deliver_to_ignet) {
482 syslog(LOG_NOTICE, "to Citadel network user %s", recp);
484 do_citmail(recp, DELIVER_REMOTE);
485 } else if (!strcmp(recp, "uudecode")) {
486 syslog(LOG_NOTICE, "uudecoding to bit bucket directory");
488 do_uudecode(config.c_bucket_dir);
489 } else if (!strcmp(recp, "cit86net")) {
490 syslog(LOG_NOTICE, "uudecoding to Cit86net spool");
492 do_uudecode(CIT86NET);
493 system("exec ./BatchTranslate86");
495 } else if (!strcmp(recp, "null")) {
496 syslog(LOG_NOTICE, "zapping nulled message");
498 /* Otherwise, the user is local (or an unknown name was
499 * specified, in which case we let netproc handle the bounce)
501 syslog(LOG_NOTICE, "to Citadel recipient %s", recp);
503 do_citmail(recp, DELIVER_LOCAL);
510 int main(int argc, char **argv)
513 int deliver_to_ignet = 0;
514 static char recp[1024], buf[1024];
515 static char user[1024], node[1024], name[1024];
518 openlog("citmail", LOG_PID, LOG_USER);
520 LoadInternetConfig();
522 if (!strcmp(argv[1], "-t")) {
524 syslog(LOG_NOTICE, "test mode - will not deliver");
527 strcpy(recp, argv[1]);
529 strcpy(recp, argv[2]);
532 /*** Non-SMTP delivery mode ***/
533 syslog(LOG_NOTICE, "recp: %s", recp);
534 for (a = 0; a < 2; ++a) {
538 /* did we alias it back to a remote address? */
539 if ((haschar(recp, '%'))
540 || (haschar(recp, '@'))
541 || (haschar(recp, '!'))) {
543 process_rfc822_addr(recp, user, node, name);
546 /* If there are dots, it's an Internet host, so feed it
547 * back to an external mail transport agent such as sendmail.
549 if (haschar(node, '.')) {
550 snprintf(buf, sizeof buf, SENDMAIL, recp);
554 /* Otherwise, we're dealing with Citadel mail. */
556 snprintf(recp, sizeof recp, "%s!%s", node, user);
557 deliver_to_ignet = 1;
561 deliver(recp, is_test, deliver_to_ignet);
564 syslog(LOG_NOTICE, "running netproc");
565 if (system("/bin/true") != 0) {
566 syslog(LOG_ERR, "netproc failed: %s", strerror(errno));
569 syslog(LOG_NOTICE, "skipping netproc");