]> Pileus Git - ~andy/fetchmail/blob - odmr.c
Implemented fetchdomains.
[~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\n", 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\n", 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     return(gen_transact(sock, "QUIT"));
186 }
187
188 const static struct method odmr =
189 {
190     "ODMR",             /* ODMR protocol */
191 #if INET6_ENABLE
192     "odmr",             /* standard SMTP port */
193     "odmrs",            /* ssl SMTP port */
194 #else /* INET6_ENABLE */
195     366,                /* standard SMTP port */
196     2366,               /* ssl SMTP port (BOGUS! RANDOM VALUE) */
197 #endif /* INET6_ENABLE */
198     FALSE,              /* this is not a tagged protocol */
199     FALSE,              /* this does not use a message delimiter */
200     odmr_ok,            /* parse command response */
201     NULL,               /* no need to get authentication */
202     odmr_getrange,      /* initialize message sending */
203     NULL,               /* we cannot get a list of sizes */
204     NULL,               /* how do we tell a message is old? */
205     NULL,               /* no way to fetch headers */
206     NULL,               /* no way to fetch body */
207     NULL,               /* no message trailer */
208     NULL,               /* how to delete a message */
209     odmr_logout,        /* log out, we're done */
210     FALSE,              /* no, we can't re-poll */
211 };
212
213 int doODMR (struct query *ctl)
214 /* retrieve messages using ODMR */
215 {
216     int status;
217
218     if (ctl->keep) {
219         fprintf(stderr, _("Option --keep is not supported with ODMR\n"));
220         return(PS_SYNTAX);
221     }
222     if (ctl->flush) {
223         fprintf(stderr, _("Option --flush is not supported with ODMR\n"));
224         return(PS_SYNTAX);
225     }
226     if (ctl->mailboxes->id) {
227         fprintf(stderr, _("Option --remote is not supported with ODMR\n"));
228         return(PS_SYNTAX);
229     }
230     if (check_only) {
231         fprintf(stderr, _("Option --check is not supported with ODMR\n"));
232         return(PS_SYNTAX);
233     }
234     peek_capable = FALSE;
235
236     status = do_protocol(ctl, &odmr);
237     if (status == PS_NOMAIL)
238         status = PS_SUCCESS;
239     return(status);
240 }
241 #endif /* ODMR_ENABLE */
242
243 /* odmr.c ends here */