3 * sendmail.c -- nmh sendmail interface
9 #include <mts/smtp/smtp.h>
10 #include <zotnet/mts/mts.h>
12 #include "h/signals.h" /* for SIGNAL() */
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 sm_ierror (char *fmt, ...);
90 static int smtalk (int time, char *fmt, ...);
91 static int sm_wrecord (char *, int);
92 static int sm_wstream (char *, int);
93 static int sm_werror (void);
94 static int smhear (void);
95 static int sm_rrecord (char *, int *);
96 static int sm_rerror (void);
97 static RETSIGTYPE alrmser (int);
101 sm_init (char *client, char *server, int watch, int verbose,
102 int debug, int onex, int queued)
111 sm_verbose = verbose;
113 if (sm_rfp != NULL && sm_wfp != NULL)
116 if (client == NULL || *client == '\0') {
120 client = LocalName(); /* no clientname -> LocalName */
124 if (client == NULL || *client == '\0')
125 client = "localhost";
128 if (pipe (pdi) == NOTOK)
129 return sm_ierror ("no pipes");
130 if (pipe (pdo) == NOTOK) {
133 return sm_ierror ("no pipes");
136 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
145 return sm_ierror ("unable to fork");
148 if (pdo[0] != fileno (stdin))
149 dup2 (pdo[0], fileno (stdin));
150 if (pdi[1] != fileno (stdout))
151 dup2 (pdi[1], fileno (stdout));
152 if (pdi[1] != fileno (stderr))
153 dup2 (pdi[1], fileno (stderr));
154 for (i = fileno (stderr) + 1; i < NBITS; i++)
158 vec[vecp++] = r1bindex (sendmail, '/');
161 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
162 vec[vecp++] = "-oem";
167 # endif /* not RAND */
168 #endif /* not ZMAILER */
173 execvp (sendmail, vec);
174 fprintf (stderr, "unable to exec ");
176 _exit (-1); /* NOTREACHED */
179 SIGNAL (SIGALRM, alrmser);
180 SIGNAL (SIGPIPE, SIG_IGN);
184 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
185 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
188 sm_rfp = sm_wfp = NULL;
189 return sm_ierror ("unable to fdopen");
204 if (client && *client) {
206 result = smtalk (SM_HELO, "EHLO %s", client);
209 if (500 <= result && result <= 599)
210 result = smtalk (SM_HELO, "HELO %s", client);
224 smtalk (SM_HELO, "ONEX");
227 smtalk (SM_HELO, "VERB on");
235 sm_winit (int mode, char *from)
238 if (sm_ispool && !sm_wfp) {
239 strlen (strcpy (sm_reply.text, "unable to create new spool file"));
240 sm_reply.code = NOTOK;
245 switch (smtalk (SM_MAIL, "%s FROM:<%s>",
246 mode == S_SEND ? "SEND" : mode == S_SOML ? "SOML"
247 : mode == S_SAML ? "SAML" : "MAIL", from)) {
264 sm_wadr (char *mbox, char *host, char *path)
266 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
268 path ? path : "", mbox, host)) {
278 #endif /* SENDMAILBUG */
303 switch (smtalk (SM_DATA, "DATA")) {
312 #endif /* SENDMAILBUG */
329 sm_wtxt (char *buffer, int len)
335 result = sm_wstream (buffer, len);
338 return (result == NOTOK ? RP_BHST : RP_OK);
345 if (sm_wstream ((char *) NULL, 0) == NOTOK)
348 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
356 #endif /* SENDMAILBUG */
383 if (sm_rfp == NULL && sm_wfp == NULL)
388 smtalk (SM_QUIT, "QUIT");
392 sm_note.code = sm_reply.code;
393 strncpy (sm_note.text, sm_reply.text, sm_note.length = sm_reply.length);/* fall */
395 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
397 kill (sm_child, SIGKILL);
401 sm_reply.code = sm_note.code;
402 strncpy (sm_reply.text, sm_note.text, sm_reply.length = sm_note.length);
406 if (sm_rfp != NULL) {
411 if (sm_wfp != NULL) {
417 status = pidwait (sm_child, OK);
420 sm_rfp = sm_wfp = NULL;
422 return (status ? RP_BHST : RP_OK);
427 sm_ierror (char *fmt, ...)
432 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
435 sm_reply.length = strlen (sm_reply.text);
436 sm_reply.code = NOTOK;
443 smtalk (int time, char *fmt, ...)
450 vsnprintf (buffer, sizeof(buffer), fmt, ap);
454 printf ("=> %s\n", buffer);
462 if (strcmp (buffer, ".") == 0)
464 fprintf (sm_wfp, "%s\r\n", buffer);
470 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
471 (char) (sm_ispool + 'a' - 1));
472 if (rename (sm_tmpfil, file) == NOTOK) {
476 snprintf (sm_reply.text, sizeof(sm_reply.text),
477 "error renaming %s to %s: ", sm_tmpfil, file);
481 if ((s = strerror (errno)))
482 strncpy (bp, s, sizeof(sm_reply.text) - len);
484 snprintf (bp, sizeof(sm_reply.text) - len,
485 "unknown error %d", errno);
486 sm_reply.length = strlen (sm_reply.text);
487 sm_reply.code = NOTOK;
491 if (sm_wfp = fopen (sm_tmpfil, "w"))
492 chmod (sm_tmpfil, 0600);
503 ftruncate (fileno (sm_wfp), 0L);
504 fseek (sm_wfp, 0L, SEEK_SET);
523 printf ("<= %d\n", result);
527 sm_reply.text[sm_reply.length = 0] = NULL;
528 return (sm_reply.code = result);
533 alarm ((unsigned) time);
534 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
543 sm_wrecord (char *buffer, int len)
548 fwrite (buffer, sizeof *buffer, len, sm_wfp);
549 fputs ("\r\n", sm_wfp);
552 return (ferror (sm_wfp) ? sm_werror () : OK);
557 sm_wstream (char *buffer, int len)
565 if (buffer == NULL && len == 0) {
567 fputs ("\r\n", sm_wfp);
569 return (ferror (sm_wfp) ? sm_werror () : OK);
572 for (bp = buffer; len > 0; bp++, len--) {
576 fputc ('\r', sm_wfp);
581 fputc ('.', sm_wfp);/* FALL THROUGH */
592 return (ferror (sm_wfp) ? sm_werror () : OK);
598 * AIX by default will inline the strlen and strcpy commands by redefining
599 * them as __strlen and __strcpy respectively. This causes compile problems
600 * with the #ifdef MPOP in the middle. Should the #ifdef MPOP be removed,
601 * remove these #undefs.
611 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no pipe opened"
612 : sm_alarmed ? "write to pipe timed out"
613 : "error writing to pipe"));
615 return (sm_reply.code = NOTOK);
622 int i, code, cont, bc, rc, more;
624 char **ehlo, buffer[BUFSIZ];
627 static int at_least_once = 0;
630 for (ehlo = EHLOkeys; *ehlo; ehlo++)
636 *(ehlo = EHLOkeys) = NULL;
641 sm_reply.text[sm_reply.length = 0] = 0;
644 rc = sizeof(sm_reply.text) - 1;
646 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
648 printf ("<= %s\n", buffer);
653 && strncmp (buffer, "250", sizeof("250") - 1) == 0
654 && (buffer[3] == '-' || doingEHLO == 2)
656 if (doingEHLO == 2) {
657 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
658 strcpy (*ehlo++, buffer + 4);
660 if (ehlo >= EHLOkeys + MAXEHLO)
670 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
676 for (; bc > 0 && isspace (*bp); bp++, bc--)
678 if (bc > 0 && *bp == '-') {
681 for (; bc > 0 && isspace (*bp); bp++, bc--)
686 if (code != sm_reply.code || cont)
690 sm_reply.code = code;
693 strncpy (buffer, sm_noreply, sizeof(buffer));
695 bc = strlen (sm_noreply);
698 if ((i = min (bc, rc)) > 0) {
702 if (more && rc > strlen (sm_moreply) + 1) {
703 strncpy (sm_reply.text + rc, sm_moreply, sizeof(sm_reply.text) - rc);
704 rc += strlen (sm_moreply);
709 if (sm_reply.code < 100) {
711 printf ("%s\n", sm_reply.text);
717 sm_reply.length = rp - sm_reply.text;
719 return sm_reply.code;
727 sm_rrecord (char *buffer, int *len)
732 buffer[*len = 0] = 0;
734 fgets (buffer, BUFSIZ, sm_rfp);
735 *len = strlen (buffer);
736 if (ferror (sm_rfp) || feof (sm_rfp))
738 if (buffer[*len - 1] != '\n')
739 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
742 if (buffer[*len - 2] == '\r')
744 buffer[*len - 1] = 0;
754 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
755 : sm_alarmed ? "read from pipe timed out"
756 : feof (sm_rfp) ? "premature end-of-file on pipe"
757 : "error reading from pipe"));
759 return (sm_reply.code = NOTOK);
766 #ifndef RELIABLE_SIGNALS
767 SIGNAL (SIGALRM, alrmser);
772 printf ("timed out...\n");
782 static char buffer[BUFSIZ];
784 switch (sm_reply.code != NOTOK ? code : NOTOK) {
804 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
824 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
825 text, sm_reply.code, sm_reply.text);