X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fcitmail.c;h=f1c6ae8c780b1e85708f8a378520dae8d1328151;hb=5b4f25945be356aa3a8797e70bbe16355538a967;hp=0a80370f3ccc0419c246481450fbe9dc6f6543ba;hpb=e310a61bd53e030e3c6adcb24848adf096a3020f;p=citadel.git diff --git a/citadel/citmail.c b/citadel/citmail.c index 0a80370f3..f1c6ae8c7 100644 --- a/citadel/citmail.c +++ b/citadel/citmail.c @@ -1,688 +1,342 @@ -/* - * citmail.c v4.1 - * $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) - * citmail -i - Run as an SMTP daemon (typically from inetd) - * - */ +// 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" - -#define LOCAL 0 -#define REMOTE 1 -#define UUCP 2 -#define CCITADEL 3 - -#undef tolower -#define tolower(x) isupper(x) ? (x+'a'-'A') : x +#include "citadel_dirs.h" -char *monthdesc[] = { "Jan","Feb","Mar","Apr","May","Jun", - "Jul","Aug","Sep","Oct","Nov","Dec" }; +int serv_sock; +int debug = 0; -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; +void strip_trailing_nonprint(char *buf) { + while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) ) { + buf[strlen(buf) - 1] = 0; + } +} -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=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)); - } +void timeout(int signum) { + exit(signum); +} - /* next try to extract a month */ - - tval = (-1); - for (a=0; a= 0) { - tmbuf->tm_mon = tval; - strcpy(&dbuf[cpos],&dbuf[cpos+3]); - } - /* now the year */ +int uds_connectsock(char *sockpath) { + int s; + struct sockaddr_un addr; - for (a=0; a=1900) && (dbuf[a]!=32)) { - tmbuf->tm_year = atoi(&dbuf[a]) - 1900; - strcpy(&dbuf[a],&dbuf[a+4]); - } + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, sockpath); - /* whatever's left is the mday (hopefully) */ - - for (a=0; 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)); + s = socket(AF_UNIX, SOCK_STREAM, 0); + if (s < 0) { + fprintf(stderr, "citmail: Can't create socket: %s\n", + strerror(errno)); + exit(3); } - -#ifndef HAVE_STRERROR -/* - * replacement strerror() for systems that don't have it - */ -char *strerror(int e) -{ - static char buf[32]; - - snprintf(buf,sizeof buf,"errno = %d",e); - return(buf); + if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + fprintf(stderr, "citmail: can't connect: %s\n", + strerror(errno)); + close(s); + exit(3); } -#endif -int haschar(char *st, int ch) -{ - int a,b; - b=0; - for (a=0; a0) && (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[]) { - - 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; - } +void serv_read(char *buf, int bytes) +{ + int len, rlen; - /* Other hosts in the gateway domain? */ - for (a=0; a */ - strcpy(name,rfc822); - for (a=0; a 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 0) && (name[0]!='(') ) { - strcpy(&name[0],&name[1]); - } - strcpy(&name[0],&name[1]); - for (a=0; a 0) && (name[0]!=34) ) { - strcpy(&name[0],&name[1]); - } - strcpy(&name[0],&name[1]); - for (a=0; a 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') 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 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]); - } - strcpy(&node[0],&node[1]); - for (a=0; a') node[a]=0; + int bytes_written = 0; + int retval; + while (bytes_written < nbytes) { + retval = write(serv_sock, &buf[bytes_written], + nbytes - bytes_written); + if (retval < 1) { + return; } - - /* 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 %s\n", buf); +} - if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]); - if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]); - if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]); - } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) ); - process_rfc822_addr(from, userbuf, nodebuf, frombuf); - - /* now convert it to Citadel format */ - fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0); - fprintf(temp,"T%ld%c", now, 0); - fprintf(temp,"A%s%c", userbuf, 0); +/* + * send line to server - implemented in terms of serv_write() + */ +void serv_puts(char *buf) +{ + if (debug) fprintf(stderr, "< %s\n", buf); + serv_write(buf, strlen(buf)); + serv_write("\n", 1); +} - if (strlen(targetroom) > 0) { - fprintf(temp, "O%s%c", targetroom, 0); - } - else { - fprintf(temp, "O%s%c", MAILROOM, 0); - } - fprintf(temp,"N%s%c", nodebuf, 0); - fprintf(temp,"H%s%c", frombuf, 0); - if (dtype==REMOTE) { - fprintf(temp,"D%s%c", destsys, 0); - } - if (strlen(recp) > 0) { - fprintf(temp,"R%s%c", recp, 0); - } +void cleanup(int exitcode) { + char buf[1024]; - if (strlen(subject)>0) { - fprintf(temp,"U%s%c", subject, 0); - } - putc('M',temp); - if (strcmp(buf, ".")) loopcopy(temp, stdin); - putc(0,temp); - fclose(temp); + 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); +} -void do_uudecode(char *target) -{ - static char buf[1024]; - FILE *fp; - - snprintf(buf,sizeof buf,"cd %s; uudecode",target); - - fp=popen(buf,"w"); - if (fp==NULL) return; - while (fgets(buf,1024,stdin)!=NULL) { - fprintf(fp,"%s",buf); - } - pclose(fp); - - } -int alias(char *name) -{ +int main(int argc, char **argv) { + char buf[1024]; + char fromline[1024]; FILE *fp; - int a; - char abuf[256]; - - 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; apw_name, hostname); + while (fgets(buf, 1024, stdin) != NULL) { + if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) { + in_body = 1; + if (from_header == 0) { + fprintf(fp, "%s%s", fromline, buf); + } } - else { - strcpy(recp,argv[2]); + if (in_body == 0 && !strncasecmp(buf, "From:", 5)) { + strcpy(fromline, buf); + from_header = 1; } - - if (smtp) { - /*** SMTP delivery mode ***/ - - printf("200 Citadel/UX SMTP gateway ready.\n"); - - do { - fflush(stdout); - fflush(stderr); - fgets(buf, 1024, stdin); - while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) { - buf[strlen(buf)-1] = 0; - } - - /* null-pad to allow some lazy compares */ - buf[strlen(buf)+1] = 0; - buf[strlen(buf)+2] = 0; - buf[strlen(buf)+3] = 0; - - if (!strncasecmp(buf, "QUIT", 4)) { - printf("221 Later, dude.\n"); - } - else if (!strncasecmp(buf, "HELP", 4)) { - printf("214 You think _you_ need help?\n"); + 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 if (!strncasecmp(buf, "HELO", 4)) { - printf("250 Howdy ho, Mr. Hankey!\n"); + else { + to_or_cc = 0; } - else if (!strncasecmp(buf, "MAIL", 4)) { - printf("250 Sure, whatever...\n"); + if (to_or_cc) { + add_these_recipients = &buf[3]; } + } - - else if (!strncasecmp(buf, "RCPT To: ", 9)) { - if (strlen(recp) > 0) { - printf("571 Multiple recipients not supported.\n"); - } - else { - strcpy(recp, &buf[9]); - if (haschar(recp, ',')) { - printf("571 Multiple recipients not supported.\n"); - strcpy(recp, ""); - } - else { - syslog(LOG_NOTICE,"recp: %s",recp); - for (a=0; a<2; ++a) { - alias(recp); - } - - /* did we alias it back to a remote address? */ - if ( (haschar(recp,'%')) - || (haschar(recp,'@')) - || (haschar(recp,'!')) ) { - - process_rfc822_addr(recp, user, node, name); - host_alias(node); - - /* If there are dots, it's an Internet host, so feed it - * back to an external mail transport agent such as sendmail. - */ - if (haschar(node, '.')) { - printf("571 Away with thee, spammer!\n"); - strcpy(recp, ""); - } - - /* Otherwise, we're dealing with Citadel mail. */ - else { - snprintf(recp, sizeof recp, "%s!%s", node, user); - deliver_to_ignet = 1; - printf("250 IGnet recipient.\n"); - } - } - else { - printf("250 Local recipient.\n"); - } - - } - + 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 0) { - printf("354 Sock it to me, baby...\n"); - fflush(stdout); - deliver(recp, is_test, deliver_to_ignet); - printf("250 Cool beans!\n"); - } - else { - printf("503 No recipient has been specified.\n"); - } - } - else { - printf("500 Huh?\n"); - } - - } while (strncasecmp(buf,"QUIT",4)); - } + serv_puts("DATA"); + serv_gets(buf); + if (buf[0]!='3') { + if (debug) fprintf(stderr, "citmail: DATA command failed\n"); + cleanup(6); + } + rewind(fp); + while (fgets(buf, sizeof buf, fp) != NULL) { + strip_trailing_nonprint(buf); + serv_puts(buf); + } + serv_puts("."); + serv_gets(buf); + if (buf[0] != '2') { + fprintf(stderr, "%s\n", &buf[4]); + cleanup(7); + } else { - /*** Non-SMTP delivery mode ***/ - syslog(LOG_NOTICE,"recp: %s",recp); - for (a=0; a<2; ++a) { - alias(recp); - } - - /* did we alias it back to a remote address? */ - if ( (haschar(recp,'%')) - || (haschar(recp,'@')) - || (haschar(recp,'!')) ) { - - process_rfc822_addr(recp, user, node, name); - host_alias(node); - - /* If there are dots, it's an Internet host, so feed it - * back to an external mail transport agent such as sendmail. - */ - if (haschar(node, '.')) { - snprintf(buf, sizeof buf, SENDMAIL, recp); - system(buf); - exit(0); - } - - /* Otherwise, we're dealing with Citadel mail. */ - else { - snprintf(recp, sizeof recp, "%s!%s", node, user); - deliver_to_ignet = 1; - } - - } - - deliver(recp, is_test, deliver_to_ignet); - } - - closelog(); - if (RUN_NETPROC) execlp("./netproc", "netproc", "-i", NULL); - exit(0); + cleanup(0); } + /* We won't actually reach this statement but the compiler will + * display a spurious warning about an invalid return type if + * we don't return an int. + */ + return(0); +}