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.
14 #include "fetchmail.h"
19 static void SMTP_auth(int, char *);
27 static struct opt extensions[] =
29 {"8BITMIME", ESMTP_8BITMIME},
32 {"AUTH ", ESMTP_AUTH},
35 #endif /* ODMR_ENABLE */
39 char smtp_response[MSGBUFSIZE];
40 char esmtp_auth_username[65];
41 char esmtp_auth_password[256];
43 static char smtp_mode = 'S';
45 void SMTP_setmode(char sl)
46 /* set whether we are speaking SMTP or LMTP */
51 int SMTP_helo(int sock,const char *host)
52 /* send a "HELO" message to the SMTP listener */
56 SockPrintf(sock,"HELO %s\r\n", host);
57 if (outlevel >= O_MONITOR)
58 report(stdout, "SMTP> HELO %s\n", host);
63 int SMTP_ehlo(int sock, const char *host, int *opt)
64 /* send a "EHLO" message to the SMTP listener, return extension status bits */
67 char auth_response[256];
69 SockPrintf(sock,"%cHLO %s\r\n", (smtp_mode == 'S') ? 'E' : smtp_mode, host);
70 if (outlevel >= O_MONITOR)
71 report(stdout, "%cMTP> %cHLO %s\n",
72 smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
75 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
77 int n = strlen(smtp_response);
79 if (smtp_response[strlen(smtp_response)-1] == '\n')
80 smtp_response[strlen(smtp_response)-1] = '\0';
81 if (smtp_response[strlen(smtp_response)-1] == '\r')
82 smtp_response[strlen(smtp_response)-1] = '\0';
85 smtp_response[n] = '\0';
86 if (outlevel >= O_MONITOR)
87 report(stdout, "SMTP< %s\n", smtp_response);
88 for (hp = extensions; hp->name; hp++)
89 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name))) {
91 if (strncmp(hp->name, "AUTH ", 5) == 0)
92 strncpy(auth_response, smtp_response, sizeof(auth_response));
94 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ') {
95 if (*opt & ESMTP_AUTH)
96 SMTP_auth(sock, auth_response);
99 else if (smtp_response[3] != '-')
102 return SM_UNRECOVERABLE;
105 int SMTP_from(int sock, const char *from, const char *opts)
106 /* send a "MAIL FROM:" message to the SMTP listener */
109 char buf[MSGBUFSIZE];
111 if (strchr(from, '<'))
113 snprintf(buf, sizeof(buf),
116 #endif /* HAVE_SNPRINTF */
117 "MAIL FROM: %s", from);
120 snprintf(buf, sizeof(buf),
123 #endif /* HAVE_SNPRINTF */
124 "MAIL FROM:<%s>", from);
127 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
130 #endif /* HAVE_SNPRINTF */
131 SockPrintf(sock,"%s\r\n", buf);
132 if (outlevel >= O_MONITOR)
133 report(stdout, "%cMTP> %s\n", smtp_mode, buf);
138 int SMTP_rcpt(int sock, const char *to)
139 /* send a "RCPT TO:" message to the SMTP listener */
143 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
144 if (outlevel >= O_MONITOR)
145 report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
150 int SMTP_data(int sock)
151 /* send a "DATA" message to the SMTP listener */
155 SockPrintf(sock,"DATA\r\n");
156 if (outlevel >= O_MONITOR)
157 report(stdout, "%cMTP> DATA\n", smtp_mode);
162 int SMTP_rset(int sock)
163 /* send a "RSET" message to the SMTP listener */
167 SockPrintf(sock,"RSET\r\n");
168 if (outlevel >= O_MONITOR)
169 report(stdout, "%cMTP> RSET\n", smtp_mode);
174 int SMTP_quit(int sock)
175 /* send a "QUIT" message to the SMTP listener */
179 SockPrintf(sock,"QUIT\r\n");
180 if (outlevel >= O_MONITOR)
181 report(stdout, "%cMTP> QUIT\n", smtp_mode);
186 int SMTP_eom(int sock)
187 /* send a message data terminator to the SMTP listener */
191 SockPrintf(sock,".\r\n");
192 if (outlevel >= O_MONITOR)
193 report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
196 * When doing LMTP, must process many of these at the outer level.
198 if (smtp_mode == 'S')
206 int SMTP_ok(int sock)
207 /* returns status of SMTP connection */
209 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
211 int n = strlen(smtp_response);
213 if (smtp_response[strlen(smtp_response)-1] == '\n')
214 smtp_response[strlen(smtp_response)-1] = '\0';
215 if (smtp_response[strlen(smtp_response)-1] == '\r')
216 smtp_response[strlen(smtp_response)-1] = '\0';
219 smtp_response[n] = '\0';
220 if (outlevel >= O_MONITOR)
221 report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
222 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
224 else if (smtp_response[3] != '-')
227 return SM_UNRECOVERABLE;
230 static void SMTP_auth(int sock, char *buf)
231 /* ESMTP Authentication support for Fetchmail by Wojciech Polak */
238 memset(b64buf, 0, sizeof(b64buf));
239 memset(tmp, 0, sizeof(tmp));
241 if (strstr(buf, "CRAM-MD5")) {
242 unsigned char digest[16];
243 static char ascii_digest[33];
244 memset(digest, 0, 16);
246 if (outlevel >= O_MONITOR)
247 report(stdout, "ESMTP CRAM-MD5 Authentication...\n");
248 SockPrintf(sock, "AUTH CRAM-MD5\r\n");
249 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
250 strncpy(tmp, smtp_response, sizeof(tmp));
252 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
253 SockPrintf(sock, "*\r\n");
254 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
255 if (outlevel >= O_MONITOR)
256 report(stdout, "Server rejected the AUTH command.\n");
260 p = strchr(tmp, ' ');
262 from64tobits(b64buf, p, sizeof(b64buf));
263 if (outlevel >= O_DEBUG)
264 report(stdout, "Challenge decoded: %s\n", b64buf);
265 hmac_md5(esmtp_auth_password, strlen(esmtp_auth_password),
266 b64buf, strlen(b64buf), digest, sizeof(digest));
267 for (c = 0; c < 16; c++)
268 sprintf(ascii_digest + 2 * c, "%02x", digest[c]);
270 snprintf(tmp, sizeof(tmp),
273 #endif /* HAVE_SNPRINTF */
274 "%s %s", esmtp_auth_username, ascii_digest);
276 to64frombits(b64buf, tmp, strlen(tmp));
277 SockPrintf(sock, "%s\r\n", b64buf);
280 else if (strstr(buf, "PLAIN")) {
282 if (outlevel >= O_MONITOR)
283 report(stdout, "ESMTP PLAIN Authentication...\n");
285 snprintf(tmp, sizeof(tmp),
288 #endif /* HAVE_SNPRINTF */
289 "^%s^%s", esmtp_auth_username, esmtp_auth_password);
292 for (c = len - 1; c >= 0; c--)
297 to64frombits(b64buf, tmp, len);
298 SockPrintf(sock, "AUTH PLAIN %s\r\n", b64buf);
301 else if (strstr(buf, "LOGIN")) {
302 if (outlevel >= O_MONITOR)
303 report(stdout, "ESMTP LOGIN Authentication...\n");
304 SockPrintf(sock, "AUTH LOGIN\r\n");
305 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
306 strncpy(tmp, smtp_response, sizeof(tmp));
308 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
309 SockPrintf(sock, "*\r\n");
310 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
311 if (outlevel >= O_MONITOR)
312 report(stdout, "Server rejected the AUTH command.\n");
316 p = strchr(tmp, ' ');
318 from64tobits(b64buf, p, sizeof(b64buf));
319 to64frombits(b64buf, esmtp_auth_username, strlen(esmtp_auth_username));
320 SockPrintf(sock, "%s\r\n", b64buf);
321 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
322 strncpy(tmp, smtp_response, sizeof(tmp));
323 p = strchr(tmp, ' ');
325 from64tobits(b64buf, p, sizeof(b64buf));
326 to64frombits(b64buf, esmtp_auth_password, strlen(esmtp_auth_password));
327 SockPrintf(sock, "%s\r\n", b64buf);
333 /* smtp.c ends here */