2 * smtp.c -- nmh SMTP interface
9 #include <zotnet/mts/mts.h>
11 #include <h/signals.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #endif /* CYRUS_SASL */
26 * This module implements an interface to SendMail very similar
27 * to the MMDF mm_(3) routines. The sm_() routines herein talk
28 * SMTP to a sendmail process, mapping SMTP reply codes into
33 * On older 4.2BSD machines without the POSIX function `sigaction',
34 * the alarm handing stuff for time-outs will NOT work due to the way
35 * syscalls get restarted. This is not really crucial, since SendMail
36 * is generally well-behaved in this area.
41 * It appears that some versions of Sendmail will return Code 451
42 * when they don't really want to indicate a failure.
43 * "Code 451 almost always means sendmail has deferred; we don't
44 * really want bomb out at this point since sendmail will rectify
45 * things later." So, if you define SENDMAILBUG, Code 451 is
46 * considered the same as Code 250. Yuck!
53 #define NBITS ((sizeof (int)) * 8)
56 * these codes must all be different!
58 #define SM_OPEN 90 /* Changed from 30 in case of nameserver flakiness */
70 static int sm_addrs = 0;
71 static int sm_alarmed = 0;
72 static int sm_child = NOTOK;
73 static int sm_debug = 0;
74 static int sm_nl = TRUE;
75 static int sm_verbose = 0;
77 static FILE *sm_rfp = NULL;
78 static FILE *sm_wfp = NULL;
81 static int sm_ispool = 0;
82 static char sm_tmpfil[BUFSIZ];
87 * Some globals needed by SASL
90 static sasl_conn_t *conn = NULL; /* SASL connection state */
91 static int sasl_complete = 0; /* Has authentication succeded? */
92 static sasl_ssf_t sasl_ssf; /* Our security strength factor */
93 static char *sasl_pw_context[2]; /* Context to pass into sm_get_pass */
94 static int maxoutbuf; /* Maximum crypto output buffer */
95 static int sm_get_user(void *, int, const char **, unsigned *);
96 static int sm_get_pass(sasl_conn_t *, void *, int, sasl_secret_t **);
98 static sasl_callback_t callbacks[] = {
99 { SASL_CB_USER, sm_get_user, NULL },
100 #define SM_SASL_N_CB_USER 0
101 { SASL_CB_PASS, sm_get_pass, NULL },
102 #define SM_SASL_N_CB_PASS 1
103 { SASL_CB_LIST_END, NULL, NULL },
105 #endif /* CYRUS_SASL */
107 static char *sm_noreply = "No reply text given";
108 static char *sm_moreply = "; ";
110 struct smtp sm_reply; /* global... */
114 static int doingEHLO;
115 char *EHLOkeys[MAXEHLO + 1];
120 static int smtp_init (char *, char *, int, int, int, int, int, int,
122 static int sendmail_init (char *, char *, int, int, int, int, int);
124 static int rclient (char *, char *, char *);
125 static int sm_ierror (char *fmt, ...);
126 static int smtalk (int time, char *fmt, ...);
127 static int sm_wrecord (char *, int);
128 static int sm_wstream (char *, int);
129 static int sm_werror (void);
130 static int smhear (void);
131 static int sm_rrecord (char *, int *);
132 static int sm_rerror (void);
133 static RETSIGTYPE alrmser (int);
134 static char *EHLOset (char *);
138 * smtp.c's own static copy of several nmh library subroutines
140 static char **smail_brkstring (char *, char *, char *);
141 static int smail_brkany (char, char *);
142 char **smail_copyip (char **, char **, int);
147 * Function prototypes needed for SASL
150 static int sm_auth_sasl(char *, char *, char *);
151 #endif /* CYRUS_SASL */
153 /* from zotnet/mts/client.c */
154 int client (char *, char *, char *, int, char *, int);
157 sm_init (char *client, char *server, int watch, int verbose,
158 int debug, int onex, int queued, int sasl, char *saslmech,
161 if (sm_mts == MTS_SMTP)
162 return smtp_init (client, server, watch, verbose,
163 debug, onex, queued, sasl, saslmech, user);
165 return sendmail_init (client, server, watch, verbose,
166 debug, onex, queued);
170 smtp_init (char *client, char *server, int watch, int verbose,
171 int debug, int onex, int queued, int sasl, char *saslmech,
176 #endif /* CYRUS_SASL */
177 int result, sd1, sd2;
182 sm_verbose = verbose;
190 if (sm_rfp != NULL && sm_wfp != NULL)
193 if (client == NULL || *client == '\0') {
197 client = LocalName(); /* no clientname -> LocalName */
202 if (client == NULL || *client == '\0')
203 client = "localhost";
206 if ((sd1 = rclient (server, "tcp", "smtp")) == NOTOK)
217 if ((sm_wfp = fdopen (sd1, "w")) == NULL) {
220 return sm_ierror ("unable to fdopen");
223 sm_reply.text[sm_reply.length = 0] = NULL;
224 return (sm_reply.code = RP_OK);
228 if ((sd2 = dup (sd1)) == NOTOK) {
230 return sm_ierror ("unable to dup");
233 SIGNAL (SIGALRM, alrmser);
234 SIGNAL (SIGPIPE, SIG_IGN);
236 if ((sm_rfp = fdopen (sd1, "r")) == NULL
237 || (sm_wfp = fdopen (sd2, "w")) == NULL) {
240 sm_rfp = sm_wfp = NULL;
241 return sm_ierror ("unable to fdopen");
259 * Give EHLO or HELO command
261 if (client && *client) {
263 result = smtalk (SM_HELO, "EHLO %s", client);
266 if (result >= 500 && result <= 599)
267 result = smtalk (SM_HELO, "HELO %s", client);
277 * If the user asked for SASL, then check to see if the SMTP server
278 * supports it. Otherwise, error out (because the SMTP server
279 * might have been spoofed; we don't want to just silently not
284 if (! (server_mechs = EHLOset("AUTH"))) {
286 return sm_ierror("SMTP server does not support SASL");
289 if (saslmech && stringdex(saslmech, server_mechs) == -1) {
291 return sm_ierror("Requested SASL mech \"%s\" is not in the "
292 "list of supported mechanisms:\n%s",
293 saslmech, server_mechs);
296 if (sm_auth_sasl(user, saslmech ? saslmech : server_mechs,
302 #endif /* CYRUS_SASL */
305 if (watch && EHLOset ("XVRB"))
306 smtalk (SM_HELO, "VERB on");
307 if (onex && EHLOset ("XONE"))
308 smtalk (SM_HELO, "ONEX");
309 if (queued && EHLOset ("XQUE"))
310 smtalk (SM_HELO, "QUED");
316 sendmail_init (char *client, char *server, int watch, int verbose,
317 int debug, int onex, int queued)
326 sm_verbose = verbose;
328 if (sm_rfp != NULL && sm_wfp != NULL)
331 if (client == NULL || *client == '\0') {
335 client = LocalName(); /* no clientname -> LocalName */
339 if (client == NULL || *client == '\0')
340 client = "localhost";
343 if (pipe (pdi) == NOTOK)
344 return sm_ierror ("no pipes");
345 if (pipe (pdo) == NOTOK) {
348 return sm_ierror ("no pipes");
351 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
360 return sm_ierror ("unable to fork");
363 if (pdo[0] != fileno (stdin))
364 dup2 (pdo[0], fileno (stdin));
365 if (pdi[1] != fileno (stdout))
366 dup2 (pdi[1], fileno (stdout));
367 if (pdi[1] != fileno (stderr))
368 dup2 (pdi[1], fileno (stderr));
369 for (i = fileno (stderr) + 1; i < NBITS; i++)
373 vec[vecp++] = r1bindex (sendmail, '/');
376 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
377 vec[vecp++] = "-oem";
382 # endif /* not RAND */
383 #endif /* not ZMAILER */
388 execvp (sendmail, vec);
389 fprintf (stderr, "unable to exec ");
391 _exit (-1); /* NOTREACHED */
394 SIGNAL (SIGALRM, alrmser);
395 SIGNAL (SIGPIPE, SIG_IGN);
399 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
400 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
403 sm_rfp = sm_wfp = NULL;
404 return sm_ierror ("unable to fdopen");
419 if (client && *client) {
421 result = smtalk (SM_HELO, "EHLO %s", client);
424 if (500 <= result && result <= 599)
425 result = smtalk (SM_HELO, "HELO %s", client);
439 smtalk (SM_HELO, "ONEX");
442 smtalk (SM_HELO, "VERB on");
449 # define MAXARGS 1000
453 rclient (char *server, char *protocol, char *service)
456 char response[BUFSIZ];
461 if ((sd = client (server, protocol, service, FALSE, response, sizeof(response))) != NOTOK)
465 if (!server && servers && (cp = strchr(servers, '/'))) {
467 char *arguments[MAXARGS];
469 smail_copyip (smail_brkstring (cp = getcpy (servers), " ", "\n"), arguments, MAXARGS);
471 for (ap = arguments; *ap; ap++)
475 if ((dp = strrchr(*ap, '/')) && *++dp == NULL)
477 snprintf (sm_tmpfil, sizeof(sm_tmpfil), "%s/smtpXXXXXX", *ap);
479 sd = mkstemp (sm_tmpfil);
483 if ((sd = creat (sm_tmpfil, 0600)) != NOTOK) {
496 sm_ierror ("%s", response);
502 #include <sys/socket.h>
503 #include <netinet/in.h>
504 #include <arpa/inet.h>
507 #endif /* CYRUS_SASL */
510 sm_winit (int mode, char *from)
515 if (sm_ispool && !sm_wfp) {
516 strlen (strcpy (sm_reply.text, "unable to create new spool file"));
517 sm_reply.code = NOTOK;
540 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
557 sm_wadr (char *mbox, char *host, char *path)
559 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
561 path ? path : "", mbox, host)) {
571 #endif /* SENDMAILBUG */
596 switch (smtalk (SM_DATA, "DATA")) {
605 #endif /* SENDMAILBUG */
622 sm_wtxt (char *buffer, int len)
628 result = sm_wstream (buffer, len);
631 return (result == NOTOK ? RP_BHST : RP_OK);
638 if (sm_wstream ((char *) NULL, 0) == NOTOK)
641 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
649 #endif /* SENDMAILBUG */
667 if (sm_mts == MTS_SENDMAIL) {
678 if (sm_rfp == NULL && sm_wfp == NULL)
683 smtalk (SM_QUIT, "QUIT");
687 sm_note.code = sm_reply.code;
688 strncpy (sm_note.text, sm_reply.text, sm_note.length = sm_reply.length);/* fall */
690 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
692 if (sm_mts == MTS_SMTP)
693 smtalk (SM_QUIT, "QUIT");
695 kill (sm_child, SIGKILL);
700 sm_reply.code = sm_note.code;
701 strncpy (sm_reply.text, sm_note.text, sm_reply.length = sm_note.length);
718 if (sm_rfp != NULL) {
723 if (sm_wfp != NULL) {
729 if (sm_mts == MTS_SMTP) {
734 #endif /* CYRUS_SASL */
736 status = pidwait (sm_child, OK);
740 sm_rfp = sm_wfp = NULL;
741 return (status ? RP_BHST : RP_OK);
750 int cc, i, j, k, result;
752 char *dp, *bp, *cp, s;
753 char buffer[BUFSIZ], sender[BUFSIZ];
757 k = strlen (file) - sizeof(".bulk");
758 if ((fp = fopen (file, "r")) == NULL) {
761 snprintf (sm_reply.text, sizeof(sm_reply.text),
762 "unable to read %s: ", file);
766 if ((s = strerror (errno)))
767 strncpy (bp, s, sizeof(sm_reply.text) - len);
769 snprintf (bp, sizeof(sm_reply.text) - len, "Error %d", errno);
770 sm_reply.length = strlen (sm_reply.text);
771 sm_reply.code = NOTOK;
775 printf ("reading file %s\n", file);
780 while (fgets (buffer, sizeof(buffer), fp)) {
782 strncpy (sender, buffer + sizeof("MAIL FROM:") - 1, sizeof(sender));
783 if (strcmp (buffer, "DATA\r\n") == 0) {
790 printf ("no DATA...\n");
794 snprintf (buffer, sizeof(buffer), "%s.bad", file);
795 rename (file, buffer);
797 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
806 printf ("no %srecipients...\n", j < 1 ? "sender or " : "");
812 if ((cp = malloc ((size_t) (cc = (pos = ftell (fp)) + 1))) == NULL) {
813 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
815 sm_reply.code = NOTOK;
819 fseek (fp, 0L, SEEK_SET);
820 for (dp = cp, i = 0; i++ < j; dp += strlen (dp))
821 if (fgets (dp, cc - (dp - cp), fp) == NULL) {
822 sm_reply.length = strlen (strcpy (sm_reply.text, "premature eof"));
829 for (dp = cp, i = cc - 1; i > 0; dp += cc, i -= cc)
830 if ((cc = write (fileno (sm_wfp), dp, i)) == NOTOK) {
833 strcpy (sm_reply.text, "error writing to server: ",
834 sizeof(sm_reply.text));
838 if ((s = strerror (errno)))
839 strncpy (bp, s, sizeof(sm_reply.text) - len);
841 snprintf (bp, sizeof(sm_reply.text) - len,
842 "unknown error %d", errno);
843 sm_reply.length = strlen (sm_reply.text);
848 printf ("wrote %d octets to server\n", cc);
852 for (dp = cp, i = 0; i++ < j; dp = strchr(dp, '\n'), dp++) {
854 if (bp = strchr(dp, '\r'))
856 printf ("=> %s\n", dp);
862 switch (smhear () + (i == 1 ? 1000 : i != j ? 2000 : 3000)) {
874 smtalk (SM_RSET, "RSET");
902 if (k <= 0 || strcmp (sender, "<>\r\n") == 0)
906 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
907 if ((gp = fopen (buffer, "w+")) == NULL)
909 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
912 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
913 l - 4, l - 4, sender + 1, file);
914 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
915 dtimenow (0), LocalName ());
917 if (bp = strchr(dp, '\r'))
919 fprintf (gp, "=> %s\r\n", dp);
922 fprintf (gp, "<= %s\r\n", rp_string (result));
940 smtalk (SM_RSET, "RSET");
943 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
954 smtalk (SM_RSET, "RSET");
966 #ifdef HAVE_ST_BLKSIZE
969 if (fstat (fileno (sm_wfp), &st) == NOTOK || (cc = st.st_blksize) < BUFSIZ)
974 if ((cp = malloc ((size_t) cc)) == NULL) {
975 smtalk (SM_RSET, "RSET");
976 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
981 fseek (fp, pos, SEEK_SET);
985 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
986 if ((j = fread (cp, sizeof(*cp), i, fp)) == OK) {
990 snprintf (sm_reply.text, sizeof(sm_reply.text),
991 "error reading %s: ", file);
995 if ((s = strerror (errno)))
996 strncpy (bp, s, sizeof(sm_reply.text) - len);
998 snprintf (bp, sizeof(sm_reply.text) - len,
999 "unknown error %d", errno);
1000 sm_reply.length = strlen (sm_reply.text);
1008 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
1009 if ((j = write (fileno (sm_wfp), dp, i)) == NOTOK)
1013 printf ("wrote %d octets to server\n", j);
1022 switch (smhear ()) {
1040 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1051 if (k <= 0 || strcmp (sender, "<>\r\n") == 0) {
1057 ftruncate (fileno (gp), 0L);
1058 fseek (gp, 0L, SEEK_SET);
1061 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1062 if ((gp = fopen (buffer, "w")) == NULL)
1065 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
1066 i = strlen (sender);
1067 fprintf (gp, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1068 i - 4, i - 4, sender + 1, file);
1069 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1070 dtimenow (0), LocalName ());
1075 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp);
1076 fseek (fp, pos, SEEK_SET);
1077 while (fgets (buffer, sizeof(buffer), fp)) {
1078 if (buffer[0] == '-')
1080 if (strcmp (buffer, ".\r\n"))
1083 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp);
1098 * This function implements SASL authentication for SMTP. If this function
1099 * completes successfully, then authentication is successful and we've
1100 * (optionally) negotiated a security layer.
1102 * Right now we don't support session encryption.
1105 sm_auth_sasl(char *user, char *mechlist, char *host)
1107 int result, status, outlen;
1108 unsigned int buflen;
1109 char *buf, outbuf[BUFSIZ];
1110 const char *chosen_mech;
1111 sasl_security_properties_t secprops;
1112 sasl_external_properties_t extprops;
1117 * Initialize the callback contexts
1121 user = getusername();
1123 callbacks[SM_SASL_N_CB_USER].context = user;
1126 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1127 * to us on the command line, then call getpeername and do a
1128 * reverse-address lookup on the IP address to get the name.
1132 struct sockaddr_in sin;
1133 int len = sizeof(sin);
1136 if (getpeername(fileno(sm_wfp), (struct sockaddr *) &sin, &len) < 0) {
1137 sm_ierror("getpeername on SMTP socket failed: %s",
1142 if ((hp = gethostbyaddr((void *) &sin.sin_addr, sizeof(sin.sin_addr),
1143 sin.sin_family)) == NULL) {
1144 sm_ierror("DNS lookup on IP address %s failed",
1145 inet_ntoa(sin.sin_addr));
1149 host = strdup(hp->h_name);
1152 sasl_pw_context[0] = host;
1153 sasl_pw_context[1] = user;
1155 callbacks[SM_SASL_N_CB_PASS].context = sasl_pw_context;
1157 result = sasl_client_init(callbacks);
1159 if (result != SASL_OK) {
1160 sm_ierror("SASL library initialization failed: %s",
1161 sasl_errstring(result, NULL, NULL));
1165 result = sasl_client_new("smtp", host, NULL, SASL_SECURITY_LAYER, &conn);
1167 if (result != SASL_OK) {
1168 sm_ierror("SASL client initialization failed: %s",
1169 sasl_errstring(result, NULL, NULL));
1174 * Initialize the security properties
1177 memset(&secprops, 0, sizeof(secprops));
1178 secprops.maxbufsize = BUFSIZ;
1179 secprops.max_ssf = 0; /* XXX change this when we do encryption */
1180 memset(&extprops, 0, sizeof(extprops));
1182 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
1184 if (result != SASL_OK) {
1185 sm_ierror("SASL security property initialization failed: %s",
1186 sasl_errstring(result, NULL, NULL));
1190 result = sasl_setprop(conn, SASL_SSF_EXTERNAL, &extprops);
1192 if (result != SASL_OK) {
1193 sm_ierror("SASL external property initialization failed: %s",
1194 sasl_errstring(result, NULL, NULL));
1199 * Start the actual protocol. Feed the mech list into the library
1200 * and get out a possible initial challenge
1203 result = sasl_client_start(conn, mechlist, NULL, NULL, &buf, &buflen,
1206 if (result != SASL_OK && result != SASL_CONTINUE) {
1207 sm_ierror("SASL client start failed: %s",
1208 sasl_errstring(result, NULL, NULL));
1213 * If we got an initial challenge, send it as part of the AUTH
1214 * command; otherwise, just send a plain AUTH command.
1218 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1220 if (status != SASL_OK) {
1221 sm_ierror("SASL base64 encode failed: %s",
1222 sasl_errstring(status, NULL, NULL));
1226 status = smtalk(SM_AUTH, "AUTH %s %s", chosen_mech, outbuf);
1228 status = smtalk(SM_AUTH, "AUTH %s", chosen_mech);
1231 * Now we loop until we either fail, get a SASL_OK, or a 235
1232 * response code. Receive the challenges and process them until
1236 while (result == SASL_CONTINUE) {
1239 * If we get a 235 response, that means authentication has
1240 * succeeded and we need to break out of the loop (yes, even if
1241 * we still get SASL_CONTINUE from sasl_client_step()).
1243 * Otherwise, if we get a message that doesn't seem to be a
1244 * valid response, then abort
1249 else if (status < 300 || status > 399)
1253 * Special case; a zero-length response from the SMTP server
1254 * is returned as a single =. If we get that, then set buflen
1255 * to be zero. Otherwise, just decode the response.
1258 if (strcmp("=", sm_reply.text) == 0) {
1261 result = sasl_decode64(sm_reply.text, sm_reply.length,
1264 if (result != SASL_OK) {
1265 smtalk(SM_AUTH, "*");
1266 sm_ierror("SASL base64 decode failed: %s",
1267 sasl_errstring(result, NULL, NULL));
1272 result = sasl_client_step(conn, outbuf, outlen, NULL, &buf, &buflen);
1274 if (result != SASL_OK && result != SASL_CONTINUE) {
1275 smtalk(SM_AUTH, "*");
1276 sm_ierror("SASL client negotiation failed: %s",
1277 sasl_errstring(result, NULL, NULL));
1281 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1284 if (status != SASL_OK) {
1285 smtalk(SM_AUTH, "*");
1286 sm_ierror("SASL base64 encode failed: %s",
1287 sasl_errstring(status, NULL, NULL));
1291 status = smtalk(SM_AUTH, outbuf);
1295 * Make sure that we got the correct response
1298 if (status < 200 || status > 299)
1302 * Depending on the mechanism, we need to do a FINAL call to
1303 * sasl_client_step(). Do that now.
1306 result = sasl_client_step(conn, NULL, 0, NULL, &buf, &buflen);
1308 if (result != SASL_OK) {
1309 sm_ierror("SASL final client negotiation failed: %s",
1310 sasl_errstring(result, NULL, NULL));
1315 * We _should_ have completed the authentication successfully.
1316 * Get a few properties from the authentication exchange.
1319 result = sasl_getprop(conn, SASL_MAXOUTBUF, (void **) &outbufmax);
1321 if (result != SASL_OK) {
1322 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1323 sasl_errstring(result, NULL, NULL));
1327 maxoutbuf = *outbufmax;
1329 result = sasl_getprop(conn, SASL_SSF, (void **) &ssf);
1333 if (result != SASL_OK) {
1334 sm_ierror("Cannot retrieve SASL negotiated security strength "
1335 "factor: %s", sasl_errstring(result, NULL, NULL));
1339 if (maxoutbuf == 0 || maxoutbuf > BUFSIZ)
1348 * Our callback functions to feed data to the SASL library
1352 sm_get_user(void *context, int id, const char **result, unsigned *len)
1354 char *user = (char *) context;
1356 if (! result || id != SASL_CB_USER)
1357 return SASL_BADPARAM;
1361 *len = strlen(user);
1367 sm_get_pass(sasl_conn_t *conn, void *context, int id,
1368 sasl_secret_t **psecret)
1370 char **pw_context = (char **) context;
1374 if (! psecret || id != SASL_CB_PASS)
1375 return SASL_BADPARAM;
1377 ruserpass(pw_context[0], &(pw_context[1]), &pass);
1381 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
1388 (*psecret)->len = len;
1389 strcpy((*psecret)->data, pass);
1394 #endif /* CYRUS_SASL */
1397 sm_ierror (char *fmt, ...)
1402 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
1405 sm_reply.length = strlen (sm_reply.text);
1406 sm_reply.code = NOTOK;
1413 smtalk (int time, char *fmt, ...)
1417 char buffer[BUFSIZ];
1420 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1424 printf ("=> %s\n", buffer);
1432 if (strcmp (buffer, ".") == 0)
1434 fprintf (sm_wfp, "%s\r\n", buffer);
1438 if (ferror (sm_wfp))
1439 return sm_werror ();
1440 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
1441 (char) (sm_ispool + 'a' - 1));
1442 if (rename (sm_tmpfil, file) == NOTOK) {
1446 snprintf (sm_reply.text, sizeof(sm_reply.text),
1447 "error renaming %s to %s: ", sm_tmpfil, file);
1451 if ((s = strerror (errno)))
1452 strncpy (bp, s, sizeof(sm_reply.text) - len);
1454 snprintf (bp, sizeof(sm_reply.text) - len,
1455 "unknown error %d", errno);
1456 sm_reply.length = strlen (sm_reply.text);
1457 sm_reply.code = NOTOK;
1461 if (sm_wfp = fopen (sm_tmpfil, "w"))
1462 chmod (sm_tmpfil, 0600);
1473 ftruncate (fileno (sm_wfp), 0L);
1474 fseek (sm_wfp, 0L, SEEK_SET);
1493 printf ("<= %d\n", result);
1497 sm_reply.text[sm_reply.length = 0] = NULL;
1498 return (sm_reply.code = result);
1503 alarm ((unsigned) time);
1504 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1513 * write the buffer to the open SMTP channel
1517 sm_wrecord (char *buffer, int len)
1520 return sm_werror ();
1522 fwrite (buffer, sizeof(*buffer), len, sm_wfp);
1523 fputs ("\r\n", sm_wfp);
1526 return (ferror (sm_wfp) ? sm_werror () : OK);
1531 sm_wstream (char *buffer, int len)
1534 static char lc = '\0';
1537 return sm_werror ();
1539 if (buffer == NULL && len == 0) {
1541 fputs ("\r\n", sm_wfp);
1543 return (ferror (sm_wfp) ? sm_werror () : OK);
1546 for (bp = buffer; len > 0; bp++, len--) {
1550 fputc ('\r', sm_wfp);
1555 fputc ('.', sm_wfp);/* FALL THROUGH */
1559 fputc (*bp, sm_wfp);
1560 if (ferror (sm_wfp))
1561 return sm_werror ();
1566 return (ferror (sm_wfp) ? sm_werror () : OK);
1571 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1572 * causes compile problems with the #ifdef MPOP in the middle. Should the
1573 * #ifdef MPOP be removed, remove these #undefs.
1586 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1587 : sm_alarmed ? "write to socket timed out"
1589 : sm_ispool ? "error writing to spool file"
1591 : "error writing to socket"));
1593 return (sm_reply.code = NOTOK);
1600 int i, code, cont, bc, rc, more;
1602 char **ehlo, buffer[BUFSIZ];
1605 static int at_least_once = 0;
1607 if (at_least_once) {
1610 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1624 sm_reply.length = 0;
1625 sm_reply.text[0] = 0;
1627 rc = sizeof(sm_reply.text) - 1;
1629 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
1631 printf ("<= %s\n", buffer);
1636 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1637 && (buffer[3] == '-' || doingEHLO == 2)
1639 if (doingEHLO == 2) {
1640 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1641 strcpy (*ehlo++, buffer + 4);
1643 if (ehlo >= EHLOkeys + MAXEHLO)
1653 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1659 for (; bc > 0 && isspace (*bp); bp++, bc--)
1661 if (bc > 0 && *bp == '-') {
1664 for (; bc > 0 && isspace (*bp); bp++, bc--)
1669 if (code != sm_reply.code || cont)
1673 sm_reply.code = code;
1676 strncpy (buffer, sm_noreply, sizeof(buffer));
1678 bc = strlen (sm_noreply);
1682 if ((i = min (bc, rc)) > 0) {
1683 strncpy (rp, bp, i);
1686 if (more && rc > strlen (sm_moreply) + 1) {
1687 strncpy (sm_reply.text + rc, sm_moreply, sizeof(sm_reply.text) - rc);
1688 rc += strlen (sm_moreply);
1693 if (sm_reply.code < 100) {
1695 printf ("%s\n", sm_reply.text);
1701 sm_reply.length = rp - sm_reply.text;
1702 return sm_reply.code;
1709 sm_rrecord (char *buffer, int *len)
1712 return sm_rerror ();
1714 buffer[*len = 0] = 0;
1716 fgets (buffer, BUFSIZ, sm_rfp);
1717 *len = strlen (buffer);
1718 if (ferror (sm_rfp) || feof (sm_rfp))
1719 return sm_rerror ();
1720 if (buffer[*len - 1] != '\n')
1721 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
1724 if (buffer[*len - 2] == '\r')
1726 buffer[*len - 1] = 0;
1735 if (sm_mts == MTS_SMTP)
1737 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1738 : sm_alarmed ? "read from socket timed out"
1739 : feof (sm_rfp) ? "premature end-of-file on socket"
1740 : "error reading from socket"));
1743 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1744 : sm_alarmed ? "read from pipe timed out"
1745 : feof (sm_rfp) ? "premature end-of-file on pipe"
1746 : "error reading from pipe"));
1748 return (sm_reply.code = NOTOK);
1755 #ifndef RELIABLE_SIGNALS
1756 SIGNAL (SIGALRM, alrmser);
1761 printf ("timed out...\n");
1768 rp_string (int code)
1771 static char buffer[BUFSIZ];
1773 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1793 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1813 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1814 text, sm_reply.code, sm_reply.text);
1821 static char *broken[MAXARGS + 1];
1824 smail_brkstring (char *strg, char *brksep, char *brkterm)
1831 for (bi = 0; bi < MAXARGS; bi++) {
1832 while (smail_brkany (c = *sp, brksep))
1834 if (!c || smail_brkany (c, brkterm)) {
1841 while ((c = *++sp) && !smail_brkany (c, brksep) && !smail_brkany (c, brkterm))
1844 broken[MAXARGS] = 0;
1851 * returns 1 if chr in strg, 0 otherwise
1854 smail_brkany (char chr, char *strg)
1859 for (sp = strg; *sp; sp++)
1866 * copy a string array and return pointer to end
1869 smail_copyip (char **p, char **q, int len_q)
1871 while (*p && --len_q > 0)
1890 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1892 if (strncmp (ep, s, len) == 0) {
1893 for (ep += len; *ep == ' '; ep++)