]> Pileus Git - ~andy/fetchmail/blob - smtp.c
8642feb0f8f3e19d520f085bd4353d9f4c5fd222
[~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 "config.h"
12 #include "fetchmail.h"
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <signal.h>
18 #include "socket.h"
19 #include "smtp.h"
20 #include "i18n.h"
21
22 struct opt
23 {
24     const char *name;
25     int value;
26 };
27
28 static struct opt extensions[] =
29 {
30     {"8BITMIME",        ESMTP_8BITMIME},
31     {"SIZE",            ESMTP_SIZE},
32     {"ETRN",            ESMTP_ETRN},
33     {"AUTH ",           ESMTP_AUTH},
34 #ifdef ODMR_ENABLE
35     {"ATRN",            ESMTP_ATRN},
36 #endif /* ODMR_ENABLE */
37     {(char *)NULL, 0},
38 };
39
40 char smtp_response[MSGBUFSIZE];
41
42 static char smtp_mode = 'S';
43
44 void SMTP_setmode(char sl)
45 /* set whether we are speaking SMTP or LMTP */
46 {
47     smtp_mode = sl;
48 }
49
50 int SMTP_helo(int sock,const char *host)
51 /* send a "HELO" message to the SMTP listener */
52 {
53   int ok;
54
55   SockPrintf(sock,"HELO %s\r\n", host);
56   if (outlevel >= O_MONITOR)
57       report(stdout, "SMTP> HELO %s\n", host);
58   ok = SMTP_ok(sock);
59   return ok;
60 }
61
62 static void SMTP_auth_error(int sock, char *msg)
63 {
64     SockPrintf(sock, "*\r\n");
65     SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
66     if (outlevel >= O_MONITOR) report(stdout, msg);
67 }
68
69 static void SMTP_auth(int sock, char *username, char *password, char *buf)
70 /* ESMTP Authentication support for fetchmail by Wojciech Polak */
71 {       
72         int c;
73         char *p = 0;
74         char b64buf[512];
75         char tmp[512];
76
77         if (!username || !password) return;
78
79         memset(b64buf, 0, sizeof(b64buf));
80         memset(tmp, 0, sizeof(tmp));
81
82         if (strstr(buf, "CRAM-MD5")) {
83                 unsigned char digest[16];
84                 memset(digest, 0, sizeof(digest));
85
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';
92
93                 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
94                         SMTP_auth_error(sock, GT_("Server rejected the AUTH command.\n"));
95                         return;
96                 }
97
98                 p = strchr(tmp, ' ');
99                 p++;
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"));
103                         return;
104                 }
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]);
115
116                 to64frombits(b64buf, tmp, strlen(tmp));
117                 SockPrintf(sock, "%s\r\n", b64buf);
118                 SMTP_ok(sock);
119         }
120         else if (strstr(buf, "PLAIN")) {
121                 int len;
122                 if (outlevel >= O_MONITOR)
123                         report(stdout, GT_("ESMTP PLAIN Authentication...\n"));
124                 snprintf(tmp, sizeof(tmp), "^%s^%s", username, password);
125
126                 len = strlen(tmp);
127                 for (c = len - 1; c >= 0; c--)
128                 {
129                         if (tmp[c] == '^')
130                                 tmp[c] = '\0';
131                 }
132                 to64frombits(b64buf, tmp, len);
133                 SockPrintf(sock, "AUTH PLAIN %s\r\n", b64buf);
134                 SMTP_ok(sock);
135         }
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';
143
144                 if (strncmp(tmp, "334 ", 4)) { /* Server rejects AUTH */
145                         SMTP_auth_error(sock, GT_("Server rejected the AUTH command.\n"));
146                         return;
147                 }
148
149                 p = strchr(tmp, ' ');
150                 p++;
151                 if (from64tobits(b64buf, p, sizeof(b64buf) - 1) <= 0) {
152                         SMTP_auth_error(sock, GT_("Bad base64 reply from server.\n"));
153                         return;
154                 }
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, ' ');
161                 if (!p) {
162                         SMTP_auth_error(sock, GT_("Bad base64 reply from server.\n"));
163                         return;
164                 }
165                 p++;
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"));
169                         return;
170                 }
171                 to64frombits(b64buf, password, strlen(password));
172                 SockPrintf(sock, "%s\r\n", b64buf);
173                 SMTP_ok(sock);
174         }
175         return;
176 }
177
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 */
180 {
181   struct opt *hp;
182   char auth_response[511];
183
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);
188   
189   *opt = 0;
190   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
191   {
192       int  n = strlen(smtp_response);
193
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';
198       if (n < 4)
199           return SM_ERROR;
200       smtp_response[n] = '\0';
201       if (outlevel >= O_MONITOR)
202           report(stdout, "SMTP< %s\n", smtp_response);
203       for (hp = extensions; hp->name; hp++)
204           if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name))) {
205               *opt |= hp->value;
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';
209           }
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);
213           return SM_OK;
214       }
215       else if (smtp_response[3] != '-')
216           return SM_ERROR;
217   }
218   return SM_UNRECOVERABLE;
219 }
220
221 int SMTP_from(int sock, const char *from, const char *opts)
222 /* send a "MAIL FROM:" message to the SMTP listener */
223 {
224     int ok;
225     char buf[MSGBUFSIZE];
226
227     if (from[0]=='<')
228         snprintf(buf, sizeof(buf), "MAIL FROM:%s", from);
229     else
230         snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", from);
231     if (opts)
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);
236     ok = SMTP_ok(sock);
237     return ok;
238 }
239
240 int SMTP_rcpt(int sock, const char *to)
241 /* send a "RCPT TO:" message to the SMTP listener */
242 {
243   int ok;
244
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);
248   ok = SMTP_ok(sock);
249   return ok;
250 }
251
252 int SMTP_data(int sock)
253 /* send a "DATA" message to the SMTP listener */
254 {
255   int ok;
256
257   SockPrintf(sock,"DATA\r\n");
258   if (outlevel >= O_MONITOR)
259       report(stdout, "%cMTP> DATA\n", smtp_mode);
260   ok = SMTP_ok(sock);
261   return ok;
262 }
263
264 int SMTP_rset(int sock)
265 /* send a "RSET" message to the SMTP listener */
266 {
267   int ok;
268
269   SockPrintf(sock,"RSET\r\n");
270   if (outlevel >= O_MONITOR)
271       report(stdout, "%cMTP> RSET\n", smtp_mode);
272   ok = SMTP_ok(sock);
273   return ok;
274 }
275
276 int SMTP_quit(int sock)
277 /* send a "QUIT" message to the SMTP listener */
278 {
279   int ok;
280
281   SockPrintf(sock,"QUIT\r\n");
282   if (outlevel >= O_MONITOR)
283       report(stdout, "%cMTP> QUIT\n", smtp_mode);
284   ok = SMTP_ok(sock);
285   return ok;
286 }
287
288 int SMTP_eom(int sock)
289 /* send a message data terminator to the SMTP listener */
290 {
291   int ok;
292
293   SockPrintf(sock,".\r\n");
294   if (outlevel >= O_MONITOR)
295       report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
296
297   /* 
298    * When doing LMTP, must process many of these at the outer level. 
299    */
300   if (smtp_mode == 'S')
301       ok = SMTP_ok(sock);
302   else
303       ok = SM_OK;
304
305   return ok;
306 }
307
308 time_t last_smtp_ok = 0;
309
310 int SMTP_ok(int sock)
311 /* returns status of SMTP connection */
312 {
313     SIGHANDLERTYPE alrmsave;
314
315     /* set an alarm for smtp ok */
316     alrmsave = set_signal_handler(SIGALRM, null_signal_handler);
317     set_timeout(mytimeout);
318
319     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
320     {
321         int n;
322
323         /* restore alarm */
324         set_timeout(0);
325         set_signal_handler(SIGALRM, alrmsave);
326
327         n = strlen(smtp_response);
328         if (n > 0 && smtp_response[n-1] == '\n')
329             n--;
330         if (n > 0 && smtp_response[n-1] == '\r')
331             n--;
332         smtp_response[n] = '\0';
333         if (outlevel >= O_MONITOR)
334             report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
335         if (n < 4 ||
336             (smtp_response[3] != ' ' && smtp_response[3] != '-'))
337         {
338             if (outlevel >= O_MONITOR)
339                 report(stderr, GT_("smtp listener protocol error\n"));
340             return SM_UNRECOVERABLE;
341         }
342
343         last_smtp_ok = time((time_t *) NULL);
344
345         if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') &&
346             smtp_response[3] == ' ')
347             return SM_OK;
348         else if (smtp_response[3] != '-')
349             return SM_ERROR;
350
351         /* set an alarm for smtp ok */
352         set_signal_handler(SIGALRM, null_signal_handler);
353         set_timeout(mytimeout);
354
355     }
356
357     /* restore alarm */
358     set_timeout(0);
359     set_signal_handler(SIGALRM, alrmsave);
360
361     if (outlevel >= O_MONITOR)
362         report(stderr, GT_("smtp listener protocol error\n"));
363     return SM_UNRECOVERABLE;
364 }
365
366 /* smtp.c ends here */