xref: /freebsd/crypto/heimdal/appl/push/push.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*
2  * Copyright (c) 1997-2000 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.44 2001/02/20 01:44:47 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     { "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 
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 = -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 
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 = emalloc(w->allociovecs * sizeof(*w->iovecs));
161     w->fd = fd;
162 }
163 
164 static void
165 write_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 
183 static void
184 write_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 
193 static void
194 write_state_destroy (struct write_state *w)
195 {
196     free (w->iovecs);
197 }
198 
199 static int
200 doit(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     size_t 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 (net_write (s, out_buf, out_len) != out_len)
266 	err (1, "write");
267     if (verbose > 1)
268 	write (STDERR_FILENO, out_buf, out_len);
269 
270     if (!do_from)
271 	write_state_init (&write_state, out_fd);
272 
273     while(state != QUIT) {
274 	fd_set readset, writeset;
275 
276 	FD_ZERO(&readset);
277 	FD_ZERO(&writeset);
278 	if (s >= FD_SETSIZE)
279 	    errx (1, "fd too large");
280 	FD_SET(s,&readset);
281 	if (((state == STAT || state == RETR || state == TOP)
282 	     && asked_for < count)
283 	    || (state == XDELE && !sent_xdele)
284 	    || (state == DELE && asked_deleted < count))
285 	    FD_SET(s,&writeset);
286 	ret = select (s + 1, &readset, &writeset, NULL, NULL);
287 	if (ret < 0) {
288 	    if (errno == EAGAIN)
289 		continue;
290 	    else
291 		err (1, "select");
292 	}
293 
294 	if (FD_ISSET(s, &readset)) {
295 	    char *beg, *p;
296 	    size_t rem;
297 	    int blank_line = 0;
298 
299 	    ret = read (s, in_ptr, sizeof(in_buf) - in_len - 1);
300 	    if (ret < 0)
301 		err (1, "read");
302 	    else if (ret == 0)
303 		errx (1, "EOF during read");
304 
305 	    in_len += ret;
306 	    in_ptr += ret;
307 	    *in_ptr = '\0';
308 
309 	    beg = in_buf;
310 	    rem = in_len;
311 	    while(rem > 1
312 		  && (p = strstr(beg, "\r\n")) != NULL) {
313 		if (state == TOP) {
314 		    char *copy = beg;
315 
316 		    for (i = 0; i < numheaders; i++) {
317 			size_t len;
318 
319 			len = min(p - copy + 1, strlen(headers[i]));
320 			if (strncasecmp(copy, headers[i], len) == 0) {
321 			    fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
322 			}
323 		    }
324 		    if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
325 			if (numheaders > 1)
326 			    fprintf (stdout, "\n");
327 			state = STAT;
328 			if (++retrieved == count) {
329 			    state = QUIT;
330 			    net_write (s, "QUIT\r\n", 6);
331 			    if (verbose > 1)
332 				net_write (STDERR_FILENO, "QUIT\r\n", 6);
333 			}
334 		    }
335 		    rem -= p - beg + 2;
336 		    beg = p + 2;
337 		} else if (state == RETR) {
338 		    char *copy = beg;
339 		    if (beg[0] == '.') {
340 			if (beg[1] == '\r' && beg[2] == '\n') {
341 			    if(!blank_line)
342 				write_state_add(&write_state, "\n", 1);
343 			    state = STAT;
344 			    rem -= p - beg + 2;
345 			    beg = p + 2;
346 			    if (++retrieved == count) {
347 				write_state_flush (&write_state);
348 				if (fsync (out_fd) < 0)
349 				    err (1, "fsync");
350 				close(out_fd);
351 				if (leavep) {
352 				    state = QUIT;
353 				    net_write (s, "QUIT\r\n", 6);
354 				    if (verbose > 1)
355 					net_write (STDERR_FILENO, "QUIT\r\n", 6);
356 				} else {
357 				    if (forkp) {
358 					pid_t pid;
359 
360 					pid = fork();
361 					if (pid < 0)
362 					    warn ("fork");
363 					else if(pid != 0) {
364 					    if(verbose)
365 						fprintf (stderr,
366 							 "(exiting)");
367 					    return 0;
368 					}
369 				    }
370 
371 				    state = XDELE;
372 				    if (verbose)
373 					fprintf (stderr, "deleting... ");
374 				}
375 			    }
376 			    continue;
377 			} else
378 			    ++copy;
379 		    }
380 		    *p = '\n';
381 		    if(blank_line &&
382 		       strncmp(copy, "From ", min(p - copy + 1, 5)) == 0)
383 			write_state_add(&write_state, ">", 1);
384 		    write_state_add(&write_state, copy, p - copy + 1);
385 		    blank_line = (*copy == '\n');
386 		    rem -= p - beg + 2;
387 		    beg = p + 2;
388 		} else if (rem >= 3 && strncmp (beg, "+OK", 3) == 0) {
389 		    if (state == STAT) {
390 			if (!do_from)
391 			    write_state_add(&write_state,
392 					    from_line, from_line_length);
393 			blank_line = 0;
394 			if (do_from)
395 			    state = TOP;
396 			else
397 			    state = RETR;
398 		    } else if (state == XDELE) {
399 			state = QUIT;
400 			net_write (s, "QUIT\r\n", 6);
401 			if (verbose > 1)
402 			    net_write (STDERR_FILENO, "QUIT\r\n", 6);
403 			break;
404 		    } else if (state == DELE) {
405 			if (++deleted == count) {
406 			    state = QUIT;
407 			    net_write (s, "QUIT\r\n", 6);
408 			    if (verbose > 1)
409 				net_write (STDERR_FILENO, "QUIT\r\n", 6);
410 			    break;
411 			}
412 		    } else if (++state == STAT) {
413 			if(sscanf (beg + 4, "%u %u", &count, &bytes) != 2)
414 			    errx(1, "Bad STAT-line: %.*s", (int)(p - beg), beg);
415 			if (verbose) {
416 			    fprintf (stderr, "%u message(s) (%u bytes). "
417 				     "fetching... ",
418 				     count, bytes);
419 			    if (do_from)
420 				fprintf (stderr, "\n");
421 			} else if (do_count) {
422 			    fprintf (stderr, "%u message(s) (%u bytes).\n",
423 				     count, bytes);
424 			}
425 			if (count == 0) {
426 			    state = QUIT;
427 			    net_write (s, "QUIT\r\n", 6);
428 			    if (verbose > 1)
429 				net_write (STDERR_FILENO, "QUIT\r\n", 6);
430 			    break;
431 			}
432 		    }
433 
434 		    rem -= p - beg + 2;
435 		    beg = p + 2;
436 		} else {
437 		    if(state == XDELE) {
438 			state = DELE;
439 			rem -= p - beg + 2;
440 			beg = p + 2;
441 		    } else
442 			errx (1, "Bad response: %.*s", (int)(p - beg), beg);
443 		}
444 	    }
445 	    if (!do_from)
446 		write_state_flush (&write_state);
447 
448 	    memmove (in_buf, beg, rem);
449 	    in_len = rem;
450 	    in_ptr = in_buf + rem;
451 	}
452 	if (FD_ISSET(s, &writeset)) {
453 	    if ((state == STAT && !do_from) || state == RETR)
454 		out_len = snprintf (out_buf, sizeof(out_buf),
455 				    "RETR %u\r\n", ++asked_for);
456 	    else if ((state == STAT && do_from) || state == TOP)
457 		out_len = snprintf (out_buf, sizeof(out_buf),
458 				    "TOP %u 0\r\n", ++asked_for);
459 	    else if(state == XDELE) {
460 		out_len = snprintf(out_buf, sizeof(out_buf),
461 				   "XDELE %u %u\r\n", 1, count);
462 		sent_xdele++;
463 	    }
464 	    else if(state == DELE)
465 		out_len = snprintf (out_buf, sizeof(out_buf),
466 				    "DELE %u\r\n", ++asked_deleted);
467 	    if (net_write (s, out_buf, out_len) != out_len)
468 		err (1, "write");
469 	    if (verbose > 1)
470 		write (STDERR_FILENO, out_buf, out_len);
471 	}
472     }
473     if (verbose)
474 	fprintf (stderr, "Done\n");
475     if (do_from) {
476 	free (tmp);
477 	free (headers);
478     } else {
479 	write_state_destroy (&write_state);
480     }
481     return 0;
482 }
483 
484 #ifdef KRB5
485 static int
486 do_v5 (const char *host,
487        int port,
488        const char *user,
489        const char *filename,
490        const char *header_str,
491        int leavep,
492        int verbose,
493        int forkp)
494 {
495     krb5_error_code ret;
496     krb5_auth_context auth_context = NULL;
497     krb5_principal server;
498     int s;
499 
500     s = do_connect (host, port, 1);
501     if (s < 0)
502 	return 1;
503 
504     ret = krb5_sname_to_principal (context,
505 				   host,
506 				   "pop",
507 				   KRB5_NT_SRV_HST,
508 				   &server);
509     if (ret) {
510 	warnx ("krb5_sname_to_principal: %s",
511 	       krb5_get_err_text (context, ret));
512 	return 1;
513     }
514 
515     ret = krb5_sendauth (context,
516 			 &auth_context,
517 			 &s,
518 			 "KPOPV1.0",
519 			 NULL,
520 			 server,
521 			 0,
522 			 NULL,
523 			 NULL,
524 			 NULL,
525 			 NULL,
526 			 NULL,
527 			 NULL);
528     krb5_free_principal (context, server);
529     if (ret) {
530 	warnx ("krb5_sendauth: %s",
531 	       krb5_get_err_text (context, ret));
532 	return 1;
533     }
534     return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
535 }
536 #endif
537 
538 #ifdef KRB4
539 static int
540 do_v4 (const char *host,
541        int port,
542        const char *user,
543        const char *filename,
544        const char *header_str,
545        int leavep,
546        int verbose,
547        int forkp)
548 {
549     KTEXT_ST ticket;
550     MSG_DAT msg_data;
551     CREDENTIALS cred;
552     des_key_schedule sched;
553     int s;
554     int ret;
555 
556     s = do_connect (host, port, 1);
557     if (s < 0)
558 	return 1;
559     ret = krb_sendauth(0,
560 		       s,
561 		       &ticket,
562 		       "pop",
563 		       (char *)host,
564 		       krb_realmofhost(host),
565 		       getpid(),
566 		       &msg_data,
567 		       &cred,
568 		       sched,
569 		       NULL,
570 		       NULL,
571 		       "KPOPV0.1");
572     if(ret) {
573 	warnx("krb_sendauth: %s", krb_get_err_text(ret));
574 	return 1;
575     }
576     return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
577 }
578 #endif /* KRB4 */
579 
580 #ifdef HESIOD
581 
582 #ifdef HESIOD_INTERFACES
583 
584 static char *
585 hesiod_get_pobox (const char **user)
586 {
587     void *context;
588     struct hesiod_postoffice *hpo;
589     char *ret = NULL;
590 
591     if(hesiod_init (&context) != 0)
592 	err (1, "hesiod_init");
593 
594     hpo = hesiod_getmailhost (context, *user);
595     if (hpo == NULL) {
596 	warn ("hesiod_getmailhost %s", *user);
597     } else {
598 	if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
599 	    errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
600 
601 	ret = estrdup(hpo->hesiod_po_host);
602 	*user = estrdup(hpo->hesiod_po_name);
603 	hesiod_free_postoffice (context, hpo);
604     }
605     hesiod_end (context);
606     return ret;
607 }
608 
609 #else /* !HESIOD_INTERFACES */
610 
611 static char *
612 hesiod_get_pobox (const char **user)
613 {
614     char *ret = NULL;
615     struct hes_postoffice *hpo;
616 
617     hpo = hes_getmailhost (*user);
618     if (hpo == NULL) {
619 	warn ("hes_getmailhost %s", *user);
620     } else {
621 	if (strcasecmp(hpo->po_type, "pop") != 0)
622 	    errx (1, "Unsupported po type %s", hpo->po_type);
623 
624 	ret = estrdup(hpo->po_host);
625 	*user = estrdup(hpo->po_name);
626     }
627     return ret;
628 }
629 
630 #endif /* HESIOD_INTERFACES */
631 
632 #endif /* HESIOD */
633 
634 static char *
635 get_pobox (const char **user)
636 {
637     char *ret = NULL;
638 
639 #ifdef HESIOD
640     ret = hesiod_get_pobox (user);
641 #endif
642 
643     if (ret == NULL)
644 	ret = getenv("MAILHOST");
645     if (ret == NULL)
646 	errx (1, "MAILHOST not set");
647     return ret;
648 }
649 
650 static void
651 parse_pobox (char *a0, const char **host, const char **user)
652 {
653     const char *h, *u;
654     char *p;
655     int po = 0;
656 
657     if (a0 == NULL) {
658 
659 	*user = getenv ("USERNAME");
660 	if (*user == NULL) {
661 	    struct passwd *pwd = getpwuid (getuid ());
662 
663 	    if (pwd == NULL)
664 		errx (1, "Who are you?");
665 	    *user = estrdup (pwd->pw_name);
666 	}
667 	*host = get_pobox (user);
668 	return;
669     }
670 
671     /* if the specification starts with po:, remember this information */
672     if(strncmp(a0, "po:", 3) == 0) {
673 	a0 += 3;
674 	po++;
675     }
676     /* if there is an `@', the hostname is after it, otherwise at the
677        beginning of the string */
678     p = strchr(a0, '@');
679     if(p != NULL) {
680 	*p++ = '\0';
681 	h = p;
682     } else {
683 	h = a0;
684     }
685     /* if there is a `:', the username comes before it, otherwise at
686        the beginning of the string */
687     p = strchr(a0, ':');
688     if(p != NULL) {
689 	*p++ = '\0';
690 	u = p;
691     } else {
692 	u = a0;
693     }
694     if(h == u) {
695 	/* some inconsistent compatibility with various mailers */
696 	if(po) {
697 	    h = get_pobox (&u);
698 	} else {
699 	    u = get_default_username ();
700 	    if (u == NULL)
701 		errx (1, "Who are you?");
702 	}
703     }
704     *host = h;
705     *user = u;
706 }
707 
708 int
709 main(int argc, char **argv)
710 {
711     int port = 0;
712     int optind = 0;
713     int ret = 1;
714     const char *host, *user, *filename = NULL;
715     char *pobox = NULL;
716 
717     setprogname (argv[0]);
718 
719 #ifdef KRB5
720     {
721 	krb5_error_code ret;
722 
723 	ret = krb5_init_context (&context);
724 	if (ret)
725 	    errx (1, "krb5_init_context failed: %d", ret);
726     }
727 #endif
728 
729     if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
730 		&optind))
731 	usage (1);
732 
733     argc -= optind;
734     argv += optind;
735 
736 #if defined(KRB4) && defined(KRB5)
737     if(use_v4 == -1 && use_v5 == 1)
738 	use_v4 = 0;
739     if(use_v5 == -1 && use_v4 == 1)
740 	use_v5 = 0;
741 #endif
742 
743     if (do_help)
744 	usage (0);
745 
746     if (do_version) {
747 	print_version(NULL);
748 	return 0;
749     }
750 
751     if (do_from && header_str == NULL)
752 	header_str = "From:";
753     else if (header_str != NULL)
754 	do_from = 1;
755 
756     if (do_from) {
757 	if (argc == 0)
758 	    pobox = NULL;
759 	else if (argc == 1)
760 	    pobox = argv[0];
761 	else
762 	    usage (1);
763     } else {
764 	if (argc == 1) {
765 	    filename = argv[0];
766 	    pobox    = NULL;
767 	} else if (argc == 2) {
768 	    filename = argv[1];
769 	    pobox    = argv[0];
770 	} else
771 	    usage (1);
772     }
773 
774     if (port_str) {
775 	struct servent *s = roken_getservbyname (port_str, "tcp");
776 
777 	if (s)
778 	    port = s->s_port;
779 	else {
780 	    char *ptr;
781 
782 	    port = strtol (port_str, &ptr, 10);
783 	    if (port == 0 && ptr == port_str)
784 		errx (1, "Bad port `%s'", port_str);
785 	    port = htons(port);
786 	}
787     }
788     if (port == 0) {
789 #ifdef KRB5
790 	port = krb5_getportbyname (context, "kpop", "tcp", 1109);
791 #elif defined(KRB4)
792 	port = k_getportbyname ("kpop", "tcp", htons(1109));
793 #else
794 #error must define KRB4 or KRB5
795 #endif
796     }
797 
798     parse_pobox (pobox, &host, &user);
799 
800 #ifdef KRB5
801     if (ret && use_v5) {
802 	ret = do_v5 (host, port, user, filename, header_str,
803 		     do_leave, verbose_level, do_fork);
804     }
805 #endif
806 
807 #ifdef KRB4
808     if (ret && use_v4) {
809 	ret = do_v4 (host, port, user, filename, header_str,
810 		     do_leave, verbose_level, do_fork);
811     }
812 #endif /* KRB4 */
813     return ret;
814 }
815