push.c revision 120945
1/*
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"
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
140static char *pop_state_string[] = {
141    "INIT", "GREET", "USER", "PASS", "STAT", "RETR", "TOP",
142    "DELE", "XDELE", "QUIT"
143};
144
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)
275	fprintf (stderr, "%s", out_buf);
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
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)
346				fprintf (stderr, "QUIT\r\n");
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)
369					fprintf (stderr, "QUIT\r\n");
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)
416			    fprintf (stderr, "QUIT\r\n");
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)
423				fprintf (stderr, "QUIT\r\n");
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)
443				fprintf (stderr, "QUIT\r\n");
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)
486		fprintf (stderr, "%s", out_buf);
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}
831