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_v4
= -1;
42 static int use_v5
= -1;
43 static krb5_context context
;
46 static char *port_str
;
47 static int verbose_level
;
50 static int do_version
;
54 static char *header_str
;
56 struct getargs args
[] = {
58 { "krb4", '4', arg_flag
, &use_v4
, "Use Kerberos V4",
62 { "krb5", '5', arg_flag
, &use_v5
, "Use Kerberos V5",
65 { "verbose",'v', arg_counter
, &verbose_level
, "Verbose",
67 { "fork", 'f', arg_flag
, &do_fork
, "Fork deleting proc",
69 { "leave", 'l', arg_flag
, &do_leave
, "Leave mail on server",
71 { "port", 'p', arg_string
, &port_str
, "Use this port",
72 "number-or-service" },
73 { "from", 0, arg_flag
, &do_from
, "Behave like from",
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",
79 { "help", 0, arg_flag
, &do_help
, NULL
,
88 sizeof(args
) / sizeof(args
[0]),
90 "[[{po:username[@hostname] | hostname[:username]}] ...] "
96 do_connect (const char *hostname
, int port
, int nodelay
)
98 struct addrinfo
*ai
, *a
;
99 struct addrinfo hints
;
102 char portstr
[NI_MAXSERV
];
104 memset (&hints
, 0, sizeof(hints
));
105 hints
.ai_socktype
= SOCK_STREAM
;
106 hints
.ai_protocol
= IPPROTO_TCP
;
108 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(port
));
110 error
= getaddrinfo (hostname
, portstr
, &hints
, &ai
);
112 errx (1, "getaddrinfo(%s): %s", hostname
, gai_strerror(error
));
114 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
115 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
118 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
119 warn ("connect(%s)", hostname
);
127 warnx ("failed to contact %s", hostname
);
131 if(setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
,
132 (void *)&nodelay
, sizeof(nodelay
)) < 0)
133 err (1, "setsockopt TCP_NODELAY");
137 typedef enum { INIT
= 0, GREET
, USER
, PASS
, STAT
, RETR
, TOP
,
138 DELE
, XDELE
, QUIT
} pop_state
;
140 static char *pop_state_string
[] = {
141 "INIT", "GREET", "USER", "PASS", "STAT", "RETR", "TOP",
142 "DELE", "XDELE", "QUIT"
145 #define PUSH_BUFSIZ 65536
150 struct iovec
*iovecs
;
151 size_t niovecs
, maxiovecs
, allociovecs
;
156 write_state_init (struct write_state
*w
, int fd
)
159 w
->maxiovecs
= UIO_MAXIOV
;
163 w
->allociovecs
= min(STEP
, w
->maxiovecs
);
165 w
->iovecs
= emalloc(w
->allociovecs
* sizeof(*w
->iovecs
));
170 write_state_add (struct write_state
*w
, void *v
, size_t len
)
172 if(w
->niovecs
== w
->allociovecs
) {
173 if(w
->niovecs
== w
->maxiovecs
) {
174 if(writev (w
->fd
, w
->iovecs
, w
->niovecs
) < 0)
178 w
->allociovecs
= min(w
->allociovecs
+ STEP
, w
->maxiovecs
);
179 w
->iovecs
= erealloc (w
->iovecs
,
180 w
->allociovecs
* sizeof(*w
->iovecs
));
183 w
->iovecs
[w
->niovecs
].iov_base
= v
;
184 w
->iovecs
[w
->niovecs
].iov_len
= len
;
189 write_state_flush (struct write_state
*w
)
192 if (writev (w
->fd
, w
->iovecs
, w
->niovecs
) < 0)
199 write_state_destroy (struct write_state
*w
)
208 const char *outfilename
,
209 const char *header_str
,
215 char out_buf
[PUSH_BUFSIZ
];
221 pop_state state
= INIT
;
222 unsigned count
, bytes
;
223 unsigned asked_for
= 0, retrieved
= 0, asked_deleted
= 0, deleted
= 0;
224 unsigned sent_xdele
= 0;
227 size_t from_line_length
;
229 struct write_state write_state
;
230 unsigned int numheaders
= 1;
231 char **headers
= NULL
;
235 in_buf
= emalloc(PUSH_BUFSIZ
+ 1);
237 in_buf_size
= PUSH_BUFSIZ
;
242 tmp2
= tmp
= estrdup(header_str
);
246 fprintf (stderr
, "%s@%s\n", user
, host
);
247 while (*tmp
!= '\0') {
248 tmp
= strchr(tmp
, ',');
255 headers
= emalloc(sizeof(char *) * (numheaders
+ 1));
256 for (i
= 0; i
< numheaders
; i
++) {
257 headers
[i
] = strtok_r(tmp2
, ",", &tmp2
);
259 headers
[numheaders
] = NULL
;
261 out_fd
= open(outfilename
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
263 err (1, "open %s", outfilename
);
265 fprintf (stderr
, "%s@%s -> %s\n", user
, host
, outfilename
);
269 from_line_length
= snprintf (from_line
, sizeof(from_line
),
270 "From %s %s", "push", ctime(&now
));
271 if (from_line_length
< 0 || from_line_length
> sizeof(from_line
))
272 errx (1, "snprintf failed");
274 out_len
= snprintf (out_buf
, sizeof(out_buf
),
275 "USER %s\r\nPASS hej\r\nSTAT\r\n",
277 if (out_len
< 0 || out_len
> sizeof(out_buf
))
278 errx (1, "snprintf failed");
279 if (net_write (s
, out_buf
, out_len
) != out_len
)
282 fprintf (stderr
, "%s", out_buf
);
285 write_state_init (&write_state
, out_fd
);
287 while(state
!= QUIT
) {
288 fd_set readset
, writeset
;
293 errx (1, "fd too large");
297 fprintf (stderr
, "state: %s count: %d asked_for: %d "
298 "retrieved: %d asked_deleted: %d\n",
299 pop_state_string
[state
],
300 count
, asked_for
, retrieved
, asked_deleted
);
302 if (((state
== STAT
|| state
== RETR
|| state
== TOP
)
303 && asked_for
< count
)
304 || (state
== XDELE
&& !sent_xdele
)
305 || (state
== DELE
&& asked_deleted
< count
))
307 ret
= select (s
+ 1, &readset
, &writeset
, NULL
, NULL
);
315 if (FD_ISSET(s
, &readset
)) {
320 if(in_len
>= in_buf_size
) {
321 char *tmp
= erealloc(in_buf
, in_buf_size
+ PUSH_BUFSIZ
+ 1);
322 in_ptr
= tmp
+ (in_ptr
- in_buf
);
324 in_buf_size
+= PUSH_BUFSIZ
;
327 ret
= read (s
, in_ptr
, in_buf_size
- in_len
);
331 errx (1, "EOF during read");
340 && (p
= strstr(beg
, "\r\n")) != NULL
) {
344 for (i
= 0; i
< numheaders
; i
++) {
347 len
= min(p
- copy
+ 1, strlen(headers
[i
]));
348 if (strncasecmp(copy
, headers
[i
], len
) == 0) {
349 fprintf (stdout
, "%.*s\n", (int)(p
- copy
), copy
);
352 if (beg
[0] == '.' && beg
[1] == '\r' && beg
[2] == '\n') {
354 fprintf (stdout
, "\n");
356 if (++retrieved
== count
) {
358 net_write (s
, "QUIT\r\n", 6);
360 fprintf (stderr
, "QUIT\r\n");
365 } else if (state
== RETR
) {
368 if (beg
[1] == '\r' && beg
[2] == '\n') {
370 write_state_add(&write_state
, "\n", 1);
374 if (++retrieved
== count
) {
375 write_state_flush (&write_state
);
376 if (fsync (out_fd
) < 0)
381 net_write (s
, "QUIT\r\n", 6);
383 fprintf (stderr
, "QUIT\r\n");
401 fprintf (stderr
, "deleting... ");
410 strncmp(copy
, "From ", min(p
- copy
+ 1, 5)) == 0)
411 write_state_add(&write_state
, ">", 1);
412 write_state_add(&write_state
, copy
, p
- copy
+ 1);
413 blank_line
= (*copy
== '\n');
416 } else if (rem
>= 3 && strncmp (beg
, "+OK", 3) == 0) {
419 write_state_add(&write_state
,
420 from_line
, from_line_length
);
426 } else if (state
== XDELE
) {
428 net_write (s
, "QUIT\r\n", 6);
430 fprintf (stderr
, "QUIT\r\n");
432 } else if (state
== DELE
) {
433 if (++deleted
== count
) {
435 net_write (s
, "QUIT\r\n", 6);
437 fprintf (stderr
, "QUIT\r\n");
440 } else if (++state
== STAT
) {
441 if(sscanf (beg
+ 4, "%u %u", &count
, &bytes
) != 2)
442 errx(1, "Bad STAT-line: %.*s", (int)(p
- beg
), beg
);
444 fprintf (stderr
, "%u message(s) (%u bytes). "
448 fprintf (stderr
, "\n");
449 } else if (do_count
) {
450 fprintf (stderr
, "%u message(s) (%u bytes).\n",
455 net_write (s
, "QUIT\r\n", 6);
457 fprintf (stderr
, "QUIT\r\n");
470 errx (1, "Bad response: %.*s", (int)(p
- beg
), beg
);
474 write_state_flush (&write_state
);
476 memmove (in_buf
, beg
, rem
);
478 in_ptr
= in_buf
+ rem
;
480 if (FD_ISSET(s
, &writeset
)) {
481 if ((state
== STAT
&& !do_from
) || state
== RETR
)
482 out_len
= snprintf (out_buf
, sizeof(out_buf
),
483 "RETR %u\r\n", ++asked_for
);
484 else if ((state
== STAT
&& do_from
) || state
== TOP
)
485 out_len
= snprintf (out_buf
, sizeof(out_buf
),
486 "TOP %u 0\r\n", ++asked_for
);
487 else if(state
== XDELE
) {
488 out_len
= snprintf(out_buf
, sizeof(out_buf
),
489 "XDELE %u %u\r\n", 1, count
);
492 else if(state
== DELE
)
493 out_len
= snprintf (out_buf
, sizeof(out_buf
),
494 "DELE %u\r\n", ++asked_deleted
);
495 if (out_len
< 0 || out_len
> sizeof(out_buf
))
496 errx (1, "snprintf failed");
497 if (net_write (s
, out_buf
, out_len
) != out_len
)
500 fprintf (stderr
, "%s", out_buf
);
504 fprintf (stderr
, "Done\n");
509 write_state_destroy (&write_state
);
516 do_v5 (const char *host
,
519 const char *filename
,
520 const char *header_str
,
526 krb5_auth_context auth_context
= NULL
;
527 krb5_principal server
;
530 s
= do_connect (host
, port
, 1);
534 ret
= krb5_sname_to_principal (context
,
540 warnx ("krb5_sname_to_principal: %s",
541 krb5_get_err_text (context
, ret
));
545 ret
= krb5_sendauth (context
,
558 krb5_free_principal (context
, server
);
560 warnx ("krb5_sendauth: %s",
561 krb5_get_err_text (context
, ret
));
564 return doit (s
, host
, user
, filename
, header_str
, leavep
, verbose
, forkp
);
570 do_v4 (const char *host
,
573 const char *filename
,
574 const char *header_str
,
582 des_key_schedule sched
;
586 s
= do_connect (host
, port
, 1);
589 ret
= krb_sendauth(0,
594 krb_realmofhost(host
),
603 warnx("krb_sendauth: %s", krb_get_err_text(ret
));
606 return doit (s
, host
, user
, filename
, header_str
, leavep
, verbose
, forkp
);
612 #ifdef HESIOD_INTERFACES
615 hesiod_get_pobox (const char **user
)
618 struct hesiod_postoffice
*hpo
;
621 if(hesiod_init (&context
) != 0)
622 err (1, "hesiod_init");
624 hpo
= hesiod_getmailhost (context
, *user
);
626 warn ("hesiod_getmailhost %s", *user
);
628 if (strcasecmp(hpo
->hesiod_po_type
, "pop") != 0)
629 errx (1, "Unsupported po type %s", hpo
->hesiod_po_type
);
631 ret
= estrdup(hpo
->hesiod_po_host
);
632 *user
= estrdup(hpo
->hesiod_po_name
);
633 hesiod_free_postoffice (context
, hpo
);
635 hesiod_end (context
);
639 #else /* !HESIOD_INTERFACES */
642 hesiod_get_pobox (const char **user
)
645 struct hes_postoffice
*hpo
;
647 hpo
= hes_getmailhost (*user
);
649 warn ("hes_getmailhost %s", *user
);
651 if (strcasecmp(hpo
->po_type
, "pop") != 0)
652 errx (1, "Unsupported po type %s", hpo
->po_type
);
654 ret
= estrdup(hpo
->po_host
);
655 *user
= estrdup(hpo
->po_name
);
660 #endif /* HESIOD_INTERFACES */
665 get_pobox (const char **user
)
670 ret
= hesiod_get_pobox (user
);
674 ret
= getenv("MAILHOST");
676 errx (1, "MAILHOST not set");
681 parse_pobox (char *a0
, const char **host
, const char **user
)
689 *user
= getenv ("USERNAME");
691 struct passwd
*pwd
= getpwuid (getuid ());
694 errx (1, "Who are you?");
695 *user
= estrdup (pwd
->pw_name
);
697 *host
= get_pobox (user
);
701 /* if the specification starts with po:, remember this information */
702 if(strncmp(a0
, "po:", 3) == 0) {
706 /* if there is an `@', the hostname is after it, otherwise at the
707 beginning of the string */
715 /* if there is a `:', the username comes before it, otherwise at
716 the beginning of the string */
725 /* some inconsistent compatibility with various mailers */
729 u
= get_default_username ();
731 errx (1, "Who are you?");
739 main(int argc
, char **argv
)
744 const char *host
, *user
, *filename
= NULL
;
747 setprogname (argv
[0]);
753 ret
= krb5_init_context (&context
);
755 errx (1, "krb5_init_context failed: %d", ret
);
759 if (getarg (args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
,
766 #if defined(KRB4) && defined(KRB5)
767 if(use_v4
== -1 && use_v5
== 1)
769 if(use_v5
== -1 && use_v4
== 1)
781 if (do_from
&& header_str
== NULL
)
782 header_str
= "From:";
783 else if (header_str
!= NULL
)
797 } else if (argc
== 2) {
805 struct servent
*s
= roken_getservbyname (port_str
, "tcp");
812 port
= strtol (port_str
, &ptr
, 10);
813 if (port
== 0 && ptr
== port_str
)
814 errx (1, "Bad port `%s'", port_str
);
820 port
= krb5_getportbyname (context
, "kpop", "tcp", 1109);
822 port
= k_getportbyname ("kpop", "tcp", htons(1109));
824 #error must define KRB4 or KRB5
828 parse_pobox (pobox
, &host
, &user
);
832 ret
= do_v5 (host
, port
, user
, filename
, header_str
,
833 do_leave
, verbose_level
, do_fork
);
839 ret
= do_v4 (host
, port
, user
, filename
, header_str
,
840 do_leave
, verbose_level
, do_fork
);