X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fcitmail.c;h=f1c6ae8c780b1e85708f8a378520dae8d1328151;hb=5b4f25945be356aa3a8797e70bbe16355538a967;hp=49a8524e65e4211f9ad22cb209ddb9e18a5ae58c;hpb=dec376f24d293ec5b3b979981238115ad4e7fd9c;p=citadel.git diff --git a/citadel/citmail.c b/citadel/citmail.c index 49a8524e6..f1c6ae8c7 100644 --- a/citadel/citmail.c +++ b/citadel/citmail.c @@ -1,631 +1,342 @@ -/* - * citmail.c v4.2 - * $Id$ - * - * This program may be used as a local mail delivery agent, which will allow - * all Citadel users to receive Internet e-mail. To enable this functionality, - * you must tell sendmail, smail, or whatever mailer you are using, that this - * program is your local mail delivery agent. This program is a direct - * replacement for lmail, deliver, or whatever. - * - * Usage: - * - * citmail - Deliver a message - * citmail -t - Address test mode (will not deliver) - * - */ +// This program attempts to act like a local MDA if you're using sendmail or +// some other non-Citadel MTA. It basically just contacts the Citadel LMTP +// listener on a unix domain socket and transmits the message. Really though, +// if your MTA supports LMTP then you definitely should be using that instead. +// +// Copyright (c) 1987-2021 by the citadel.org team +// +// This program is open source software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 3. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. #include "sysdep.h" #include #include -#include -#include #include +#include +#include +#include +#include +#include +#include #include -#include #include #include -#include +#include #include +#include #include "citadel.h" -#include "config.h" -#include "internetmail.h" - -/* message delivery classes */ -enum { - DELIVER_LOCAL, - DELIVER_REMOTE, - DELIVER_INTERNET, - DELIVER_CCITADEL -}; - - -#undef tolower -#define tolower(x) isupper(x) ? (x+'a'-'A') : x - -char *monthdesc[] = -{"Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - -char ALIASES[128]; -char CIT86NET[128]; -char SENDMAIL[128]; -char FALLBACK[128]; -char GW_DOMAIN[128]; -char TABLEFILE[128]; -char OUTGOING_FQDN[128]; -int RUN_NETPROC = 1; - - -long conv_date(char *sdbuf) -{ - int a, b, cpos, tend, tval; - time_t now; - struct tm *tmbuf; - char dbuf[128]; - - strcpy(dbuf, sdbuf); - time(&now); - tmbuf = (struct tm *) localtime(&now); - - /* get rid of + or - timezone mods */ - for (a = 0; a < strlen(dbuf); ++a) - if ((dbuf[a] == '+') || (dbuf[a] == '-')) - do { - strcpy(&dbuf[a], &dbuf[a + 1]); - } while ((dbuf[a] != 32) && (dbuf[a] != 0)); - - /* try and extract the time by looking for colons */ - cpos = (-1); - for (a = strlen(dbuf); a >= 0; --a) - if ((dbuf[a] == ':') && (atoi(&dbuf[a - 1]) != 0)) - cpos = a; - if (cpos >= 0) { - cpos = cpos - 2; - tend = strlen(dbuf); - for (a = tend; a >= cpos; --a) - if (dbuf[a] == ' ') - tend = a; - - tmbuf->tm_hour = atoi(&dbuf[cpos]); - tmbuf->tm_min = atoi(&dbuf[cpos + 3]); - tmbuf->tm_sec = atoi(&dbuf[cpos + 6]); - - do { - strcpy(&dbuf[cpos], &dbuf[cpos + 1]); - } while ((dbuf[cpos] != 32) && (dbuf[cpos] != 0)); - } - /* next try to extract a month */ - - tval = (-1); - for (a = 0; a < strlen(dbuf); ++a) - for (b = 0; b < 12; ++b) - if (!strncmp(&dbuf[a], monthdesc[b], 3)) { - tval = b; - cpos = a; - } - if (tval >= 0) { - tmbuf->tm_mon = tval; - strcpy(&dbuf[cpos], &dbuf[cpos + 3]); +#include "citadel_dirs.h" + +int serv_sock; +int debug = 0; + +void strip_trailing_nonprint(char *buf) { + while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) ) { + buf[strlen(buf) - 1] = 0; } - /* now the year */ +} - for (a = 0; a < strlen(dbuf); ++a) - if ((atoi(&dbuf[a]) >= 1900) && (dbuf[a] != 32)) { - tmbuf->tm_year = atoi(&dbuf[a]) - 1900; - strcpy(&dbuf[a], &dbuf[a + 4]); - } - /* whatever's left is the mday (hopefully) */ - for (a = 0; a < strlen(dbuf); ++a) - if ((dbuf[a] != 32) && (atoi(&dbuf[a]) >= 1) && (atoi(&dbuf[a]) <= 31) - && ((a == 0) || (dbuf[a - 1] == ' '))) { - tmbuf->tm_mday = atoi(&dbuf[a]); - strcpy(&dbuf[a], &dbuf[a + 2]); - } - return ((long) mktime(tmbuf)); +void timeout(int signum) { + exit(signum); } -#ifndef HAVE_STRERROR -/* - * replacement strerror() for systems that don't have it - */ -char *strerror(int e) -{ - static char buf[32]; +int uds_connectsock(char *sockpath) { + int s; + struct sockaddr_un addr; - snprintf(buf, sizeof buf, "errno = %d", e); - return (buf); -} -#endif + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, sockpath); -int haschar(char *st, int ch) -{ - int a, b; - b = 0; - for (a = 0; a < strlen(st); ++a) - if (st[a] == ch) - ++b; - return (b); -} + s = socket(AF_UNIX, SOCK_STREAM, 0); + if (s < 0) { + fprintf(stderr, "citmail: Can't create socket: %s\n", + strerror(errno)); + exit(3); + } -void strip_trailing_whitespace(char *buf) -{ - while (isspace(buf[strlen(buf) - 1])) - buf[strlen(buf) - 1] = 0; -} + if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + fprintf(stderr, "citmail: can't connect: %s\n", + strerror(errno)); + close(s); + exit(3); + } -/* strip leading and trailing spaces */ -void striplt(char *buf) -{ - while ((strlen(buf) > 0) && (buf[0] == 32)) - strcpy(buf, &buf[1]); - while (buf[strlen(buf) - 1] == 32) - buf[strlen(buf) - 1] = 0; + return s; } /* - * Check to see if a given FQDN really maps to a Citadel network node + * input binary data from socket */ -void host_alias(char host[]) +void serv_read(char *buf, int bytes) { + int len, rlen; - int a; - - /* What name is the local host known by? */ - /* if (!strcasecmp(host, config.c_fqdn)) { */ - if (IsHostLocal(host)) { - strcpy(host, config.c_nodename); - return; - } - /* Other hosts in the gateway domain? */ - for (a = 0; a < strlen(host); ++a) { - if ((host[a] == '.') && (!strcasecmp(&host[a + 1], GW_DOMAIN))) { - host[a] = 0; - for (a = 0; a < strlen(host); ++a) { - if (host[a] == '.') - host[a] = 0; - } + len = 0; + while (len < bytes) { + rlen = read(serv_sock, &buf[len], bytes - len); + if (rlen < 1) { return; } + len = len + rlen; } - - /* Otherwise, do nothing... */ } - /* - * Split an RFC822-style address into userid, host, and full name + * send binary to server */ -void process_rfc822_addr(char *rfc822, char *user, char *node, char *name) +void serv_write(char *buf, int nbytes) { - int a; - - /* extract full name - first, it's From minus */ - strcpy(name, rfc822); - for (a = 0; a < strlen(name); ++a) - if (name[a] == '<') { - do { - strcpy(&name[a], &name[a + 1]); - } while ((strlen(name) > 0) && (name[a] != '>')); - strcpy(&name[a], &name[a + 1]); - } - /* strip anything to the left of a bang */ - while ((strlen(name) > 0) && (haschar(name, '!') > 0)) - strcpy(name, &name[1]); - - /* and anything to the right of a @ or % */ - for (a = 0; a < strlen(name); ++a) { - if (name[a] == '@') - name[a] = 0; - if (name[a] == '%') - name[a] = 0; - } - - /* but if there are parentheses, that changes the rules... */ - if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) { - strcpy(name, rfc822); - while ((strlen(name) > 0) && (name[0] != '(')) { - strcpy(&name[0], &name[1]); - } - strcpy(&name[0], &name[1]); - for (a = 0; a < strlen(name); ++a) - if (name[a] == ')') - name[a] = 0; - } - /* but if there are a set of quotes, that supersedes everything */ - if (haschar(rfc822, 34) == 2) { - strcpy(name, rfc822); - while ((strlen(name) > 0) && (name[0] != 34)) { - strcpy(&name[0], &name[1]); - } - strcpy(&name[0], &name[1]); - for (a = 0; a < strlen(name); ++a) - if (name[a] == 34) - name[a] = 0; - } - /* extract user id */ - strcpy(user, rfc822); - - /* first get rid of anything in parens */ - for (a = 0; a < strlen(user); ++a) - if (user[a] == '(') { - do { - strcpy(&user[a], &user[a + 1]); - } while ((strlen(user) > 0) && (user[a] != ')')); - strcpy(&user[a], &user[a + 1]); - } - /* if there's a set of angle brackets, strip it down to that */ - if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) { - while ((strlen(user) > 0) && (user[0] != '<')) { - strcpy(&user[0], &user[1]); - } - strcpy(&user[0], &user[1]); - for (a = 0; a < strlen(user); ++a) - if (user[a] == '>') - user[a] = 0; - } - /* strip anything to the left of a bang */ - while ((strlen(user) > 0) && (haschar(user, '!') > 0)) - strcpy(user, &user[1]); - - /* and anything to the right of a @ or % */ - for (a = 0; a < strlen(user); ++a) { - if (user[a] == '@') - user[a] = 0; - if (user[a] == '%') - user[a] = 0; - } - - - - /* extract node name */ - strcpy(node, rfc822); - - /* first get rid of anything in parens */ - for (a = 0; a < strlen(node); ++a) - if (node[a] == '(') { - do { - strcpy(&node[a], &node[a + 1]); - } while ((strlen(node) > 0) && (node[a] != ')')); - strcpy(&node[a], &node[a + 1]); - } - /* if there's a set of angle brackets, strip it down to that */ - if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) { - while ((strlen(node) > 0) && (node[0] != '<')) { - strcpy(&node[0], &node[1]); + int bytes_written = 0; + int retval; + while (bytes_written < nbytes) { + retval = write(serv_sock, &buf[bytes_written], + nbytes - bytes_written); + if (retval < 1) { + return; } - strcpy(&node[0], &node[1]); - for (a = 0; a < strlen(node); ++a) - if (node[a] == '>') - node[a] = 0; + bytes_written = bytes_written + retval; } - /* strip anything to the left of a @ */ - while ((strlen(node) > 0) && (haschar(node, '@') > 0)) - strcpy(node, &node[1]); - - /* strip anything to the left of a % */ - while ((strlen(node) > 0) && (haschar(node, '%') > 0)) - strcpy(node, &node[1]); - - /* reduce multiple system bang paths to node!user */ - while ((strlen(node) > 0) && (haschar(node, '!') > 1)) - strcpy(node, &node[1]); - - /* now get rid of the user portion of a node!user string */ - for (a = 0; a < strlen(node); ++a) - if (node[a] == '!') - node[a] = 0; - +} - /* strip leading and trailing spaces in all strings */ - striplt(user); - striplt(node); - striplt(name); -} /* - * Copy line by line, ending at EOF or a "." + * input string from socket - implemented in terms of serv_read() */ -void loopcopy(FILE * to, FILE * from) +void serv_gets(char *buf) { - char buf[1024]; - char *r; - - while (1) { - r = fgets(buf, sizeof(buf), from); - if (r == NULL) - return; - strip_trailing_whitespace(buf); - if (!strcmp(buf, ".")) - return; - fprintf(to, "%s\n", buf); + int i; + + /* Read one character at a time. + */ + for (i = 0;; i++) { + serv_read(&buf[i], 1); + if (buf[i] == '\n' || i == (SIZ-1)) + break; } + + /* If we got a long line, discard characters until the newline. + */ + if (i == (SIZ-1)) + while (buf[i] != '\n') + serv_read(&buf[i], 1); + + /* Strip all trailing nonprintables (crlf) + */ + buf[i] = 0; + strip_trailing_nonprint(buf); + if (debug) fprintf(stderr, "> %s\n", buf); } /* - * pipe message through netproc + * send line to server - implemented in terms of serv_write() */ -void do_citmail(char recp[], int dtype) +void serv_puts(char *buf) { + if (debug) fprintf(stderr, "< %s\n", buf); + serv_write(buf, strlen(buf)); + serv_write("\n", 1); +} - time_t now; - FILE *temp; - int a; - int format_type = 0; - char buf[128]; - char from[512]; - char userbuf[256]; - char frombuf[256]; - char nodebuf[256]; - char destsys[256]; - char subject[256]; - char targetroom[256]; - char content_type[256]; - char *extra_headers = NULL; - - - if (dtype == DELIVER_REMOTE) { - - /* get the Citadel node name out of the path */ - strncpy(destsys, recp, sizeof(destsys)); - for (a = 0; a < strlen(destsys); ++a) { - if ((destsys[a] == '!') || (destsys[a] == '.')) { - destsys[a] = 0; - } - } - - /* chop the system name out, so we're left with a user */ - while (haschar(recp, '!')) - strcpy(recp, &recp[1]); - } - /* Convert underscores to spaces */ - for (a = 0; a < strlen(recp); ++a) - if (recp[a] == '_') - recp[a] = ' '; - - /* Are we delivering to a room instead of a user? */ - if (!strncasecmp(recp, "room ", 5)) { - strcpy(targetroom, &recp[5]); - strcpy(recp, ""); - } else { - strcpy(targetroom, MAILROOM); - } - - time(&now); - snprintf(from, sizeof from, "postmaster@%s", config.c_nodename); - - snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid()); - temp = fopen(buf, "w"); - - strcpy(subject, ""); - strcpy(nodebuf, config.c_nodename); - strcpy(content_type, "text/plain"); - - do { - if (fgets(buf, 128, stdin) == NULL) - strcpy(buf, "."); - strip_trailing_whitespace(buf); - - if (!strncasecmp(buf, "Subject: ", 9)) - strcpy(subject, &buf[9]); - else if (!strncasecmp(buf, "Date: ", 6)) - now = conv_date(&buf[6]); - else if (!strncasecmp(buf, "From: ", 6)) - strcpy(from, &buf[6]); - else if (!strncasecmp(buf, "Content-type: ", 14)) - strcpy(content_type, &buf[14]); - else if (!strncasecmp(buf, "From ", 5)) { /* ignore */ - } else { - if (extra_headers == NULL) { - extra_headers = malloc(strlen(buf) + 2); - strcpy(extra_headers, ""); - } else { - extra_headers = realloc(extra_headers, - (strlen(extra_headers) + strlen(buf) + 2)); - } - strcat(extra_headers, buf); - strcat(extra_headers, "\n"); - } - } while ((strcmp(buf, ".")) && (strcmp(buf, ""))); - process_rfc822_addr(from, userbuf, nodebuf, frombuf); - if (!strncasecmp(content_type, "text/plain", 10)) - format_type = 1; /* plain ASCII message */ - else - format_type = 4; /* MIME message */ +void cleanup(int exitcode) { + char buf[1024]; - /* now convert it to Citadel format */ + if (exitcode != 0) { + fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode); + fprintf(stderr, "Please check your Citadel configuration.\n"); + } + serv_puts("QUIT"); + serv_gets(buf); + exit(exitcode); +} - /* Header bytes */ - putc(255, temp); /* 0xFF = start-of-message byte */ - putc(MES_NORMAL, temp); /* Non-anonymous message */ - putc(format_type, temp); /* Format type */ - /* Origination */ - fprintf(temp, "P%s@%s%c", userbuf, nodebuf, 0); - fprintf(temp, "T%ld%c", (long)now, 0); - fprintf(temp, "A%s%c", userbuf, 0); - /* Destination */ - if (strlen(targetroom) > 0) { - fprintf(temp, "O%s%c", targetroom, 0); - } else { - fprintf(temp, "O%s%c", MAILROOM, 0); +int main(int argc, char **argv) { + char buf[1024]; + char fromline[1024]; + FILE *fp; + int i; + struct passwd *pw; + int from_header = 0; + int in_body = 0; + char ctdldir[PATH_MAX]=CTDLDIR; + char *sp, *ep; + char hostname[256]; + char **recipients = NULL; + int num_recipients = 0; + int to_or_cc = 0; + int read_recipients_from_headers = 0; + char *add_these_recipients = NULL; + + for (i=1; i 0) { - fprintf(temp, "R%s%c", recp, 0); + + pw = getpwuid(getuid()); + + fp = tmpfile(); + if (fp == NULL) return(errno); + serv_sock = uds_connectsock(file_lmtp_socket); + serv_gets(buf); + if (buf[0] != '2') { + fprintf(stderr, "%s\n", &buf[4]); + if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n"); + cleanup(1); } - /* Subject and text */ - if (strlen(subject) > 0) { - fprintf(temp, "U%s%c", subject, 0); + + sp = strchr (buf, ' '); + if (sp == NULL) { + if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n"); + cleanup(2); } - putc('M', temp); - if (format_type == 4) { - fprintf(temp, "Content-type: %s\n", content_type); - if (extra_headers != NULL) - fprintf(temp, "%s", extra_headers); - fprintf(temp, "\n"); + sp ++; + ep = strchr (sp, ' '); + if (ep == NULL) { + if (debug) fprintf(stderr, "citmail: error parsing hostname\n"); + cleanup(3); } - if (extra_headers != NULL) - free(extra_headers); - if (strcmp(buf, ".")) - loopcopy(temp, stdin); - putc(0, temp); - fclose(temp); -} - - -void do_uudecode(char *target) -{ - static char buf[1024]; - FILE *fp; + else + *ep = '\0'; - snprintf(buf, sizeof buf, "cd %s; uudecode", target); + strncpy(hostname, sp, sizeof hostname); - fp = popen(buf, "w"); - if (fp == NULL) - return; + snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname); while (fgets(buf, 1024, stdin) != NULL) { - fprintf(fp, "%s", buf); - } - pclose(fp); - -} + if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) { + in_body = 1; + if (from_header == 0) { + fprintf(fp, "%s%s", fromline, buf); + } + } + if (in_body == 0 && !strncasecmp(buf, "From:", 5)) { + strcpy(fromline, buf); + from_header = 1; + } -int alias(char *name) -{ - FILE *fp; - int a; - char abuf[256]; + if (read_recipients_from_headers) { + add_these_recipients = NULL; + if ((isspace(buf[0])) && (to_or_cc)) { + add_these_recipients = buf; + } + else { + if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) { + to_or_cc = 1; + } + else { + to_or_cc = 0; + } + if (to_or_cc) { + add_these_recipients = &buf[3]; + } + } - fp = fopen(ALIASES, "r"); - if (fp == NULL) { - syslog(LOG_ERR, "cannot open %s: %s", ALIASES, strerror(errno)); - return (2); - } - while (fgets(abuf, 256, fp) != NULL) { - strip_trailing_whitespace(abuf); - for (a = 0; a < strlen(abuf); ++a) { - if (abuf[a] == ',') { - abuf[a] = 0; - if (!strcasecmp(name, abuf)) { - strcpy(name, &abuf[a + 1]); + if (add_these_recipients) { + int num_recp_on_this_line; + char this_recp[256]; + + num_recp_on_this_line = num_tokens(add_these_recipients, ','); + for (i=0; i