2 * smtp.c -- nmh SMTP interface
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 #include <h/signals.h>
16 #include <sasl/sasl.h>
17 #include <sasl/saslutil.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #endif /* CYRUS_SASL */
26 #include <openssl/ssl.h>
27 #include <openssl/err.h>
28 #endif /* TLS_SUPPORT */
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 #endif /* CYRUS_SASL */
77 static int sm_addrs
= 0;
78 static int sm_alarmed
= 0;
79 static int sm_child
= NOTOK
;
80 static int sm_debug
= 0;
81 static int sm_nl
= TRUE
;
82 static int sm_verbose
= 0;
84 static FILE *sm_rfp
= NULL
;
85 static FILE *sm_wfp
= NULL
;
89 * Some globals needed by SASL
92 static sasl_conn_t
*conn
= NULL
; /* SASL connection state */
93 static int sasl_complete
= 0; /* Has authentication succeded? */
94 static sasl_ssf_t sasl_ssf
; /* Our security strength factor */
95 static char *sasl_pw_context
[2]; /* Context to pass into sm_get_pass */
96 static int maxoutbuf
; /* Maximum crypto output buffer */
97 static char *sasl_outbuffer
; /* SASL output buffer for encryption */
98 static int sasl_outbuflen
; /* Current length of data in outbuf */
99 static int sm_get_user(void *, int, const char **, unsigned *);
100 static int sm_get_pass(sasl_conn_t
*, void *, int, sasl_secret_t
**);
102 static sasl_callback_t callbacks
[] = {
103 { SASL_CB_USER
, sm_get_user
, NULL
},
104 #define SM_SASL_N_CB_USER 0
105 { SASL_CB_PASS
, sm_get_pass
, NULL
},
106 #define SM_SASL_N_CB_PASS 1
107 { SASL_CB_AUTHNAME
, sm_get_user
, NULL
},
108 #define SM_SASL_N_CB_AUTHNAME 2
109 { SASL_CB_LIST_END
, NULL
, NULL
},
112 #else /* CYRUS_SASL */
114 #endif /* CYRUS_SASL */
117 static SSL_CTX
*sslctx
= NULL
;
118 static SSL
*ssl
= NULL
;
119 static BIO
*sbior
= NULL
;
120 static BIO
*sbiow
= NULL
;
121 static BIO
*io
= NULL
;
122 #endif /* TLS_SUPPORT */
124 #if defined(CYRUS_SASL) || defined(TLS_SUPPORT)
125 #define SASL_MAXRECVBUF 65536
126 static int sm_fgetc(FILE *);
127 static char *sasl_inbuffer
; /* SASL input buffer for encryption */
128 static char *sasl_inptr
; /* Pointer to current inbuf position */
129 static int sasl_inbuflen
; /* Current length of data in inbuf */
131 #define sm_fgetc fgetc
134 static int tls_active
= 0;
136 static char *sm_noreply
= "No reply text given";
137 static char *sm_moreply
= "; ";
139 struct smtp sm_reply
; /* global... */
143 static int doingEHLO
;
144 char *EHLOkeys
[MAXEHLO
+ 1];
149 static int smtp_init (char *, char *, char *, int, int, int, int, int, int,
150 int, char *, char *, int);
151 static int sendmail_init (char *, char *, int, int, int, int, int, int,
152 int, char *, char *);
154 static int rclient (char *, char *);
155 static int sm_ierror (char *fmt
, ...);
156 static int smtalk (int time
, char *fmt
, ...);
157 static int sm_wrecord (char *, int);
158 static int sm_wstream (char *, int);
159 static int sm_werror (void);
160 static int smhear (void);
161 static int sm_rrecord (char *, int *);
162 static int sm_rerror (int);
163 static void alrmser (int);
164 static char *EHLOset (char *);
165 static int sm_fwrite(char *, int);
166 static int sm_fputs(char *);
167 static int sm_fputc(int);
168 static void sm_fflush(void);
169 static int sm_fgets(char *, int, FILE *);
173 * Function prototypes needed for SASL
176 static int sm_auth_sasl(char *, int, char *, char *);
177 #endif /* CYRUS_SASL */
180 sm_init (char *client
, char *server
, char *port
, int watch
, int verbose
,
181 int debug
, int onex
, int queued
, int sasl
, int saslssf
,
182 char *saslmech
, char *user
, int tls
)
184 if (sm_mts
== MTS_SMTP
)
185 return smtp_init (client
, server
, port
, watch
, verbose
,
186 debug
, onex
, queued
, sasl
, saslssf
, saslmech
,
189 return sendmail_init (client
, server
, watch
, verbose
,
190 debug
, onex
, queued
, sasl
, saslssf
, saslmech
,
195 smtp_init (char *client
, char *server
, char *port
, int watch
, int verbose
,
196 int debug
, int onex
, int queued
,
197 int sasl
, int saslssf
, char *saslmech
, char *user
, int tls
)
201 #else /* CYRUS_SASL */
203 NMH_UNUSED (saslssf
);
204 NMH_UNUSED (saslmech
);
206 #endif /* CYRUS_SASL */
207 int result
, sd1
, sd2
;
212 sm_verbose
= verbose
;
215 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
218 if (client
== NULL
|| *client
== '\0') {
222 client
= LocalName(1); /* no clientname -> LocalName */
227 * Last-ditch check just in case client still isn't set to anything
230 if (client
== NULL
|| *client
== '\0')
231 client
= "localhost";
233 #if defined(CYRUS_SASL) || defined(TLS_SUPPORT)
234 sasl_inbuffer
= malloc(SASL_MAXRECVBUF
);
236 return sm_ierror("Unable to allocate %d bytes for read buffer",
238 #endif /* CYRUS_SASL || TLS_SUPPORT */
240 if ((sd1
= rclient (server
, port
)) == NOTOK
)
243 if ((sd2
= dup (sd1
)) == NOTOK
) {
245 return sm_ierror ("unable to dup");
248 SIGNAL (SIGALRM
, alrmser
);
249 SIGNAL (SIGPIPE
, SIG_IGN
);
251 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
252 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
255 sm_rfp
= sm_wfp
= NULL
;
256 return sm_ierror ("unable to fdopen");
276 * Give EHLO or HELO command
280 result
= smtalk (SM_HELO
, "EHLO %s", client
);
283 if (result
>= 500 && result
<= 599)
284 result
= smtalk (SM_HELO
, "HELO %s", client
);
293 * If the user requested TLS support, then try to do the STARTTLS command
294 * as part of the initial dialog. Assuming this works, we then need to
295 * restart the EHLO dialog after TLS negotiation is complete.
301 if (! EHLOset("STARTTLS")) {
303 return sm_ierror("SMTP server does not support TLS");
306 result
= smtalk(SM_HELO
, "STARTTLS");
314 * Okay, the other side should be waiting for us to start TLS
315 * negotiation. Oblige them.
319 const SSL_METHOD
*method
;
322 SSL_load_error_strings();
324 method
= TLSv1_client_method(); /* Not sure about this */
326 sslctx
= SSL_CTX_new(method
);
330 return sm_ierror("Unable to initialize OpenSSL context: %s",
331 ERR_error_string(ERR_get_error(), NULL
));
335 ssl
= SSL_new(sslctx
);
339 return sm_ierror("Unable to create SSL connection: %s",
340 ERR_error_string(ERR_get_error(), NULL
));
343 sbior
= BIO_new_socket(fileno(sm_rfp
), BIO_NOCLOSE
);
344 sbiow
= BIO_new_socket(fileno(sm_wfp
), BIO_NOCLOSE
);
346 if (sbior
== NULL
|| sbiow
== NULL
) {
348 return sm_ierror("Unable to create BIO endpoints: %s",
349 ERR_error_string(ERR_get_error(), NULL
));
352 SSL_set_bio(ssl
, sbior
, sbiow
);
353 SSL_set_connect_state(ssl
);
356 * Set up a BIO to handle buffering for us
359 io
= BIO_new(BIO_f_buffer());
363 return sm_ierror("Unable to create a buffer BIO: %s",
364 ERR_error_string(ERR_get_error(), NULL
));
367 ssl_bio
= BIO_new(BIO_f_ssl());
371 return sm_ierror("Unable to create a SSL BIO: %s",
372 ERR_error_string(ERR_get_error(), NULL
));
375 BIO_set_ssl(ssl_bio
, ssl
, BIO_CLOSE
);
376 BIO_push(io
, ssl_bio
);
379 * Try doing the handshake now
382 if (BIO_do_handshake(io
) < 1) {
384 return sm_ierror("Unable to negotiate SSL connection: %s",
385 ERR_error_string(ERR_get_error(), NULL
));
389 const SSL_CIPHER
*cipher
= SSL_get_current_cipher(ssl
);
390 printf("SSL negotiation successful: %s(%d) %s\n",
391 SSL_CIPHER_get_name(cipher
),
392 SSL_CIPHER_get_bits(cipher
, NULL
),
393 SSL_CIPHER_get_version(cipher
));
400 result
= smtalk (SM_HELO
, "EHLO %s", client
);
408 #else /* TLS_SUPPORT */
410 #endif /* TLS_SUPPORT */
414 * If the user asked for SASL, then check to see if the SMTP server
415 * supports it. Otherwise, error out (because the SMTP server
416 * might have been spoofed; we don't want to just silently not
421 if (! (server_mechs
= EHLOset("AUTH"))) {
423 return sm_ierror("SMTP server does not support SASL");
426 if (saslmech
&& stringdex(saslmech
, server_mechs
) == -1) {
428 return sm_ierror("Requested SASL mech \"%s\" is not in the "
429 "list of supported mechanisms:\n%s",
430 saslmech
, server_mechs
);
433 if (sm_auth_sasl(user
, saslssf
, saslmech
? saslmech
: server_mechs
,
439 #endif /* CYRUS_SASL */
442 if (watch
&& EHLOset ("XVRB"))
443 smtalk (SM_HELO
, "VERB on");
444 if (onex
&& EHLOset ("XONE"))
445 smtalk (SM_HELO
, "ONEX");
446 if (queued
&& EHLOset ("XQUE"))
447 smtalk (SM_HELO
, "QUED");
453 sendmail_init (char *client
, char *server
, int watch
, int verbose
,
454 int debug
, int onex
, int queued
,
455 int sasl
, int saslssf
, char *saslmech
, char *user
)
459 #else /* CYRUS_SASL */
462 NMH_UNUSED (saslssf
);
463 NMH_UNUSED (saslmech
);
465 #endif /* CYRUS_SASL */
466 unsigned int i
, result
, vecp
;
473 sm_verbose
= verbose
;
475 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
478 if (client
== NULL
|| *client
== '\0') {
482 client
= LocalName(1); /* no clientname -> LocalName */
486 * Last-ditch check just in case client still isn't set to anything
489 if (client
== NULL
|| *client
== '\0')
490 client
= "localhost";
492 #if defined(CYRUS_SASL) || defined(TLS_SUPPORT)
493 sasl_inbuffer
= malloc(SASL_MAXRECVBUF
);
495 return sm_ierror("Unable to allocate %d bytes for read buffer",
497 #endif /* CYRUS_SASL || TLS_SUPPORT */
499 if (pipe (pdi
) == NOTOK
)
500 return sm_ierror ("no pipes");
501 if (pipe (pdo
) == NOTOK
) {
504 return sm_ierror ("no pipes");
507 for (i
= 0; (sm_child
= fork ()) == NOTOK
&& i
< 5; i
++)
516 return sm_ierror ("unable to fork");
519 if (pdo
[0] != fileno (stdin
))
520 dup2 (pdo
[0], fileno (stdin
));
521 if (pdi
[1] != fileno (stdout
))
522 dup2 (pdi
[1], fileno (stdout
));
523 if (pdi
[1] != fileno (stderr
))
524 dup2 (pdi
[1], fileno (stderr
));
525 for (i
= fileno (stderr
) + 1; i
< NBITS
; i
++)
529 vec
[vecp
++] = r1bindex (sendmail
, '/');
531 vec
[vecp
++] = watch
? "-odi" : queued
? "-odq" : "-odb";
532 vec
[vecp
++] = "-oem";
537 # endif /* not RAND */
542 execvp (sendmail
, vec
);
543 fprintf (stderr
, "unable to exec ");
545 _exit (-1); /* NOTREACHED */
548 SIGNAL (SIGALRM
, alrmser
);
549 SIGNAL (SIGPIPE
, SIG_IGN
);
553 if ((sm_rfp
= fdopen (pdi
[0], "r")) == NULL
554 || (sm_wfp
= fdopen (pdo
[1], "w")) == NULL
) {
557 sm_rfp
= sm_wfp
= NULL
;
558 return sm_ierror ("unable to fdopen");
574 result
= smtalk (SM_HELO
, "EHLO %s", client
);
577 if (500 <= result
&& result
<= 599)
578 result
= smtalk (SM_HELO
, "HELO %s", client
);
591 * If the user asked for SASL, then check to see if the SMTP server
592 * supports it. Otherwise, error out (because the SMTP server
593 * might have been spoofed; we don't want to just silently not
598 if (! (server_mechs
= EHLOset("AUTH"))) {
600 return sm_ierror("SMTP server does not support SASL");
603 if (saslmech
&& stringdex(saslmech
, server_mechs
) == -1) {
605 return sm_ierror("Requested SASL mech \"%s\" is not in the "
606 "list of supported mechanisms:\n%s",
607 saslmech
, server_mechs
);
610 if (sm_auth_sasl(user
, saslssf
, saslmech
? saslmech
: server_mechs
,
616 #endif /* CYRUS_SASL */
619 smtalk (SM_HELO
, "ONEX");
621 smtalk (SM_HELO
, "VERB on");
628 rclient (char *server
, char *service
)
631 char response
[BUFSIZ
];
633 if ((sd
= client (server
, service
, response
, sizeof(response
),
637 sm_ierror ("%s", response
);
642 sm_winit (char *from
)
644 switch (smtalk (SM_MAIL
, "MAIL FROM:<%s>", from
)) {
661 sm_wadr (char *mbox
, char *host
, char *path
)
663 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
665 path
? path
: "", mbox
, host
)) {
675 #endif /* SENDMAILBUG */
700 switch (smtalk (SM_DATA
, "DATA")) {
709 #endif /* SENDMAILBUG */
726 sm_wtxt (char *buffer
, int len
)
732 result
= sm_wstream (buffer
, len
);
735 return (result
== NOTOK
? RP_BHST
: RP_OK
);
742 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
745 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
753 #endif /* SENDMAILBUG */
771 if (sm_mts
== MTS_SENDMAIL
) {
782 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
787 smtalk (SM_QUIT
, "QUIT");
791 sm_note
.code
= sm_reply
.code
;
792 sm_note
.length
= sm_reply
.length
;
793 memcpy (sm_note
.text
, sm_reply
.text
, sm_reply
.length
+ 1);/* fall */
795 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
797 if (sm_mts
== MTS_SMTP
)
798 smtalk (SM_QUIT
, "QUIT");
800 kill (sm_child
, SIGKILL
);
805 sm_reply
.code
= sm_note
.code
;
806 sm_reply
.length
= sm_note
.length
;
807 memcpy (sm_reply
.text
, sm_note
.text
, sm_note
.length
+ 1);
814 BIO_ssl_shutdown(io
);
817 #endif /* TLS_SUPPORT */
819 if (sm_rfp
!= NULL
) {
824 if (sm_wfp
!= NULL
) {
830 if (sm_mts
== MTS_SMTP
) {
835 if (sasl_outbuffer
) {
836 free(sasl_outbuffer
);
841 #endif /* CYRUS_SASL */
843 status
= pidwait (sm_child
, OK
);
847 sm_rfp
= sm_wfp
= NULL
;
848 return (status
? RP_BHST
: RP_OK
);
853 * This function implements SASL authentication for SMTP. If this function
854 * completes successfully, then authentication is successful and we've
855 * (optionally) negotiated a security layer.
858 sm_auth_sasl(char *user
, int saslssf
, char *mechlist
, char *inhost
)
861 unsigned int buflen
, outlen
;
862 char *buf
, outbuf
[BUFSIZ
], host
[NI_MAXHOST
];
863 const char *chosen_mech
;
864 sasl_security_properties_t secprops
;
869 * Initialize the callback contexts
873 user
= getusername();
875 callbacks
[SM_SASL_N_CB_USER
].context
= user
;
876 callbacks
[SM_SASL_N_CB_AUTHNAME
].context
= user
;
879 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
880 * to us on the command line, then call getpeername and do a
881 * reverse-address lookup on the IP address to get the name.
884 memset(host
, 0, sizeof(host
));
887 struct sockaddr_storage sin
;
888 socklen_t len
= sizeof(sin
);
891 if (getpeername(fileno(sm_wfp
), (struct sockaddr
*) &sin
, &len
) < 0) {
892 sm_ierror("getpeername on SMTP socket failed: %s",
897 result
= getnameinfo((struct sockaddr
*) &sin
, len
, host
, sizeof(host
),
898 NULL
, 0, NI_NAMEREQD
);
900 sm_ierror("Unable to look up name of connected host: %s",
901 gai_strerror(result
));
905 strncpy(host
, inhost
, sizeof(host
) - 1);
908 sasl_pw_context
[0] = host
;
909 sasl_pw_context
[1] = user
;
911 callbacks
[SM_SASL_N_CB_PASS
].context
= sasl_pw_context
;
913 result
= sasl_client_init(callbacks
);
915 if (result
!= SASL_OK
) {
916 sm_ierror("SASL library initialization failed: %s",
917 sasl_errstring(result
, NULL
, NULL
));
921 result
= sasl_client_new("smtp", host
, NULL
, NULL
, NULL
, 0, &conn
);
923 if (result
!= SASL_OK
) {
924 sm_ierror("SASL client initialization failed: %s",
925 sasl_errstring(result
, NULL
, NULL
));
930 * Initialize the security properties. But if TLS is active, then
931 * don't negotiate encryption here.
934 memset(&secprops
, 0, sizeof(secprops
));
935 secprops
.maxbufsize
= SASL_MAXRECVBUF
;
937 tls_active
? 0 : (saslssf
!= -1 ? (unsigned int) saslssf
: UINT_MAX
);
939 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
941 if (result
!= SASL_OK
) {
942 sm_ierror("SASL security property initialization failed: %s",
943 sasl_errstring(result
, NULL
, NULL
));
948 * Start the actual protocol. Feed the mech list into the library
949 * and get out a possible initial challenge
952 result
= sasl_client_start(conn
, mechlist
, NULL
, (const char **) &buf
,
953 &buflen
, (const char **) &chosen_mech
);
955 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
956 sm_ierror("SASL client start failed: %s", sasl_errdetail(conn
));
961 * If we got an initial challenge, send it as part of the AUTH
962 * command; otherwise, just send a plain AUTH command.
966 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
967 if (status
!= SASL_OK
) {
968 sm_ierror("SASL base64 encode failed: %s",
969 sasl_errstring(status
, NULL
, NULL
));
973 status
= smtalk(SM_AUTH
, "AUTH %s %s", chosen_mech
, outbuf
);
975 status
= smtalk(SM_AUTH
, "AUTH %s", chosen_mech
);
978 * Now we loop until we either fail, get a SASL_OK, or a 235
979 * response code. Receive the challenges and process them until
983 while (result
== SASL_CONTINUE
) {
986 * If we get a 235 response, that means authentication has
987 * succeeded and we need to break out of the loop (yes, even if
988 * we still get SASL_CONTINUE from sasl_client_step()).
990 * Otherwise, if we get a message that doesn't seem to be a
991 * valid response, then abort
996 else if (status
< 300 || status
> 399)
1000 * Special case; a zero-length response from the SMTP server
1001 * is returned as a single =. If we get that, then set buflen
1002 * to be zero. Otherwise, just decode the response.
1005 if (strcmp("=", sm_reply
.text
) == 0) {
1008 result
= sasl_decode64(sm_reply
.text
, sm_reply
.length
,
1009 outbuf
, sizeof(outbuf
), &outlen
);
1011 if (result
!= SASL_OK
) {
1012 smtalk(SM_AUTH
, "*");
1013 sm_ierror("SASL base64 decode failed: %s",
1014 sasl_errstring(result
, NULL
, NULL
));
1019 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
,
1020 (const char **) &buf
, &buflen
);
1022 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1023 smtalk(SM_AUTH
, "*");
1024 sm_ierror("SASL client negotiation failed: %s",
1025 sasl_errstring(result
, NULL
, NULL
));
1029 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1031 if (status
!= SASL_OK
) {
1032 smtalk(SM_AUTH
, "*");
1033 sm_ierror("SASL base64 encode failed: %s",
1034 sasl_errstring(status
, NULL
, NULL
));
1038 status
= smtalk(SM_AUTH
, outbuf
);
1042 * Make sure that we got the correct response
1045 if (status
< 200 || status
> 299)
1049 * We _should_ have completed the authentication successfully.
1050 * Get a few properties from the authentication exchange.
1053 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (const void **) &outbufmax
);
1055 if (result
!= SASL_OK
) {
1056 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1057 sasl_errstring(result
, NULL
, NULL
));
1061 maxoutbuf
= *outbufmax
;
1063 result
= sasl_getprop(conn
, SASL_SSF
, (const void **) &ssf
);
1067 if (result
!= SASL_OK
) {
1068 sm_ierror("Cannot retrieve SASL negotiated security strength "
1069 "factor: %s", sasl_errstring(result
, NULL
, NULL
));
1074 sasl_outbuffer
= malloc(maxoutbuf
);
1076 if (sasl_outbuffer
== NULL
) {
1077 sm_ierror("Unable to allocate %d bytes for SASL output "
1078 "buffer", maxoutbuf
);
1083 sasl_inptr
= sasl_inbuffer
;
1085 sasl_outbuffer
= NULL
;
1086 /* Don't NULL out sasl_inbuffer because it could be used in
1096 * Our callback functions to feed data to the SASL library
1100 sm_get_user(void *context
, int id
, const char **result
, unsigned *len
)
1102 char *user
= (char *) context
;
1104 if (! result
|| ((id
!= SASL_CB_USER
) && (id
!= SASL_CB_AUTHNAME
)))
1105 return SASL_BADPARAM
;
1109 *len
= strlen(user
);
1115 sm_get_pass(sasl_conn_t
*conn
, void *context
, int id
,
1116 sasl_secret_t
**psecret
)
1120 char **pw_context
= (char **) context
;
1124 if (! psecret
|| id
!= SASL_CB_PASS
)
1125 return SASL_BADPARAM
;
1127 ruserpass(pw_context
[0], &(pw_context
[1]), &pass
);
1131 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
1138 (*psecret
)->len
= len
;
1139 strcpy((char *) (*psecret
)->data
, pass
);
1144 #endif /* CYRUS_SASL */
1147 sm_ierror (char *fmt
, ...)
1152 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
1155 sm_reply
.length
= strlen (sm_reply
.text
);
1156 sm_reply
.code
= NOTOK
;
1162 smtalk (int time
, char *fmt
, ...)
1166 char buffer
[BUFSIZ
];
1169 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
1174 printf("(sasl-encrypted) ");
1176 printf("(tls-encrypted) ");
1177 printf ("=> %s\n", buffer
);
1182 alarm ((unsigned) time
);
1183 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
1192 * write the buffer to the open SMTP channel
1196 sm_wrecord (char *buffer
, int len
)
1199 return sm_werror ();
1201 sm_fwrite (buffer
, len
);
1205 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1210 sm_wstream (char *buffer
, int len
)
1213 static char lc
= '\0';
1216 return sm_werror ();
1218 if (buffer
== NULL
&& len
== 0) {
1222 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1225 for (bp
= buffer
; len
> 0; bp
++, len
--) {
1234 sm_fputc ('.');/* FALL THROUGH */
1239 if (ferror (sm_wfp
))
1240 return sm_werror ();
1245 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1249 * Write out to the network, but do buffering for SASL (if enabled)
1253 sm_fwrite(char *buffer
, int len
)
1257 unsigned int outputlen
;
1259 if (sasl_complete
== 0 || sasl_ssf
== 0) {
1260 #endif /* CYRUS_SASL */
1265 ret
= BIO_write(io
, buffer
, len
);
1268 sm_ierror("TLS error during write: %s",
1269 ERR_error_string(ERR_get_error(), NULL
));
1273 #endif /* TLS_SUPPORT */
1274 fwrite(buffer
, sizeof(*buffer
), len
, sm_wfp
);
1277 while (len
>= maxoutbuf
- sasl_outbuflen
) {
1278 memcpy(sasl_outbuffer
+ sasl_outbuflen
, buffer
,
1279 maxoutbuf
- sasl_outbuflen
);
1280 len
-= maxoutbuf
- sasl_outbuflen
;
1283 if (sasl_encode(conn
, sasl_outbuffer
, maxoutbuf
,
1284 &output
, &outputlen
) != SASL_OK
) {
1285 sm_ierror("Unable to SASL encode connection data: %s",
1286 sasl_errdetail(conn
));
1290 fwrite(output
, sizeof(*output
), outputlen
, sm_wfp
);
1294 memcpy(sasl_outbuffer
+ sasl_outbuflen
, buffer
, len
);
1295 sasl_outbuflen
+= len
;
1298 #endif /* CYRUS_SASL */
1299 return ferror(sm_wfp
) ? NOTOK
: RP_OK
;
1303 * Convenience functions to replace occurences of fputs() and fputc()
1307 sm_fputs(char *buffer
)
1309 return sm_fwrite(buffer
, strlen(buffer
));
1317 return sm_fwrite(&h
, 1);
1321 * Flush out any pending data on the connection
1329 unsigned int outputlen
;
1332 if (sasl_complete
== 1 && sasl_ssf
> 0 && sasl_outbuflen
> 0) {
1333 result
= sasl_encode(conn
, sasl_outbuffer
, sasl_outbuflen
,
1334 &output
, &outputlen
);
1335 if (result
!= SASL_OK
) {
1336 sm_ierror("Unable to SASL encode connection data: %s",
1337 sasl_errdetail(conn
));
1341 fwrite(output
, sizeof(*output
), outputlen
, sm_wfp
);
1344 #endif /* CYRUS_SASL */
1348 (void) BIO_flush(io
);
1350 #endif /* TLS_SUPPORT */
1359 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1360 : sm_alarmed
? "write to socket timed out"
1361 : "error writing to socket"));
1363 return (sm_reply
.code
= NOTOK
);
1370 int i
, code
, cont
, bc
= 0, rc
, more
;
1373 char **ehlo
= NULL
, buffer
[BUFSIZ
];
1376 static int at_least_once
= 0;
1378 if (at_least_once
) {
1381 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1395 sm_reply
.length
= 0;
1396 sm_reply
.text
[0] = 0;
1398 rc
= sizeof(sm_reply
.text
) - 1;
1400 for (more
= FALSE
; sm_rrecord ((char *) (bp
= (unsigned char *) buffer
),
1404 printf("(sasl-decrypted) ");
1406 printf("(tls-decrypted) ");
1407 printf ("<= %s\n", buffer
);
1412 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1413 && (buffer
[3] == '-' || doingEHLO
== 2)
1415 if (doingEHLO
== 2) {
1416 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1417 strcpy (*ehlo
++, buffer
+ 4);
1419 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1429 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1433 code
= atoi ((char *) bp
);
1435 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1437 if (bc
> 0 && *bp
== '-') {
1440 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1445 if (code
!= sm_reply
.code
|| cont
)
1449 sm_reply
.code
= code
;
1452 /* can never fail to 0-terminate because of size of buffer vs fixed string */
1453 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1454 bp
= (unsigned char *) buffer
;
1455 bc
= strlen (sm_noreply
);
1459 if ((i
= min (bc
, rc
)) > 0) {
1463 i
= strlen(sm_moreply
);
1464 if (more
&& rc
> i
+ 1) {
1465 memcpy (rp
, sm_moreply
, i
); /* safe because of check in if() */
1472 if (sm_reply
.code
< 100) {
1474 printf ("%s\n", sm_reply
.text
);
1480 sm_reply
.length
= rp
- sm_reply
.text
;
1481 sm_reply
.text
[sm_reply
.length
] = 0;
1482 return sm_reply
.code
;
1489 sm_rrecord (char *buffer
, int *len
)
1494 return sm_rerror(0);
1496 buffer
[*len
= 0] = 0;
1498 if ((retval
= sm_fgets (buffer
, BUFSIZ
, sm_rfp
)) != RP_OK
)
1499 return sm_rerror (retval
);
1500 *len
= strlen (buffer
);
1501 /* *len should be >0 except on EOF, but check for safety's sake */
1503 return sm_rerror (RP_EOF
);
1504 if (buffer
[*len
- 1] != '\n')
1505 while ((retval
= sm_fgetc (sm_rfp
)) != '\n' && retval
!= EOF
&&
1509 if ((*len
> 1) && (buffer
[*len
- 2] == '\r'))
1518 * Our version of fgets, which calls our private fgetc function
1522 sm_fgets(char *buffer
, int size
, FILE *f
)
1536 } while (size
> 1 && c
!= '\n');
1544 #if defined(CYRUS_SASL) || defined(TLS_SUPPORT)
1546 * Read from the network, but do SASL or TLS encryption
1552 char tmpbuf
[BUFSIZ
], *retbuf
;
1553 unsigned int retbufsize
= 0;
1557 * If we have leftover data, return it
1560 if (sasl_inbuflen
) {
1562 return (int) *sasl_inptr
++;
1566 * If not, read from the network until we have some data to return
1569 while (retbufsize
== 0) {
1573 cc
= SSL_read(ssl
, tmpbuf
, sizeof(tmpbuf
));
1576 result
= SSL_get_error(ssl
, cc
);
1578 if (result
!= SSL_ERROR_ZERO_RETURN
) {
1579 sm_ierror("TLS peer aborted connection");
1586 sm_ierror("SSL_read failed: %s",
1587 ERR_error_string(ERR_get_error(), NULL
));
1591 #endif /* TLS_SUPPORT */
1593 cc
= read(fileno(f
), tmpbuf
, sizeof(tmpbuf
));
1599 sm_ierror("Unable to read from network: %s", strerror(errno
));
1604 * Don't call sasl_decode unless sasl is complete and we have
1605 * encryption working
1609 if (sasl_complete
== 0 || sasl_ssf
== 0) {
1613 result
= sasl_decode(conn
, tmpbuf
, cc
, (const char **) &retbuf
,
1616 if (result
!= SASL_OK
) {
1617 sm_ierror("Unable to decode SASL network data: %s",
1618 sasl_errdetail(conn
));
1622 #else /* ! CYRUS_SASL */
1625 #endif /* CYRUS_SASL */
1628 if (retbufsize
> SASL_MAXRECVBUF
) {
1629 sm_ierror("Received data (%d bytes) is larger than the buffer "
1630 "size (%d bytes)", retbufsize
, SASL_MAXRECVBUF
);
1634 memcpy(sasl_inbuffer
, retbuf
, retbufsize
);
1635 sasl_inptr
= sasl_inbuffer
+ 1;
1636 sasl_inbuflen
= retbufsize
- 1;
1638 return (int) sasl_inbuffer
[0];
1640 #endif /* CYRUS_SASL || TLS_SUPPORT */
1645 if (sm_mts
== MTS_SMTP
)
1647 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1648 : sm_alarmed
? "read from socket timed out"
1649 : rc
== RP_EOF
? "premature end-of-file on socket"
1650 : "error reading from socket"));
1653 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no pipe opened"
1654 : sm_alarmed
? "read from pipe timed out"
1655 : rc
== RP_EOF
? "premature end-of-file on pipe"
1656 : "error reading from pipe"));
1658 return (sm_reply
.code
= NOTOK
);
1667 #ifndef RELIABLE_SIGNALS
1668 SIGNAL (SIGALRM
, alrmser
);
1673 printf ("timed out...\n");
1680 rp_string (int code
)
1683 static char buffer
[BUFSIZ
];
1685 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1705 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1725 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1726 text
, sm_reply
.code
, sm_reply
.text
);
1738 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1740 if (strncmp (ep
, s
, len
) == 0) {
1741 for (ep
+= len
; *ep
== ' '; ep
++)