1*bc3a204fScamield /* $OpenBSD: ftp-proxy.c,v 1.25 2012/04/05 19:08:40 camield Exp $ */ 28bfc93fdScamield 33b53f69aScamield /* 43b53f69aScamield * Copyright (c) 2004, 2005 Camiel Dobbelaar, <cd@sentia.nl> 53b53f69aScamield * 63b53f69aScamield * Permission to use, copy, modify, and distribute this software for any 73b53f69aScamield * purpose with or without fee is hereby granted, provided that the above 83b53f69aScamield * copyright notice and this permission notice appear in all copies. 93b53f69aScamield * 103b53f69aScamield * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 113b53f69aScamield * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 123b53f69aScamield * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 133b53f69aScamield * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 143b53f69aScamield * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 153b53f69aScamield * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 163b53f69aScamield * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 173b53f69aScamield */ 183b53f69aScamield 193b53f69aScamield #include <sys/queue.h> 203b53f69aScamield #include <sys/types.h> 213b53f69aScamield #include <sys/time.h> 223b53f69aScamield #include <sys/resource.h> 233b53f69aScamield #include <sys/socket.h> 243b53f69aScamield 253b53f69aScamield #include <net/if.h> 263b53f69aScamield #include <net/pfvar.h> 273b53f69aScamield #include <netinet/in.h> 283b53f69aScamield #include <arpa/inet.h> 293b53f69aScamield 303b53f69aScamield #include <err.h> 313b53f69aScamield #include <errno.h> 323b53f69aScamield #include <event.h> 333b53f69aScamield #include <fcntl.h> 343b53f69aScamield #include <netdb.h> 353b53f69aScamield #include <pwd.h> 363b53f69aScamield #include <signal.h> 373b53f69aScamield #include <stdarg.h> 383b53f69aScamield #include <stdio.h> 393b53f69aScamield #include <stdlib.h> 403b53f69aScamield #include <string.h> 413b53f69aScamield #include <syslog.h> 423b53f69aScamield #include <unistd.h> 433b53f69aScamield #include <vis.h> 443b53f69aScamield 453b53f69aScamield #include "filter.h" 463b53f69aScamield 473b53f69aScamield #define CONNECT_TIMEOUT 30 483b53f69aScamield #define MIN_PORT 1024 493b53f69aScamield #define MAX_LINE 500 503b53f69aScamield #define MAX_LOGLINE 300 513b53f69aScamield #define NTOP_BUFS 3 523b53f69aScamield #define TCP_BACKLOG 10 533b53f69aScamield 543b53f69aScamield #define CHROOT_DIR "/var/empty" 553b53f69aScamield #define NOPRIV_USER "proxy" 563b53f69aScamield 573b53f69aScamield /* pfctl standard NAT range. */ 583b53f69aScamield #define PF_NAT_PROXY_PORT_LOW 50001 593b53f69aScamield #define PF_NAT_PROXY_PORT_HIGH 65535 603b53f69aScamield 613b53f69aScamield #define sstosa(ss) ((struct sockaddr *)(ss)) 623b53f69aScamield 633b53f69aScamield enum { CMD_NONE = 0, CMD_PORT, CMD_EPRT, CMD_PASV, CMD_EPSV }; 643b53f69aScamield 653b53f69aScamield struct session { 663b53f69aScamield u_int32_t id; 673b53f69aScamield struct sockaddr_storage client_ss; 683b53f69aScamield struct sockaddr_storage proxy_ss; 693b53f69aScamield struct sockaddr_storage server_ss; 703b53f69aScamield struct sockaddr_storage orig_server_ss; 713b53f69aScamield struct bufferevent *client_bufev; 723b53f69aScamield struct bufferevent *server_bufev; 733b53f69aScamield int client_fd; 743b53f69aScamield int server_fd; 753b53f69aScamield char cbuf[MAX_LINE]; 763b53f69aScamield size_t cbuf_valid; 773b53f69aScamield char sbuf[MAX_LINE]; 783b53f69aScamield size_t sbuf_valid; 793b53f69aScamield int cmd; 8085ec68abSclaudio int client_rd; 813b53f69aScamield u_int16_t port; 823b53f69aScamield u_int16_t proxy_port; 833b53f69aScamield LIST_ENTRY(session) entry; 843b53f69aScamield }; 853b53f69aScamield 863b53f69aScamield LIST_HEAD(, session) sessions = LIST_HEAD_INITIALIZER(sessions); 873b53f69aScamield 883b53f69aScamield void client_error(struct bufferevent *, short, void *); 893b53f69aScamield int client_parse(struct session *s); 903b53f69aScamield int client_parse_anon(struct session *s); 913b53f69aScamield int client_parse_cmd(struct session *s); 923b53f69aScamield void client_read(struct bufferevent *, void *); 933b53f69aScamield int drop_privs(void); 943b53f69aScamield void end_session(struct session *); 958562b6e2Sclaudio void exit_daemon(void); 96f9bbbf45Sfgsch int get_line(char *, size_t *); 973b53f69aScamield void handle_connection(const int, short, void *); 984e6f7131Scamield void handle_signal(int, short, void *); 993b53f69aScamield struct session * init_session(void); 1003b53f69aScamield void logmsg(int, const char *, ...); 1013b53f69aScamield u_int16_t parse_port(int); 1023b53f69aScamield u_int16_t pick_proxy_port(void); 1033b53f69aScamield void proxy_reply(int, struct sockaddr *, u_int16_t); 1043b53f69aScamield void server_error(struct bufferevent *, short, void *); 1053b53f69aScamield int server_parse(struct session *s); 1069136fc3fScamield int allow_data_connection(struct session *s); 1073b53f69aScamield void server_read(struct bufferevent *, void *); 1083b53f69aScamield const char *sock_ntop(struct sockaddr *); 1093b53f69aScamield void usage(void); 1103b53f69aScamield 1113b53f69aScamield char linebuf[MAX_LINE + 1]; 1123b53f69aScamield size_t linelen; 1133b53f69aScamield 1143b53f69aScamield char ntop_buf[NTOP_BUFS][INET6_ADDRSTRLEN]; 1153b53f69aScamield 116*bc3a204fScamield struct event listen_ev, pause_accept_ev; 1173b53f69aScamield struct sockaddr_storage fixed_server_ss, fixed_proxy_ss; 1183b53f69aScamield char *fixed_server, *fixed_server_port, *fixed_proxy, *listen_ip, *listen_port, 1191a90e72aShenning *qname, *tagname; 1200678ffdcScamield int anonymous_only, daemonize, id_count, ipv6_mode, loglevel, max_sessions, 1210678ffdcScamield rfc_mode, session_count, timeout, verbose; 1223b53f69aScamield extern char *__progname; 1233b53f69aScamield 1243b53f69aScamield void 1253b53f69aScamield client_error(struct bufferevent *bufev, short what, void *arg) 1263b53f69aScamield { 1273b53f69aScamield struct session *s = arg; 1283b53f69aScamield 1293b53f69aScamield if (what & EVBUFFER_EOF) 1303b53f69aScamield logmsg(LOG_INFO, "#%d client close", s->id); 1313b53f69aScamield else if (what == (EVBUFFER_ERROR | EVBUFFER_READ)) 1323b53f69aScamield logmsg(LOG_ERR, "#%d client reset connection", s->id); 1333b53f69aScamield else if (what & EVBUFFER_TIMEOUT) 1343b53f69aScamield logmsg(LOG_ERR, "#%d client timeout", s->id); 1353b53f69aScamield else if (what & EVBUFFER_WRITE) 1363b53f69aScamield logmsg(LOG_ERR, "#%d client write error: %d", s->id, what); 1373b53f69aScamield else 1383b53f69aScamield logmsg(LOG_ERR, "#%d abnormal client error: %d", s->id, what); 1393b53f69aScamield 1403b53f69aScamield end_session(s); 1413b53f69aScamield } 1423b53f69aScamield 1433b53f69aScamield int 1443b53f69aScamield client_parse(struct session *s) 1453b53f69aScamield { 1463b53f69aScamield /* Reset any previous command. */ 1473b53f69aScamield s->cmd = CMD_NONE; 1483b53f69aScamield s->port = 0; 1493b53f69aScamield 1503b53f69aScamield /* Commands we are looking for are at least 4 chars long. */ 1513b53f69aScamield if (linelen < 4) 1523b53f69aScamield return (1); 1533b53f69aScamield 1543b53f69aScamield if (linebuf[0] == 'P' || linebuf[0] == 'p' || 1559136fc3fScamield linebuf[0] == 'E' || linebuf[0] == 'e') { 1569136fc3fScamield if (!client_parse_cmd(s)) 1579136fc3fScamield return (0); 1589136fc3fScamield 1599136fc3fScamield /* 1609136fc3fScamield * Allow active mode connections immediately, instead of 1619136fc3fScamield * waiting for a positive reply from the server. Some 1629136fc3fScamield * rare servers/proxies try to probe or setup the data 1639136fc3fScamield * connection before an actual transfer request. 1649136fc3fScamield */ 1659136fc3fScamield if (s->cmd == CMD_PORT || s->cmd == CMD_EPRT) 1669136fc3fScamield return (allow_data_connection(s)); 1679136fc3fScamield } 1683b53f69aScamield 1693b53f69aScamield if (anonymous_only && (linebuf[0] == 'U' || linebuf[0] == 'u')) 1703b53f69aScamield return (client_parse_anon(s)); 1713b53f69aScamield 1723b53f69aScamield return (1); 1733b53f69aScamield } 1743b53f69aScamield 1753b53f69aScamield int 1763b53f69aScamield client_parse_anon(struct session *s) 1773b53f69aScamield { 1783b53f69aScamield if (strcasecmp("USER ftp\r\n", linebuf) != 0 && 1793b53f69aScamield strcasecmp("USER anonymous\r\n", linebuf) != 0) { 1804f154adaSderaadt snprintf(linebuf, sizeof linebuf, 1813b53f69aScamield "500 Only anonymous FTP allowed\r\n"); 1823b53f69aScamield logmsg(LOG_DEBUG, "#%d proxy: %s", s->id, linebuf); 1833b53f69aScamield 1843b53f69aScamield /* Talk back to the client ourself. */ 1854f154adaSderaadt linelen = strlen(linebuf); 1863b53f69aScamield bufferevent_write(s->client_bufev, linebuf, linelen); 1873b53f69aScamield 1883b53f69aScamield /* Clear buffer so it's not sent to the server. */ 1893b53f69aScamield linebuf[0] = '\0'; 1903b53f69aScamield linelen = 0; 1913b53f69aScamield } 1923b53f69aScamield 1933b53f69aScamield return (1); 1943b53f69aScamield } 1953b53f69aScamield 1963b53f69aScamield int 1973b53f69aScamield client_parse_cmd(struct session *s) 1983b53f69aScamield { 1993b53f69aScamield if (strncasecmp("PASV", linebuf, 4) == 0) 2003b53f69aScamield s->cmd = CMD_PASV; 2013b53f69aScamield else if (strncasecmp("PORT ", linebuf, 5) == 0) 2023b53f69aScamield s->cmd = CMD_PORT; 2033b53f69aScamield else if (strncasecmp("EPSV", linebuf, 4) == 0) 2043b53f69aScamield s->cmd = CMD_EPSV; 2053b53f69aScamield else if (strncasecmp("EPRT ", linebuf, 5) == 0) 2063b53f69aScamield s->cmd = CMD_EPRT; 2073b53f69aScamield else 2083b53f69aScamield return (1); 2093b53f69aScamield 2103b53f69aScamield if (ipv6_mode && (s->cmd == CMD_PASV || s->cmd == CMD_PORT)) { 2113b53f69aScamield logmsg(LOG_CRIT, "PASV and PORT not allowed with IPv6"); 2123b53f69aScamield return (0); 2133b53f69aScamield } 2143b53f69aScamield 2153b53f69aScamield if (s->cmd == CMD_PORT || s->cmd == CMD_EPRT) { 2163b53f69aScamield s->port = parse_port(s->cmd); 2173b53f69aScamield if (s->port < MIN_PORT) { 2183b53f69aScamield logmsg(LOG_CRIT, "#%d bad port in '%s'", s->id, 2193b53f69aScamield linebuf); 2203b53f69aScamield return (0); 2213b53f69aScamield } 2223b53f69aScamield s->proxy_port = pick_proxy_port(); 2233b53f69aScamield proxy_reply(s->cmd, sstosa(&s->proxy_ss), s->proxy_port); 2243b53f69aScamield logmsg(LOG_DEBUG, "#%d proxy: %s", s->id, linebuf); 2253b53f69aScamield } 2263b53f69aScamield 2273b53f69aScamield return (1); 2283b53f69aScamield } 2293b53f69aScamield 2303b53f69aScamield void 2313b53f69aScamield client_read(struct bufferevent *bufev, void *arg) 2323b53f69aScamield { 2333b53f69aScamield struct session *s = arg; 2343b53f69aScamield size_t buf_avail, read; 2353b53f69aScamield int n; 2363b53f69aScamield 2373b53f69aScamield do { 2383b53f69aScamield buf_avail = sizeof s->cbuf - s->cbuf_valid; 2393b53f69aScamield read = bufferevent_read(bufev, s->cbuf + s->cbuf_valid, 2403b53f69aScamield buf_avail); 2413b53f69aScamield s->cbuf_valid += read; 2423b53f69aScamield 243f9bbbf45Sfgsch while ((n = get_line(s->cbuf, &s->cbuf_valid)) > 0) { 2443b53f69aScamield logmsg(LOG_DEBUG, "#%d client: %s", s->id, linebuf); 2453b53f69aScamield if (!client_parse(s)) { 2463b53f69aScamield end_session(s); 2473b53f69aScamield return; 2483b53f69aScamield } 2493b53f69aScamield bufferevent_write(s->server_bufev, linebuf, linelen); 2503b53f69aScamield } 2513b53f69aScamield 2523b53f69aScamield if (n == -1) { 253f5c0a919Scamield logmsg(LOG_ERR, "#%d client command too long or not" 254f5c0a919Scamield " clean", s->id); 2553b53f69aScamield end_session(s); 2563b53f69aScamield return; 2573b53f69aScamield } 2583b53f69aScamield } while (read == buf_avail); 2593b53f69aScamield } 2603b53f69aScamield 2613b53f69aScamield int 2623b53f69aScamield drop_privs(void) 2633b53f69aScamield { 2643b53f69aScamield struct passwd *pw; 2653b53f69aScamield 2663b53f69aScamield pw = getpwnam(NOPRIV_USER); 2673b53f69aScamield if (pw == NULL) 2683b53f69aScamield return (0); 2693b53f69aScamield 2703b53f69aScamield tzset(); 2713b53f69aScamield if (chroot(CHROOT_DIR) != 0 || chdir("/") != 0 || 2723b53f69aScamield setgroups(1, &pw->pw_gid) != 0 || 2733b53f69aScamield setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0 || 2743b53f69aScamield setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) 2753b53f69aScamield return (0); 2763b53f69aScamield 2773b53f69aScamield return (1); 2783b53f69aScamield } 2793b53f69aScamield 2803b53f69aScamield void 2813b53f69aScamield end_session(struct session *s) 2823b53f69aScamield { 2833b53f69aScamield int err; 2843b53f69aScamield 2853b53f69aScamield logmsg(LOG_INFO, "#%d ending session", s->id); 2863b53f69aScamield 2876767c32eSjoel /* Flush output buffers. */ 2886767c32eSjoel if (s->client_bufev && s->client_fd != -1) 2896767c32eSjoel evbuffer_write(s->client_bufev->output, s->client_fd); 2906767c32eSjoel if (s->server_bufev && s->server_fd != -1) 2916767c32eSjoel evbuffer_write(s->server_bufev->output, s->server_fd); 2926767c32eSjoel 2933b53f69aScamield if (s->client_fd != -1) 2946afc2855Scamield close(s->client_fd); 2953b53f69aScamield if (s->server_fd != -1) 2966afc2855Scamield close(s->server_fd); 2973b53f69aScamield 2982e6efe12Scamield if (s->client_bufev) 2992e6efe12Scamield bufferevent_free(s->client_bufev); 3002e6efe12Scamield if (s->server_bufev) 3012e6efe12Scamield bufferevent_free(s->server_bufev); 3022e6efe12Scamield 3033b53f69aScamield /* Remove rulesets by commiting empty ones. */ 3043b53f69aScamield err = 0; 3053b53f69aScamield if (prepare_commit(s->id) == -1) 3063b53f69aScamield err = errno; 3073b53f69aScamield else if (do_commit() == -1) { 3083b53f69aScamield err = errno; 3093b53f69aScamield do_rollback(); 3103b53f69aScamield } 3113b53f69aScamield if (err) 3123b53f69aScamield logmsg(LOG_ERR, "#%d pf rule removal failed: %s", s->id, 3133b53f69aScamield strerror(err)); 3143b53f69aScamield 3153b53f69aScamield LIST_REMOVE(s, entry); 3163b53f69aScamield free(s); 3173b53f69aScamield session_count--; 3183b53f69aScamield } 3193b53f69aScamield 3208562b6e2Sclaudio void 3213b53f69aScamield exit_daemon(void) 3223b53f69aScamield { 3233b53f69aScamield struct session *s, *next; 3243b53f69aScamield 3253b53f69aScamield for (s = LIST_FIRST(&sessions); s != LIST_END(&sessions); s = next) { 3263b53f69aScamield next = LIST_NEXT(s, entry); 3273b53f69aScamield end_session(s); 3283b53f69aScamield } 3293b53f69aScamield 3303b53f69aScamield if (daemonize) 3313b53f69aScamield closelog(); 3323b53f69aScamield 3333b53f69aScamield exit(0); 3343b53f69aScamield } 3353b53f69aScamield 3363b53f69aScamield int 337f9bbbf45Sfgsch get_line(char *buf, size_t *valid) 3383b53f69aScamield { 3393b53f69aScamield size_t i; 3403b53f69aScamield 3413b53f69aScamield if (*valid > MAX_LINE) 3423b53f69aScamield return (-1); 3433b53f69aScamield 3443b53f69aScamield /* Copy to linebuf while searching for a newline. */ 345f5c0a919Scamield for (i = 0; i < *valid; i++) { 346f5c0a919Scamield linebuf[i] = buf[i]; 347f5c0a919Scamield if (buf[i] == '\0') 348f5c0a919Scamield return (-1); 349f5c0a919Scamield if (buf[i] == '\n') 3503b53f69aScamield break; 351f5c0a919Scamield } 3523b53f69aScamield 3533b53f69aScamield if (i == *valid) { 3543b53f69aScamield /* No newline found. */ 3553b53f69aScamield linebuf[0] = '\0'; 3563b53f69aScamield linelen = 0; 3573b53f69aScamield if (i < MAX_LINE) 3583b53f69aScamield return (0); 3593b53f69aScamield return (-1); 3603b53f69aScamield } 3613b53f69aScamield 3623b53f69aScamield linelen = i + 1; 3633b53f69aScamield linebuf[linelen] = '\0'; 3643b53f69aScamield *valid -= linelen; 3653b53f69aScamield 3663b53f69aScamield /* Move leftovers to the start. */ 3673b53f69aScamield if (*valid != 0) 3683b53f69aScamield bcopy(buf + linelen, buf, *valid); 3693b53f69aScamield 3703b53f69aScamield return ((int)linelen); 3713b53f69aScamield } 3723b53f69aScamield 3733b53f69aScamield void 374*bc3a204fScamield handle_connection(const int listen_fd, short event, void *arg) 3753b53f69aScamield { 3763b53f69aScamield struct sockaddr_storage tmp_ss; 3773b53f69aScamield struct sockaddr *client_sa, *server_sa, *fixed_server_sa; 37848ed0d11Smikeb struct sockaddr *proxy_to_server_sa; 3793b53f69aScamield struct session *s; 3803b53f69aScamield socklen_t len; 3813b53f69aScamield int client_fd, fc, on; 3823b53f69aScamield 383*bc3a204fScamield event_add(&listen_ev, NULL); 384*bc3a204fScamield 385*bc3a204fScamield if ((event & EV_TIMEOUT)) 386*bc3a204fScamield /* accept() is no longer paused. */ 387*bc3a204fScamield return; 388*bc3a204fScamield 3893b53f69aScamield /* 3903b53f69aScamield * We _must_ accept the connection, otherwise libevent will keep 3913b53f69aScamield * coming back, and we will chew up all CPU. 3923b53f69aScamield */ 3933b53f69aScamield client_sa = sstosa(&tmp_ss); 3943b53f69aScamield len = sizeof(struct sockaddr_storage); 3953b53f69aScamield if ((client_fd = accept(listen_fd, client_sa, &len)) < 0) { 396*bc3a204fScamield logmsg(LOG_CRIT, "accept() failed: %s", strerror(errno)); 397*bc3a204fScamield 398*bc3a204fScamield /* 399*bc3a204fScamield * Pause accept if we are out of file descriptors, or 400*bc3a204fScamield * libevent will haunt us here too. 401*bc3a204fScamield */ 402*bc3a204fScamield if (errno == ENFILE || errno == EMFILE) { 403*bc3a204fScamield struct timeval pause = { 1, 0 }; 404*bc3a204fScamield 405*bc3a204fScamield event_del(&listen_ev); 406*bc3a204fScamield evtimer_add(&pause_accept_ev, &pause); 407*bc3a204fScamield } 4083b53f69aScamield return; 4093b53f69aScamield } 4103b53f69aScamield 4113b53f69aScamield /* Refuse connection if the maximum is reached. */ 4123b53f69aScamield if (session_count >= max_sessions) { 4133b53f69aScamield logmsg(LOG_ERR, "client limit (%d) reached, refusing " 4143b53f69aScamield "connection from %s", max_sessions, sock_ntop(client_sa)); 4153b53f69aScamield close(client_fd); 4163b53f69aScamield return; 4173b53f69aScamield } 4183b53f69aScamield 4193b53f69aScamield /* Allocate session and copy back the info from the accept(). */ 4203b53f69aScamield s = init_session(); 4213b53f69aScamield if (s == NULL) { 4223b53f69aScamield logmsg(LOG_CRIT, "init_session failed"); 4233b53f69aScamield close(client_fd); 4243b53f69aScamield return; 4253b53f69aScamield } 4263b53f69aScamield s->client_fd = client_fd; 4273b53f69aScamield memcpy(sstosa(&s->client_ss), client_sa, client_sa->sa_len); 4283b53f69aScamield 4293b53f69aScamield /* Cast it once, and be done with it. */ 4303b53f69aScamield client_sa = sstosa(&s->client_ss); 4313b53f69aScamield server_sa = sstosa(&s->server_ss); 4323b53f69aScamield proxy_to_server_sa = sstosa(&s->proxy_ss); 4333b53f69aScamield fixed_server_sa = sstosa(&fixed_server_ss); 4343b53f69aScamield 4353b53f69aScamield /* Log id/client early to ease debugging. */ 4363b53f69aScamield logmsg(LOG_DEBUG, "#%d accepted connection from %s", s->id, 4373b53f69aScamield sock_ntop(client_sa)); 4383b53f69aScamield 4393b53f69aScamield /* 4403b53f69aScamield * Find out the real server and port that the client wanted. 4413b53f69aScamield */ 4423b53f69aScamield len = sizeof(struct sockaddr_storage); 44348ed0d11Smikeb if (getsockname(s->client_fd, server_sa, &len) < 0) { 4443b53f69aScamield logmsg(LOG_CRIT, "#%d getsockname failed: %s", s->id, 4453b53f69aScamield strerror(errno)); 4463b53f69aScamield goto fail; 4473b53f69aScamield } 44848ed0d11Smikeb len = sizeof(s->client_rd); 44913ae2b57Smikeb if (getsockopt(s->client_fd, SOL_SOCKET, SO_RTABLE, &s->client_rd, 45013ae2b57Smikeb &len) && errno != ENOPROTOOPT) { 45148ed0d11Smikeb logmsg(LOG_CRIT, "#%d getsockopt failed: %s", s->id, 45248ed0d11Smikeb strerror(errno)); 4533b53f69aScamield goto fail; 4543b53f69aScamield } 4553b53f69aScamield if (fixed_server) { 4563b53f69aScamield memcpy(sstosa(&s->orig_server_ss), server_sa, 4573b53f69aScamield server_sa->sa_len); 4583b53f69aScamield memcpy(server_sa, fixed_server_sa, fixed_server_sa->sa_len); 4593b53f69aScamield } 4603b53f69aScamield 4613b53f69aScamield /* XXX: check we are not connecting to ourself. */ 4623b53f69aScamield 4633b53f69aScamield /* 4643b53f69aScamield * Setup socket and connect to server. 4653b53f69aScamield */ 4663b53f69aScamield if ((s->server_fd = socket(server_sa->sa_family, SOCK_STREAM, 4673b53f69aScamield IPPROTO_TCP)) < 0) { 4683b53f69aScamield logmsg(LOG_CRIT, "#%d server socket failed: %s", s->id, 4693b53f69aScamield strerror(errno)); 4703b53f69aScamield goto fail; 4713b53f69aScamield } 4723b53f69aScamield if (fixed_proxy && bind(s->server_fd, sstosa(&fixed_proxy_ss), 4733b53f69aScamield fixed_proxy_ss.ss_len) != 0) { 4743b53f69aScamield logmsg(LOG_CRIT, "#%d cannot bind fixed proxy address: %s", 4753b53f69aScamield s->id, strerror(errno)); 4763b53f69aScamield goto fail; 4773b53f69aScamield } 4783b53f69aScamield 4793b53f69aScamield /* Use non-blocking connect(), see CONNECT_TIMEOUT below. */ 4803b53f69aScamield if ((fc = fcntl(s->server_fd, F_GETFL)) == -1 || 4813b53f69aScamield fcntl(s->server_fd, F_SETFL, fc | O_NONBLOCK) == -1) { 4823b53f69aScamield logmsg(LOG_CRIT, "#%d cannot mark socket non-blocking: %s", 4833b53f69aScamield s->id, strerror(errno)); 4843b53f69aScamield goto fail; 4853b53f69aScamield } 4863b53f69aScamield if (connect(s->server_fd, server_sa, server_sa->sa_len) < 0 && 4873b53f69aScamield errno != EINPROGRESS) { 4883b53f69aScamield logmsg(LOG_CRIT, "#%d proxy cannot connect to server %s: %s", 4893b53f69aScamield s->id, sock_ntop(server_sa), strerror(errno)); 4903b53f69aScamield goto fail; 4913b53f69aScamield } 4923b53f69aScamield 4933b53f69aScamield len = sizeof(struct sockaddr_storage); 4943b53f69aScamield if ((getsockname(s->server_fd, proxy_to_server_sa, &len)) < 0) { 4953b53f69aScamield logmsg(LOG_CRIT, "#%d getsockname failed: %s", s->id, 4963b53f69aScamield strerror(errno)); 4973b53f69aScamield goto fail; 4983b53f69aScamield } 4993b53f69aScamield 5003b53f69aScamield logmsg(LOG_INFO, "#%d FTP session %d/%d started: client %s to server " 5013b53f69aScamield "%s via proxy %s", s->id, session_count, max_sessions, 5023b53f69aScamield sock_ntop(client_sa), sock_ntop(server_sa), 5033b53f69aScamield sock_ntop(proxy_to_server_sa)); 5043b53f69aScamield 5053b53f69aScamield /* Keepalive is nice, but don't care if it fails. */ 5063b53f69aScamield on = 1; 5073b53f69aScamield setsockopt(s->client_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, 5083b53f69aScamield sizeof on); 5093b53f69aScamield setsockopt(s->server_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, 5103b53f69aScamield sizeof on); 5113b53f69aScamield 5123b53f69aScamield /* 5133b53f69aScamield * Setup buffered events. 5143b53f69aScamield */ 5159150f310Scamield s->client_bufev = bufferevent_new(s->client_fd, &client_read, NULL, 5169150f310Scamield &client_error, s); 5173b53f69aScamield if (s->client_bufev == NULL) { 5183b53f69aScamield logmsg(LOG_CRIT, "#%d bufferevent_new client failed", s->id); 5193b53f69aScamield goto fail; 5203b53f69aScamield } 5213b53f69aScamield bufferevent_settimeout(s->client_bufev, timeout, 0); 5223b53f69aScamield bufferevent_enable(s->client_bufev, EV_READ | EV_TIMEOUT); 5233b53f69aScamield 5249150f310Scamield s->server_bufev = bufferevent_new(s->server_fd, &server_read, NULL, 5259150f310Scamield &server_error, s); 5263b53f69aScamield if (s->server_bufev == NULL) { 5273b53f69aScamield logmsg(LOG_CRIT, "#%d bufferevent_new server failed", s->id); 5283b53f69aScamield goto fail; 5293b53f69aScamield } 5303b53f69aScamield bufferevent_settimeout(s->server_bufev, CONNECT_TIMEOUT, 0); 5313b53f69aScamield bufferevent_enable(s->server_bufev, EV_READ | EV_TIMEOUT); 5323b53f69aScamield 5333b53f69aScamield return; 5343b53f69aScamield 5353b53f69aScamield fail: 5363b53f69aScamield end_session(s); 5373b53f69aScamield } 5383b53f69aScamield 5394e6f7131Scamield void 5404e6f7131Scamield handle_signal(int sig, short event, void *arg) 5414e6f7131Scamield { 5424e6f7131Scamield /* 5434e6f7131Scamield * Signal handler rules don't apply, libevent decouples for us. 5444e6f7131Scamield */ 5454e6f7131Scamield 5468562b6e2Sclaudio logmsg(LOG_ERR, "exiting on signal %d", sig); 5474e6f7131Scamield 5484e6f7131Scamield exit_daemon(); 5494e6f7131Scamield } 5504e6f7131Scamield 5514e6f7131Scamield 5523b53f69aScamield struct session * 5533b53f69aScamield init_session(void) 5543b53f69aScamield { 5553b53f69aScamield struct session *s; 5563b53f69aScamield 5573b53f69aScamield s = calloc(1, sizeof(struct session)); 5583b53f69aScamield if (s == NULL) 5593b53f69aScamield return (NULL); 5603b53f69aScamield 5613b53f69aScamield s->id = id_count++; 5623b53f69aScamield s->client_fd = -1; 5633b53f69aScamield s->server_fd = -1; 5643b53f69aScamield s->cbuf[0] = '\0'; 5653b53f69aScamield s->cbuf_valid = 0; 5663b53f69aScamield s->sbuf[0] = '\0'; 5673b53f69aScamield s->sbuf_valid = 0; 5683b53f69aScamield s->client_bufev = NULL; 5693b53f69aScamield s->server_bufev = NULL; 5703b53f69aScamield s->cmd = CMD_NONE; 5713b53f69aScamield s->port = 0; 5723b53f69aScamield 5733b53f69aScamield LIST_INSERT_HEAD(&sessions, s, entry); 5743b53f69aScamield session_count++; 5753b53f69aScamield 5763b53f69aScamield return (s); 5773b53f69aScamield } 5783b53f69aScamield 5793b53f69aScamield void 5803b53f69aScamield logmsg(int pri, const char *message, ...) 5813b53f69aScamield { 5823b53f69aScamield va_list ap; 5833b53f69aScamield 5843b53f69aScamield if (pri > loglevel) 5853b53f69aScamield return; 5863b53f69aScamield 5878582d7b5Scamield va_start(ap, message); 5888582d7b5Scamield 5893b53f69aScamield if (daemonize) 5903b53f69aScamield /* syslog does its own vissing. */ 5913b53f69aScamield vsyslog(pri, message, ap); 5923b53f69aScamield else { 5933b53f69aScamield char buf[MAX_LOGLINE]; 5943b53f69aScamield char visbuf[2 * MAX_LOGLINE]; 5953b53f69aScamield 5963b53f69aScamield /* We don't care about truncation. */ 5973b53f69aScamield vsnprintf(buf, sizeof buf, message, ap); 5983b53f69aScamield strnvis(visbuf, buf, sizeof visbuf, VIS_CSTYLE | VIS_NL); 5993b53f69aScamield fprintf(stderr, "%s\n", visbuf); 6003b53f69aScamield } 6013b53f69aScamield 6023b53f69aScamield va_end(ap); 6033b53f69aScamield } 6043b53f69aScamield 6053b53f69aScamield int 6063b53f69aScamield main(int argc, char *argv[]) 6073b53f69aScamield { 6083b53f69aScamield struct rlimit rlp; 6093b53f69aScamield struct addrinfo hints, *res; 610*bc3a204fScamield struct event ev_sighup, ev_sigint, ev_sigterm; 6113b53f69aScamield int ch, error, listenfd, on; 612f9429323Scamield const char *errstr; 6133b53f69aScamield 6143b53f69aScamield /* Defaults. */ 6153b53f69aScamield anonymous_only = 0; 6163b53f69aScamield daemonize = 1; 6173b53f69aScamield fixed_proxy = NULL; 6183b53f69aScamield fixed_server = NULL; 6193b53f69aScamield fixed_server_port = "21"; 6203b53f69aScamield ipv6_mode = 0; 6213b53f69aScamield listen_ip = NULL; 6223b53f69aScamield listen_port = "8021"; 6233b53f69aScamield loglevel = LOG_NOTICE; 6243b53f69aScamield max_sessions = 100; 6253b53f69aScamield qname = NULL; 6263b53f69aScamield rfc_mode = 0; 6271a90e72aShenning tagname = NULL; 6283b53f69aScamield timeout = 24 * 3600; 629867a6359Scamield verbose = 0; 6303b53f69aScamield 6313b53f69aScamield /* Other initialization. */ 6323b53f69aScamield id_count = 1; 6333b53f69aScamield session_count = 0; 6343b53f69aScamield 6351a90e72aShenning while ((ch = getopt(argc, argv, "6Aa:b:D:dm:P:p:q:R:rT:t:v")) != -1) { 6363b53f69aScamield switch (ch) { 6373b53f69aScamield case '6': 6383b53f69aScamield ipv6_mode = 1; 6393b53f69aScamield break; 6403b53f69aScamield case 'A': 6413b53f69aScamield anonymous_only = 1; 6423b53f69aScamield break; 6433b53f69aScamield case 'a': 6443b53f69aScamield fixed_proxy = optarg; 6453b53f69aScamield break; 6463b53f69aScamield case 'b': 6473b53f69aScamield listen_ip = optarg; 6483b53f69aScamield break; 6493b53f69aScamield case 'D': 650f9429323Scamield loglevel = strtonum(optarg, LOG_EMERG, LOG_DEBUG, 651f9429323Scamield &errstr); 652f9429323Scamield if (errstr) 653f9429323Scamield errx(1, "loglevel %s", errstr); 6543b53f69aScamield break; 6553b53f69aScamield case 'd': 6563b53f69aScamield daemonize = 0; 6573b53f69aScamield break; 6583b53f69aScamield case 'm': 659f9429323Scamield max_sessions = strtonum(optarg, 1, 500, &errstr); 660f9429323Scamield if (errstr) 661f9429323Scamield errx(1, "max sessions %s", errstr); 6623b53f69aScamield break; 6633b53f69aScamield case 'P': 6643b53f69aScamield fixed_server_port = optarg; 6653b53f69aScamield break; 6663b53f69aScamield case 'p': 6673b53f69aScamield listen_port = optarg; 6683b53f69aScamield break; 6693b53f69aScamield case 'q': 6703b53f69aScamield if (strlen(optarg) >= PF_QNAME_SIZE) 6713b53f69aScamield errx(1, "queuename too long"); 6723b53f69aScamield qname = optarg; 6733b53f69aScamield break; 6743b53f69aScamield case 'R': 6753b53f69aScamield fixed_server = optarg; 6763b53f69aScamield break; 6773b53f69aScamield case 'r': 6783b53f69aScamield rfc_mode = 1; 6793b53f69aScamield break; 6801a90e72aShenning case 'T': 6811a90e72aShenning if (strlen(optarg) >= PF_TAG_NAME_SIZE) 6821a90e72aShenning errx(1, "tagname too long"); 6831a90e72aShenning tagname = optarg; 6841a90e72aShenning break; 6853b53f69aScamield case 't': 686f9429323Scamield timeout = strtonum(optarg, 0, 86400, &errstr); 687f9429323Scamield if (errstr) 688f9429323Scamield errx(1, "timeout %s", errstr); 6893b53f69aScamield break; 690867a6359Scamield case 'v': 691867a6359Scamield verbose++; 692867a6359Scamield if (verbose > 2) 693867a6359Scamield usage(); 694867a6359Scamield break; 6953b53f69aScamield default: 6963b53f69aScamield usage(); 6973b53f69aScamield } 6983b53f69aScamield } 6993b53f69aScamield 7003b53f69aScamield if (listen_ip == NULL) 7013b53f69aScamield listen_ip = ipv6_mode ? "::1" : "127.0.0.1"; 7023b53f69aScamield 7033b53f69aScamield /* Check for root to save the user from cryptic failure messages. */ 7043b53f69aScamield if (getuid() != 0) 7053b53f69aScamield errx(1, "needs to start as root"); 7063b53f69aScamield 7073b53f69aScamield /* Raise max. open files limit to satisfy max. sessions. */ 7083b53f69aScamield rlp.rlim_cur = rlp.rlim_max = (2 * max_sessions) + 10; 7093b53f69aScamield if (setrlimit(RLIMIT_NOFILE, &rlp) == -1) 7103b53f69aScamield err(1, "setrlimit"); 7113b53f69aScamield 7123b53f69aScamield if (fixed_proxy) { 7133b53f69aScamield memset(&hints, 0, sizeof hints); 7143b53f69aScamield hints.ai_flags = AI_NUMERICHOST; 7153b53f69aScamield hints.ai_family = ipv6_mode ? AF_INET6 : AF_INET; 7163b53f69aScamield hints.ai_socktype = SOCK_STREAM; 7173b53f69aScamield error = getaddrinfo(fixed_proxy, NULL, &hints, &res); 7183b53f69aScamield if (error) 7193b53f69aScamield errx(1, "getaddrinfo fixed proxy address failed: %s", 7203b53f69aScamield gai_strerror(error)); 7213b53f69aScamield memcpy(&fixed_proxy_ss, res->ai_addr, res->ai_addrlen); 7223b53f69aScamield logmsg(LOG_INFO, "using %s to connect to servers", 7233b53f69aScamield sock_ntop(sstosa(&fixed_proxy_ss))); 7243b53f69aScamield freeaddrinfo(res); 7253b53f69aScamield } 7263b53f69aScamield 7273b53f69aScamield if (fixed_server) { 7283b53f69aScamield memset(&hints, 0, sizeof hints); 7293b53f69aScamield hints.ai_family = ipv6_mode ? AF_INET6 : AF_INET; 7303b53f69aScamield hints.ai_socktype = SOCK_STREAM; 7313b53f69aScamield error = getaddrinfo(fixed_server, fixed_server_port, &hints, 7323b53f69aScamield &res); 7333b53f69aScamield if (error) 7343b53f69aScamield errx(1, "getaddrinfo fixed server address failed: %s", 7353b53f69aScamield gai_strerror(error)); 7363b53f69aScamield memcpy(&fixed_server_ss, res->ai_addr, res->ai_addrlen); 7373b53f69aScamield logmsg(LOG_INFO, "using fixed server %s", 7383b53f69aScamield sock_ntop(sstosa(&fixed_server_ss))); 7393b53f69aScamield freeaddrinfo(res); 7403b53f69aScamield } 7413b53f69aScamield 7423b53f69aScamield /* Setup listener. */ 7433b53f69aScamield memset(&hints, 0, sizeof hints); 7443b53f69aScamield hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; 7453b53f69aScamield hints.ai_family = ipv6_mode ? AF_INET6 : AF_INET; 7463b53f69aScamield hints.ai_socktype = SOCK_STREAM; 7473b53f69aScamield error = getaddrinfo(listen_ip, listen_port, &hints, &res); 7483b53f69aScamield if (error) 7493b53f69aScamield errx(1, "getaddrinfo listen address failed: %s", 7503b53f69aScamield gai_strerror(error)); 7513b53f69aScamield if ((listenfd = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP)) == -1) 7523b53f69aScamield errx(1, "socket failed"); 7533b53f69aScamield on = 1; 7543b53f69aScamield if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, 7553b53f69aScamield sizeof on) != 0) 7563b53f69aScamield err(1, "setsockopt failed"); 7573b53f69aScamield if (bind(listenfd, (struct sockaddr *)res->ai_addr, 7583b53f69aScamield (socklen_t)res->ai_addrlen) != 0) 7593b53f69aScamield err(1, "bind failed"); 7603b53f69aScamield if (listen(listenfd, TCP_BACKLOG) != 0) 7613b53f69aScamield err(1, "listen failed"); 7623b53f69aScamield freeaddrinfo(res); 7633b53f69aScamield 7643b53f69aScamield /* Initialize pf. */ 7651a90e72aShenning init_filter(qname, tagname, verbose); 7663b53f69aScamield 7673b53f69aScamield if (daemonize) { 7683b53f69aScamield if (daemon(0, 0) == -1) 7693b53f69aScamield err(1, "cannot daemonize"); 7703b53f69aScamield openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 7713b53f69aScamield } 7723b53f69aScamield 7733b53f69aScamield /* Use logmsg for output from here on. */ 7743b53f69aScamield 7753b53f69aScamield if (!drop_privs()) { 7763b53f69aScamield logmsg(LOG_ERR, "cannot drop privileges: %s", strerror(errno)); 7773b53f69aScamield exit(1); 7783b53f69aScamield } 7793b53f69aScamield 7803b53f69aScamield event_init(); 7814e6f7131Scamield 7824e6f7131Scamield /* Setup signal handler. */ 7836ba63af2Scamield signal(SIGPIPE, SIG_IGN); 7844e6f7131Scamield signal_set(&ev_sighup, SIGHUP, handle_signal, NULL); 7854e6f7131Scamield signal_set(&ev_sigint, SIGINT, handle_signal, NULL); 7864e6f7131Scamield signal_set(&ev_sigterm, SIGTERM, handle_signal, NULL); 7874e6f7131Scamield signal_add(&ev_sighup, NULL); 7884e6f7131Scamield signal_add(&ev_sigint, NULL); 7894e6f7131Scamield signal_add(&ev_sigterm, NULL); 7904e6f7131Scamield 791*bc3a204fScamield event_set(&listen_ev, listenfd, EV_READ, handle_connection, NULL); 792*bc3a204fScamield event_add(&listen_ev, NULL); 793*bc3a204fScamield evtimer_set(&pause_accept_ev, handle_connection, NULL); 7943b53f69aScamield 7953b53f69aScamield logmsg(LOG_NOTICE, "listening on %s port %s", listen_ip, listen_port); 7963b53f69aScamield 7973b53f69aScamield /* Vroom, vroom. */ 7983b53f69aScamield event_dispatch(); 7993b53f69aScamield 8003b53f69aScamield logmsg(LOG_ERR, "event_dispatch error: %s", strerror(errno)); 8013b53f69aScamield exit_daemon(); 8023b53f69aScamield 8033b53f69aScamield /* NOTREACHED */ 8043b53f69aScamield return (1); 8053b53f69aScamield } 8063b53f69aScamield 8073b53f69aScamield u_int16_t 8083b53f69aScamield parse_port(int mode) 8093b53f69aScamield { 8103b53f69aScamield unsigned int port, v[6]; 8113b53f69aScamield int n; 8123b53f69aScamield char *p; 8133b53f69aScamield 8143b53f69aScamield /* Find the last space or left-parenthesis. */ 8153b53f69aScamield for (p = linebuf + linelen; p > linebuf; p--) 8163b53f69aScamield if (*p == ' ' || *p == '(') 8173b53f69aScamield break; 8183b53f69aScamield if (p == linebuf) 8193b53f69aScamield return (0); 8203b53f69aScamield 8213b53f69aScamield switch (mode) { 8223b53f69aScamield case CMD_PORT: 8233b53f69aScamield n = sscanf(p, " %u,%u,%u,%u,%u,%u", &v[0], &v[1], &v[2], 8243b53f69aScamield &v[3], &v[4], &v[5]); 8253b53f69aScamield if (n == 6 && v[0] < 256 && v[1] < 256 && v[2] < 256 && 8263b53f69aScamield v[3] < 256 && v[4] < 256 && v[5] < 256) 8273b53f69aScamield return ((v[4] << 8) | v[5]); 8283b53f69aScamield break; 8293b53f69aScamield case CMD_PASV: 8303b53f69aScamield n = sscanf(p, "(%u,%u,%u,%u,%u,%u)", &v[0], &v[1], &v[2], 8313b53f69aScamield &v[3], &v[4], &v[5]); 8323b53f69aScamield if (n == 6 && v[0] < 256 && v[1] < 256 && v[2] < 256 && 8333b53f69aScamield v[3] < 256 && v[4] < 256 && v[5] < 256) 8343b53f69aScamield return ((v[4] << 8) | v[5]); 8353b53f69aScamield break; 8363b53f69aScamield case CMD_EPSV: 8373b53f69aScamield n = sscanf(p, "(|||%u|)", &port); 8383b53f69aScamield if (n == 1 && port < 65536) 8393b53f69aScamield return (port); 8403b53f69aScamield break; 8413b53f69aScamield case CMD_EPRT: 8423b53f69aScamield n = sscanf(p, " |1|%u.%u.%u.%u|%u|", &v[0], &v[1], &v[2], 8433b53f69aScamield &v[3], &port); 8443b53f69aScamield if (n == 5 && v[0] < 256 && v[1] < 256 && v[2] < 256 && 8453b53f69aScamield v[3] < 256 && port < 65536) 8463b53f69aScamield return (port); 8473b53f69aScamield n = sscanf(p, " |2|%*[a-fA-F0-9:]|%u|", &port); 8483b53f69aScamield if (n == 1 && port < 65536) 8493b53f69aScamield return (port); 8503b53f69aScamield break; 8513b53f69aScamield default: 8523b53f69aScamield return (0); 8533b53f69aScamield } 8543b53f69aScamield 8553b53f69aScamield return (0); 8563b53f69aScamield } 8573b53f69aScamield 8583b53f69aScamield u_int16_t 8593b53f69aScamield pick_proxy_port(void) 8603b53f69aScamield { 8613b53f69aScamield /* Random should be good enough for avoiding port collisions. */ 86266ad965fSdjm return (IPPORT_HIFIRSTAUTO + 86366ad965fSdjm arc4random_uniform(IPPORT_HILASTAUTO - IPPORT_HIFIRSTAUTO)); 8643b53f69aScamield } 8653b53f69aScamield 8663b53f69aScamield void 8673b53f69aScamield proxy_reply(int cmd, struct sockaddr *sa, u_int16_t port) 8683b53f69aScamield { 8693b53f69aScamield int i, r; 8703b53f69aScamield 8713b53f69aScamield switch (cmd) { 8723b53f69aScamield case CMD_PORT: 8733b53f69aScamield r = snprintf(linebuf, sizeof linebuf, 8743b53f69aScamield "PORT %s,%u,%u\r\n", sock_ntop(sa), port / 256, 8753b53f69aScamield port % 256); 8763b53f69aScamield break; 8773b53f69aScamield case CMD_PASV: 8783b53f69aScamield r = snprintf(linebuf, sizeof linebuf, 8793b53f69aScamield "227 Entering Passive Mode (%s,%u,%u)\r\n", sock_ntop(sa), 8803b53f69aScamield port / 256, port % 256); 8813b53f69aScamield break; 8823b53f69aScamield case CMD_EPRT: 8833b53f69aScamield if (sa->sa_family == AF_INET) 8843b53f69aScamield r = snprintf(linebuf, sizeof linebuf, 8853b53f69aScamield "EPRT |1|%s|%u|\r\n", sock_ntop(sa), port); 8863b53f69aScamield else if (sa->sa_family == AF_INET6) 8873b53f69aScamield r = snprintf(linebuf, sizeof linebuf, 8883b53f69aScamield "EPRT |2|%s|%u|\r\n", sock_ntop(sa), port); 8893b53f69aScamield break; 8903b53f69aScamield case CMD_EPSV: 8913b53f69aScamield r = snprintf(linebuf, sizeof linebuf, 8923b53f69aScamield "229 Entering Extended Passive Mode (|||%u|)\r\n", port); 8933b53f69aScamield break; 8943b53f69aScamield } 8953b53f69aScamield 8963b53f69aScamield if (r < 0 || r >= sizeof linebuf) { 8973b53f69aScamield logmsg(LOG_ERR, "proxy_reply failed: %d", r); 8983b53f69aScamield linebuf[0] = '\0'; 8993b53f69aScamield linelen = 0; 9003b53f69aScamield return; 9013b53f69aScamield } 9023b53f69aScamield linelen = (size_t)r; 9033b53f69aScamield 9043b53f69aScamield if (cmd == CMD_PORT || cmd == CMD_PASV) { 9053b53f69aScamield /* Replace dots in IP address with commas. */ 9063b53f69aScamield for (i = 0; i < linelen; i++) 9073b53f69aScamield if (linebuf[i] == '.') 9083b53f69aScamield linebuf[i] = ','; 9093b53f69aScamield } 9103b53f69aScamield } 9113b53f69aScamield 9123b53f69aScamield void 9133b53f69aScamield server_error(struct bufferevent *bufev, short what, void *arg) 9143b53f69aScamield { 9153b53f69aScamield struct session *s = arg; 9163b53f69aScamield 9173b53f69aScamield if (what & EVBUFFER_EOF) 9183b53f69aScamield logmsg(LOG_INFO, "#%d server close", s->id); 9193b53f69aScamield else if (what == (EVBUFFER_ERROR | EVBUFFER_READ)) 9203b53f69aScamield logmsg(LOG_ERR, "#%d server refused connection", s->id); 9213b53f69aScamield else if (what & EVBUFFER_WRITE) 9223b53f69aScamield logmsg(LOG_ERR, "#%d server write error: %d", s->id, what); 9233b53f69aScamield else if (what & EVBUFFER_TIMEOUT) 9243b53f69aScamield logmsg(LOG_NOTICE, "#%d server timeout", s->id); 9253b53f69aScamield else 9263b53f69aScamield logmsg(LOG_ERR, "#%d abnormal server error: %d", s->id, what); 9273b53f69aScamield 9283b53f69aScamield end_session(s); 9293b53f69aScamield } 9303b53f69aScamield 9313b53f69aScamield int 9323b53f69aScamield server_parse(struct session *s) 9333b53f69aScamield { 9343b53f69aScamield if (s->cmd == CMD_NONE || linelen < 4 || linebuf[0] != '2') 9353b53f69aScamield goto out; 9363b53f69aScamield 9379136fc3fScamield if ((s->cmd == CMD_PASV && strncmp("227 ", linebuf, 4) == 0) || 9389136fc3fScamield (s->cmd == CMD_EPSV && strncmp("229 ", linebuf, 4) == 0)) 9399136fc3fScamield return (allow_data_connection(s)); 9409136fc3fScamield 9419136fc3fScamield out: 9429136fc3fScamield s->cmd = CMD_NONE; 9439136fc3fScamield s->port = 0; 9449136fc3fScamield 9459136fc3fScamield return (1); 9469136fc3fScamield } 9479136fc3fScamield 9489136fc3fScamield int 9499136fc3fScamield allow_data_connection(struct session *s) 9509136fc3fScamield { 9519136fc3fScamield struct sockaddr *client_sa, *orig_sa, *proxy_sa, *server_sa; 9529136fc3fScamield int prepared = 0; 9539136fc3fScamield 9543b53f69aScamield /* 9553b53f69aScamield * The pf rules below do quite some NAT rewriting, to keep up 9563b53f69aScamield * appearances. Points to keep in mind: 9573b53f69aScamield * 1) The client must think it's talking to the real server, 9583b53f69aScamield * for both control and data connections. Transparently. 9593b53f69aScamield * 2) The server must think that the proxy is the client. 9603b53f69aScamield * 3) Source and destination ports are rewritten to minimize 9613b53f69aScamield * port collisions, to aid security (some systems pick weak 9623b53f69aScamield * ports) or to satisfy RFC requirements (source port 20). 9633b53f69aScamield */ 9643b53f69aScamield 9653b53f69aScamield /* Cast this once, to make code below it more readable. */ 9663b53f69aScamield client_sa = sstosa(&s->client_ss); 9673b53f69aScamield server_sa = sstosa(&s->server_ss); 9683b53f69aScamield proxy_sa = sstosa(&s->proxy_ss); 9693b53f69aScamield if (fixed_server) 9703b53f69aScamield /* Fixed server: data connections must appear to come 9713b53f69aScamield from / go to the original server, not the fixed one. */ 9723b53f69aScamield orig_sa = sstosa(&s->orig_server_ss); 9733b53f69aScamield else 9743b53f69aScamield /* Server not fixed: orig_server == server. */ 9753b53f69aScamield orig_sa = sstosa(&s->server_ss); 9763b53f69aScamield 9773b53f69aScamield /* Passive modes. */ 9789136fc3fScamield if (s->cmd == CMD_PASV || s->cmd == CMD_EPSV) { 9793b53f69aScamield s->port = parse_port(s->cmd); 9803b53f69aScamield if (s->port < MIN_PORT) { 9813b53f69aScamield logmsg(LOG_CRIT, "#%d bad port in '%s'", s->id, 9823b53f69aScamield linebuf); 9833b53f69aScamield return (0); 9843b53f69aScamield } 9853b53f69aScamield s->proxy_port = pick_proxy_port(); 9863b53f69aScamield logmsg(LOG_INFO, "#%d passive: client to server port %d" 9873b53f69aScamield " via port %d", s->id, s->port, s->proxy_port); 9883b53f69aScamield 9893b53f69aScamield if (prepare_commit(s->id) == -1) 9903b53f69aScamield goto fail; 9913b53f69aScamield prepared = 1; 9923b53f69aScamield 9933b53f69aScamield proxy_reply(s->cmd, orig_sa, s->proxy_port); 9943b53f69aScamield logmsg(LOG_DEBUG, "#%d proxy: %s", s->id, linebuf); 9953b53f69aScamield 996bb9fd9ecSclaudio /* pass in from $client to $orig_server port $proxy_port 997bb9fd9ecSclaudio rdr-to $server port $port */ 99885ec68abSclaudio if (add_rdr(s->id, client_sa, s->client_rd, orig_sa, 99985ec68abSclaudio s->proxy_port, server_sa, s->port, getrtable()) == -1) 10003b53f69aScamield goto fail; 10013b53f69aScamield 1002bb9fd9ecSclaudio /* pass out from $client to $server port $port nat-to $proxy */ 100385ec68abSclaudio if (add_nat(s->id, client_sa, getrtable(), server_sa, 100485ec68abSclaudio s->port, proxy_sa, PF_NAT_PROXY_PORT_LOW, 100585ec68abSclaudio PF_NAT_PROXY_PORT_HIGH) == -1) 10063b53f69aScamield goto fail; 10073b53f69aScamield } 10083b53f69aScamield 10093b53f69aScamield /* Active modes. */ 10109136fc3fScamield if (s->cmd == CMD_PORT || s->cmd == CMD_EPRT) { 10113b53f69aScamield logmsg(LOG_INFO, "#%d active: server to client port %d" 10123b53f69aScamield " via port %d", s->id, s->port, s->proxy_port); 10133b53f69aScamield 10143b53f69aScamield if (prepare_commit(s->id) == -1) 10153b53f69aScamield goto fail; 10163b53f69aScamield prepared = 1; 10173b53f69aScamield 1018bb9fd9ecSclaudio /* pass in from $server to $proxy port $proxy_port 1019bb9fd9ecSclaudio rdr-to $client port $port */ 102085ec68abSclaudio if (add_rdr(s->id, server_sa, getrtable(), proxy_sa, 102185ec68abSclaudio s->proxy_port, client_sa, s->port, s->client_rd) == -1) 10223b53f69aScamield goto fail; 10233b53f69aScamield 1024bb9fd9ecSclaudio /* pass out from $server to $client port $port 1025bb9fd9ecSclaudio nat-to $orig_server port $natport */ 10263b53f69aScamield if (rfc_mode && s->cmd == CMD_PORT) { 10273b53f69aScamield /* Rewrite sourceport to RFC mandated 20. */ 102885ec68abSclaudio if (add_nat(s->id, server_sa, s->client_rd, client_sa, 102985ec68abSclaudio s->port, orig_sa, 20, 20) == -1) 10303b53f69aScamield goto fail; 10313b53f69aScamield } else { 10323b53f69aScamield /* Let pf pick a source port from the standard range. */ 103385ec68abSclaudio if (add_nat(s->id, server_sa, s->client_rd, client_sa, 103485ec68abSclaudio s->port, orig_sa, PF_NAT_PROXY_PORT_LOW, 10353b53f69aScamield PF_NAT_PROXY_PORT_HIGH) == -1) 10363b53f69aScamield goto fail; 10373b53f69aScamield } 10383b53f69aScamield } 10393b53f69aScamield 10403b53f69aScamield /* Commit rules if they were prepared. */ 10413b53f69aScamield if (prepared && (do_commit() == -1)) { 10423b53f69aScamield if (errno != EBUSY) 10433b53f69aScamield goto fail; 10443b53f69aScamield /* One more try if busy. */ 10453b53f69aScamield usleep(5000); 10463b53f69aScamield if (do_commit() == -1) 10473b53f69aScamield goto fail; 10483b53f69aScamield } 10493b53f69aScamield 10503b53f69aScamield s->cmd = CMD_NONE; 10513b53f69aScamield s->port = 0; 10523b53f69aScamield 10533b53f69aScamield return (1); 10543b53f69aScamield 10553b53f69aScamield fail: 10563b53f69aScamield logmsg(LOG_CRIT, "#%d pf operation failed: %s", s->id, strerror(errno)); 10573b53f69aScamield if (prepared) 10583b53f69aScamield do_rollback(); 10593b53f69aScamield return (0); 10603b53f69aScamield } 10613b53f69aScamield 10623b53f69aScamield void 10633b53f69aScamield server_read(struct bufferevent *bufev, void *arg) 10643b53f69aScamield { 10653b53f69aScamield struct session *s = arg; 10663b53f69aScamield size_t buf_avail, read; 10673b53f69aScamield int n; 10683b53f69aScamield 10693b53f69aScamield bufferevent_settimeout(bufev, timeout, 0); 10703b53f69aScamield 10713b53f69aScamield do { 10723b53f69aScamield buf_avail = sizeof s->sbuf - s->sbuf_valid; 10733b53f69aScamield read = bufferevent_read(bufev, s->sbuf + s->sbuf_valid, 10743b53f69aScamield buf_avail); 10753b53f69aScamield s->sbuf_valid += read; 10763b53f69aScamield 1077f9bbbf45Sfgsch while ((n = get_line(s->sbuf, &s->sbuf_valid)) > 0) { 10783b53f69aScamield logmsg(LOG_DEBUG, "#%d server: %s", s->id, linebuf); 10793b53f69aScamield if (!server_parse(s)) { 10803b53f69aScamield end_session(s); 10813b53f69aScamield return; 10823b53f69aScamield } 10833b53f69aScamield bufferevent_write(s->client_bufev, linebuf, linelen); 10843b53f69aScamield } 10853b53f69aScamield 10863b53f69aScamield if (n == -1) { 1087f5c0a919Scamield logmsg(LOG_ERR, "#%d server reply too long or not" 1088f5c0a919Scamield " clean", s->id); 10893b53f69aScamield end_session(s); 10903b53f69aScamield return; 10913b53f69aScamield } 10923b53f69aScamield } while (read == buf_avail); 10933b53f69aScamield } 10943b53f69aScamield 10953b53f69aScamield const char * 10963b53f69aScamield sock_ntop(struct sockaddr *sa) 10973b53f69aScamield { 10983b53f69aScamield static int n = 0; 10993b53f69aScamield 11003b53f69aScamield /* Cycle to next buffer. */ 11013b53f69aScamield n = (n + 1) % NTOP_BUFS; 11023b53f69aScamield ntop_buf[n][0] = '\0'; 11033b53f69aScamield 11043b53f69aScamield if (sa->sa_family == AF_INET) { 11053b53f69aScamield struct sockaddr_in *sin = (struct sockaddr_in *)sa; 11063b53f69aScamield 11073b53f69aScamield return (inet_ntop(AF_INET, &sin->sin_addr, ntop_buf[n], 11083b53f69aScamield sizeof ntop_buf[0])); 11093b53f69aScamield } 11103b53f69aScamield 11113b53f69aScamield if (sa->sa_family == AF_INET6) { 11123b53f69aScamield struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; 11133b53f69aScamield 11143b53f69aScamield return (inet_ntop(AF_INET6, &sin6->sin6_addr, ntop_buf[n], 11153b53f69aScamield sizeof ntop_buf[0])); 11163b53f69aScamield } 11173b53f69aScamield 11183b53f69aScamield return (NULL); 11193b53f69aScamield } 11203b53f69aScamield 11213b53f69aScamield void 11223b53f69aScamield usage(void) 11233b53f69aScamield { 1124867a6359Scamield fprintf(stderr, "usage: %s [-6Adrv] [-a address] [-b address]" 11253b53f69aScamield " [-D level] [-m maxsessions]\n [-P port]" 1126f9b24bdcShenning " [-p port] [-q queue] [-R address] [-T tag]\n" 1127f9b24bdcShenning " [-t timeout]\n", __progname); 11283b53f69aScamield exit(1); 11293b53f69aScamield } 1130