From e2754301d6ff0020c5ff349491ad814cbc70161c Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Tue, 9 Nov 2010 17:33:16 +0300 Subject: ppp: reduced length of session id --- accel-pptpd/CMakeLists.txt | 3 +- accel-pptpd/cli/telnet.c | 139 +++++++++++++++++++-- accel-pptpd/ctrl/l2tp/l2tp.c | 2 + accel-pptpd/ppp/ppp.c | 58 ++++++--- accel-pptpd/ppp/ppp.h | 2 +- .../net-dialup/accel-pptp/accel-pptp-9999.ebuild | 4 +- 6 files changed, 183 insertions(+), 25 deletions(-) diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt index 7b7c2790..71fdef62 100644 --- a/accel-pptpd/CMakeLists.txt +++ b/accel-pptpd/CMakeLists.txt @@ -73,4 +73,5 @@ INSTALL(FILES accel-pptp.conf DESTINATION etc) INSTALL(FILES accel-pptp.conf.5 DESTINATION usr/share/man/man5) INSTALL(CODE "EXECUTE_PROCESS(COMMAND mkdir -p /var/log/accel-pptp)") - +INSTALL(CODE "EXECUTE_PROCESS(COMMAND mkdir -p /var/run/accel-pptp)") +INSTALL(CODE "EXECUTE_PROCESS(COMMAND echo 0 > /var/run/accel-pptp/seq)") diff --git a/accel-pptpd/cli/telnet.c b/accel-pptpd/cli/telnet.c index e05fd08e..50ef9240 100644 --- a/accel-pptpd/cli/telnet.c +++ b/accel-pptpd/cli/telnet.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -22,18 +23,98 @@ struct client_t { struct list_head entry; struct triton_md_handler_t hnd; - char *recv_buf; + uint8_t *recv_buf; int recv_pos; struct list_head xmit_queue; + struct buffer_t *xmit_buf; + int xmit_pos; int auth:1; }; +struct buffer_t +{ + struct list_head entry; + int size; + uint8_t buf[0]; +}; + static struct triton_context_t serv_ctx; static struct triton_md_handler_t serv_hnd; -static void send_banner(struct client_t *cln) +static void disconnect(struct client_t *cln) +{ + struct buffer_t *b; + + log_debug("cli: disconnect\n"); + + triton_md_unregister_handler(&cln->hnd); + close(cln->hnd.fd); + + if (cln->xmit_buf) + _free(cln->xmit_buf); + + while (!list_empty(&cln->xmit_queue)) { + b = list_entry(cln->xmit_queue.next, typeof(*b), entry); + list_del(&b->entry); + _free(b); + } + + _free(cln->recv_buf); + _free(cln); +} + +static void queue_buffer(struct client_t *cln, struct buffer_t *b) +{ + if (cln->xmit_buf) + list_add_tail(&b->entry, &cln->xmit_queue); + else + cln->xmit_buf = b; +} + +static int telnet_send(struct client_t *cln, const void *_buf, int size) +{ + int n, k; + struct buffer_t *b; + const uint8_t *buf = (const uint8_t *)_buf; + + for (n = 0; n < size; n += k) { + k = write(cln->hnd.fd, buf + n, size - n); + if (k < 0) { + if (errno == EAGAIN) { + b = _malloc(sizeof(*b) + size - n); + b->size = size - n; + memcpy(b->buf, buf, size - n); + queue_buffer(cln, b); + + triton_md_enable_handler(&cln->hnd, MD_MODE_WRITE); + break; + } + if (errno != EPIPE) + log_error("cli: write: %s\n", strerror(errno)); + disconnect(cln); + return -1; + } + } + return 0; +} + +static int send_banner(struct client_t *cln) +{ + return telnet_send(cln, BANNER, sizeof(BANNER)); +} + +static int send_auth_request(struct client_t *cln) { - write(cln->hnd.fd, BANNER, sizeof(BANNER)); + return 0; +} + +static void print_buf(const uint8_t *buf, int size) +{ + int i; + + for (i = 0; i < size; i++) + log_debug("%x ", buf[i]); + log_debug("\n"); } static int cln_read(struct triton_md_handler_t *h) @@ -44,7 +125,7 @@ static int cln_read(struct triton_md_handler_t *h) while (1) { n = read(h->fd, cln->recv_buf + cln->recv_pos, RECV_BUF_SIZE - cln->recv_pos); if (n == 0) { - //disconnect(cln); + disconnect(cln); return 0; } if (n < 0) { @@ -53,11 +134,45 @@ static int cln_read(struct triton_md_handler_t *h) return 0; } log_debug("cli: read(%i): ", n); + print_buf(cln->recv_buf + cln->recv_pos, n); } return 0; } +static int cln_write(struct triton_md_handler_t *h) +{ + struct client_t *cln = container_of(h, typeof(*cln), hnd); + int k; + + while (1) { + for (; cln->xmit_pos < cln->xmit_buf->size; cln->xmit_pos += k) { + k = write(cln->hnd.fd, cln->xmit_buf->buf + cln->xmit_pos, cln->xmit_buf->size - cln->xmit_pos); + if (k < 0) { + if (errno == EAGAIN) + return 0; + if (errno != EPIPE) + log_error("cli: write: %s\n", strerror(errno)); + disconnect(cln); + return -1; + } + } + + _free(cln->xmit_buf); + cln->xmit_pos = 0; + + if (list_empty(&cln->xmit_queue)) + break; + + cln->xmit_buf = list_entry(cln->xmit_queue.next, typeof(*cln->xmit_buf), entry); + list_del(&cln->xmit_buf->entry); + } + + triton_md_disable_handler(&cln->hnd, MD_MODE_WRITE); + + return 0; +} + static int serv_read(struct triton_md_handler_t *h) { struct sockaddr_in addr; @@ -86,14 +201,16 @@ static int serv_read(struct triton_md_handler_t *h) memset(conn, 0, sizeof(*conn)); conn->hnd.fd = sock; conn->hnd.read = cln_read; - //conn->hnd.write = cln_write; + conn->hnd.write = cln_write; conn->recv_buf = _malloc(RECV_BUF_SIZE); INIT_LIST_HEAD(&conn->xmit_queue); triton_md_register_handler(&serv_ctx, &conn->hnd); triton_md_enable_handler(&conn->hnd,MD_MODE_READ); - send_banner(conn); + if (send_banner(conn)) + continue; + send_auth_request(conn); } return 0; } @@ -122,6 +239,14 @@ static void start_server(const char *host, int port) return; } + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + if (host) + addr.sin_addr.s_addr = inet_addr(host); + else + addr.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind (serv_hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) { log_emerg("cli: failed to bind socket: %s\n", strerror(errno)); close(serv_hnd.fd); @@ -164,7 +289,7 @@ static void __init init(void) if (opt) host = opt; - if (!host || !port) { + if (!port) { log_emerg("cli: disabled\n"); return; } diff --git a/accel-pptpd/ctrl/l2tp/l2tp.c b/accel-pptpd/ctrl/l2tp/l2tp.c index 5bfe89ef..c9af9126 100644 --- a/accel-pptpd/ctrl/l2tp/l2tp.c +++ b/accel-pptpd/ctrl/l2tp/l2tp.c @@ -1015,6 +1015,8 @@ static void start_udp_server(void) log_emerg("l2tp: socket: %s\n", strerror(errno)); return; } + + memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(L2TP_PORT); diff --git a/accel-pptpd/ppp/ppp.c b/accel-pptpd/ppp/ppp.c index 6ba0dd7c..4cb5e163 100644 --- a/accel-pptpd/ppp/ppp.c +++ b/accel-pptpd/ppp/ppp.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "linux_ppp.h" #include @@ -17,6 +18,7 @@ #include "ppp.h" #include "ppp_fsm.h" #include "log.h" +#include "spinlock.h" #include "memdebug.h" @@ -25,6 +27,9 @@ int __export conf_ppp_verbose; static LIST_HEAD(layers); int __export sock_fd; +static spinlock_t seq_lock = SPINLOCK_INITIALIZER; +static uint64_t seq; + struct layer_node_t { struct list_head entry; @@ -58,21 +63,14 @@ static void _free_ppp(struct ppp_t *ppp) static void generate_sessionid(struct ppp_t *ppp) { - MD5_CTX ctx; - uint8_t md5[MD5_DIGEST_LENGTH]; - int i; - - MD5_Init(&ctx); - MD5_Update(&ctx,&ppp->unit_idx, 4); - MD5_Update(&ctx,&ppp->unit_fd, 4); - MD5_Update(&ctx,&ppp->chan_fd, 4); - MD5_Update(&ctx,&ppp->fd, 4); - MD5_Update(&ctx,&ppp->start_time, sizeof(time_t)); - MD5_Update(&ctx,ppp->ctrl->ctx, sizeof(void *)); - MD5_Final(md5,&ctx); - - for( i = 0; i < 16; i++) - sprintf(ppp->sessionid + i*2, "%02X", md5[i]); + unsigned long long sid; + + spin_lock(&seq_lock); + seq++; + sid = seq; + spin_unlock(&seq_lock); + + sprintf(ppp->sessionid, "%016llx", sid); } int __export establish_ppp(struct ppp_t *ppp) @@ -546,9 +544,24 @@ struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer return NULL; } +static void save_seq(void) +{ + FILE *f; + unsigned long long sid = seq; + char *opt = conf_get_opt("ppp", "seq-file"); + if (!opt) + opt = "/var/run/accel-pptp/seq"; + + f = fopen(opt, "w"); + fprintf(f, "%llu", sid); + fclose(f); +} + static void __init init(void) { char *opt; + FILE *f; + unsigned long long sid; sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if (sock_fd < 0) { @@ -559,5 +572,20 @@ static void __init init(void) opt = conf_get_opt("ppp", "verbose"); if (opt && atoi(opt) > 0) conf_ppp_verbose = 1; + + opt = conf_get_opt("ppp", "seq-file"); + if (!opt) + opt = "/var/run/accel-pptp/seq"; + + f = fopen(opt, "r"); + if (f) { + fscanf(f, "%llu", &sid); + seq = sid; + fclose(f); + } else + //log_emerg("ppp: failed to open seq-file (%s): %s\n", opt, strerror(errno)); + seq = (unsigned long long)random() * (unsigned long long)random(); + + atexit(save_seq); } diff --git a/accel-pptpd/ppp/ppp.h b/accel-pptpd/ppp/ppp.h index 563250ce..b36f0e42 100644 --- a/accel-pptpd/ppp/ppp.h +++ b/accel-pptpd/ppp/ppp.h @@ -43,7 +43,7 @@ #define PPP_LAYER_CCP 3 #define PPP_LAYER_IPCP 4 -#define PPP_SESSIONID_LEN 32 +#define PPP_SESSIONID_LEN 16 #define PPP_IFNAME_LEN 10 #define TERM_USER_REQUEST 1 diff --git a/contrib/gentoo/net-dialup/accel-pptp/accel-pptp-9999.ebuild b/contrib/gentoo/net-dialup/accel-pptp/accel-pptp-9999.ebuild index 645132d1..cd158d60 100644 --- a/contrib/gentoo/net-dialup/accel-pptp/accel-pptp-9999.ebuild +++ b/contrib/gentoo/net-dialup/accel-pptp/accel-pptp-9999.ebuild @@ -19,7 +19,7 @@ IUSE="postgres debug shaper" DEPEND=">=sys-libs/glibc-2.8 dev-libs/openssl dev-libs/libaio - shaper? ( =dev-libs/libnl-9999 ) + shaper? ( =dev-libs/libnl-2 ) postgres? ( >=dev-db/postgresql-base-8.1 )" RDEPEND="$DEPEND @@ -38,6 +38,7 @@ src_unpack () { src_prepare() { sed -i -e "/mkdir/d" "${S}/accel-pptpd/CMakeLists.txt" + sed -i -e "/echo/d" "${S}/accel-pptpd/CMakeLists.txt" sed -i -e "/INSTALL/d" "${S}/driver/CMakeLists.txt" } @@ -78,5 +79,6 @@ src_install() { newins "${S}/contrib/gentoo/net-dialup/accel-pptp/files/pptpd-confd" accel-pptpd dodir /var/log/accel-pptp + dodir /var/run/accel-pptp dodir /var/run/radattr } -- cgit v1.2.3