1 /* rmail.c - replacement for /bin/rmail */
3 static char ident[] = "@(#)$Id: rmail.c,v 1.3 1993/08/25 17:27:43 jromine Exp $";
6 /* This program has a long, long history. It started as UCB's rmail, and
7 was then modified by OBrien@Rand-Unix to run with MMDF. Then DpK@Brl
8 re-wrote it, and SmB@Unc hacked it up a bit. After that
9 MRose.UCI@Rand-Relay upgraded it to use the nifty MF (mail filtering)
10 system. Finally, the latter stripped it down to work with MH.
12 This program should be used ONLY if you have both "mts mh" and "uucp on"
13 set in your MH configuration.
18 #include "../h/addrsbr.h"
19 #include "../zotnet/mf.h"
20 #include "../zotnet/tws.h"
22 #include "../zotnet/mts.h"
29 #define ADDROK 0 /* okay to use post to deliver message */
30 #define UUCP 1 /* okay to use uux to deliver message */
31 #define RETURN 2 /* message loses */
34 int pbroke; /* broken-pipe flag */
35 int rtnflag; /* note was sent back */
37 char date[BUFSIZ]; /* date of origination from uucp header */
38 char from[BUFSIZ]; /* accumulated path of sender */
39 char origsys[BUFSIZ]; /* originating system */
40 char origpath[BUFSIZ]; /* path from us to originating system */
41 char usrfrm[BUFSIZ]; /* the 822 version of from[] */
42 char Mailsys[BUFSIZ]; /* address of the mail agent */
43 char overseer[BUFSIZ]; /* address of the watchdog */
45 char mmdf[BUFSIZ]; /* filtered mail file */
47 char *rtnmessage[] = {
48 " Your message has been intercepted trying to access\n",
49 "a restricted access host (e.g. an ARPANET host). A copy\n",
50 "of your message has been sent to the system administrators.\n",
51 "The text of your message follows.\n\n",
56 " ---------------- Returned Mail Follows --------------\n";
58 " --------------- End of Returned Mail ---------------\n";
60 char *oopsmessage[] = {
61 "\n\n\tThe system administrators (%s) have been informed of\n",
62 "the problem, but have not been given a copy of your message.\n\n",
66 FILE * fromf; /* UUCP "From lines */
67 FILE * msgf; /* message text */
68 FILE * pipef; /* output for "post" or "uux" */
90 setlocale(LC_ALL, "");
92 invo_name = r1bindex (*argv, '/');
97 adios (NULLCP, "usage: %s user [user ...]", invo_name);
102 (void) sprintf (Mailsys, "%s@%s", Mailer, LocalName ());
103 if (Overseer == NULL)
105 if (index (Overseer, '@') == NULL) {
106 (void) sprintf (overseer, "%s@%s", Overseer, LocalName ());
110 (void) mktemp (Errtmp);
111 if (freopen (Errtmp, "w", stderr) == NULL)
112 adios (Errtmp, "unable to create");
113 (void) dup2 (fileno (stderr), fileno (stdout));
115 (void) mktemp (Msgtmp);
116 if ((msgf = fdopen (creat (Msgtmp, Tmpmode), "w")) == NULL)
117 adios (Msgtmp, "unable to create");
119 (void) mktemp (Fromtmp);
120 if ((fromf = fdopen (creat (Fromtmp, Tmpmode), "w")) == NULL)
121 adios (Fromtmp, "unable to create");
126 if (fgets (linebuf, sizeof linebuf, stdin) == NULL)
128 if (strncmp (linebuf, "From ", 5)
129 && strncmp (linebuf, ">From ", 6))
132 if (linebuf[0] != '>')
134 fputs (linebuf, fromf);
135 cp = index (linebuf, ' ');
137 cp = index (cp, ' ');
139 (void) strcpy (fromwhom, fromptr);
140 (void) strncpy (date, cp, 24);
143 if ((cp = index (cp + 1, 'r')) == NULL) {
144 if ((cp = rindex (fromwhom, '!')) != NULL) {
147 if ((p = rindex (fromwhom, '!')) != NULL)
148 (void) strcpy (origsys, p + 1);
150 (void) strcpy (origsys, fromwhom);
151 (void) strcat (from, fromwhom);
152 (void) strcat (from, "!");
153 (void) strcpy (fromwhom, cp + 1);
156 (void) strcpy (sys, SystemName ());
157 (void) strcat (from, sys);
158 (void) strcpy (origsys, sys);
159 (void) strcat (from, "!");
162 if (strncmp (cp, "remote from ", 12) == 0)
166 (void) sscanf (cp, "remote from %s", sys);
167 (void) strcat (from, sys);
168 (void) strcpy (origsys, sys);
169 (void) strcat (from, "!");
172 if (fromwhom[0] == NULL)
173 adios (NULLCP, "no from line");
177 (void) strcpy (origpath, from);
178 (void) strcat (from, fromwhom);
179 get_mmdf_addr (from, usrfrm);
180 if ((cp = rindex (usrfrm, '<')) != NULL) {
181 (void) strcpy (usrfrm, ++cp); /* sigh */
182 if ((cp = rindex (usrfrm, '>')) != NULL)
185 if (usrfrm[0] == NULL)
186 (void) sprintf (usrfrm, "%s!%s%%%s@%s%c",
187 SystemName (), from, UucpChan (), LocalName (), NULL);
189 fputs (linebuf, msgf);
190 if (txtcpy (stdin, msgf) == NOTOK)
191 fputs ("\n *** Problem during receipt from UUCP ***\n", msgf);
193 (void) freopen (Msgtmp, "r", msgf);
194 (void) freopen (Fromtmp, "r", fromf);
195 (void) unlink (Fromtmp);
199 for (argv++; --argc > 0;) {
203 if (deliver (*argv++) == NOTOK && !rtnflag)
207 (void) fflush (stderr);
208 (void) fflush (stdout);
215 (void) unlink (Msgtmp);
217 (void) unlink (mmdf);
218 (void) unlink (Errtmp);
232 switch (adrcheck (to)) {
234 if (mmdf[0] == NULL && filter () == NOTOK)
235 (void) strcpy (mmdf, Msgtmp);
236 replyval = xpost (to, mmdf);
240 if ((replyval = xuucp (to)) == NOTOK)
243 if ((replyval = txtcpy (fromf, pipef)) != NOTOK)
244 replyval = txtcpy (msgf, pipef);
245 i = (pclose (pipef) >> 8) & 0xff;
246 if (replyval != NOTOK)
247 replyval = (i != 0 ? NOTOK : OK);
254 switch (adrcheck (from)) {
257 (void) strcpy (tmpfil, "/tmp/rmailXXXXXX");
258 (void) unlink (mktemp (tmpfil));
259 if ((pipef = fdopen (creat (tmpfil, Tmpmode), "w")) == NULL)
262 fprintf (pipef, "Date: %s\nFrom: %s\n",
263 dtimenow (), Mailsys);
264 fprintf (pipef, "To: %s\ncc: %s\n", from, Overseer);
266 (void) fclose (pipef);
268 replyval = xpost (from, tmpfil);
269 (void) unlink (tmpfil);
273 if ((replyval = xuucp (from)) == NOTOK)
276 fprintf (pipef, "To: %s\ncc: %s\n", from, Overseer);
278 i = (pclose (pipef) >> 8) & 0xff;
279 if (replyval != NOTOK)
280 replyval = (i != 0 ? NOTOK : OK);
284 (void) strcpy (tmpfil, "/tmp/rmailXXXXXX");
285 (void) unlink (mktemp (tmpfil));
286 if ((pipef = fdopen (creat (tmpfil, Tmpmode), "w")) == NULL)
289 fprintf (pipef, "Date: %s\nFrom: %s\n",
290 dtimenow (), Mailsys);
291 fprintf (pipef, "To: %s\ncc: %s\n", usrfrm, Overseer);
293 (void) fclose (pipef);
295 replyval = xpost (Overseer, tmpfil);
296 (void) unlink (tmpfil);
314 if ((cp = getname (adr)) == NULL)
316 mp = getm (cp, NULLCP, 0, AD_HOST, NULLCP);
323 (void) strcpy (host, mp -> m_host);
325 if (mp -> m_mbox == NULL)
333 return (strcmp (host, SystemName ()) ? UUCP : ADDROK);
336 if (lookup (origsys, Okhosts) == OK)
338 return (okhost (host) == NOTOK ? RETURN : ADDROK);
347 return (lookup (origsys, Okhosts) == OK
348 || lookup (host, Okhosts) == OK
349 || lookup (host, Okdests) == OK ? OK : NOTOK);
361 if ((lookf = fopen (where, "r")) == NULL)
363 while (fgets (entry, sizeof entry, lookf) != NULL) {
365 while (*cp != '\n' && *cp != ' ' && *cp != '\t')
368 if (uleq (what, entry)) {
369 (void) fclose (lookf);
373 (void) fclose (lookf);
386 fprintf (pipef, "Subject: Illegal Address (%s)\n\n", badadr);
387 for (i = 0; rtnmessage[i]; i++)
388 fputs (rtnmessage[i], pipef);
389 fputs (rtnbegin, pipef);
392 (void) txtcpy (fromf, pipef);
394 (void) txtcpy (msgf, pipef);
396 fputs (rtnend, pipef);
407 && (nread = fread (buffer, sizeof (*buffer), BUFSIZ, frm)) > 0)
408 (void) fwrite (buffer, sizeof (*buffer), nread, to);
410 return (ferror (frm) ? NOTOK : OK);
422 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
429 execlp (postproc, r1bindex (postproc, '/'),
430 "-deliver", addr, file, NULLCP);
431 fprintf (stderr, "unable to exec ");
436 return (pidwait (child_id, OK) ? NOTOK : OK);
449 (void) strcpy (buffer, to);
450 if (cp = index (buffer, '!'))
453 fprintf (stderr, "internal error -- %s has no host\n", to);
456 (void) sprintf (cmdstr, "uux -p %s!rmail \\(%s\\)", buffer, cp);
458 if ((pipef = popen (cmdstr, "w")) == NULL)
461 (void) signal (SIGPIPE, pipeser);
473 static int pipeser (i)
477 (void) signal (i, SIG_IGN);
491 (void) strcpy (tmpfil, "/tmp/rmailXXXXXX");
492 (void) unlink (mktemp (tmpfil));
493 if ((pipef = fdopen (creat (tmpfil, Tmpmode), "w")) == NULL)
496 fprintf (pipef, "Date: %s\nFrom: %s\n", dtimenow (), Mailsys);
497 fprintf (pipef, "To: %s\n", usrfrm);
498 fprintf (pipef, "\nProblems sending mail:\n\n");
500 if ((fp = fopen (Errtmp, "r")) != NULL) {
501 while (fgets (buffer, sizeof buffer, fp) != NULL) {
504 fputs (buffer, pipef);
509 fprintf (pipef, "\tunknown problem\n");
510 for (i = 0; oopsmessage[i]; i++)
511 fprintf (pipef, oopsmessage[i], Overseer);
512 fputs (rtnbegin, pipef);
515 (void) txtcpy (fromf, pipef);
517 (void) txtcpy (msgf, pipef);
519 fputs (rtnend, pipef);
520 (void) fclose (pipef);
522 (void) xpost (usrfrm, tmpfil);
523 (void) unlink (tmpfil);
534 (void) strcpy (tmpfil, "/tmp/rmailXXXXXX");
535 (void) unlink (mktemp (tmpfil));
536 if ((pipef = fdopen (creat (tmpfil, Tmpmode), "w")) == NULL)
539 fprintf (pipef, "Date: %s\nFrom: %s\n", dtimenow (), Mailsys);
540 fprintf (pipef, "To: %s\n", Mailsys);
541 fprintf (pipef, "\nProblems sending mail for %s (aka %s):\n\n",
545 if ((fp = fopen (Errtmp, "r")) != NULL) {
546 while (fgets (buffer, sizeof buffer, fp) != NULL) {
549 fputs (buffer, pipef);
554 fprintf (pipef, "\tunknown problem\n");
557 fprintf (pipef, "\tunable to open %s\n", Errtmp);
558 (void) fclose (pipef);
560 (void) xpost (Mailsys, tmpfil);
561 (void) unlink (tmpfil);
574 (void) strcpy (tmpfil, "/tmp/rmailXXXXXX");
575 (void) unlink (mktemp (tmpfil));
576 if ((fd = creat (tmpfil, Tmpmode)) == NOTOK)
579 if ((fd = open (tmpfil, 2)) == NOTOK)
581 if ((out = fdopen (fd, "w")) == NULL) {
585 if ((td = dup (fd)) == NOTOK) {
590 fprintf (out, "From %s %s\n", from, date);
591 if (txtcpy (msgf, out) == NOTOK) {
597 (void) lseek (td, (off_t)0, 0);
599 (void) strcpy (mmdfil, "/tmp/mmdfXXXXXX");
600 (void) unlink (mktemp (mmdfil));
601 if ((fd = creat (mmdfil, Tmpmode)) == NOTOK) {
603 (void) unlink (tmpfil);
606 if ((fd = open (mmdfil, 2)) == NOTOK) {
608 (void) unlink (tmpfil);
614 switch (i = uucp2mmdf (td, fd, TRUE)) {
616 (void) strcpy (mmdf, mmdfil);
624 (void) unlink (tmpfil);
627 return (i != OK ? NOTOK : OK);
632 get_mmdf_addr (addr, to)
639 if ((adrxp = seekadrx (addr)) == NULL)
642 addr_convert (adrxp, to, TRUE);
643 while (seekadrx (NULLCP))