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>
21 #include <sasl/sasl.h>
22 #include <sasl/saslutil.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);
506 sm_winit (int mode, char *from)
511 if (sm_ispool && !sm_wfp) {
512 strlen (strcpy (sm_reply.text, "unable to create new spool file"));
513 sm_reply.code = NOTOK;
536 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
553 sm_wadr (char *mbox, char *host, char *path)
555 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
557 path ? path : "", mbox, host)) {
567 #endif /* SENDMAILBUG */
592 switch (smtalk (SM_DATA, "DATA")) {
601 #endif /* SENDMAILBUG */
618 sm_wtxt (char *buffer, int len)
624 result = sm_wstream (buffer, len);
627 return (result == NOTOK ? RP_BHST : RP_OK);
634 if (sm_wstream ((char *) NULL, 0) == NOTOK)
637 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
645 #endif /* SENDMAILBUG */
663 if (sm_mts == MTS_SENDMAIL) {
674 if (sm_rfp == NULL && sm_wfp == NULL)
679 smtalk (SM_QUIT, "QUIT");
683 sm_note.code = sm_reply.code;
684 strncpy (sm_note.text, sm_reply.text, sm_note.length = sm_reply.length);/* fall */
686 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
688 if (sm_mts == MTS_SMTP)
689 smtalk (SM_QUIT, "QUIT");
691 kill (sm_child, SIGKILL);
696 sm_reply.code = sm_note.code;
697 strncpy (sm_reply.text, sm_note.text, sm_reply.length = sm_note.length);
714 if (sm_rfp != NULL) {
719 if (sm_wfp != NULL) {
725 if (sm_mts == MTS_SMTP) {
730 #endif /* CYRUS_SASL */
732 status = pidwait (sm_child, OK);
736 sm_rfp = sm_wfp = NULL;
737 return (status ? RP_BHST : RP_OK);
746 int cc, i, j, k, result;
748 char *dp, *bp, *cp, s;
749 char buffer[BUFSIZ], sender[BUFSIZ];
753 k = strlen (file) - sizeof(".bulk");
754 if ((fp = fopen (file, "r")) == NULL) {
757 snprintf (sm_reply.text, sizeof(sm_reply.text),
758 "unable to read %s: ", file);
762 if ((s = strerror (errno)))
763 strncpy (bp, s, sizeof(sm_reply.text) - len);
765 snprintf (bp, sizeof(sm_reply.text) - len, "Error %d", errno);
766 sm_reply.length = strlen (sm_reply.text);
767 sm_reply.code = NOTOK;
771 printf ("reading file %s\n", file);
776 while (fgets (buffer, sizeof(buffer), fp)) {
778 strncpy (sender, buffer + sizeof("MAIL FROM:") - 1, sizeof(sender));
779 if (strcmp (buffer, "DATA\r\n") == 0) {
786 printf ("no DATA...\n");
790 snprintf (buffer, sizeof(buffer), "%s.bad", file);
791 rename (file, buffer);
793 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
802 printf ("no %srecipients...\n", j < 1 ? "sender or " : "");
808 if ((cp = malloc ((size_t) (cc = (pos = ftell (fp)) + 1))) == NULL) {
809 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
811 sm_reply.code = NOTOK;
815 fseek (fp, 0L, SEEK_SET);
816 for (dp = cp, i = 0; i++ < j; dp += strlen (dp))
817 if (fgets (dp, cc - (dp - cp), fp) == NULL) {
818 sm_reply.length = strlen (strcpy (sm_reply.text, "premature eof"));
825 for (dp = cp, i = cc - 1; i > 0; dp += cc, i -= cc)
826 if ((cc = write (fileno (sm_wfp), dp, i)) == NOTOK) {
829 strcpy (sm_reply.text, "error writing to server: ",
830 sizeof(sm_reply.text));
834 if ((s = strerror (errno)))
835 strncpy (bp, s, sizeof(sm_reply.text) - len);
837 snprintf (bp, sizeof(sm_reply.text) - len,
838 "unknown error %d", errno);
839 sm_reply.length = strlen (sm_reply.text);
844 printf ("wrote %d octets to server\n", cc);
848 for (dp = cp, i = 0; i++ < j; dp = strchr(dp, '\n'), dp++) {
850 if (bp = strchr(dp, '\r'))
852 printf ("=> %s\n", dp);
858 switch (smhear () + (i == 1 ? 1000 : i != j ? 2000 : 3000)) {
870 smtalk (SM_RSET, "RSET");
898 if (k <= 0 || strcmp (sender, "<>\r\n") == 0)
902 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
903 if ((gp = fopen (buffer, "w+")) == NULL)
905 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
908 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
909 l - 4, l - 4, sender + 1, file);
910 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
911 dtimenow (0), LocalName ());
913 if (bp = strchr(dp, '\r'))
915 fprintf (gp, "=> %s\r\n", dp);
918 fprintf (gp, "<= %s\r\n", rp_string (result));
936 smtalk (SM_RSET, "RSET");
939 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
950 smtalk (SM_RSET, "RSET");
962 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
965 if (fstat (fileno (sm_wfp), &st) == NOTOK || (cc = st.st_blksize) < BUFSIZ)
970 if ((cp = malloc ((size_t) cc)) == NULL) {
971 smtalk (SM_RSET, "RSET");
972 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
977 fseek (fp, pos, SEEK_SET);
981 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
982 if ((j = fread (cp, sizeof(*cp), i, fp)) == OK) {
986 snprintf (sm_reply.text, sizeof(sm_reply.text),
987 "error reading %s: ", file);
991 if ((s = strerror (errno)))
992 strncpy (bp, s, sizeof(sm_reply.text) - len);
994 snprintf (bp, sizeof(sm_reply.text) - len,
995 "unknown error %d", errno);
996 sm_reply.length = strlen (sm_reply.text);
1004 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
1005 if ((j = write (fileno (sm_wfp), dp, i)) == NOTOK)
1009 printf ("wrote %d octets to server\n", j);
1018 switch (smhear ()) {
1036 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1047 if (k <= 0 || strcmp (sender, "<>\r\n") == 0) {
1053 ftruncate (fileno (gp), 0L);
1054 fseek (gp, 0L, SEEK_SET);
1057 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1058 if ((gp = fopen (buffer, "w")) == NULL)
1061 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
1062 i = strlen (sender);
1063 fprintf (gp, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1064 i - 4, i - 4, sender + 1, file);
1065 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1066 dtimenow (0), LocalName ());
1071 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp);
1072 fseek (fp, pos, SEEK_SET);
1073 while (fgets (buffer, sizeof(buffer), fp)) {
1074 if (buffer[0] == '-')
1076 if (strcmp (buffer, ".\r\n"))
1079 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp);
1094 * This function implements SASL authentication for SMTP. If this function
1095 * completes successfully, then authentication is successful and we've
1096 * (optionally) negotiated a security layer.
1098 * Right now we don't support session encryption.
1101 sm_auth_sasl(char *user, char *mechlist, char *host)
1104 unsigned int buflen, outlen;
1105 char *buf, outbuf[BUFSIZ];
1106 const char *chosen_mech;
1107 sasl_security_properties_t secprops;
1112 * Initialize the callback contexts
1116 user = getusername();
1118 callbacks[SM_SASL_N_CB_USER].context = user;
1121 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1122 * to us on the command line, then call getpeername and do a
1123 * reverse-address lookup on the IP address to get the name.
1127 struct sockaddr_in sin;
1128 int len = sizeof(sin);
1131 if (getpeername(fileno(sm_wfp), (struct sockaddr *) &sin, &len) < 0) {
1132 sm_ierror("getpeername on SMTP socket failed: %s",
1137 if ((hp = gethostbyaddr((void *) &sin.sin_addr, sizeof(sin.sin_addr),
1138 sin.sin_family)) == NULL) {
1139 sm_ierror("DNS lookup on IP address %s failed",
1140 inet_ntoa(sin.sin_addr));
1144 host = strdup(hp->h_name);
1147 sasl_pw_context[0] = host;
1148 sasl_pw_context[1] = user;
1150 callbacks[SM_SASL_N_CB_PASS].context = sasl_pw_context;
1152 result = sasl_client_init(callbacks);
1154 if (result != SASL_OK) {
1155 sm_ierror("SASL library initialization failed: %s",
1156 sasl_errstring(result, NULL, NULL));
1160 result = sasl_client_new("smtp", host, NULL, NULL, NULL, 0, &conn);
1162 if (result != SASL_OK) {
1163 sm_ierror("SASL client initialization failed: %s",
1164 sasl_errstring(result, NULL, NULL));
1169 * Initialize the security properties
1172 memset(&secprops, 0, sizeof(secprops));
1173 secprops.maxbufsize = BUFSIZ;
1174 secprops.max_ssf = 0; /* XXX change this when we do encryption */
1176 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
1178 if (result != SASL_OK) {
1179 sm_ierror("SASL security property initialization failed: %s",
1180 sasl_errstring(result, NULL, NULL));
1185 * Start the actual protocol. Feed the mech list into the library
1186 * and get out a possible initial challenge
1189 result = sasl_client_start(conn, mechlist, NULL, (const char **) &buf,
1190 &buflen, (const char **) &chosen_mech);
1192 if (result != SASL_OK && result != SASL_CONTINUE) {
1193 sm_ierror("SASL client start failed: %s",
1194 sasl_errstring(result, NULL, NULL));
1199 * If we got an initial challenge, send it as part of the AUTH
1200 * command; otherwise, just send a plain AUTH command.
1204 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1205 if (status != SASL_OK) {
1206 sm_ierror("SASL base64 encode failed: %s",
1207 sasl_errstring(status, NULL, NULL));
1211 status = smtalk(SM_AUTH, "AUTH %s %s", chosen_mech, outbuf);
1213 status = smtalk(SM_AUTH, "AUTH %s", chosen_mech);
1216 * Now we loop until we either fail, get a SASL_OK, or a 235
1217 * response code. Receive the challenges and process them until
1221 while (result == SASL_CONTINUE) {
1224 * If we get a 235 response, that means authentication has
1225 * succeeded and we need to break out of the loop (yes, even if
1226 * we still get SASL_CONTINUE from sasl_client_step()).
1228 * Otherwise, if we get a message that doesn't seem to be a
1229 * valid response, then abort
1234 else if (status < 300 || status > 399)
1238 * Special case; a zero-length response from the SMTP server
1239 * is returned as a single =. If we get that, then set buflen
1240 * to be zero. Otherwise, just decode the response.
1243 if (strcmp("=", sm_reply.text) == 0) {
1246 result = sasl_decode64(sm_reply.text, sm_reply.length,
1247 outbuf, sizeof(outbuf), &outlen);
1249 if (result != SASL_OK) {
1250 smtalk(SM_AUTH, "*");
1251 sm_ierror("SASL base64 decode failed: %s",
1252 sasl_errstring(result, NULL, NULL));
1257 result = sasl_client_step(conn, outbuf, outlen, NULL,
1258 (const char **) &buf, &buflen);
1260 if (result != SASL_OK && result != SASL_CONTINUE) {
1261 smtalk(SM_AUTH, "*");
1262 sm_ierror("SASL client negotiation failed: %s",
1263 sasl_errstring(result, NULL, NULL));
1267 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1269 if (status != SASL_OK) {
1270 smtalk(SM_AUTH, "*");
1271 sm_ierror("SASL base64 encode failed: %s",
1272 sasl_errstring(status, NULL, NULL));
1276 status = smtalk(SM_AUTH, outbuf);
1280 * Make sure that we got the correct response
1283 if (status < 200 || status > 299)
1287 * We _should_ have completed the authentication successfully.
1288 * Get a few properties from the authentication exchange.
1291 result = sasl_getprop(conn, SASL_MAXOUTBUF, (const void **) &outbufmax);
1293 if (result != SASL_OK) {
1294 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1295 sasl_errstring(result, NULL, NULL));
1299 maxoutbuf = *outbufmax;
1301 result = sasl_getprop(conn, SASL_SSF, (const void **) &ssf);
1305 if (result != SASL_OK) {
1306 sm_ierror("Cannot retrieve SASL negotiated security strength "
1307 "factor: %s", sasl_errstring(result, NULL, NULL));
1311 if (maxoutbuf == 0 || maxoutbuf > BUFSIZ)
1320 * Our callback functions to feed data to the SASL library
1324 sm_get_user(void *context, int id, const char **result, unsigned *len)
1326 char *user = (char *) context;
1328 if (! result || id != SASL_CB_USER)
1329 return SASL_BADPARAM;
1333 *len = strlen(user);
1339 sm_get_pass(sasl_conn_t *conn, void *context, int id,
1340 sasl_secret_t **psecret)
1342 char **pw_context = (char **) context;
1346 if (! psecret || id != SASL_CB_PASS)
1347 return SASL_BADPARAM;
1349 ruserpass(pw_context[0], &(pw_context[1]), &pass);
1353 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
1360 (*psecret)->len = len;
1361 strcpy((char *) (*psecret)->data, pass);
1366 #endif /* CYRUS_SASL */
1369 sm_ierror (char *fmt, ...)
1374 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
1377 sm_reply.length = strlen (sm_reply.text);
1378 sm_reply.code = NOTOK;
1385 smtalk (int time, char *fmt, ...)
1389 char buffer[BUFSIZ];
1392 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1396 printf ("=> %s\n", buffer);
1404 if (strcmp (buffer, ".") == 0)
1406 fprintf (sm_wfp, "%s\r\n", buffer);
1410 if (ferror (sm_wfp))
1411 return sm_werror ();
1412 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
1413 (char) (sm_ispool + 'a' - 1));
1414 if (rename (sm_tmpfil, file) == NOTOK) {
1418 snprintf (sm_reply.text, sizeof(sm_reply.text),
1419 "error renaming %s to %s: ", sm_tmpfil, file);
1423 if ((s = strerror (errno)))
1424 strncpy (bp, s, sizeof(sm_reply.text) - len);
1426 snprintf (bp, sizeof(sm_reply.text) - len,
1427 "unknown error %d", errno);
1428 sm_reply.length = strlen (sm_reply.text);
1429 sm_reply.code = NOTOK;
1433 if (sm_wfp = fopen (sm_tmpfil, "w"))
1434 chmod (sm_tmpfil, 0600);
1445 ftruncate (fileno (sm_wfp), 0L);
1446 fseek (sm_wfp, 0L, SEEK_SET);
1465 printf ("<= %d\n", result);
1469 sm_reply.text[sm_reply.length = 0] = NULL;
1470 return (sm_reply.code = result);
1475 alarm ((unsigned) time);
1476 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1485 * write the buffer to the open SMTP channel
1489 sm_wrecord (char *buffer, int len)
1492 return sm_werror ();
1494 fwrite (buffer, sizeof(*buffer), len, sm_wfp);
1495 fputs ("\r\n", sm_wfp);
1498 return (ferror (sm_wfp) ? sm_werror () : OK);
1503 sm_wstream (char *buffer, int len)
1506 static char lc = '\0';
1509 return sm_werror ();
1511 if (buffer == NULL && len == 0) {
1513 fputs ("\r\n", sm_wfp);
1515 return (ferror (sm_wfp) ? sm_werror () : OK);
1518 for (bp = buffer; len > 0; bp++, len--) {
1522 fputc ('\r', sm_wfp);
1527 fputc ('.', sm_wfp);/* FALL THROUGH */
1531 fputc (*bp, sm_wfp);
1532 if (ferror (sm_wfp))
1533 return sm_werror ();
1538 return (ferror (sm_wfp) ? sm_werror () : OK);
1543 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1544 * causes compile problems with the #ifdef MPOP in the middle. Should the
1545 * #ifdef MPOP be removed, remove these #undefs.
1558 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1559 : sm_alarmed ? "write to socket timed out"
1561 : sm_ispool ? "error writing to spool file"
1563 : "error writing to socket"));
1565 return (sm_reply.code = NOTOK);
1572 int i, code, cont, bc, rc, more;
1574 char **ehlo, buffer[BUFSIZ];
1577 static int at_least_once = 0;
1579 if (at_least_once) {
1582 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1596 sm_reply.length = 0;
1597 sm_reply.text[0] = 0;
1599 rc = sizeof(sm_reply.text) - 1;
1601 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
1603 printf ("<= %s\n", buffer);
1608 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1609 && (buffer[3] == '-' || doingEHLO == 2)
1611 if (doingEHLO == 2) {
1612 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1613 strcpy (*ehlo++, buffer + 4);
1615 if (ehlo >= EHLOkeys + MAXEHLO)
1625 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1631 for (; bc > 0 && isspace (*bp); bp++, bc--)
1633 if (bc > 0 && *bp == '-') {
1636 for (; bc > 0 && isspace (*bp); bp++, bc--)
1641 if (code != sm_reply.code || cont)
1645 sm_reply.code = code;
1648 strncpy (buffer, sm_noreply, sizeof(buffer));
1650 bc = strlen (sm_noreply);
1654 if ((i = min (bc, rc)) > 0) {
1655 strncpy (rp, bp, i);
1658 if (more && rc > strlen (sm_moreply) + 1) {
1659 strncpy (sm_reply.text + rc, sm_moreply, sizeof(sm_reply.text) - rc);
1660 rc += strlen (sm_moreply);
1665 if (sm_reply.code < 100) {
1667 printf ("%s\n", sm_reply.text);
1673 sm_reply.length = rp - sm_reply.text;
1674 return sm_reply.code;
1681 sm_rrecord (char *buffer, int *len)
1684 return sm_rerror ();
1686 buffer[*len = 0] = 0;
1688 fgets (buffer, BUFSIZ, sm_rfp);
1689 *len = strlen (buffer);
1690 if (ferror (sm_rfp) || feof (sm_rfp))
1691 return sm_rerror ();
1692 if (buffer[*len - 1] != '\n')
1693 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
1696 if (buffer[*len - 2] == '\r')
1698 buffer[*len - 1] = 0;
1707 if (sm_mts == MTS_SMTP)
1709 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1710 : sm_alarmed ? "read from socket timed out"
1711 : feof (sm_rfp) ? "premature end-of-file on socket"
1712 : "error reading from socket"));
1715 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1716 : sm_alarmed ? "read from pipe timed out"
1717 : feof (sm_rfp) ? "premature end-of-file on pipe"
1718 : "error reading from pipe"));
1720 return (sm_reply.code = NOTOK);
1727 #ifndef RELIABLE_SIGNALS
1728 SIGNAL (SIGALRM, alrmser);
1733 printf ("timed out...\n");
1740 rp_string (int code)
1743 static char buffer[BUFSIZ];
1745 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1765 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1785 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1786 text, sm_reply.code, sm_reply.text);
1793 static char *broken[MAXARGS + 1];
1796 smail_brkstring (char *strg, char *brksep, char *brkterm)
1803 for (bi = 0; bi < MAXARGS; bi++) {
1804 while (smail_brkany (c = *sp, brksep))
1806 if (!c || smail_brkany (c, brkterm)) {
1813 while ((c = *++sp) && !smail_brkany (c, brksep) && !smail_brkany (c, brkterm))
1816 broken[MAXARGS] = 0;
1823 * returns 1 if chr in strg, 0 otherwise
1826 smail_brkany (char chr, char *strg)
1831 for (sp = strg; *sp; sp++)
1838 * copy a string array and return pointer to end
1841 smail_copyip (char **p, char **q, int len_q)
1843 while (*p && --len_q > 0)
1862 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1864 if (strncmp (ep, s, len) == 0) {
1865 for (ep += len; *ep == ' '; ep++)