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"
38 static int use_v5
= -1;
39 static krb5_context context
;
42 static char *port_str
;
43 static int verbose_level
;
46 static int do_version
;
50 static char *header_str
;
52 struct getargs args
[] = {
54 { "krb5", '5', arg_flag
, &use_v5
, "Use Kerberos V5",
57 { "verbose",'v', arg_counter
, &verbose_level
, "Verbose",
59 { "fork", 'f', arg_flag
, &do_fork
, "Fork deleting proc",
61 { "leave", 'l', arg_flag
, &do_leave
, "Leave mail on server",
63 { "port", 'p', arg_string
, &port_str
, "Use this port",
64 "number-or-service" },
65 { "from", 0, arg_flag
, &do_from
, "Behave like from",
67 { "headers", 0, arg_string
, &header_str
, "Headers to print", NULL
},
68 { "count", 'c', arg_flag
, &do_count
, "Print number of messages", NULL
},
69 { "version", 0, arg_flag
, &do_version
, "Print version",
71 { "help", 0, arg_flag
, &do_help
, NULL
,
80 sizeof(args
) / sizeof(args
[0]),
82 "[[{po:username[@hostname] | hostname[:username]}] ...] "
88 do_connect (const char *hostname
, int port
, int nodelay
)
90 struct addrinfo
*ai
, *a
;
91 struct addrinfo hints
;
94 char portstr
[NI_MAXSERV
];
96 memset (&hints
, 0, sizeof(hints
));
97 hints
.ai_socktype
= SOCK_STREAM
;
98 hints
.ai_protocol
= IPPROTO_TCP
;
100 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(port
));
102 error
= getaddrinfo (hostname
, portstr
, &hints
, &ai
);
104 errx (1, "getaddrinfo(%s): %s", hostname
, gai_strerror(error
));
106 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
107 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
110 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
111 warn ("connect(%s)", hostname
);
119 warnx ("failed to contact %s", hostname
);
123 if(setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
,
124 (void *)&nodelay
, sizeof(nodelay
)) < 0)
125 err (1, "setsockopt TCP_NODELAY");
129 typedef enum { INIT
= 0, GREET
, USER
, PASS
, STAT
, RETR
, TOP
,
130 DELE
, XDELE
, QUIT
} pop_state
;
132 static char *pop_state_string
[] = {
133 "INIT", "GREET", "USER", "PASS", "STAT", "RETR", "TOP",
134 "DELE", "XDELE", "QUIT"
137 #define PUSH_BUFSIZ 65536
142 struct iovec
*iovecs
;
143 size_t niovecs
, maxiovecs
, allociovecs
;
148 write_state_init (struct write_state
*w
, int fd
)
151 w
->maxiovecs
= UIO_MAXIOV
;
155 w
->allociovecs
= min(STEP
, w
->maxiovecs
);
157 w
->iovecs
= emalloc(w
->allociovecs
* sizeof(*w
->iovecs
));
162 write_state_add (struct write_state
*w
, void *v
, size_t len
)
164 if(w
->niovecs
== w
->allociovecs
) {
165 if(w
->niovecs
== w
->maxiovecs
) {
166 if(writev (w
->fd
, w
->iovecs
, w
->niovecs
) < 0)
170 w
->allociovecs
= min(w
->allociovecs
+ STEP
, w
->maxiovecs
);
171 w
->iovecs
= erealloc (w
->iovecs
,
172 w
->allociovecs
* sizeof(*w
->iovecs
));
175 w
->iovecs
[w
->niovecs
].iov_base
= v
;
176 w
->iovecs
[w
->niovecs
].iov_len
= len
;
181 write_state_flush (struct write_state
*w
)
184 if (writev (w
->fd
, w
->iovecs
, w
->niovecs
) < 0)
191 write_state_destroy (struct write_state
*w
)
200 const char *outfilename
,
201 const char *header_str
,
207 char out_buf
[PUSH_BUFSIZ
];
213 pop_state state
= INIT
;
214 unsigned count
= 0, bytes
;
215 unsigned asked_for
= 0, retrieved
= 0, asked_deleted
= 0, deleted
= 0;
216 unsigned sent_xdele
= 0;
219 size_t from_line_length
;
221 struct write_state write_state
;
222 unsigned int numheaders
= 1;
223 char **headers
= NULL
;
227 in_buf
= emalloc(PUSH_BUFSIZ
+ 1);
229 in_buf_size
= PUSH_BUFSIZ
;
234 tmp2
= tmp
= estrdup(header_str
);
238 fprintf (stderr
, "%s@%s\n", user
, host
);
239 while (*tmp
!= '\0') {
240 tmp
= strchr(tmp
, ',');
247 headers
= emalloc(sizeof(char *) * (numheaders
+ 1));
248 for (i
= 0; i
< numheaders
; i
++) {
249 headers
[i
] = strtok_r(tmp2
, ",", &tmp2
);
251 headers
[numheaders
] = NULL
;
253 out_fd
= open(outfilename
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
255 err (1, "open %s", outfilename
);
257 fprintf (stderr
, "%s@%s -> %s\n", user
, host
, outfilename
);
261 from_line_length
= snprintf (from_line
, sizeof(from_line
),
262 "From %s %s", "push", ctime(&now
));
263 if (from_line_length
< 0 || from_line_length
> sizeof(from_line
))
264 errx (1, "snprintf failed");
266 out_len
= snprintf (out_buf
, sizeof(out_buf
),
267 "USER %s\r\nPASS hej\r\nSTAT\r\n",
269 if (out_len
< 0 || out_len
> sizeof(out_buf
))
270 errx (1, "snprintf failed");
271 if (net_write (s
, out_buf
, out_len
) != out_len
)
274 fprintf (stderr
, "%s", out_buf
);
277 write_state_init (&write_state
, out_fd
);
279 while(state
!= QUIT
) {
280 fd_set readset
, writeset
;
285 errx (1, "fd too large");
289 fprintf (stderr
, "state: %s count: %d asked_for: %d "
290 "retrieved: %d asked_deleted: %d\n",
291 pop_state_string
[state
],
292 count
, asked_for
, retrieved
, asked_deleted
);
294 if (((state
== STAT
|| state
== RETR
|| state
== TOP
)
295 && asked_for
< count
)
296 || (state
== XDELE
&& !sent_xdele
)
297 || (state
== DELE
&& asked_deleted
< count
))
299 ret
= select (s
+ 1, &readset
, &writeset
, NULL
, NULL
);
307 if (FD_ISSET(s
, &readset
)) {
312 if(in_len
>= in_buf_size
) {
313 char *tmp
= erealloc(in_buf
, in_buf_size
+ PUSH_BUFSIZ
+ 1);
314 in_ptr
= tmp
+ (in_ptr
- in_buf
);
316 in_buf_size
+= PUSH_BUFSIZ
;
319 ret
= read (s
, in_ptr
, in_buf_size
- in_len
);
323 errx (1, "EOF during read");
332 && (p
= strstr(beg
, "\r\n")) != NULL
) {
336 for (i
= 0; i
< numheaders
; i
++) {
339 len
= min(p
- copy
+ 1, strlen(headers
[i
]));
340 if (strncasecmp(copy
, headers
[i
], len
) == 0) {
341 fprintf (stdout
, "%.*s\n", (int)(p
- copy
), copy
);
344 if (beg
[0] == '.' && beg
[1] == '\r' && beg
[2] == '\n') {
346 fprintf (stdout
, "\n");
348 if (++retrieved
== count
) {
350 net_write (s
, "QUIT\r\n", 6);
352 fprintf (stderr
, "QUIT\r\n");
357 } else if (state
== RETR
) {
360 if (beg
[1] == '\r' && beg
[2] == '\n') {
362 write_state_add(&write_state
, "\n", 1);
366 if (++retrieved
== count
) {
367 write_state_flush (&write_state
);
368 if (fsync (out_fd
) < 0)
373 net_write (s
, "QUIT\r\n", 6);
375 fprintf (stderr
, "QUIT\r\n");
393 fprintf (stderr
, "deleting... ");
402 strncmp(copy
, "From ", min(p
- copy
+ 1, 5)) == 0)
403 write_state_add(&write_state
, ">", 1);
404 write_state_add(&write_state
, copy
, p
- copy
+ 1);
405 blank_line
= (*copy
== '\n');
408 } else if (rem
>= 3 && strncmp (beg
, "+OK", 3) == 0) {
411 write_state_add(&write_state
,
412 from_line
, from_line_length
);
418 } else if (state
== XDELE
) {
420 net_write (s
, "QUIT\r\n", 6);
422 fprintf (stderr
, "QUIT\r\n");
424 } else if (state
== DELE
) {
425 if (++deleted
== count
) {
427 net_write (s
, "QUIT\r\n", 6);
429 fprintf (stderr
, "QUIT\r\n");
432 } else if (++state
== STAT
) {
433 if(sscanf (beg
+ 4, "%u %u", &count
, &bytes
) != 2)
434 errx(1, "Bad STAT-line: %.*s", (int)(p
- beg
), beg
);
436 fprintf (stderr
, "%u message(s) (%u bytes). "
440 fprintf (stderr
, "\n");
441 } else if (do_count
) {
442 fprintf (stderr
, "%u message(s) (%u bytes).\n",
447 net_write (s
, "QUIT\r\n", 6);
449 fprintf (stderr
, "QUIT\r\n");
462 errx (1, "Bad response: %.*s", (int)(p
- beg
), beg
);
466 write_state_flush (&write_state
);
468 memmove (in_buf
, beg
, rem
);
470 in_ptr
= in_buf
+ rem
;
472 if (FD_ISSET(s
, &writeset
)) {
473 if ((state
== STAT
&& !do_from
) || state
== RETR
)
474 out_len
= snprintf (out_buf
, sizeof(out_buf
),
475 "RETR %u\r\n", ++asked_for
);
476 else if ((state
== STAT
&& do_from
) || state
== TOP
)
477 out_len
= snprintf (out_buf
, sizeof(out_buf
),
478 "TOP %u 0\r\n", ++asked_for
);
479 else if(state
== XDELE
) {
480 out_len
= snprintf(out_buf
, sizeof(out_buf
),
481 "XDELE %u %u\r\n", 1, count
);
484 else if(state
== DELE
)
485 out_len
= snprintf (out_buf
, sizeof(out_buf
),
486 "DELE %u\r\n", ++asked_deleted
);
487 if (out_len
< 0 || out_len
> sizeof(out_buf
))
488 errx (1, "snprintf failed");
489 if (net_write (s
, out_buf
, out_len
) != out_len
)
492 fprintf (stderr
, "%s", out_buf
);
496 fprintf (stderr
, "Done\n");
501 write_state_destroy (&write_state
);
508 do_v5 (const char *host
,
511 const char *filename
,
512 const char *header_str
,
518 krb5_auth_context auth_context
= NULL
;
519 krb5_principal server
;
522 s
= do_connect (host
, port
, 1);
526 ret
= krb5_sname_to_principal (context
,
532 warnx ("krb5_sname_to_principal: %s",
533 krb5_get_err_text (context
, ret
));
537 ret
= krb5_sendauth (context
,
550 krb5_free_principal (context
, server
);
552 warnx ("krb5_sendauth: %s",
553 krb5_get_err_text (context
, ret
));
556 return doit (s
, host
, user
, filename
, header_str
, leavep
, verbose
, forkp
);
562 #ifdef HESIOD_INTERFACES
565 hesiod_get_pobox (const char **user
)
568 struct hesiod_postoffice
*hpo
;
571 if(hesiod_init (&context
) != 0)
572 err (1, "hesiod_init");
574 hpo
= hesiod_getmailhost (context
, *user
);
576 warn ("hesiod_getmailhost %s", *user
);
578 if (strcasecmp(hpo
->hesiod_po_type
, "pop") != 0)
579 errx (1, "Unsupported po type %s", hpo
->hesiod_po_type
);
581 ret
= estrdup(hpo
->hesiod_po_host
);
582 *user
= estrdup(hpo
->hesiod_po_name
);
583 hesiod_free_postoffice (context
, hpo
);
585 hesiod_end (context
);
589 #else /* !HESIOD_INTERFACES */
592 hesiod_get_pobox (const char **user
)
595 struct hes_postoffice
*hpo
;
597 hpo
= hes_getmailhost (*user
);
599 warn ("hes_getmailhost %s", *user
);
601 if (strcasecmp(hpo
->po_type
, "pop") != 0)
602 errx (1, "Unsupported po type %s", hpo
->po_type
);
604 ret
= estrdup(hpo
->po_host
);
605 *user
= estrdup(hpo
->po_name
);
610 #endif /* HESIOD_INTERFACES */
615 get_pobox (const char **user
)
620 ret
= hesiod_get_pobox (user
);
624 ret
= getenv("MAILHOST");
626 errx (1, "MAILHOST not set");
631 parse_pobox (char *a0
, const char **host
, const char **user
)
639 *user
= getenv ("USERNAME");
641 struct passwd
*pwd
= getpwuid (getuid ());
644 errx (1, "Who are you?");
645 *user
= estrdup (pwd
->pw_name
);
647 *host
= get_pobox (user
);
651 /* if the specification starts with po:, remember this information */
652 if(strncmp(a0
, "po:", 3) == 0) {
656 /* if there is an `@', the hostname is after it, otherwise at the
657 beginning of the string */
665 /* if there is a `:', the username comes before it, otherwise at
666 the beginning of the string */
675 /* some inconsistent compatibility with various mailers */
679 u
= get_default_username ();
681 errx (1, "Who are you?");
689 main(int argc
, char **argv
)
694 const char *host
, *user
, *filename
= NULL
;
697 setprogname (argv
[0]);
703 ret
= krb5_init_context (&context
);
705 errx (1, "krb5_init_context failed: %d", ret
);
709 if (getarg (args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
,
724 if (do_from
&& header_str
== NULL
)
725 header_str
= "From:";
726 else if (header_str
!= NULL
)
740 } else if (argc
== 2) {
748 struct servent
*s
= roken_getservbyname (port_str
, "tcp");
755 port
= strtol (port_str
, &ptr
, 10);
756 if (port
== 0 && ptr
== port_str
)
757 errx (1, "Bad port `%s'", port_str
);
763 port
= krb5_getportbyname (context
, "kpop", "tcp", 1109);
765 #error must define KRB5
769 parse_pobox (pobox
, &host
, &user
);
773 ret
= do_v5 (host
, port
, user
, filename
, header_str
,
774 do_leave
, verbose_level
, do_fork
);