Deleted Added
full compact
push.c (90926) push.c (120945)
1/*
1/*
2 * Copyright (c) 1997-2001 Kungliga Tekniska H�gskolan
2 * Copyright (c) 1997-2001, 2003 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "push_locl.h"
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "push_locl.h"
35RCSID("$Id: push.c,v 1.45 2001/09/04 09:45:52 assar Exp $");
35RCSID("$Id: push.c,v 1.47 2003/04/04 02:10:17 assar Exp $");
36
37#ifdef KRB4
38static int use_v4 = -1;
39#endif
40
41#ifdef KRB5
42static int use_v5 = -1;
43static krb5_context context;
44#endif
45
46static char *port_str;
47static int verbose_level;
48static int do_fork;
49static int do_leave;
50static int do_version;
51static int do_help;
52static int do_from;
53static int do_count;
54static char *header_str;
55
56struct getargs args[] = {
57#ifdef KRB4
58 { "krb4", '4', arg_flag, &use_v4, "Use Kerberos V4",
59 NULL },
60#endif
61#ifdef KRB5
62 { "krb5", '5', arg_flag, &use_v5, "Use Kerberos V5",
63 NULL },
64#endif
65 { "verbose",'v', arg_counter, &verbose_level, "Verbose",
66 NULL },
67 { "fork", 'f', arg_flag, &do_fork, "Fork deleting proc",
68 NULL },
69 { "leave", 'l', arg_flag, &do_leave, "Leave mail on server",
70 NULL },
71 { "port", 'p', arg_string, &port_str, "Use this port",
72 "number-or-service" },
73 { "from", 0, arg_flag, &do_from, "Behave like from",
74 NULL },
75 { "headers", 0, arg_string, &header_str, "Headers to print", NULL },
76 { "count", 'c', arg_flag, &do_count, "Print number of messages", NULL},
77 { "version", 0, arg_flag, &do_version, "Print version",
78 NULL },
79 { "help", 0, arg_flag, &do_help, NULL,
80 NULL }
81
82};
83
84static void
85usage (int ret)
86{
87 arg_printusage (args,
88 sizeof(args) / sizeof(args[0]),
89 NULL,
90 "[[{po:username[@hostname] | hostname[:username]}] ...] "
91 "filename");
92 exit (ret);
93}
94
95static int
96do_connect (const char *hostname, int port, int nodelay)
97{
98 struct addrinfo *ai, *a;
99 struct addrinfo hints;
100 int error;
101 int s = -1;
102 char portstr[NI_MAXSERV];
103
104 memset (&hints, 0, sizeof(hints));
105 hints.ai_socktype = SOCK_STREAM;
106 hints.ai_protocol = IPPROTO_TCP;
107
108 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
109
110 error = getaddrinfo (hostname, portstr, &hints, &ai);
111 if (error)
112 errx (1, "getaddrinfo(%s): %s", hostname, gai_strerror(error));
113
114 for (a = ai; a != NULL; a = a->ai_next) {
115 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
116 if (s < 0)
117 continue;
118 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
119 warn ("connect(%s)", hostname);
120 close (s);
121 continue;
122 }
123 break;
124 }
125 freeaddrinfo (ai);
126 if (a == NULL) {
127 warnx ("failed to contact %s", hostname);
128 return -1;
129 }
130
131 if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
132 (void *)&nodelay, sizeof(nodelay)) < 0)
133 err (1, "setsockopt TCP_NODELAY");
134 return s;
135}
136
137typedef enum { INIT = 0, GREET, USER, PASS, STAT, RETR, TOP,
138 DELE, XDELE, QUIT} pop_state;
139
36
37#ifdef KRB4
38static int use_v4 = -1;
39#endif
40
41#ifdef KRB5
42static int use_v5 = -1;
43static krb5_context context;
44#endif
45
46static char *port_str;
47static int verbose_level;
48static int do_fork;
49static int do_leave;
50static int do_version;
51static int do_help;
52static int do_from;
53static int do_count;
54static char *header_str;
55
56struct getargs args[] = {
57#ifdef KRB4
58 { "krb4", '4', arg_flag, &use_v4, "Use Kerberos V4",
59 NULL },
60#endif
61#ifdef KRB5
62 { "krb5", '5', arg_flag, &use_v5, "Use Kerberos V5",
63 NULL },
64#endif
65 { "verbose",'v', arg_counter, &verbose_level, "Verbose",
66 NULL },
67 { "fork", 'f', arg_flag, &do_fork, "Fork deleting proc",
68 NULL },
69 { "leave", 'l', arg_flag, &do_leave, "Leave mail on server",
70 NULL },
71 { "port", 'p', arg_string, &port_str, "Use this port",
72 "number-or-service" },
73 { "from", 0, arg_flag, &do_from, "Behave like from",
74 NULL },
75 { "headers", 0, arg_string, &header_str, "Headers to print", NULL },
76 { "count", 'c', arg_flag, &do_count, "Print number of messages", NULL},
77 { "version", 0, arg_flag, &do_version, "Print version",
78 NULL },
79 { "help", 0, arg_flag, &do_help, NULL,
80 NULL }
81
82};
83
84static void
85usage (int ret)
86{
87 arg_printusage (args,
88 sizeof(args) / sizeof(args[0]),
89 NULL,
90 "[[{po:username[@hostname] | hostname[:username]}] ...] "
91 "filename");
92 exit (ret);
93}
94
95static int
96do_connect (const char *hostname, int port, int nodelay)
97{
98 struct addrinfo *ai, *a;
99 struct addrinfo hints;
100 int error;
101 int s = -1;
102 char portstr[NI_MAXSERV];
103
104 memset (&hints, 0, sizeof(hints));
105 hints.ai_socktype = SOCK_STREAM;
106 hints.ai_protocol = IPPROTO_TCP;
107
108 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
109
110 error = getaddrinfo (hostname, portstr, &hints, &ai);
111 if (error)
112 errx (1, "getaddrinfo(%s): %s", hostname, gai_strerror(error));
113
114 for (a = ai; a != NULL; a = a->ai_next) {
115 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
116 if (s < 0)
117 continue;
118 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
119 warn ("connect(%s)", hostname);
120 close (s);
121 continue;
122 }
123 break;
124 }
125 freeaddrinfo (ai);
126 if (a == NULL) {
127 warnx ("failed to contact %s", hostname);
128 return -1;
129 }
130
131 if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
132 (void *)&nodelay, sizeof(nodelay)) < 0)
133 err (1, "setsockopt TCP_NODELAY");
134 return s;
135}
136
137typedef enum { INIT = 0, GREET, USER, PASS, STAT, RETR, TOP,
138 DELE, XDELE, QUIT} pop_state;
139
140static char *pop_state_string[] = {
141 "INIT", "GREET", "USER", "PASS", "STAT", "RETR", "TOP",
142 "DELE", "XDELE", "QUIT"
143};
144
140#define PUSH_BUFSIZ 65536
141
142#define STEP 16
143
144struct write_state {
145 struct iovec *iovecs;
146 size_t niovecs, maxiovecs, allociovecs;
147 int fd;
148};
149
150static void
151write_state_init (struct write_state *w, int fd)
152{
153#ifdef UIO_MAXIOV
154 w->maxiovecs = UIO_MAXIOV;
155#else
156 w->maxiovecs = 16;
157#endif
158 w->allociovecs = min(STEP, w->maxiovecs);
159 w->niovecs = 0;
160 w->iovecs = emalloc(w->allociovecs * sizeof(*w->iovecs));
161 w->fd = fd;
162}
163
164static void
165write_state_add (struct write_state *w, void *v, size_t len)
166{
167 if(w->niovecs == w->allociovecs) {
168 if(w->niovecs == w->maxiovecs) {
169 if(writev (w->fd, w->iovecs, w->niovecs) < 0)
170 err(1, "writev");
171 w->niovecs = 0;
172 } else {
173 w->allociovecs = min(w->allociovecs + STEP, w->maxiovecs);
174 w->iovecs = erealloc (w->iovecs,
175 w->allociovecs * sizeof(*w->iovecs));
176 }
177 }
178 w->iovecs[w->niovecs].iov_base = v;
179 w->iovecs[w->niovecs].iov_len = len;
180 ++w->niovecs;
181}
182
183static void
184write_state_flush (struct write_state *w)
185{
186 if (w->niovecs) {
187 if (writev (w->fd, w->iovecs, w->niovecs) < 0)
188 err (1, "writev");
189 w->niovecs = 0;
190 }
191}
192
193static void
194write_state_destroy (struct write_state *w)
195{
196 free (w->iovecs);
197}
198
199static int
200doit(int s,
201 const char *host,
202 const char *user,
203 const char *outfilename,
204 const char *header_str,
205 int leavep,
206 int verbose,
207 int forkp)
208{
209 int ret;
210 char out_buf[PUSH_BUFSIZ];
211 int out_len = 0;
212 char in_buf[PUSH_BUFSIZ + 1]; /* sentinel */
213 size_t in_len = 0;
214 char *in_ptr = in_buf;
215 pop_state state = INIT;
216 unsigned count, bytes;
217 unsigned asked_for = 0, retrieved = 0, asked_deleted = 0, deleted = 0;
218 unsigned sent_xdele = 0;
219 int out_fd;
220 char from_line[128];
221 size_t from_line_length;
222 time_t now;
223 struct write_state write_state;
224 int numheaders = 1;
225 char **headers = NULL;
226 int i;
227 char *tmp = NULL;
228
229 if (do_from) {
230 char *tmp2;
231
232 tmp2 = tmp = estrdup(header_str);
233
234 out_fd = -1;
235 if (verbose)
236 fprintf (stderr, "%s@%s\n", user, host);
237 while (*tmp != '\0') {
238 tmp = strchr(tmp, ',');
239 if (tmp == NULL)
240 break;
241 tmp++;
242 numheaders++;
243 }
244
245 headers = emalloc(sizeof(char *) * (numheaders + 1));
246 for (i = 0; i < numheaders; i++) {
247 headers[i] = strtok_r(tmp2, ",", &tmp2);
248 }
249 headers[numheaders] = NULL;
250 } else {
251 out_fd = open(outfilename, O_WRONLY | O_APPEND | O_CREAT, 0666);
252 if (out_fd < 0)
253 err (1, "open %s", outfilename);
254 if (verbose)
255 fprintf (stderr, "%s@%s -> %s\n", user, host, outfilename);
256 }
257
258 now = time(NULL);
259 from_line_length = snprintf (from_line, sizeof(from_line),
260 "From %s %s", "push", ctime(&now));
261
262 out_len = snprintf (out_buf, sizeof(out_buf),
263 "USER %s\r\nPASS hej\r\nSTAT\r\n",
264 user);
265 if (out_len < 0)
266 errx (1, "snprintf failed");
267 if (net_write (s, out_buf, out_len) != out_len)
268 err (1, "write");
269 if (verbose > 1)
145#define PUSH_BUFSIZ 65536
146
147#define STEP 16
148
149struct write_state {
150 struct iovec *iovecs;
151 size_t niovecs, maxiovecs, allociovecs;
152 int fd;
153};
154
155static void
156write_state_init (struct write_state *w, int fd)
157{
158#ifdef UIO_MAXIOV
159 w->maxiovecs = UIO_MAXIOV;
160#else
161 w->maxiovecs = 16;
162#endif
163 w->allociovecs = min(STEP, w->maxiovecs);
164 w->niovecs = 0;
165 w->iovecs = emalloc(w->allociovecs * sizeof(*w->iovecs));
166 w->fd = fd;
167}
168
169static void
170write_state_add (struct write_state *w, void *v, size_t len)
171{
172 if(w->niovecs == w->allociovecs) {
173 if(w->niovecs == w->maxiovecs) {
174 if(writev (w->fd, w->iovecs, w->niovecs) < 0)
175 err(1, "writev");
176 w->niovecs = 0;
177 } else {
178 w->allociovecs = min(w->allociovecs + STEP, w->maxiovecs);
179 w->iovecs = erealloc (w->iovecs,
180 w->allociovecs * sizeof(*w->iovecs));
181 }
182 }
183 w->iovecs[w->niovecs].iov_base = v;
184 w->iovecs[w->niovecs].iov_len = len;
185 ++w->niovecs;
186}
187
188static void
189write_state_flush (struct write_state *w)
190{
191 if (w->niovecs) {
192 if (writev (w->fd, w->iovecs, w->niovecs) < 0)
193 err (1, "writev");
194 w->niovecs = 0;
195 }
196}
197
198static void
199write_state_destroy (struct write_state *w)
200{
201 free (w->iovecs);
202}
203
204static int
205doit(int s,
206 const char *host,
207 const char *user,
208 const char *outfilename,
209 const char *header_str,
210 int leavep,
211 int verbose,
212 int forkp)
213{
214 int ret;
215 char out_buf[PUSH_BUFSIZ];
216 int out_len = 0;
217 char in_buf[PUSH_BUFSIZ + 1]; /* sentinel */
218 size_t in_len = 0;
219 char *in_ptr = in_buf;
220 pop_state state = INIT;
221 unsigned count, bytes;
222 unsigned asked_for = 0, retrieved = 0, asked_deleted = 0, deleted = 0;
223 unsigned sent_xdele = 0;
224 int out_fd;
225 char from_line[128];
226 size_t from_line_length;
227 time_t now;
228 struct write_state write_state;
229 int numheaders = 1;
230 char **headers = NULL;
231 int i;
232 char *tmp = NULL;
233
234 if (do_from) {
235 char *tmp2;
236
237 tmp2 = tmp = estrdup(header_str);
238
239 out_fd = -1;
240 if (verbose)
241 fprintf (stderr, "%s@%s\n", user, host);
242 while (*tmp != '\0') {
243 tmp = strchr(tmp, ',');
244 if (tmp == NULL)
245 break;
246 tmp++;
247 numheaders++;
248 }
249
250 headers = emalloc(sizeof(char *) * (numheaders + 1));
251 for (i = 0; i < numheaders; i++) {
252 headers[i] = strtok_r(tmp2, ",", &tmp2);
253 }
254 headers[numheaders] = NULL;
255 } else {
256 out_fd = open(outfilename, O_WRONLY | O_APPEND | O_CREAT, 0666);
257 if (out_fd < 0)
258 err (1, "open %s", outfilename);
259 if (verbose)
260 fprintf (stderr, "%s@%s -> %s\n", user, host, outfilename);
261 }
262
263 now = time(NULL);
264 from_line_length = snprintf (from_line, sizeof(from_line),
265 "From %s %s", "push", ctime(&now));
266
267 out_len = snprintf (out_buf, sizeof(out_buf),
268 "USER %s\r\nPASS hej\r\nSTAT\r\n",
269 user);
270 if (out_len < 0)
271 errx (1, "snprintf failed");
272 if (net_write (s, out_buf, out_len) != out_len)
273 err (1, "write");
274 if (verbose > 1)
270 write (STDERR_FILENO, out_buf, out_len);
275 fprintf (stderr, "%s", out_buf);
271
272 if (!do_from)
273 write_state_init (&write_state, out_fd);
274
275 while(state != QUIT) {
276 fd_set readset, writeset;
277
278 FD_ZERO(&readset);
279 FD_ZERO(&writeset);
280 if (s >= FD_SETSIZE)
281 errx (1, "fd too large");
282 FD_SET(s,&readset);
276
277 if (!do_from)
278 write_state_init (&write_state, out_fd);
279
280 while(state != QUIT) {
281 fd_set readset, writeset;
282
283 FD_ZERO(&readset);
284 FD_ZERO(&writeset);
285 if (s >= FD_SETSIZE)
286 errx (1, "fd too large");
287 FD_SET(s,&readset);
288
289 if (verbose > 1)
290 fprintf (stderr, "state: %s count: %d asked_for: %d "
291 "retrieved: %d asked_deleted: %d\n",
292 pop_state_string[state],
293 count, asked_for, retrieved, asked_deleted);
294
283 if (((state == STAT || state == RETR || state == TOP)
284 && asked_for < count)
285 || (state == XDELE && !sent_xdele)
286 || (state == DELE && asked_deleted < count))
287 FD_SET(s,&writeset);
288 ret = select (s + 1, &readset, &writeset, NULL, NULL);
289 if (ret < 0) {
290 if (errno == EAGAIN)
291 continue;
292 else
293 err (1, "select");
294 }
295
296 if (FD_ISSET(s, &readset)) {
297 char *beg, *p;
298 size_t rem;
299 int blank_line = 0;
300
301 ret = read (s, in_ptr, sizeof(in_buf) - in_len - 1);
302 if (ret < 0)
303 err (1, "read");
304 else if (ret == 0)
305 errx (1, "EOF during read");
306
307 in_len += ret;
308 in_ptr += ret;
309 *in_ptr = '\0';
310
311 beg = in_buf;
312 rem = in_len;
313 while(rem > 1
314 && (p = strstr(beg, "\r\n")) != NULL) {
315 if (state == TOP) {
316 char *copy = beg;
317
318 for (i = 0; i < numheaders; i++) {
319 size_t len;
320
321 len = min(p - copy + 1, strlen(headers[i]));
322 if (strncasecmp(copy, headers[i], len) == 0) {
323 fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
324 }
325 }
326 if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
327 if (numheaders > 1)
328 fprintf (stdout, "\n");
329 state = STAT;
330 if (++retrieved == count) {
331 state = QUIT;
332 net_write (s, "QUIT\r\n", 6);
333 if (verbose > 1)
295 if (((state == STAT || state == RETR || state == TOP)
296 && asked_for < count)
297 || (state == XDELE && !sent_xdele)
298 || (state == DELE && asked_deleted < count))
299 FD_SET(s,&writeset);
300 ret = select (s + 1, &readset, &writeset, NULL, NULL);
301 if (ret < 0) {
302 if (errno == EAGAIN)
303 continue;
304 else
305 err (1, "select");
306 }
307
308 if (FD_ISSET(s, &readset)) {
309 char *beg, *p;
310 size_t rem;
311 int blank_line = 0;
312
313 ret = read (s, in_ptr, sizeof(in_buf) - in_len - 1);
314 if (ret < 0)
315 err (1, "read");
316 else if (ret == 0)
317 errx (1, "EOF during read");
318
319 in_len += ret;
320 in_ptr += ret;
321 *in_ptr = '\0';
322
323 beg = in_buf;
324 rem = in_len;
325 while(rem > 1
326 && (p = strstr(beg, "\r\n")) != NULL) {
327 if (state == TOP) {
328 char *copy = beg;
329
330 for (i = 0; i < numheaders; i++) {
331 size_t len;
332
333 len = min(p - copy + 1, strlen(headers[i]));
334 if (strncasecmp(copy, headers[i], len) == 0) {
335 fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
336 }
337 }
338 if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
339 if (numheaders > 1)
340 fprintf (stdout, "\n");
341 state = STAT;
342 if (++retrieved == count) {
343 state = QUIT;
344 net_write (s, "QUIT\r\n", 6);
345 if (verbose > 1)
334 net_write (STDERR_FILENO, "QUIT\r\n", 6);
346 fprintf (stderr, "QUIT\r\n");
335 }
336 }
337 rem -= p - beg + 2;
338 beg = p + 2;
339 } else if (state == RETR) {
340 char *copy = beg;
341 if (beg[0] == '.') {
342 if (beg[1] == '\r' && beg[2] == '\n') {
343 if(!blank_line)
344 write_state_add(&write_state, "\n", 1);
345 state = STAT;
346 rem -= p - beg + 2;
347 beg = p + 2;
348 if (++retrieved == count) {
349 write_state_flush (&write_state);
350 if (fsync (out_fd) < 0)
351 err (1, "fsync");
352 close(out_fd);
353 if (leavep) {
354 state = QUIT;
355 net_write (s, "QUIT\r\n", 6);
356 if (verbose > 1)
347 }
348 }
349 rem -= p - beg + 2;
350 beg = p + 2;
351 } else if (state == RETR) {
352 char *copy = beg;
353 if (beg[0] == '.') {
354 if (beg[1] == '\r' && beg[2] == '\n') {
355 if(!blank_line)
356 write_state_add(&write_state, "\n", 1);
357 state = STAT;
358 rem -= p - beg + 2;
359 beg = p + 2;
360 if (++retrieved == count) {
361 write_state_flush (&write_state);
362 if (fsync (out_fd) < 0)
363 err (1, "fsync");
364 close(out_fd);
365 if (leavep) {
366 state = QUIT;
367 net_write (s, "QUIT\r\n", 6);
368 if (verbose > 1)
357 net_write (STDERR_FILENO, "QUIT\r\n", 6);
369 fprintf (stderr, "QUIT\r\n");
358 } else {
359 if (forkp) {
360 pid_t pid;
361
362 pid = fork();
363 if (pid < 0)
364 warn ("fork");
365 else if(pid != 0) {
366 if(verbose)
367 fprintf (stderr,
368 "(exiting)");
369 return 0;
370 }
371 }
372
373 state = XDELE;
374 if (verbose)
375 fprintf (stderr, "deleting... ");
376 }
377 }
378 continue;
379 } else
380 ++copy;
381 }
382 *p = '\n';
383 if(blank_line &&
384 strncmp(copy, "From ", min(p - copy + 1, 5)) == 0)
385 write_state_add(&write_state, ">", 1);
386 write_state_add(&write_state, copy, p - copy + 1);
387 blank_line = (*copy == '\n');
388 rem -= p - beg + 2;
389 beg = p + 2;
390 } else if (rem >= 3 && strncmp (beg, "+OK", 3) == 0) {
391 if (state == STAT) {
392 if (!do_from)
393 write_state_add(&write_state,
394 from_line, from_line_length);
395 blank_line = 0;
396 if (do_from)
397 state = TOP;
398 else
399 state = RETR;
400 } else if (state == XDELE) {
401 state = QUIT;
402 net_write (s, "QUIT\r\n", 6);
403 if (verbose > 1)
370 } else {
371 if (forkp) {
372 pid_t pid;
373
374 pid = fork();
375 if (pid < 0)
376 warn ("fork");
377 else if(pid != 0) {
378 if(verbose)
379 fprintf (stderr,
380 "(exiting)");
381 return 0;
382 }
383 }
384
385 state = XDELE;
386 if (verbose)
387 fprintf (stderr, "deleting... ");
388 }
389 }
390 continue;
391 } else
392 ++copy;
393 }
394 *p = '\n';
395 if(blank_line &&
396 strncmp(copy, "From ", min(p - copy + 1, 5)) == 0)
397 write_state_add(&write_state, ">", 1);
398 write_state_add(&write_state, copy, p - copy + 1);
399 blank_line = (*copy == '\n');
400 rem -= p - beg + 2;
401 beg = p + 2;
402 } else if (rem >= 3 && strncmp (beg, "+OK", 3) == 0) {
403 if (state == STAT) {
404 if (!do_from)
405 write_state_add(&write_state,
406 from_line, from_line_length);
407 blank_line = 0;
408 if (do_from)
409 state = TOP;
410 else
411 state = RETR;
412 } else if (state == XDELE) {
413 state = QUIT;
414 net_write (s, "QUIT\r\n", 6);
415 if (verbose > 1)
404 net_write (STDERR_FILENO, "QUIT\r\n", 6);
416 fprintf (stderr, "QUIT\r\n");
405 break;
406 } else if (state == DELE) {
407 if (++deleted == count) {
408 state = QUIT;
409 net_write (s, "QUIT\r\n", 6);
410 if (verbose > 1)
417 break;
418 } else if (state == DELE) {
419 if (++deleted == count) {
420 state = QUIT;
421 net_write (s, "QUIT\r\n", 6);
422 if (verbose > 1)
411 net_write (STDERR_FILENO, "QUIT\r\n", 6);
423 fprintf (stderr, "QUIT\r\n");
412 break;
413 }
414 } else if (++state == STAT) {
415 if(sscanf (beg + 4, "%u %u", &count, &bytes) != 2)
416 errx(1, "Bad STAT-line: %.*s", (int)(p - beg), beg);
417 if (verbose) {
418 fprintf (stderr, "%u message(s) (%u bytes). "
419 "fetching... ",
420 count, bytes);
421 if (do_from)
422 fprintf (stderr, "\n");
423 } else if (do_count) {
424 fprintf (stderr, "%u message(s) (%u bytes).\n",
425 count, bytes);
426 }
427 if (count == 0) {
428 state = QUIT;
429 net_write (s, "QUIT\r\n", 6);
430 if (verbose > 1)
424 break;
425 }
426 } else if (++state == STAT) {
427 if(sscanf (beg + 4, "%u %u", &count, &bytes) != 2)
428 errx(1, "Bad STAT-line: %.*s", (int)(p - beg), beg);
429 if (verbose) {
430 fprintf (stderr, "%u message(s) (%u bytes). "
431 "fetching... ",
432 count, bytes);
433 if (do_from)
434 fprintf (stderr, "\n");
435 } else if (do_count) {
436 fprintf (stderr, "%u message(s) (%u bytes).\n",
437 count, bytes);
438 }
439 if (count == 0) {
440 state = QUIT;
441 net_write (s, "QUIT\r\n", 6);
442 if (verbose > 1)
431 net_write (STDERR_FILENO, "QUIT\r\n", 6);
443 fprintf (stderr, "QUIT\r\n");
432 break;
433 }
434 }
435
436 rem -= p - beg + 2;
437 beg = p + 2;
438 } else {
439 if(state == XDELE) {
440 state = DELE;
441 rem -= p - beg + 2;
442 beg = p + 2;
443 } else
444 errx (1, "Bad response: %.*s", (int)(p - beg), beg);
445 }
446 }
447 if (!do_from)
448 write_state_flush (&write_state);
449
450 memmove (in_buf, beg, rem);
451 in_len = rem;
452 in_ptr = in_buf + rem;
453 }
454 if (FD_ISSET(s, &writeset)) {
455 if ((state == STAT && !do_from) || state == RETR)
456 out_len = snprintf (out_buf, sizeof(out_buf),
457 "RETR %u\r\n", ++asked_for);
458 else if ((state == STAT && do_from) || state == TOP)
459 out_len = snprintf (out_buf, sizeof(out_buf),
460 "TOP %u 0\r\n", ++asked_for);
461 else if(state == XDELE) {
462 out_len = snprintf(out_buf, sizeof(out_buf),
463 "XDELE %u %u\r\n", 1, count);
464 sent_xdele++;
465 }
466 else if(state == DELE)
467 out_len = snprintf (out_buf, sizeof(out_buf),
468 "DELE %u\r\n", ++asked_deleted);
469 if (out_len < 0)
470 errx (1, "snprintf failed");
471 if (net_write (s, out_buf, out_len) != out_len)
472 err (1, "write");
473 if (verbose > 1)
444 break;
445 }
446 }
447
448 rem -= p - beg + 2;
449 beg = p + 2;
450 } else {
451 if(state == XDELE) {
452 state = DELE;
453 rem -= p - beg + 2;
454 beg = p + 2;
455 } else
456 errx (1, "Bad response: %.*s", (int)(p - beg), beg);
457 }
458 }
459 if (!do_from)
460 write_state_flush (&write_state);
461
462 memmove (in_buf, beg, rem);
463 in_len = rem;
464 in_ptr = in_buf + rem;
465 }
466 if (FD_ISSET(s, &writeset)) {
467 if ((state == STAT && !do_from) || state == RETR)
468 out_len = snprintf (out_buf, sizeof(out_buf),
469 "RETR %u\r\n", ++asked_for);
470 else if ((state == STAT && do_from) || state == TOP)
471 out_len = snprintf (out_buf, sizeof(out_buf),
472 "TOP %u 0\r\n", ++asked_for);
473 else if(state == XDELE) {
474 out_len = snprintf(out_buf, sizeof(out_buf),
475 "XDELE %u %u\r\n", 1, count);
476 sent_xdele++;
477 }
478 else if(state == DELE)
479 out_len = snprintf (out_buf, sizeof(out_buf),
480 "DELE %u\r\n", ++asked_deleted);
481 if (out_len < 0)
482 errx (1, "snprintf failed");
483 if (net_write (s, out_buf, out_len) != out_len)
484 err (1, "write");
485 if (verbose > 1)
474 write (STDERR_FILENO, out_buf, out_len);
486 fprintf (stderr, "%s", out_buf);
475 }
476 }
477 if (verbose)
478 fprintf (stderr, "Done\n");
479 if (do_from) {
480 free (tmp);
481 free (headers);
482 } else {
483 write_state_destroy (&write_state);
484 }
485 return 0;
486}
487
488#ifdef KRB5
489static int
490do_v5 (const char *host,
491 int port,
492 const char *user,
493 const char *filename,
494 const char *header_str,
495 int leavep,
496 int verbose,
497 int forkp)
498{
499 krb5_error_code ret;
500 krb5_auth_context auth_context = NULL;
501 krb5_principal server;
502 int s;
503
504 s = do_connect (host, port, 1);
505 if (s < 0)
506 return 1;
507
508 ret = krb5_sname_to_principal (context,
509 host,
510 "pop",
511 KRB5_NT_SRV_HST,
512 &server);
513 if (ret) {
514 warnx ("krb5_sname_to_principal: %s",
515 krb5_get_err_text (context, ret));
516 return 1;
517 }
518
519 ret = krb5_sendauth (context,
520 &auth_context,
521 &s,
522 "KPOPV1.0",
523 NULL,
524 server,
525 0,
526 NULL,
527 NULL,
528 NULL,
529 NULL,
530 NULL,
531 NULL);
532 krb5_free_principal (context, server);
533 if (ret) {
534 warnx ("krb5_sendauth: %s",
535 krb5_get_err_text (context, ret));
536 return 1;
537 }
538 return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
539}
540#endif
541
542#ifdef KRB4
543static int
544do_v4 (const char *host,
545 int port,
546 const char *user,
547 const char *filename,
548 const char *header_str,
549 int leavep,
550 int verbose,
551 int forkp)
552{
553 KTEXT_ST ticket;
554 MSG_DAT msg_data;
555 CREDENTIALS cred;
556 des_key_schedule sched;
557 int s;
558 int ret;
559
560 s = do_connect (host, port, 1);
561 if (s < 0)
562 return 1;
563 ret = krb_sendauth(0,
564 s,
565 &ticket,
566 "pop",
567 (char *)host,
568 krb_realmofhost(host),
569 getpid(),
570 &msg_data,
571 &cred,
572 sched,
573 NULL,
574 NULL,
575 "KPOPV0.1");
576 if(ret) {
577 warnx("krb_sendauth: %s", krb_get_err_text(ret));
578 return 1;
579 }
580 return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
581}
582#endif /* KRB4 */
583
584#ifdef HESIOD
585
586#ifdef HESIOD_INTERFACES
587
588static char *
589hesiod_get_pobox (const char **user)
590{
591 void *context;
592 struct hesiod_postoffice *hpo;
593 char *ret = NULL;
594
595 if(hesiod_init (&context) != 0)
596 err (1, "hesiod_init");
597
598 hpo = hesiod_getmailhost (context, *user);
599 if (hpo == NULL) {
600 warn ("hesiod_getmailhost %s", *user);
601 } else {
602 if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
603 errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
604
605 ret = estrdup(hpo->hesiod_po_host);
606 *user = estrdup(hpo->hesiod_po_name);
607 hesiod_free_postoffice (context, hpo);
608 }
609 hesiod_end (context);
610 return ret;
611}
612
613#else /* !HESIOD_INTERFACES */
614
615static char *
616hesiod_get_pobox (const char **user)
617{
618 char *ret = NULL;
619 struct hes_postoffice *hpo;
620
621 hpo = hes_getmailhost (*user);
622 if (hpo == NULL) {
623 warn ("hes_getmailhost %s", *user);
624 } else {
625 if (strcasecmp(hpo->po_type, "pop") != 0)
626 errx (1, "Unsupported po type %s", hpo->po_type);
627
628 ret = estrdup(hpo->po_host);
629 *user = estrdup(hpo->po_name);
630 }
631 return ret;
632}
633
634#endif /* HESIOD_INTERFACES */
635
636#endif /* HESIOD */
637
638static char *
639get_pobox (const char **user)
640{
641 char *ret = NULL;
642
643#ifdef HESIOD
644 ret = hesiod_get_pobox (user);
645#endif
646
647 if (ret == NULL)
648 ret = getenv("MAILHOST");
649 if (ret == NULL)
650 errx (1, "MAILHOST not set");
651 return ret;
652}
653
654static void
655parse_pobox (char *a0, const char **host, const char **user)
656{
657 const char *h, *u;
658 char *p;
659 int po = 0;
660
661 if (a0 == NULL) {
662
663 *user = getenv ("USERNAME");
664 if (*user == NULL) {
665 struct passwd *pwd = getpwuid (getuid ());
666
667 if (pwd == NULL)
668 errx (1, "Who are you?");
669 *user = estrdup (pwd->pw_name);
670 }
671 *host = get_pobox (user);
672 return;
673 }
674
675 /* if the specification starts with po:, remember this information */
676 if(strncmp(a0, "po:", 3) == 0) {
677 a0 += 3;
678 po++;
679 }
680 /* if there is an `@', the hostname is after it, otherwise at the
681 beginning of the string */
682 p = strchr(a0, '@');
683 if(p != NULL) {
684 *p++ = '\0';
685 h = p;
686 } else {
687 h = a0;
688 }
689 /* if there is a `:', the username comes before it, otherwise at
690 the beginning of the string */
691 p = strchr(a0, ':');
692 if(p != NULL) {
693 *p++ = '\0';
694 u = p;
695 } else {
696 u = a0;
697 }
698 if(h == u) {
699 /* some inconsistent compatibility with various mailers */
700 if(po) {
701 h = get_pobox (&u);
702 } else {
703 u = get_default_username ();
704 if (u == NULL)
705 errx (1, "Who are you?");
706 }
707 }
708 *host = h;
709 *user = u;
710}
711
712int
713main(int argc, char **argv)
714{
715 int port = 0;
716 int optind = 0;
717 int ret = 1;
718 const char *host, *user, *filename = NULL;
719 char *pobox = NULL;
720
721 setprogname (argv[0]);
722
723#ifdef KRB5
724 {
725 krb5_error_code ret;
726
727 ret = krb5_init_context (&context);
728 if (ret)
729 errx (1, "krb5_init_context failed: %d", ret);
730 }
731#endif
732
733 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
734 &optind))
735 usage (1);
736
737 argc -= optind;
738 argv += optind;
739
740#if defined(KRB4) && defined(KRB5)
741 if(use_v4 == -1 && use_v5 == 1)
742 use_v4 = 0;
743 if(use_v5 == -1 && use_v4 == 1)
744 use_v5 = 0;
745#endif
746
747 if (do_help)
748 usage (0);
749
750 if (do_version) {
751 print_version(NULL);
752 return 0;
753 }
754
755 if (do_from && header_str == NULL)
756 header_str = "From:";
757 else if (header_str != NULL)
758 do_from = 1;
759
760 if (do_from) {
761 if (argc == 0)
762 pobox = NULL;
763 else if (argc == 1)
764 pobox = argv[0];
765 else
766 usage (1);
767 } else {
768 if (argc == 1) {
769 filename = argv[0];
770 pobox = NULL;
771 } else if (argc == 2) {
772 filename = argv[1];
773 pobox = argv[0];
774 } else
775 usage (1);
776 }
777
778 if (port_str) {
779 struct servent *s = roken_getservbyname (port_str, "tcp");
780
781 if (s)
782 port = s->s_port;
783 else {
784 char *ptr;
785
786 port = strtol (port_str, &ptr, 10);
787 if (port == 0 && ptr == port_str)
788 errx (1, "Bad port `%s'", port_str);
789 port = htons(port);
790 }
791 }
792 if (port == 0) {
793#ifdef KRB5
794 port = krb5_getportbyname (context, "kpop", "tcp", 1109);
795#elif defined(KRB4)
796 port = k_getportbyname ("kpop", "tcp", htons(1109));
797#else
798#error must define KRB4 or KRB5
799#endif
800 }
801
802 parse_pobox (pobox, &host, &user);
803
804#ifdef KRB5
805 if (ret && use_v5) {
806 ret = do_v5 (host, port, user, filename, header_str,
807 do_leave, verbose_level, do_fork);
808 }
809#endif
810
811#ifdef KRB4
812 if (ret && use_v4) {
813 ret = do_v4 (host, port, user, filename, header_str,
814 do_leave, verbose_level, do_fork);
815 }
816#endif /* KRB4 */
817 return ret;
818}
487 }
488 }
489 if (verbose)
490 fprintf (stderr, "Done\n");
491 if (do_from) {
492 free (tmp);
493 free (headers);
494 } else {
495 write_state_destroy (&write_state);
496 }
497 return 0;
498}
499
500#ifdef KRB5
501static int
502do_v5 (const char *host,
503 int port,
504 const char *user,
505 const char *filename,
506 const char *header_str,
507 int leavep,
508 int verbose,
509 int forkp)
510{
511 krb5_error_code ret;
512 krb5_auth_context auth_context = NULL;
513 krb5_principal server;
514 int s;
515
516 s = do_connect (host, port, 1);
517 if (s < 0)
518 return 1;
519
520 ret = krb5_sname_to_principal (context,
521 host,
522 "pop",
523 KRB5_NT_SRV_HST,
524 &server);
525 if (ret) {
526 warnx ("krb5_sname_to_principal: %s",
527 krb5_get_err_text (context, ret));
528 return 1;
529 }
530
531 ret = krb5_sendauth (context,
532 &auth_context,
533 &s,
534 "KPOPV1.0",
535 NULL,
536 server,
537 0,
538 NULL,
539 NULL,
540 NULL,
541 NULL,
542 NULL,
543 NULL);
544 krb5_free_principal (context, server);
545 if (ret) {
546 warnx ("krb5_sendauth: %s",
547 krb5_get_err_text (context, ret));
548 return 1;
549 }
550 return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
551}
552#endif
553
554#ifdef KRB4
555static int
556do_v4 (const char *host,
557 int port,
558 const char *user,
559 const char *filename,
560 const char *header_str,
561 int leavep,
562 int verbose,
563 int forkp)
564{
565 KTEXT_ST ticket;
566 MSG_DAT msg_data;
567 CREDENTIALS cred;
568 des_key_schedule sched;
569 int s;
570 int ret;
571
572 s = do_connect (host, port, 1);
573 if (s < 0)
574 return 1;
575 ret = krb_sendauth(0,
576 s,
577 &ticket,
578 "pop",
579 (char *)host,
580 krb_realmofhost(host),
581 getpid(),
582 &msg_data,
583 &cred,
584 sched,
585 NULL,
586 NULL,
587 "KPOPV0.1");
588 if(ret) {
589 warnx("krb_sendauth: %s", krb_get_err_text(ret));
590 return 1;
591 }
592 return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
593}
594#endif /* KRB4 */
595
596#ifdef HESIOD
597
598#ifdef HESIOD_INTERFACES
599
600static char *
601hesiod_get_pobox (const char **user)
602{
603 void *context;
604 struct hesiod_postoffice *hpo;
605 char *ret = NULL;
606
607 if(hesiod_init (&context) != 0)
608 err (1, "hesiod_init");
609
610 hpo = hesiod_getmailhost (context, *user);
611 if (hpo == NULL) {
612 warn ("hesiod_getmailhost %s", *user);
613 } else {
614 if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
615 errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
616
617 ret = estrdup(hpo->hesiod_po_host);
618 *user = estrdup(hpo->hesiod_po_name);
619 hesiod_free_postoffice (context, hpo);
620 }
621 hesiod_end (context);
622 return ret;
623}
624
625#else /* !HESIOD_INTERFACES */
626
627static char *
628hesiod_get_pobox (const char **user)
629{
630 char *ret = NULL;
631 struct hes_postoffice *hpo;
632
633 hpo = hes_getmailhost (*user);
634 if (hpo == NULL) {
635 warn ("hes_getmailhost %s", *user);
636 } else {
637 if (strcasecmp(hpo->po_type, "pop") != 0)
638 errx (1, "Unsupported po type %s", hpo->po_type);
639
640 ret = estrdup(hpo->po_host);
641 *user = estrdup(hpo->po_name);
642 }
643 return ret;
644}
645
646#endif /* HESIOD_INTERFACES */
647
648#endif /* HESIOD */
649
650static char *
651get_pobox (const char **user)
652{
653 char *ret = NULL;
654
655#ifdef HESIOD
656 ret = hesiod_get_pobox (user);
657#endif
658
659 if (ret == NULL)
660 ret = getenv("MAILHOST");
661 if (ret == NULL)
662 errx (1, "MAILHOST not set");
663 return ret;
664}
665
666static void
667parse_pobox (char *a0, const char **host, const char **user)
668{
669 const char *h, *u;
670 char *p;
671 int po = 0;
672
673 if (a0 == NULL) {
674
675 *user = getenv ("USERNAME");
676 if (*user == NULL) {
677 struct passwd *pwd = getpwuid (getuid ());
678
679 if (pwd == NULL)
680 errx (1, "Who are you?");
681 *user = estrdup (pwd->pw_name);
682 }
683 *host = get_pobox (user);
684 return;
685 }
686
687 /* if the specification starts with po:, remember this information */
688 if(strncmp(a0, "po:", 3) == 0) {
689 a0 += 3;
690 po++;
691 }
692 /* if there is an `@', the hostname is after it, otherwise at the
693 beginning of the string */
694 p = strchr(a0, '@');
695 if(p != NULL) {
696 *p++ = '\0';
697 h = p;
698 } else {
699 h = a0;
700 }
701 /* if there is a `:', the username comes before it, otherwise at
702 the beginning of the string */
703 p = strchr(a0, ':');
704 if(p != NULL) {
705 *p++ = '\0';
706 u = p;
707 } else {
708 u = a0;
709 }
710 if(h == u) {
711 /* some inconsistent compatibility with various mailers */
712 if(po) {
713 h = get_pobox (&u);
714 } else {
715 u = get_default_username ();
716 if (u == NULL)
717 errx (1, "Who are you?");
718 }
719 }
720 *host = h;
721 *user = u;
722}
723
724int
725main(int argc, char **argv)
726{
727 int port = 0;
728 int optind = 0;
729 int ret = 1;
730 const char *host, *user, *filename = NULL;
731 char *pobox = NULL;
732
733 setprogname (argv[0]);
734
735#ifdef KRB5
736 {
737 krb5_error_code ret;
738
739 ret = krb5_init_context (&context);
740 if (ret)
741 errx (1, "krb5_init_context failed: %d", ret);
742 }
743#endif
744
745 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
746 &optind))
747 usage (1);
748
749 argc -= optind;
750 argv += optind;
751
752#if defined(KRB4) && defined(KRB5)
753 if(use_v4 == -1 && use_v5 == 1)
754 use_v4 = 0;
755 if(use_v5 == -1 && use_v4 == 1)
756 use_v5 = 0;
757#endif
758
759 if (do_help)
760 usage (0);
761
762 if (do_version) {
763 print_version(NULL);
764 return 0;
765 }
766
767 if (do_from && header_str == NULL)
768 header_str = "From:";
769 else if (header_str != NULL)
770 do_from = 1;
771
772 if (do_from) {
773 if (argc == 0)
774 pobox = NULL;
775 else if (argc == 1)
776 pobox = argv[0];
777 else
778 usage (1);
779 } else {
780 if (argc == 1) {
781 filename = argv[0];
782 pobox = NULL;
783 } else if (argc == 2) {
784 filename = argv[1];
785 pobox = argv[0];
786 } else
787 usage (1);
788 }
789
790 if (port_str) {
791 struct servent *s = roken_getservbyname (port_str, "tcp");
792
793 if (s)
794 port = s->s_port;
795 else {
796 char *ptr;
797
798 port = strtol (port_str, &ptr, 10);
799 if (port == 0 && ptr == port_str)
800 errx (1, "Bad port `%s'", port_str);
801 port = htons(port);
802 }
803 }
804 if (port == 0) {
805#ifdef KRB5
806 port = krb5_getportbyname (context, "kpop", "tcp", 1109);
807#elif defined(KRB4)
808 port = k_getportbyname ("kpop", "tcp", htons(1109));
809#else
810#error must define KRB4 or KRB5
811#endif
812 }
813
814 parse_pobox (pobox, &host, &user);
815
816#ifdef KRB5
817 if (ret && use_v5) {
818 ret = do_v5 (host, port, user, filename, header_str,
819 do_leave, verbose_level, do_fork);
820 }
821#endif
822
823#ifdef KRB4
824 if (ret && use_v4) {
825 ret = do_v4 (host, port, user, filename, header_str,
826 do_leave, verbose_level, do_fork);
827 }
828#endif /* KRB4 */
829 return ret;
830}