]>
diplodocus.org Git - nmh/blob - mts/smtp/smtp.c
3 * smtp.c -- nmh SMTP interface
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!
45 * these codes must all be different!
47 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
58 static int sm_addrs
= 0;
59 static int sm_alarmed
= 0;
60 static int sm_debug
= 0;
61 static int sm_nl
= TRUE
;
62 static int sm_verbose
= 0;
64 static FILE *sm_rfp
= NULL
;
65 static FILE *sm_wfp
= NULL
;
68 static int sm_ispool
= 0;
69 static char sm_tmpfil
[BUFSIZ
];
72 static char *sm_noreply
= "No reply text given";
73 static char *sm_moreply
= "; ";
75 struct smtp sm_reply
; /* global... */
81 char *EHLOkeys
[MAXEHLO
+ 1];
86 static int rclient (char *, char *, char *);
87 static int sm_ierror (char *fmt
, ...);
88 static int smtalk (int time
, char *fmt
, ...);
89 static int sm_wrecord (char *, int);
90 static int sm_wstream (char *, int);
91 static int sm_werror (void);
92 static int smhear (void);
93 static int sm_rrecord (char *, int *);
94 static int sm_rerror (void);
95 static RETSIGTYPE
alrmser (int);
96 static char *EHLOset (char *);
100 * smtp.c's own static copy of several nmh library subroutines
102 static char **smail_brkstring (char *, char *, char *);
103 static int smail_brkany (char, char *);
104 char **smail_copyip (char **, char **, int);
109 sm_init (char *client
, char *server
, int watch
, int verbose
,
110 int debug
, int onex
, int queued
)
112 int result
, sd1
, sd2
;
117 sm_verbose
= verbose
;
125 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
128 if (client
== NULL
|| *client
== '\0')
132 client
= LocalName(); /* no clientname -> LocalName */
135 if (client
== NULL
|| *client
== '\0')
136 client
= "localhost";
139 if ((sd1
= rclient (server
, "tcp", "smtp")) == NOTOK
)
150 if ((sm_wfp
= fdopen (sd1
, "w")) == NULL
) {
153 return sm_ierror ("unable to fdopen");
156 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
157 return (sm_reply
.code
= RP_OK
);
161 if ((sd2
= dup (sd1
)) == NOTOK
) {
163 return sm_ierror ("unable to dup");
166 SIGNAL (SIGALRM
, alrmser
);
167 SIGNAL (SIGPIPE
, SIG_IGN
);
169 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
170 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
173 sm_rfp
= sm_wfp
= NULL
;
174 return sm_ierror ("unable to fdopen");
192 * Give EHLO or HELO command
194 if (client
&& *client
) {
196 result
= smtalk (SM_HELO
, "EHLO %s", client
);
199 if (result
>= 500 && result
<= 599)
200 result
= smtalk (SM_HELO
, "HELO %s", client
);
209 if (watch
&& EHLOset ("XVRB"))
210 smtalk (SM_HELO
, "VERB on");
211 if (onex
&& EHLOset ("XONE"))
212 smtalk (SM_HELO
, "ONEX");
213 if (queued
&& EHLOset ("XQUE"))
214 smtalk (SM_HELO
, "QUED");
221 # define MAXARGS 1000
225 rclient (char *server
, char *protocol
, char *service
)
228 char response
[BUFSIZ
];
233 if ((sd
= client (server
, protocol
, service
, FALSE
, response
, sizeof(response
))) != NOTOK
)
237 if (!server
&& servers
&& (cp
= strchr(servers
, '/'))) {
239 char *arguments
[MAXARGS
];
241 smail_copyip (smail_brkstring (cp
= getcpy (servers
), " ", "\n"), arguments
, MAXARGS
);
243 for (ap
= arguments
; *ap
; ap
++)
247 if ((dp
= strrchr(*ap
, '/')) && *++dp
== NULL
)
249 snprintf (sm_tmpfil
, sizeof(sm_tmpfil
), "%s/smtpXXXXXX", *ap
);
251 sd
= mkstemp (sm_tmpfil
);
255 if ((sd
= creat (sm_tmpfil
, 0600)) != NOTOK
) {
268 sm_ierror ("%s", response
);
274 sm_winit (int mode
, char *from
)
279 if (sm_ispool
&& !sm_wfp
) {
280 strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
281 sm_reply
.code
= NOTOK
;
304 switch (smtalk (SM_MAIL
, "%s FROM:<%s>", smtpcom
, from
)) {
321 sm_wadr (char *mbox
, char *host
, char *path
)
323 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
325 path
? path
: "", mbox
, host
)) {
335 #endif /* SENDMAILBUG */
360 switch (smtalk (SM_DATA
, "DATA")) {
369 #endif /* SENDMAILBUG */
386 sm_wtxt (char *buffer
, int len
)
392 result
= sm_wstream (buffer
, len
);
395 return (result
== NOTOK
? RP_BHST
: RP_OK
);
402 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
405 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
413 #endif /* SENDMAILBUG */
431 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
436 smtalk (SM_QUIT
, "QUIT");
440 sm_note
.code
= sm_reply
.code
;
441 strncpy (sm_note
.text
, sm_reply
.text
, sm_note
.length
= sm_reply
.length
);/* fall */
443 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
445 smtalk (SM_QUIT
, "QUIT");
447 sm_reply
.code
= sm_note
.code
;
448 strncpy (sm_reply
.text
, sm_note
.text
, sm_reply
.length
= sm_note
.length
);
465 if (sm_rfp
!= NULL
) {
470 if (sm_wfp
!= NULL
) {
477 sm_rfp
= sm_wfp
= NULL
;
478 return (status
? RP_BHST
: RP_OK
);
487 int cc
, i
, j
, k
, result
;
489 char *dp
, *bp
, *cp
, s
;
490 char buffer
[BUFSIZ
], sender
[BUFSIZ
];
494 k
= strlen (file
) - sizeof(".bulk");
495 if ((fp
= fopen (file
, "r")) == NULL
) {
498 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
499 "unable to read %s: ", file
);
503 if ((s
= strerror (errno
)))
504 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
506 snprintf (bp
, sizeof(sm_reply
.text
) - len
, "Error %d", errno
);
507 sm_reply
.length
= strlen (sm_reply
.text
);
508 sm_reply
.code
= NOTOK
;
512 printf ("reading file %s\n", file
);
517 while (fgets (buffer
, sizeof(buffer
), fp
)) {
519 strncpy (sender
, buffer
+ sizeof("MAIL FROM:") - 1, sizeof(sender
));
520 if (strcmp (buffer
, "DATA\r\n") == 0) {
527 printf ("no DATA...\n");
531 snprintf (buffer
, sizeof(buffer
), "%s.bad", file
);
532 rename (file
, buffer
);
534 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
543 printf ("no %srecipients...\n", j
< 1 ? "sender or " : "");
549 if ((cp
= malloc ((size_t) (cc
= (pos
= ftell (fp
)) + 1))) == NULL
) {
550 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
552 sm_reply
.code
= NOTOK
;
556 fseek (fp
, 0L, SEEK_SET
);
557 for (dp
= cp
, i
= 0; i
++ < j
; dp
+= strlen (dp
))
558 if (fgets (dp
, cc
- (dp
- cp
), fp
) == NULL
) {
559 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "premature eof"));
566 for (dp
= cp
, i
= cc
- 1; i
> 0; dp
+= cc
, i
-= cc
)
567 if ((cc
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
) {
570 strcpy (sm_reply
.text
, "error writing to server: ",
571 sizeof(sm_reply
.text
));
575 if ((s
= strerror (errno
)))
576 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
578 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
579 "unknown error %d", errno
);
580 sm_reply
.length
= strlen (sm_reply
.text
);
585 printf ("wrote %d octets to server\n", cc
);
589 for (dp
= cp
, i
= 0; i
++ < j
; dp
= strchr(dp
, '\n'), dp
++) {
591 if (bp
= strchr(dp
, '\r'))
593 printf ("=> %s\n", dp
);
599 switch (smhear () + (i
== 1 ? 1000 : i
!= j
? 2000 : 3000)) {
611 smtalk (SM_RSET
, "RSET");
639 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0)
643 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
644 if ((gp
= fopen (buffer
, "w+")) == NULL
)
646 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
649 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
650 l
- 4, l
- 4, sender
+ 1, file
);
651 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
652 dtimenow (0), LocalName ());
654 if (bp
= strchr(dp
, '\r'))
656 fprintf (gp
, "=> %s\r\n", dp
);
659 fprintf (gp
, "<= %s\r\n", rp_string (result
));
677 smtalk (SM_RSET
, "RSET");
680 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
691 smtalk (SM_RSET
, "RSET");
703 #ifdef HAVE_ST_BLKSIZE
706 if (fstat (fileno (sm_wfp
), &st
) == NOTOK
|| (cc
= st
.st_blksize
) < BUFSIZ
)
711 if ((cp
= malloc ((size_t) cc
)) == NULL
) {
712 smtalk (SM_RSET
, "RSET");
713 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
718 fseek (fp
, pos
, SEEK_SET
);
722 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
723 if ((j
= fread (cp
, sizeof(*cp
), i
, fp
)) == OK
) {
727 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
728 "error reading %s: ", file
);
732 if ((s
= strerror (errno
)))
733 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
735 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
736 "unknown error %d", errno
);
737 sm_reply
.length
= strlen (sm_reply
.text
);
745 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
746 if ((j
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
)
750 printf ("wrote %d octets to server\n", j
);
777 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
788 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0) {
794 ftruncate (fileno (gp
), 0L);
795 fseek (gp
, 0L, SEEK_SET
);
798 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
799 if ((gp
= fopen (buffer
, "w")) == NULL
)
802 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
804 fprintf (gp
, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
805 i
- 4, i
- 4, sender
+ 1, file
);
806 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
807 dtimenow (0), LocalName ());
812 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp
);
813 fseek (fp
, pos
, SEEK_SET
);
814 while (fgets (buffer
, sizeof(buffer
), fp
)) {
815 if (buffer
[0] == '-')
817 if (strcmp (buffer
, ".\r\n"))
820 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp
);
834 sm_ierror (char *fmt
, ...)
839 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
842 sm_reply
.length
= strlen (sm_reply
.text
);
843 sm_reply
.code
= NOTOK
;
850 smtalk (int time
, char *fmt
, ...)
857 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
861 printf ("=> %s\n", buffer
);
869 if (strcmp (buffer
, ".") == 0)
871 fprintf (sm_wfp
, "%s\r\n", buffer
);
877 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
878 (char) (sm_ispool
+ 'a' - 1));
879 if (rename (sm_tmpfil
, file
) == NOTOK
) {
883 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
884 "error renaming %s to %s: ", sm_tmpfil
, file
);
888 if ((s
= strerror (errno
)))
889 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
891 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
892 "unknown error %d", errno
);
893 sm_reply
.length
= strlen (sm_reply
.text
);
894 sm_reply
.code
= NOTOK
;
898 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
899 chmod (sm_tmpfil
, 0600);
910 ftruncate (fileno (sm_wfp
), 0L);
911 fseek (sm_wfp
, 0L, SEEK_SET
);
930 printf ("<= %d\n", result
);
934 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
935 return (sm_reply
.code
= result
);
940 alarm ((unsigned) time
);
941 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
950 * write the buffer to the open SMTP channel
954 sm_wrecord (char *buffer
, int len
)
959 fwrite (buffer
, sizeof(*buffer
), len
, sm_wfp
);
960 fputs ("\r\n", sm_wfp
);
963 return (ferror (sm_wfp
) ? sm_werror () : OK
);
968 sm_wstream (char *buffer
, int len
)
971 static char lc
= '\0';
976 if (buffer
== NULL
&& len
== 0) {
978 fputs ("\r\n", sm_wfp
);
980 return (ferror (sm_wfp
) ? sm_werror () : OK
);
983 for (bp
= buffer
; len
> 0; bp
++, len
--) {
987 fputc ('\r', sm_wfp
);
992 fputc ('.', sm_wfp
);/* FALL THROUGH */
1003 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1009 * AIX by default will inline the strlen and strcpy commands by redefining
1010 * them as __strlen and __strcpy respectively. This causes compile problems
1011 * with the #ifdef MPOP in the middle. Should the #ifdef MPOP be removed,
1012 * remove these #undefs.
1022 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1023 : sm_alarmed
? "write to socket timed out"
1025 : sm_ispool
? "error writing to spool file"
1027 : "error writing to socket"));
1029 return (sm_reply
.code
= NOTOK
);
1036 int i
, code
, cont
, bc
, rc
, more
;
1038 char **ehlo
, buffer
[BUFSIZ
];
1041 static int at_least_once
= 0;
1043 if (at_least_once
) {
1046 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1060 sm_reply
.length
= 0;
1061 sm_reply
.text
[0] = 0;
1063 rc
= sizeof(sm_reply
.text
) - 1;
1065 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
1067 printf ("<= %s\n", buffer
);
1072 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1073 && (buffer
[3] == '-' || doingEHLO
== 2)
1075 if (doingEHLO
== 2) {
1076 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1077 strcpy (*ehlo
++, buffer
+ 4);
1079 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1089 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1095 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1097 if (bc
> 0 && *bp
== '-') {
1100 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1105 if (code
!= sm_reply
.code
|| cont
)
1109 sm_reply
.code
= code
;
1112 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1114 bc
= strlen (sm_noreply
);
1118 if ((i
= min (bc
, rc
)) > 0) {
1119 strncpy (rp
, bp
, i
);
1121 if (more
&& rc
> strlen (sm_moreply
) + 1) {
1122 strcpy (sm_reply
.text
+ rc
, sm_moreply
);
1123 rc
+= strlen (sm_moreply
);
1128 if (sm_reply
.code
< 100) {
1130 printf ("%s\n", sm_reply
.text
);
1136 sm_reply
.length
= rp
- sm_reply
.text
;
1137 return sm_reply
.code
;
1144 sm_rrecord (char *buffer
, int *len
)
1147 return sm_rerror ();
1149 buffer
[*len
= 0] = 0;
1151 fgets (buffer
, BUFSIZ
, sm_rfp
);
1152 *len
= strlen (buffer
);
1153 if (ferror (sm_rfp
) || feof (sm_rfp
))
1154 return sm_rerror ();
1155 if (buffer
[*len
- 1] != '\n')
1156 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
1159 if (buffer
[*len
- 2] == '\r')
1161 buffer
[*len
- 1] = 0;
1171 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1172 : sm_alarmed
? "read from socket timed out"
1173 : feof (sm_rfp
) ? "premature end-of-file on socket"
1174 : "error reading from socket"));
1176 return (sm_reply
.code
= NOTOK
);
1183 #ifndef RELIABLE_SIGNALS
1184 SIGNAL (SIGALRM
, alrmser
);
1189 printf ("timed out...\n");
1196 rp_string (int code
)
1199 static char buffer
[BUFSIZ
];
1201 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1221 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1241 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1242 text
, sm_reply
.code
, sm_reply
.text
);
1249 static char *broken
[MAXARGS
+ 1];
1252 smail_brkstring (char *strg
, char *brksep
, char *brkterm
)
1259 for (bi
= 0; bi
< MAXARGS
; bi
++) {
1260 while (smail_brkany (c
= *sp
, brksep
))
1262 if (!c
|| smail_brkany (c
, brkterm
)) {
1269 while ((c
= *++sp
) && !smail_brkany (c
, brksep
) && !smail_brkany (c
, brkterm
))
1272 broken
[MAXARGS
] = 0;
1279 * returns 1 if chr in strg, 0 otherwise
1282 smail_brkany (char chr
, char *strg
)
1287 for (sp
= strg
; *sp
; sp
++)
1294 * copy a string array and return pointer to end
1297 smail_copyip (char **p
, char **q
, int len_q
)
1299 while (*p
&& --len_q
> 0)
1318 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1320 if (strncmp (ep
, s
, len
) == 0) {
1321 for (ep
+= len
; *ep
== ' '; ep
++)