xref: /freebsd/crypto/heimdal/appl/push/push.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997-1999 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"
35 RCSID("$Id: push.c,v 1.38 1999/12/28 03:46:06 assar Exp $");
36 
37 #ifdef KRB4
38 static int use_v4 = -1;
39 #endif
40 
41 #ifdef KRB5
42 static int use_v5 = -1;
43 static krb5_context context;
44 #endif
45 
46 static char *port_str;
47 static int verbose_level;
48 static int do_fork;
49 static int do_leave;
50 static int do_version;
51 static int do_help;
52 static int do_from;
53 static int do_count;
54 static char *header_str;
55 
56 struct 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     { "header",	 0,  arg_string,	&header_str,	"Header string 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 
84 static void
85 usage (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 
95 static int
96 do_connect (const char *hostname, int port, int nodelay)
97 {
98     struct addrinfo *ai, *a;
99     struct addrinfo hints;
100     int error;
101     int s;
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 
137 typedef enum { INIT = 0, GREET, USER, PASS, STAT, RETR, TOP,
138 	       DELE, XDELE, QUIT} pop_state;
139 
140 #define PUSH_BUFSIZ 65536
141 
142 #define STEP 16
143 
144 struct write_state {
145     struct iovec *iovecs;
146     size_t niovecs, maxiovecs, allociovecs;
147     int fd;
148 };
149 
150 static void
151 write_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 = malloc(w->allociovecs * sizeof(*w->iovecs));
161     if (w->iovecs == NULL)
162 	err (1, "malloc");
163     w->fd = fd;
164 }
165 
166 static void
167 write_state_add (struct write_state *w, void *v, size_t len)
168 {
169     if(w->niovecs == w->allociovecs) {
170 	if(w->niovecs == w->maxiovecs) {
171 	    if(writev (w->fd, w->iovecs, w->niovecs) < 0)
172 		err(1, "writev");
173 	    w->niovecs = 0;
174 	} else {
175 	    w->allociovecs = min(w->allociovecs + STEP, w->maxiovecs);
176 	    w->iovecs = realloc (w->iovecs,
177 				 w->allociovecs * sizeof(*w->iovecs));
178 	    if (w->iovecs == NULL)
179 		errx (1, "realloc");
180 	}
181     }
182     w->iovecs[w->niovecs].iov_base = v;
183     w->iovecs[w->niovecs].iov_len  = len;
184     ++w->niovecs;
185 }
186 
187 static void
188 write_state_flush (struct write_state *w)
189 {
190     if (w->niovecs) {
191 	if (writev (w->fd, w->iovecs, w->niovecs) < 0)
192 	    err (1, "writev");
193 	w->niovecs = 0;
194     }
195 }
196 
197 static void
198 write_state_destroy (struct write_state *w)
199 {
200     free (w->iovecs);
201 }
202 
203 static int
204 doit(int s,
205      const char *host,
206      const char *user,
207      const char *outfilename,
208      const char *header_str,
209      int leavep,
210      int verbose,
211      int forkp)
212 {
213     int ret;
214     char out_buf[PUSH_BUFSIZ];
215     size_t out_len = 0;
216     char in_buf[PUSH_BUFSIZ + 1];	/* sentinel */
217     size_t in_len = 0;
218     char *in_ptr = in_buf;
219     pop_state state = INIT;
220     unsigned count, bytes;
221     unsigned asked_for = 0, retrieved = 0, asked_deleted = 0, deleted = 0;
222     unsigned sent_xdele = 0;
223     int out_fd;
224     char from_line[128];
225     size_t from_line_length;
226     time_t now;
227     struct write_state write_state;
228 
229     if (do_from) {
230 	out_fd = -1;
231 	if (verbose)
232 	    fprintf (stderr, "%s@%s\n", user, host);
233     } else {
234 	out_fd = open(outfilename, O_WRONLY | O_APPEND | O_CREAT, 0666);
235 	if (out_fd < 0)
236 	    err (1, "open %s", outfilename);
237 	if (verbose)
238 	    fprintf (stderr, "%s@%s -> %s\n", user, host, outfilename);
239     }
240 
241     now = time(NULL);
242     from_line_length = snprintf (from_line, sizeof(from_line),
243 				 "From %s %s", "push", ctime(&now));
244 
245     out_len = snprintf (out_buf, sizeof(out_buf),
246 			"USER %s\r\nPASS hej\r\nSTAT\r\n",
247 			user);
248     if (net_write (s, out_buf, out_len) != out_len)
249 	err (1, "write");
250     if (verbose > 1)
251 	write (STDERR_FILENO, out_buf, out_len);
252 
253     if (!do_from)
254 	write_state_init (&write_state, out_fd);
255 
256     while(state != QUIT) {
257 	fd_set readset, writeset;
258 
259 	FD_ZERO(&readset);
260 	FD_ZERO(&writeset);
261 	FD_SET(s,&readset);
262 	if (((state == STAT || state == RETR || state == TOP)
263 	     && asked_for < count)
264 	    || (state == XDELE && !sent_xdele)
265 	    || (state == DELE && asked_deleted < count))
266 	    FD_SET(s,&writeset);
267 	ret = select (s + 1, &readset, &writeset, NULL, NULL);
268 	if (ret < 0) {
269 	    if (errno == EAGAIN)
270 		continue;
271 	    else
272 		err (1, "select");
273 	}
274 
275 	if (FD_ISSET(s, &readset)) {
276 	    char *beg, *p;
277 	    size_t rem;
278 	    int blank_line = 0;
279 
280 	    ret = read (s, in_ptr, sizeof(in_buf) - in_len - 1);
281 	    if (ret < 0)
282 		err (1, "read");
283 	    else if (ret == 0)
284 		errx (1, "EOF during read");
285 
286 	    in_len += ret;
287 	    in_ptr += ret;
288 	    *in_ptr = '\0';
289 
290 	    beg = in_buf;
291 	    rem = in_len;
292 	    while(rem > 1
293 		  && (p = strstr(beg, "\r\n")) != NULL) {
294 		if (state == TOP) {
295 		    char *copy = beg;
296 
297 		    if (strncasecmp(copy,
298 				    header_str,
299 				    min(p - copy + 1, strlen(header_str))) == 0) {
300 			fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
301 		    }
302 		    if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
303 			state = STAT;
304 			if (++retrieved == count) {
305 			    state = QUIT;
306 			    net_write (s, "QUIT\r\n", 6);
307 			    if (verbose > 1)
308 				net_write (STDERR_FILENO, "QUIT\r\n", 6);
309 			}
310 		    }
311 		    rem -= p - beg + 2;
312 		    beg = p + 2;
313 		} else if (state == RETR) {
314 		    char *copy = beg;
315 		    if (beg[0] == '.') {
316 			if (beg[1] == '\r' && beg[2] == '\n') {
317 			    if(!blank_line)
318 				write_state_add(&write_state, "\n", 1);
319 			    state = STAT;
320 			    rem -= p - beg + 2;
321 			    beg = p + 2;
322 			    if (++retrieved == count) {
323 				write_state_flush (&write_state);
324 				if (fsync (out_fd) < 0)
325 				    err (1, "fsync");
326 				close(out_fd);
327 				if (leavep) {
328 				    state = QUIT;
329 				    net_write (s, "QUIT\r\n", 6);
330 				    if (verbose > 1)
331 					net_write (STDERR_FILENO, "QUIT\r\n", 6);
332 				} else {
333 				    if (forkp) {
334 					pid_t pid;
335 
336 					pid = fork();
337 					if (pid < 0)
338 					    warn ("fork");
339 					else if(pid != 0) {
340 					    if(verbose)
341 						fprintf (stderr,
342 							 "(exiting)");
343 					    return 0;
344 					}
345 				    }
346 
347 				    state = XDELE;
348 				    if (verbose)
349 					fprintf (stderr, "deleting... ");
350 				}
351 			    }
352 			    continue;
353 			} else
354 			    ++copy;
355 		    }
356 		    *p = '\n';
357 		    if(blank_line &&
358 		       strncmp(copy, "From ", min(p - copy + 1, 5)) == 0)
359 			write_state_add(&write_state, ">", 1);
360 		    write_state_add(&write_state, copy, p - copy + 1);
361 		    blank_line = (*copy == '\n');
362 		    rem -= p - beg + 2;
363 		    beg = p + 2;
364 		} else if (rem >= 3 && strncmp (beg, "+OK", 3) == 0) {
365 		    if (state == STAT) {
366 			if (!do_from)
367 			    write_state_add(&write_state,
368 					    from_line, from_line_length);
369 			blank_line = 0;
370 			if (do_from)
371 			    state = TOP;
372 			else
373 			    state = RETR;
374 		    } else if (state == XDELE) {
375 			state = QUIT;
376 			net_write (s, "QUIT\r\n", 6);
377 			if (verbose > 1)
378 			    net_write (STDERR_FILENO, "QUIT\r\n", 6);
379 			break;
380 		    } else if (state == DELE) {
381 			if (++deleted == count) {
382 			    state = QUIT;
383 			    net_write (s, "QUIT\r\n", 6);
384 			    if (verbose > 1)
385 				net_write (STDERR_FILENO, "QUIT\r\n", 6);
386 			    break;
387 			}
388 		    } else if (++state == STAT) {
389 			if(sscanf (beg + 4, "%u %u", &count, &bytes) != 2)
390 			    errx(1, "Bad STAT-line: %.*s", (int)(p - beg), beg);
391 			if (verbose) {
392 			    fprintf (stderr, "%u message(s) (%u bytes). "
393 				     "fetching... ",
394 				     count, bytes);
395 			    if (do_from)
396 				fprintf (stderr, "\n");
397 			} else if (do_count) {
398 			    fprintf (stderr, "%u message(s) (%u bytes).\n",
399 				     count, bytes);
400 			}
401 			if (count == 0) {
402 			    state = QUIT;
403 			    net_write (s, "QUIT\r\n", 6);
404 			    if (verbose > 1)
405 				net_write (STDERR_FILENO, "QUIT\r\n", 6);
406 			    break;
407 			}
408 		    }
409 
410 		    rem -= p - beg + 2;
411 		    beg = p + 2;
412 		} else {
413 		    if(state == XDELE) {
414 			state = DELE;
415 			rem -= p - beg + 2;
416 			beg = p + 2;
417 		    } else
418 			errx (1, "Bad response: %.*s", (int)(p - beg), beg);
419 		}
420 	    }
421 	    if (!do_from)
422 		write_state_flush (&write_state);
423 
424 	    memmove (in_buf, beg, rem);
425 	    in_len = rem;
426 	    in_ptr = in_buf + rem;
427 	}
428 	if (FD_ISSET(s, &writeset)) {
429 	    if ((state == STAT && !do_from) || state == RETR)
430 		out_len = snprintf (out_buf, sizeof(out_buf),
431 				    "RETR %u\r\n", ++asked_for);
432 	    else if ((state == STAT && do_from) || state == TOP)
433 		out_len = snprintf (out_buf, sizeof(out_buf),
434 				    "TOP %u 0\r\n", ++asked_for);
435 	    else if(state == XDELE) {
436 		out_len = snprintf(out_buf, sizeof(out_buf),
437 				   "XDELE %u %u\r\n", 1, count);
438 		sent_xdele++;
439 	    }
440 	    else if(state == DELE)
441 		out_len = snprintf (out_buf, sizeof(out_buf),
442 				    "DELE %u\r\n", ++asked_deleted);
443 	    if (net_write (s, out_buf, out_len) != out_len)
444 		err (1, "write");
445 	    if (verbose > 1)
446 		write (STDERR_FILENO, out_buf, out_len);
447 	}
448     }
449     if (verbose)
450 	fprintf (stderr, "Done\n");
451     if (!do_from)
452 	write_state_destroy (&write_state);
453     return 0;
454 }
455 
456 #ifdef KRB5
457 static int
458 do_v5 (const char *host,
459        int port,
460        const char *user,
461        const char *filename,
462        const char *header_str,
463        int leavep,
464        int verbose,
465        int forkp)
466 {
467     krb5_error_code ret;
468     krb5_auth_context auth_context = NULL;
469     krb5_principal server;
470     int s;
471 
472     s = do_connect (host, port, 1);
473     if (s < 0)
474 	return 1;
475 
476     ret = krb5_sname_to_principal (context,
477 				   host,
478 				   "pop",
479 				   KRB5_NT_SRV_HST,
480 				   &server);
481     if (ret) {
482 	warnx ("krb5_sname_to_principal: %s",
483 	       krb5_get_err_text (context, ret));
484 	return 1;
485     }
486 
487     ret = krb5_sendauth (context,
488 			 &auth_context,
489 			 &s,
490 			 "KPOPV1.0",
491 			 NULL,
492 			 server,
493 			 0,
494 			 NULL,
495 			 NULL,
496 			 NULL,
497 			 NULL,
498 			 NULL,
499 			 NULL);
500     krb5_free_principal (context, server);
501     if (ret) {
502 	warnx ("krb5_sendauth: %s",
503 	       krb5_get_err_text (context, ret));
504 	return 1;
505     }
506     return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
507 }
508 #endif
509 
510 #ifdef KRB4
511 static int
512 do_v4 (const char *host,
513        int port,
514        const char *user,
515        const char *filename,
516        const char *header_str,
517        int leavep,
518        int verbose,
519        int forkp)
520 {
521     KTEXT_ST ticket;
522     MSG_DAT msg_data;
523     CREDENTIALS cred;
524     des_key_schedule sched;
525     int s;
526     int ret;
527 
528     s = do_connect (host, port, 1);
529     if (s < 0)
530 	return 1;
531     ret = krb_sendauth(0,
532 		       s,
533 		       &ticket,
534 		       "pop",
535 		       (char *)host,
536 		       krb_realmofhost(host),
537 		       getpid(),
538 		       &msg_data,
539 		       &cred,
540 		       sched,
541 		       NULL,
542 		       NULL,
543 		       "KPOPV0.1");
544     if(ret) {
545 	warnx("krb_sendauth: %s", krb_get_err_text(ret));
546 	return 1;
547     }
548     return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
549 }
550 #endif /* KRB4 */
551 
552 #ifdef HESIOD
553 
554 #ifdef HESIOD_INTERFACES
555 
556 static char *
557 hesiod_get_pobox (const char **user)
558 {
559     void *context;
560     struct hesiod_postoffice *hpo;
561     char *ret = NULL;
562 
563     if(hesiod_init (&context) != 0)
564 	err (1, "hesiod_init");
565 
566     hpo = hesiod_getmailhost (context, *user);
567     if (hpo == NULL) {
568 	warn ("hesiod_getmailhost %s", *user);
569     } else {
570 	if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
571 	    errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
572 
573 	ret = strdup(hpo->hesiod_po_host);
574 	if(ret == NULL)
575 	    errx (1, "strdup: out of memory");
576 	*user = strdup(hpo->hesiod_po_name);
577 	if (*user == NULL)
578 	    errx (1, "strdup: out of memory");
579 	hesiod_free_postoffice (context, hpo);
580     }
581     hesiod_end (context);
582     return ret;
583 }
584 
585 #else /* !HESIOD_INTERFACES */
586 
587 static char *
588 hesiod_get_pobox (const char **user)
589 {
590     char *ret = NULL;
591     struct hes_postoffice *hpo;
592 
593     hpo = hes_getmailhost (*user);
594     if (hpo == NULL) {
595 	warn ("hes_getmailhost %s", *user);
596     } else {
597 	if (strcasecmp(hpo->po_type, "pop") != 0)
598 	    errx (1, "Unsupported po type %s", hpo->po_type);
599 
600 	ret = strdup(hpo->po_host);
601 	if(ret == NULL)
602 	    errx (1, "strdup: out of memory");
603 	*user = strdup(hpo->po_name);
604 	if (*user == NULL)
605 	    errx (1, "strdup: out of memory");
606     }
607     return ret;
608 }
609 
610 #endif /* HESIOD_INTERFACES */
611 
612 #endif /* HESIOD */
613 
614 static char *
615 get_pobox (const char **user)
616 {
617     char *ret = NULL;
618 
619 #ifdef HESIOD
620     ret = hesiod_get_pobox (user);
621 #endif
622 
623     if (ret == NULL)
624 	ret = getenv("MAILHOST");
625     if (ret == NULL)
626 	errx (1, "MAILHOST not set");
627     return ret;
628 }
629 
630 static void
631 parse_pobox (char *a0, const char **host, const char **user)
632 {
633     const char *h, *u;
634     char *p;
635     int po = 0;
636 
637     if (a0 == NULL) {
638 
639 	*user = getenv ("USERNAME");
640 	if (*user == NULL) {
641 	    struct passwd *pwd = getpwuid (getuid ());
642 
643 	    if (pwd == NULL)
644 		errx (1, "Who are you?");
645 	    *user = strdup (pwd->pw_name);
646 	    if (*user == NULL)
647 		errx (1, "strdup: out of memory");
648 	}
649 	*host = get_pobox (user);
650 	return;
651     }
652 
653     /* if the specification starts with po:, remember this information */
654     if(strncmp(a0, "po:", 3) == 0) {
655 	a0 += 3;
656 	po++;
657     }
658     /* if there is an `@', the hostname is after it, otherwise at the
659        beginning of the string */
660     p = strchr(a0, '@');
661     if(p != NULL) {
662 	*p++ = '\0';
663 	h = p;
664     } else {
665 	h = a0;
666     }
667     /* if there is a `:', the username comes before it, otherwise at
668        the beginning of the string */
669     p = strchr(a0, ':');
670     if(p != NULL) {
671 	*p++ = '\0';
672 	u = p;
673     } else {
674 	u = a0;
675     }
676     if(h == u) {
677 	/* some inconsistent compatibility with various mailers */
678 	if(po) {
679 	    h = get_pobox (&u);
680 	} else {
681 	    u = get_default_username ();
682 	    if (u == NULL)
683 		errx (1, "Who are you?");
684 	}
685     }
686     *host = h;
687     *user = u;
688 }
689 
690 int
691 main(int argc, char **argv)
692 {
693     int port = 0;
694     int optind = 0;
695     int ret = 1;
696     const char *host, *user, *filename = NULL;
697     char *pobox = NULL;
698 
699     set_progname (argv[0]);
700 
701 #ifdef KRB5
702     krb5_init_context (&context);
703 #endif
704 
705     if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
706 		&optind))
707 	usage (1);
708 
709     argc -= optind;
710     argv += optind;
711 
712 #if defined(KRB4) && defined(KRB5)
713     if(use_v4 == -1 && use_v5 == 1)
714 	use_v4 = 0;
715     if(use_v5 == -1 && use_v4 == 1)
716 	use_v5 = 0;
717 #endif
718 
719     if (do_help)
720 	usage (0);
721 
722     if (do_version) {
723 	print_version(NULL);
724 	return 0;
725     }
726 
727     if (do_from && header_str == NULL)
728 	header_str = "From:";
729     else if (header_str != NULL)
730 	do_from = 1;
731 
732     if (do_from) {
733 	if (argc == 0)
734 	    pobox = NULL;
735 	else if (argc == 1)
736 	    pobox = argv[0];
737 	else
738 	    usage (1);
739     } else {
740 	if (argc == 1) {
741 	    filename = argv[0];
742 	    pobox    = NULL;
743 	} else if (argc == 2) {
744 	    filename = argv[1];
745 	    pobox    = argv[0];
746 	} else
747 	    usage (1);
748     }
749 
750     if (port_str) {
751 	struct servent *s = roken_getservbyname (port_str, "tcp");
752 
753 	if (s)
754 	    port = s->s_port;
755 	else {
756 	    char *ptr;
757 
758 	    port = strtol (port_str, &ptr, 10);
759 	    if (port == 0 && ptr == port_str)
760 		errx (1, "Bad port `%s'", port_str);
761 	    port = htons(port);
762 	}
763     }
764     if (port == 0) {
765 #ifdef KRB5
766 	port = krb5_getportbyname (context, "kpop", "tcp", 1109);
767 #elif defined(KRB4)
768 	port = k_getportbyname ("kpop", "tcp", htons(1109));
769 #else
770 #error must define KRB4 or KRB5
771 #endif
772     }
773 
774     parse_pobox (pobox, &host, &user);
775 
776 #ifdef KRB5
777     if (ret && use_v5) {
778 	ret = do_v5 (host, port, user, filename, header_str,
779 		     do_leave, verbose_level, do_fork);
780     }
781 #endif
782 
783 #ifdef KRB4
784     if (ret && use_v4) {
785 	ret = do_v4 (host, port, user, filename, header_str,
786 		     do_leave, verbose_level, do_fork);
787     }
788 #endif /* KRB4 */
789     return ret;
790 }
791