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>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
27 #endif /* CYRUS_SASL */
30 * This module implements an interface to SendMail very similar
31 * to the MMDF mm_(3) routines. The sm_() routines herein talk
32 * SMTP to a sendmail process, mapping SMTP reply codes into
37 * On older 4.2BSD machines without the POSIX function `sigaction',
38 * the alarm handing stuff for time-outs will NOT work due to the way
39 * syscalls get restarted. This is not really crucial, since SendMail
40 * is generally well-behaved in this area.
45 * It appears that some versions of Sendmail will return Code 451
46 * when they don't really want to indicate a failure.
47 * "Code 451 almost always means sendmail has deferred; we don't
48 * really want bomb out at this point since sendmail will rectify
49 * things later." So, if you define SENDMAILBUG, Code 451 is
50 * considered the same as Code 250. Yuck!
57 #define NBITS ((sizeof (int)) * 8)
60 * these codes must all be different!
62 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
74 static int sm_addrs
= 0;
75 static int sm_alarmed
= 0;
76 static int sm_child
= NOTOK
;
77 static int sm_debug
= 0;
78 static int sm_nl
= TRUE
;
79 static int sm_verbose
= 0;
81 static FILE *sm_rfp
= NULL
;
82 static FILE *sm_wfp
= NULL
;
85 static int sm_ispool
= 0;
86 static char sm_tmpfil
[BUFSIZ
];
91 * Some globals needed by SASL
94 static sasl_conn_t
*conn
= NULL
; /* SASL connection state */
95 static int sasl_complete
= 0; /* Has authentication succeded? */
96 static sasl_ssf_t sasl_ssf
; /* Our security strength factor */
97 static char *sasl_pw_context
[2]; /* Context to pass into sm_get_pass */
98 static int maxoutbuf
; /* Maximum crypto output buffer */
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_LIST_END
, NULL
, NULL
},
109 #endif /* CYRUS_SASL */
111 static char *sm_noreply
= "No reply text given";
112 static char *sm_moreply
= "; ";
114 struct smtp sm_reply
; /* global... */
118 static int doingEHLO
;
119 char *EHLOkeys
[MAXEHLO
+ 1];
124 static int smtp_init (char *, char *, int, int, int, int, int, int,
126 static int sendmail_init (char *, char *, int, int, int, int, int);
128 static int rclient (char *, char *, char *);
129 static int sm_ierror (char *fmt
, ...);
130 static int smtalk (int time
, char *fmt
, ...);
131 static int sm_wrecord (char *, int);
132 static int sm_wstream (char *, int);
133 static int sm_werror (void);
134 static int smhear (void);
135 static int sm_rrecord (char *, int *);
136 static int sm_rerror (void);
137 static RETSIGTYPE
alrmser (int);
138 static char *EHLOset (char *);
142 * smtp.c's own static copy of several nmh library subroutines
144 static char **smail_brkstring (char *, char *, char *);
145 static int smail_brkany (char, char *);
146 char **smail_copyip (char **, char **, int);
151 * Function prototypes needed for SASL
154 static int sm_auth_sasl(char *, char *, char *);
155 #endif /* CYRUS_SASL */
157 /* from mts/generic/client.c */
158 int client (char *, char *, char *, int, char *, int);
161 sm_init (char *client
, char *server
, int watch
, int verbose
,
162 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
165 if (sm_mts
== MTS_SMTP
)
166 return smtp_init (client
, server
, watch
, verbose
,
167 debug
, onex
, queued
, sasl
, saslmech
, user
);
169 return sendmail_init (client
, server
, watch
, verbose
,
170 debug
, onex
, queued
);
174 smtp_init (char *client
, char *server
, int watch
, int verbose
,
175 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
180 #endif /* CYRUS_SASL */
181 int result
, sd1
, sd2
;
186 sm_verbose
= verbose
;
194 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
197 if (client
== NULL
|| *client
== '\0') {
201 client
= LocalName(); /* no clientname -> LocalName */
206 if (client
== NULL
|| *client
== '\0')
207 client
= "localhost";
210 if ((sd1
= rclient (server
, "tcp", "smtp")) == NOTOK
)
221 if ((sm_wfp
= fdopen (sd1
, "w")) == NULL
) {
224 return sm_ierror ("unable to fdopen");
227 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
228 return (sm_reply
.code
= RP_OK
);
232 if ((sd2
= dup (sd1
)) == NOTOK
) {
234 return sm_ierror ("unable to dup");
237 SIGNAL (SIGALRM
, alrmser
);
238 SIGNAL (SIGPIPE
, SIG_IGN
);
240 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
241 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
244 sm_rfp
= sm_wfp
= NULL
;
245 return sm_ierror ("unable to fdopen");
263 * Give EHLO or HELO command
265 if (client
&& *client
) {
267 result
= smtalk (SM_HELO
, "EHLO %s", client
);
270 if (result
>= 500 && result
<= 599)
271 result
= smtalk (SM_HELO
, "HELO %s", client
);
281 * If the user asked for SASL, then check to see if the SMTP server
282 * supports it. Otherwise, error out (because the SMTP server
283 * might have been spoofed; we don't want to just silently not
288 if (! (server_mechs
= EHLOset("AUTH"))) {
290 return sm_ierror("SMTP server does not support SASL");
293 if (saslmech
&& stringdex(saslmech
, server_mechs
) == -1) {
295 return sm_ierror("Requested SASL mech \"%s\" is not in the "
296 "list of supported mechanisms:\n%s",
297 saslmech
, server_mechs
);
300 if (sm_auth_sasl(user
, saslmech
? saslmech
: server_mechs
,
306 #endif /* CYRUS_SASL */
309 if (watch
&& EHLOset ("XVRB"))
310 smtalk (SM_HELO
, "VERB on");
311 if (onex
&& EHLOset ("XONE"))
312 smtalk (SM_HELO
, "ONEX");
313 if (queued
&& EHLOset ("XQUE"))
314 smtalk (SM_HELO
, "QUED");
320 sendmail_init (char *client
, char *server
, int watch
, int verbose
,
321 int debug
, int onex
, int queued
)
330 sm_verbose
= verbose
;
332 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
335 if (client
== NULL
|| *client
== '\0') {
339 client
= LocalName(); /* no clientname -> LocalName */
343 if (client
== NULL
|| *client
== '\0')
344 client
= "localhost";
347 if (pipe (pdi
) == NOTOK
)
348 return sm_ierror ("no pipes");
349 if (pipe (pdo
) == NOTOK
) {
352 return sm_ierror ("no pipes");
355 for (i
= 0; (sm_child
= fork ()) == NOTOK
&& i
< 5; i
++)
364 return sm_ierror ("unable to fork");
367 if (pdo
[0] != fileno (stdin
))
368 dup2 (pdo
[0], fileno (stdin
));
369 if (pdi
[1] != fileno (stdout
))
370 dup2 (pdi
[1], fileno (stdout
));
371 if (pdi
[1] != fileno (stderr
))
372 dup2 (pdi
[1], fileno (stderr
));
373 for (i
= fileno (stderr
) + 1; i
< NBITS
; i
++)
377 vec
[vecp
++] = r1bindex (sendmail
, '/');
380 vec
[vecp
++] = watch
? "-odi" : queued
? "-odq" : "-odb";
381 vec
[vecp
++] = "-oem";
386 # endif /* not RAND */
387 #endif /* not ZMAILER */
392 execvp (sendmail
, vec
);
393 fprintf (stderr
, "unable to exec ");
395 _exit (-1); /* NOTREACHED */
398 SIGNAL (SIGALRM
, alrmser
);
399 SIGNAL (SIGPIPE
, SIG_IGN
);
403 if ((sm_rfp
= fdopen (pdi
[0], "r")) == NULL
404 || (sm_wfp
= fdopen (pdo
[1], "w")) == NULL
) {
407 sm_rfp
= sm_wfp
= NULL
;
408 return sm_ierror ("unable to fdopen");
423 if (client
&& *client
) {
425 result
= smtalk (SM_HELO
, "EHLO %s", client
);
428 if (500 <= result
&& result
<= 599)
429 result
= smtalk (SM_HELO
, "HELO %s", client
);
443 smtalk (SM_HELO
, "ONEX");
446 smtalk (SM_HELO
, "VERB on");
453 # define MAXARGS 1000
457 rclient (char *server
, char *protocol
, char *service
)
460 char response
[BUFSIZ
];
465 if ((sd
= client (server
, protocol
, service
, FALSE
, response
, sizeof(response
))) != NOTOK
)
469 if (!server
&& servers
&& (cp
= strchr(servers
, '/'))) {
471 char *arguments
[MAXARGS
];
473 smail_copyip (smail_brkstring (cp
= getcpy (servers
), " ", "\n"), arguments
, MAXARGS
);
475 for (ap
= arguments
; *ap
; ap
++)
479 if ((dp
= strrchr(*ap
, '/')) && *++dp
== NULL
)
481 snprintf (sm_tmpfil
, sizeof(sm_tmpfil
), "%s/smtpXXXXXX", *ap
);
483 sd
= mkstemp (sm_tmpfil
);
487 if ((sd
= creat (sm_tmpfil
, 0600)) != NOTOK
) {
500 sm_ierror ("%s", response
);
506 #include <sys/socket.h>
507 #include <netinet/in.h>
508 #include <arpa/inet.h>
511 #endif /* CYRUS_SASL */
514 sm_winit (int mode
, char *from
)
519 if (sm_ispool
&& !sm_wfp
) {
520 strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
521 sm_reply
.code
= NOTOK
;
544 switch (smtalk (SM_MAIL
, "%s FROM:<%s>", smtpcom
, from
)) {
561 sm_wadr (char *mbox
, char *host
, char *path
)
563 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
565 path
? path
: "", mbox
, host
)) {
575 #endif /* SENDMAILBUG */
600 switch (smtalk (SM_DATA
, "DATA")) {
609 #endif /* SENDMAILBUG */
626 sm_wtxt (char *buffer
, int len
)
632 result
= sm_wstream (buffer
, len
);
635 return (result
== NOTOK
? RP_BHST
: RP_OK
);
642 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
645 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
653 #endif /* SENDMAILBUG */
671 if (sm_mts
== MTS_SENDMAIL
) {
682 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
687 smtalk (SM_QUIT
, "QUIT");
691 sm_note
.code
= sm_reply
.code
;
692 strncpy (sm_note
.text
, sm_reply
.text
, sm_note
.length
= sm_reply
.length
);/* fall */
694 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
696 if (sm_mts
== MTS_SMTP
)
697 smtalk (SM_QUIT
, "QUIT");
699 kill (sm_child
, SIGKILL
);
704 sm_reply
.code
= sm_note
.code
;
705 strncpy (sm_reply
.text
, sm_note
.text
, sm_reply
.length
= sm_note
.length
);
722 if (sm_rfp
!= NULL
) {
727 if (sm_wfp
!= NULL
) {
733 if (sm_mts
== MTS_SMTP
) {
738 #endif /* CYRUS_SASL */
740 status
= pidwait (sm_child
, OK
);
744 sm_rfp
= sm_wfp
= NULL
;
745 return (status
? RP_BHST
: RP_OK
);
754 int cc
, i
, j
, k
, result
;
756 char *dp
, *bp
, *cp
, s
;
757 char buffer
[BUFSIZ
], sender
[BUFSIZ
];
761 k
= strlen (file
) - sizeof(".bulk");
762 if ((fp
= fopen (file
, "r")) == NULL
) {
765 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
766 "unable to read %s: ", file
);
770 if ((s
= strerror (errno
)))
771 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
773 snprintf (bp
, sizeof(sm_reply
.text
) - len
, "Error %d", errno
);
774 sm_reply
.length
= strlen (sm_reply
.text
);
775 sm_reply
.code
= NOTOK
;
779 printf ("reading file %s\n", file
);
784 while (fgets (buffer
, sizeof(buffer
), fp
)) {
786 strncpy (sender
, buffer
+ sizeof("MAIL FROM:") - 1, sizeof(sender
));
787 if (strcmp (buffer
, "DATA\r\n") == 0) {
794 printf ("no DATA...\n");
798 snprintf (buffer
, sizeof(buffer
), "%s.bad", file
);
799 rename (file
, buffer
);
801 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
810 printf ("no %srecipients...\n", j
< 1 ? "sender or " : "");
816 if ((cp
= malloc ((size_t) (cc
= (pos
= ftell (fp
)) + 1))) == NULL
) {
817 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
819 sm_reply
.code
= NOTOK
;
823 fseek (fp
, 0L, SEEK_SET
);
824 for (dp
= cp
, i
= 0; i
++ < j
; dp
+= strlen (dp
))
825 if (fgets (dp
, cc
- (dp
- cp
), fp
) == NULL
) {
826 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "premature eof"));
833 for (dp
= cp
, i
= cc
- 1; i
> 0; dp
+= cc
, i
-= cc
)
834 if ((cc
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
) {
837 strcpy (sm_reply
.text
, "error writing to server: ",
838 sizeof(sm_reply
.text
));
842 if ((s
= strerror (errno
)))
843 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
845 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
846 "unknown error %d", errno
);
847 sm_reply
.length
= strlen (sm_reply
.text
);
852 printf ("wrote %d octets to server\n", cc
);
856 for (dp
= cp
, i
= 0; i
++ < j
; dp
= strchr(dp
, '\n'), dp
++) {
858 if (bp
= strchr(dp
, '\r'))
860 printf ("=> %s\n", dp
);
866 switch (smhear () + (i
== 1 ? 1000 : i
!= j
? 2000 : 3000)) {
878 smtalk (SM_RSET
, "RSET");
906 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0)
910 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
911 if ((gp
= fopen (buffer
, "w+")) == NULL
)
913 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
916 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
917 l
- 4, l
- 4, sender
+ 1, file
);
918 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
919 dtimenow (0), LocalName ());
921 if (bp
= strchr(dp
, '\r'))
923 fprintf (gp
, "=> %s\r\n", dp
);
926 fprintf (gp
, "<= %s\r\n", rp_string (result
));
944 smtalk (SM_RSET
, "RSET");
947 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
958 smtalk (SM_RSET
, "RSET");
970 #ifdef HAVE_ST_BLKSIZE
973 if (fstat (fileno (sm_wfp
), &st
) == NOTOK
|| (cc
= st
.st_blksize
) < BUFSIZ
)
978 if ((cp
= malloc ((size_t) cc
)) == NULL
) {
979 smtalk (SM_RSET
, "RSET");
980 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
985 fseek (fp
, pos
, SEEK_SET
);
989 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
990 if ((j
= fread (cp
, sizeof(*cp
), i
, fp
)) == OK
) {
994 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
995 "error reading %s: ", file
);
999 if ((s
= strerror (errno
)))
1000 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
1002 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
1003 "unknown error %d", errno
);
1004 sm_reply
.length
= strlen (sm_reply
.text
);
1012 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
1013 if ((j
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
)
1017 printf ("wrote %d octets to server\n", j
);
1026 switch (smhear ()) {
1044 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1055 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0) {
1061 ftruncate (fileno (gp
), 0L);
1062 fseek (gp
, 0L, SEEK_SET
);
1065 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1066 if ((gp
= fopen (buffer
, "w")) == NULL
)
1069 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
1070 i
= strlen (sender
);
1071 fprintf (gp
, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1072 i
- 4, i
- 4, sender
+ 1, file
);
1073 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1074 dtimenow (0), LocalName ());
1079 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp
);
1080 fseek (fp
, pos
, SEEK_SET
);
1081 while (fgets (buffer
, sizeof(buffer
), fp
)) {
1082 if (buffer
[0] == '-')
1084 if (strcmp (buffer
, ".\r\n"))
1087 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp
);
1102 * This function implements SASL authentication for SMTP. If this function
1103 * completes successfully, then authentication is successful and we've
1104 * (optionally) negotiated a security layer.
1106 * Right now we don't support session encryption.
1109 sm_auth_sasl(char *user
, char *mechlist
, char *host
)
1111 int result
, status
, outlen
;
1112 unsigned int buflen
;
1113 char *buf
, outbuf
[BUFSIZ
];
1114 const char *chosen_mech
;
1115 sasl_security_properties_t secprops
;
1116 sasl_external_properties_t extprops
;
1121 * Initialize the callback contexts
1125 user
= getusername();
1127 callbacks
[SM_SASL_N_CB_USER
].context
= user
;
1130 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1131 * to us on the command line, then call getpeername and do a
1132 * reverse-address lookup on the IP address to get the name.
1136 struct sockaddr_in sin
;
1137 int len
= sizeof(sin
);
1140 if (getpeername(fileno(sm_wfp
), (struct sockaddr
*) &sin
, &len
) < 0) {
1141 sm_ierror("getpeername on SMTP socket failed: %s",
1146 if ((hp
= gethostbyaddr((void *) &sin
.sin_addr
, sizeof(sin
.sin_addr
),
1147 sin
.sin_family
)) == NULL
) {
1148 sm_ierror("DNS lookup on IP address %s failed",
1149 inet_ntoa(sin
.sin_addr
));
1153 host
= strdup(hp
->h_name
);
1156 sasl_pw_context
[0] = host
;
1157 sasl_pw_context
[1] = user
;
1159 callbacks
[SM_SASL_N_CB_PASS
].context
= sasl_pw_context
;
1161 result
= sasl_client_init(callbacks
);
1163 if (result
!= SASL_OK
) {
1164 sm_ierror("SASL library initialization failed: %s",
1165 sasl_errstring(result
, NULL
, NULL
));
1169 result
= sasl_client_new("smtp", host
, NULL
, SASL_SECURITY_LAYER
, &conn
);
1171 if (result
!= SASL_OK
) {
1172 sm_ierror("SASL client initialization failed: %s",
1173 sasl_errstring(result
, NULL
, NULL
));
1178 * Initialize the security properties
1181 memset(&secprops
, 0, sizeof(secprops
));
1182 secprops
.maxbufsize
= BUFSIZ
;
1183 secprops
.max_ssf
= 0; /* XXX change this when we do encryption */
1184 memset(&extprops
, 0, sizeof(extprops
));
1186 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
1188 if (result
!= SASL_OK
) {
1189 sm_ierror("SASL security property initialization failed: %s",
1190 sasl_errstring(result
, NULL
, NULL
));
1194 result
= sasl_setprop(conn
, SASL_SSF_EXTERNAL
, &extprops
);
1196 if (result
!= SASL_OK
) {
1197 sm_ierror("SASL external property initialization failed: %s",
1198 sasl_errstring(result
, NULL
, NULL
));
1203 * Start the actual protocol. Feed the mech list into the library
1204 * and get out a possible initial challenge
1207 result
= sasl_client_start(conn
, mechlist
, NULL
, NULL
, &buf
, &buflen
,
1210 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1211 sm_ierror("SASL client start failed: %s",
1212 sasl_errstring(result
, NULL
, NULL
));
1217 * If we got an initial challenge, send it as part of the AUTH
1218 * command; otherwise, just send a plain AUTH command.
1222 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1224 if (status
!= SASL_OK
) {
1225 sm_ierror("SASL base64 encode failed: %s",
1226 sasl_errstring(status
, NULL
, NULL
));
1230 status
= smtalk(SM_AUTH
, "AUTH %s %s", chosen_mech
, outbuf
);
1232 status
= smtalk(SM_AUTH
, "AUTH %s", chosen_mech
);
1235 * Now we loop until we either fail, get a SASL_OK, or a 235
1236 * response code. Receive the challenges and process them until
1240 while (result
== SASL_CONTINUE
) {
1243 * If we get a 235 response, that means authentication has
1244 * succeeded and we need to break out of the loop (yes, even if
1245 * we still get SASL_CONTINUE from sasl_client_step()).
1247 * Otherwise, if we get a message that doesn't seem to be a
1248 * valid response, then abort
1253 else if (status
< 300 || status
> 399)
1257 * Special case; a zero-length response from the SMTP server
1258 * is returned as a single =. If we get that, then set buflen
1259 * to be zero. Otherwise, just decode the response.
1262 if (strcmp("=", sm_reply
.text
) == 0) {
1265 result
= sasl_decode64(sm_reply
.text
, sm_reply
.length
,
1268 if (result
!= SASL_OK
) {
1269 smtalk(SM_AUTH
, "*");
1270 sm_ierror("SASL base64 decode failed: %s",
1271 sasl_errstring(result
, NULL
, NULL
));
1276 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
, &buf
, &buflen
);
1278 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1279 smtalk(SM_AUTH
, "*");
1280 sm_ierror("SASL client negotiation failed: %s",
1281 sasl_errstring(result
, NULL
, NULL
));
1285 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1288 if (status
!= SASL_OK
) {
1289 smtalk(SM_AUTH
, "*");
1290 sm_ierror("SASL base64 encode failed: %s",
1291 sasl_errstring(status
, NULL
, NULL
));
1295 status
= smtalk(SM_AUTH
, outbuf
);
1299 * Make sure that we got the correct response
1302 if (status
< 200 || status
> 299)
1306 * Depending on the mechanism, we need to do a FINAL call to
1307 * sasl_client_step(). Do that now.
1310 result
= sasl_client_step(conn
, NULL
, 0, NULL
, &buf
, &buflen
);
1312 if (result
!= SASL_OK
) {
1313 sm_ierror("SASL final client negotiation failed: %s",
1314 sasl_errstring(result
, NULL
, NULL
));
1319 * We _should_ have completed the authentication successfully.
1320 * Get a few properties from the authentication exchange.
1323 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (void **) &outbufmax
);
1325 if (result
!= SASL_OK
) {
1326 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1327 sasl_errstring(result
, NULL
, NULL
));
1331 maxoutbuf
= *outbufmax
;
1333 result
= sasl_getprop(conn
, SASL_SSF
, (void **) &ssf
);
1337 if (result
!= SASL_OK
) {
1338 sm_ierror("Cannot retrieve SASL negotiated security strength "
1339 "factor: %s", sasl_errstring(result
, NULL
, NULL
));
1343 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
1352 * Our callback functions to feed data to the SASL library
1356 sm_get_user(void *context
, int id
, const char **result
, unsigned *len
)
1358 char *user
= (char *) context
;
1360 if (! result
|| id
!= SASL_CB_USER
)
1361 return SASL_BADPARAM
;
1365 *len
= strlen(user
);
1371 sm_get_pass(sasl_conn_t
*conn
, void *context
, int id
,
1372 sasl_secret_t
**psecret
)
1374 char **pw_context
= (char **) context
;
1378 if (! psecret
|| id
!= SASL_CB_PASS
)
1379 return SASL_BADPARAM
;
1381 ruserpass(pw_context
[0], &(pw_context
[1]), &pass
);
1385 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
1392 (*psecret
)->len
= len
;
1393 strcpy((*psecret
)->data
, pass
);
1398 #endif /* CYRUS_SASL */
1401 sm_ierror (char *fmt
, ...)
1406 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
1409 sm_reply
.length
= strlen (sm_reply
.text
);
1410 sm_reply
.code
= NOTOK
;
1417 smtalk (int time
, char *fmt
, ...)
1421 char buffer
[BUFSIZ
];
1424 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
1428 printf ("=> %s\n", buffer
);
1436 if (strcmp (buffer
, ".") == 0)
1438 fprintf (sm_wfp
, "%s\r\n", buffer
);
1442 if (ferror (sm_wfp
))
1443 return sm_werror ();
1444 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
1445 (char) (sm_ispool
+ 'a' - 1));
1446 if (rename (sm_tmpfil
, file
) == NOTOK
) {
1450 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
1451 "error renaming %s to %s: ", sm_tmpfil
, file
);
1455 if ((s
= strerror (errno
)))
1456 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
1458 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
1459 "unknown error %d", errno
);
1460 sm_reply
.length
= strlen (sm_reply
.text
);
1461 sm_reply
.code
= NOTOK
;
1465 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
1466 chmod (sm_tmpfil
, 0600);
1477 ftruncate (fileno (sm_wfp
), 0L);
1478 fseek (sm_wfp
, 0L, SEEK_SET
);
1497 printf ("<= %d\n", result
);
1501 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
1502 return (sm_reply
.code
= result
);
1507 alarm ((unsigned) time
);
1508 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
1517 * write the buffer to the open SMTP channel
1521 sm_wrecord (char *buffer
, int len
)
1524 return sm_werror ();
1526 fwrite (buffer
, sizeof(*buffer
), len
, sm_wfp
);
1527 fputs ("\r\n", sm_wfp
);
1530 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1535 sm_wstream (char *buffer
, int len
)
1538 static char lc
= '\0';
1541 return sm_werror ();
1543 if (buffer
== NULL
&& len
== 0) {
1545 fputs ("\r\n", sm_wfp
);
1547 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1550 for (bp
= buffer
; len
> 0; bp
++, len
--) {
1554 fputc ('\r', sm_wfp
);
1559 fputc ('.', sm_wfp
);/* FALL THROUGH */
1563 fputc (*bp
, sm_wfp
);
1564 if (ferror (sm_wfp
))
1565 return sm_werror ();
1570 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1575 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1576 * causes compile problems with the #ifdef MPOP in the middle. Should the
1577 * #ifdef MPOP be removed, remove these #undefs.
1590 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1591 : sm_alarmed
? "write to socket timed out"
1593 : sm_ispool
? "error writing to spool file"
1595 : "error writing to socket"));
1597 return (sm_reply
.code
= NOTOK
);
1604 int i
, code
, cont
, bc
, rc
, more
;
1606 char **ehlo
, buffer
[BUFSIZ
];
1609 static int at_least_once
= 0;
1611 if (at_least_once
) {
1614 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1628 sm_reply
.length
= 0;
1629 sm_reply
.text
[0] = 0;
1631 rc
= sizeof(sm_reply
.text
) - 1;
1633 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
1635 printf ("<= %s\n", buffer
);
1640 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1641 && (buffer
[3] == '-' || doingEHLO
== 2)
1643 if (doingEHLO
== 2) {
1644 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1645 strcpy (*ehlo
++, buffer
+ 4);
1647 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1657 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1663 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1665 if (bc
> 0 && *bp
== '-') {
1668 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1673 if (code
!= sm_reply
.code
|| cont
)
1677 sm_reply
.code
= code
;
1680 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1682 bc
= strlen (sm_noreply
);
1686 if ((i
= min (bc
, rc
)) > 0) {
1687 strncpy (rp
, bp
, i
);
1690 if (more
&& rc
> strlen (sm_moreply
) + 1) {
1691 strncpy (sm_reply
.text
+ rc
, sm_moreply
, sizeof(sm_reply
.text
) - rc
);
1692 rc
+= strlen (sm_moreply
);
1697 if (sm_reply
.code
< 100) {
1699 printf ("%s\n", sm_reply
.text
);
1705 sm_reply
.length
= rp
- sm_reply
.text
;
1706 return sm_reply
.code
;
1713 sm_rrecord (char *buffer
, int *len
)
1716 return sm_rerror ();
1718 buffer
[*len
= 0] = 0;
1720 fgets (buffer
, BUFSIZ
, sm_rfp
);
1721 *len
= strlen (buffer
);
1722 if (ferror (sm_rfp
) || feof (sm_rfp
))
1723 return sm_rerror ();
1724 if (buffer
[*len
- 1] != '\n')
1725 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
1728 if (buffer
[*len
- 2] == '\r')
1730 buffer
[*len
- 1] = 0;
1739 if (sm_mts
== MTS_SMTP
)
1741 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1742 : sm_alarmed
? "read from socket timed out"
1743 : feof (sm_rfp
) ? "premature end-of-file on socket"
1744 : "error reading from socket"));
1747 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no pipe opened"
1748 : sm_alarmed
? "read from pipe timed out"
1749 : feof (sm_rfp
) ? "premature end-of-file on pipe"
1750 : "error reading from pipe"));
1752 return (sm_reply
.code
= NOTOK
);
1759 #ifndef RELIABLE_SIGNALS
1760 SIGNAL (SIGALRM
, alrmser
);
1765 printf ("timed out...\n");
1772 rp_string (int code
)
1775 static char buffer
[BUFSIZ
];
1777 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1797 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1817 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1818 text
, sm_reply
.code
, sm_reply
.text
);
1825 static char *broken
[MAXARGS
+ 1];
1828 smail_brkstring (char *strg
, char *brksep
, char *brkterm
)
1835 for (bi
= 0; bi
< MAXARGS
; bi
++) {
1836 while (smail_brkany (c
= *sp
, brksep
))
1838 if (!c
|| smail_brkany (c
, brkterm
)) {
1845 while ((c
= *++sp
) && !smail_brkany (c
, brksep
) && !smail_brkany (c
, brkterm
))
1848 broken
[MAXARGS
] = 0;
1855 * returns 1 if chr in strg, 0 otherwise
1858 smail_brkany (char chr
, char *strg
)
1863 for (sp
= strg
; *sp
; sp
++)
1870 * copy a string array and return pointer to end
1873 smail_copyip (char **p
, char **q
, int len_q
)
1875 while (*p
&& --len_q
> 0)
1894 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1896 if (strncmp (ep
, s
, len
) == 0) {
1897 for (ep
+= len
; *ep
== ' '; ep
++)