2 * smtp.c -- nmh SMTP interface
9 #include <zotnet/mts/mts.h>
11 #include <h/signals.h>
17 * This module implements an interface to SendMail very similar
18 * to the MMDF mm_(3) routines. The sm_() routines herein talk
19 * SMTP to a sendmail process, mapping SMTP reply codes into
24 * On older 4.2BSD machines without the POSIX function `sigaction',
25 * the alarm handing stuff for time-outs will NOT work due to the way
26 * syscalls get restarted. This is not really crucial, since SendMail
27 * is generally well-behaved in this area.
32 * It appears that some versions of Sendmail will return Code 451
33 * when they don't really want to indicate a failure.
34 * "Code 451 almost always means sendmail has deferred; we don't
35 * really want bomb out at this point since sendmail will rectify
36 * things later." So, if you define SENDMAILBUG, Code 451 is
37 * considered the same as Code 250. Yuck!
44 #define NBITS ((sizeof (int)) * 8)
47 * these codes must all be different!
49 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
61 static int sm_addrs
= 0;
62 static int sm_alarmed
= 0;
63 static int sm_child
= NOTOK
;
64 static int sm_debug
= 0;
65 static int sm_nl
= TRUE
;
66 static int sm_verbose
= 0;
68 static FILE *sm_rfp
= NULL
;
69 static FILE *sm_wfp
= NULL
;
72 static int sm_ispool
= 0;
73 static char sm_tmpfil
[BUFSIZ
];
78 * Some globals needed by SASL
81 static sasl_conn_t
*conn
= NULL
; /* SASL connection state */
82 static int sasl_complete
= 0; /* Has authentication succeded? */
83 static sasl_ssf_t sasl_ssf
; /* Our security strength factor */
84 static char *sasl_pw_context
[2]; /* Context to pass into sm_get_pass */
85 static int maxoutbuf
; /* Maximum crypto output buffer */
86 static int sm_get_user(void *, int, const char **, unsigned *);
87 static int sm_get_pass(sasl_conn_t
*, void *, int, sasl_secret_t
**);
89 static sasl_callback_t callbacks
[] = {
90 { SASL_CB_USER
, sm_get_user
, NULL
},
91 #define SM_SASL_N_CB_USER 0
92 { SASL_CB_PASS
, sm_get_pass
, NULL
},
93 #define SM_SASL_N_CB_PASS 1
94 { SASL_CB_LIST_END
, NULL
, NULL
},
96 #endif /* CYRUS_SASL */
98 static char *sm_noreply
= "No reply text given";
99 static char *sm_moreply
= "; ";
101 struct smtp sm_reply
; /* global... */
105 static int doingEHLO
;
106 char *EHLOkeys
[MAXEHLO
+ 1];
111 static int smtp_init (char *, char *, int, int, int, int, int, int,
113 static int sendmail_init (char *, char *, int, int, int, int, int);
115 static int rclient (char *, char *, char *);
116 static int sm_ierror (char *fmt
, ...);
117 static int smtalk (int time
, char *fmt
, ...);
118 static int sm_wrecord (char *, int);
119 static int sm_wstream (char *, int);
120 static int sm_werror (void);
121 static int smhear (void);
122 static int sm_rrecord (char *, int *);
123 static int sm_rerror (void);
124 static RETSIGTYPE
alrmser (int);
125 static char *EHLOset (char *);
129 * smtp.c's own static copy of several nmh library subroutines
131 static char **smail_brkstring (char *, char *, char *);
132 static int smail_brkany (char, char *);
133 char **smail_copyip (char **, char **, int);
138 * Function prototypes needed for SASL
141 static int sm_auth_sasl(char *, char *, char *);
142 #endif /* CYRUS_SASL */
144 /* from zotnet/mts/client.c */
145 int client (char *, char *, char *, int, char *, int);
148 sm_init (char *client
, char *server
, int watch
, int verbose
,
149 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
152 if (sm_mts
== MTS_SMTP
)
153 return smtp_init (client
, server
, watch
, verbose
,
154 debug
, onex
, queued
, sasl
, saslmech
, user
);
156 return sendmail_init (client
, server
, watch
, verbose
,
157 debug
, onex
, queued
);
161 smtp_init (char *client
, char *server
, int watch
, int verbose
,
162 int debug
, int onex
, int queued
, int sasl
, char *saslmech
,
167 #endif /* CYRUS_SASL */
168 int result
, sd1
, sd2
;
173 sm_verbose
= verbose
;
181 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
184 if (client
== NULL
|| *client
== '\0') {
188 client
= LocalName(); /* no clientname -> LocalName */
193 if (client
== NULL
|| *client
== '\0')
194 client
= "localhost";
197 if ((sd1
= rclient (server
, "tcp", "smtp")) == NOTOK
)
208 if ((sm_wfp
= fdopen (sd1
, "w")) == NULL
) {
211 return sm_ierror ("unable to fdopen");
214 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
215 return (sm_reply
.code
= RP_OK
);
219 if ((sd2
= dup (sd1
)) == NOTOK
) {
221 return sm_ierror ("unable to dup");
224 SIGNAL (SIGALRM
, alrmser
);
225 SIGNAL (SIGPIPE
, SIG_IGN
);
227 if ((sm_rfp
= fdopen (sd1
, "r")) == NULL
228 || (sm_wfp
= fdopen (sd2
, "w")) == NULL
) {
231 sm_rfp
= sm_wfp
= NULL
;
232 return sm_ierror ("unable to fdopen");
250 * Give EHLO or HELO command
252 if (client
&& *client
) {
254 result
= smtalk (SM_HELO
, "EHLO %s", client
);
257 if (result
>= 500 && result
<= 599)
258 result
= smtalk (SM_HELO
, "HELO %s", client
);
268 * If the user asked for SASL, then check to see if the SMTP server
269 * supports it. Otherwise, error out (because the SMTP server
270 * might have been spoofed; we don't want to just silently not
275 if (! (server_mechs
= EHLOset("AUTH"))) {
277 return sm_ierror("SMTP server does not support SASL");
280 if (saslmech
&& stringdex(saslmech
, server_mechs
) == -1) {
282 return sm_ierror("Requested SASL mech \"%s\" is not in the "
283 "list of supported mechanisms:\n%s",
284 saslmech
, server_mechs
);
287 if (sm_auth_sasl(user
, saslmech
? saslmech
: server_mechs
,
293 #endif /* CYRUS_SASL */
296 if (watch
&& EHLOset ("XVRB"))
297 smtalk (SM_HELO
, "VERB on");
298 if (onex
&& EHLOset ("XONE"))
299 smtalk (SM_HELO
, "ONEX");
300 if (queued
&& EHLOset ("XQUE"))
301 smtalk (SM_HELO
, "QUED");
307 sendmail_init (char *client
, char *server
, int watch
, int verbose
,
308 int debug
, int onex
, int queued
)
317 sm_verbose
= verbose
;
319 if (sm_rfp
!= NULL
&& sm_wfp
!= NULL
)
322 if (client
== NULL
|| *client
== '\0') {
326 client
= LocalName(); /* no clientname -> LocalName */
330 if (client
== NULL
|| *client
== '\0')
331 client
= "localhost";
334 if (pipe (pdi
) == NOTOK
)
335 return sm_ierror ("no pipes");
336 if (pipe (pdo
) == NOTOK
) {
339 return sm_ierror ("no pipes");
342 for (i
= 0; (sm_child
= fork ()) == NOTOK
&& i
< 5; i
++)
351 return sm_ierror ("unable to fork");
354 if (pdo
[0] != fileno (stdin
))
355 dup2 (pdo
[0], fileno (stdin
));
356 if (pdi
[1] != fileno (stdout
))
357 dup2 (pdi
[1], fileno (stdout
));
358 if (pdi
[1] != fileno (stderr
))
359 dup2 (pdi
[1], fileno (stderr
));
360 for (i
= fileno (stderr
) + 1; i
< NBITS
; i
++)
364 vec
[vecp
++] = r1bindex (sendmail
, '/');
367 vec
[vecp
++] = watch
? "-odi" : queued
? "-odq" : "-odb";
368 vec
[vecp
++] = "-oem";
373 # endif /* not RAND */
374 #endif /* not ZMAILER */
379 execvp (sendmail
, vec
);
380 fprintf (stderr
, "unable to exec ");
382 _exit (-1); /* NOTREACHED */
385 SIGNAL (SIGALRM
, alrmser
);
386 SIGNAL (SIGPIPE
, SIG_IGN
);
390 if ((sm_rfp
= fdopen (pdi
[0], "r")) == NULL
391 || (sm_wfp
= fdopen (pdo
[1], "w")) == NULL
) {
394 sm_rfp
= sm_wfp
= NULL
;
395 return sm_ierror ("unable to fdopen");
410 if (client
&& *client
) {
412 result
= smtalk (SM_HELO
, "EHLO %s", client
);
415 if (500 <= result
&& result
<= 599)
416 result
= smtalk (SM_HELO
, "HELO %s", client
);
430 smtalk (SM_HELO
, "ONEX");
433 smtalk (SM_HELO
, "VERB on");
440 # define MAXARGS 1000
444 rclient (char *server
, char *protocol
, char *service
)
447 char response
[BUFSIZ
];
452 if ((sd
= client (server
, protocol
, service
, FALSE
, response
, sizeof(response
))) != NOTOK
)
456 if (!server
&& servers
&& (cp
= strchr(servers
, '/'))) {
458 char *arguments
[MAXARGS
];
460 smail_copyip (smail_brkstring (cp
= getcpy (servers
), " ", "\n"), arguments
, MAXARGS
);
462 for (ap
= arguments
; *ap
; ap
++)
466 if ((dp
= strrchr(*ap
, '/')) && *++dp
== NULL
)
468 snprintf (sm_tmpfil
, sizeof(sm_tmpfil
), "%s/smtpXXXXXX", *ap
);
470 sd
= mkstemp (sm_tmpfil
);
474 if ((sd
= creat (sm_tmpfil
, 0600)) != NOTOK
) {
487 sm_ierror ("%s", response
);
493 #include <sys/socket.h>
494 #include <netinet/in.h>
495 #include <arpa/inet.h>
498 #endif /* CYRUS_SASL */
501 sm_winit (int mode
, char *from
)
506 if (sm_ispool
&& !sm_wfp
) {
507 strlen (strcpy (sm_reply
.text
, "unable to create new spool file"));
508 sm_reply
.code
= NOTOK
;
531 switch (smtalk (SM_MAIL
, "%s FROM:<%s>", smtpcom
, from
)) {
548 sm_wadr (char *mbox
, char *host
, char *path
)
550 switch (smtalk (SM_RCPT
, host
&& *host
? "RCPT TO:<%s%s@%s>"
552 path
? path
: "", mbox
, host
)) {
562 #endif /* SENDMAILBUG */
587 switch (smtalk (SM_DATA
, "DATA")) {
596 #endif /* SENDMAILBUG */
613 sm_wtxt (char *buffer
, int len
)
619 result
= sm_wstream (buffer
, len
);
622 return (result
== NOTOK
? RP_BHST
: RP_OK
);
629 if (sm_wstream ((char *) NULL
, 0) == NOTOK
)
632 switch (smtalk (SM_DOT
+ 3 * sm_addrs
, ".")) {
640 #endif /* SENDMAILBUG */
658 if (sm_mts
== MTS_SENDMAIL
) {
669 if (sm_rfp
== NULL
&& sm_wfp
== NULL
)
674 smtalk (SM_QUIT
, "QUIT");
678 sm_note
.code
= sm_reply
.code
;
679 strncpy (sm_note
.text
, sm_reply
.text
, sm_note
.length
= sm_reply
.length
);/* fall */
681 if (smtalk (SM_RSET
, "RSET") == 250 && type
== DONE
)
683 if (sm_mts
== MTS_SMTP
)
684 smtalk (SM_QUIT
, "QUIT");
686 kill (sm_child
, SIGKILL
);
691 sm_reply
.code
= sm_note
.code
;
692 strncpy (sm_reply
.text
, sm_note
.text
, sm_reply
.length
= sm_note
.length
);
709 if (sm_rfp
!= NULL
) {
714 if (sm_wfp
!= NULL
) {
720 if (sm_mts
== MTS_SMTP
) {
725 #endif /* CYRUS_SASL */
727 status
= pidwait (sm_child
, OK
);
731 sm_rfp
= sm_wfp
= NULL
;
732 return (status
? RP_BHST
: RP_OK
);
741 int cc
, i
, j
, k
, result
;
743 char *dp
, *bp
, *cp
, s
;
744 char buffer
[BUFSIZ
], sender
[BUFSIZ
];
748 k
= strlen (file
) - sizeof(".bulk");
749 if ((fp
= fopen (file
, "r")) == NULL
) {
752 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
753 "unable to read %s: ", file
);
757 if ((s
= strerror (errno
)))
758 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
760 snprintf (bp
, sizeof(sm_reply
.text
) - len
, "Error %d", errno
);
761 sm_reply
.length
= strlen (sm_reply
.text
);
762 sm_reply
.code
= NOTOK
;
766 printf ("reading file %s\n", file
);
771 while (fgets (buffer
, sizeof(buffer
), fp
)) {
773 strncpy (sender
, buffer
+ sizeof("MAIL FROM:") - 1, sizeof(sender
));
774 if (strcmp (buffer
, "DATA\r\n") == 0) {
781 printf ("no DATA...\n");
785 snprintf (buffer
, sizeof(buffer
), "%s.bad", file
);
786 rename (file
, buffer
);
788 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
797 printf ("no %srecipients...\n", j
< 1 ? "sender or " : "");
803 if ((cp
= malloc ((size_t) (cc
= (pos
= ftell (fp
)) + 1))) == NULL
) {
804 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
806 sm_reply
.code
= NOTOK
;
810 fseek (fp
, 0L, SEEK_SET
);
811 for (dp
= cp
, i
= 0; i
++ < j
; dp
+= strlen (dp
))
812 if (fgets (dp
, cc
- (dp
- cp
), fp
) == NULL
) {
813 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "premature eof"));
820 for (dp
= cp
, i
= cc
- 1; i
> 0; dp
+= cc
, i
-= cc
)
821 if ((cc
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
) {
824 strcpy (sm_reply
.text
, "error writing to server: ",
825 sizeof(sm_reply
.text
));
829 if ((s
= strerror (errno
)))
830 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
832 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
833 "unknown error %d", errno
);
834 sm_reply
.length
= strlen (sm_reply
.text
);
839 printf ("wrote %d octets to server\n", cc
);
843 for (dp
= cp
, i
= 0; i
++ < j
; dp
= strchr(dp
, '\n'), dp
++) {
845 if (bp
= strchr(dp
, '\r'))
847 printf ("=> %s\n", dp
);
853 switch (smhear () + (i
== 1 ? 1000 : i
!= j
? 2000 : 3000)) {
865 smtalk (SM_RSET
, "RSET");
893 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0)
897 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
898 if ((gp
= fopen (buffer
, "w+")) == NULL
)
900 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
903 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
904 l
- 4, l
- 4, sender
+ 1, file
);
905 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
906 dtimenow (0), LocalName ());
908 if (bp
= strchr(dp
, '\r'))
910 fprintf (gp
, "=> %s\r\n", dp
);
913 fprintf (gp
, "<= %s\r\n", rp_string (result
));
931 smtalk (SM_RSET
, "RSET");
934 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
945 smtalk (SM_RSET
, "RSET");
957 #ifdef HAVE_ST_BLKSIZE
960 if (fstat (fileno (sm_wfp
), &st
) == NOTOK
|| (cc
= st
.st_blksize
) < BUFSIZ
)
965 if ((cp
= malloc ((size_t) cc
)) == NULL
) {
966 smtalk (SM_RSET
, "RSET");
967 sm_reply
.length
= strlen (strcpy (sm_reply
.text
, "out of memory"));
972 fseek (fp
, pos
, SEEK_SET
);
976 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
977 if ((j
= fread (cp
, sizeof(*cp
), i
, fp
)) == OK
) {
981 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
982 "error reading %s: ", file
);
986 if ((s
= strerror (errno
)))
987 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
989 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
990 "unknown error %d", errno
);
991 sm_reply
.length
= strlen (sm_reply
.text
);
999 for (dp
= cp
, i
= cc
; i
> 0; dp
+= j
, i
-= j
)
1000 if ((j
= write (fileno (sm_wfp
), dp
, i
)) == NOTOK
)
1004 printf ("wrote %d octets to server\n", j
);
1013 switch (smhear ()) {
1031 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1042 if (k
<= 0 || strcmp (sender
, "<>\r\n") == 0) {
1048 ftruncate (fileno (gp
), 0L);
1049 fseek (gp
, 0L, SEEK_SET
);
1052 snprintf (buffer
, sizeof(buffer
), "%*.*sA.bulk", k
, k
, file
);
1053 if ((gp
= fopen (buffer
, "w")) == NULL
)
1056 fprintf (gp
, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender
);
1057 i
= strlen (sender
);
1058 fprintf (gp
, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1059 i
- 4, i
- 4, sender
+ 1, file
);
1060 fprintf (gp
, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1061 dtimenow (0), LocalName ());
1066 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp
);
1067 fseek (fp
, pos
, SEEK_SET
);
1068 while (fgets (buffer
, sizeof(buffer
), fp
)) {
1069 if (buffer
[0] == '-')
1071 if (strcmp (buffer
, ".\r\n"))
1074 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp
);
1089 * This function implements SASL authentication for SMTP. If this function
1090 * completes successfully, then authentication is successful and we've
1091 * (optionally) negotiated a security layer.
1093 * Right now we don't support session encryption.
1096 sm_auth_sasl(char *user
, char *mechlist
, char *host
)
1098 int result
, status
, outlen
;
1099 unsigned int buflen
;
1100 char *buf
, outbuf
[BUFSIZ
];
1101 const char *chosen_mech
;
1102 sasl_security_properties_t secprops
;
1103 sasl_external_properties_t extprops
;
1108 * Initialize the callback contexts
1112 user
= getusername();
1114 callbacks
[SM_SASL_N_CB_USER
].context
= user
;
1117 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1118 * to us on the command line, then call getpeername and do a
1119 * reverse-address lookup on the IP address to get the name.
1123 struct sockaddr_in sin
;
1124 int len
= sizeof(sin
);
1127 if (getpeername(fileno(sm_wfp
), (struct sockaddr
*) &sin
, &len
) < 0) {
1128 sm_ierror("getpeername on SMTP socket failed: %s",
1133 if ((hp
= gethostbyaddr((void *) &sin
.sin_addr
, sizeof(sin
.sin_addr
),
1134 sin
.sin_family
)) == NULL
) {
1135 sm_ierror("DNS lookup on IP address %s failed",
1136 inet_ntoa(sin
.sin_addr
));
1140 host
= strdup(hp
->h_name
);
1143 sasl_pw_context
[0] = host
;
1144 sasl_pw_context
[1] = user
;
1146 callbacks
[SM_SASL_N_CB_PASS
].context
= sasl_pw_context
;
1148 result
= sasl_client_init(callbacks
);
1150 if (result
!= SASL_OK
) {
1151 sm_ierror("SASL library initialization failed: %s",
1152 sasl_errstring(result
, NULL
, NULL
));
1156 result
= sasl_client_new("smtp", host
, NULL
, SASL_SECURITY_LAYER
, &conn
);
1158 if (result
!= SASL_OK
) {
1159 sm_ierror("SASL client initialization failed: %s",
1160 sasl_errstring(result
, NULL
, NULL
));
1165 * Initialize the security properties
1168 memset(&secprops
, 0, sizeof(secprops
));
1169 secprops
.maxbufsize
= BUFSIZ
;
1170 secprops
.max_ssf
= 0; /* XXX change this when we do encryption */
1171 memset(&extprops
, 0, sizeof(extprops
));
1173 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
1175 if (result
!= SASL_OK
) {
1176 sm_ierror("SASL security property initialization failed: %s",
1177 sasl_errstring(result
, NULL
, NULL
));
1181 result
= sasl_setprop(conn
, SASL_SSF_EXTERNAL
, &extprops
);
1183 if (result
!= SASL_OK
) {
1184 sm_ierror("SASL external property initialization failed: %s",
1185 sasl_errstring(result
, NULL
, NULL
));
1190 * Start the actual protocol. Feed the mech list into the library
1191 * and get out a possible initial challenge
1194 result
= sasl_client_start(conn
, mechlist
, NULL
, NULL
, &buf
, &buflen
,
1197 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1198 sm_ierror("SASL client start failed: %s",
1199 sasl_errstring(result
, NULL
, NULL
));
1204 * If we got an initial challenge, send it as part of the AUTH
1205 * command; otherwise, just send a plain AUTH command.
1209 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1211 if (status
!= SASL_OK
) {
1212 sm_ierror("SASL base64 encode failed: %s",
1213 sasl_errstring(status
, NULL
, NULL
));
1217 status
= smtalk(SM_AUTH
, "AUTH %s %s", chosen_mech
, outbuf
);
1219 status
= smtalk(SM_AUTH
, "AUTH %s", chosen_mech
);
1222 * Now we loop until we either fail, get a SASL_OK, or a 235
1223 * response code. Receive the challenges and process them until
1227 while (result
== SASL_CONTINUE
) {
1230 * If we get a 235 response, that means authentication has
1231 * succeeded and we need to break out of the loop (yes, even if
1232 * we still get SASL_CONTINUE from sasl_client_step()).
1234 * Otherwise, if we get a message that doesn't seem to be a
1235 * valid response, then abort
1240 else if (status
< 300 || status
> 399)
1244 * Special case; a zero-length response from the SMTP server
1245 * is returned as a single =. If we get that, then set buflen
1246 * to be zero. Otherwise, just decode the response.
1249 if (strcmp("=", sm_reply
.text
) == 0) {
1252 result
= sasl_decode64(sm_reply
.text
, sm_reply
.length
,
1255 if (result
!= SASL_OK
) {
1256 smtalk(SM_AUTH
, "*");
1257 sm_ierror("SASL base64 decode failed: %s",
1258 sasl_errstring(result
, NULL
, NULL
));
1263 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
, &buf
, &buflen
);
1265 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
1266 smtalk(SM_AUTH
, "*");
1267 sm_ierror("SASL client negotiation failed: %s",
1268 sasl_errstring(result
, NULL
, NULL
));
1272 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
1275 if (status
!= SASL_OK
) {
1276 smtalk(SM_AUTH
, "*");
1277 sm_ierror("SASL base64 encode failed: %s",
1278 sasl_errstring(status
, NULL
, NULL
));
1282 status
= smtalk(SM_AUTH
, outbuf
);
1286 * Make sure that we got the correct response
1289 if (status
< 200 || status
> 299)
1293 * Depending on the mechanism, we need to do a FINAL call to
1294 * sasl_client_step(). Do that now.
1297 result
= sasl_client_step(conn
, NULL
, 0, NULL
, &buf
, &buflen
);
1299 if (result
!= SASL_OK
) {
1300 sm_ierror("SASL final client negotiation failed: %s",
1301 sasl_errstring(result
, NULL
, NULL
));
1306 * We _should_ have completed the authentication successfully.
1307 * Get a few properties from the authentication exchange.
1310 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (void **) &outbufmax
);
1312 if (result
!= SASL_OK
) {
1313 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1314 sasl_errstring(result
, NULL
, NULL
));
1318 maxoutbuf
= *outbufmax
;
1320 result
= sasl_getprop(conn
, SASL_SSF
, (void **) &ssf
);
1324 if (result
!= SASL_OK
) {
1325 sm_ierror("Cannot retrieve SASL negotiated security strength "
1326 "factor: %s", sasl_errstring(result
, NULL
, NULL
));
1330 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
1339 * Our callback functions to feed data to the SASL library
1343 sm_get_user(void *context
, int id
, const char **result
, unsigned *len
)
1345 char *user
= (char *) context
;
1347 if (! result
|| id
!= SASL_CB_USER
)
1348 return SASL_BADPARAM
;
1352 *len
= strlen(user
);
1358 sm_get_pass(sasl_conn_t
*conn
, void *context
, int id
,
1359 sasl_secret_t
**psecret
)
1361 char **pw_context
= (char **) context
;
1365 if (! psecret
|| id
!= SASL_CB_PASS
)
1366 return SASL_BADPARAM
;
1368 ruserpass(pw_context
[0], &(pw_context
[1]), &pass
);
1372 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
1379 (*psecret
)->len
= len
;
1380 strcpy((*psecret
)->data
, pass
);
1385 #endif /* CYRUS_SASL */
1388 sm_ierror (char *fmt
, ...)
1393 vsnprintf (sm_reply
.text
, sizeof(sm_reply
.text
), fmt
, ap
);
1396 sm_reply
.length
= strlen (sm_reply
.text
);
1397 sm_reply
.code
= NOTOK
;
1404 smtalk (int time
, char *fmt
, ...)
1408 char buffer
[BUFSIZ
];
1411 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
1415 printf ("=> %s\n", buffer
);
1423 if (strcmp (buffer
, ".") == 0)
1425 fprintf (sm_wfp
, "%s\r\n", buffer
);
1429 if (ferror (sm_wfp
))
1430 return sm_werror ();
1431 snprintf (file
, sizeof(file
), "%s%c.bulk", sm_tmpfil
,
1432 (char) (sm_ispool
+ 'a' - 1));
1433 if (rename (sm_tmpfil
, file
) == NOTOK
) {
1437 snprintf (sm_reply
.text
, sizeof(sm_reply
.text
),
1438 "error renaming %s to %s: ", sm_tmpfil
, file
);
1442 if ((s
= strerror (errno
)))
1443 strncpy (bp
, s
, sizeof(sm_reply
.text
) - len
);
1445 snprintf (bp
, sizeof(sm_reply
.text
) - len
,
1446 "unknown error %d", errno
);
1447 sm_reply
.length
= strlen (sm_reply
.text
);
1448 sm_reply
.code
= NOTOK
;
1452 if (sm_wfp
= fopen (sm_tmpfil
, "w"))
1453 chmod (sm_tmpfil
, 0600);
1464 ftruncate (fileno (sm_wfp
), 0L);
1465 fseek (sm_wfp
, 0L, SEEK_SET
);
1484 printf ("<= %d\n", result
);
1488 sm_reply
.text
[sm_reply
.length
= 0] = NULL
;
1489 return (sm_reply
.code
= result
);
1494 alarm ((unsigned) time
);
1495 if ((result
= sm_wrecord (buffer
, strlen (buffer
))) != NOTOK
)
1504 * write the buffer to the open SMTP channel
1508 sm_wrecord (char *buffer
, int len
)
1511 return sm_werror ();
1513 fwrite (buffer
, sizeof(*buffer
), len
, sm_wfp
);
1514 fputs ("\r\n", sm_wfp
);
1517 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1522 sm_wstream (char *buffer
, int len
)
1525 static char lc
= '\0';
1528 return sm_werror ();
1530 if (buffer
== NULL
&& len
== 0) {
1532 fputs ("\r\n", sm_wfp
);
1534 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1537 for (bp
= buffer
; len
> 0; bp
++, len
--) {
1541 fputc ('\r', sm_wfp
);
1546 fputc ('.', sm_wfp
);/* FALL THROUGH */
1550 fputc (*bp
, sm_wfp
);
1551 if (ferror (sm_wfp
))
1552 return sm_werror ();
1557 return (ferror (sm_wfp
) ? sm_werror () : OK
);
1562 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1563 * causes compile problems with the #ifdef MPOP in the middle. Should the
1564 * #ifdef MPOP be removed, remove these #undefs.
1577 strlen (strcpy (sm_reply
.text
, sm_wfp
== NULL
? "no socket opened"
1578 : sm_alarmed
? "write to socket timed out"
1580 : sm_ispool
? "error writing to spool file"
1582 : "error writing to socket"));
1584 return (sm_reply
.code
= NOTOK
);
1591 int i
, code
, cont
, bc
, rc
, more
;
1593 char **ehlo
, buffer
[BUFSIZ
];
1596 static int at_least_once
= 0;
1598 if (at_least_once
) {
1601 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1615 sm_reply
.length
= 0;
1616 sm_reply
.text
[0] = 0;
1618 rc
= sizeof(sm_reply
.text
) - 1;
1620 for (more
= FALSE
; sm_rrecord (bp
= buffer
, &bc
) != NOTOK
;) {
1622 printf ("<= %s\n", buffer
);
1627 && strncmp (buffer
, "250", sizeof("250") - 1) == 0
1628 && (buffer
[3] == '-' || doingEHLO
== 2)
1630 if (doingEHLO
== 2) {
1631 if ((*ehlo
= malloc ((size_t) (strlen (buffer
+ 4) + 1)))) {
1632 strcpy (*ehlo
++, buffer
+ 4);
1634 if (ehlo
>= EHLOkeys
+ MAXEHLO
)
1644 for (; bc
> 0 && (!isascii (*bp
) || !isdigit (*bp
)); bp
++, bc
--)
1650 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1652 if (bc
> 0 && *bp
== '-') {
1655 for (; bc
> 0 && isspace (*bp
); bp
++, bc
--)
1660 if (code
!= sm_reply
.code
|| cont
)
1664 sm_reply
.code
= code
;
1667 strncpy (buffer
, sm_noreply
, sizeof(buffer
));
1669 bc
= strlen (sm_noreply
);
1673 if ((i
= min (bc
, rc
)) > 0) {
1674 strncpy (rp
, bp
, i
);
1677 if (more
&& rc
> strlen (sm_moreply
) + 1) {
1678 strncpy (sm_reply
.text
+ rc
, sm_moreply
, sizeof(sm_reply
.text
) - rc
);
1679 rc
+= strlen (sm_moreply
);
1684 if (sm_reply
.code
< 100) {
1686 printf ("%s\n", sm_reply
.text
);
1692 sm_reply
.length
= rp
- sm_reply
.text
;
1693 return sm_reply
.code
;
1700 sm_rrecord (char *buffer
, int *len
)
1703 return sm_rerror ();
1705 buffer
[*len
= 0] = 0;
1707 fgets (buffer
, BUFSIZ
, sm_rfp
);
1708 *len
= strlen (buffer
);
1709 if (ferror (sm_rfp
) || feof (sm_rfp
))
1710 return sm_rerror ();
1711 if (buffer
[*len
- 1] != '\n')
1712 while (getc (sm_rfp
) != '\n' && !ferror (sm_rfp
) && !feof (sm_rfp
))
1715 if (buffer
[*len
- 2] == '\r')
1717 buffer
[*len
- 1] = 0;
1726 if (sm_mts
== MTS_SMTP
)
1728 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no socket opened"
1729 : sm_alarmed
? "read from socket timed out"
1730 : feof (sm_rfp
) ? "premature end-of-file on socket"
1731 : "error reading from socket"));
1734 strlen (strcpy (sm_reply
.text
, sm_rfp
== NULL
? "no pipe opened"
1735 : sm_alarmed
? "read from pipe timed out"
1736 : feof (sm_rfp
) ? "premature end-of-file on pipe"
1737 : "error reading from pipe"));
1739 return (sm_reply
.code
= NOTOK
);
1746 #ifndef RELIABLE_SIGNALS
1747 SIGNAL (SIGALRM
, alrmser
);
1752 printf ("timed out...\n");
1759 rp_string (int code
)
1762 static char buffer
[BUFSIZ
];
1764 switch (sm_reply
.code
!= NOTOK
? code
: NOTOK
) {
1784 snprintf (buffer
, sizeof(buffer
), "[%s] %s", text
, sm_reply
.text
);
1804 snprintf (buffer
, sizeof(buffer
), "[%s] %3d %s",
1805 text
, sm_reply
.code
, sm_reply
.text
);
1812 static char *broken
[MAXARGS
+ 1];
1815 smail_brkstring (char *strg
, char *brksep
, char *brkterm
)
1822 for (bi
= 0; bi
< MAXARGS
; bi
++) {
1823 while (smail_brkany (c
= *sp
, brksep
))
1825 if (!c
|| smail_brkany (c
, brkterm
)) {
1832 while ((c
= *++sp
) && !smail_brkany (c
, brksep
) && !smail_brkany (c
, brkterm
))
1835 broken
[MAXARGS
] = 0;
1842 * returns 1 if chr in strg, 0 otherwise
1845 smail_brkany (char chr
, char *strg
)
1850 for (sp
= strg
; *sp
; sp
++)
1857 * copy a string array and return pointer to end
1860 smail_copyip (char **p
, char **q
, int len_q
)
1862 while (*p
&& --len_q
> 0)
1881 for (ehlo
= EHLOkeys
; *ehlo
; ehlo
++) {
1883 if (strncmp (ep
, s
, len
) == 0) {
1884 for (ep
+= len
; *ep
== ' '; ep
++)