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"
25 static struct opt extensions[] =
27 {"8BITMIME", ESMTP_8BITMIME},
30 {"AUTH ", ESMTP_AUTH},
33 #endif /* ODMR_ENABLE */
37 char smtp_response[MSGBUFSIZE];
39 static char smtp_mode = 'S';
41 void SMTP_setmode(char sl)
42 /* set whether we are speaking SMTP or LMTP */
47 int SMTP_helo(int sock,const char *host)
48 /* send a "HELO" message to the SMTP listener */
52 SockPrintf(sock,"HELO %s\r\n", host);
53 if (outlevel >= O_MONITOR)
54 report(stdout, "SMTP> HELO %s\n", host);
59 static void SMTP_auth(int sock, char *username, char *password, char *buf)
60 /* ESMTP Authentication support for fetchmail by Wojciech Polak */
67 memset(b64buf, 0, sizeof(b64buf));
68 memset(tmp, 0, sizeof(tmp));
70 if (strstr(buf, "CRAM-MD5")) {
71 unsigned char digest[16];
72 static char ascii_digest[33];
73 memset(digest, 0, 16);
75 if (outlevel >= O_MONITOR)
76 report(stdout, "ESMTP CRAM-MD5 Authentication...\n");
77 SockPrintf(sock, "AUTH CRAM-MD5\r\n");
78 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
79 strncpy(tmp, smtp_response, sizeof(tmp));
81 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
82 SockPrintf(sock, "*\r\n");
83 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
84 if (outlevel >= O_MONITOR)
85 report(stdout, "Server rejected the AUTH command.\n");
91 from64tobits(b64buf, p, sizeof(b64buf));
92 if (outlevel >= O_DEBUG)
93 report(stdout, "Challenge decoded: %s\n", b64buf);
94 hmac_md5(password, strlen(password),
95 b64buf, strlen(b64buf), digest, sizeof(digest));
96 for (c = 0; c < 16; c++)
97 sprintf(ascii_digest + 2 * c, "%02x", digest[c]);
99 snprintf(tmp, sizeof(tmp),
102 #endif /* HAVE_SNPRINTF */
103 "%s %s", username, ascii_digest);
105 to64frombits(b64buf, tmp, strlen(tmp));
106 SockPrintf(sock, "%s\r\n", b64buf);
109 else if (strstr(buf, "PLAIN")) {
111 if (outlevel >= O_MONITOR)
112 report(stdout, "ESMTP PLAIN Authentication...\n");
114 snprintf(tmp, sizeof(tmp),
117 #endif /* HAVE_SNPRINTF */
118 "^%s^%s", username, password);
121 for (c = len - 1; c >= 0; c--)
126 to64frombits(b64buf, tmp, len);
127 SockPrintf(sock, "AUTH PLAIN %s\r\n", b64buf);
130 else if (strstr(buf, "LOGIN")) {
131 if (outlevel >= O_MONITOR)
132 report(stdout, "ESMTP LOGIN Authentication...\n");
133 SockPrintf(sock, "AUTH LOGIN\r\n");
134 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
135 strncpy(tmp, smtp_response, sizeof(tmp));
137 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
138 SockPrintf(sock, "*\r\n");
139 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
140 if (outlevel >= O_MONITOR)
141 report(stdout, "Server rejected the AUTH command.\n");
145 p = strchr(tmp, ' ');
147 from64tobits(b64buf, p, sizeof(b64buf));
148 to64frombits(b64buf, username, strlen(username));
149 SockPrintf(sock, "%s\r\n", b64buf);
150 SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
151 strncpy(tmp, smtp_response, sizeof(tmp));
152 p = strchr(tmp, ' ');
154 from64tobits(b64buf, p, sizeof(b64buf));
155 to64frombits(b64buf, password, strlen(password));
156 SockPrintf(sock, "%s\r\n", b64buf);
162 int SMTP_ehlo(int sock, const char *host, char *name, char *password, int *opt)
163 /* send a "EHLO" message to the SMTP listener, return extension status bits */
166 char auth_response[256];
168 SockPrintf(sock,"%cHLO %s\r\n", (smtp_mode == 'S') ? 'E' : smtp_mode, host);
169 if (outlevel >= O_MONITOR)
170 report(stdout, "%cMTP> %cHLO %s\n",
171 smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
174 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
176 int n = strlen(smtp_response);
178 if (smtp_response[strlen(smtp_response)-1] == '\n')
179 smtp_response[strlen(smtp_response)-1] = '\0';
180 if (smtp_response[strlen(smtp_response)-1] == '\r')
181 smtp_response[strlen(smtp_response)-1] = '\0';
184 smtp_response[n] = '\0';
185 if (outlevel >= O_MONITOR)
186 report(stdout, "SMTP< %s\n", smtp_response);
187 for (hp = extensions; hp->name; hp++)
188 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name))) {
190 if (strncmp(hp->name, "AUTH ", 5) == 0)
191 strncpy(auth_response, smtp_response, sizeof(auth_response));
193 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ') {
194 if (*opt & ESMTP_AUTH)
195 SMTP_auth(sock, name, password, auth_response);
198 else if (smtp_response[3] != '-')
201 return SM_UNRECOVERABLE;
204 int SMTP_from(int sock, const char *from, const char *opts)
205 /* send a "MAIL FROM:" message to the SMTP listener */
208 char buf[MSGBUFSIZE];
210 if (strchr(from, '<'))
212 snprintf(buf, sizeof(buf),
215 #endif /* HAVE_SNPRINTF */
216 "MAIL FROM: %s", from);
219 snprintf(buf, sizeof(buf),
222 #endif /* HAVE_SNPRINTF */
223 "MAIL FROM:<%s>", from);
226 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
229 #endif /* HAVE_SNPRINTF */
230 SockPrintf(sock,"%s\r\n", buf);
231 if (outlevel >= O_MONITOR)
232 report(stdout, "%cMTP> %s\n", smtp_mode, buf);
237 int SMTP_rcpt(int sock, const char *to)
238 /* send a "RCPT TO:" message to the SMTP listener */
242 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
243 if (outlevel >= O_MONITOR)
244 report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
249 int SMTP_data(int sock)
250 /* send a "DATA" message to the SMTP listener */
254 SockPrintf(sock,"DATA\r\n");
255 if (outlevel >= O_MONITOR)
256 report(stdout, "%cMTP> DATA\n", smtp_mode);
261 int SMTP_rset(int sock)
262 /* send a "RSET" message to the SMTP listener */
266 SockPrintf(sock,"RSET\r\n");
267 if (outlevel >= O_MONITOR)
268 report(stdout, "%cMTP> RSET\n", smtp_mode);
273 int SMTP_quit(int sock)
274 /* send a "QUIT" message to the SMTP listener */
278 SockPrintf(sock,"QUIT\r\n");
279 if (outlevel >= O_MONITOR)
280 report(stdout, "%cMTP> QUIT\n", smtp_mode);
285 int SMTP_eom(int sock)
286 /* send a message data terminator to the SMTP listener */
290 SockPrintf(sock,".\r\n");
291 if (outlevel >= O_MONITOR)
292 report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
295 * When doing LMTP, must process many of these at the outer level.
297 if (smtp_mode == 'S')
305 int SMTP_ok(int sock)
306 /* returns status of SMTP connection */
308 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
310 int n = strlen(smtp_response);
312 if (smtp_response[strlen(smtp_response)-1] == '\n')
313 smtp_response[strlen(smtp_response)-1] = '\0';
314 if (smtp_response[strlen(smtp_response)-1] == '\r')
315 smtp_response[strlen(smtp_response)-1] = '\0';
318 smtp_response[n] = '\0';
319 if (outlevel >= O_MONITOR)
320 report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
321 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
323 else if (smtp_response[3] != '-')
326 return SM_UNRECOVERABLE;
329 /* smtp.c ends here */