]>
diplodocus.org Git - nmh/blob - mts/sendmail/sendmail.c
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 */
123 if (client
== NULL
|| *client
== '\0')
124 client
= "localhost";
127 if (pipe (pdi
) == NOTOK
)
128 return sm_ierror ("no pipes");
129 if (pipe (pdo
) == NOTOK
) {
132 return sm_ierror ("no pipes");
135 for (i
= 0; (sm_child
= fork ()) == NOTOK
&& i
< 5; i
++)
144 return sm_ierror ("unable to fork");
147 if (pdo
[0] != fileno (stdin
))
148 dup2 (pdo
[0], fileno (stdin
));
149 if (pdi
[1] != fileno (stdout
))
150 dup2 (pdi
[1], fileno (stdout
));
151 if (pdi
[1] != fileno (stderr
))
152 dup2 (pdi
[1], fileno (stderr
));
153 for (i
= fileno (stderr
) + 1; i
< NBITS
; i
++)
157 vec
[vecp
++] = r1bindex (sendmail
, '/');
160 vec
[vecp
++] = watch
? "-odi" : queued
? "-odq" : "-odb";
161 vec
[vecp
++] = "-oem";
166 # endif /* not RAND */
167 #endif /* not ZMAILER */
172 execvp (sendmail
, vec
);
173 fprintf (stderr
, "unable to exec ");
175 _exit (-1); /* NOTREACHED */
178 SIGNAL (SIGALRM
, alrmser
);
179 SIGNAL (SIGPIPE
, SIG_IGN
);
183 if ((sm_rfp
= fdopen (pdi
[0], "r")) == NULL
184 || (sm_wfp
= fdopen (pdo
[1], "w")) == NULL
) {
187 sm_rfp
= sm_wfp
= NULL
;
188 return sm_ierror ("unable to fdopen");
203 if (client
&& *client
) {
205 result
= smtalk (SM_HELO
, "EHLO %s", client
);
208 if (500 <= result
&& result
<= 599)
209 result
= smtalk (SM_HELO
, "HELO %s", client
);
223 smtalk (SM_HELO
, "ONEX");
226 smtalk (SM_HELO
, "VERB on");
234 sm_winit (int mode
, char *from
)
237 if (sm_ispool
&& !sm_wfp
) {
238 strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
239 sm_reply
.code
= NOTOK
;
244 switch (smtalk (SM_MAIL
, "%s FROM:<%s>",
245 mode
== S_SEND
? "SEND" : mode
== S_SOML
? "SOML"
246 : mode
== S_SAML
? "SAML" : "MAIL", from
)) {
263 sm_wadr (char *mbox
, char *host
, char *path
)
265 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
267 path
? path
: "", mbox
, host
)) {
277 #endif /* SENDMAILBUG */
302 switch (smtalk (SM_DATA
, "DATA")) {
311 #endif /* SENDMAILBUG */
328 sm_wtxt (char *buffer
, int len
)
334 result
= sm_wstream (buffer
, len
);
337 return (result
== NOTOK
? RP_BHST
: RP_OK
);
344 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
347 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
355 #endif /* SENDMAILBUG */
382 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
387 smtalk (SM_QUIT
, "QUIT");
391 sm_note
.code
= sm_reply
.code
;
392 strncpy (sm_note
.text
, sm_reply
.text
, sm_note
.length
= sm_reply
.length
);/* fall */
394 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
396 kill (sm_child
, SIGKILL
);
400 sm_reply
.code
= sm_note
.code
;
401 strncpy (sm_reply
.text
, sm_note
.text
, sm_reply
.length
= sm_note
.length
);
405 if (sm_rfp
!= NULL
) {
410 if (sm_wfp
!= NULL
) {
416 status
= pidwait (sm_child
, OK
);
419 sm_rfp
= sm_wfp
= NULL
;
421 return (status
? RP_BHST
: RP_OK
);
426 sm_ierror (char *fmt
, ...)
431 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
434 sm_reply
.length
= strlen (sm_reply
.text
);
435 sm_reply
.code
= NOTOK
;
442 smtalk (int time
, char *fmt
, ...)
449 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
453 printf ("=> %s\n", buffer
);
461 if (strcmp (buffer
, ".") == 0)
463 fprintf (sm_wfp
, "%s\r\n", buffer
);
469 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
470 (char) (sm_ispool
+ 'a' - 1));
471 if (rename (sm_tmpfil
, file
) == NOTOK
) {
475 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
476 "error renaming %s to %s: ", sm_tmpfil
, file
);
480 if ((s
= strerror (errno
)))
481 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
483 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
484 "unknown error %d", errno
);
485 sm_reply
.length
= strlen (sm_reply
.text
);
486 sm_reply
.code
= NOTOK
;
490 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
491 chmod (sm_tmpfil
, 0600);
502 ftruncate (fileno (sm_wfp
), 0L);
503 fseek (sm_wfp
, 0L, SEEK_SET
);
522 printf ("<= %d\n", result
);
526 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
527 return (sm_reply
.code
= result
);
532 alarm ((unsigned) time
);
533 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
542 sm_wrecord (char *buffer
, int len
)
547 fwrite (buffer
, sizeof *buffer
, len
, sm_wfp
);
548 fputs ("\r\n", sm_wfp
);
551 return (ferror (sm_wfp
) ? sm_werror () : OK
);
556 sm_wstream (char *buffer
, int len
)
564 if (buffer
== NULL
&& len
== 0) {
566 fputs ("\r\n", sm_wfp
);
568 return (ferror (sm_wfp
) ? sm_werror () : OK
);
571 for (bp
= buffer
; len
> 0; bp
++, len
--) {
575 fputc ('\r', sm_wfp
);
580 fputc ('.', sm_wfp
);/* FALL THROUGH */
591 return (ferror (sm_wfp
) ? sm_werror () : OK
);
597 * AIX by default will inline the strlen and strcpy commands by redefining
598 * them as __strlen and __strcpy respectively. This causes compile problems
599 * with the #ifdef MPOP in the middle. Should the #ifdef MPOP be removed,
600 * remove these #undefs.
610 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no pipe opened"
611 : sm_alarmed
? "write to pipe timed out"
612 : "error writing to pipe"));
614 return (sm_reply
.code
= NOTOK
);
621 int i
, code
, cont
, bc
, rc
, more
;
623 char **ehlo
, buffer
[BUFSIZ
];
626 static int at_least_once
= 0;
629 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++)
635 *(ehlo
= EHLOkeys
) = NULL
;
640 sm_reply
.text
[sm_reply
.length
= 0] = 0;
643 rc
= sizeof(sm_reply
.text
) - 1;
645 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
647 printf ("<= %s\n", buffer
);
652 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
653 && (buffer
[3] == '-' || doingEHLO
== 2)
655 if (doingEHLO
== 2) {
656 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
657 strcpy (*ehlo
++, buffer
+ 4);
659 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
669 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
675 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
677 if (bc
> 0 && *bp
== '-') {
680 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
685 if (code
!= sm_reply
.code
|| cont
)
689 sm_reply
.code
= code
;
692 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
694 bc
= strlen (sm_noreply
);
697 if ((i
= min (bc
, rc
)) > 0) {
701 if (more
&& rc
> strlen (sm_moreply
) + 1) {
702 strncpy (sm_reply
.text
+ rc
, sm_moreply
, sizeof(sm_reply
.text
) - rc
);
703 rc
+= strlen (sm_moreply
);
708 if (sm_reply
.code
< 100) {
710 printf ("%s\n", sm_reply
.text
);
716 sm_reply
.length
= rp
- sm_reply
.text
;
718 return sm_reply
.code
;
726 sm_rrecord (char *buffer
, int *len
)
731 buffer
[*len
= 0] = 0;
733 fgets (buffer
, BUFSIZ
, sm_rfp
);
734 *len
= strlen (buffer
);
735 if (ferror (sm_rfp
) || feof (sm_rfp
))
737 if (buffer
[*len
- 1] != '\n')
738 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
741 if (buffer
[*len
- 2] == '\r')
743 buffer
[*len
- 1] = 0;
753 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no pipe opened"
754 : sm_alarmed
? "read from pipe timed out"
755 : feof (sm_rfp
) ? "premature end-of-file on pipe"
756 : "error reading from pipe"));
758 return (sm_reply
.code
= NOTOK
);
765 #ifndef RELIABLE_SIGNALS
766 SIGNAL (SIGALRM
, alrmser
);
771 printf ("timed out...\n");
781 static char buffer
[BUFSIZ
];
783 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
803 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
823 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
824 text
, sm_reply
.code
, sm_reply
.text
);