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>
18 #include <sasl/sasl.h>
19 #include <sasl/saslutil.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
25 #endif /* CYRUS_SASL */
28 * This module implements an interface to SendMail very similar
29 * to the MMDF mm_(3) routines. The sm_() routines herein talk
30 * SMTP to a sendmail process, mapping SMTP reply codes into
35 * On older 4.2BSD machines without the POSIX function `sigaction',
36 * the alarm handing stuff for time-outs will NOT work due to the way
37 * syscalls get restarted. This is not really crucial, since SendMail
38 * is generally well-behaved in this area.
43 * It appears that some versions of Sendmail will return Code 451
44 * when they don't really want to indicate a failure.
45 * "Code 451 almost always means sendmail has deferred; we don't
46 * really want bomb out at this point since sendmail will rectify
47 * things later." So, if you define SENDMAILBUG, Code 451 is
48 * considered the same as Code 250. Yuck!
55 #define NBITS ((sizeof (int)) * 8)
58 * these codes must all be different!
60 #define SM_OPEN 300 /* Changed to 5 minutes to comply with a SHOULD in RFC 1123 */
63 #define SM_MAIL 301 /* changed to 5 minutes and a second (for uniqueness), see above */
64 #define SM_RCPT 302 /* see above */
65 #define SM_DATA 120 /* see above */
66 #define SM_TEXT 180 /* see above */
67 #define SM_DOT 600 /* see above */
72 static int sm_addrs = 0;
73 static int sm_alarmed = 0;
74 static int sm_child = NOTOK;
75 static int sm_debug = 0;
76 static int sm_nl = TRUE;
77 static int sm_verbose = 0;
79 static FILE *sm_rfp = NULL;
80 static FILE *sm_wfp = NULL;
84 * Some globals needed by SASL
87 static sasl_conn_t *conn = NULL; /* SASL connection state */
88 static int sasl_complete = 0; /* Has authentication succeded? */
89 static sasl_ssf_t sasl_ssf; /* Our security strength factor */
90 static char *sasl_pw_context[2]; /* Context to pass into sm_get_pass */
91 static int maxoutbuf; /* Maximum crypto output buffer */
92 static char *sasl_outbuffer; /* SASL output buffer for encryption */
93 static int sasl_outbuflen; /* Current length of data in outbuf */
94 static char *sasl_inbuffer; /* SASL input buffer for encryption */
95 static char *sasl_inptr; /* Pointer to current inbuf position */
96 static int sasl_inbuflen; /* Current length of data in inbuf */
97 static int sm_get_user(void *, int, const char **, unsigned *);
98 static int sm_get_pass(sasl_conn_t *, void *, int, sasl_secret_t **);
99 static int sm_fgetc(FILE *);
101 static sasl_callback_t callbacks[] = {
102 { SASL_CB_USER, sm_get_user, NULL },
103 #define SM_SASL_N_CB_USER 0
104 { SASL_CB_PASS, sm_get_pass, NULL },
105 #define SM_SASL_N_CB_PASS 1
106 { SASL_CB_AUTHNAME, sm_get_user, NULL },
107 #define SM_SASL_N_CB_AUTHNAME 2
108 { SASL_CB_LIST_END, NULL, NULL },
111 #define SASL_MAXRECVBUF 65536
112 #else /* CYRUS_SASL */
113 #define sm_fgetc fgetc
114 #endif /* CYRUS_SASL */
116 static char *sm_noreply = "No reply text given";
117 static char *sm_moreply = "; ";
119 struct smtp sm_reply; /* global... */
123 static int doingEHLO;
124 char *EHLOkeys[MAXEHLO + 1];
129 static int smtp_init (char *, char *, char *, int, int, int, int, int, int,
131 static int sendmail_init (char *, char *, int, int, int, int, int);
133 static int rclient (char *, char *);
134 static int sm_ierror (char *fmt, ...);
135 static int smtalk (int time, char *fmt, ...);
136 static int sm_wrecord (char *, int);
137 static int sm_wstream (char *, int);
138 static int sm_werror (void);
139 static int smhear (void);
140 static int sm_rrecord (char *, int *);
141 static int sm_rerror (int);
142 static RETSIGTYPE alrmser (int);
143 static char *EHLOset (char *);
144 static int sm_fwrite(char *, int);
145 static int sm_fputs(char *);
146 static int sm_fputc(int);
147 static void sm_fflush(void);
148 static int sm_fgets(char *, int, FILE *);
152 * Function prototypes needed for SASL
155 static int sm_auth_sasl(char *, char *, char *);
156 #endif /* CYRUS_SASL */
159 sm_init (char *client, char *server, char *port, int watch, int verbose,
160 int debug, int onex, int queued, int sasl, char *saslmech,
163 if (sm_mts == MTS_SMTP)
164 return smtp_init (client, server, port, watch, verbose,
165 debug, onex, queued, sasl, saslmech, user);
167 return sendmail_init (client, server, watch, verbose,
168 debug, onex, queued);
172 smtp_init (char *client, char *server, char *port, int watch, int verbose,
173 int debug, int onex, int queued, int sasl, char *saslmech,
178 #endif /* CYRUS_SASL */
179 int result, sd1, sd2;
184 sm_verbose = verbose;
187 if (sm_rfp != NULL && sm_wfp != NULL)
190 if (client == NULL || *client == '\0') {
194 client = LocalName(); /* no clientname -> LocalName */
199 if (client == NULL || *client == '\0')
200 client = "localhost";
204 sasl_inbuffer = malloc(SASL_MAXRECVBUF);
206 return sm_ierror("Unable to allocate %d bytes for read buffer",
208 #endif /* CYRUS_SASL */
210 if ((sd1 = rclient (server, port)) == NOTOK)
213 if ((sd2 = dup (sd1)) == NOTOK) {
215 return sm_ierror ("unable to dup");
218 SIGNAL (SIGALRM, alrmser);
219 SIGNAL (SIGPIPE, SIG_IGN);
221 if ((sm_rfp = fdopen (sd1, "r")) == NULL
222 || (sm_wfp = fdopen (sd2, "w")) == NULL) {
225 sm_rfp = sm_wfp = NULL;
226 return sm_ierror ("unable to fdopen");
244 * Give EHLO or HELO command
246 if (client && *client) {
248 result = smtalk (SM_HELO, "EHLO %s", client);
251 if (result >= 500 && result <= 599)
252 result = smtalk (SM_HELO, "HELO %s", client);
262 * If the user asked for SASL, then check to see if the SMTP server
263 * supports it. Otherwise, error out (because the SMTP server
264 * might have been spoofed; we don't want to just silently not
269 if (! (server_mechs = EHLOset("AUTH"))) {
271 return sm_ierror("SMTP server does not support SASL");
274 if (saslmech && stringdex(saslmech, server_mechs) == -1) {
276 return sm_ierror("Requested SASL mech \"%s\" is not in the "
277 "list of supported mechanisms:\n%s",
278 saslmech, server_mechs);
281 if (sm_auth_sasl(user, saslmech ? saslmech : server_mechs,
287 #endif /* CYRUS_SASL */
290 if (watch && EHLOset ("XVRB"))
291 smtalk (SM_HELO, "VERB on");
292 if (onex && EHLOset ("XONE"))
293 smtalk (SM_HELO, "ONEX");
294 if (queued && EHLOset ("XQUE"))
295 smtalk (SM_HELO, "QUED");
301 sendmail_init (char *client, char *server, int watch, int verbose,
302 int debug, int onex, int queued)
311 sm_verbose = verbose;
313 if (sm_rfp != NULL && sm_wfp != NULL)
316 if (client == NULL || *client == '\0') {
320 client = LocalName(); /* no clientname -> LocalName */
324 if (client == NULL || *client == '\0')
325 client = "localhost";
329 sasl_inbuffer = malloc(SASL_MAXRECVBUF);
331 return sm_ierror("Unable to allocate %d bytes for read buffer",
333 #endif /* CYRUS_SASL */
335 if (pipe (pdi) == NOTOK)
336 return sm_ierror ("no pipes");
337 if (pipe (pdo) == NOTOK) {
340 return sm_ierror ("no pipes");
343 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
352 return sm_ierror ("unable to fork");
355 if (pdo[0] != fileno (stdin))
356 dup2 (pdo[0], fileno (stdin));
357 if (pdi[1] != fileno (stdout))
358 dup2 (pdi[1], fileno (stdout));
359 if (pdi[1] != fileno (stderr))
360 dup2 (pdi[1], fileno (stderr));
361 for (i = fileno (stderr) + 1; i < NBITS; i++)
365 vec[vecp++] = r1bindex (sendmail, '/');
368 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
369 vec[vecp++] = "-oem";
374 # endif /* not RAND */
375 #endif /* not ZMAILER */
380 execvp (sendmail, vec);
381 fprintf (stderr, "unable to exec ");
383 _exit (-1); /* NOTREACHED */
386 SIGNAL (SIGALRM, alrmser);
387 SIGNAL (SIGPIPE, SIG_IGN);
391 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
392 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
395 sm_rfp = sm_wfp = NULL;
396 return sm_ierror ("unable to fdopen");
411 if (client && *client) {
413 result = smtalk (SM_HELO, "EHLO %s", client);
416 if (500 <= result && result <= 599)
417 result = smtalk (SM_HELO, "HELO %s", client);
431 smtalk (SM_HELO, "ONEX");
434 smtalk (SM_HELO, "VERB on");
441 rclient (char *server, char *service)
444 char response[BUFSIZ];
446 if ((sd = client (server, service, response, sizeof(response),
450 sm_ierror ("%s", response);
455 sm_winit (int mode, char *from)
477 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
494 sm_wadr (char *mbox, char *host, char *path)
496 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
498 path ? path : "", mbox, host)) {
508 #endif /* SENDMAILBUG */
533 switch (smtalk (SM_DATA, "DATA")) {
542 #endif /* SENDMAILBUG */
559 sm_wtxt (char *buffer, int len)
565 result = sm_wstream (buffer, len);
568 return (result == NOTOK ? RP_BHST : RP_OK);
575 if (sm_wstream ((char *) NULL, 0) == NOTOK)
578 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
586 #endif /* SENDMAILBUG */
604 if (sm_mts == MTS_SENDMAIL) {
615 if (sm_rfp == NULL && sm_wfp == NULL)
620 smtalk (SM_QUIT, "QUIT");
624 sm_note.code = sm_reply.code;
625 sm_note.length = sm_reply.length;
626 memcpy (sm_note.text, sm_reply.text, sm_reply.length + 1);/* fall */
628 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
630 if (sm_mts == MTS_SMTP)
631 smtalk (SM_QUIT, "QUIT");
633 kill (sm_child, SIGKILL);
638 sm_reply.code = sm_note.code;
639 sm_reply.length = sm_note.length;
640 memcpy (sm_reply.text, sm_note.text, sm_note.length + 1);
645 if (sm_rfp != NULL) {
650 if (sm_wfp != NULL) {
656 if (sm_mts == MTS_SMTP) {
661 if (sasl_outbuffer) {
662 free(sasl_outbuffer);
667 #endif /* CYRUS_SASL */
669 status = pidwait (sm_child, OK);
673 sm_rfp = sm_wfp = NULL;
674 return (status ? RP_BHST : RP_OK);
679 * This function implements SASL authentication for SMTP. If this function
680 * completes successfully, then authentication is successful and we've
681 * (optionally) negotiated a security layer.
684 sm_auth_sasl(char *user, char *mechlist, char *inhost)
687 unsigned int buflen, outlen;
688 char *buf, outbuf[BUFSIZ], host[NI_MAXHOST];
689 const char *chosen_mech;
690 sasl_security_properties_t secprops;
695 * Initialize the callback contexts
699 user = getusername();
701 callbacks[SM_SASL_N_CB_USER].context = user;
702 callbacks[SM_SASL_N_CB_AUTHNAME].context = user;
705 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
706 * to us on the command line, then call getpeername and do a
707 * reverse-address lookup on the IP address to get the name.
710 memset(host, 0, sizeof(host));
713 struct sockaddr_storage sin;
714 socklen_t len = sizeof(sin);
717 if (getpeername(fileno(sm_wfp), (struct sockaddr *) &sin, &len) < 0) {
718 sm_ierror("getpeername on SMTP socket failed: %s",
723 result = getnameinfo((struct sockaddr *) &sin, len, host, sizeof(host),
724 NULL, 0, NI_NAMEREQD);
726 sm_ierror("Unable to look up name of connected host: %s",
727 gai_strerror(result));
731 strncpy(host, inhost, sizeof(host) - 1);
734 sasl_pw_context[0] = host;
735 sasl_pw_context[1] = user;
737 callbacks[SM_SASL_N_CB_PASS].context = sasl_pw_context;
739 result = sasl_client_init(callbacks);
741 if (result != SASL_OK) {
742 sm_ierror("SASL library initialization failed: %s",
743 sasl_errstring(result, NULL, NULL));
747 result = sasl_client_new("smtp", host, NULL, NULL, NULL, 0, &conn);
749 if (result != SASL_OK) {
750 sm_ierror("SASL client initialization failed: %s",
751 sasl_errstring(result, NULL, NULL));
756 * Initialize the security properties
759 memset(&secprops, 0, sizeof(secprops));
760 secprops.maxbufsize = SASL_MAXRECVBUF;
761 secprops.max_ssf = UINT_MAX;
763 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
765 if (result != SASL_OK) {
766 sm_ierror("SASL security property initialization failed: %s",
767 sasl_errstring(result, NULL, NULL));
772 * Start the actual protocol. Feed the mech list into the library
773 * and get out a possible initial challenge
776 result = sasl_client_start(conn, mechlist, NULL, (const char **) &buf,
777 &buflen, (const char **) &chosen_mech);
779 if (result != SASL_OK && result != SASL_CONTINUE) {
780 sm_ierror("SASL client start failed: %s", sasl_errdetail(conn));
785 * If we got an initial challenge, send it as part of the AUTH
786 * command; otherwise, just send a plain AUTH command.
790 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
791 if (status != SASL_OK) {
792 sm_ierror("SASL base64 encode failed: %s",
793 sasl_errstring(status, NULL, NULL));
797 status = smtalk(SM_AUTH, "AUTH %s %s", chosen_mech, outbuf);
799 status = smtalk(SM_AUTH, "AUTH %s", chosen_mech);
802 * Now we loop until we either fail, get a SASL_OK, or a 235
803 * response code. Receive the challenges and process them until
807 while (result == SASL_CONTINUE) {
810 * If we get a 235 response, that means authentication has
811 * succeeded and we need to break out of the loop (yes, even if
812 * we still get SASL_CONTINUE from sasl_client_step()).
814 * Otherwise, if we get a message that doesn't seem to be a
815 * valid response, then abort
820 else if (status < 300 || status > 399)
824 * Special case; a zero-length response from the SMTP server
825 * is returned as a single =. If we get that, then set buflen
826 * to be zero. Otherwise, just decode the response.
829 if (strcmp("=", sm_reply.text) == 0) {
832 result = sasl_decode64(sm_reply.text, sm_reply.length,
833 outbuf, sizeof(outbuf), &outlen);
835 if (result != SASL_OK) {
836 smtalk(SM_AUTH, "*");
837 sm_ierror("SASL base64 decode failed: %s",
838 sasl_errstring(result, NULL, NULL));
843 result = sasl_client_step(conn, outbuf, outlen, NULL,
844 (const char **) &buf, &buflen);
846 if (result != SASL_OK && result != SASL_CONTINUE) {
847 smtalk(SM_AUTH, "*");
848 sm_ierror("SASL client negotiation failed: %s",
849 sasl_errstring(result, NULL, NULL));
853 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
855 if (status != SASL_OK) {
856 smtalk(SM_AUTH, "*");
857 sm_ierror("SASL base64 encode failed: %s",
858 sasl_errstring(status, NULL, NULL));
862 status = smtalk(SM_AUTH, outbuf);
866 * Make sure that we got the correct response
869 if (status < 200 || status > 299)
873 * We _should_ have completed the authentication successfully.
874 * Get a few properties from the authentication exchange.
877 result = sasl_getprop(conn, SASL_MAXOUTBUF, (const void **) &outbufmax);
879 if (result != SASL_OK) {
880 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
881 sasl_errstring(result, NULL, NULL));
885 maxoutbuf = *outbufmax;
887 result = sasl_getprop(conn, SASL_SSF, (const void **) &ssf);
891 if (result != SASL_OK) {
892 sm_ierror("Cannot retrieve SASL negotiated security strength "
893 "factor: %s", sasl_errstring(result, NULL, NULL));
898 sasl_outbuffer = malloc(maxoutbuf);
900 if (sasl_outbuffer == NULL) {
901 sm_ierror("Unable to allocate %d bytes for SASL output "
902 "buffer", maxoutbuf);
907 sasl_inbuffer = malloc(SASL_MAXRECVBUF);
909 if (sasl_inbuffer == NULL) {
910 sm_ierror("Unable to allocate %d bytes for SASL input "
911 "buffer", SASL_MAXRECVBUF);
912 free(sasl_outbuffer);
916 sasl_inptr = sasl_inbuffer;
918 sasl_outbuffer = NULL;
919 sasl_inbuffer = NULL;
928 * Our callback functions to feed data to the SASL library
932 sm_get_user(void *context, int id, const char **result, unsigned *len)
934 char *user = (char *) context;
936 if (! result || ((id != SASL_CB_USER) && (id != SASL_CB_AUTHNAME)))
937 return SASL_BADPARAM;
947 sm_get_pass(sasl_conn_t *conn, void *context, int id,
948 sasl_secret_t **psecret)
950 char **pw_context = (char **) context;
954 if (! psecret || id != SASL_CB_PASS)
955 return SASL_BADPARAM;
957 ruserpass(pw_context[0], &(pw_context[1]), &pass);
961 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
968 (*psecret)->len = len;
969 strcpy((char *) (*psecret)->data, pass);
974 #endif /* CYRUS_SASL */
977 sm_ierror (char *fmt, ...)
982 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
985 sm_reply.length = strlen (sm_reply.text);
986 sm_reply.code = NOTOK;
992 smtalk (int time, char *fmt, ...)
999 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1005 printf("(encrypted) ");
1006 #endif /* CYRUS_SASL */
1007 printf ("=> %s\n", buffer);
1012 alarm ((unsigned) time);
1013 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1022 * write the buffer to the open SMTP channel
1026 sm_wrecord (char *buffer, int len)
1029 return sm_werror ();
1031 sm_fwrite (buffer, len);
1035 return (ferror (sm_wfp) ? sm_werror () : OK);
1040 sm_wstream (char *buffer, int len)
1043 static char lc = '\0';
1046 return sm_werror ();
1048 if (buffer == NULL && len == 0) {
1052 return (ferror (sm_wfp) ? sm_werror () : OK);
1055 for (bp = buffer; len > 0; bp++, len--) {
1064 sm_fputc ('.');/* FALL THROUGH */
1069 if (ferror (sm_wfp))
1070 return sm_werror ();
1075 return (ferror (sm_wfp) ? sm_werror () : OK);
1079 * Write out to the network, but do buffering for SASL (if enabled)
1083 sm_fwrite(char *buffer, int len)
1087 unsigned int outputlen;
1089 if (sasl_complete == 0 || sasl_ssf == 0)
1090 #endif /* CYRUS_SASL */
1091 fwrite(buffer, sizeof(*buffer), len, sm_wfp);
1094 while (len >= maxoutbuf - sasl_outbuflen) {
1095 memcpy(sasl_outbuffer + sasl_outbuflen, buffer,
1096 maxoutbuf - sasl_outbuflen);
1097 len -= maxoutbuf - sasl_outbuflen;
1100 if (sasl_encode(conn, sasl_outbuffer, maxoutbuf,
1101 &output, &outputlen) != SASL_OK) {
1102 sm_ierror("Unable to SASL encode connection data: %s",
1103 sasl_errdetail(conn));
1107 fwrite(output, sizeof(*output), outputlen, sm_wfp);
1111 memcpy(sasl_outbuffer + sasl_outbuflen, buffer, len);
1112 sasl_outbuflen += len;
1115 #endif /* CYRUS_SASL */
1116 return ferror(sm_wfp) ? NOTOK : RP_OK;
1120 * Convenience functions to replace occurences of fputs() and fputc()
1124 sm_fputs(char *buffer)
1126 return sm_fwrite(buffer, strlen(buffer));
1134 return sm_fwrite(&h, 1);
1138 * Flush out any pending data on the connection
1146 unsigned int outputlen;
1149 if (sasl_complete == 1 && sasl_ssf > 0 && sasl_outbuflen > 0) {
1150 result = sasl_encode(conn, sasl_outbuffer, sasl_outbuflen,
1151 &output, &outputlen);
1152 if (result != SASL_OK) {
1153 sm_ierror("Unable to SASL encode connection data: %s",
1154 sasl_errdetail(conn));
1158 fwrite(output, sizeof(*output), outputlen, sm_wfp);
1161 #endif /* CYRUS_SASL */
1170 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1171 : sm_alarmed ? "write to socket timed out"
1172 : "error writing to socket"));
1174 return (sm_reply.code = NOTOK);
1181 int i, code, cont, bc, rc, more;
1184 char **ehlo, buffer[BUFSIZ];
1187 static int at_least_once = 0;
1189 if (at_least_once) {
1192 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1206 sm_reply.length = 0;
1207 sm_reply.text[0] = 0;
1209 rc = sizeof(sm_reply.text) - 1;
1211 for (more = FALSE; sm_rrecord ((char *) (bp = (unsigned char *) buffer),
1216 printf("(decrypted) ");
1217 #endif /* CYRUS_SASL */
1218 printf ("<= %s\n", buffer);
1223 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1224 && (buffer[3] == '-' || doingEHLO == 2)
1226 if (doingEHLO == 2) {
1227 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1228 strcpy (*ehlo++, buffer + 4);
1230 if (ehlo >= EHLOkeys + MAXEHLO)
1240 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1244 code = atoi ((char *) bp);
1246 for (; bc > 0 && isspace (*bp); bp++, bc--)
1248 if (bc > 0 && *bp == '-') {
1251 for (; bc > 0 && isspace (*bp); bp++, bc--)
1256 if (code != sm_reply.code || cont)
1260 sm_reply.code = code;
1263 /* can never fail to 0-terminate because of size of buffer vs fixed string */
1264 strncpy (buffer, sm_noreply, sizeof(buffer));
1265 bp = (unsigned char *) buffer;
1266 bc = strlen (sm_noreply);
1270 if ((i = min (bc, rc)) > 0) {
1274 i = strlen(sm_moreply);
1275 if (more && rc > i + 1) {
1276 memcpy (rp, sm_moreply, i); /* safe because of check in if() */
1283 if (sm_reply.code < 100) {
1285 printf ("%s\n", sm_reply.text);
1291 sm_reply.length = rp - sm_reply.text;
1292 sm_reply.text[sm_reply.length] = 0;
1293 return sm_reply.code;
1300 sm_rrecord (char *buffer, int *len)
1305 return sm_rerror(0);
1307 buffer[*len = 0] = 0;
1309 if ((retval = sm_fgets (buffer, BUFSIZ, sm_rfp)) != RP_OK)
1311 *len = strlen (buffer);
1312 /* *len should be >0 except on EOF, but check for safety's sake */
1314 return sm_rerror (RP_EOF);
1315 if (buffer[*len - 1] != '\n')
1316 while ((retval = sm_fgetc (sm_rfp)) != '\n' && retval != EOF &&
1320 if ((*len > 1) && (buffer[*len - 2] == '\r'))
1329 * Our version of fgets, which calls our private fgetc function
1333 sm_fgets(char *buffer, int size, FILE *f)
1347 } while (size > 1 && c != '\n');
1357 * Read from the network, but do SASL encryption
1363 char tmpbuf[BUFSIZ], *retbuf;
1364 unsigned int retbufsize = 0;
1368 * If we have leftover data, return it
1371 if (sasl_inbuflen) {
1373 return (int) *sasl_inptr++;
1377 * If not, read from the network until we have some data to return
1380 while (retbufsize == 0) {
1382 cc = read(fileno(f), tmpbuf, sizeof(tmpbuf));
1388 sm_ierror("Unable to read from network: %s", strerror(errno));
1393 * Don't call sasl_decode unless sasl is complete and we have
1394 * encryption working
1397 if (sasl_complete == 0 || sasl_ssf == 0) {
1401 result = sasl_decode(conn, tmpbuf, cc, (const char **) &retbuf,
1404 if (result != SASL_OK) {
1405 sm_ierror("Unable to decode SASL network data: %s",
1406 sasl_errdetail(conn));
1412 if (retbufsize > SASL_MAXRECVBUF) {
1413 sm_ierror("Received data (%d bytes) is larger than the buffer "
1414 "size (%d bytes)", retbufsize, SASL_MAXRECVBUF);
1418 memcpy(sasl_inbuffer, retbuf, retbufsize);
1419 sasl_inptr = sasl_inbuffer + 1;
1420 sasl_inbuflen = retbufsize - 1;
1422 return (int) sasl_inbuffer[0];
1424 #endif /* CYRUS_SASL */
1429 if (sm_mts == MTS_SMTP)
1431 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1432 : sm_alarmed ? "read from socket timed out"
1433 : rc == RP_EOF ? "premature end-of-file on socket"
1434 : "error reading from socket"));
1437 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1438 : sm_alarmed ? "read from pipe timed out"
1439 : rc == RP_EOF ? "premature end-of-file on pipe"
1440 : "error reading from pipe"));
1442 return (sm_reply.code = NOTOK);
1449 #ifndef RELIABLE_SIGNALS
1450 SIGNAL (SIGALRM, alrmser);
1455 printf ("timed out...\n");
1462 rp_string (int code)
1465 static char buffer[BUFSIZ];
1467 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1487 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1507 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1508 text, sm_reply.code, sm_reply.text);
1520 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1522 if (strncmp (ep, s, len) == 0) {
1523 for (ep += len; *ep == ' '; ep++)