2 * smtp.c -- nmh SMTP interface
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
15 #include <h/signals.h>
21 #include <sasl/sasl.h>
22 #include <sasl/saslutil.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
28 #endif /* CYRUS_SASL */
31 * This module implements an interface to SendMail very similar
32 * to the MMDF mm_(3) routines. The sm_() routines herein talk
33 * SMTP to a sendmail process, mapping SMTP reply codes into
38 * On older 4.2BSD machines without the POSIX function `sigaction',
39 * the alarm handing stuff for time-outs will NOT work due to the way
40 * syscalls get restarted. This is not really crucial, since SendMail
41 * is generally well-behaved in this area.
46 * It appears that some versions of Sendmail will return Code 451
47 * when they don't really want to indicate a failure.
48 * "Code 451 almost always means sendmail has deferred; we don't
49 * really want bomb out at this point since sendmail will rectify
50 * things later." So, if you define SENDMAILBUG, Code 451 is
51 * considered the same as Code 250. Yuck!
58 #define NBITS ((sizeof (int)) * 8)
61 * these codes must all be different!
63 #define SM_OPEN 300 /* Changed to 5 minutes to comply with a SHOULD in RFC 1123 */
66 #define SM_MAIL 301 /* changed to 5 minutes and a second (for uniqueness), see above */
67 #define SM_RCPT 302 /* see above */
68 #define SM_DATA 120 /* see above */
69 #define SM_TEXT 180 /* see above */
70 #define SM_DOT 600 /* see above */
75 static int sm_addrs
= 0;
76 static int sm_alarmed
= 0;
77 static int sm_child
= NOTOK
;
78 static int sm_debug
= 0;
79 static int sm_nl
= TRUE
;
80 static int sm_verbose
= 0;
82 static FILE *sm_rfp
= NULL
;
83 static FILE *sm_wfp
= NULL
;
86 static int sm_ispool
= 0;
87 static char sm_tmpfil
[BUFSIZ
];
92 * Some globals needed by SASL
95 static sasl_conn_t
*conn
= NULL
; /* SASL connection state */
96 static int sasl_complete
= 0; /* Has authentication succeded? */
97 static sasl_ssf_t sasl_ssf
; /* Our security strength factor */
98 static char *sasl_pw_context
[2]; /* Context to pass into sm_get_pass */
99 static int maxoutbuf
; /* Maximum crypto output buffer */
100 static int sm_get_user(void *, int, const char **, unsigned *);
101 static int sm_get_pass(sasl_conn_t
*, void *, int, sasl_secret_t
**);
103 static sasl_callback_t callbacks
[] = {
104 { SASL_CB_USER
, sm_get_user
, NULL
},
105 #define SM_SASL_N_CB_USER 0
106 { SASL_CB_PASS
, sm_get_pass
, NULL
},
107 #define SM_SASL_N_CB_PASS 1
108 { SASL_CB_LIST_END
, NULL
, NULL
},
110 #endif /* CYRUS_SASL */
112 static char *sm_noreply
= "No reply text given";
113 static char *sm_moreply
= "; ";
115 struct smtp sm_reply
; /* global... */
119 static int doingEHLO
;
120 char *EHLOkeys
[MAXEHLO
+ 1];
125 static int smtp_init (char *, char *, int, int, int, int, int, int,
127 static int sendmail_init (char *, char *, int, int, int, int, int);
129 static int rclient (char *, char *, char *);
130 static int sm_ierror (char *fmt
, ...);
131 static int smtalk (int time
, char *fmt
, ...);
132 static int sm_wrecord (char *, int);
133 static int sm_wstream (char *, int);
134 static int sm_werror (void);
135 static int smhear (void);
136 static int sm_rrecord (char *, int *);
137 static int sm_rerror (void);
138 static RETSIGTYPE
alrmser (int);
139 static char *EHLOset (char *);
142 static int sm_perror (char *fmt
, ...);
144 * smtp.c's own static copy of several nmh library subroutines
146 static char **smail_brkstring (char *, char *, char *);
147 static int smail_brkany (char, char *);
148 char **smail_copyip (char **, char **, int);
153 * Function prototypes needed for SASL
156 static int sm_auth_sasl(char *, char *, char *);
157 #endif /* CYRUS_SASL */
159 /* from mts/generic/client.c */
160 int client (char *, char *, char *, int, char *, int);
163 sm_init (char *client
, char *server
, int watch
, int verbose
,
164 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
167 if (sm_mts
== MTS_SMTP
)
168 return smtp_init (client
, server
, watch
, verbose
,
169 debug
, onex
, queued
, sasl
, saslmech
, user
);
171 return sendmail_init (client
, server
, watch
, verbose
,
172 debug
, onex
, queued
);
176 smtp_init (char *client
, char *server
, int watch
, int verbose
,
177 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
182 #endif /* CYRUS_SASL */
183 int result
, sd1
, sd2
;
188 sm_verbose
= verbose
;
196 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
199 if (client
== NULL
|| *client
== '\0') {
203 client
= LocalName(); /* no clientname -> LocalName */
208 if (client
== NULL
|| *client
== '\0')
209 client
= "localhost";
212 if ((sd1
= rclient (server
, "tcp", "smtp")) == NOTOK
)
223 if ((sm_wfp
= fdopen (sd1
, "w")) == NULL
) {
226 return sm_ierror ("unable to fdopen");
229 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
230 return (sm_reply
.code
= RP_OK
);
234 if ((sd2
= dup (sd1
)) == NOTOK
) {
236 return sm_ierror ("unable to dup");
239 SIGNAL (SIGALRM
, alrmser
);
240 SIGNAL (SIGPIPE
, SIG_IGN
);
242 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
243 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
246 sm_rfp
= sm_wfp
= NULL
;
247 return sm_ierror ("unable to fdopen");
265 * Give EHLO or HELO command
267 if (client
&& *client
) {
269 result
= smtalk (SM_HELO
, "EHLO %s", client
);
272 if (result
>= 500 && result
<= 599)
273 result
= smtalk (SM_HELO
, "HELO %s", client
);
283 * If the user asked for SASL, then check to see if the SMTP server
284 * supports it. Otherwise, error out (because the SMTP server
285 * might have been spoofed; we don't want to just silently not
290 if (! (server_mechs
= EHLOset("AUTH"))) {
292 return sm_ierror("SMTP server does not support SASL");
295 if (saslmech
&& stringdex(saslmech
, server_mechs
) == -1) {
297 return sm_ierror("Requested SASL mech \"%s\" is not in the "
298 "list of supported mechanisms:\n%s",
299 saslmech
, server_mechs
);
302 if (sm_auth_sasl(user
, saslmech
? saslmech
: server_mechs
,
308 #endif /* CYRUS_SASL */
311 if (watch
&& EHLOset ("XVRB"))
312 smtalk (SM_HELO
, "VERB on");
313 if (onex
&& EHLOset ("XONE"))
314 smtalk (SM_HELO
, "ONEX");
315 if (queued
&& EHLOset ("XQUE"))
316 smtalk (SM_HELO
, "QUED");
322 sendmail_init (char *client
, char *server
, int watch
, int verbose
,
323 int debug
, int onex
, int queued
)
332 sm_verbose
= verbose
;
334 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
337 if (client
== NULL
|| *client
== '\0') {
341 client
= LocalName(); /* no clientname -> LocalName */
345 if (client
== NULL
|| *client
== '\0')
346 client
= "localhost";
349 if (pipe (pdi
) == NOTOK
)
350 return sm_ierror ("no pipes");
351 if (pipe (pdo
) == NOTOK
) {
354 return sm_ierror ("no pipes");
357 for (i
= 0; (sm_child
= fork ()) == NOTOK
&& i
< 5; i
++)
366 return sm_ierror ("unable to fork");
369 if (pdo
[0] != fileno (stdin
))
370 dup2 (pdo
[0], fileno (stdin
));
371 if (pdi
[1] != fileno (stdout
))
372 dup2 (pdi
[1], fileno (stdout
));
373 if (pdi
[1] != fileno (stderr
))
374 dup2 (pdi
[1], fileno (stderr
));
375 for (i
= fileno (stderr
) + 1; i
< NBITS
; i
++)
379 vec
[vecp
++] = r1bindex (sendmail
, '/');
382 vec
[vecp
++] = watch
? "-odi" : queued
? "-odq" : "-odb";
383 vec
[vecp
++] = "-oem";
388 # endif /* not RAND */
389 #endif /* not ZMAILER */
394 execvp (sendmail
, vec
);
395 fprintf (stderr
, "unable to exec ");
397 _exit (-1); /* NOTREACHED */
400 SIGNAL (SIGALRM
, alrmser
);
401 SIGNAL (SIGPIPE
, SIG_IGN
);
405 if ((sm_rfp
= fdopen (pdi
[0], "r")) == NULL
406 || (sm_wfp
= fdopen (pdo
[1], "w")) == NULL
) {
409 sm_rfp
= sm_wfp
= NULL
;
410 return sm_ierror ("unable to fdopen");
425 if (client
&& *client
) {
427 result
= smtalk (SM_HELO
, "EHLO %s", client
);
430 if (500 <= result
&& result
<= 599)
431 result
= smtalk (SM_HELO
, "HELO %s", client
);
445 smtalk (SM_HELO
, "ONEX");
448 smtalk (SM_HELO
, "VERB on");
455 # define MAXARGS 1000
459 rclient (char *server
, char *protocol
, char *service
)
462 char response
[BUFSIZ
];
467 if ((sd
= client (server
, protocol
, service
, FALSE
, response
, sizeof(response
))) != NOTOK
)
471 if (!server
&& servers
&& (cp
= strchr(servers
, '/'))) {
473 char *arguments
[MAXARGS
];
475 smail_copyip (smail_brkstring (cp
= getcpy (servers
), " ", "\n"), arguments
, MAXARGS
);
477 for (ap
= arguments
; *ap
; ap
++)
481 if ((dp
= strrchr(*ap
, '/')) && *++dp
== NULL
)
483 snprintf (sm_tmpfil
, sizeof(sm_tmpfil
), "%s/smtpXXXXXX", *ap
);
485 sd
= mkstemp (sm_tmpfil
);
489 if ((sd
= creat (sm_tmpfil
, 0600)) != NOTOK
) {
502 sm_ierror ("%s", response
);
507 sm_winit (int mode
, char *from
)
512 if (sm_ispool
&& !sm_wfp
) {
513 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
514 sm_reply
.code
= NOTOK
;
537 switch (smtalk (SM_MAIL
, "%s FROM:<%s>", smtpcom
, from
)) {
554 sm_wadr (char *mbox
, char *host
, char *path
)
556 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
558 path
? path
: "", mbox
, host
)) {
568 #endif /* SENDMAILBUG */
593 switch (smtalk (SM_DATA
, "DATA")) {
602 #endif /* SENDMAILBUG */
619 sm_wtxt (char *buffer
, int len
)
625 result
= sm_wstream (buffer
, len
);
628 return (result
== NOTOK
? RP_BHST
: RP_OK
);
635 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
638 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
646 #endif /* SENDMAILBUG */
664 if (sm_mts
== MTS_SENDMAIL
) {
675 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
680 smtalk (SM_QUIT
, "QUIT");
684 sm_note
.code
= sm_reply
.code
;
685 sm_note
.length
= sm_reply
.length
;
686 memcpy (sm_note
.text
, sm_reply
.text
, sm_reply
.length
+ 1);/* fall */
688 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
690 if (sm_mts
== MTS_SMTP
)
691 smtalk (SM_QUIT
, "QUIT");
693 kill (sm_child
, SIGKILL
);
698 sm_reply
.code
= sm_note
.code
;
699 sm_reply
.length
= sm_note
.length
;
700 memcpy (sm_reply
.text
, sm_note
.text
, sm_note
.length
+ 1);
717 if (sm_rfp
!= NULL
) {
722 if (sm_wfp
!= NULL
) {
728 if (sm_mts
== MTS_SMTP
) {
733 #endif /* CYRUS_SASL */
735 status
= pidwait (sm_child
, OK
);
739 sm_rfp
= sm_wfp
= NULL
;
740 return (status
? RP_BHST
: RP_OK
);
749 int cc
, i
, j
, k
, result
;
751 char *dp
, *bp
, *cp
, s
;
752 char buffer
[BUFSIZ
], sender
[BUFSIZ
];
756 k
= strlen (file
) - sizeof(".bulk");
757 if ((fp
= fopen (file
, "r")) == NULL
) {
758 return sm_perror("unable to read %s: ", file
);
761 printf ("reading file %s\n", file
);
766 while (fgets (buffer
, sizeof(buffer
), fp
)) {
768 strncpy (sender
, buffer
+ sizeof("MAIL FROM:") - 1, sizeof(sender
));
769 if (strcmp (buffer
, "DATA\r\n") == 0) {
776 printf ("no DATA...\n");
780 snprintf (buffer
, sizeof(buffer
), "%s.bad", file
);
781 rename (file
, buffer
);
783 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
792 printf ("no %srecipients...\n", j
< 1 ? "sender or " : "");
798 if ((cp
= malloc ((size_t) (cc
= (pos
= ftell (fp
)) + 1))) == NULL
) {
799 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
801 sm_reply
.code
= NOTOK
;
805 fseek (fp
, 0L, SEEK_SET
);
806 for (dp
= cp
, i
= 0; i
++ < j
; dp
+= strlen (dp
))
807 if (fgets (dp
, cc
- (dp
- cp
), fp
) == NULL
) {
808 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "premature eof"));
815 for (dp
= cp
, i
= cc
- 1; i
> 0; dp
+= cc
, i
-= cc
)
816 if ((cc
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
) {
819 sm_perror("error writing to server: ");
824 printf ("wrote %d octets to server\n", cc
);
828 for (dp
= cp
, i
= 0; i
++ < j
; dp
= strchr(dp
, '\n'), dp
++) {
830 if (bp
= strchr(dp
, '\r'))
832 printf ("=> %s\n", dp
);
838 switch (smhear () + (i
== 1 ? 1000 : i
!= j
? 2000 : 3000)) {
850 smtalk (SM_RSET
, "RSET");
878 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0)
882 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
883 if ((gp
= fopen (buffer
, "w+")) == NULL
)
885 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
888 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
889 l
- 4, l
- 4, sender
+ 1, file
);
890 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
891 dtimenow (0), LocalName ());
893 if (bp
= strchr(dp
, '\r'))
895 fprintf (gp
, "=> %s\r\n", dp
);
898 fprintf (gp
, "<= %s\r\n", rp_string (result
));
916 smtalk (SM_RSET
, "RSET");
919 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
930 smtalk (SM_RSET
, "RSET");
942 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
945 if (fstat (fileno (sm_wfp
), &st
) == NOTOK
|| (cc
= st
.st_blksize
) < BUFSIZ
)
950 if ((cp
= malloc ((size_t) cc
)) == NULL
) {
951 smtalk (SM_RSET
, "RSET");
952 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
957 fseek (fp
, pos
, SEEK_SET
);
961 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
962 if ((j
= fread (cp
, sizeof(*cp
), i
, fp
)) == OK
) {
964 sm_perror("error reading %s: ", file
);
972 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
973 if ((j
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
)
977 printf ("wrote %d octets to server\n", j
);
1004 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1015 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0) {
1021 ftruncate (fileno (gp
), 0L);
1022 fseek (gp
, 0L, SEEK_SET
);
1025 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1026 if ((gp
= fopen (buffer
, "w")) == NULL
)
1029 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
1030 i
= strlen (sender
);
1031 fprintf (gp
, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1032 i
- 4, i
- 4, sender
+ 1, file
);
1033 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1034 dtimenow (0), LocalName ());
1039 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp
);
1040 fseek (fp
, pos
, SEEK_SET
);
1041 while (fgets (buffer
, sizeof(buffer
), fp
)) {
1042 if (buffer
[0] == '-')
1044 if (strcmp (buffer
, ".\r\n"))
1047 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp
);
1062 * This function implements SASL authentication for SMTP. If this function
1063 * completes successfully, then authentication is successful and we've
1064 * (optionally) negotiated a security layer.
1066 * Right now we don't support session encryption.
1069 sm_auth_sasl(char *user
, char *mechlist
, char *host
)
1072 unsigned int buflen
, outlen
;
1073 char *buf
, outbuf
[BUFSIZ
];
1074 const char *chosen_mech
;
1075 sasl_security_properties_t secprops
;
1080 * Initialize the callback contexts
1084 user
= getusername();
1086 callbacks
[SM_SASL_N_CB_USER
].context
= user
;
1089 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1090 * to us on the command line, then call getpeername and do a
1091 * reverse-address lookup on the IP address to get the name.
1095 struct sockaddr_in sin
;
1096 int len
= sizeof(sin
);
1099 if (getpeername(fileno(sm_wfp
), (struct sockaddr
*) &sin
, &len
) < 0) {
1100 sm_ierror("getpeername on SMTP socket failed: %s",
1105 if ((hp
= gethostbyaddr((void *) &sin
.sin_addr
, sizeof(sin
.sin_addr
),
1106 sin
.sin_family
)) == NULL
) {
1107 sm_ierror("DNS lookup on IP address %s failed",
1108 inet_ntoa(sin
.sin_addr
));
1112 host
= strdup(hp
->h_name
);
1115 sasl_pw_context
[0] = host
;
1116 sasl_pw_context
[1] = user
;
1118 callbacks
[SM_SASL_N_CB_PASS
].context
= sasl_pw_context
;
1120 result
= sasl_client_init(callbacks
);
1122 if (result
!= SASL_OK
) {
1123 sm_ierror("SASL library initialization failed: %s",
1124 sasl_errstring(result
, NULL
, NULL
));
1128 result
= sasl_client_new("smtp", host
, NULL
, NULL
, NULL
, 0, &conn
);
1130 if (result
!= SASL_OK
) {
1131 sm_ierror("SASL client initialization failed: %s",
1132 sasl_errstring(result
, NULL
, NULL
));
1137 * Initialize the security properties
1140 memset(&secprops
, 0, sizeof(secprops
));
1141 secprops
.maxbufsize
= BUFSIZ
;
1142 secprops
.max_ssf
= 0; /* XXX change this when we do encryption */
1144 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
1146 if (result
!= SASL_OK
) {
1147 sm_ierror("SASL security property initialization failed: %s",
1148 sasl_errstring(result
, NULL
, NULL
));
1153 * Start the actual protocol. Feed the mech list into the library
1154 * and get out a possible initial challenge
1157 result
= sasl_client_start(conn
, mechlist
, NULL
, (const char **) &buf
,
1158 &buflen
, (const char **) &chosen_mech
);
1160 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1161 sm_ierror("SASL client start failed: %s",
1162 sasl_errstring(result
, NULL
, NULL
));
1167 * If we got an initial challenge, send it as part of the AUTH
1168 * command; otherwise, just send a plain AUTH command.
1172 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1173 if (status
!= SASL_OK
) {
1174 sm_ierror("SASL base64 encode failed: %s",
1175 sasl_errstring(status
, NULL
, NULL
));
1179 status
= smtalk(SM_AUTH
, "AUTH %s %s", chosen_mech
, outbuf
);
1181 status
= smtalk(SM_AUTH
, "AUTH %s", chosen_mech
);
1184 * Now we loop until we either fail, get a SASL_OK, or a 235
1185 * response code. Receive the challenges and process them until
1189 while (result
== SASL_CONTINUE
) {
1192 * If we get a 235 response, that means authentication has
1193 * succeeded and we need to break out of the loop (yes, even if
1194 * we still get SASL_CONTINUE from sasl_client_step()).
1196 * Otherwise, if we get a message that doesn't seem to be a
1197 * valid response, then abort
1202 else if (status
< 300 || status
> 399)
1206 * Special case; a zero-length response from the SMTP server
1207 * is returned as a single =. If we get that, then set buflen
1208 * to be zero. Otherwise, just decode the response.
1211 if (strcmp("=", sm_reply
.text
) == 0) {
1214 result
= sasl_decode64(sm_reply
.text
, sm_reply
.length
,
1215 outbuf
, sizeof(outbuf
), &outlen
);
1217 if (result
!= SASL_OK
) {
1218 smtalk(SM_AUTH
, "*");
1219 sm_ierror("SASL base64 decode failed: %s",
1220 sasl_errstring(result
, NULL
, NULL
));
1225 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
,
1226 (const char **) &buf
, &buflen
);
1228 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1229 smtalk(SM_AUTH
, "*");
1230 sm_ierror("SASL client negotiation failed: %s",
1231 sasl_errstring(result
, NULL
, NULL
));
1235 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1237 if (status
!= SASL_OK
) {
1238 smtalk(SM_AUTH
, "*");
1239 sm_ierror("SASL base64 encode failed: %s",
1240 sasl_errstring(status
, NULL
, NULL
));
1244 status
= smtalk(SM_AUTH
, outbuf
);
1248 * Make sure that we got the correct response
1251 if (status
< 200 || status
> 299)
1255 * We _should_ have completed the authentication successfully.
1256 * Get a few properties from the authentication exchange.
1259 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (const void **) &outbufmax
);
1261 if (result
!= SASL_OK
) {
1262 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1263 sasl_errstring(result
, NULL
, NULL
));
1267 maxoutbuf
= *outbufmax
;
1269 result
= sasl_getprop(conn
, SASL_SSF
, (const void **) &ssf
);
1273 if (result
!= SASL_OK
) {
1274 sm_ierror("Cannot retrieve SASL negotiated security strength "
1275 "factor: %s", sasl_errstring(result
, NULL
, NULL
));
1279 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
1288 * Our callback functions to feed data to the SASL library
1292 sm_get_user(void *context
, int id
, const char **result
, unsigned *len
)
1294 char *user
= (char *) context
;
1296 if (! result
|| id
!= SASL_CB_USER
)
1297 return SASL_BADPARAM
;
1301 *len
= strlen(user
);
1307 sm_get_pass(sasl_conn_t
*conn
, void *context
, int id
,
1308 sasl_secret_t
**psecret
)
1310 char **pw_context
= (char **) context
;
1314 if (! psecret
|| id
!= SASL_CB_PASS
)
1315 return SASL_BADPARAM
;
1317 ruserpass(pw_context
[0], &(pw_context
[1]), &pass
);
1321 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
1328 (*psecret
)->len
= len
;
1329 strcpy((char *) (*psecret
)->data
, pass
);
1334 #endif /* CYRUS_SASL */
1337 sm_ierror (char *fmt
, ...)
1342 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
1345 sm_reply
.length
= strlen (sm_reply
.text
);
1346 sm_reply
.code
= NOTOK
;
1353 sm_perror (char *fmt
, ...)
1355 /* Fill in sm_reply with a suitable error string based on errno.
1356 * This isn't particularly MPOP specific, it just happens that that's
1357 * the only code that uses it currently.
1360 int len
, eno
= errno
;
1364 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
1370 if ((s
= strerror(eno
)))
1371 snprintf(bp
, sizeof(sm_reply
.text
) - len
, "%s", s
);
1373 snprintf(bp
, sizeof(sm_reply
.text
) - len
, "unknown error %d", eno
);
1375 sm_reply
.length
= strlen (sm_reply
.text
);
1376 sm_reply
.code
= NOTOK
;
1383 smtalk (int time
, char *fmt
, ...)
1387 char buffer
[BUFSIZ
];
1390 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
1394 printf ("=> %s\n", buffer
);
1402 if (strcmp (buffer
, ".") == 0)
1404 fprintf (sm_wfp
, "%s\r\n", buffer
);
1408 if (ferror (sm_wfp
))
1409 return sm_werror ();
1410 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
1411 (char) (sm_ispool
+ 'a' - 1));
1412 if (rename (sm_tmpfil
, file
) == NOTOK
) {
1413 return sm_perror("error renaming %s to %s: ", sm_tmpfil
, file
);
1416 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
1417 chmod (sm_tmpfil
, 0600);
1428 ftruncate (fileno (sm_wfp
), 0L);
1429 fseek (sm_wfp
, 0L, SEEK_SET
);
1448 printf ("<= %d\n", result
);
1452 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
1453 return (sm_reply
.code
= result
);
1458 alarm ((unsigned) time
);
1459 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
1468 * write the buffer to the open SMTP channel
1472 sm_wrecord (char *buffer
, int len
)
1475 return sm_werror ();
1477 fwrite (buffer
, sizeof(*buffer
), len
, sm_wfp
);
1478 fputs ("\r\n", sm_wfp
);
1481 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1486 sm_wstream (char *buffer
, int len
)
1489 static char lc
= '\0';
1492 return sm_werror ();
1494 if (buffer
== NULL
&& len
== 0) {
1496 fputs ("\r\n", sm_wfp
);
1498 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1501 for (bp
= buffer
; len
> 0; bp
++, len
--) {
1505 fputc ('\r', sm_wfp
);
1510 fputc ('.', sm_wfp
);/* FALL THROUGH */
1514 fputc (*bp
, sm_wfp
);
1515 if (ferror (sm_wfp
))
1516 return sm_werror ();
1521 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1526 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1527 * causes compile problems with the #ifdef MPOP in the middle. Should the
1528 * #ifdef MPOP be removed, remove these #undefs.
1541 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1542 : sm_alarmed
? "write to socket timed out"
1544 : sm_ispool
? "error writing to spool file"
1546 : "error writing to socket"));
1548 return (sm_reply
.code
= NOTOK
);
1555 int i
, code
, cont
, bc
, rc
, more
;
1558 char **ehlo
, buffer
[BUFSIZ
];
1561 static int at_least_once
= 0;
1563 if (at_least_once
) {
1566 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1580 sm_reply
.length
= 0;
1581 sm_reply
.text
[0] = 0;
1583 rc
= sizeof(sm_reply
.text
) - 1;
1585 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
1587 printf ("<= %s\n", buffer
);
1592 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1593 && (buffer
[3] == '-' || doingEHLO
== 2)
1595 if (doingEHLO
== 2) {
1596 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1597 strcpy (*ehlo
++, buffer
+ 4);
1599 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1609 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1615 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1617 if (bc
> 0 && *bp
== '-') {
1620 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1625 if (code
!= sm_reply
.code
|| cont
)
1629 sm_reply
.code
= code
;
1632 /* can never fail to 0-terminate because of size of buffer vs fixed string */
1633 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1635 bc
= strlen (sm_noreply
);
1639 if ((i
= min (bc
, rc
)) > 0) {
1643 i
= strlen(sm_moreply
);
1644 if (more
&& rc
> i
+ 1) {
1645 memcpy (rp
, sm_moreply
, i
); /* safe because of check in if() */
1652 if (sm_reply
.code
< 100) {
1654 printf ("%s\n", sm_reply
.text
);
1660 sm_reply
.length
= rp
- sm_reply
.text
;
1661 sm_reply
.text
[sm_reply
.length
] = 0;
1662 return sm_reply
.code
;
1669 sm_rrecord (char *buffer
, int *len
)
1672 return sm_rerror ();
1674 buffer
[*len
= 0] = 0;
1676 fgets (buffer
, BUFSIZ
, sm_rfp
);
1677 *len
= strlen (buffer
);
1678 /* *len should be >0 except on EOF, but check for safety's sake */
1679 if (ferror (sm_rfp
) || feof (sm_rfp
) || (*len
== 0))
1680 return sm_rerror ();
1681 if (buffer
[*len
- 1] != '\n')
1682 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
1685 if ((*len
> 1) && (buffer
[*len
- 2] == '\r'))
1697 if (sm_mts
== MTS_SMTP
)
1699 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1700 : sm_alarmed
? "read from socket timed out"
1701 : feof (sm_rfp
) ? "premature end-of-file on socket"
1702 : "error reading from socket"));
1705 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no pipe opened"
1706 : sm_alarmed
? "read from pipe timed out"
1707 : feof (sm_rfp
) ? "premature end-of-file on pipe"
1708 : "error reading from pipe"));
1710 return (sm_reply
.code
= NOTOK
);
1717 #ifndef RELIABLE_SIGNALS
1718 SIGNAL (SIGALRM
, alrmser
);
1723 printf ("timed out...\n");
1730 rp_string (int code
)
1733 static char buffer
[BUFSIZ
];
1735 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1755 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1775 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1776 text
, sm_reply
.code
, sm_reply
.text
);
1783 static char *broken
[MAXARGS
+ 1];
1786 smail_brkstring (char *strg
, char *brksep
, char *brkterm
)
1793 for (bi
= 0; bi
< MAXARGS
; bi
++) {
1794 while (smail_brkany (c
= *sp
, brksep
))
1796 if (!c
|| smail_brkany (c
, brkterm
)) {
1803 while ((c
= *++sp
) && !smail_brkany (c
, brksep
) && !smail_brkany (c
, brkterm
))
1806 broken
[MAXARGS
] = 0;
1813 * returns 1 if chr in strg, 0 otherwise
1816 smail_brkany (char chr
, char *strg
)
1821 for (sp
= strg
; *sp
; sp
++)
1828 * copy a string array and return pointer to end
1831 smail_copyip (char **p
, char **q
, int len_q
)
1833 while (*p
&& --len_q
> 0)
1852 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1854 if (strncmp (ep
, s
, len
) == 0) {
1855 for (ep
+= len
; *ep
== ' '; ep
++)