]>
diplodocus.org Git - nmh/blob - mts/smtp/smtp.c
f7756381d2df2aae2858534215fa9ab575f9d79b
3 * smtp.c -- nmh SMTP interface
10 #include <zotnet/mts/mts.h>
14 * This module implements an interface to SendMail very similar
15 * to the MMDF mm_(3) routines. The sm_() routines herein talk
16 * SMTP to a sendmail process, mapping SMTP reply codes into
21 * On older 4.2BSD machines without the POSIX function `sigaction',
22 * the alarm handing stuff for time-outs will NOT work due to the way
23 * syscalls get restarted. This is not really crucial, since SendMail
24 * is generally well-behaved in this area.
29 * It appears that some versions of Sendmail will return Code 451
30 * when they don't really want to indicate a failure.
31 * "Code 451 almost always means sendmail has deferred; we don't
32 * really want bomb out at this point since sendmail will rectify
33 * things later." So, if you define SENDMAILBUG, Code 451 is
34 * considered the same as Code 250. Yuck!
42 * these codes must all be different!
44 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
55 static int sm_addrs
= 0;
56 static int sm_alarmed
= 0;
57 static int sm_debug
= 0;
58 static int sm_nl
= TRUE
;
59 static int sm_verbose
= 0;
61 static FILE *sm_rfp
= NULL
;
62 static FILE *sm_wfp
= NULL
;
65 static int sm_ispool
= 0;
66 static char sm_tmpfil
[BUFSIZ
];
69 static char *sm_noreply
= "No reply text given";
70 static char *sm_moreply
= "; ";
72 struct smtp sm_reply
; /* global... */
82 char *EHLOkeys
[MAXEHLO
+ 1];
87 static int rclient (char *, char *, char *);
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);
97 static char *EHLOset (char *);
101 * smtp.c's own static copy of several nmh library subroutines
103 static char **smail_brkstring (char *, char *, char *);
104 static int smail_brkany (char, char *);
105 char **smail_copyip (char **, char **, int);
110 sm_init (char *client
, char *server
, int watch
, int verbose
,
111 int debug
, int onex
, int queued
)
113 int result
, sd1
, sd2
;
118 sm_verbose
= verbose
;
126 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
129 if (client
== NULL
|| *client
== '\0')
133 client
= LocalName(); /* no clientname -> LocalName */
136 if (client
== NULL
|| *client
== '\0')
137 client
= "localhost";
140 if ((sd1
= rclient (server
, "tcp", "smtp")) == NOTOK
)
151 if ((sm_wfp
= fdopen (sd1
, "w")) == NULL
) {
154 return sm_ierror ("unable to fdopen");
157 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
158 return (sm_reply
.code
= RP_OK
);
162 if ((sd2
= dup (sd1
)) == NOTOK
) {
164 return sm_ierror ("unable to dup");
167 SIGNAL (SIGALRM
, alrmser
);
168 SIGNAL (SIGPIPE
, SIG_IGN
);
170 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
171 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
174 sm_rfp
= sm_wfp
= NULL
;
175 return sm_ierror ("unable to fdopen");
193 * Give EHLO or HELO command
195 if (client
&& *client
) {
197 result
= smtalk (SM_HELO
, "EHLO %s", client
);
200 if (result
>= 500 && result
<= 599)
201 result
= smtalk (SM_HELO
, "HELO %s", client
);
210 if (watch
&& EHLOset ("XVRB"))
211 smtalk (SM_HELO
, "VERB on");
212 if (onex
&& EHLOset ("XONE"))
213 smtalk (SM_HELO
, "ONEX");
214 if (queued
&& EHLOset ("XQUE"))
215 smtalk (SM_HELO
, "QUED");
222 # define MAXARGS 1000
226 rclient (char *server
, char *protocol
, char *service
)
229 char response
[BUFSIZ
];
234 if ((sd
= client (server
, protocol
, service
, FALSE
, response
, sizeof(response
))) != NOTOK
)
238 if (!server
&& servers
&& (cp
= strchr(servers
, '/'))) {
240 char *arguments
[MAXARGS
];
242 smail_copyip (smail_brkstring (cp
= getcpy (servers
), " ", "\n"), arguments
, MAXARGS
);
244 for (ap
= arguments
; *ap
; ap
++)
248 if ((dp
= strrchr(*ap
, '/')) && *++dp
== NULL
)
250 snprintf (sm_tmpfil
, sizeof(sm_tmpfil
), "%s/smtpXXXXXX", *ap
);
253 if ((sd
= creat (sm_tmpfil
, 0600)) != NOTOK
) {
265 sm_ierror ("%s", response
);
271 sm_winit (int mode
, char *from
)
276 if (sm_ispool
&& !sm_wfp
) {
277 strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
278 sm_reply
.code
= NOTOK
;
301 switch (smtalk (SM_MAIL
, "%s FROM:<%s>", smtpcom
, from
)) {
318 sm_wadr (char *mbox
, char *host
, char *path
)
320 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
322 path
? path
: "", mbox
, host
)) {
332 #endif /* SENDMAILBUG */
357 switch (smtalk (SM_DATA
, "DATA")) {
366 #endif /* SENDMAILBUG */
383 sm_wtxt (char *buffer
, int len
)
389 result
= sm_wstream (buffer
, len
);
392 return (result
== NOTOK
? RP_BHST
: RP_OK
);
399 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
402 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
410 #endif /* SENDMAILBUG */
428 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
433 smtalk (SM_QUIT
, "QUIT");
437 sm_note
.code
= sm_reply
.code
;
438 strncpy (sm_note
.text
, sm_reply
.text
, sm_note
.length
= sm_reply
.length
);/* fall */
440 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
442 smtalk (SM_QUIT
, "QUIT");
444 sm_reply
.code
= sm_note
.code
;
445 strncpy (sm_reply
.text
, sm_note
.text
, sm_reply
.length
= sm_note
.length
);
462 if (sm_rfp
!= NULL
) {
467 if (sm_wfp
!= NULL
) {
474 sm_rfp
= sm_wfp
= NULL
;
475 return (status
? RP_BHST
: RP_OK
);
484 int cc
, i
, j
, k
, result
;
486 char *dp
, *bp
, *cp
, s
;
487 char buffer
[BUFSIZ
], sender
[BUFSIZ
];
491 k
= strlen (file
) - sizeof(".bulk");
492 if ((fp
= fopen (file
, "r")) == NULL
) {
495 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
496 "unable to read %s: ", file
);
500 if ((s
= strerror (errno
)))
501 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
503 snprintf (bp
, sizeof(sm_reply
.text
) - len
, "Error %d", errno
);
504 sm_reply
.length
= strlen (sm_reply
.text
);
505 sm_reply
.code
= NOTOK
;
509 printf ("reading file %s\n", file
);
514 while (fgets (buffer
, sizeof(buffer
), fp
)) {
516 strncpy (sender
, buffer
+ sizeof("MAIL FROM:") - 1, sizeof(sender
));
517 if (strcmp (buffer
, "DATA\r\n") == 0) {
524 printf ("no DATA...\n");
528 snprintf (buffer
, sizeof(buffer
), "%s.bad", file
);
529 rename (file
, buffer
);
531 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
540 printf ("no %srecipients...\n", j
< 1 ? "sender or " : "");
546 if ((cp
= malloc ((size_t) (cc
= (pos
= ftell (fp
)) + 1))) == NULL
) {
547 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
549 sm_reply
.code
= NOTOK
;
553 fseek (fp
, 0L, SEEK_SET
);
554 for (dp
= cp
, i
= 0; i
++ < j
; dp
+= strlen (dp
))
555 if (fgets (dp
, cc
- (dp
- cp
), fp
) == NULL
) {
556 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "premature eof"));
563 for (dp
= cp
, i
= cc
- 1; i
> 0; dp
+= cc
, i
-= cc
)
564 if ((cc
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
) {
567 strcpy (sm_reply
.text
, "error writing to server: ",
568 sizeof(sm_reply
.text
));
572 if ((s
= strerror (errno
)))
573 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
575 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
576 "unknown error %d", errno
);
577 sm_reply
.length
= strlen (sm_reply
.text
);
582 printf ("wrote %d octets to server\n", cc
);
586 for (dp
= cp
, i
= 0; i
++ < j
; dp
= strchr(dp
, '\n'), dp
++) {
588 if (bp
= strchr(dp
, '\r'))
590 printf ("=> %s\n", dp
);
596 switch (smhear () + (i
== 1 ? 1000 : i
!= j
? 2000 : 3000)) {
608 smtalk (SM_RSET
, "RSET");
636 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0)
640 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
641 if ((gp
= fopen (buffer
, "w+")) == NULL
)
643 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
646 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
647 l
- 4, l
- 4, sender
+ 1, file
);
648 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
649 dtimenow (0), LocalName ());
651 if (bp
= strchr(dp
, '\r'))
653 fprintf (gp
, "=> %s\r\n", dp
);
656 fprintf (gp
, "<= %s\r\n", rp_string (result
));
674 smtalk (SM_RSET
, "RSET");
677 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
688 smtalk (SM_RSET
, "RSET");
700 #ifdef HAVE_ST_BLKSIZE
703 if (fstat (fileno (sm_wfp
), &st
) == NOTOK
|| (cc
= st
.st_blksize
) < BUFSIZ
)
708 if ((cp
= malloc ((size_t) cc
)) == NULL
) {
709 smtalk (SM_RSET
, "RSET");
710 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
715 fseek (fp
, pos
, SEEK_SET
);
719 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
720 if ((j
= fread (cp
, sizeof(*cp
), i
, fp
)) == OK
) {
724 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
725 "error reading %s: ", file
);
729 if ((s
= strerror (errno
)))
730 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
732 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
733 "unknown error %d", errno
);
734 sm_reply
.length
= strlen (sm_reply
.text
);
742 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
743 if ((j
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
)
747 printf ("wrote %d octets to server\n", j
);
774 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
785 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0) {
791 ftruncate (fileno (gp
), 0L);
792 fseek (gp
, 0L, SEEK_SET
);
795 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
796 if ((gp
= fopen (buffer
, "w")) == NULL
)
799 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
801 fprintf (gp
, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
802 i
- 4, i
- 4, sender
+ 1, file
);
803 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
804 dtimenow (0), LocalName ());
809 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp
);
810 fseek (fp
, pos
, SEEK_SET
);
811 while (fgets (buffer
, sizeof(buffer
), fp
)) {
812 if (buffer
[0] == '-')
814 if (strcmp (buffer
, ".\r\n"))
817 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp
);
831 sm_ierror (char *fmt
, ...)
836 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
839 sm_reply
.length
= strlen (sm_reply
.text
);
840 sm_reply
.code
= NOTOK
;
847 smtalk (int time
, char *fmt
, ...)
854 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
858 printf ("=> %s\n", buffer
);
866 if (strcmp (buffer
, ".") == 0)
868 fprintf (sm_wfp
, "%s\r\n", buffer
);
874 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
875 (char) (sm_ispool
+ 'a' - 1));
876 if (rename (sm_tmpfil
, file
) == NOTOK
) {
880 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
881 "error renaming %s to %s: ", sm_tmpfil
, file
);
885 if ((s
= strerror (errno
)))
886 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
888 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
889 "unknown error %d", errno
);
890 sm_reply
.length
= strlen (sm_reply
.text
);
891 sm_reply
.code
= NOTOK
;
895 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
896 chmod (sm_tmpfil
, 0600);
907 ftruncate (fileno (sm_wfp
), 0L);
908 fseek (sm_wfp
, 0L, SEEK_SET
);
927 printf ("<= %d\n", result
);
931 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
932 return (sm_reply
.code
= result
);
937 alarm ((unsigned) time
);
938 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
947 * write the buffer to the open SMTP channel
951 sm_wrecord (char *buffer
, int len
)
956 fwrite (buffer
, sizeof(*buffer
), len
, sm_wfp
);
957 fputs ("\r\n", sm_wfp
);
960 return (ferror (sm_wfp
) ? sm_werror () : OK
);
965 sm_wstream (char *buffer
, int len
)
968 static char lc
= '\0';
973 if (buffer
== NULL
&& len
== 0) {
975 fputs ("\r\n", sm_wfp
);
977 return (ferror (sm_wfp
) ? sm_werror () : OK
);
980 for (bp
= buffer
; len
> 0; bp
++, len
--) {
984 fputc ('\r', sm_wfp
);
989 fputc ('.', sm_wfp
);/* FALL THROUGH */
1000 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1006 * AIX by default will inline the strlen and strcpy commands by redefining
1007 * them as __strlen and __strcpy respectively. This causes compile problems
1008 * with the #ifdef MPOP in the middle. Should the #ifdef MPOP be removed,
1009 * remove these #undefs.
1019 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1020 : sm_alarmed
? "write to socket timed out"
1022 : sm_ispool
? "error writing to spool file"
1024 : "error writing to socket"));
1026 return (sm_reply
.code
= NOTOK
);
1033 int i
, code
, cont
, bc
, rc
, more
;
1035 char **ehlo
, buffer
[BUFSIZ
];
1038 static int at_least_once
= 0;
1040 if (at_least_once
) {
1043 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1057 sm_reply
.length
= 0;
1058 sm_reply
.text
[0] = 0;
1060 rc
= sizeof(sm_reply
.text
) - 1;
1062 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
1064 printf ("<= %s\n", buffer
);
1069 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1070 && (buffer
[3] == '-' || doingEHLO
== 2)
1072 if (doingEHLO
== 2) {
1073 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1074 strcpy (*ehlo
++, buffer
+ 4);
1076 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1086 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1092 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1094 if (bc
> 0 && *bp
== '-') {
1097 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1102 if (code
!= sm_reply
.code
|| cont
)
1106 sm_reply
.code
= code
;
1109 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1111 bc
= strlen (sm_noreply
);
1115 if ((i
= min (bc
, rc
)) > 0) {
1116 strncpy (rp
, bp
, i
);
1118 if (more
&& rc
> strlen (sm_moreply
) + 1) {
1119 strcpy (sm_reply
.text
+ rc
, sm_moreply
);
1120 rc
+= strlen (sm_moreply
);
1125 if (sm_reply
.code
< 100) {
1127 printf ("%s\n", sm_reply
.text
);
1133 sm_reply
.length
= rp
- sm_reply
.text
;
1134 return sm_reply
.code
;
1141 sm_rrecord (char *buffer
, int *len
)
1144 return sm_rerror ();
1146 buffer
[*len
= 0] = 0;
1148 fgets (buffer
, BUFSIZ
, sm_rfp
);
1149 *len
= strlen (buffer
);
1150 if (ferror (sm_rfp
) || feof (sm_rfp
))
1151 return sm_rerror ();
1152 if (buffer
[*len
- 1] != '\n')
1153 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
1156 if (buffer
[*len
- 2] == '\r')
1158 buffer
[*len
- 1] = 0;
1168 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1169 : sm_alarmed
? "read from socket timed out"
1170 : feof (sm_rfp
) ? "premature end-of-file on socket"
1171 : "error reading from socket"));
1173 return (sm_reply
.code
= NOTOK
);
1180 #ifndef RELIABLE_SIGNALS
1181 SIGNAL (SIGALRM
, alrmser
);
1186 printf ("timed out...\n");
1193 rp_string (int code
)
1196 static char buffer
[BUFSIZ
];
1198 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1218 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1238 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1239 text
, sm_reply
.code
, sm_reply
.text
);
1246 static char *broken
[MAXARGS
+ 1];
1249 smail_brkstring (char *strg
, char *brksep
, char *brkterm
)
1256 for (bi
= 0; bi
< MAXARGS
; bi
++) {
1257 while (smail_brkany (c
= *sp
, brksep
))
1259 if (!c
|| smail_brkany (c
, brkterm
)) {
1266 while ((c
= *++sp
) && !smail_brkany (c
, brksep
) && !smail_brkany (c
, brkterm
))
1269 broken
[MAXARGS
] = 0;
1276 * returns 1 if chr in strg, 0 otherwise
1279 smail_brkany (char chr
, char *strg
)
1284 for (sp
= strg
; *sp
; sp
++)
1291 * copy a string array and return pointer to end
1294 smail_copyip (char **p
, char **q
, int len_q
)
1296 while (*p
&& --len_q
> 0)
1315 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1317 if (strncmp (ep
, s
, len
) == 0) {
1318 for (ep
+= len
; *ep
== ' '; ep
++)