]> Pileus Git - ~andy/fetchmail/blob - smtp.c
ee99bcf7c0e55c2b41e63b54be9fff1bc0ffc64c
[~andy/fetchmail] / smtp.c
1 /*
2  * smtp.c -- code for speaking SMTP to a listener port
3  *
4  * Concept due to Harry Hochheiser.  Implementation by ESR.  Cleanup and
5  * strict RFC821 compliance by Cameron MacPherson.
6  *
7  * Copyright 1997 Eric S. Raymond
8  * For license terms, see the file COPYING in this directory.
9  */
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include "fetchmail.h"
15 #include "socket.h"
16 #include "smtp.h"
17 #include "config.h"
18
19 static void SMTP_auth(int, char *);
20
21 struct opt
22 {
23     const char *name;
24     int value;
25 };
26
27 static struct opt extensions[] =
28 {
29     {"8BITMIME",        ESMTP_8BITMIME},
30     {"SIZE",            ESMTP_SIZE},
31     {"ETRN",            ESMTP_ETRN},
32     {"AUTH ",           ESMTP_AUTH},
33 #ifdef ODMR_ENABLE
34     {"ATRN",            ESMTP_ATRN},
35 #endif /* ODMR_ENABLE */
36     {(char *)NULL, 0},
37 };
38
39 char smtp_response[MSGBUFSIZE];
40 char esmtp_auth_username[65];
41 char esmtp_auth_password[256];
42
43 static char smtp_mode = 'S';
44
45 void SMTP_setmode(char sl)
46 /* set whether we are speaking SMTP or LMTP */
47 {
48     smtp_mode = sl;
49 }
50
51 int SMTP_helo(int sock,const char *host)
52 /* send a "HELO" message to the SMTP listener */
53 {
54   int ok;
55
56   SockPrintf(sock,"HELO %s\r\n", host);
57   if (outlevel >= O_MONITOR)
58       report(stdout, "SMTP> HELO %s\n", host);
59   ok = SMTP_ok(sock);
60   return ok;
61 }
62
63 int SMTP_ehlo(int sock, const char *host, int *opt)
64 /* send a "EHLO" message to the SMTP listener, return extension status bits */
65 {
66   struct opt *hp;
67   char auth_response[256];
68
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);
73   
74   *opt = 0;
75   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
76   {
77       int  n = strlen(smtp_response);
78
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';
83       if (n < 4)
84           return SM_ERROR;
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))) {
90               *opt |= hp->value;
91               if (strncmp(hp->name, "AUTH ", 5) == 0)
92                 strncpy(auth_response, smtp_response, sizeof(auth_response));
93           }
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);
97           return SM_OK;
98       }
99       else if (smtp_response[3] != '-')
100           return SM_ERROR;
101   }
102   return SM_UNRECOVERABLE;
103 }
104
105 int SMTP_from(int sock, const char *from, const char *opts)
106 /* send a "MAIL FROM:" message to the SMTP listener */
107 {
108     int ok;
109     char buf[MSGBUFSIZE];
110
111     if (strchr(from, '<'))
112 #ifdef HAVE_SNPRINTF
113         snprintf(buf, sizeof(buf),
114 #else
115         sprintf(buf,
116 #endif /* HAVE_SNPRINTF */
117                 "MAIL FROM: %s", from);
118     else
119 #ifdef HAVE_SNPRINTF
120     snprintf(buf, sizeof(buf),
121 #else
122     sprintf(buf,
123 #endif /* HAVE_SNPRINTF */
124             "MAIL FROM:<%s>", from);
125     if (opts)
126 #ifdef HAVE_SNPRINTF
127         snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
128 #else
129         strcat(buf, 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);
134     ok = SMTP_ok(sock);
135     return ok;
136 }
137
138 int SMTP_rcpt(int sock, const char *to)
139 /* send a "RCPT TO:" message to the SMTP listener */
140 {
141   int ok;
142
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);
146   ok = SMTP_ok(sock);
147   return ok;
148 }
149
150 int SMTP_data(int sock)
151 /* send a "DATA" message to the SMTP listener */
152 {
153   int ok;
154
155   SockPrintf(sock,"DATA\r\n");
156   if (outlevel >= O_MONITOR)
157       report(stdout, "%cMTP> DATA\n", smtp_mode);
158   ok = SMTP_ok(sock);
159   return ok;
160 }
161
162 int SMTP_rset(int sock)
163 /* send a "RSET" message to the SMTP listener */
164 {
165   int ok;
166
167   SockPrintf(sock,"RSET\r\n");
168   if (outlevel >= O_MONITOR)
169       report(stdout, "%cMTP> RSET\n", smtp_mode);
170   ok = SMTP_ok(sock);
171   return ok;
172 }
173
174 int SMTP_quit(int sock)
175 /* send a "QUIT" message to the SMTP listener */
176 {
177   int ok;
178
179   SockPrintf(sock,"QUIT\r\n");
180   if (outlevel >= O_MONITOR)
181       report(stdout, "%cMTP> QUIT\n", smtp_mode);
182   ok = SMTP_ok(sock);
183   return ok;
184 }
185
186 int SMTP_eom(int sock)
187 /* send a message data terminator to the SMTP listener */
188 {
189   int ok;
190
191   SockPrintf(sock,".\r\n");
192   if (outlevel >= O_MONITOR)
193       report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
194
195   /* 
196    * When doing LMTP, must process many of these at the outer level. 
197    */
198   if (smtp_mode == 'S')
199       ok = SMTP_ok(sock);
200   else
201       ok = SM_OK;
202
203   return ok;
204 }
205
206 int SMTP_ok(int sock)
207 /* returns status of SMTP connection */
208 {
209     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
210     {
211         int  n = strlen(smtp_response);
212
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';
217         if (n < 4)
218             return SM_ERROR;
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] == ' ')
223             return SM_OK;
224         else if (smtp_response[3] != '-')
225             return SM_ERROR;
226     }
227     return SM_UNRECOVERABLE;
228 }
229
230 static void SMTP_auth(int sock, char *buf)
231 /* ESMTP Authentication support for Fetchmail by Wojciech Polak */
232 {
233         int c;
234         char *p = 0;
235         char b64buf[512];
236         char tmp[512];
237
238         memset(b64buf, 0, sizeof(b64buf));
239         memset(tmp, 0, sizeof(tmp));
240
241         if (strstr(buf, "CRAM-MD5")) {
242                 unsigned char digest[16];
243                 static char ascii_digest[33];
244                 memset(digest, 0, 16);
245
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));
251
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");
257                         return;
258                 }
259
260                 p = strchr(tmp, ' ');
261                 p++;
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]);
269 #ifdef HAVE_SNPRINTF
270                 snprintf(tmp, sizeof(tmp),
271 #else
272                 sprintf(tmp,
273 #endif /* HAVE_SNPRINTF */
274                 "%s %s", esmtp_auth_username, ascii_digest);
275
276                 to64frombits(b64buf, tmp, strlen(tmp));
277                 SockPrintf(sock, "%s\r\n", b64buf);
278                 SMTP_ok(sock);
279         }
280         else if (strstr(buf, "PLAIN")) {
281                 int len;
282                 if (outlevel >= O_MONITOR)
283                         report(stdout, "ESMTP PLAIN Authentication...\n");
284 #ifdef HAVE_SNPRINTF
285                 snprintf(tmp, sizeof(tmp),
286 #else
287                 sprintf(tmp,
288 #endif /* HAVE_SNPRINTF */
289                 "^%s^%s", esmtp_auth_username, esmtp_auth_password);
290
291                 len = strlen(tmp);
292                 for (c = len - 1; c >= 0; c--)
293                 {
294                         if (tmp[c] == '^')
295                                 tmp[c] = '\0';
296                 }
297                 to64frombits(b64buf, tmp, len);
298                 SockPrintf(sock, "AUTH PLAIN %s\r\n", b64buf);
299                 SMTP_ok(sock);
300         }
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));
307
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");
313                         return;
314                 }
315
316                 p = strchr(tmp, ' ');
317                 p++;
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, ' ');
324                 p++;
325                 from64tobits(b64buf, p, sizeof(b64buf));
326                 to64frombits(b64buf, esmtp_auth_password, strlen(esmtp_auth_password));
327                 SockPrintf(sock, "%s\r\n", b64buf);
328                 SMTP_ok(sock);
329         }
330         return;
331 }
332
333 /* smtp.c ends here */