]> Pileus Git - ~andy/fetchmail/blob - odmr.c
merge Mirek's fetchmail-signed.patch
[~andy/fetchmail] / odmr.c
1 /*
2  * odmr.c -- ODMR protocol methods (see RFC 2645)
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include  "config.h"
8 #ifdef ODMR_ENABLE
9 #include  <stdio.h>
10 #include  <stdlib.h>
11 #include  <assert.h>
12 #ifdef HAVE_STRING_H /* strcat() */
13 #include <string.h>
14 #endif
15 #ifdef HAVE_NET_SOCKET_H /* BeOS needs this */
16 #include <net/socket.h>
17 #endif
18 #include  <sys/types.h>
19 #ifdef HAVE_NET_SELECT_H /* AIX needs this */
20 #include <net/select.h>
21 #endif
22 #ifdef HAVE_SYS_SELECT_H /* AIX 4.1, at least, needs this */
23 #include  <sys/select.h>
24 #endif
25 #include  <netdb.h>
26 #include  <errno.h>
27 #include  <unistd.h>
28 #include  "i18n.h"
29 #include  "fetchmail.h"
30 #include  "smtp.h"
31 #include  "socket.h"
32
33 static int odmr_ok (int sock, char *argbuf)
34 /* parse command response */
35 {
36     int ok;
37
38     ok = SMTP_ok(sock, SMTP_MODE);
39     if (ok == SM_UNRECOVERABLE)
40         return(PS_PROTOCOL);
41     else
42         return(ok);
43 }
44
45 static int odmr_getrange(int sock, struct query *ctl, const char *id, 
46                          int *countp, int *newp, int *bytes)
47 /* send ODMR and then run a reverse SMTP session */
48 {
49     int ok, opts, smtp_sock;
50     int doing_smtp_data = 0;   /* Are we in SMTP DATA state? */
51     char buf [MSGBUFSIZE+1];
52     struct idlist *qnp;         /* pointer to Q names */
53
54     if ((ok = SMTP_ehlo(sock, SMTP_MODE, fetchmailhost, 
55                         ctl->server.esmtp_name, ctl->server.esmtp_password,
56                         &opts)))
57     {
58         report(stderr, GT_("%s's SMTP listener does not support ESMTP\n"),
59               ctl->server.pollname);
60         return(ok);
61     }
62     else if (!(opts & ESMTP_ATRN))
63     {
64         report(stderr, GT_("%s's SMTP listener does not support ATRN\n"),
65               ctl->server.pollname);
66         return(PS_PROTOCOL);
67     }
68
69     /* make sure we don't enter the fetch loop */
70     *bytes = *countp = *newp = -1;
71
72     /* authenticate via CRAM-MD5 */
73     ok = do_cram_md5(sock, "AUTH", ctl, "334 ");
74     if (ok)
75         return(ok);
76
77     /*
78      * By default, the hostlist has a single entry, the fetchmail host's
79      * canonical DNS name.
80      */
81     buf[0] = '\0';
82     for (qnp = ctl->domainlist; qnp; qnp = qnp->next)
83         if (strlen(buf) + strlen(qnp->id) + 1 >= sizeof(buf))
84             break;
85         else
86         {
87             strcat(buf, qnp->id);
88             strcat(buf, ",");
89         }
90     buf[strlen(buf) - 1] = '\0';        /* nuke final comma */
91
92     /* ship the domain list and get turnaround */
93     gen_send(sock, "ATRN %s", buf);
94     if ((ok = gen_recv(sock, buf, sizeof(buf))))
95         return(ok);
96
97     /* this switch includes all response codes described in RFC2645 */
98     switch(atoi(buf))
99     {
100     case 250:   /* OK, turnaround is about to happe */
101         if (outlevel > O_SILENT)
102             report(stdout, GT_("Turnaround now...\n"));
103         break;
104
105     case 450:   /* ATRN request refused */
106         if (outlevel > O_SILENT)
107             report(stdout, GT_("ATRN request refused.\n"));
108         return(PS_PROTOCOL);
109
110     case 451:   /* Unable to process ATRN request now */
111         report(stderr, GT_("Unable to process ATRN request now\n"));
112         return(PS_EXCLUDE);
113
114     case 453:   /* You have no mail */
115         if (outlevel > O_SILENT)
116             report(stderr, GT_("You have no mail.\n"));
117         return(PS_NOMAIL);
118
119     case 502:   /* Command not implemented */
120         report(stderr, GT_("Command not implemented\n"));
121         return(PS_PROTOCOL);
122
123     case 530:   /* Authentication required */
124         report(stderr, GT_("Authentication required.\n"));
125         return(PS_AUTHFAIL);
126
127     default:
128         report(stderr, GT_("Unknown ODMR error %d\n"), atoi(buf));
129         return(PS_PROTOCOL);
130     }
131
132     /*
133      * OK, if we got here it's time to become a pipe between the ODMR
134      * remote server (sending) and the SMTP listener we've designated
135      * (receiving).  We're not going to try to be a protocol machine;
136      * instead, we'll use select(2) to watch the read sides of both
137      * sockets and just throw their data at each other.
138      */
139     if ((smtp_sock = smtp_open(ctl)) == -1)
140         return(PS_SOCKET);
141     else
142     {
143         int     maxfd = (sock > smtp_sock) ? sock : smtp_sock;
144
145         for (;;)
146         {
147             fd_set      readfds;
148             struct timeval timeout;
149             char        buf[MSGBUFSIZE];
150
151             FD_ZERO(&readfds);
152             FD_SET(sock, &readfds);
153             FD_SET(smtp_sock, &readfds);
154
155             timeout.tv_sec  = ctl->server.timeout;
156             timeout.tv_usec = 0;
157
158             if (select(maxfd+1, &readfds, NULL, NULL, &timeout) == -1)
159                 return(PS_PROTOCOL);            /* timeout */
160
161             if (FD_ISSET(sock, &readfds))
162             {
163                 int n = SockRead(sock, buf, sizeof(buf));
164                 if (n <= 0)
165                     break;
166
167                 SockWrite(smtp_sock, buf, n);
168                 if (outlevel >= O_MONITOR && !doing_smtp_data)
169                     report(stdout, "ODMR< %s", buf);
170             }
171             if (FD_ISSET(smtp_sock, &readfds))
172             {
173                 int n = SockRead(smtp_sock, buf, sizeof(buf));
174                 if (n <= 0)
175                     break;
176
177                 SockWrite(sock, buf, n);
178                 if (outlevel >= O_MONITOR)
179                     report(stdout, "ODMR> %s", buf);
180
181                /* We are about to receive message data if the local MTA
182                 * sends 354 (after receiving DATA) */
183                if (!doing_smtp_data && !strncmp(buf, "354", 3))
184                {
185                    doing_smtp_data = 1;
186                    if (outlevel > O_SILENT)
187                        report(stdout, GT_("receiving message data\n"));
188                }
189                else if (doing_smtp_data)
190                    doing_smtp_data = 0;
191             }
192         }
193         SockClose(smtp_sock);
194     }
195
196     return(0);
197 }
198
199 static int odmr_logout(int sock, struct query *ctl)
200 /* send logout command */
201 {
202     /* if we have a smtp_socket, then we've turned around and the
203        local smtp server is in control of the connection (so we don't
204        send QUIT) */
205     if (ctl->smtp_socket == -1)
206        return(gen_transact(sock, "QUIT"));
207     else
208        return(PS_SUCCESS);
209 }
210
211 static const struct method odmr =
212 {
213     "ODMR",             /* ODMR protocol */
214     "odmr",             /* standard ODMR port */
215     "odmrs",            /* ssl ODMR port */
216     FALSE,              /* this is not a tagged protocol */
217     FALSE,              /* this does not use a message delimiter */
218     odmr_ok,            /* parse command response */
219     NULL,               /* no need to get authentication */
220     odmr_getrange,      /* initialize message sending */
221     NULL,               /* we cannot get a list of sizes */
222     NULL,               /* we cannot get a list of sizes of subsets */
223     NULL,               /* how do we tell a message is old? */
224     NULL,               /* no way to fetch headers */
225     NULL,               /* no way to fetch body */
226     NULL,               /* no message trailer */
227     NULL,               /* how to delete a message */
228     NULL,               /* how to mark a message as seen */
229     NULL,               /* no mailbox support */
230     odmr_logout,        /* log out, we're done */
231     FALSE,              /* no, we can't re-poll */
232 };
233
234 int doODMR (struct query *ctl)
235 /* retrieve messages using ODMR */
236 {
237     int status;
238
239     if (ctl->keep) {
240         fprintf(stderr, GT_("Option --keep is not supported with ODMR\n"));
241         return(PS_SYNTAX);
242     }
243     if (ctl->flush) {
244         fprintf(stderr, GT_("Option --flush is not supported with ODMR\n"));
245         return(PS_SYNTAX);
246     }
247     if (ctl->mailboxes->id) {
248         fprintf(stderr, GT_("Option --folder is not supported with ODMR\n"));
249         return(PS_SYNTAX);
250     }
251     if (check_only) {
252         fprintf(stderr, GT_("Option --check is not supported with ODMR\n"));
253         return(PS_SYNTAX);
254     }
255     peek_capable = FALSE;
256
257     status = do_protocol(ctl, &odmr);
258     if (status == PS_NOMAIL)
259         status = PS_SUCCESS;
260     return(status);
261 }
262 #endif /* ODMR_ENABLE */
263
264 /* odmr.c ends here */