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