3 * sendmail.c -- nmh sendmail interface
9 #include <mts/smtp/smtp.h>
10 #include <zotnet/mts/mts.h>
17 * This module implements an interface to SendMail very similar
18 * to the MMDF mm_(3) routines. The sm_() routines herein talk
19 * SMTP to a sendmail process, mapping SMTP reply codes into
24 * On older 4.2BSD machines without the POSIX function `sigaction',
25 * the alarm handing stuff for time-outs will NOT work due to the way
26 * syscalls get restarted. This is not really crucial, since SendMail
27 * is generally well-behaved in this area.
32 * It appears that some versions of Sendmail will return Code 451
33 * when they don't really want to indicate a failure.
34 * "Code 451 almost always means sendmail has deferred; we don't
35 * really want bomb out at this point since sendmail will rectify
36 * things later." So, if you define SENDMAILBUG, Code 451 is
37 * considered the same as Code 250. Yuck!
44 #define NBITS ((sizeof (int)) * 8)
47 * these codes must all be different!
49 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
60 static int sm_addrs = 0;
61 static int sm_alarmed = 0;
62 static int sm_child = NOTOK;
63 static int sm_debug = 0;
64 static int sm_nl = TRUE;
65 static int sm_verbose = 0;
67 static FILE *sm_rfp = NULL;
68 static FILE *sm_wfp = NULL;
71 static int sm_ispool = 0;
72 static char sm_tmpfil[BUFSIZ];
75 static char *sm_noreply = "No reply text given";
76 static char *sm_moreply = "; ";
78 struct smtp sm_reply; /* global... */
83 char *EHLOkeys[MAXEHLO + 1];
88 static int sm_ierror (char *fmt, ...);
89 static int smtalk (int time, char *fmt, ...);
90 static int sm_wrecord (char *, int);
91 static int sm_wstream (char *, int);
92 static int sm_werror (void);
93 static int smhear (void);
94 static int sm_rrecord (char *, int *);
95 static int sm_rerror (void);
96 static RETSIGTYPE alrmser (int);
100 sm_init (char *client, char *server, int watch, int verbose,
101 int debug, int onex, int queued)
110 sm_verbose = verbose;
112 if (sm_rfp != NULL && sm_wfp != NULL)
115 if (client == NULL || *client == '\0')
119 client = LocalName(); /* no clientname -> LocalName */
122 if (client == NULL || *client == '\0')
123 client = "localhost";
126 if (pipe (pdi) == NOTOK)
127 return sm_ierror ("no pipes");
128 if (pipe (pdo) == NOTOK) {
131 return sm_ierror ("no pipes");
134 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
143 return sm_ierror ("unable to fork");
146 if (pdo[0] != fileno (stdin))
147 dup2 (pdo[0], fileno (stdin));
148 if (pdi[1] != fileno (stdout))
149 dup2 (pdi[1], fileno (stdout));
150 if (pdi[1] != fileno (stderr))
151 dup2 (pdi[1], fileno (stderr));
152 for (i = fileno (stderr) + 1; i < NBITS; i++)
156 vec[vecp++] = r1bindex (sendmail, '/');
159 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
160 vec[vecp++] = "-oem";
165 # endif /* not RAND */
166 #endif /* not ZMAILER */
171 execvp (sendmail, vec);
172 fprintf (stderr, "unable to exec ");
174 _exit (-1); /* NOTREACHED */
177 SIGNAL (SIGALRM, alrmser);
178 SIGNAL (SIGPIPE, SIG_IGN);
182 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
183 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
186 sm_rfp = sm_wfp = NULL;
187 return sm_ierror ("unable to fdopen");
202 if (client && *client) {
204 result = smtalk (SM_HELO, "EHLO %s", client);
207 if (500 <= result && result <= 599)
208 result = smtalk (SM_HELO, "HELO %s", client);
222 smtalk (SM_HELO, "ONEX");
225 smtalk (SM_HELO, "VERB on");
233 sm_winit (int mode, char *from)
236 if (sm_ispool && !sm_wfp) {
237 strlen (strcpy (sm_reply.text, "unable to create new spool file"));
238 sm_reply.code = NOTOK;
243 switch (smtalk (SM_MAIL, "%s FROM:<%s>",
244 mode == S_SEND ? "SEND" : mode == S_SOML ? "SOML"
245 : mode == S_SAML ? "SAML" : "MAIL", from)) {
262 sm_wadr (char *mbox, char *host, char *path)
264 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
266 path ? path : "", mbox, host)) {
276 #endif /* SENDMAILBUG */
301 switch (smtalk (SM_DATA, "DATA")) {
310 #endif /* SENDMAILBUG */
327 sm_wtxt (char *buffer, int len)
333 result = sm_wstream (buffer, len);
336 return (result == NOTOK ? RP_BHST : RP_OK);
343 if (sm_wstream ((char *) NULL, 0) == NOTOK)
346 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
354 #endif /* SENDMAILBUG */
381 if (sm_rfp == NULL && sm_wfp == NULL)
386 smtalk (SM_QUIT, "QUIT");
390 sm_note.code = sm_reply.code;
391 strncpy (sm_note.text, sm_reply.text, sm_note.length = sm_reply.length);/* fall */
393 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
395 kill (sm_child, SIGKILL);
399 sm_reply.code = sm_note.code;
400 strncpy (sm_reply.text, sm_note.text, sm_reply.length = sm_note.length);
404 if (sm_rfp != NULL) {
409 if (sm_wfp != NULL) {
415 status = pidwait (sm_child, OK);
418 sm_rfp = sm_wfp = NULL;
420 return (status ? RP_BHST : RP_OK);
425 sm_ierror (char *fmt, ...)
430 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
433 sm_reply.length = strlen (sm_reply.text);
434 sm_reply.code = NOTOK;
441 smtalk (int time, char *fmt, ...)
448 vsnprintf (buffer, sizeof(buffer), fmt, ap);
452 printf ("=> %s\n", buffer);
460 if (strcmp (buffer, ".") == 0)
462 fprintf (sm_wfp, "%s\r\n", buffer);
468 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
469 (char) (sm_ispool + 'a' - 1));
470 if (rename (sm_tmpfil, file) == NOTOK) {
474 snprintf (sm_reply.text, sizeof(sm_reply.text),
475 "error renaming %s to %s: ", sm_tmpfil, file);
479 if ((s = strerror (errno)))
480 strncpy (bp, s, sizeof(sm_reply.text) - len);
482 snprintf (bp, sizeof(sm_reply.text) - len,
483 "unknown error %d", errno);
484 sm_reply.length = strlen (sm_reply.text);
485 sm_reply.code = NOTOK;
489 if (sm_wfp = fopen (sm_tmpfil, "w"))
490 chmod (sm_tmpfil, 0600);
501 ftruncate (fileno (sm_wfp), 0L);
502 fseek (sm_wfp, 0L, SEEK_SET);
521 printf ("<= %d\n", result);
525 sm_reply.text[sm_reply.length = 0] = NULL;
526 return (sm_reply.code = result);
531 alarm ((unsigned) time);
532 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
541 sm_wrecord (char *buffer, int len)
546 fwrite (buffer, sizeof *buffer, len, sm_wfp);
547 fputs ("\r\n", sm_wfp);
550 return (ferror (sm_wfp) ? sm_werror () : OK);
555 sm_wstream (char *buffer, int len)
563 if (buffer == NULL && len == 0) {
565 fputs ("\r\n", sm_wfp);
567 return (ferror (sm_wfp) ? sm_werror () : OK);
570 for (bp = buffer; len > 0; bp++, len--) {
574 fputc ('\r', sm_wfp);
579 fputc ('.', sm_wfp);/* FALL THROUGH */
590 return (ferror (sm_wfp) ? sm_werror () : OK);
596 * AIX by default will inline the strlen and strcpy commands by redefining
597 * them as __strlen and __strcpy respectively. This causes compile problems
598 * with the #ifdef MPOP in the middle. Should the #ifdef MPOP be removed,
599 * remove these #undefs.
609 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no pipe opened"
610 : sm_alarmed ? "write to pipe timed out"
611 : "error writing to pipe"));
613 return (sm_reply.code = NOTOK);
620 int i, code, cont, bc, rc, more;
622 char **ehlo, buffer[BUFSIZ];
625 static int at_least_once = 0;
628 for (ehlo = EHLOkeys; *ehlo; ehlo++)
634 *(ehlo = EHLOkeys) = NULL;
639 sm_reply.text[sm_reply.length = 0] = 0;
642 rc = sizeof(sm_reply.text) - 1;
644 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
646 printf ("<= %s\n", buffer);
651 && strncmp (buffer, "250", sizeof("250") - 1) == 0
652 && (buffer[3] == '-' || doingEHLO == 2)
654 if (doingEHLO == 2) {
655 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
656 strcpy (*ehlo++, buffer + 4);
658 if (ehlo >= EHLOkeys + MAXEHLO)
668 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
674 for (; bc > 0 && isspace (*bp); bp++, bc--)
676 if (bc > 0 && *bp == '-') {
679 for (; bc > 0 && isspace (*bp); bp++, bc--)
684 if (code != sm_reply.code || cont)
688 sm_reply.code = code;
691 strncpy (buffer, sm_noreply, sizeof(buffer));
693 bc = strlen (sm_noreply);
696 if ((i = min (bc, rc)) > 0) {
700 if (more && rc > strlen (sm_moreply) + 1) {
701 strncpy (sm_reply.text + rc, sm_moreply, sizeof(sm_reply.text) - rc);
702 rc += strlen (sm_moreply);
707 if (sm_reply.code < 100) {
709 printf ("%s\n", sm_reply.text);
715 sm_reply.length = rp - sm_reply.text;
717 return sm_reply.code;
725 sm_rrecord (char *buffer, int *len)
730 buffer[*len = 0] = 0;
732 fgets (buffer, BUFSIZ, sm_rfp);
733 *len = strlen (buffer);
734 if (ferror (sm_rfp) || feof (sm_rfp))
736 if (buffer[*len - 1] != '\n')
737 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
740 if (buffer[*len - 2] == '\r')
742 buffer[*len - 1] = 0;
752 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
753 : sm_alarmed ? "read from pipe timed out"
754 : feof (sm_rfp) ? "premature end-of-file on pipe"
755 : "error reading from pipe"));
757 return (sm_reply.code = NOTOK);
764 #ifndef RELIABLE_SIGNALS
765 SIGNAL (SIGALRM, alrmser);
770 printf ("timed out...\n");
780 static char buffer[BUFSIZ];
782 switch (sm_reply.code != NOTOK ? code : NOTOK) {
802 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
822 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
823 text, sm_reply.code, sm_reply.text);