2 * smtp.c -- code for speaking SMTP to a listener port
4 * Concept due to Harry Hochheiser. Implementation by ESR. Cleanup and
5 * strict RFC821 compliance by Cameron MacPherson.
7 * Copyright 1997 Eric S. Raymond
8 * For license terms, see the file COPYING in this directory.
12 #include "fetchmail.h"
28 static struct opt extensions[] =
30 {"8BITMIME", ESMTP_8BITMIME},
33 {"AUTH ", ESMTP_AUTH},
36 #endif /* ODMR_ENABLE */
40 char smtp_response[MSGBUFSIZE];
42 static char smtp_mode = 'S';
44 void SMTP_setmode(char sl)
45 /* set whether we are speaking SMTP or LMTP */
50 int SMTP_helo(int sock,const char *host)
51 /* send a "HELO" message to the SMTP listener */
55 SockPrintf(sock,"HELO %s\r\n", host);
56 if (outlevel >= O_MONITOR)
57 report(stdout, "SMTP> HELO %s\n", host);
62 static void SMTP_auth_error(int sock, char *msg)
64 SockPrintf(sock, "*\r\n");
65 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
66 if (outlevel >= O_MONITOR) report(stdout, msg);
69 static void SMTP_auth(int sock, char *username, char *password, char *buf)
70 /* ESMTP Authentication support for fetchmail by Wojciech Polak */
77 if (!username || !password) return;
79 memset(b64buf, 0, sizeof(b64buf));
80 memset(tmp, 0, sizeof(tmp));
82 if (strstr(buf, "CRAM-MD5")) {
83 unsigned char digest[16];
84 memset(digest, 0, sizeof(digest));
86 if (outlevel >= O_MONITOR)
87 report(stdout, GT_("ESMTP CRAM-MD5 Authentication...\n"));
88 SockPrintf(sock, "AUTH CRAM-MD5\r\n");
89 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
90 strncpy(tmp, smtp_response, sizeof(tmp));
91 tmp[sizeof(tmp)-1] = '\0';
93 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
94 SMTP_auth_error(sock, GT_("Server rejected the AUTH command.\n"));
100 /* (hmh) from64tobits will not NULL-terminate strings! */
101 if (from64tobits(b64buf, p, sizeof(b64buf) - 1) <= 0) {
102 SMTP_auth_error(sock, GT_("Bad base64 reply from server.\n"));
105 if (outlevel >= O_DEBUG)
106 report(stdout, GT_("Challenge decoded: %s\n"), b64buf);
107 hmac_md5(password, strlen(password),
108 b64buf, strlen(b64buf), digest, sizeof(digest));
109 snprintf(tmp, sizeof(tmp),
110 "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
111 username, digest[0], digest[1], digest[2], digest[3],
112 digest[4], digest[5], digest[6], digest[7], digest[8],
113 digest[9], digest[10], digest[11], digest[12], digest[13],
114 digest[14], digest[15]);
116 to64frombits(b64buf, tmp, strlen(tmp));
117 SockPrintf(sock, "%s\r\n", b64buf);
120 else if (strstr(buf, "PLAIN")) {
122 if (outlevel >= O_MONITOR)
123 report(stdout, GT_("ESMTP PLAIN Authentication...\n"));
124 snprintf(tmp, sizeof(tmp), "^%s^%s", username, password);
127 for (c = len - 1; c >= 0; c--)
132 to64frombits(b64buf, tmp, len);
133 SockPrintf(sock, "AUTH PLAIN %s\r\n", b64buf);
136 else if (strstr(buf, "LOGIN")) {
137 if (outlevel >= O_MONITOR)
138 report(stdout, GT_("ESMTP LOGIN Authentication...\n"));
139 SockPrintf(sock, "AUTH LOGIN\r\n");
140 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
141 strncpy(tmp, smtp_response, sizeof(tmp));
142 tmp[sizeof(tmp)-1] = '\0';
144 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
145 SMTP_auth_error(sock, GT_("Server rejected the AUTH command.\n"));
149 p = strchr(tmp, ' ');
151 if (from64tobits(b64buf, p, sizeof(b64buf) - 1) <= 0) {
152 SMTP_auth_error(sock, GT_("Bad base64 reply from server.\n"));
155 to64frombits(b64buf, username, strlen(username));
156 SockPrintf(sock, "%s\r\n", b64buf);
157 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
158 strncpy(tmp, smtp_response, sizeof(tmp));
159 tmp[sizeof(tmp)-1] = '\0';
160 p = strchr(tmp, ' ');
162 SMTP_auth_error(sock, GT_("Bad base64 reply from server.\n"));
166 memset(b64buf, 0, sizeof(b64buf));
167 if (from64tobits(b64buf, p, sizeof(b64buf) - 1) <= 0) {
168 SMTP_auth_error(sock, GT_("Bad base64 reply from server.\n"));
171 to64frombits(b64buf, password, strlen(password));
172 SockPrintf(sock, "%s\r\n", b64buf);
178 int SMTP_ehlo(int sock, const char *host, char *name, char *password, int *opt)
179 /* send a "EHLO" message to the SMTP listener, return extension status bits */
182 char auth_response[511];
184 SockPrintf(sock,"%cHLO %s\r\n", (smtp_mode == 'S') ? 'E' : smtp_mode, host);
185 if (outlevel >= O_MONITOR)
186 report(stdout, "%cMTP> %cHLO %s\n",
187 smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
190 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
192 int n = strlen(smtp_response);
194 if (smtp_response[strlen(smtp_response)-1] == '\n')
195 smtp_response[strlen(smtp_response)-1] = '\0';
196 if (smtp_response[strlen(smtp_response)-1] == '\r')
197 smtp_response[strlen(smtp_response)-1] = '\0';
200 smtp_response[n] = '\0';
201 if (outlevel >= O_MONITOR)
202 report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
203 for (hp = extensions; hp->name; hp++)
204 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name))) {
206 if (strncmp(hp->name, "AUTH ", 5) == 0)
207 strncpy(auth_response, smtp_response, sizeof(auth_response));
208 auth_response[sizeof(auth_response)-1] = '\0';
210 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ') {
211 if (*opt & ESMTP_AUTH)
212 SMTP_auth(sock, name, password, auth_response);
215 else if (smtp_response[3] != '-')
218 return SM_UNRECOVERABLE;
221 int SMTP_from(int sock, const char *from, const char *opts)
222 /* send a "MAIL FROM:" message to the SMTP listener */
225 char buf[MSGBUFSIZE];
228 snprintf(buf, sizeof(buf), "MAIL FROM:%s", from);
230 snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", from);
232 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
233 SockPrintf(sock,"%s\r\n", buf);
234 if (outlevel >= O_MONITOR)
235 report(stdout, "%cMTP> %s\n", smtp_mode, buf);
240 int SMTP_rcpt(int sock, const char *to)
241 /* send a "RCPT TO:" message to the SMTP listener */
245 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
246 if (outlevel >= O_MONITOR)
247 report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
252 int SMTP_data(int sock)
253 /* send a "DATA" message to the SMTP listener */
257 SockPrintf(sock,"DATA\r\n");
258 if (outlevel >= O_MONITOR)
259 report(stdout, "%cMTP> DATA\n", smtp_mode);
264 int SMTP_rset(int sock)
265 /* send a "RSET" message to the SMTP listener */
269 SockPrintf(sock,"RSET\r\n");
270 if (outlevel >= O_MONITOR)
271 report(stdout, "%cMTP> RSET\n", smtp_mode);
276 int SMTP_quit(int sock)
277 /* send a "QUIT" message to the SMTP listener */
281 SockPrintf(sock,"QUIT\r\n");
282 if (outlevel >= O_MONITOR)
283 report(stdout, "%cMTP> QUIT\n", smtp_mode);
288 int SMTP_eom(int sock)
289 /* send a message data terminator to the SMTP listener */
293 SockPrintf(sock,".\r\n");
294 if (outlevel >= O_MONITOR)
295 report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
298 * When doing LMTP, must process many of these at the outer level.
300 if (smtp_mode == 'S')
308 time_t last_smtp_ok = 0;
310 int SMTP_ok(int sock)
311 /* returns status of SMTP connection */
313 SIGHANDLERTYPE alrmsave;
315 /* set an alarm for smtp ok */
316 alrmsave = set_signal_handler(SIGALRM, null_signal_handler);
317 set_timeout(mytimeout);
319 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
325 set_signal_handler(SIGALRM, alrmsave);
327 n = strlen(smtp_response);
328 if (n > 0 && smtp_response[n-1] == '\n')
330 if (n > 0 && smtp_response[n-1] == '\r')
332 smtp_response[n] = '\0';
333 if (outlevel >= O_MONITOR)
334 report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
336 (smtp_response[3] != ' ' && smtp_response[3] != '-'))
338 if (outlevel >= O_MONITOR)
339 report(stderr, GT_("smtp listener protocol error\n"));
340 return SM_UNRECOVERABLE;
343 last_smtp_ok = time((time_t *) NULL);
345 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') &&
346 smtp_response[3] == ' ')
348 else if (smtp_response[3] != '-')
351 /* set an alarm for smtp ok */
352 set_signal_handler(SIGALRM, null_signal_handler);
353 set_timeout(mytimeout);
359 set_signal_handler(SIGALRM, alrmsave);
361 if (outlevel >= O_MONITOR)
362 report(stderr, GT_("smtp listener protocol error\n"));
363 return SM_UNRECOVERABLE;
366 /* smtp.c ends here */