diff -rupN stunnel-4.44/doc/stunnel.8 stunnel-4.44-xforwardedfor/doc/stunnel.8 --- stunnel-4.44/doc/stunnel.8 2011-09-07 13:21:06.000000000 -0700 +++ stunnel-4.44-xforwardedfor/doc/stunnel.8 2011-09-23 13:02:58.000000000 -0700 @@ -670,6 +670,10 @@ This options has been renamed to \fInone .RE .RS 4 .RE +.IP "\fBxforwardedfor\fR = yes | no" 4 +.IX Item "xforwardedfor = yes | no" +append an 'X-Forwarded-For:' HTTP request header providing the +client's IP address to the server. .IP "\fBverify\fR = level" 4 .IX Item "verify = level" verify peer certificate diff -rupN stunnel-4.44/doc/stunnel.fr.8 stunnel-4.44-xforwardedfor/doc/stunnel.fr.8 --- stunnel-4.44/doc/stunnel.fr.8 2011-09-07 13:21:06.000000000 -0700 +++ stunnel-4.44-xforwardedfor/doc/stunnel.fr.8 2011-09-23 12:45:49.000000000 -0700 @@ -393,6 +393,10 @@ Durée d'attente sur une connexion inact .IP "\fBtransparent\fR = yes | no (Unix seulement)" 4 .IX Item "transparent = yes | no (Unix seulement)" Mode mandataire transparent +.IP "\fBxforwardedfor\fR = yes | no" 4 +.IX Item "xforwardedfor = yes | no" +Ajoute un en-tête 'X-Forwarded-For:' dans la requête HTTP fournissant +au serveur l'adresse IP du client. .Sp Ré\-écrit les adresses pour qu'elles apparaissent provenir de la machine client \s-1SSL\s0 plutôt que de celle qui exécute \fBstunnel\fR. diff -rupN stunnel-4.44/src/client.c stunnel-4.44-xforwardedfor/src/client.c --- stunnel-4.44/src/client.c 2011-09-07 13:00:10.000000000 -0700 +++ stunnel-4.44-xforwardedfor/src/client.c 2011-09-23 12:45:49.000000000 -0700 @@ -81,6 +81,12 @@ CLI *alloc_client_session(SERVICE_OPTION } str_detach(c); c->opt=opt; + /* some options need space to add some information */ + if (c->opt->option.xforwardedfor) + c->buffsize = BUFFSIZE - BUFF_RESERVED; + else + c->buffsize = BUFFSIZE; + c->crlf_seen=0; c->local_rfd.fd=rfd; c->local_wfd.fd=wfd; return c; @@ -383,6 +389,29 @@ static void init_ssl(CLI *c) { } } +/* Moves all data from the buffer between positions and + * to insert of length . and are updated to their + * new respective values, and the number of characters inserted is returned. + * If is too long, nothing is done and -1 is returned. + * Note that neither nor can be NULL. + */ +static int buffer_insert_with_len(char *buffer, int *start, int *stop, int limit, char *string, int len) { + if (len > limit - *stop) + return -1; + if (*start > *stop) + return -1; + memmove(buffer + *start + len, buffer + *start, *stop - *start); + memcpy(buffer + *start, string, len); + *start += len; + *stop += len; + return len; +} + +static int buffer_insert(char *buffer, int *start, int *stop, int limit, char *string) { + return buffer_insert_with_len(buffer, start, stop, limit, string, strlen(string)); +} + + /****************************** transfer data */ static void transfer(CLI *c) { int watchdog=0; /* a counter to detect an infinite loop */ @@ -401,7 +430,7 @@ static void transfer(CLI *c) { do { /* main loop of client data transfer */ /****************************** initialize *_wants_* */ read_wants_read= - ssl_open_rd && c->ssl_ptrssl_ptrbuffsize && !read_wants_write; write_wants_write= ssl_open_wr && c->sock_ptr && !write_wants_read; @@ -410,7 +439,7 @@ static void transfer(CLI *c) { /* for plain socket open data strem = open file descriptor */ /* make sure to add each open socket to receive exceptions! */ if(sock_open_rd) - s_poll_add(&c->fds, c->sock_rfd->fd, c->sock_ptrfds, c->sock_rfd->fd, c->sock_ptrbuffsize, 0); if(sock_open_wr) s_poll_add(&c->fds, c->sock_wfd->fd, 0, c->ssl_ptr); /* for SSL assume that sockets are open if there any pending requests */ @@ -544,7 +573,7 @@ static void transfer(CLI *c) { /****************************** read from socket */ if(sock_open_rd && sock_can_rd) { num=readsocket(c->sock_rfd->fd, - c->sock_buff+c->sock_ptr, BUFFSIZE-c->sock_ptr); + c->sock_buff+c->sock_ptr, c->buffsize-c->sock_ptr); switch(num) { case -1: parse_socket_error(c, "readsocket"); @@ -580,7 +609,7 @@ static void transfer(CLI *c) { /****************************** update *_wants_* based on new *_ptr */ /* this update is also required for SSL_pending() to be used */ read_wants_read= - ssl_open_rd && c->ssl_ptrssl_ptrbuffsize && !read_wants_write; write_wants_write= ssl_open_wr && c->sock_ptr && !write_wants_read; @@ -590,10 +619,72 @@ static void transfer(CLI *c) { * writesocket() above made some room in c->ssl_buff */ (read_wants_write && ssl_can_wr)) { read_wants_write=0; - num=SSL_read(c->ssl, c->ssl_buff+c->ssl_ptr, BUFFSIZE-c->ssl_ptr); + num=SSL_read(c->ssl, c->ssl_buff+c->ssl_ptr, c->buffsize-c->ssl_ptr); switch(err=SSL_get_error(c->ssl, num)) { case SSL_ERROR_NONE: - c->ssl_ptr+=num; + if (c->buffsize != BUFFSIZE && c->opt->option.xforwardedfor) { /* some work left to do */ + int last = c->ssl_ptr; + c->ssl_ptr += num; + + /* Look for end of HTTP headers between last and ssl_ptr. + * To achieve this reliably, we have to count the number of + * successive [CR]LF and to memorize it in case it's spread + * over multiple segments. --WT. + */ + while (last < c->ssl_ptr) { + if (c->ssl_buff[last] == '\n') { + if (++c->crlf_seen == 2) + break; + } else if (last < c->ssl_ptr - 1 && + c->ssl_buff[last] == '\r' && + c->ssl_buff[last+1] == '\n') { + if (++c->crlf_seen == 2) + break; + last++; + } else if (c->ssl_buff[last] != '\r') + /* don't refuse '\r' because we may get a '\n' on next read */ + c->crlf_seen = 0; + last++; + } + if (c->crlf_seen >= 2) { + /* We have all the HTTP headers now. We don't need to + * reserve any space anymore. points to the + * first byte of unread data, and points to the + * exact location where we want to insert our headers, + * which is right before the empty line. + */ + c->buffsize = BUFFSIZE; + + if (c->opt->option.xforwardedfor) { + /* X-Forwarded-For: xxxx \r\n\0 */ + char xforw[17 + IPLEN + 3]; + + /* We will insert our X-Forwarded-For: header here. + * We need to write the IP address, but if we use + * sprintf, it will pad with the terminating 0. + * So we will pass via a temporary buffer allocated + * on the stack. + */ + memcpy(xforw, "X-Forwarded-For: ", 17); + if (getnameinfo(&c->peer_addr.addr[0].sa, + addr_len(c->peer_addr.addr[0]), + xforw + 17, IPLEN, NULL, 0, + NI_NUMERICHOST) == 0) { + strcat(xforw + 17, "\r\n"); + buffer_insert(c->ssl_buff, &last, &c->ssl_ptr, + c->buffsize, xforw); + } + /* last still points to the \r\n and ssl_ptr to the + * end of the buffer, so we may add as many headers + * as wee need to. + */ + } + } + } + else + c->ssl_ptr+=num; + + watchdog=0; /* reset watchdog */ break; case SSL_ERROR_WANT_WRITE: diff -rupN stunnel-4.44/src/common.h stunnel-4.44-xforwardedfor/src/common.h --- stunnel-4.44/src/common.h 2011-09-10 11:26:34.000000000 -0700 +++ stunnel-4.44-xforwardedfor/src/common.h 2011-09-23 18:43:13.000000000 -0700 @@ -52,6 +52,9 @@ /* I/O buffer size */ #define BUFFSIZE 16384 +/* maximum space reserved for header insertion in BUFFSIZE */ +#define BUFF_RESERVED 1024 + /* IP address and TCP port textual representation length */ #define IPLEN 128 diff -rupN stunnel-4.44/src/options.c stunnel-4.44-xforwardedfor/src/options.c --- stunnel-4.44/src/options.c 2011-09-10 09:44:16.000000000 -0700 +++ stunnel-4.44-xforwardedfor/src/options.c 2011-09-23 12:45:49.000000000 -0700 @@ -811,6 +811,29 @@ static char *parse_service_option(CMD cm } #endif + /* xforwardedfor */ + switch(cmd) { + case CMD_INIT: + section->option.xforwardedfor=0; + break; + case CMD_EXEC: + if(strcasecmp(opt, "xforwardedfor")) + break; + if(!strcasecmp(arg, "yes")) + section->option.xforwardedfor=1; + else if(!strcasecmp(arg, "no")) + section->option.xforwardedfor=0; + else + return "argument should be either 'yes' or 'no'"; + return NULL; /* OK */ + case CMD_DEFAULT: + break; + case CMD_HELP: + s_log(LOG_NOTICE, "%-15s = yes|no append an HTTP X-Forwarded-For header", + "xforwardedfor"); + break; + } + /* exec */ switch(cmd) { case CMD_INIT: diff -rupN stunnel-4.44/src/prototypes.h stunnel-4.44-xforwardedfor/src/prototypes.h --- stunnel-4.44/src/prototypes.h 2011-09-13 06:36:52.000000000 -0700 +++ stunnel-4.44-xforwardedfor/src/prototypes.h 2011-09-23 18:51:00.000000000 -0700 @@ -183,6 +183,7 @@ typedef struct service_options_struct { unsigned int accept:1; /* endpoint: accept */ unsigned int client:1; unsigned int delayed_lookup:1; + unsigned int xforwardedfor:1; #ifdef USE_LIBWRAP unsigned int libwrap:1; #endif @@ -385,6 +386,8 @@ typedef struct { FD *ssl_rfd, *ssl_wfd; /* read and write SSL descriptors */ int sock_bytes, ssl_bytes; /* bytes written to socket and SSL */ s_poll_set fds; /* file descriptors */ + int buffsize; /* current buffer size, may be lower than BUFFSIZE */ + int crlf_seen; /* the number of successive CRLF seen */ } CLI; CLI *alloc_client_session(SERVICE_OPTIONS *, int, int);