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);
112 static int sendmail_init (char *, char *, int, int, int, int, int);
114 static int rclient (char *, char *, char *);
115 static int sm_ierror (char *fmt, ...);
116 static int smtalk (int time, char *fmt, ...);
117 static int sm_wrecord (char *, int);
118 static int sm_wstream (char *, int);
119 static int sm_werror (void);
120 static int smhear (void);
121 static int sm_rrecord (char *, int *);
122 static int sm_rerror (void);
123 static RETSIGTYPE alrmser (int);
124 static char *EHLOset (char *);
128 * smtp.c's own static copy of several nmh library subroutines
130 static char **smail_brkstring (char *, char *, char *);
131 static int smail_brkany (char, char *);
132 char **smail_copyip (char **, char **, int);
137 * Function prototypes needed for SASL
140 static int sm_auth_sasl(char *, char *, char *);
141 #endif /* CYRUS_SASL */
143 /* from zotnet/mts/client.c */
144 int client (char *, char *, char *, int, char *, int);
147 sm_init (char *client, char *server, int watch, int verbose,
148 int debug, int onex, int queued, int sasl, char *saslmech,
151 if (sm_mts == MTS_SMTP)
152 return smtp_init (client, server, watch, verbose,
153 debug, onex, queued, sasl, saslmech, user);
155 return sendmail_init (client, server, watch, verbose,
156 debug, onex, queued);
160 smtp_init (char *client, char *server, int watch, int verbose,
161 int debug, int onex, int queued, int sasl, char *saslmech,
166 #endif /* CYRUS_SASL */
167 int result, sd1, sd2;
172 sm_verbose = verbose;
180 if (sm_rfp != NULL && sm_wfp != NULL)
183 if (client == NULL || *client == '\0') {
187 client = LocalName(); /* no clientname -> LocalName */
192 if (client == NULL || *client == '\0')
193 client = "localhost";
196 if ((sd1 = rclient (server, "tcp", "smtp")) == NOTOK)
207 if ((sm_wfp = fdopen (sd1, "w")) == NULL) {
210 return sm_ierror ("unable to fdopen");
213 sm_reply.text[sm_reply.length = 0] = NULL;
214 return (sm_reply.code = RP_OK);
218 if ((sd2 = dup (sd1)) == NOTOK) {
220 return sm_ierror ("unable to dup");
223 SIGNAL (SIGALRM, alrmser);
224 SIGNAL (SIGPIPE, SIG_IGN);
226 if ((sm_rfp = fdopen (sd1, "r")) == NULL
227 || (sm_wfp = fdopen (sd2, "w")) == NULL) {
230 sm_rfp = sm_wfp = NULL;
231 return sm_ierror ("unable to fdopen");
249 * Give EHLO or HELO command
251 if (client && *client) {
253 result = smtalk (SM_HELO, "EHLO %s", client);
256 if (result >= 500 && result <= 599)
257 result = smtalk (SM_HELO, "HELO %s", client);
267 * If the user asked for SASL, then check to see if the SMTP server
268 * supports it. Otherwise, error out (because the SMTP server
269 * might have been spoofed; we don't want to just silently not
274 if (! (server_mechs = EHLOset("AUTH"))) {
276 return sm_ierror("SMTP server does not support SASL");
279 if (saslmech && stringdex(saslmech, server_mechs) == -1) {
281 return sm_ierror("Requested SASL mech \"%s\" is not in the "
282 "list of supported mechanisms:\n%s",
283 saslmech, server_mechs);
286 if (sm_auth_sasl(user, saslmech ? saslmech : server_mechs,
292 #endif /* CYRUS_SASL */
295 if (watch && EHLOset ("XVRB"))
296 smtalk (SM_HELO, "VERB on");
297 if (onex && EHLOset ("XONE"))
298 smtalk (SM_HELO, "ONEX");
299 if (queued && EHLOset ("XQUE"))
300 smtalk (SM_HELO, "QUED");
306 sendmail_init (char *client, char *server, int watch, int verbose,
307 int debug, int onex, int queued)
316 sm_verbose = verbose;
318 if (sm_rfp != NULL && sm_wfp != NULL)
321 if (client == NULL || *client == '\0') {
325 client = LocalName(); /* no clientname -> LocalName */
329 if (client == NULL || *client == '\0')
330 client = "localhost";
333 if (pipe (pdi) == NOTOK)
334 return sm_ierror ("no pipes");
335 if (pipe (pdo) == NOTOK) {
338 return sm_ierror ("no pipes");
341 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
350 return sm_ierror ("unable to fork");
353 if (pdo[0] != fileno (stdin))
354 dup2 (pdo[0], fileno (stdin));
355 if (pdi[1] != fileno (stdout))
356 dup2 (pdi[1], fileno (stdout));
357 if (pdi[1] != fileno (stderr))
358 dup2 (pdi[1], fileno (stderr));
359 for (i = fileno (stderr) + 1; i < NBITS; i++)
363 vec[vecp++] = r1bindex (sendmail, '/');
366 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
367 vec[vecp++] = "-oem";
372 # endif /* not RAND */
373 #endif /* not ZMAILER */
378 execvp (sendmail, vec);
379 fprintf (stderr, "unable to exec ");
381 _exit (-1); /* NOTREACHED */
384 SIGNAL (SIGALRM, alrmser);
385 SIGNAL (SIGPIPE, SIG_IGN);
389 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
390 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
393 sm_rfp = sm_wfp = NULL;
394 return sm_ierror ("unable to fdopen");
409 if (client && *client) {
411 result = smtalk (SM_HELO, "EHLO %s", client);
414 if (500 <= result && result <= 599)
415 result = smtalk (SM_HELO, "HELO %s", client);
429 smtalk (SM_HELO, "ONEX");
432 smtalk (SM_HELO, "VERB on");
439 # define MAXARGS 1000
443 rclient (char *server, char *protocol, char *service)
446 char response[BUFSIZ];
451 if ((sd = client (server, protocol, service, FALSE, response, sizeof(response))) != NOTOK)
455 if (!server && servers && (cp = strchr(servers, '/'))) {
457 char *arguments[MAXARGS];
459 smail_copyip (smail_brkstring (cp = getcpy (servers), " ", "\n"), arguments, MAXARGS);
461 for (ap = arguments; *ap; ap++)
465 if ((dp = strrchr(*ap, '/')) && *++dp == NULL)
467 snprintf (sm_tmpfil, sizeof(sm_tmpfil), "%s/smtpXXXXXX", *ap);
469 sd = mkstemp (sm_tmpfil);
473 if ((sd = creat (sm_tmpfil, 0600)) != NOTOK) {
486 sm_ierror ("%s", response);
492 #include <sys/socket.h>
493 #include <netinet/in.h>
494 #include <arpa/inet.h>
497 #endif /* CYRUS_SASL */
500 sm_winit (int mode, char *from)
505 if (sm_ispool && !sm_wfp) {
506 strlen (strcpy (sm_reply.text, "unable to create new spool file"));
507 sm_reply.code = NOTOK;
530 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
547 sm_wadr (char *mbox, char *host, char *path)
549 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
551 path ? path : "", mbox, host)) {
561 #endif /* SENDMAILBUG */
586 switch (smtalk (SM_DATA, "DATA")) {
595 #endif /* SENDMAILBUG */
612 sm_wtxt (char *buffer, int len)
618 result = sm_wstream (buffer, len);
621 return (result == NOTOK ? RP_BHST : RP_OK);
628 if (sm_wstream ((char *) NULL, 0) == NOTOK)
631 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
639 #endif /* SENDMAILBUG */
657 if (sm_mts == MTS_SENDMAIL) {
668 if (sm_rfp == NULL && sm_wfp == NULL)
673 smtalk (SM_QUIT, "QUIT");
677 sm_note.code = sm_reply.code;
678 strncpy (sm_note.text, sm_reply.text, sm_note.length = sm_reply.length);/* fall */
680 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
682 if (sm_mts == MTS_SMTP)
683 smtalk (SM_QUIT, "QUIT");
685 kill (sm_child, SIGKILL);
690 sm_reply.code = sm_note.code;
691 strncpy (sm_reply.text, sm_note.text, sm_reply.length = sm_note.length);
708 if (sm_rfp != NULL) {
713 if (sm_wfp != NULL) {
719 if (sm_mts == MTS_SMTP) {
724 #endif /* CYRUS_SASL */
726 status = pidwait (sm_child, OK);
730 sm_rfp = sm_wfp = NULL;
731 return (status ? RP_BHST : RP_OK);
740 int cc, i, j, k, result;
742 char *dp, *bp, *cp, s;
743 char buffer[BUFSIZ], sender[BUFSIZ];
747 k = strlen (file) - sizeof(".bulk");
748 if ((fp = fopen (file, "r")) == NULL) {
751 snprintf (sm_reply.text, sizeof(sm_reply.text),
752 "unable to read %s: ", file);
756 if ((s = strerror (errno)))
757 strncpy (bp, s, sizeof(sm_reply.text) - len);
759 snprintf (bp, sizeof(sm_reply.text) - len, "Error %d", errno);
760 sm_reply.length = strlen (sm_reply.text);
761 sm_reply.code = NOTOK;
765 printf ("reading file %s\n", file);
770 while (fgets (buffer, sizeof(buffer), fp)) {
772 strncpy (sender, buffer + sizeof("MAIL FROM:") - 1, sizeof(sender));
773 if (strcmp (buffer, "DATA\r\n") == 0) {
780 printf ("no DATA...\n");
784 snprintf (buffer, sizeof(buffer), "%s.bad", file);
785 rename (file, buffer);
787 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
796 printf ("no %srecipients...\n", j < 1 ? "sender or " : "");
802 if ((cp = malloc ((size_t) (cc = (pos = ftell (fp)) + 1))) == NULL) {
803 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
805 sm_reply.code = NOTOK;
809 fseek (fp, 0L, SEEK_SET);
810 for (dp = cp, i = 0; i++ < j; dp += strlen (dp))
811 if (fgets (dp, cc - (dp - cp), fp) == NULL) {
812 sm_reply.length = strlen (strcpy (sm_reply.text, "premature eof"));
819 for (dp = cp, i = cc - 1; i > 0; dp += cc, i -= cc)
820 if ((cc = write (fileno (sm_wfp), dp, i)) == NOTOK) {
823 strcpy (sm_reply.text, "error writing to server: ",
824 sizeof(sm_reply.text));
828 if ((s = strerror (errno)))
829 strncpy (bp, s, sizeof(sm_reply.text) - len);
831 snprintf (bp, sizeof(sm_reply.text) - len,
832 "unknown error %d", errno);
833 sm_reply.length = strlen (sm_reply.text);
838 printf ("wrote %d octets to server\n", cc);
842 for (dp = cp, i = 0; i++ < j; dp = strchr(dp, '\n'), dp++) {
844 if (bp = strchr(dp, '\r'))
846 printf ("=> %s\n", dp);
852 switch (smhear () + (i == 1 ? 1000 : i != j ? 2000 : 3000)) {
864 smtalk (SM_RSET, "RSET");
892 if (k <= 0 || strcmp (sender, "<>\r\n") == 0)
896 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
897 if ((gp = fopen (buffer, "w+")) == NULL)
899 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
902 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
903 l - 4, l - 4, sender + 1, file);
904 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
905 dtimenow (0), LocalName ());
907 if (bp = strchr(dp, '\r'))
909 fprintf (gp, "=> %s\r\n", dp);
912 fprintf (gp, "<= %s\r\n", rp_string (result));
930 smtalk (SM_RSET, "RSET");
933 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
944 smtalk (SM_RSET, "RSET");
956 #ifdef HAVE_ST_BLKSIZE
959 if (fstat (fileno (sm_wfp), &st) == NOTOK || (cc = st.st_blksize) < BUFSIZ)
964 if ((cp = malloc ((size_t) cc)) == NULL) {
965 smtalk (SM_RSET, "RSET");
966 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
971 fseek (fp, pos, SEEK_SET);
975 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
976 if ((j = fread (cp, sizeof(*cp), i, fp)) == OK) {
980 snprintf (sm_reply.text, sizeof(sm_reply.text),
981 "error reading %s: ", file);
985 if ((s = strerror (errno)))
986 strncpy (bp, s, sizeof(sm_reply.text) - len);
988 snprintf (bp, sizeof(sm_reply.text) - len,
989 "unknown error %d", errno);
990 sm_reply.length = strlen (sm_reply.text);
998 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
999 if ((j = write (fileno (sm_wfp), dp, i)) == NOTOK)
1003 printf ("wrote %d octets to server\n", j);
1012 switch (smhear ()) {
1030 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1041 if (k <= 0 || strcmp (sender, "<>\r\n") == 0) {
1047 ftruncate (fileno (gp), 0L);
1048 fseek (gp, 0L, SEEK_SET);
1051 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1052 if ((gp = fopen (buffer, "w")) == NULL)
1055 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
1056 i = strlen (sender);
1057 fprintf (gp, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1058 i - 4, i - 4, sender + 1, file);
1059 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1060 dtimenow (0), LocalName ());
1065 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp);
1066 fseek (fp, pos, SEEK_SET);
1067 while (fgets (buffer, sizeof(buffer), fp)) {
1068 if (buffer[0] == '-')
1070 if (strcmp (buffer, ".\r\n"))
1073 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp);
1088 * This function implements SASL authentication for SMTP. If this function
1089 * completes successfully, then authentication is successful and we've
1090 * (optionally) negotiated a security layer.
1092 * Right now we don't support session encryption.
1095 sm_auth_sasl(char *user, char *mechlist, char *host)
1097 int result, status, outlen;
1098 unsigned int buflen;
1099 char *buf, outbuf[BUFSIZ];
1100 const char *chosen_mech;
1101 sasl_security_properties_t secprops;
1102 sasl_external_properties_t extprops;
1107 * Initialize the callback contexts
1111 user = getusername();
1113 callbacks[SM_SASL_N_CB_USER].context = user;
1116 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1117 * to us on the command line, then call getpeername and do a
1118 * reverse-address lookup on the IP address to get the name.
1122 struct sockaddr_in sin;
1123 int len = sizeof(sin);
1126 if (getpeername(fileno(sm_wfp), (struct sockaddr *) &sin, &len) < 0) {
1127 sm_ierror("getpeername on SMTP socket failed: %s",
1132 if ((hp = gethostbyaddr((void *) &sin.sin_addr, sizeof(sin.sin_addr),
1133 sin.sin_family)) == NULL) {
1134 sm_ierror("DNS lookup on IP address %s failed",
1135 inet_ntoa(sin.sin_addr));
1139 host = strdup(hp->h_name);
1142 sasl_pw_context[0] = host;
1143 sasl_pw_context[1] = user;
1145 callbacks[SM_SASL_N_CB_PASS].context = sasl_pw_context;
1147 result = sasl_client_init(callbacks);
1149 if (result != SASL_OK) {
1150 sm_ierror("SASL library initialization failed: %s",
1151 sasl_errstring(result, NULL, NULL));
1155 result = sasl_client_new("smtp", host, NULL, SASL_SECURITY_LAYER, &conn);
1157 if (result != SASL_OK) {
1158 sm_ierror("SASL client initialization failed: %s",
1159 sasl_errstring(result, NULL, NULL));
1164 * Initialize the security properties
1167 memset(&secprops, 0, sizeof(secprops));
1168 secprops.maxbufsize = BUFSIZ;
1169 secprops.max_ssf = 0; /* XXX change this when we do encryption */
1170 memset(&extprops, 0, sizeof(extprops));
1172 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
1174 if (result != SASL_OK) {
1175 sm_ierror("SASL security property initialization failed: %s",
1176 sasl_errstring(result, NULL, NULL));
1180 result = sasl_setprop(conn, SASL_SSF_EXTERNAL, &extprops);
1182 if (result != SASL_OK) {
1183 sm_ierror("SASL external property initialization failed: %s",
1184 sasl_errstring(result, NULL, NULL));
1189 * Start the actual protocol. Feed the mech list into the library
1190 * and get out a possible initial challenge
1193 result = sasl_client_start(conn, mechlist, NULL, NULL, &buf, &buflen,
1196 if (result != SASL_OK && result != SASL_CONTINUE) {
1197 sm_ierror("SASL client start failed: %s",
1198 sasl_errstring(result, NULL, NULL));
1203 * If we got an initial challenge, send it as part of the AUTH
1204 * command; otherwise, just send a plain AUTH command.
1208 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1210 if (status != SASL_OK) {
1211 sm_ierror("SASL base64 encode failed: %s",
1212 sasl_errstring(status, NULL, NULL));
1216 status = smtalk(SM_AUTH, "AUTH %s %s", chosen_mech, outbuf);
1218 status = smtalk(SM_AUTH, "AUTH %s", chosen_mech);
1221 * Now we loop until we either fail, get a SASL_OK, or a 235
1222 * response code. Receive the challenges and process them until
1226 while (result == SASL_CONTINUE) {
1229 * If we get a 235 response, that means authentication has
1230 * succeeded and we need to break out of the loop (yes, even if
1231 * we still get SASL_CONTINUE from sasl_client_step()).
1233 * Otherwise, if we get a message that doesn't seem to be a
1234 * valid response, then abort
1239 else if (status < 300 || status > 399)
1243 * Special case; a zero-length response from the SMTP server
1244 * is returned as a single =. If we get that, then set buflen
1245 * to be zero. Otherwise, just decode the response.
1248 if (strcmp("=", sm_reply.text) == 0) {
1251 result = sasl_decode64(sm_reply.text, sm_reply.length,
1254 if (result != SASL_OK) {
1255 smtalk(SM_AUTH, "*");
1256 sm_ierror("SASL base64 decode failed: %s",
1257 sasl_errstring(result, NULL, NULL));
1262 result = sasl_client_step(conn, outbuf, outlen, NULL, &buf, &buflen);
1264 if (result != SASL_OK && result != SASL_CONTINUE) {
1265 smtalk(SM_AUTH, "*");
1266 sm_ierror("SASL client negotiation failed: %s",
1267 sasl_errstring(result, NULL, NULL));
1271 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1274 if (status != SASL_OK) {
1275 smtalk(SM_AUTH, "*");
1276 sm_ierror("SASL base64 encode failed: %s",
1277 sasl_errstring(status, NULL, NULL));
1281 status = smtalk(SM_AUTH, outbuf);
1285 * Make sure that we got the correct response
1288 if (status < 200 || status > 299)
1292 * Depending on the mechanism, we need to do a FINAL call to
1293 * sasl_client_step(). Do that now.
1296 result = sasl_client_step(conn, NULL, 0, NULL, &buf, &buflen);
1298 if (result != SASL_OK) {
1299 sm_ierror("SASL final client negotiation failed: %s",
1300 sasl_errstring(result, NULL, NULL));
1305 * We _should_ have completed the authentication successfully.
1306 * Get a few properties from the authentication exchange.
1309 result = sasl_getprop(conn, SASL_MAXOUTBUF, (void **) &outbufmax);
1311 if (result != SASL_OK) {
1312 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1313 sasl_errstring(result, NULL, NULL));
1317 maxoutbuf = *outbufmax;
1319 result = sasl_getprop(conn, SASL_SSF, (void **) &ssf);
1323 if (result != SASL_OK) {
1324 sm_ierror("Cannot retrieve SASL negotiated security strength "
1325 "factor: %s", sasl_errstring(result, NULL, NULL));
1329 if (maxoutbuf == 0 || maxoutbuf > BUFSIZ)
1338 * Our callback functions to feed data to the SASL library
1342 sm_get_user(void *context, int id, const char **result, unsigned *len)
1344 char *user = (char *) context;
1346 if (! result || id != SASL_CB_USER)
1347 return SASL_BADPARAM;
1351 *len = strlen(user);
1357 sm_get_pass(sasl_conn_t *conn, void *context, int id,
1358 sasl_secret_t **psecret)
1360 char **pw_context = (char **) context;
1364 if (! psecret || id != SASL_CB_PASS)
1365 return SASL_BADPARAM;
1367 ruserpass(pw_context[0], &(pw_context[1]), &pass);
1371 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
1378 (*psecret)->len = len;
1379 strcpy((*psecret)->data, pass);
1384 #endif /* CYRUS_SASL */
1387 sm_ierror (char *fmt, ...)
1392 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
1395 sm_reply.length = strlen (sm_reply.text);
1396 sm_reply.code = NOTOK;
1403 smtalk (int time, char *fmt, ...)
1407 char buffer[BUFSIZ];
1410 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1414 printf ("=> %s\n", buffer);
1422 if (strcmp (buffer, ".") == 0)
1424 fprintf (sm_wfp, "%s\r\n", buffer);
1428 if (ferror (sm_wfp))
1429 return sm_werror ();
1430 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
1431 (char) (sm_ispool + 'a' - 1));
1432 if (rename (sm_tmpfil, file) == NOTOK) {
1436 snprintf (sm_reply.text, sizeof(sm_reply.text),
1437 "error renaming %s to %s: ", sm_tmpfil, file);
1441 if ((s = strerror (errno)))
1442 strncpy (bp, s, sizeof(sm_reply.text) - len);
1444 snprintf (bp, sizeof(sm_reply.text) - len,
1445 "unknown error %d", errno);
1446 sm_reply.length = strlen (sm_reply.text);
1447 sm_reply.code = NOTOK;
1451 if (sm_wfp = fopen (sm_tmpfil, "w"))
1452 chmod (sm_tmpfil, 0600);
1463 ftruncate (fileno (sm_wfp), 0L);
1464 fseek (sm_wfp, 0L, SEEK_SET);
1483 printf ("<= %d\n", result);
1487 sm_reply.text[sm_reply.length = 0] = NULL;
1488 return (sm_reply.code = result);
1493 alarm ((unsigned) time);
1494 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1503 * write the buffer to the open SMTP channel
1507 sm_wrecord (char *buffer, int len)
1510 return sm_werror ();
1512 fwrite (buffer, sizeof(*buffer), len, sm_wfp);
1513 fputs ("\r\n", sm_wfp);
1516 return (ferror (sm_wfp) ? sm_werror () : OK);
1521 sm_wstream (char *buffer, int len)
1524 static char lc = '\0';
1527 return sm_werror ();
1529 if (buffer == NULL && len == 0) {
1531 fputs ("\r\n", sm_wfp);
1533 return (ferror (sm_wfp) ? sm_werror () : OK);
1536 for (bp = buffer; len > 0; bp++, len--) {
1540 fputc ('\r', sm_wfp);
1545 fputc ('.', sm_wfp);/* FALL THROUGH */
1549 fputc (*bp, sm_wfp);
1550 if (ferror (sm_wfp))
1551 return sm_werror ();
1556 return (ferror (sm_wfp) ? sm_werror () : OK);
1561 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1562 * causes compile problems with the #ifdef MPOP in the middle. Should the
1563 * #ifdef MPOP be removed, remove these #undefs.
1576 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1577 : sm_alarmed ? "write to socket timed out"
1579 : sm_ispool ? "error writing to spool file"
1581 : "error writing to socket"));
1583 return (sm_reply.code = NOTOK);
1590 int i, code, cont, bc, rc, more;
1592 char **ehlo, buffer[BUFSIZ];
1595 static int at_least_once = 0;
1597 if (at_least_once) {
1600 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1614 sm_reply.length = 0;
1615 sm_reply.text[0] = 0;
1617 rc = sizeof(sm_reply.text) - 1;
1619 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
1621 printf ("<= %s\n", buffer);
1626 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1627 && (buffer[3] == '-' || doingEHLO == 2)
1629 if (doingEHLO == 2) {
1630 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1631 strcpy (*ehlo++, buffer + 4);
1633 if (ehlo >= EHLOkeys + MAXEHLO)
1643 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1649 for (; bc > 0 && isspace (*bp); bp++, bc--)
1651 if (bc > 0 && *bp == '-') {
1654 for (; bc > 0 && isspace (*bp); bp++, bc--)
1659 if (code != sm_reply.code || cont)
1663 sm_reply.code = code;
1666 strncpy (buffer, sm_noreply, sizeof(buffer));
1668 bc = strlen (sm_noreply);
1672 if ((i = min (bc, rc)) > 0) {
1673 strncpy (rp, bp, i);
1676 if (more && rc > strlen (sm_moreply) + 1) {
1677 strncpy (sm_reply.text + rc, sm_moreply, sizeof(sm_reply.text) - rc);
1678 rc += strlen (sm_moreply);
1683 if (sm_reply.code < 100) {
1685 printf ("%s\n", sm_reply.text);
1691 sm_reply.length = rp - sm_reply.text;
1692 return sm_reply.code;
1699 sm_rrecord (char *buffer, int *len)
1702 return sm_rerror ();
1704 buffer[*len = 0] = 0;
1706 fgets (buffer, BUFSIZ, sm_rfp);
1707 *len = strlen (buffer);
1708 if (ferror (sm_rfp) || feof (sm_rfp))
1709 return sm_rerror ();
1710 if (buffer[*len - 1] != '\n')
1711 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
1714 if (buffer[*len - 2] == '\r')
1716 buffer[*len - 1] = 0;
1725 if (sm_mts == MTS_SMTP)
1727 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1728 : sm_alarmed ? "read from socket timed out"
1729 : feof (sm_rfp) ? "premature end-of-file on socket"
1730 : "error reading from socket"));
1733 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1734 : sm_alarmed ? "read from pipe timed out"
1735 : feof (sm_rfp) ? "premature end-of-file on pipe"
1736 : "error reading from pipe"));
1738 return (sm_reply.code = NOTOK);
1745 #ifndef RELIABLE_SIGNALS
1746 SIGNAL (SIGALRM, alrmser);
1751 printf ("timed out...\n");
1758 rp_string (int code)
1761 static char buffer[BUFSIZ];
1763 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1783 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1803 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1804 text, sm_reply.code, sm_reply.text);
1811 static char *broken[MAXARGS + 1];
1814 smail_brkstring (char *strg, char *brksep, char *brkterm)
1821 for (bi = 0; bi < MAXARGS; bi++) {
1822 while (smail_brkany (c = *sp, brksep))
1824 if (!c || smail_brkany (c, brkterm)) {
1831 while ((c = *++sp) && !smail_brkany (c, brksep) && !smail_brkany (c, brkterm))
1834 broken[MAXARGS] = 0;
1841 * returns 1 if chr in strg, 0 otherwise
1844 smail_brkany (char chr, char *strg)
1849 for (sp = strg; *sp; sp++)
1856 * copy a string array and return pointer to end
1859 smail_copyip (char **p, char **q, int len_q)
1861 while (*p && --len_q > 0)
1880 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1882 if (strncmp (ep, s, len) == 0) {
1883 for (ep += len; *ep == ' '; ep++)