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>
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 *);
143 * smtp.c's own static copy of several nmh library subroutines
145 static char **smail_brkstring (char *, char *, char *);
146 static int smail_brkany (char, char *);
147 char **smail_copyip (char **, char **, int);
152 * Function prototypes needed for SASL
155 static int sm_auth_sasl(char *, char *, char *);
156 #endif /* CYRUS_SASL */
158 /* from mts/generic/client.c */
159 int client (char *, char *, char *, int, char *, int);
162 sm_init (char *client
, char *server
, int watch
, int verbose
,
163 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
166 if (sm_mts
== MTS_SMTP
)
167 return smtp_init (client
, server
, watch
, verbose
,
168 debug
, onex
, queued
, sasl
, saslmech
, user
);
170 return sendmail_init (client
, server
, watch
, verbose
,
171 debug
, onex
, queued
);
175 smtp_init (char *client
, char *server
, int watch
, int verbose
,
176 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
181 #endif /* CYRUS_SASL */
182 int result
, sd1
, sd2
;
187 sm_verbose
= verbose
;
195 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
198 if (client
== NULL
|| *client
== '\0') {
202 client
= LocalName(); /* no clientname -> LocalName */
207 if (client
== NULL
|| *client
== '\0')
208 client
= "localhost";
211 if ((sd1
= rclient (server
, "tcp", "smtp")) == NOTOK
)
222 if ((sm_wfp
= fdopen (sd1
, "w")) == NULL
) {
225 return sm_ierror ("unable to fdopen");
228 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
229 return (sm_reply
.code
= RP_OK
);
233 if ((sd2
= dup (sd1
)) == NOTOK
) {
235 return sm_ierror ("unable to dup");
238 SIGNAL (SIGALRM
, alrmser
);
239 SIGNAL (SIGPIPE
, SIG_IGN
);
241 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
242 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
245 sm_rfp
= sm_wfp
= NULL
;
246 return sm_ierror ("unable to fdopen");
264 * Give EHLO or HELO command
266 if (client
&& *client
) {
268 result
= smtalk (SM_HELO
, "EHLO %s", client
);
271 if (result
>= 500 && result
<= 599)
272 result
= smtalk (SM_HELO
, "HELO %s", client
);
282 * If the user asked for SASL, then check to see if the SMTP server
283 * supports it. Otherwise, error out (because the SMTP server
284 * might have been spoofed; we don't want to just silently not
289 if (! (server_mechs
= EHLOset("AUTH"))) {
291 return sm_ierror("SMTP server does not support SASL");
294 if (saslmech
&& stringdex(saslmech
, server_mechs
) == -1) {
296 return sm_ierror("Requested SASL mech \"%s\" is not in the "
297 "list of supported mechanisms:\n%s",
298 saslmech
, server_mechs
);
301 if (sm_auth_sasl(user
, saslmech
? saslmech
: server_mechs
,
307 #endif /* CYRUS_SASL */
310 if (watch
&& EHLOset ("XVRB"))
311 smtalk (SM_HELO
, "VERB on");
312 if (onex
&& EHLOset ("XONE"))
313 smtalk (SM_HELO
, "ONEX");
314 if (queued
&& EHLOset ("XQUE"))
315 smtalk (SM_HELO
, "QUED");
321 sendmail_init (char *client
, char *server
, int watch
, int verbose
,
322 int debug
, int onex
, int queued
)
331 sm_verbose
= verbose
;
333 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
336 if (client
== NULL
|| *client
== '\0') {
340 client
= LocalName(); /* no clientname -> LocalName */
344 if (client
== NULL
|| *client
== '\0')
345 client
= "localhost";
348 if (pipe (pdi
) == NOTOK
)
349 return sm_ierror ("no pipes");
350 if (pipe (pdo
) == NOTOK
) {
353 return sm_ierror ("no pipes");
356 for (i
= 0; (sm_child
= fork ()) == NOTOK
&& i
< 5; i
++)
365 return sm_ierror ("unable to fork");
368 if (pdo
[0] != fileno (stdin
))
369 dup2 (pdo
[0], fileno (stdin
));
370 if (pdi
[1] != fileno (stdout
))
371 dup2 (pdi
[1], fileno (stdout
));
372 if (pdi
[1] != fileno (stderr
))
373 dup2 (pdi
[1], fileno (stderr
));
374 for (i
= fileno (stderr
) + 1; i
< NBITS
; i
++)
378 vec
[vecp
++] = r1bindex (sendmail
, '/');
381 vec
[vecp
++] = watch
? "-odi" : queued
? "-odq" : "-odb";
382 vec
[vecp
++] = "-oem";
387 # endif /* not RAND */
388 #endif /* not ZMAILER */
393 execvp (sendmail
, vec
);
394 fprintf (stderr
, "unable to exec ");
396 _exit (-1); /* NOTREACHED */
399 SIGNAL (SIGALRM
, alrmser
);
400 SIGNAL (SIGPIPE
, SIG_IGN
);
404 if ((sm_rfp
= fdopen (pdi
[0], "r")) == NULL
405 || (sm_wfp
= fdopen (pdo
[1], "w")) == NULL
) {
408 sm_rfp
= sm_wfp
= NULL
;
409 return sm_ierror ("unable to fdopen");
424 if (client
&& *client
) {
426 result
= smtalk (SM_HELO
, "EHLO %s", client
);
429 if (500 <= result
&& result
<= 599)
430 result
= smtalk (SM_HELO
, "HELO %s", client
);
444 smtalk (SM_HELO
, "ONEX");
447 smtalk (SM_HELO
, "VERB on");
454 # define MAXARGS 1000
458 rclient (char *server
, char *protocol
, char *service
)
461 char response
[BUFSIZ
];
466 if ((sd
= client (server
, protocol
, service
, FALSE
, response
, sizeof(response
))) != NOTOK
)
470 if (!server
&& servers
&& (cp
= strchr(servers
, '/'))) {
472 char *arguments
[MAXARGS
];
474 smail_copyip (smail_brkstring (cp
= getcpy (servers
), " ", "\n"), arguments
, MAXARGS
);
476 for (ap
= arguments
; *ap
; ap
++)
480 if ((dp
= strrchr(*ap
, '/')) && *++dp
== NULL
)
482 snprintf (sm_tmpfil
, sizeof(sm_tmpfil
), "%s/smtpXXXXXX", *ap
);
484 sd
= mkstemp (sm_tmpfil
);
488 if ((sd
= creat (sm_tmpfil
, 0600)) != NOTOK
) {
501 sm_ierror ("%s", response
);
507 #include <sys/socket.h>
508 #include <netinet/in.h>
509 #include <arpa/inet.h>
512 #endif /* CYRUS_SASL */
515 sm_winit (int mode
, char *from
)
520 if (sm_ispool
&& !sm_wfp
) {
521 strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
522 sm_reply
.code
= NOTOK
;
545 switch (smtalk (SM_MAIL
, "%s FROM:<%s>", smtpcom
, from
)) {
562 sm_wadr (char *mbox
, char *host
, char *path
)
564 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
566 path
? path
: "", mbox
, host
)) {
576 #endif /* SENDMAILBUG */
601 switch (smtalk (SM_DATA
, "DATA")) {
610 #endif /* SENDMAILBUG */
627 sm_wtxt (char *buffer
, int len
)
633 result
= sm_wstream (buffer
, len
);
636 return (result
== NOTOK
? RP_BHST
: RP_OK
);
643 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
646 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
654 #endif /* SENDMAILBUG */
672 if (sm_mts
== MTS_SENDMAIL
) {
683 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
688 smtalk (SM_QUIT
, "QUIT");
692 sm_note
.code
= sm_reply
.code
;
693 strncpy (sm_note
.text
, sm_reply
.text
, sm_note
.length
= sm_reply
.length
);/* fall */
695 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
697 if (sm_mts
== MTS_SMTP
)
698 smtalk (SM_QUIT
, "QUIT");
700 kill (sm_child
, SIGKILL
);
705 sm_reply
.code
= sm_note
.code
;
706 strncpy (sm_reply
.text
, sm_note
.text
, sm_reply
.length
= sm_note
.length
);
723 if (sm_rfp
!= NULL
) {
728 if (sm_wfp
!= NULL
) {
734 if (sm_mts
== MTS_SMTP
) {
739 #endif /* CYRUS_SASL */
741 status
= pidwait (sm_child
, OK
);
745 sm_rfp
= sm_wfp
= NULL
;
746 return (status
? RP_BHST
: RP_OK
);
755 int cc
, i
, j
, k
, result
;
757 char *dp
, *bp
, *cp
, s
;
758 char buffer
[BUFSIZ
], sender
[BUFSIZ
];
762 k
= strlen (file
) - sizeof(".bulk");
763 if ((fp
= fopen (file
, "r")) == NULL
) {
766 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
767 "unable to read %s: ", file
);
771 if ((s
= strerror (errno
)))
772 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
774 snprintf (bp
, sizeof(sm_reply
.text
) - len
, "Error %d", errno
);
775 sm_reply
.length
= strlen (sm_reply
.text
);
776 sm_reply
.code
= NOTOK
;
780 printf ("reading file %s\n", file
);
785 while (fgets (buffer
, sizeof(buffer
), fp
)) {
787 strncpy (sender
, buffer
+ sizeof("MAIL FROM:") - 1, sizeof(sender
));
788 if (strcmp (buffer
, "DATA\r\n") == 0) {
795 printf ("no DATA...\n");
799 snprintf (buffer
, sizeof(buffer
), "%s.bad", file
);
800 rename (file
, buffer
);
802 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
811 printf ("no %srecipients...\n", j
< 1 ? "sender or " : "");
817 if ((cp
= malloc ((size_t) (cc
= (pos
= ftell (fp
)) + 1))) == NULL
) {
818 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
820 sm_reply
.code
= NOTOK
;
824 fseek (fp
, 0L, SEEK_SET
);
825 for (dp
= cp
, i
= 0; i
++ < j
; dp
+= strlen (dp
))
826 if (fgets (dp
, cc
- (dp
- cp
), fp
) == NULL
) {
827 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "premature eof"));
834 for (dp
= cp
, i
= cc
- 1; i
> 0; dp
+= cc
, i
-= cc
)
835 if ((cc
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
) {
838 strcpy (sm_reply
.text
, "error writing to server: ",
839 sizeof(sm_reply
.text
));
843 if ((s
= strerror (errno
)))
844 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
846 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
847 "unknown error %d", errno
);
848 sm_reply
.length
= strlen (sm_reply
.text
);
853 printf ("wrote %d octets to server\n", cc
);
857 for (dp
= cp
, i
= 0; i
++ < j
; dp
= strchr(dp
, '\n'), dp
++) {
859 if (bp
= strchr(dp
, '\r'))
861 printf ("=> %s\n", dp
);
867 switch (smhear () + (i
== 1 ? 1000 : i
!= j
? 2000 : 3000)) {
879 smtalk (SM_RSET
, "RSET");
907 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0)
911 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
912 if ((gp
= fopen (buffer
, "w+")) == NULL
)
914 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
917 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
918 l
- 4, l
- 4, sender
+ 1, file
);
919 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
920 dtimenow (0), LocalName ());
922 if (bp
= strchr(dp
, '\r'))
924 fprintf (gp
, "=> %s\r\n", dp
);
927 fprintf (gp
, "<= %s\r\n", rp_string (result
));
945 smtalk (SM_RSET
, "RSET");
948 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
959 smtalk (SM_RSET
, "RSET");
971 #ifdef HAVE_ST_BLKSIZE
974 if (fstat (fileno (sm_wfp
), &st
) == NOTOK
|| (cc
= st
.st_blksize
) < BUFSIZ
)
979 if ((cp
= malloc ((size_t) cc
)) == NULL
) {
980 smtalk (SM_RSET
, "RSET");
981 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
986 fseek (fp
, pos
, SEEK_SET
);
990 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
991 if ((j
= fread (cp
, sizeof(*cp
), i
, fp
)) == OK
) {
995 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
996 "error reading %s: ", file
);
1000 if ((s
= strerror (errno
)))
1001 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
1003 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
1004 "unknown error %d", errno
);
1005 sm_reply
.length
= strlen (sm_reply
.text
);
1013 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
1014 if ((j
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
)
1018 printf ("wrote %d octets to server\n", j
);
1027 switch (smhear ()) {
1045 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1056 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0) {
1062 ftruncate (fileno (gp
), 0L);
1063 fseek (gp
, 0L, SEEK_SET
);
1066 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1067 if ((gp
= fopen (buffer
, "w")) == NULL
)
1070 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
1071 i
= strlen (sender
);
1072 fprintf (gp
, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1073 i
- 4, i
- 4, sender
+ 1, file
);
1074 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1075 dtimenow (0), LocalName ());
1080 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp
);
1081 fseek (fp
, pos
, SEEK_SET
);
1082 while (fgets (buffer
, sizeof(buffer
), fp
)) {
1083 if (buffer
[0] == '-')
1085 if (strcmp (buffer
, ".\r\n"))
1088 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp
);
1103 * This function implements SASL authentication for SMTP. If this function
1104 * completes successfully, then authentication is successful and we've
1105 * (optionally) negotiated a security layer.
1107 * Right now we don't support session encryption.
1110 sm_auth_sasl(char *user
, char *mechlist
, char *host
)
1113 unsigned int buflen
, outlen
;
1114 char *buf
, outbuf
[BUFSIZ
];
1115 const char *chosen_mech
;
1116 sasl_security_properties_t secprops
;
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
, NULL
, NULL
, 0, &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 */
1185 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
1187 if (result
!= SASL_OK
) {
1188 sm_ierror("SASL security property initialization failed: %s",
1189 sasl_errstring(result
, NULL
, NULL
));
1194 * Start the actual protocol. Feed the mech list into the library
1195 * and get out a possible initial challenge
1198 result
= sasl_client_start(conn
, mechlist
, NULL
, (const char **) &buf
,
1199 &buflen
, (const char **) &chosen_mech
);
1201 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1202 sm_ierror("SASL client start failed: %s",
1203 sasl_errstring(result
, NULL
, NULL
));
1208 * If we got an initial challenge, send it as part of the AUTH
1209 * command; otherwise, just send a plain AUTH command.
1213 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1214 if (status
!= SASL_OK
) {
1215 sm_ierror("SASL base64 encode failed: %s",
1216 sasl_errstring(status
, NULL
, NULL
));
1220 status
= smtalk(SM_AUTH
, "AUTH %s %s", chosen_mech
, outbuf
);
1222 status
= smtalk(SM_AUTH
, "AUTH %s", chosen_mech
);
1225 * Now we loop until we either fail, get a SASL_OK, or a 235
1226 * response code. Receive the challenges and process them until
1230 while (result
== SASL_CONTINUE
) {
1233 * If we get a 235 response, that means authentication has
1234 * succeeded and we need to break out of the loop (yes, even if
1235 * we still get SASL_CONTINUE from sasl_client_step()).
1237 * Otherwise, if we get a message that doesn't seem to be a
1238 * valid response, then abort
1243 else if (status
< 300 || status
> 399)
1247 * Special case; a zero-length response from the SMTP server
1248 * is returned as a single =. If we get that, then set buflen
1249 * to be zero. Otherwise, just decode the response.
1252 if (strcmp("=", sm_reply
.text
) == 0) {
1255 result
= sasl_decode64(sm_reply
.text
, sm_reply
.length
,
1256 outbuf
, sizeof(outbuf
), &outlen
);
1258 if (result
!= SASL_OK
) {
1259 smtalk(SM_AUTH
, "*");
1260 sm_ierror("SASL base64 decode failed: %s",
1261 sasl_errstring(result
, NULL
, NULL
));
1266 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
,
1267 (const char **) &buf
, &buflen
);
1269 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1270 smtalk(SM_AUTH
, "*");
1271 sm_ierror("SASL client negotiation failed: %s",
1272 sasl_errstring(result
, NULL
, NULL
));
1276 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1278 if (status
!= SASL_OK
) {
1279 smtalk(SM_AUTH
, "*");
1280 sm_ierror("SASL base64 encode failed: %s",
1281 sasl_errstring(status
, NULL
, NULL
));
1285 status
= smtalk(SM_AUTH
, outbuf
);
1289 * Make sure that we got the correct response
1292 if (status
< 200 || status
> 299)
1296 * We _should_ have completed the authentication successfully.
1297 * Get a few properties from the authentication exchange.
1300 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (const void **) &outbufmax
);
1302 if (result
!= SASL_OK
) {
1303 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1304 sasl_errstring(result
, NULL
, NULL
));
1308 maxoutbuf
= *outbufmax
;
1310 result
= sasl_getprop(conn
, SASL_SSF
, (const void **) &ssf
);
1314 if (result
!= SASL_OK
) {
1315 sm_ierror("Cannot retrieve SASL negotiated security strength "
1316 "factor: %s", sasl_errstring(result
, NULL
, NULL
));
1320 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
1329 * Our callback functions to feed data to the SASL library
1333 sm_get_user(void *context
, int id
, const char **result
, unsigned *len
)
1335 char *user
= (char *) context
;
1337 if (! result
|| id
!= SASL_CB_USER
)
1338 return SASL_BADPARAM
;
1342 *len
= strlen(user
);
1348 sm_get_pass(sasl_conn_t
*conn
, void *context
, int id
,
1349 sasl_secret_t
**psecret
)
1351 char **pw_context
= (char **) context
;
1355 if (! psecret
|| id
!= SASL_CB_PASS
)
1356 return SASL_BADPARAM
;
1358 ruserpass(pw_context
[0], &(pw_context
[1]), &pass
);
1362 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
1369 (*psecret
)->len
= len
;
1370 strcpy((char *) (*psecret
)->data
, pass
);
1375 #endif /* CYRUS_SASL */
1378 sm_ierror (char *fmt
, ...)
1383 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
1386 sm_reply
.length
= strlen (sm_reply
.text
);
1387 sm_reply
.code
= NOTOK
;
1394 smtalk (int time
, char *fmt
, ...)
1398 char buffer
[BUFSIZ
];
1401 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
1405 printf ("=> %s\n", buffer
);
1413 if (strcmp (buffer
, ".") == 0)
1415 fprintf (sm_wfp
, "%s\r\n", buffer
);
1419 if (ferror (sm_wfp
))
1420 return sm_werror ();
1421 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
1422 (char) (sm_ispool
+ 'a' - 1));
1423 if (rename (sm_tmpfil
, file
) == NOTOK
) {
1427 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
1428 "error renaming %s to %s: ", sm_tmpfil
, file
);
1432 if ((s
= strerror (errno
)))
1433 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
1435 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
1436 "unknown error %d", errno
);
1437 sm_reply
.length
= strlen (sm_reply
.text
);
1438 sm_reply
.code
= NOTOK
;
1442 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
1443 chmod (sm_tmpfil
, 0600);
1454 ftruncate (fileno (sm_wfp
), 0L);
1455 fseek (sm_wfp
, 0L, SEEK_SET
);
1474 printf ("<= %d\n", result
);
1478 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
1479 return (sm_reply
.code
= result
);
1484 alarm ((unsigned) time
);
1485 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
1494 * write the buffer to the open SMTP channel
1498 sm_wrecord (char *buffer
, int len
)
1501 return sm_werror ();
1503 fwrite (buffer
, sizeof(*buffer
), len
, sm_wfp
);
1504 fputs ("\r\n", sm_wfp
);
1507 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1512 sm_wstream (char *buffer
, int len
)
1515 static char lc
= '\0';
1518 return sm_werror ();
1520 if (buffer
== NULL
&& len
== 0) {
1522 fputs ("\r\n", sm_wfp
);
1524 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1527 for (bp
= buffer
; len
> 0; bp
++, len
--) {
1531 fputc ('\r', sm_wfp
);
1536 fputc ('.', sm_wfp
);/* FALL THROUGH */
1540 fputc (*bp
, sm_wfp
);
1541 if (ferror (sm_wfp
))
1542 return sm_werror ();
1547 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1552 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1553 * causes compile problems with the #ifdef MPOP in the middle. Should the
1554 * #ifdef MPOP be removed, remove these #undefs.
1567 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1568 : sm_alarmed
? "write to socket timed out"
1570 : sm_ispool
? "error writing to spool file"
1572 : "error writing to socket"));
1574 return (sm_reply
.code
= NOTOK
);
1581 int i
, code
, cont
, bc
, rc
, more
;
1583 char **ehlo
, buffer
[BUFSIZ
];
1586 static int at_least_once
= 0;
1588 if (at_least_once
) {
1591 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1605 sm_reply
.length
= 0;
1606 sm_reply
.text
[0] = 0;
1608 rc
= sizeof(sm_reply
.text
) - 1;
1610 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
1612 printf ("<= %s\n", buffer
);
1617 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1618 && (buffer
[3] == '-' || doingEHLO
== 2)
1620 if (doingEHLO
== 2) {
1621 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1622 strcpy (*ehlo
++, buffer
+ 4);
1624 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1634 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1640 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1642 if (bc
> 0 && *bp
== '-') {
1645 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1650 if (code
!= sm_reply
.code
|| cont
)
1654 sm_reply
.code
= code
;
1657 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1659 bc
= strlen (sm_noreply
);
1663 if ((i
= min (bc
, rc
)) > 0) {
1664 strncpy (rp
, bp
, i
);
1667 if (more
&& rc
> strlen (sm_moreply
) + 1) {
1668 strncpy (sm_reply
.text
+ rc
, sm_moreply
, sizeof(sm_reply
.text
) - rc
);
1669 rc
+= strlen (sm_moreply
);
1674 if (sm_reply
.code
< 100) {
1676 printf ("%s\n", sm_reply
.text
);
1682 sm_reply
.length
= rp
- sm_reply
.text
;
1683 return sm_reply
.code
;
1690 sm_rrecord (char *buffer
, int *len
)
1693 return sm_rerror ();
1695 buffer
[*len
= 0] = 0;
1697 fgets (buffer
, BUFSIZ
, sm_rfp
);
1698 *len
= strlen (buffer
);
1699 if (ferror (sm_rfp
) || feof (sm_rfp
))
1700 return sm_rerror ();
1701 if (buffer
[*len
- 1] != '\n')
1702 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
1705 if (buffer
[*len
- 2] == '\r')
1707 buffer
[*len
- 1] = 0;
1716 if (sm_mts
== MTS_SMTP
)
1718 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1719 : sm_alarmed
? "read from socket timed out"
1720 : feof (sm_rfp
) ? "premature end-of-file on socket"
1721 : "error reading from socket"));
1724 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no pipe opened"
1725 : sm_alarmed
? "read from pipe timed out"
1726 : feof (sm_rfp
) ? "premature end-of-file on pipe"
1727 : "error reading from pipe"));
1729 return (sm_reply
.code
= NOTOK
);
1736 #ifndef RELIABLE_SIGNALS
1737 SIGNAL (SIGALRM
, alrmser
);
1742 printf ("timed out...\n");
1749 rp_string (int code
)
1752 static char buffer
[BUFSIZ
];
1754 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1774 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1794 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1795 text
, sm_reply
.code
, sm_reply
.text
);
1802 static char *broken
[MAXARGS
+ 1];
1805 smail_brkstring (char *strg
, char *brksep
, char *brkterm
)
1812 for (bi
= 0; bi
< MAXARGS
; bi
++) {
1813 while (smail_brkany (c
= *sp
, brksep
))
1815 if (!c
|| smail_brkany (c
, brkterm
)) {
1822 while ((c
= *++sp
) && !smail_brkany (c
, brksep
) && !smail_brkany (c
, brkterm
))
1825 broken
[MAXARGS
] = 0;
1832 * returns 1 if chr in strg, 0 otherwise
1835 smail_brkany (char chr
, char *strg
)
1840 for (sp
= strg
; *sp
; sp
++)
1847 * copy a string array and return pointer to end
1850 smail_copyip (char **p
, char **q
, int len_q
)
1852 while (*p
&& --len_q
> 0)
1871 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1873 if (strncmp (ep
, s
, len
) == 0) {
1874 for (ep
+= len
; *ep
== ' '; ep
++)