diff --git a/common.c b/common.c index 49b29e7..cf92272 100644 --- a/common.c +++ b/common.c @@ -550,7 +550,7 @@ void resolve_name(struct addrinfo **out, char* fullname) } /* Log to syslog or stderr if foreground */ -void log_message(int type, char* msg, ...) +void log_message(int type, const char* msg, ...) { va_list ap; @@ -562,48 +562,63 @@ void log_message(int type, char* msg, ...) va_end(ap); } -/* syslogs who connected to where */ -void log_connection(struct connection *cnx) + +/* Fills a connection description; returns 0 on failure */ +int get_connection_desc(struct connection_desc* desc, const struct connection *cnx) { + int res; struct addrinfo addr; struct sockaddr_storage ss; -#define MAX_NAMELENGTH (NI_MAXHOST + NI_MAXSERV + 1) - char peer[MAX_NAMELENGTH], service[MAX_NAMELENGTH], - local[MAX_NAMELENGTH], target[MAX_NAMELENGTH]; - int res; - - if (cnx->proto->log_level < 1) - return; addr.ai_addr = (struct sockaddr*)&ss; addr.ai_addrlen = sizeof(ss); res = getpeername(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen); - if (res == -1) return; /* Can happen if connection drops before we get here. + if (res == -1) return 0; /* Can happen if connection drops before we get here. In that case, don't log anything (there is no connection) */ - sprintaddr(peer, sizeof(peer), &addr); + sprintaddr(desc->peer, sizeof(desc->peer), &addr); addr.ai_addrlen = sizeof(ss); res = getsockname(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen); - if (res == -1) return; - sprintaddr(service, sizeof(service), &addr); + if (res == -1) return 0; + sprintaddr(desc->service, sizeof(desc->service), &addr); addr.ai_addrlen = sizeof(ss); res = getpeername(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen); - if (res == -1) return; - sprintaddr(target, sizeof(target), &addr); + if (res == -1) return 0; + sprintaddr(desc->target, sizeof(desc->target), &addr); addr.ai_addrlen = sizeof(ss); res = getsockname(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen); - if (res == -1) return; - sprintaddr(local, sizeof(local), &addr); + if (res == -1) return 0; + sprintaddr(desc->local, sizeof(desc->local), &addr); + + return 1; +} + +/* syslogs who connected to where + * desc: string description of the connection. if NULL, log_connection will + * manage on its own + * cnx: connection descriptor + * */ +void log_connection(struct connection_desc* desc, const struct connection *cnx) +{ + struct connection_desc d; + + if (cnx->proto->log_level < 1) + return; + + if (!desc) { + desc = &d; + get_connection_desc(desc, cnx); + } log_message(LOG_INFO, "%s:connection from %s to %s forwarded from %s to %s\n", cnx->proto->name, - peer, - service, - local, - target); + desc->peer, + desc->service, + desc->local, + desc->target); } diff --git a/common.h b/common.h index 37ccebc..bc1f16d 100644 --- a/common.h +++ b/common.h @@ -107,6 +107,13 @@ struct connection { #define FD_NODATA -1 #define FD_STALLED -2 +/* String description of a connection */ +#define MAX_NAMELENGTH (NI_MAXHOST + NI_MAXSERV + 1) +struct connection_desc { + char peer[MAX_NAMELENGTH], service[MAX_NAMELENGTH], + local[MAX_NAMELENGTH], target[MAX_NAMELENGTH]; +}; + /* common.c */ void init_cnx(struct connection *cnx); @@ -114,13 +121,14 @@ int connect_addr(struct connection *cnx, int fd_from); int fd2fd(struct queue *target, struct queue *from); char* sprintaddr(char* buf, size_t size, struct addrinfo *a); void resolve_name(struct addrinfo **out, char* fullname); -void log_connection(struct connection *cnx); +int get_connection_desc(struct connection_desc* desc, const struct connection *cnx); +void log_connection(struct connection_desc* desc, const struct connection *cnx); int check_access_rights(int in_socket, const char* service); void setup_signals(void); void setup_syslog(const char* bin_name); void drop_privileges(const char* user_name, const char* chroot_path); void write_pid_file(const char* pidfile); -void log_message(int type, char* msg, ...); +void log_message(int type, const char* msg, ...); void dump_connection(struct connection *cnx); int resolve_split_name(struct addrinfo **out, char* hostname, char* port); diff --git a/sslh-fork.c b/sslh-fork.c index 164be80..0c16af6 100644 --- a/sslh-fork.c +++ b/sslh-fork.c @@ -72,6 +72,7 @@ void start_shoveler(int in_socket) int res = PROBE_AGAIN; int out_socket; struct connection cnx; + struct connection_desc desc; init_cnx(&cnx); cnx.q[0].fd = in_socket; @@ -111,7 +112,8 @@ void start_shoveler(int in_socket) cnx.q[1].fd = out_socket; - log_connection(&cnx); + get_connection_desc(&desc, &cnx); + log_connection(&desc, &cnx); flush_deferred(&cnx.q[1]); diff --git a/sslh-select.c b/sslh-select.c index a770bde..cd2c516 100644 --- a/sslh-select.c +++ b/sslh-select.c @@ -143,7 +143,7 @@ int connect_queue(struct connection *cnx, fd_set *fds_r, fd_set *fds_w) q->fd = connect_addr(cnx, cnx->q[0].fd); if ((q->fd != -1) && fd_is_in_range(q->fd)) { - log_connection(cnx); + log_connection(NULL, cnx); set_nonblock(q->fd); flush_deferred(q); if (q->deferred_data) { @@ -262,7 +262,7 @@ void connect_proxy(struct connection *cnx) cnx->q[1].fd = out_socket; - log_connection(cnx); + log_connection(NULL, cnx); shovel_single(cnx);