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 int sm_getc(void);
148 static void sm_fflush(void);
149 static int sm_fgets(char *, int, FILE *);
153 * Function prototypes needed for SASL
156 static int sm_auth_sasl(char *, char *, char *);
157 #endif /* CYRUS_SASL */
160 sm_init (char *client, char *server, char *port, int watch, int verbose,
161 int debug, int onex, int queued, int sasl, char *saslmech,
164 if (sm_mts == MTS_SMTP)
165 return smtp_init (client, server, port, watch, verbose,
166 debug, onex, queued, sasl, saslmech, user);
168 return sendmail_init (client, server, watch, verbose,
169 debug, onex, queued);
173 smtp_init (char *client, char *server, char *port, int watch, int verbose,
174 int debug, int onex, int queued, int sasl, char *saslmech,
179 #endif /* CYRUS_SASL */
180 int result, sd1, sd2;
185 sm_verbose = verbose;
188 if (sm_rfp != NULL && sm_wfp != NULL)
191 if (client == NULL || *client == '\0') {
195 client = LocalName(); /* no clientname -> LocalName */
200 if (client == NULL || *client == '\0')
201 client = "localhost";
205 sasl_inbuffer = malloc(SASL_MAXRECVBUF);
207 return sm_ierror("Unable to allocate %d bytes for read buffer",
209 #endif /* CYRUS_SASL */
211 if ((sd1 = rclient (server, port)) == NOTOK)
214 if ((sd2 = dup (sd1)) == NOTOK) {
216 return sm_ierror ("unable to dup");
219 SIGNAL (SIGALRM, alrmser);
220 SIGNAL (SIGPIPE, SIG_IGN);
222 if ((sm_rfp = fdopen (sd1, "r")) == NULL
223 || (sm_wfp = fdopen (sd2, "w")) == NULL) {
226 sm_rfp = sm_wfp = NULL;
227 return sm_ierror ("unable to fdopen");
245 * Give EHLO or HELO command
247 if (client && *client) {
249 result = smtalk (SM_HELO, "EHLO %s", client);
252 if (result >= 500 && result <= 599)
253 result = smtalk (SM_HELO, "HELO %s", client);
263 * If the user asked for SASL, then check to see if the SMTP server
264 * supports it. Otherwise, error out (because the SMTP server
265 * might have been spoofed; we don't want to just silently not
270 if (! (server_mechs = EHLOset("AUTH"))) {
272 return sm_ierror("SMTP server does not support SASL");
275 if (saslmech && stringdex(saslmech, server_mechs) == -1) {
277 return sm_ierror("Requested SASL mech \"%s\" is not in the "
278 "list of supported mechanisms:\n%s",
279 saslmech, server_mechs);
282 if (sm_auth_sasl(user, saslmech ? saslmech : server_mechs,
288 #endif /* CYRUS_SASL */
291 if (watch && EHLOset ("XVRB"))
292 smtalk (SM_HELO, "VERB on");
293 if (onex && EHLOset ("XONE"))
294 smtalk (SM_HELO, "ONEX");
295 if (queued && EHLOset ("XQUE"))
296 smtalk (SM_HELO, "QUED");
302 sendmail_init (char *client, char *server, int watch, int verbose,
303 int debug, int onex, int queued)
312 sm_verbose = verbose;
314 if (sm_rfp != NULL && sm_wfp != NULL)
317 if (client == NULL || *client == '\0') {
321 client = LocalName(); /* no clientname -> LocalName */
325 if (client == NULL || *client == '\0')
326 client = "localhost";
329 if (pipe (pdi) == NOTOK)
330 return sm_ierror ("no pipes");
331 if (pipe (pdo) == NOTOK) {
334 return sm_ierror ("no pipes");
337 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
346 return sm_ierror ("unable to fork");
349 if (pdo[0] != fileno (stdin))
350 dup2 (pdo[0], fileno (stdin));
351 if (pdi[1] != fileno (stdout))
352 dup2 (pdi[1], fileno (stdout));
353 if (pdi[1] != fileno (stderr))
354 dup2 (pdi[1], fileno (stderr));
355 for (i = fileno (stderr) + 1; i < NBITS; i++)
359 vec[vecp++] = r1bindex (sendmail, '/');
362 vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
363 vec[vecp++] = "-oem";
368 # endif /* not RAND */
369 #endif /* not ZMAILER */
374 execvp (sendmail, vec);
375 fprintf (stderr, "unable to exec ");
377 _exit (-1); /* NOTREACHED */
380 SIGNAL (SIGALRM, alrmser);
381 SIGNAL (SIGPIPE, SIG_IGN);
385 if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
386 || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
389 sm_rfp = sm_wfp = NULL;
390 return sm_ierror ("unable to fdopen");
405 if (client && *client) {
407 result = smtalk (SM_HELO, "EHLO %s", client);
410 if (500 <= result && result <= 599)
411 result = smtalk (SM_HELO, "HELO %s", client);
425 smtalk (SM_HELO, "ONEX");
428 smtalk (SM_HELO, "VERB on");
435 rclient (char *server, char *service)
438 char response[BUFSIZ];
440 if ((sd = client (server, service, response, sizeof(response),
444 sm_ierror ("%s", response);
449 sm_winit (int mode, char *from)
471 switch (smtalk (SM_MAIL, "%s FROM:<%s>", smtpcom, from)) {
488 sm_wadr (char *mbox, char *host, char *path)
490 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
492 path ? path : "", mbox, host)) {
502 #endif /* SENDMAILBUG */
527 switch (smtalk (SM_DATA, "DATA")) {
536 #endif /* SENDMAILBUG */
553 sm_wtxt (char *buffer, int len)
559 result = sm_wstream (buffer, len);
562 return (result == NOTOK ? RP_BHST : RP_OK);
569 if (sm_wstream ((char *) NULL, 0) == NOTOK)
572 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
580 #endif /* SENDMAILBUG */
598 if (sm_mts == MTS_SENDMAIL) {
609 if (sm_rfp == NULL && sm_wfp == NULL)
614 smtalk (SM_QUIT, "QUIT");
618 sm_note.code = sm_reply.code;
619 sm_note.length = sm_reply.length;
620 memcpy (sm_note.text, sm_reply.text, sm_reply.length + 1);/* fall */
622 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
624 if (sm_mts == MTS_SMTP)
625 smtalk (SM_QUIT, "QUIT");
627 kill (sm_child, SIGKILL);
632 sm_reply.code = sm_note.code;
633 sm_reply.length = sm_note.length;
634 memcpy (sm_reply.text, sm_note.text, sm_note.length + 1);
639 if (sm_rfp != NULL) {
644 if (sm_wfp != NULL) {
650 if (sm_mts == MTS_SMTP) {
655 if (sasl_outbuffer) {
656 free(sasl_outbuffer);
661 #endif /* CYRUS_SASL */
663 status = pidwait (sm_child, OK);
667 sm_rfp = sm_wfp = NULL;
668 return (status ? RP_BHST : RP_OK);
673 * This function implements SASL authentication for SMTP. If this function
674 * completes successfully, then authentication is successful and we've
675 * (optionally) negotiated a security layer.
678 sm_auth_sasl(char *user, char *mechlist, char *inhost)
681 unsigned int buflen, outlen;
682 char *buf, outbuf[BUFSIZ], host[NI_MAXHOST];
683 const char *chosen_mech;
684 sasl_security_properties_t secprops;
689 * Initialize the callback contexts
693 user = getusername();
695 callbacks[SM_SASL_N_CB_USER].context = user;
696 callbacks[SM_SASL_N_CB_AUTHNAME].context = user;
699 * This is a _bit_ of a hack ... but if the hostname wasn't supplied
700 * to us on the command line, then call getpeername and do a
701 * reverse-address lookup on the IP address to get the name.
704 memset(host, 0, sizeof(host));
707 struct sockaddr_storage sin;
708 socklen_t len = sizeof(sin);
711 if (getpeername(fileno(sm_wfp), (struct sockaddr *) &sin, &len) < 0) {
712 sm_ierror("getpeername on SMTP socket failed: %s",
717 result = getnameinfo((struct sockaddr *) &sin, len, host, sizeof(host),
718 NULL, 0, NI_NAMEREQD);
720 sm_ierror("Unable to look up name of connected host: %s",
721 gai_strerror(result));
725 strncpy(host, inhost, sizeof(host) - 1);
728 sasl_pw_context[0] = host;
729 sasl_pw_context[1] = user;
731 callbacks[SM_SASL_N_CB_PASS].context = sasl_pw_context;
733 result = sasl_client_init(callbacks);
735 if (result != SASL_OK) {
736 sm_ierror("SASL library initialization failed: %s",
737 sasl_errstring(result, NULL, NULL));
741 result = sasl_client_new("smtp", host, NULL, NULL, NULL, 0, &conn);
743 if (result != SASL_OK) {
744 sm_ierror("SASL client initialization failed: %s",
745 sasl_errstring(result, NULL, NULL));
750 * Initialize the security properties
753 memset(&secprops, 0, sizeof(secprops));
754 secprops.maxbufsize = SASL_MAXRECVBUF;
755 secprops.max_ssf = UINT_MAX;
757 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
759 if (result != SASL_OK) {
760 sm_ierror("SASL security property initialization failed: %s",
761 sasl_errstring(result, NULL, NULL));
766 * Start the actual protocol. Feed the mech list into the library
767 * and get out a possible initial challenge
770 result = sasl_client_start(conn, mechlist, NULL, (const char **) &buf,
771 &buflen, (const char **) &chosen_mech);
773 if (result != SASL_OK && result != SASL_CONTINUE) {
774 sm_ierror("SASL client start failed: %s", sasl_errdetail(conn));
779 * If we got an initial challenge, send it as part of the AUTH
780 * command; otherwise, just send a plain AUTH command.
784 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
785 if (status != SASL_OK) {
786 sm_ierror("SASL base64 encode failed: %s",
787 sasl_errstring(status, NULL, NULL));
791 status = smtalk(SM_AUTH, "AUTH %s %s", chosen_mech, outbuf);
793 status = smtalk(SM_AUTH, "AUTH %s", chosen_mech);
796 * Now we loop until we either fail, get a SASL_OK, or a 235
797 * response code. Receive the challenges and process them until
801 while (result == SASL_CONTINUE) {
804 * If we get a 235 response, that means authentication has
805 * succeeded and we need to break out of the loop (yes, even if
806 * we still get SASL_CONTINUE from sasl_client_step()).
808 * Otherwise, if we get a message that doesn't seem to be a
809 * valid response, then abort
814 else if (status < 300 || status > 399)
818 * Special case; a zero-length response from the SMTP server
819 * is returned as a single =. If we get that, then set buflen
820 * to be zero. Otherwise, just decode the response.
823 if (strcmp("=", sm_reply.text) == 0) {
826 result = sasl_decode64(sm_reply.text, sm_reply.length,
827 outbuf, sizeof(outbuf), &outlen);
829 if (result != SASL_OK) {
830 smtalk(SM_AUTH, "*");
831 sm_ierror("SASL base64 decode failed: %s",
832 sasl_errstring(result, NULL, NULL));
837 result = sasl_client_step(conn, outbuf, outlen, NULL,
838 (const char **) &buf, &buflen);
840 if (result != SASL_OK && result != SASL_CONTINUE) {
841 smtalk(SM_AUTH, "*");
842 sm_ierror("SASL client negotiation failed: %s",
843 sasl_errstring(result, NULL, NULL));
847 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
849 if (status != SASL_OK) {
850 smtalk(SM_AUTH, "*");
851 sm_ierror("SASL base64 encode failed: %s",
852 sasl_errstring(status, NULL, NULL));
856 status = smtalk(SM_AUTH, outbuf);
860 * Make sure that we got the correct response
863 if (status < 200 || status > 299)
867 * We _should_ have completed the authentication successfully.
868 * Get a few properties from the authentication exchange.
871 result = sasl_getprop(conn, SASL_MAXOUTBUF, (const void **) &outbufmax);
873 if (result != SASL_OK) {
874 sm_ierror("Cannot retrieve SASL negotiated output buffer size: %s",
875 sasl_errstring(result, NULL, NULL));
879 maxoutbuf = *outbufmax;
881 result = sasl_getprop(conn, SASL_SSF, (const void **) &ssf);
885 if (result != SASL_OK) {
886 sm_ierror("Cannot retrieve SASL negotiated security strength "
887 "factor: %s", sasl_errstring(result, NULL, NULL));
892 sasl_outbuffer = malloc(maxoutbuf);
894 if (sasl_outbuffer == NULL) {
895 sm_ierror("Unable to allocate %d bytes for SASL output "
896 "buffer", maxoutbuf);
901 sasl_inbuffer = malloc(SASL_MAXRECVBUF);
903 if (sasl_inbuffer == NULL) {
904 sm_ierror("Unable to allocate %d bytes for SASL input "
905 "buffer", SASL_MAXRECVBUF);
906 free(sasl_outbuffer);
910 sasl_inptr = sasl_inbuffer;
912 sasl_outbuffer = NULL;
913 sasl_inbuffer = NULL;
922 * Our callback functions to feed data to the SASL library
926 sm_get_user(void *context, int id, const char **result, unsigned *len)
928 char *user = (char *) context;
930 if (! result || ((id != SASL_CB_USER) && (id != SASL_CB_AUTHNAME)))
931 return SASL_BADPARAM;
941 sm_get_pass(sasl_conn_t *conn, void *context, int id,
942 sasl_secret_t **psecret)
944 char **pw_context = (char **) context;
948 if (! psecret || id != SASL_CB_PASS)
949 return SASL_BADPARAM;
951 ruserpass(pw_context[0], &(pw_context[1]), &pass);
955 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
962 (*psecret)->len = len;
963 strcpy((char *) (*psecret)->data, pass);
968 #endif /* CYRUS_SASL */
971 sm_ierror (char *fmt, ...)
976 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
979 sm_reply.length = strlen (sm_reply.text);
980 sm_reply.code = NOTOK;
986 smtalk (int time, char *fmt, ...)
993 vsnprintf (buffer, sizeof(buffer), fmt, ap);
999 printf("(encrypted) ");
1000 #endif /* CYRUS_SASL */
1001 printf ("=> %s\n", buffer);
1006 alarm ((unsigned) time);
1007 if ((result = sm_wrecord (buffer, strlen (buffer))) != NOTOK)
1016 * write the buffer to the open SMTP channel
1020 sm_wrecord (char *buffer, int len)
1023 return sm_werror ();
1025 sm_fwrite (buffer, len);
1029 return (ferror (sm_wfp) ? sm_werror () : OK);
1034 sm_wstream (char *buffer, int len)
1037 static char lc = '\0';
1040 return sm_werror ();
1042 if (buffer == NULL && len == 0) {
1046 return (ferror (sm_wfp) ? sm_werror () : OK);
1049 for (bp = buffer; len > 0; bp++, len--) {
1058 sm_fputc ('.');/* FALL THROUGH */
1063 if (ferror (sm_wfp))
1064 return sm_werror ();
1069 return (ferror (sm_wfp) ? sm_werror () : OK);
1073 * Write out to the network, but do buffering for SASL (if enabled)
1077 sm_fwrite(char *buffer, int len)
1081 unsigned int outputlen;
1083 if (sasl_complete == 0 || sasl_ssf == 0)
1084 #endif /* CYRUS_SASL */
1085 fwrite(buffer, sizeof(*buffer), len, sm_wfp);
1088 while (len >= maxoutbuf - sasl_outbuflen) {
1089 memcpy(sasl_outbuffer + sasl_outbuflen, buffer,
1090 maxoutbuf - sasl_outbuflen);
1091 len -= maxoutbuf - sasl_outbuflen;
1094 if (sasl_encode(conn, sasl_outbuffer, maxoutbuf,
1095 &output, &outputlen) != SASL_OK) {
1096 sm_ierror("Unable to SASL encode connection data: %s",
1097 sasl_errdetail(conn));
1101 fwrite(output, sizeof(*output), outputlen, sm_wfp);
1105 memcpy(sasl_outbuffer + sasl_outbuflen, buffer, len);
1106 sasl_outbuflen += len;
1109 #endif /* CYRUS_SASL */
1110 return ferror(sm_wfp) ? NOTOK : RP_OK;
1114 * Convenience functions to replace occurences of fputs() and fputc()
1118 sm_fputs(char *buffer)
1120 return sm_fwrite(buffer, strlen(buffer));
1128 return sm_fwrite(&h, 1);
1132 * Flush out any pending data on the connection
1140 unsigned int outputlen;
1143 if (sasl_complete == 1 && sasl_ssf > 0 && sasl_outbuflen > 0) {
1144 result = sasl_encode(conn, sasl_outbuffer, sasl_outbuflen,
1145 &output, &outputlen);
1146 if (result != SASL_OK) {
1147 sm_ierror("Unable to SASL encode connection data: %s",
1148 sasl_errdetail(conn));
1152 fwrite(output, sizeof(*output), outputlen, sm_wfp);
1155 #endif /* CYRUS_SASL */
1164 strlen (strcpy (sm_reply.text, sm_wfp == NULL ? "no socket opened"
1165 : sm_alarmed ? "write to socket timed out"
1166 : "error writing to socket"));
1168 return (sm_reply.code = NOTOK);
1175 int i, code, cont, bc, rc, more;
1178 char **ehlo, buffer[BUFSIZ];
1181 static int at_least_once = 0;
1183 if (at_least_once) {
1186 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1200 sm_reply.length = 0;
1201 sm_reply.text[0] = 0;
1203 rc = sizeof(sm_reply.text) - 1;
1205 for (more = FALSE; sm_rrecord ((char *) (bp = (unsigned char *) buffer),
1210 printf("(decrypted) ");
1211 #endif /* CYRUS_SASL */
1212 printf ("<= %s\n", buffer);
1217 && strncmp (buffer, "250", sizeof("250") - 1) == 0
1218 && (buffer[3] == '-' || doingEHLO == 2)
1220 if (doingEHLO == 2) {
1221 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
1222 strcpy (*ehlo++, buffer + 4);
1224 if (ehlo >= EHLOkeys + MAXEHLO)
1234 for (; bc > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, bc--)
1238 code = atoi ((char *) bp);
1240 for (; bc > 0 && isspace (*bp); bp++, bc--)
1242 if (bc > 0 && *bp == '-') {
1245 for (; bc > 0 && isspace (*bp); bp++, bc--)
1250 if (code != sm_reply.code || cont)
1254 sm_reply.code = code;
1257 /* can never fail to 0-terminate because of size of buffer vs fixed string */
1258 strncpy (buffer, sm_noreply, sizeof(buffer));
1259 bp = (unsigned char *) buffer;
1260 bc = strlen (sm_noreply);
1264 if ((i = min (bc, rc)) > 0) {
1268 i = strlen(sm_moreply);
1269 if (more && rc > i + 1) {
1270 memcpy (rp, sm_moreply, i); /* safe because of check in if() */
1277 if (sm_reply.code < 100) {
1279 printf ("%s\n", sm_reply.text);
1285 sm_reply.length = rp - sm_reply.text;
1286 sm_reply.text[sm_reply.length] = 0;
1287 return sm_reply.code;
1294 sm_rrecord (char *buffer, int *len)
1299 return sm_rerror(0);
1301 buffer[*len = 0] = 0;
1303 if ((retval = sm_fgets (buffer, BUFSIZ, sm_rfp)) != RP_OK)
1305 *len = strlen (buffer);
1306 /* *len should be >0 except on EOF, but check for safety's sake */
1308 return sm_rerror (RP_EOF);
1309 if (buffer[*len - 1] != '\n')
1310 while ((retval = sm_fgetc (sm_rfp)) != '\n' && retval != EOF &&
1314 if ((*len > 1) && (buffer[*len - 2] == '\r'))
1323 * Our version of fgets, which calls our private fgetc function
1327 sm_fgets(char *buffer, int size, FILE *f)
1341 } while (size > 1 && c != '\n');
1351 * Read from the network, but do SASL encryption
1357 char tmpbuf[BUFSIZ], *retbuf;
1358 unsigned int retbufsize = 0;
1362 * If we have leftover data, return it
1365 if (sasl_inbuflen) {
1367 return (int) *sasl_inptr++;
1371 * If not, read from the network until we have some data to return
1374 while (retbufsize == 0) {
1376 cc = read(fileno(f), tmpbuf, sizeof(tmpbuf));
1382 sm_ierror("Unable to read from network: %s", strerror(errno));
1387 * Don't call sasl_decode unless sasl is complete and we have
1388 * encryption working
1391 if (sasl_complete == 0 || sasl_ssf == 0) {
1395 result = sasl_decode(conn, tmpbuf, cc, (const char **) &retbuf,
1398 if (result != SASL_OK) {
1399 sm_ierror("Unable to decode SASL network data: %s",
1400 sasl_errdetail(conn));
1406 if (retbufsize > SASL_MAXRECVBUF) {
1407 sm_ierror("Received data (%d bytes) is larger than the buffer "
1408 "size (%d bytes)", retbufsize, SASL_MAXRECVBUF);
1412 memcpy(sasl_inbuffer, retbuf, retbufsize);
1413 sasl_inptr = sasl_inbuffer + 1;
1414 sasl_inbuflen = retbufsize - 1;
1416 return (int) sasl_inbuffer[0];
1418 #endif /* CYRUS_SASL */
1423 if (sm_mts == MTS_SMTP)
1425 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no socket opened"
1426 : sm_alarmed ? "read from socket timed out"
1427 : rc == RP_EOF ? "premature end-of-file on socket"
1428 : "error reading from socket"));
1431 strlen (strcpy (sm_reply.text, sm_rfp == NULL ? "no pipe opened"
1432 : sm_alarmed ? "read from pipe timed out"
1433 : rc == RP_EOF ? "premature end-of-file on pipe"
1434 : "error reading from pipe"));
1436 return (sm_reply.code = NOTOK);
1443 #ifndef RELIABLE_SIGNALS
1444 SIGNAL (SIGALRM, alrmser);
1449 printf ("timed out...\n");
1456 rp_string (int code)
1459 static char buffer[BUFSIZ];
1461 switch (sm_reply.code != NOTOK ? code : NOTOK) {
1481 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
1501 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
1502 text, sm_reply.code, sm_reply.text);
1514 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
1516 if (strncmp (ep, s, len) == 0) {
1517 for (ep += len; *ep == ' '; ep++)