3 * smtp.c -- nmh SMTP interface
10 #include <zotnet/mts/mts.h>
12 #include <h/signals.h>
18 * This module implements an interface to SendMail very similar
19 * to the MMDF mm_(3) routines. The sm_() routines herein talk
20 * SMTP to a sendmail process, mapping SMTP reply codes into
25 * On older 4.2BSD machines without the POSIX function `sigaction',
26 * the alarm handing stuff for time-outs will NOT work due to the way
27 * syscalls get restarted. This is not really crucial, since SendMail
28 * is generally well-behaved in this area.
33 * It appears that some versions of Sendmail will return Code 451
34 * when they don't really want to indicate a failure.
35 * "Code 451 almost always means sendmail has deferred; we don't
36 * really want bomb out at this point since sendmail will rectify
37 * things later." So, if you define SENDMAILBUG, Code 451 is
38 * considered the same as Code 250. Yuck!
45 #define NBITS ((sizeof (int)) * 8)
48 * these codes must all be different!
50 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
61 static int sm_addrs = 0;
62 static int sm_alarmed = 0;
63 static int sm_child = NOTOK;
64 static int sm_debug = 0;
65 static int sm_nl = TRUE;
66 static int sm_verbose = 0;
68 static FILE *sm_rfp = NULL;
69 static FILE *sm_wfp = NULL;
72 static int sm_ispool = 0;
73 static char sm_tmpfil[BUFSIZ];
76 static char *sm_noreply = "No reply text given";
77 static char *sm_moreply = "; ";
79 struct smtp sm_reply; /* global... */
84 char *EHLOkeys[MAXEHLO + 1];
89 static int smtp_init (char *, char *, int, int, int, int, int);
90 static int sendmail_init (char *, char *, int, int, int, int, int);
92 static int rclient (char *, char *, char *);
93 static int sm_ierror (char *fmt, ...);
94 static int smtalk (int time, char *fmt, ...);
95 static int sm_wrecord (char *, int);
96 static int sm_wstream (char *, int);
97 static int sm_werror (void);
98 static int smhear (void);
99 static int sm_rrecord (char *, int *);
100 static int sm_rerror (void);
101 static RETSIGTYPE alrmser (int);
102 static char *EHLOset (char *);
106 * smtp.c's own static copy of several nmh library subroutines
108 static char **smail_brkstring (char *, char *, char *);
109 static int smail_brkany (char, char *);
110 char **smail_copyip (char **, char **, int);
113 /* from zotnet/mts/client.c */
114 int client (char *, char *, char *, int, char *, int);
117 sm_init (char *client, char *server, int watch, int verbose,
118 int debug, int onex, int queued)
120 if (sm_mts == MTS_SMTP)
121 return smtp_init (client, server, watch, verbose,
122 debug, onex, queued);
124 return sendmail_init (client, server, watch, verbose,
125 debug, onex, queued);
129 smtp_init (char *client, char *server, int watch, int verbose,
130 int debug, int onex, int queued)
132 int result, sd1, sd2;
137 sm_verbose = verbose;
145 if (sm_rfp != NULL && sm_wfp != NULL)
148 if (client == NULL || *client == '\0') {
152 client = LocalName(); /* no clientname -> LocalName */
157 if (client == NULL || *client == '\0')
158 client = "localhost";
161 if ((sd1 = rclient (server, "tcp", "smtp")) == NOTOK)
172 if ((sm_wfp = fdopen (sd1, "w")) == NULL) {
175 return sm_ierror ("unable to fdopen");
178 sm_reply.text[sm_reply.length = 0] = NULL;
179 return (sm_reply.code = RP_OK);
183 if ((sd2 = dup (sd1)) == NOTOK) {
185 return sm_ierror ("unable to dup");
188 SIGNAL (SIGALRM, alrmser);
189 SIGNAL (SIGPIPE, SIG_IGN);
191 if ((sm_rfp = fdopen (sd1, "r")) == NULL
192 || (sm_wfp = fdopen (sd2, "w")) == NULL) {
195 sm_rfp = sm_wfp = NULL;
196 return sm_ierror ("unable to fdopen");
214 * Give EHLO or HELO command
216 if (client && *client) {
218 result = smtalk (SM_HELO, "EHLO %s", client);
221 if (result >= 500 && result <= 599)
222 result = smtalk (SM_HELO, "HELO %s", client);
231 if (watch && EHLOset ("XVRB"))
232 smtalk (SM_HELO, "VERB on");
233 if (onex && EHLOset ("XONE"))
234 smtalk (SM_HELO, "ONEX");
235 if (queued && EHLOset ("XQUE"))
236 smtalk (SM_HELO, "QUED");
242 sendmail_init (char *client, char *server, int watch, int verbose,
243 int debug, int onex, int queued)
252 sm_verbose = verbose;
254 if (sm_rfp != NULL && sm_wfp != NULL)
257 if (client == NULL || *client == '\0') {
261 client = LocalName(); /* no clientname -> LocalName */
265 if (client == NULL || *client == '\0')
266 client = "localhost";
269 if (pipe (pdi) == NOTOK)
270 return sm_ierror ("no pipes");
271 if (pipe (pdo) == NOTOK) {
274 return sm_ierror ("no pipes");
277 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
286 return sm_ierror ("unable to fork");
289 if (pdo[0] != fileno (stdin))
290 dup2 (pdo[0], fileno (stdin));
291 if (pdi[1] != fileno (stdout))
292 dup2 (pdi[1], fileno (stdout));
293 if (pdi[1] != fileno (stderr))
294 dup2 (pdi[1], fileno (stderr));
295 for (i = fileno (stderr) + 1; i < NBITS; i++)
299 vec[vecp++] = r1bindex (sendmail, '/');
302 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
303 vec[vecp++] = "-oem";
308 # endif /* not RAND */
309 #endif /* not ZMAILER */
314 execvp (sendmail, vec);
315 fprintf (stderr, "unable to exec ");
317 _exit (-1); /* NOTREACHED */
320 SIGNAL (SIGALRM, alrmser);
321 SIGNAL (SIGPIPE, SIG_IGN);
325 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
326 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
329 sm_rfp = sm_wfp = NULL;
330 return sm_ierror ("unable to fdopen");
345 if (client && *client) {
347 result = smtalk (SM_HELO, "EHLO %s", client);
350 if (500 <= result && result <= 599)
351 result = smtalk (SM_HELO, "HELO %s", client);
365 smtalk (SM_HELO, "ONEX");
368 smtalk (SM_HELO, "VERB on");
375 # define MAXARGS 1000
379 rclient (char *server, char *protocol, char *service)
382 char response[BUFSIZ];
387 if ((sd = client (server, protocol, service, FALSE, response, sizeof(response))) != NOTOK)
391 if (!server && servers && (cp = strchr(servers, '/'))) {
393 char *arguments[MAXARGS];
395 smail_copyip (smail_brkstring (cp = getcpy (servers), " ", "\n"), arguments, MAXARGS);
397 for (ap = arguments; *ap; ap++)
401 if ((dp = strrchr(*ap, '/')) && *++dp == NULL)
403 snprintf (sm_tmpfil, sizeof(sm_tmpfil), "%s/smtpXXXXXX", *ap);
405 sd = mkstemp (sm_tmpfil);
409 if ((sd = creat (sm_tmpfil, 0600)) != NOTOK) {
422 sm_ierror ("%s", response);
428 sm_winit (int mode, char *from)
433 if (sm_ispool && !sm_wfp) {
434 strlen (strcpy (sm_reply.text, "unable to create new spool file"));
435 sm_reply.code = NOTOK;
458 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
475 sm_wadr (char *mbox, char *host, char *path)
477 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
479 path ? path : "", mbox, host)) {
489 #endif /* SENDMAILBUG */
514 switch (smtalk (SM_DATA, "DATA")) {
523 #endif /* SENDMAILBUG */
540 sm_wtxt (char *buffer, int len)
546 result = sm_wstream (buffer, len);
549 return (result == NOTOK ? RP_BHST : RP_OK);
556 if (sm_wstream ((char *) NULL, 0) == NOTOK)
559 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
567 #endif /* SENDMAILBUG */
585 if (sm_mts == MTS_SENDMAIL) {
596 if (sm_rfp == NULL && sm_wfp == NULL)
601 smtalk (SM_QUIT, "QUIT");
605 sm_note.code = sm_reply.code;
606 strncpy (sm_note.text, sm_reply.text, sm_note.length = sm_reply.length);/* fall */
608 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
610 if (sm_mts == MTS_SMTP)
611 smtalk (SM_QUIT, "QUIT");
613 kill (sm_child, SIGKILL);
618 sm_reply.code = sm_note.code;
619 strncpy (sm_reply.text, sm_note.text, sm_reply.length = sm_note.length);
636 if (sm_rfp != NULL) {
641 if (sm_wfp != NULL) {
647 if (sm_mts == MTS_SMTP)
650 status = pidwait (sm_child, OK);
654 sm_rfp = sm_wfp = NULL;
655 return (status ? RP_BHST : RP_OK);
664 int cc, i, j, k, result;
666 char *dp, *bp, *cp, s;
667 char buffer[BUFSIZ], sender[BUFSIZ];
671 k = strlen (file) - sizeof(".bulk");
672 if ((fp = fopen (file, "r")) == NULL) {
675 snprintf (sm_reply.text, sizeof(sm_reply.text),
676 "unable to read %s: ", file);
680 if ((s = strerror (errno)))
681 strncpy (bp, s, sizeof(sm_reply.text) - len);
683 snprintf (bp, sizeof(sm_reply.text) - len, "Error %d", errno);
684 sm_reply.length = strlen (sm_reply.text);
685 sm_reply.code = NOTOK;
689 printf ("reading file %s\n", file);
694 while (fgets (buffer, sizeof(buffer), fp)) {
696 strncpy (sender, buffer + sizeof("MAIL FROM:") - 1, sizeof(sender));
697 if (strcmp (buffer, "DATA\r\n") == 0) {
704 printf ("no DATA...\n");
708 snprintf (buffer, sizeof(buffer), "%s.bad", file);
709 rename (file, buffer);
711 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
720 printf ("no %srecipients...\n", j < 1 ? "sender or " : "");
726 if ((cp = malloc ((size_t) (cc = (pos = ftell (fp)) + 1))) == NULL) {
727 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
729 sm_reply.code = NOTOK;
733 fseek (fp, 0L, SEEK_SET);
734 for (dp = cp, i = 0; i++ < j; dp += strlen (dp))
735 if (fgets (dp, cc - (dp - cp), fp) == NULL) {
736 sm_reply.length = strlen (strcpy (sm_reply.text, "premature eof"));
743 for (dp = cp, i = cc - 1; i > 0; dp += cc, i -= cc)
744 if ((cc = write (fileno (sm_wfp), dp, i)) == NOTOK) {
747 strcpy (sm_reply.text, "error writing to server: ",
748 sizeof(sm_reply.text));
752 if ((s = strerror (errno)))
753 strncpy (bp, s, sizeof(sm_reply.text) - len);
755 snprintf (bp, sizeof(sm_reply.text) - len,
756 "unknown error %d", errno);
757 sm_reply.length = strlen (sm_reply.text);
762 printf ("wrote %d octets to server\n", cc);
766 for (dp = cp, i = 0; i++ < j; dp = strchr(dp, '\n'), dp++) {
768 if (bp = strchr(dp, '\r'))
770 printf ("=> %s\n", dp);
776 switch (smhear () + (i == 1 ? 1000 : i != j ? 2000 : 3000)) {
788 smtalk (SM_RSET, "RSET");
816 if (k <= 0 || strcmp (sender, "<>\r\n") == 0)
820 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
821 if ((gp = fopen (buffer, "w+")) == NULL)
823 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
826 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
827 l - 4, l - 4, sender + 1, file);
828 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
829 dtimenow (0), LocalName ());
831 if (bp = strchr(dp, '\r'))
833 fprintf (gp, "=> %s\r\n", dp);
836 fprintf (gp, "<= %s\r\n", rp_string (result));
854 smtalk (SM_RSET, "RSET");
857 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
868 smtalk (SM_RSET, "RSET");
880 #ifdef HAVE_ST_BLKSIZE
883 if (fstat (fileno (sm_wfp), &st) == NOTOK || (cc = st.st_blksize) < BUFSIZ)
888 if ((cp = malloc ((size_t) cc)) == NULL) {
889 smtalk (SM_RSET, "RSET");
890 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
895 fseek (fp, pos, SEEK_SET);
899 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
900 if ((j = fread (cp, sizeof(*cp), i, fp)) == OK) {
904 snprintf (sm_reply.text, sizeof(sm_reply.text),
905 "error reading %s: ", file);
909 if ((s = strerror (errno)))
910 strncpy (bp, s, sizeof(sm_reply.text) - len);
912 snprintf (bp, sizeof(sm_reply.text) - len,
913 "unknown error %d", errno);
914 sm_reply.length = strlen (sm_reply.text);
922 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
923 if ((j = write (fileno (sm_wfp), dp, i)) == NOTOK)
927 printf ("wrote %d octets to server\n", j);
954 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
965 if (k <= 0 || strcmp (sender, "<>\r\n") == 0) {
971 ftruncate (fileno (gp), 0L);
972 fseek (gp, 0L, SEEK_SET);
975 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
976 if ((gp = fopen (buffer, "w")) == NULL)
979 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
981 fprintf (gp, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
982 i - 4, i - 4, sender + 1, file);
983 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
984 dtimenow (0), LocalName ());
989 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp);
990 fseek (fp, pos, SEEK_SET);
991 while (fgets (buffer, sizeof(buffer), fp)) {
992 if (buffer[0] == '-')
994 if (strcmp (buffer, ".\r\n"))
997 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp);
1011 sm_ierror (char *fmt, ...)
1016 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
1019 sm_reply.length = strlen (sm_reply.text);
1020 sm_reply.code = NOTOK;
1027 smtalk (int time, char *fmt, ...)
1031 char buffer[BUFSIZ];
1034 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1038 printf ("=> %s\n", buffer);
1046 if (strcmp (buffer, ".") == 0)
1048 fprintf (sm_wfp, "%s\r\n", buffer);
1052 if (ferror (sm_wfp))
1053 return sm_werror ();
1054 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
1055 (char) (sm_ispool + 'a' - 1));
1056 if (rename (sm_tmpfil, file) == NOTOK) {
1060 snprintf (sm_reply.text, sizeof(sm_reply.text),
1061 "error renaming %s to %s: ", sm_tmpfil, file);
1065 if ((s = strerror (errno)))
1066 strncpy (bp, s, sizeof(sm_reply.text) - len);
1068 snprintf (bp, sizeof(sm_reply.text) - len,
1069 "unknown error %d", errno);
1070 sm_reply.length = strlen (sm_reply.text);
1071 sm_reply.code = NOTOK;
1075 if (sm_wfp = fopen (sm_tmpfil, "w"))
1076 chmod (sm_tmpfil, 0600);
1087 ftruncate (fileno (sm_wfp), 0L);
1088 fseek (sm_wfp, 0L, SEEK_SET);
1107 printf ("<= %d\n", result);
1111 sm_reply.text[sm_reply.length = 0] = NULL;
1112 return (sm_reply.code = result);
1117 alarm ((unsigned) time);
1118 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1127 * write the buffer to the open SMTP channel
1131 sm_wrecord (char *buffer, int len)
1134 return sm_werror ();
1136 fwrite (buffer, sizeof(*buffer), len, sm_wfp);
1137 fputs ("\r\n", sm_wfp);
1140 return (ferror (sm_wfp) ? sm_werror () : OK);
1145 sm_wstream (char *buffer, int len)
1148 static char lc = '\0';
1151 return sm_werror ();
1153 if (buffer == NULL && len == 0) {
1155 fputs ("\r\n", sm_wfp);
1157 return (ferror (sm_wfp) ? sm_werror () : OK);
1160 for (bp = buffer; len > 0; bp++, len--) {
1164 fputc ('\r', sm_wfp);
1169 fputc ('.', sm_wfp);/* FALL THROUGH */
1173 fputc (*bp, sm_wfp);
1174 if (ferror (sm_wfp))
1175 return sm_werror ();
1180 return (ferror (sm_wfp) ? sm_werror () : OK);
1185 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1186 * causes compile problems with the #ifdef MPOP in the middle. Should the
1187 * #ifdef MPOP be removed, remove these #undefs.
1200 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1201 : sm_alarmed ? "write to socket timed out"
1203 : sm_ispool ? "error writing to spool file"
1205 : "error writing to socket"));
1207 return (sm_reply.code = NOTOK);
1214 int i, code, cont, bc, rc, more;
1216 char **ehlo, buffer[BUFSIZ];
1219 static int at_least_once = 0;
1221 if (at_least_once) {
1224 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1238 sm_reply.length = 0;
1239 sm_reply.text[0] = 0;
1241 rc = sizeof(sm_reply.text) - 1;
1243 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
1245 printf ("<= %s\n", buffer);
1250 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1251 && (buffer[3] == '-' || doingEHLO == 2)
1253 if (doingEHLO == 2) {
1254 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1255 strcpy (*ehlo++, buffer + 4);
1257 if (ehlo >= EHLOkeys + MAXEHLO)
1267 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1273 for (; bc > 0 && isspace (*bp); bp++, bc--)
1275 if (bc > 0 && *bp == '-') {
1278 for (; bc > 0 && isspace (*bp); bp++, bc--)
1283 if (code != sm_reply.code || cont)
1287 sm_reply.code = code;
1290 strncpy (buffer, sm_noreply, sizeof(buffer));
1292 bc = strlen (sm_noreply);
1296 if ((i = min (bc, rc)) > 0) {
1297 strncpy (rp, bp, i);
1300 if (more && rc > strlen (sm_moreply) + 1) {
1301 strncpy (sm_reply.text + rc, sm_moreply, sizeof(sm_reply.text) - rc);
1302 rc += strlen (sm_moreply);
1307 if (sm_reply.code < 100) {
1309 printf ("%s\n", sm_reply.text);
1315 sm_reply.length = rp - sm_reply.text;
1316 return sm_reply.code;
1323 sm_rrecord (char *buffer, int *len)
1326 return sm_rerror ();
1328 buffer[*len = 0] = 0;
1330 fgets (buffer, BUFSIZ, sm_rfp);
1331 *len = strlen (buffer);
1332 if (ferror (sm_rfp) || feof (sm_rfp))
1333 return sm_rerror ();
1334 if (buffer[*len - 1] != '\n')
1335 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
1338 if (buffer[*len - 2] == '\r')
1340 buffer[*len - 1] = 0;
1349 if (sm_mts == MTS_SMTP)
1351 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1352 : sm_alarmed ? "read from socket timed out"
1353 : feof (sm_rfp) ? "premature end-of-file on socket"
1354 : "error reading from socket"));
1357 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1358 : sm_alarmed ? "read from pipe timed out"
1359 : feof (sm_rfp) ? "premature end-of-file on pipe"
1360 : "error reading from pipe"));
1362 return (sm_reply.code = NOTOK);
1369 #ifndef RELIABLE_SIGNALS
1370 SIGNAL (SIGALRM, alrmser);
1375 printf ("timed out...\n");
1382 rp_string (int code)
1385 static char buffer[BUFSIZ];
1387 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1407 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1427 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1428 text, sm_reply.code, sm_reply.text);
1435 static char *broken[MAXARGS + 1];
1438 smail_brkstring (char *strg, char *brksep, char *brkterm)
1445 for (bi = 0; bi < MAXARGS; bi++) {
1446 while (smail_brkany (c = *sp, brksep))
1448 if (!c || smail_brkany (c, brkterm)) {
1455 while ((c = *++sp) && !smail_brkany (c, brksep) && !smail_brkany (c, brkterm))
1458 broken[MAXARGS] = 0;
1465 * returns 1 if chr in strg, 0 otherwise
1468 smail_brkany (char chr, char *strg)
1473 for (sp = strg; *sp; sp++)
1480 * copy a string array and return pointer to end
1483 smail_copyip (char **p, char **q, int len_q)
1485 while (*p && --len_q > 0)
1504 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1506 if (strncmp (ep, s, len) == 0) {
1507 for (ep += len; *ep == ' '; ep++)