1
/*
2
 * etrn.c -- ETRN protocol methods (see RFC 1985)
3
 *
4
 * For license terms, see the file COPYING in this directory.
5
 */
6
7
#include  "config.h"
8
#ifdef ETRN_ENABLE
9
#include  <stdio.h>
10
#include  <stdlib.h>
11
#include  <assert.h>
12
#ifdef HAVE_NET_SOCKET_H /* BeOS needs this */
13
#include <net/socket.h>
14
#endif
15
#include  <netdb.h>
16
#include  <errno.h>
17
#include  <unistd.h>
18
#include  "i18n.h"
19
#include  "fetchmail.h"
20
#include  "smtp.h"
21
#include  "socket.h"
22
23
static int etrn_ok (int sock, char *argbuf)
24
/* parse command response */
25
{
26
    int ok;
27
28
    (void)argbuf;
29
    ok = SMTP_ok(sock, SMTP_MODE, TIMEOUT_DEFAULT);
30
    if (ok == SM_UNRECOVERABLE)
31
	return(PS_PROTOCOL);
32
    else
33
	return(ok);
34
}
35
36
static int etrn_getrange(int sock, struct query *ctl, const char *id, 
37
			 int *countp, int *newp, int *bytes)
38
/* send ETRN and interpret the response */
39
{
40
    int ok, opts;
41
    char buf [MSGBUFSIZE+1];
42
    struct idlist *qnp;		/* pointer to Q names */
43
44
    (void)id;
45
    if ((ok = SMTP_ehlo(sock, SMTP_MODE, fetchmailhost,
46
			ctl->server.esmtp_name, ctl->server.esmtp_password,
47
			&opts)))
48
    {
49
	report(stderr, GT_("%s's SMTP listener does not support ESMTP\n"),
50
	      ctl->server.pollname);
51
	return(ok);
52
    }
53
    else if (!(opts & ESMTP_ETRN))
54
    {
55
	report(stderr, GT_("%s's SMTP listener does not support ETRN\n"),
56
	      ctl->server.pollname);
57
	return(PS_PROTOCOL);
58
    }
59
60
    /* make sure we don't enter the fetch loop */
61
    *bytes = *countp = *newp = -1;
62
63
    /*
64
     * By default, the hostlist has a single entry, the fetchmail host's
65
     * canonical DNS name.
66
     */
67
    for (qnp = ctl->domainlist; qnp; qnp = qnp->next)
68
    {
69
	/* ship the actual poll and get the response */
70
	gen_send(sock, "ETRN %s", qnp->id);
71
	if ((ok = gen_recv(sock, buf, sizeof(buf))))
72
	    return(ok);
73
74
	/* this switch includes all response codes described in RFC1985 */
75
	switch(atoi(buf))
76
	{
77
	case 250:	/* OK, queuing for node <x> started */
78
	    if (outlevel > O_SILENT)
79
		report(stdout, GT_("Queuing for %s started\n"), qnp->id);
80
	    break;
81
82
	case 251:	/* OK, no messages waiting for node <x> */
83
	    if (outlevel > O_SILENT)
84
		report(stdout, GT_("No messages waiting for %s\n"), qnp->id);
85
	    return(PS_NOMAIL);
86
87
	case 252:	/* OK, pending messages for node <x> started */
88
	case 253:	/* OK, <n> pending messages for node <x> started */
89
	    if (outlevel > O_SILENT)
90
		report(stdout, GT_("Pending messages for %s started\n"), qnp->id);
91
	    break;
92
93
	case 458:	/* Unable to queue messages for node <x> */
94
	    report(stderr, GT_("Unable to queue messages for node %s\n"),qnp->id);
95
	    return(PS_PROTOCOL);
96
97
	case 459:	/* Node <x> not allowed: <reason> */
98
	    report(stderr, GT_("Node %s not allowed: %s\n"), qnp->id, buf);
99
	    return(PS_AUTHFAIL);
100
101
	case 500:	/* Syntax Error */
102
	    report(stderr, GT_("ETRN syntax error\n"));
103
	    return(PS_PROTOCOL);
104
105
	case 501:	/* Syntax Error in Parameters */
106
	    report(stderr, GT_("ETRN syntax error in parameters\n"));
107
	    return(PS_PROTOCOL);
108
109
	default:
110
	    report(stderr, GT_("Unknown ETRN error %d\n"), atoi(buf));
111
	    return(PS_PROTOCOL);
112
	}
113
    }
114
115
    return(0);
116
}
117
118
static int etrn_logout(int sock, struct query *ctl)
119
/* send logout command */
120
{
121
    (void)ctl;
122
    return(gen_transact(sock, "QUIT"));
123
}
124
125
static const struct method etrn =
126
{
127
    "ETRN",		/* ESMTP ETRN extension */
128
    "smtp",		/* standard SMTP port */
129
    "smtps",		/* ssl SMTP port */
130
    FALSE,		/* this is not a tagged protocol */
131
    FALSE,		/* this does not use a message delimiter */
132
    etrn_ok,		/* parse command response */
133
    NULL,		/* no need to get authentication */
134
    etrn_getrange,	/* initialize message sending */
135
    NULL,		/* we cannot get a list of sizes */
136
    NULL,		/* we cannot get a list of sizes of subsets */
137
    NULL,		/* how do we tell a message is old? */
138
    NULL,		/* no way to fetch headers */
139
    NULL,		/* no way to fetch body */
140
    NULL,		/* no message trailer */
141
    NULL,		/* how to delete a message */
142
    NULL,		/* how to mark a message as seen */
143
    NULL,		/* no mailbox support */
144
    etrn_logout,	/* log out, we're done */
145
    FALSE,		/* no, we can't re-poll */
146
};
147
148
int doETRN (struct query *ctl)
149
/* retrieve messages using ETRN */
150
{
151
    int status;
152
153
    if (ctl->keep) {
154
	fprintf(stderr, GT_("Option --keep is not supported with ETRN\n"));
155
	return(PS_SYNTAX);
156
    }
157
    if (ctl->flush) {
158
	fprintf(stderr, GT_("Option --flush is not supported with ETRN\n"));
159
	return(PS_SYNTAX);
160
    }
161
    if (ctl->mailboxes->id) {
162
	fprintf(stderr, GT_("Option --folder is not supported with ETRN\n"));
163
	return(PS_SYNTAX);
164
    }
165
    if (check_only) {
166
	fprintf(stderr, GT_("Option --check is not supported with ETRN\n"));
167
	return(PS_SYNTAX);
168
    }
169
    peek_capable = FALSE;
170
171
    status = do_protocol(ctl, &etrn);
172
    if (status == PS_NOMAIL)
173
	status = PS_SUCCESS;
174
    return(status);
175
}
176
#endif /* ETRN_ENABLE */
177
178
/* etrn.c ends here */