/*
- * citmail.c v4.0
+ * 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,
*
* citmail <recipient> - Deliver a message
* citmail -t <recipient> - Address test mode (will not deliver)
- * citmail -i - Run as an SMTP daemon (typically from inetd)
*
*/
#include <pwd.h>
#include <errno.h>
#include <syslog.h>
+#include <limits.h>
#include "citadel.h"
+#include "config.h"
+#include "internetmail.h"
#define LOCAL 0
#define REMOTE 1
char *monthdesc[] = { "Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec" };
-
-
-void LoadInternetConfig();
-void get_config();
-int IsHostLocal();
-struct config config;
-
char ALIASES[128];
char CIT86NET[128];
char SENDMAIL[128];
char OUTGOING_FQDN[128];
int RUN_NETPROC = 1;
-int struncmp(lstr,rstr,l)
-char lstr[],rstr[]; {
- int pos = 0;
- char lc,rc;
- while (1) {
- if (pos==l) return(0);
- lc=tolower(lstr[pos]);
- rc=tolower(rstr[pos]);
- if ((lc==0)&&(rc==0)) return(0);
- if (lc<rc) return(-1);
- if (lc>rc) return(1);
- pos=pos+1;
- }
- }
-long conv_date(sdbuf)
-char sdbuf[]; {
+long conv_date(char *sdbuf)
+{
int a,b,cpos,tend,tval;
- long now;
+ time_t now;
struct tm *tmbuf;
char dbuf[128];
}
-#ifdef NO_STRERROR
+#ifndef HAVE_STRERROR
/*
* replacement strerror() for systems that don't have it
*/
-char *strerror(e)
-int e; {
+char *strerror(int e)
+{
static char buf[32];
- sprintf(buf,"errno = %d",e);
+ snprintf(buf,sizeof buf,"errno = %d",e);
return(buf);
}
#endif
-int haschar(st,ch)
-char st[];
-int ch; {
+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);
}
-void strip_trailing_whitespace(buf)
-char buf[]; {
+void strip_trailing_whitespace(char *buf)
+{
while(isspace(buf[strlen(buf)-1]))
buf[strlen(buf)-1]=0;
}
-int islocalok(char recp[]) {
-
- struct usersupp ust;
- long lookfor;
- char a_recp[128];
- int found_closest_match = 0;
- int a,us;
- strcpy(a_recp,recp);
- for (a=0; a<strlen(a_recp); ++a)
- if (a_recp[a]=='_') a_recp[a]=32;
- lookfor = (-1L); if (!struncmp(recp,"cit",3)) lookfor=atol(&recp[3]);
- us=open("/appl/citadel/usersupp",O_RDONLY);
- if (us>=0) {
- while(read(us,&ust,sizeof(struct usersupp))>0) {
- if (lookfor == ust.usernum) {
- strcpy(recp,ust.fullname);
- close(us);
- return(2);
- }
- if (!strucmp(ust.fullname,a_recp)) {
- strcpy(recp,ust.fullname);
- close(us);
- return(3);
- }
- if (!struncmp(ust.fullname,a_recp,strlen(a_recp))) {
- strcpy(recp,ust.fullname);
- found_closest_match = 1;
- }
- }
- close(us);
- }
- if (getpwnam(recp)!=NULL) return(1);
- if (found_closest_match) return(3);
- return(0);
- }
-
/* strip leading and trailing spaces */
-void striplt(buf)
-char buf[]; {
+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;
}
int a;
/* What name is the local host known by? */
- /* if (!strucmp(host, config.c_fqdn)) { */
+ /* 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]=='.') && (!strucmp(&host[a+1], GW_DOMAIN))) {
+ 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;
/*
* Split an RFC822-style address into userid, host, and full name
*/
-void process_rfc822_addr(rfc822,user,node,name)
-char rfc822[];
-char user[];
-char node[];
-char name[]; {
+void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
+{
int a;
/* extract full name - first, it's From minus <userid> */
*/
void do_citmail(char recp[], int dtype) {
- long now;
+ time_t now;
FILE *temp;
int a;
+ int format_type = 0;
char buf[128];
char from[512];
char userbuf[256];
char nodebuf[256];
char destsys[256];
char subject[256];
+ char targetroom[256];
+ char content_type[256];
+ char *extra_headers = NULL;
if (dtype==REMOTE) {
/* chop the system name out, so we're left with a user */
while (haschar(recp,'!')) strcpy(recp,&recp[1]);
+ }
- /* now convert underscores to spaces */
- for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
+ /* 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);
- sprintf(from, "postmaster@%s", config.c_nodename);
+ snprintf(from, sizeof from, "postmaster@%s", config.c_nodename);
- sprintf(buf, "./network/spoolin/citmail.%d", getpid());
+ snprintf(buf, sizeof buf, "./network/spoolin/citmail.%d", getpid());
temp = fopen(buf,"w");
- putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
- strcpy(subject,"");
+ 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 (!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]);
+ 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 (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 */
+
/* now convert it to Citadel format */
+
+ /* 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,"E%s%c", userbuf, 0);
fprintf(temp,"T%ld%c", now, 0);
- fprintf(temp,"A%s%c", frombuf, 0);
- fprintf(temp,"OMail%c", 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);
+ }
+
fprintf(temp,"N%s%c", nodebuf, 0);
+ fprintf(temp,"H%s%c", frombuf, 0);
if (dtype==REMOTE) {
fprintf(temp,"D%s%c", destsys, 0);
}
- fprintf(temp,"R%s%c", recp, 0);
- 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);
- }
-
-void do_roommail(recp) /* pipe public message through netproc */
-char recp[]; {
- long now;
- FILE *temp;
- int a;
- char buf[128],userbuf[128],frombuf[128],nodebuf[128];
- char subject[128], from[256];
-
- strcpy(subject,"");
- sprintf(from, "postmaster@%s", config.c_nodename);
- strcpy(recp,&recp[5]);
- for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=32;
- time(&now);
+ if (strlen(recp) > 0) {
+ fprintf(temp,"R%s%c", recp, 0);
+ }
- sprintf(buf,"./network/spoolin/citmail.%d",getpid());
- temp = fopen(buf,"w");
-
- putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
- strcpy(frombuf,"Internet Mail Gateway");
- strcpy(nodebuf, config.c_nodename);
- do {
- if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
- strip_trailing_whitespace(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);
-
- fprintf(temp,"P%s@%s",userbuf,nodebuf); putc(0,temp);
- fprintf(temp,"T%ld",now); putc(0,temp);
- fprintf(temp,"A%s",frombuf); putc(0,temp);
- fprintf(temp,"O%s",recp); putc(0,temp);
- fprintf(temp,"N%s",nodebuf); putc(0,temp);
+ /* Subject and text */
if (strlen(subject)>0) {
- fprintf(temp,"U%s",subject); putc(0,temp);
+ fprintf(temp,"U%s%c", subject, 0);
}
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");
+ }
+ if (extra_headers != NULL) free(extra_headers);
if (strcmp(buf, ".")) loopcopy(temp, stdin);
putc(0,temp);
fclose(temp);
}
-void do_uudecode(target)
-char *target; {
+
+void do_uudecode(char *target)
+{
static char buf[1024];
FILE *fp;
- sprintf(buf,"cd %s; uudecode",target);
+ snprintf(buf,sizeof buf,"cd %s; uudecode",target);
fp=popen(buf,"w");
if (fp==NULL) return;
}
-void do_fallback(recp)
-char recp[]; {
- static char buf[1024];
- FILE *fp;
-
- sprintf(buf, FALLBACK, recp);
- fp=popen(buf,"w");
- if (fp==NULL) fp = popen("cat >/dev/null", "w");
- loopcopy(fp, stdin);
- pclose(fp);
- }
-
-int alias(name)
-char *name; {
+int alias(char *name)
+{
FILE *fp;
int a;
char abuf[256];
for (a=0; a<strlen(abuf); ++a) {
if (abuf[a]==',') {
abuf[a]=0;
- if (!strucmp(name,abuf)) {
+ if (!strcasecmp(name,abuf)) {
strcpy(name,&abuf[a+1]);
}
}
void deliver(char recp[], int is_test, int deliver_to_ignet) {
- int b;
- b=islocalok(recp);
-
/* various ways we can deliver mail... */
if (deliver_to_ignet) {
syslog(LOG_NOTICE,"zapping nulled message");
}
- else if (!struncmp(recp,"room_",5)) {
- syslog(LOG_NOTICE,"to room %s",recp);
- if (is_test == 0) do_roommail(recp);
- }
-
- else if (b==1) {
- syslog(LOG_NOTICE,"fallback mailer to user %s",recp);
- if (is_test == 0) do_fallback(recp);
- }
-
else {
- /* Otherwise, the user is local (or an unknown name was specified, in
- * which case we let netproc handle the bounce)
+ /* Otherwise, the user is local (or an unknown name was
+ * specified, in which case we let netproc handle the bounce)
*/
- syslog(LOG_NOTICE,"to Citadel user %s",recp);
+ syslog(LOG_NOTICE,"to Citadel recipient %s",recp);
if (is_test == 0) do_citmail(recp, LOCAL);
}
-void main(argc,argv)
-int argc;
-char *argv[]; {
+int main(int argc, char **argv)
+{
int is_test = 0;
int deliver_to_ignet = 0;
- int smtp = 0;
static char recp[1024], buf[1024];
static char user[1024], node[1024], name[1024];
int a;
is_test = 1;
syslog(LOG_NOTICE,"test mode - will not deliver");
}
- if (!strcmp(argv[1], "-i")) {
- smtp = 1;
- syslog(LOG_NOTICE,"started as an SMTP daemon");
- }
-
- if (smtp) {
- strcpy(recp, "");
- }
- else if (is_test == 0) {
+ if (is_test == 0) {
strcpy(recp,argv[1]);
}
else {
strcpy(recp,argv[2]);
}
-
- 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;
+ /*** Non-SMTP delivery mode ***/
+ syslog(LOG_NOTICE,"recp: %s",recp);
+ for (a=0; a<2; ++a) {
+ alias(recp);
+ }
- if (!struncmp(buf, "QUIT", 4)) {
- printf("221 Later, dude.\n");
- }
- else if (!struncmp(buf, "HELP", 4)) {
- printf("214 You think _you_ need help?\n");
- }
- else if (!struncmp(buf, "HELO", 4)) {
- printf("250 Howdy ho, Mr. Hankey!\n");
- }
- else if (!struncmp(buf, "MAIL", 4)) {
- printf("250 Sure, whatever...\n");
- }
-
+ /* did we alias it back to a remote address? */
+ if ( (haschar(recp,'%'))
+ || (haschar(recp,'@'))
+ || (haschar(recp,'!')) ) {
- else if (!struncmp(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 {
- sprintf(recp, "%s!%s", node, user);
- deliver_to_ignet = 1;
- printf("250 IGnet recipient.\n");
- }
- }
- else {
- printf("250 Local recipient.\n");
- }
+ 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;
+ }
- else if (!struncmp(buf, "RCPT", 4)) {
- printf("501 Only 'To:' commands are supported.\n");
- }
- else if (!struncmp(buf, "DATA", 4)) {
- if (strlen(recp) > 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 (struncmp(buf,"QUIT",4));
}
- 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, '.')) {
- sprintf(buf, SENDMAIL, recp);
- system(buf);
- exit(0);
- }
-
- /* Otherwise, we're dealing with Citadel mail. */
- else {
- sprintf(recp, "%s!%s", node, user);
- deliver_to_ignet = 1;
- }
-
- }
-
- deliver(recp, is_test, deliver_to_ignet);
- }
-
- closelog();
- if (RUN_NETPROC) execlp("./netproc","netproc",NULL);
+ deliver(recp, is_test, deliver_to_ignet);
+
+ if (RUN_NETPROC) execlp("./netproc", "netproc", "-i", NULL);
exit(0);
}