2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "push_locl.h"
37 #if defined(_AIX) && defined(STAT)
39 * AIX defines STAT to 1 in sys/dir.h
45 static int use_v5 = -1;
46 static krb5_context context;
49 static char *port_str;
50 static int verbose_level;
53 static int do_version;
57 static char *header_str;
59 struct getargs args[] = {
61 { "krb5", '5', arg_flag, &use_v5, "Use Kerberos V5",
64 { "verbose",'v', arg_counter, &verbose_level, "Verbose",
66 { "fork", 'f', arg_flag, &do_fork, "Fork deleting proc",
68 { "leave", 'l', arg_flag, &do_leave, "Leave mail on server",
70 { "port", 'p', arg_string, &port_str, "Use this port",
71 "number-or-service" },
72 { "from", 0, arg_flag, &do_from, "Behave like from",
74 { "headers", 0, arg_string, &header_str, "Headers to print", NULL },
75 { "count", 'c', arg_flag, &do_count, "Print number of messages", NULL},
76 { "version", 0, arg_flag, &do_version, "Print version",
78 { "help", 0, arg_flag, &do_help, NULL,
87 sizeof(args) / sizeof(args[0]),
89 "[[{po:username[@hostname] | hostname[:username]}] ...] "
95 do_connect (const char *hostname, int port, int nodelay)
97 struct addrinfo *ai, *a;
98 struct addrinfo hints;
101 char portstr[NI_MAXSERV];
103 memset (&hints, 0, sizeof(hints));
104 hints.ai_socktype = SOCK_STREAM;
105 hints.ai_protocol = IPPROTO_TCP;
107 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
109 error = getaddrinfo (hostname, portstr, &hints, &ai);
111 errx (1, "getaddrinfo(%s): %s", hostname, gai_strerror(error));
113 for (a = ai; a != NULL; a = a->ai_next) {
114 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
117 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
118 warn ("connect(%s)", hostname);
126 warnx ("failed to contact %s", hostname);
130 if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
131 (void *)&nodelay, sizeof(nodelay)) < 0)
132 err (1, "setsockopt TCP_NODELAY");
136 typedef enum { INIT = 0, GREET, USER, PASS, STAT, RETR, TOP,
137 DELE, XDELE, QUIT} pop_state;
139 static char *pop_state_string[] = {
140 "INIT", "GREET", "USER", "PASS", "STAT", "RETR", "TOP",
141 "DELE", "XDELE", "QUIT"
144 #define PUSH_BUFSIZ 65536
149 struct iovec *iovecs;
150 size_t niovecs, maxiovecs, allociovecs;
155 write_state_init (struct write_state *w, int fd)
158 w->maxiovecs = UIO_MAXIOV;
162 w->allociovecs = min(STEP, w->maxiovecs);
164 w->iovecs = emalloc(w->allociovecs * sizeof(*w->iovecs));
169 write_state_add (struct write_state *w, void *v, size_t len)
171 if(w->niovecs == w->allociovecs) {
172 if(w->niovecs == w->maxiovecs) {
173 if(writev (w->fd, w->iovecs, w->niovecs) < 0)
177 w->allociovecs = min(w->allociovecs + STEP, w->maxiovecs);
178 w->iovecs = erealloc (w->iovecs,
179 w->allociovecs * sizeof(*w->iovecs));
182 w->iovecs[w->niovecs].iov_base = v;
183 w->iovecs[w->niovecs].iov_len = len;
188 write_state_flush (struct write_state *w)
191 if (writev (w->fd, w->iovecs, w->niovecs) < 0)
198 write_state_destroy (struct write_state *w)
207 const char *outfilename,
208 const char *header_str,
214 char out_buf[PUSH_BUFSIZ];
220 pop_state state = INIT;
221 unsigned count = 0, bytes;
222 unsigned asked_for = 0, retrieved = 0, asked_deleted = 0, deleted = 0;
223 unsigned sent_xdele = 0;
226 size_t from_line_length;
228 struct write_state write_state;
229 unsigned int numheaders = 1;
230 char **headers = NULL;
234 in_buf = emalloc(PUSH_BUFSIZ + 1);
236 in_buf_size = PUSH_BUFSIZ;
241 tmp2 = tmp = estrdup(header_str);
245 fprintf (stderr, "%s@%s\n", user, host);
246 while (*tmp != '\0') {
247 tmp = strchr(tmp, ',');
254 headers = emalloc(sizeof(char *) * (numheaders + 1));
255 for (i = 0; i < numheaders; i++) {
256 headers[i] = strtok_r(tmp2, ",", &tmp2);
258 headers[numheaders] = NULL;
260 out_fd = open(outfilename, O_WRONLY | O_APPEND | O_CREAT, 0666);
262 err (1, "open %s", outfilename);
264 fprintf (stderr, "%s@%s -> %s\n", user, host, outfilename);
268 from_line_length = snprintf (from_line, sizeof(from_line),
269 "From %s %s", "push", ctime(&now));
270 if (from_line_length < 0 || from_line_length > sizeof(from_line))
271 errx (1, "snprintf failed");
273 out_len = snprintf (out_buf, sizeof(out_buf),
274 "USER %s\r\nPASS hej\r\nSTAT\r\n",
276 if (out_len < 0 || out_len > sizeof(out_buf))
277 errx (1, "snprintf failed");
278 if (net_write (s, out_buf, out_len) != out_len)
281 fprintf (stderr, "%s", out_buf);
284 write_state_init (&write_state, out_fd);
286 while(state != QUIT) {
287 fd_set readset, writeset;
292 errx (1, "fd too large");
296 fprintf (stderr, "state: %s count: %d asked_for: %d "
297 "retrieved: %d asked_deleted: %d\n",
298 pop_state_string[state],
299 count, asked_for, retrieved, asked_deleted);
301 if (((state == STAT || state == RETR || state == TOP)
302 && asked_for < count)
303 || (state == XDELE && !sent_xdele)
304 || (state == DELE && asked_deleted < count))
306 ret = select (s + 1, &readset, &writeset, NULL, NULL);
314 if (FD_ISSET(s, &readset)) {
319 if(in_len >= in_buf_size) {
320 char *tmp = erealloc(in_buf, in_buf_size + PUSH_BUFSIZ + 1);
321 in_ptr = tmp + (in_ptr - in_buf);
323 in_buf_size += PUSH_BUFSIZ;
326 ret = read (s, in_ptr, in_buf_size - in_len);
330 errx (1, "EOF during read");
339 && (p = strstr(beg, "\r\n")) != NULL) {
343 for (i = 0; i < numheaders; i++) {
346 len = min(p - copy + 1, strlen(headers[i]));
347 if (strncasecmp(copy, headers[i], len) == 0) {
348 fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
351 if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
353 fprintf (stdout, "\n");
355 if (++retrieved == count) {
357 net_write (s, "QUIT\r\n", 6);
359 fprintf (stderr, "QUIT\r\n");
364 } else if (state == RETR) {
367 if (beg[1] == '\r' && beg[2] == '\n') {
369 write_state_add(&write_state, "\n", 1);
373 if (++retrieved == count) {
374 write_state_flush (&write_state);
375 if (fsync (out_fd) < 0)
380 net_write (s, "QUIT\r\n", 6);
382 fprintf (stderr, "QUIT\r\n");
400 fprintf (stderr, "deleting... ");
409 strncmp(copy, "From ", min(p - copy + 1, 5)) == 0)
410 write_state_add(&write_state, ">", 1);
411 write_state_add(&write_state, copy, p - copy + 1);
412 blank_line = (*copy == '\n');
415 } else if (rem >= 3 && strncmp (beg, "+OK", 3) == 0) {
418 write_state_add(&write_state,
419 from_line, from_line_length);
425 } else if (state == XDELE) {
427 net_write (s, "QUIT\r\n", 6);
429 fprintf (stderr, "QUIT\r\n");
431 } else if (state == DELE) {
432 if (++deleted == count) {
434 net_write (s, "QUIT\r\n", 6);
436 fprintf (stderr, "QUIT\r\n");
439 } else if (++state == STAT) {
440 if(sscanf (beg + 4, "%u %u", &count, &bytes) != 2)
441 errx(1, "Bad STAT-line: %.*s", (int)(p - beg), beg);
443 fprintf (stderr, "%u message(s) (%u bytes). "
447 fprintf (stderr, "\n");
448 } else if (do_count) {
449 fprintf (stderr, "%u message(s) (%u bytes).\n",
454 net_write (s, "QUIT\r\n", 6);
456 fprintf (stderr, "QUIT\r\n");
469 errx (1, "Bad response: %.*s", (int)(p - beg), beg);
473 write_state_flush (&write_state);
475 memmove (in_buf, beg, rem);
477 in_ptr = in_buf + rem;
479 if (FD_ISSET(s, &writeset)) {
480 if ((state == STAT && !do_from) || state == RETR)
481 out_len = snprintf (out_buf, sizeof(out_buf),
482 "RETR %u\r\n", ++asked_for);
483 else if ((state == STAT && do_from) || state == TOP)
484 out_len = snprintf (out_buf, sizeof(out_buf),
485 "TOP %u 0\r\n", ++asked_for);
486 else if(state == XDELE) {
487 out_len = snprintf(out_buf, sizeof(out_buf),
488 "XDELE %u %u\r\n", 1, count);
491 else if(state == DELE)
492 out_len = snprintf (out_buf, sizeof(out_buf),
493 "DELE %u\r\n", ++asked_deleted);
494 if (out_len < 0 || out_len > sizeof(out_buf))
495 errx (1, "snprintf failed");
496 if (net_write (s, out_buf, out_len) != out_len)
499 fprintf (stderr, "%s", out_buf);
503 fprintf (stderr, "Done\n");
508 write_state_destroy (&write_state);
515 do_v5 (const char *host,
518 const char *filename,
519 const char *header_str,
525 krb5_auth_context auth_context = NULL;
526 krb5_principal server;
529 s = do_connect (host, port, 1);
533 ret = krb5_sname_to_principal (context,
539 warnx ("krb5_sname_to_principal: %s",
540 krb5_get_err_text (context, ret));
544 ret = krb5_sendauth (context,
557 krb5_free_principal (context, server);
559 warnx ("krb5_sendauth: %s",
560 krb5_get_err_text (context, ret));
563 return doit (s, host, user, filename, header_str, leavep, verbose, forkp);
569 #ifdef HESIOD_INTERFACES
572 hesiod_get_pobox (const char **user)
575 struct hesiod_postoffice *hpo;
578 if(hesiod_init (&context) != 0)
579 err (1, "hesiod_init");
581 hpo = hesiod_getmailhost (context, *user);
583 warn ("hesiod_getmailhost %s", *user);
585 if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
586 errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
588 ret = estrdup(hpo->hesiod_po_host);
589 *user = estrdup(hpo->hesiod_po_name);
590 hesiod_free_postoffice (context, hpo);
592 hesiod_end (context);
596 #else /* !HESIOD_INTERFACES */
599 hesiod_get_pobox (const char **user)
602 struct hes_postoffice *hpo;
604 hpo = hes_getmailhost (*user);
606 warn ("hes_getmailhost %s", *user);
608 if (strcasecmp(hpo->po_type, "pop") != 0)
609 errx (1, "Unsupported po type %s", hpo->po_type);
611 ret = estrdup(hpo->po_host);
612 *user = estrdup(hpo->po_name);
617 #endif /* HESIOD_INTERFACES */
622 get_pobox (const char **user)
627 ret = hesiod_get_pobox (user);
631 ret = getenv("MAILHOST");
633 errx (1, "MAILHOST not set");
638 parse_pobox (char *a0, const char **host, const char **user)
646 *user = getenv ("USERNAME");
648 struct passwd *pwd = getpwuid (getuid ());
651 errx (1, "Who are you?");
652 *user = estrdup (pwd->pw_name);
654 *host = get_pobox (user);
658 /* if the specification starts with po:, remember this information */
659 if(strncmp(a0, "po:", 3) == 0) {
663 /* if there is an `@', the hostname is after it, otherwise at the
664 beginning of the string */
672 /* if there is a `:', the username comes before it, otherwise at
673 the beginning of the string */
682 /* some inconsistent compatibility with various mailers */
686 u = get_default_username ();
688 errx (1, "Who are you?");
696 main(int argc, char **argv)
701 const char *host, *user, *filename = NULL;
704 setprogname (argv[0]);
710 ret = krb5_init_context (&context);
712 errx (1, "krb5_init_context failed: %d", ret);
716 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
731 if (do_from && header_str == NULL)
732 header_str = "From:";
733 else if (header_str != NULL)
747 } else if (argc == 2) {
755 struct servent *s = roken_getservbyname (port_str, "tcp");
762 port = strtol (port_str, &ptr, 10);
763 if (port == 0 && ptr == port_str)
764 errx (1, "Bad port `%s'", port_str);
770 port = krb5_getportbyname (context, "kpop", "tcp", 1109);
772 #error must define KRB5
776 parse_pobox (pobox, &host, &user);
780 ret = do_v5 (host, port, user, filename, header_str,
781 do_leave, verbose_level, do_fork);