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_AUTHNAME, sm_get_user, NULL },
109 #define SM_SASL_N_CB_AUTHNAME 2
110 { SASL_CB_LIST_END, NULL, NULL },
112 #endif /* CYRUS_SASL */
114 static char *sm_noreply = "No reply text given";
115 static char *sm_moreply = "; ";
117 struct smtp sm_reply; /* global... */
121 static int doingEHLO;
122 char *EHLOkeys[MAXEHLO + 1];
127 static int smtp_init (char *, char *, int, int, int, int, int, int,
129 static int sendmail_init (char *, char *, int, int, int, int, int);
131 static int rclient (char *, char *, char *);
132 static int sm_ierror (char *fmt, ...);
133 static int smtalk (int time, char *fmt, ...);
134 static int sm_wrecord (char *, int);
135 static int sm_wstream (char *, int);
136 static int sm_werror (void);
137 static int smhear (void);
138 static int sm_rrecord (char *, int *);
139 static int sm_rerror (void);
140 static RETSIGTYPE alrmser (int);
141 static char *EHLOset (char *);
144 static int sm_perror (char *fmt, ...);
146 * smtp.c's own static copy of several nmh library subroutines
148 static char **smail_brkstring (char *, char *, char *);
149 static int smail_brkany (char, char *);
150 char **smail_copyip (char **, char **, int);
155 * Function prototypes needed for SASL
158 static int sm_auth_sasl(char *, char *, char *);
159 #endif /* CYRUS_SASL */
161 /* from mts/generic/client.c */
162 int client (char *, char *, char *, int, char *, int);
165 sm_init (char *client, char *server, int watch, int verbose,
166 int debug, int onex, int queued, int sasl, char *saslmech,
169 if (sm_mts == MTS_SMTP)
170 return smtp_init (client, server, watch, verbose,
171 debug, onex, queued, sasl, saslmech, user);
173 return sendmail_init (client, server, watch, verbose,
174 debug, onex, queued);
178 smtp_init (char *client, char *server, int watch, int verbose,
179 int debug, int onex, int queued, int sasl, char *saslmech,
184 #endif /* CYRUS_SASL */
185 int result, sd1, sd2;
190 sm_verbose = verbose;
198 if (sm_rfp != NULL && sm_wfp != NULL)
201 if (client == NULL || *client == '\0') {
205 client = LocalName(); /* no clientname -> LocalName */
210 if (client == NULL || *client == '\0')
211 client = "localhost";
214 if ((sd1 = rclient (server, "tcp", "smtp")) == NOTOK)
225 if ((sm_wfp = fdopen (sd1, "w")) == NULL) {
228 return sm_ierror ("unable to fdopen");
231 sm_reply.text[sm_reply.length = 0] = NULL;
232 return (sm_reply.code = RP_OK);
236 if ((sd2 = dup (sd1)) == NOTOK) {
238 return sm_ierror ("unable to dup");
241 SIGNAL (SIGALRM, alrmser);
242 SIGNAL (SIGPIPE, SIG_IGN);
244 if ((sm_rfp = fdopen (sd1, "r")) == NULL
245 || (sm_wfp = fdopen (sd2, "w")) == NULL) {
248 sm_rfp = sm_wfp = NULL;
249 return sm_ierror ("unable to fdopen");
267 * Give EHLO or HELO command
269 if (client && *client) {
271 result = smtalk (SM_HELO, "EHLO %s", client);
274 if (result >= 500 && result <= 599)
275 result = smtalk (SM_HELO, "HELO %s", client);
285 * If the user asked for SASL, then check to see if the SMTP server
286 * supports it. Otherwise, error out (because the SMTP server
287 * might have been spoofed; we don't want to just silently not
292 if (! (server_mechs = EHLOset("AUTH"))) {
294 return sm_ierror("SMTP server does not support SASL");
297 if (saslmech && stringdex(saslmech, server_mechs) == -1) {
299 return sm_ierror("Requested SASL mech \"%s\" is not in the "
300 "list of supported mechanisms:\n%s",
301 saslmech, server_mechs);
304 if (sm_auth_sasl(user, saslmech ? saslmech : server_mechs,
310 #endif /* CYRUS_SASL */
313 if (watch && EHLOset ("XVRB"))
314 smtalk (SM_HELO, "VERB on");
315 if (onex && EHLOset ("XONE"))
316 smtalk (SM_HELO, "ONEX");
317 if (queued && EHLOset ("XQUE"))
318 smtalk (SM_HELO, "QUED");
324 sendmail_init (char *client, char *server, int watch, int verbose,
325 int debug, int onex, int queued)
334 sm_verbose = verbose;
336 if (sm_rfp != NULL && sm_wfp != NULL)
339 if (client == NULL || *client == '\0') {
343 client = LocalName(); /* no clientname -> LocalName */
347 if (client == NULL || *client == '\0')
348 client = "localhost";
351 if (pipe (pdi) == NOTOK)
352 return sm_ierror ("no pipes");
353 if (pipe (pdo) == NOTOK) {
356 return sm_ierror ("no pipes");
359 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
368 return sm_ierror ("unable to fork");
371 if (pdo[0] != fileno (stdin))
372 dup2 (pdo[0], fileno (stdin));
373 if (pdi[1] != fileno (stdout))
374 dup2 (pdi[1], fileno (stdout));
375 if (pdi[1] != fileno (stderr))
376 dup2 (pdi[1], fileno (stderr));
377 for (i = fileno (stderr) + 1; i < NBITS; i++)
381 vec[vecp++] = r1bindex (sendmail, '/');
384 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
385 vec[vecp++] = "-oem";
390 # endif /* not RAND */
391 #endif /* not ZMAILER */
396 execvp (sendmail, vec);
397 fprintf (stderr, "unable to exec ");
399 _exit (-1); /* NOTREACHED */
402 SIGNAL (SIGALRM, alrmser);
403 SIGNAL (SIGPIPE, SIG_IGN);
407 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
408 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
411 sm_rfp = sm_wfp = NULL;
412 return sm_ierror ("unable to fdopen");
427 if (client && *client) {
429 result = smtalk (SM_HELO, "EHLO %s", client);
432 if (500 <= result && result <= 599)
433 result = smtalk (SM_HELO, "HELO %s", client);
447 smtalk (SM_HELO, "ONEX");
450 smtalk (SM_HELO, "VERB on");
457 # define MAXARGS 1000
461 rclient (char *server, char *protocol, char *service)
464 char response[BUFSIZ];
469 if ((sd = client (server, protocol, service, FALSE, response, sizeof(response))) != NOTOK)
473 if (!server && servers && (cp = strchr(servers, '/'))) {
475 char *arguments[MAXARGS];
477 smail_copyip (smail_brkstring (cp = getcpy (servers), " ", "\n"), arguments, MAXARGS);
479 for (ap = arguments; *ap; ap++)
483 if ((dp = strrchr(*ap, '/')) && *++dp == NULL)
485 snprintf (sm_tmpfil, sizeof(sm_tmpfil), "%s/smtpXXXXXX", *ap);
487 sd = mkstemp (sm_tmpfil);
491 if ((sd = creat (sm_tmpfil, 0600)) != NOTOK) {
504 sm_ierror ("%s", response);
509 sm_winit (int mode, char *from)
514 if (sm_ispool && !sm_wfp) {
515 sm_reply.length = strlen (strcpy (sm_reply.text, "unable to create new spool file"));
516 sm_reply.code = NOTOK;
539 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
556 sm_wadr (char *mbox, char *host, char *path)
558 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
560 path ? path : "", mbox, host)) {
570 #endif /* SENDMAILBUG */
595 switch (smtalk (SM_DATA, "DATA")) {
604 #endif /* SENDMAILBUG */
621 sm_wtxt (char *buffer, int len)
627 result = sm_wstream (buffer, len);
630 return (result == NOTOK ? RP_BHST : RP_OK);
637 if (sm_wstream ((char *) NULL, 0) == NOTOK)
640 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
648 #endif /* SENDMAILBUG */
666 if (sm_mts == MTS_SENDMAIL) {
677 if (sm_rfp == NULL && sm_wfp == NULL)
682 smtalk (SM_QUIT, "QUIT");
686 sm_note.code = sm_reply.code;
687 sm_note.length = sm_reply.length;
688 memcpy (sm_note.text, sm_reply.text, sm_reply.length + 1);/* 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 sm_reply.length = sm_note.length;
702 memcpy (sm_reply.text, sm_note.text, sm_note.length + 1);
719 if (sm_rfp != NULL) {
724 if (sm_wfp != NULL) {
730 if (sm_mts == MTS_SMTP) {
735 #endif /* CYRUS_SASL */
737 status = pidwait (sm_child, OK);
741 sm_rfp = sm_wfp = NULL;
742 return (status ? RP_BHST : RP_OK);
751 int cc, i, j, k, result;
753 char *dp, *bp, *cp, s;
754 char buffer[BUFSIZ], sender[BUFSIZ];
758 k = strlen (file) - sizeof(".bulk");
759 if ((fp = fopen (file, "r")) == NULL) {
760 return sm_perror("unable to read %s: ", file);
763 printf ("reading file %s\n", file);
768 while (fgets (buffer, sizeof(buffer), fp)) {
770 strncpy (sender, buffer + sizeof("MAIL FROM:") - 1, sizeof(sender));
771 if (strcmp (buffer, "DATA\r\n") == 0) {
778 printf ("no DATA...\n");
782 snprintf (buffer, sizeof(buffer), "%s.bad", file);
783 rename (file, buffer);
785 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
794 printf ("no %srecipients...\n", j < 1 ? "sender or " : "");
800 if ((cp = malloc ((size_t) (cc = (pos = ftell (fp)) + 1))) == NULL) {
801 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
803 sm_reply.code = NOTOK;
807 fseek (fp, 0L, SEEK_SET);
808 for (dp = cp, i = 0; i++ < j; dp += strlen (dp))
809 if (fgets (dp, cc - (dp - cp), fp) == NULL) {
810 sm_reply.length = strlen (strcpy (sm_reply.text, "premature eof"));
817 for (dp = cp, i = cc - 1; i > 0; dp += cc, i -= cc)
818 if ((cc = write (fileno (sm_wfp), dp, i)) == NOTOK) {
821 sm_perror("error writing to server: ");
826 printf ("wrote %d octets to server\n", cc);
830 for (dp = cp, i = 0; i++ < j; dp = strchr(dp, '\n'), dp++) {
832 if (bp = strchr(dp, '\r'))
834 printf ("=> %s\n", dp);
840 switch (smhear () + (i == 1 ? 1000 : i != j ? 2000 : 3000)) {
852 smtalk (SM_RSET, "RSET");
880 if (k <= 0 || strcmp (sender, "<>\r\n") == 0)
884 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
885 if ((gp = fopen (buffer, "w+")) == NULL)
887 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
890 "To: %*.*s\r\nSubject: Invalid addresses (%s)\r\n",
891 l - 4, l - 4, sender + 1, file);
892 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
893 dtimenow (0), LocalName ());
895 if (bp = strchr(dp, '\r'))
897 fprintf (gp, "=> %s\r\n", dp);
900 fprintf (gp, "<= %s\r\n", rp_string (result));
918 smtalk (SM_RSET, "RSET");
921 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
932 smtalk (SM_RSET, "RSET");
944 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
947 if (fstat (fileno (sm_wfp), &st) == NOTOK || (cc = st.st_blksize) < BUFSIZ)
952 if ((cp = malloc ((size_t) cc)) == NULL) {
953 smtalk (SM_RSET, "RSET");
954 sm_reply.length = strlen (strcpy (sm_reply.text, "out of memory"));
959 fseek (fp, pos, SEEK_SET);
963 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
964 if ((j = fread (cp, sizeof(*cp), i, fp)) == OK) {
966 sm_perror("error reading %s: ", file);
974 for (dp = cp, i = cc; i > 0; dp += j, i -= j)
975 if ((j = write (fileno (sm_wfp), dp, i)) == NOTOK)
979 printf ("wrote %d octets to server\n", j);
1006 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1017 if (k <= 0 || strcmp (sender, "<>\r\n") == 0) {
1023 ftruncate (fileno (gp), 0L);
1024 fseek (gp, 0L, SEEK_SET);
1027 snprintf (buffer, sizeof(buffer), "%*.*sA.bulk", k, k, file);
1028 if ((gp = fopen (buffer, "w")) == NULL)
1031 fprintf (gp, "MAIL FROM:<>\r\nRCPT TO:%sDATA\r\n", sender);
1032 i = strlen (sender);
1033 fprintf (gp, "To: %*.*s\r\nSubject: Failed mail (%s)\r\n",
1034 i - 4, i - 4, sender + 1, file);
1035 fprintf (gp, "Date: %s\r\nFrom: Postmaster@%s\r\n\r\n",
1036 dtimenow (0), LocalName ());
1041 fputs ("\r\n------- Begin Returned message\r\n\r\n", gp);
1042 fseek (fp, pos, SEEK_SET);
1043 while (fgets (buffer, sizeof(buffer), fp)) {
1044 if (buffer[0] == '-')
1046 if (strcmp (buffer, ".\r\n"))
1049 fputs ("\r\n------- End Returned Message\r\n\r\n.\r\n", gp);
1064 * This function implements SASL authentication for SMTP. If this function
1065 * completes successfully, then authentication is successful and we've
1066 * (optionally) negotiated a security layer.
1068 * Right now we don't support session encryption.
1071 sm_auth_sasl(char *user, char *mechlist, char *host)
1074 unsigned int buflen, outlen;
1075 char *buf, outbuf[BUFSIZ];
1076 const char *chosen_mech;
1077 sasl_security_properties_t secprops;
1082 * Initialize the callback contexts
1086 user = getusername();
1088 callbacks[SM_SASL_N_CB_USER].context = user;
1089 callbacks[SM_SASL_N_CB_AUTHNAME].context = user;
1092 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
1093 * to us on the command line, then call getpeername and do a
1094 * reverse-address lookup on the IP address to get the name.
1098 struct sockaddr_in sin;
1099 int len = sizeof(sin);
1102 if (getpeername(fileno(sm_wfp), (struct sockaddr *) &sin, &len) < 0) {
1103 sm_ierror("getpeername on SMTP socket failed: %s",
1108 if ((hp = gethostbyaddr((void *) &sin.sin_addr, sizeof(sin.sin_addr),
1109 sin.sin_family)) == NULL) {
1110 sm_ierror("DNS lookup on IP address %s failed",
1111 inet_ntoa(sin.sin_addr));
1115 host = strdup(hp->h_name);
1118 sasl_pw_context[0] = host;
1119 sasl_pw_context[1] = user;
1121 callbacks[SM_SASL_N_CB_PASS].context = sasl_pw_context;
1123 result = sasl_client_init(callbacks);
1125 if (result != SASL_OK) {
1126 sm_ierror("SASL library initialization failed: %s",
1127 sasl_errstring(result, NULL, NULL));
1131 result = sasl_client_new("smtp", host, NULL, NULL, NULL, 0, &conn);
1133 if (result != SASL_OK) {
1134 sm_ierror("SASL client initialization failed: %s",
1135 sasl_errstring(result, NULL, NULL));
1140 * Initialize the security properties
1143 memset(&secprops, 0, sizeof(secprops));
1144 secprops.maxbufsize = BUFSIZ;
1145 secprops.max_ssf = 0; /* XXX change this when we do encryption */
1147 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
1149 if (result != SASL_OK) {
1150 sm_ierror("SASL security property initialization failed: %s",
1151 sasl_errstring(result, NULL, NULL));
1156 * Start the actual protocol. Feed the mech list into the library
1157 * and get out a possible initial challenge
1160 result = sasl_client_start(conn, mechlist, NULL, (const char **) &buf,
1161 &buflen, (const char **) &chosen_mech);
1163 if (result != SASL_OK && result != SASL_CONTINUE) {
1164 sm_ierror("SASL client start failed: %s",
1165 sasl_errstring(result, NULL, NULL));
1170 * If we got an initial challenge, send it as part of the AUTH
1171 * command; otherwise, just send a plain AUTH command.
1175 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1176 if (status != SASL_OK) {
1177 sm_ierror("SASL base64 encode failed: %s",
1178 sasl_errstring(status, NULL, NULL));
1182 status = smtalk(SM_AUTH, "AUTH %s %s", chosen_mech, outbuf);
1184 status = smtalk(SM_AUTH, "AUTH %s", chosen_mech);
1187 * Now we loop until we either fail, get a SASL_OK, or a 235
1188 * response code. Receive the challenges and process them until
1192 while (result == SASL_CONTINUE) {
1195 * If we get a 235 response, that means authentication has
1196 * succeeded and we need to break out of the loop (yes, even if
1197 * we still get SASL_CONTINUE from sasl_client_step()).
1199 * Otherwise, if we get a message that doesn't seem to be a
1200 * valid response, then abort
1205 else if (status < 300 || status > 399)
1209 * Special case; a zero-length response from the SMTP server
1210 * is returned as a single =. If we get that, then set buflen
1211 * to be zero. Otherwise, just decode the response.
1214 if (strcmp("=", sm_reply.text) == 0) {
1217 result = sasl_decode64(sm_reply.text, sm_reply.length,
1218 outbuf, sizeof(outbuf), &outlen);
1220 if (result != SASL_OK) {
1221 smtalk(SM_AUTH, "*");
1222 sm_ierror("SASL base64 decode failed: %s",
1223 sasl_errstring(result, NULL, NULL));
1228 result = sasl_client_step(conn, outbuf, outlen, NULL,
1229 (const char **) &buf, &buflen);
1231 if (result != SASL_OK && result != SASL_CONTINUE) {
1232 smtalk(SM_AUTH, "*");
1233 sm_ierror("SASL client negotiation failed: %s",
1234 sasl_errstring(result, NULL, NULL));
1238 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
1240 if (status != SASL_OK) {
1241 smtalk(SM_AUTH, "*");
1242 sm_ierror("SASL base64 encode failed: %s",
1243 sasl_errstring(status, NULL, NULL));
1247 status = smtalk(SM_AUTH, outbuf);
1251 * Make sure that we got the correct response
1254 if (status < 200 || status > 299)
1258 * We _should_ have completed the authentication successfully.
1259 * Get a few properties from the authentication exchange.
1262 result = sasl_getprop(conn, SASL_MAXOUTBUF, (const void **) &outbufmax);
1264 if (result != SASL_OK) {
1265 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
1266 sasl_errstring(result, NULL, NULL));
1270 maxoutbuf = *outbufmax;
1272 result = sasl_getprop(conn, SASL_SSF, (const void **) &ssf);
1276 if (result != SASL_OK) {
1277 sm_ierror("Cannot retrieve SASL negotiated security strength "
1278 "factor: %s", sasl_errstring(result, NULL, NULL));
1282 if (maxoutbuf == 0 || maxoutbuf > BUFSIZ)
1291 * Our callback functions to feed data to the SASL library
1295 sm_get_user(void *context, int id, const char **result, unsigned *len)
1297 char *user = (char *) context;
1299 if (! result || ((id != SASL_CB_USER) && (id != SASL_CB_AUTHNAME)))
1300 return SASL_BADPARAM;
1304 *len = strlen(user);
1310 sm_get_pass(sasl_conn_t *conn, void *context, int id,
1311 sasl_secret_t **psecret)
1313 char **pw_context = (char **) context;
1317 if (! psecret || id != SASL_CB_PASS)
1318 return SASL_BADPARAM;
1320 ruserpass(pw_context[0], &(pw_context[1]), &pass);
1324 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
1331 (*psecret)->len = len;
1332 strcpy((char *) (*psecret)->data, pass);
1337 #endif /* CYRUS_SASL */
1340 sm_ierror (char *fmt, ...)
1345 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
1348 sm_reply.length = strlen (sm_reply.text);
1349 sm_reply.code = NOTOK;
1356 sm_perror (char *fmt, ...)
1358 /* Fill in sm_reply with a suitable error string based on errno.
1359 * This isn't particularly MPOP specific, it just happens that that's
1360 * the only code that uses it currently.
1363 int len, eno = errno;
1367 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
1373 if ((s = strerror(eno)))
1374 snprintf(bp, sizeof(sm_reply.text) - len, "%s", s);
1376 snprintf(bp, sizeof(sm_reply.text) - len, "unknown error %d", eno);
1378 sm_reply.length = strlen (sm_reply.text);
1379 sm_reply.code = NOTOK;
1386 smtalk (int time, char *fmt, ...)
1390 char buffer[BUFSIZ];
1393 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1397 printf ("=> %s\n", buffer);
1405 if (strcmp (buffer, ".") == 0)
1407 fprintf (sm_wfp, "%s\r\n", buffer);
1411 if (ferror (sm_wfp))
1412 return sm_werror ();
1413 snprintf (file, sizeof(file), "%s%c.bulk", sm_tmpfil,
1414 (char) (sm_ispool + 'a' - 1));
1415 if (rename (sm_tmpfil, file) == NOTOK) {
1416 return sm_perror("error renaming %s to %s: ", sm_tmpfil, file);
1419 if (sm_wfp = fopen (sm_tmpfil, "w"))
1420 chmod (sm_tmpfil, 0600);
1431 ftruncate (fileno (sm_wfp), 0L);
1432 fseek (sm_wfp, 0L, SEEK_SET);
1451 printf ("<= %d\n", result);
1455 sm_reply.text[sm_reply.length = 0] = NULL;
1456 return (sm_reply.code = result);
1461 alarm ((unsigned) time);
1462 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1471 * write the buffer to the open SMTP channel
1475 sm_wrecord (char *buffer, int len)
1478 return sm_werror ();
1480 fwrite (buffer, sizeof(*buffer), len, sm_wfp);
1481 fputs ("\r\n", sm_wfp);
1484 return (ferror (sm_wfp) ? sm_werror () : OK);
1489 sm_wstream (char *buffer, int len)
1492 static char lc = '\0';
1495 return sm_werror ();
1497 if (buffer == NULL && len == 0) {
1499 fputs ("\r\n", sm_wfp);
1501 return (ferror (sm_wfp) ? sm_werror () : OK);
1504 for (bp = buffer; len > 0; bp++, len--) {
1508 fputc ('\r', sm_wfp);
1513 fputc ('.', sm_wfp);/* FALL THROUGH */
1517 fputc (*bp, sm_wfp);
1518 if (ferror (sm_wfp))
1519 return sm_werror ();
1524 return (ferror (sm_wfp) ? sm_werror () : OK);
1529 * On some systems, strlen and strcpy are defined as preprocessor macros. This
1530 * causes compile problems with the #ifdef MPOP in the middle. Should the
1531 * #ifdef MPOP be removed, remove these #undefs.
1544 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1545 : sm_alarmed ? "write to socket timed out"
1547 : sm_ispool ? "error writing to spool file"
1549 : "error writing to socket"));
1551 return (sm_reply.code = NOTOK);
1558 int i, code, cont, bc, rc, more;
1561 char **ehlo, buffer[BUFSIZ];
1564 static int at_least_once = 0;
1566 if (at_least_once) {
1569 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1583 sm_reply.length = 0;
1584 sm_reply.text[0] = 0;
1586 rc = sizeof(sm_reply.text) - 1;
1588 for (more = FALSE; sm_rrecord (bp = buffer, &bc) != NOTOK;) {
1590 printf ("<= %s\n", buffer);
1595 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1596 && (buffer[3] == '-' || doingEHLO == 2)
1598 if (doingEHLO == 2) {
1599 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1600 strcpy (*ehlo++, buffer + 4);
1602 if (ehlo >= EHLOkeys + MAXEHLO)
1612 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1618 for (; bc > 0 && isspace (*bp); bp++, bc--)
1620 if (bc > 0 && *bp == '-') {
1623 for (; bc > 0 && isspace (*bp); bp++, bc--)
1628 if (code != sm_reply.code || cont)
1632 sm_reply.code = code;
1635 /* can never fail to 0-terminate because of size of buffer vs fixed string */
1636 strncpy (buffer, sm_noreply, sizeof(buffer));
1638 bc = strlen (sm_noreply);
1642 if ((i = min (bc, rc)) > 0) {
1646 i = strlen(sm_moreply);
1647 if (more && rc > i + 1) {
1648 memcpy (rp, sm_moreply, i); /* safe because of check in if() */
1655 if (sm_reply.code < 100) {
1657 printf ("%s\n", sm_reply.text);
1663 sm_reply.length = rp - sm_reply.text;
1664 sm_reply.text[sm_reply.length] = 0;
1665 return sm_reply.code;
1672 sm_rrecord (char *buffer, int *len)
1675 return sm_rerror ();
1677 buffer[*len = 0] = 0;
1679 fgets (buffer, BUFSIZ, sm_rfp);
1680 *len = strlen (buffer);
1681 /* *len should be >0 except on EOF, but check for safety's sake */
1682 if (ferror (sm_rfp) || feof (sm_rfp) || (*len == 0))
1683 return sm_rerror ();
1684 if (buffer[*len - 1] != '\n')
1685 while (getc (sm_rfp) != '\n' && !ferror (sm_rfp) && !feof (sm_rfp))
1688 if ((*len > 1) && (buffer[*len - 2] == '\r'))
1700 if (sm_mts == MTS_SMTP)
1702 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1703 : sm_alarmed ? "read from socket timed out"
1704 : feof (sm_rfp) ? "premature end-of-file on socket"
1705 : "error reading from socket"));
1708 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1709 : sm_alarmed ? "read from pipe timed out"
1710 : feof (sm_rfp) ? "premature end-of-file on pipe"
1711 : "error reading from pipe"));
1713 return (sm_reply.code = NOTOK);
1720 #ifndef RELIABLE_SIGNALS
1721 SIGNAL (SIGALRM, alrmser);
1726 printf ("timed out...\n");
1733 rp_string (int code)
1736 static char buffer[BUFSIZ];
1738 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1758 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1778 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1779 text, sm_reply.code, sm_reply.text);
1786 static char *broken[MAXARGS + 1];
1789 smail_brkstring (char *strg, char *brksep, char *brkterm)
1796 for (bi = 0; bi < MAXARGS; bi++) {
1797 while (smail_brkany (c = *sp, brksep))
1799 if (!c || smail_brkany (c, brkterm)) {
1806 while ((c = *++sp) && !smail_brkany (c, brksep) && !smail_brkany (c, brkterm))
1809 broken[MAXARGS] = 0;
1816 * returns 1 if chr in strg, 0 otherwise
1819 smail_brkany (char chr, char *strg)
1824 for (sp = strg; *sp; sp++)
1831 * copy a string array and return pointer to end
1834 smail_copyip (char **p, char **q, int len_q)
1836 while (*p && --len_q > 0)
1855 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1857 if (strncmp (ep, s, len) == 0) {
1858 for (ep += len; *ep == ' '; ep++)