| 1 |
/* |
| 2 |
* stunnel TLS offloading and load-balancing proxy |
| 3 |
* Copyright (C) 1998-2026 Michal Trojnara <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 8 |
* option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, see <http://www.gnu.org/licenses>. |
| 17 |
* |
| 18 |
* Linking stunnel statically or dynamically with other modules is making |
| 19 |
* a combined work based on stunnel. Thus, the terms and conditions of |
| 20 |
* the GNU General Public License cover the whole combination. |
| 21 |
* |
| 22 |
* In addition, as a special exception, the copyright holder of stunnel |
| 23 |
* gives you permission to combine stunnel with free software programs or |
| 24 |
* libraries that are released under the GNU LGPL and with code included |
| 25 |
* in the standard release of OpenSSL under the OpenSSL License (or |
| 26 |
* modified versions of such code, with unchanged license). You may copy |
| 27 |
* and distribute such a system following the terms of the GNU GPL for |
| 28 |
* stunnel and the licenses of the other code concerned. |
| 29 |
* |
| 30 |
* Note that people who make modified versions of stunnel are not obligated |
| 31 |
* to grant this special exception for their modified versions; it is their |
| 32 |
* choice whether to do so. The GNU General Public License gives permission |
| 33 |
* to release a modified version without this exception; this exception |
| 34 |
* also makes it possible to release a modified version which carries |
| 35 |
* forward this exception. |
| 36 |
*/ |
| 37 |
|
| 38 |
#include "prototypes.h" |
| 39 |
|
| 40 |
#ifdef USE_LIBWRAP |
| 41 |
|
| 42 |
#include <tcpd.h> |
| 43 |
|
| 44 |
#if defined(USE_PTHREAD) && !defined(__CYGWIN__) |
| 45 |
/* http://wiki.osdev.org/Cygwin_Issues#Passing_file_descriptors */ |
| 46 |
#define USE_LIBWRAP_POOL |
| 47 |
#endif /* USE_PTHREAD && !__CYGWIN__ */ |
| 48 |
|
| 49 |
NOEXPORT uint8_t check(char *, int); |
| 50 |
|
| 51 |
int allow_severity=LOG_NOTICE, deny_severity=LOG_WARNING; |
| 52 |
|
| 53 |
#ifdef USE_LIBWRAP_POOL |
| 54 |
#define SERVNAME_LEN 256 |
| 55 |
|
| 56 |
NOEXPORT ssize_t read_fd(int, void *, size_t, int *); |
| 57 |
NOEXPORT ssize_t write_fd(int, void *, size_t, int); |
| 58 |
|
| 59 |
unsigned num_processes=0; |
| 60 |
static int *ipc_socket, *busy; |
| 61 |
#endif /* USE_LIBWRAP_POOL */ |
| 62 |
|
| 63 |
#ifdef __GNUC__ |
| 64 |
#pragma GCC diagnostic push |
| 65 |
#pragma GCC diagnostic ignored "-Wunused-result" |
| 66 |
#endif /* __GNUC__ */ |
| 67 |
int libwrap_init(void) { |
| 68 |
#ifdef USE_LIBWRAP_POOL |
| 69 |
unsigned i, j; |
| 70 |
int rfd; |
| 71 |
uint8_t result; |
| 72 |
char servname[SERVNAME_LEN]; |
| 73 |
static int initialized=0; |
| 74 |
SERVICE_OPTIONS *opt; |
| 75 |
|
| 76 |
if(initialized) /* during startup or previous configuration file reload */ |
| 77 |
return 0; |
| 78 |
for(opt=service_options.next; opt; opt=opt->next) |
| 79 |
if(opt->option.libwrap) /* libwrap is enabled for this service */ |
| 80 |
break; |
| 81 |
if(!opt) /* disabled for all sections or inetd mode (no sections) */ |
| 82 |
return 0; |
| 83 |
|
| 84 |
num_processes=LIBWRAP_CLIENTS; |
| 85 |
ipc_socket=str_alloc(2*num_processes*sizeof(int)); |
| 86 |
busy=str_alloc(num_processes*sizeof(int)); |
| 87 |
for(i=0; i<num_processes; ++i) { /* spawn a child */ |
| 88 |
if(s_socketpair(AF_UNIX, SOCK_STREAM, 0, ipc_socket+2*i, 0, "libwrap_init")) |
| 89 |
return 1; |
| 90 |
switch(fork()) { |
| 91 |
case -1: /* error */ |
| 92 |
ioerror("fork"); |
| 93 |
return 1; |
| 94 |
case 0: /* child */ |
| 95 |
tls_alloc(NULL, ui_tls, "libwrap"); |
| 96 |
drop_privileges(0); /* libwrap processes are not chrooted */ |
| 97 |
close(0); /* stdin */ |
| 98 |
close(1); /* stdout */ |
| 99 |
if(!global_options.option.log_stderr) /* for logging in read_fd */ |
| 100 |
close(2); /* stderr */ |
| 101 |
for(j=0; j<=i; ++j) /* close parent-side sockets created so far */ |
| 102 |
close(ipc_socket[2*j]); |
| 103 |
while(1) { /* main libwrap child loop */ |
| 104 |
if(read_fd(ipc_socket[2*i+1], servname, SERVNAME_LEN, &rfd)<=0) |
| 105 |
_exit(0); |
| 106 |
result=check(servname, rfd); |
| 107 |
write(ipc_socket[2*i+1], &result, sizeof result); |
| 108 |
if(rfd>=0) |
| 109 |
close(rfd); |
| 110 |
} |
| 111 |
default: /* parent */ |
| 112 |
close(ipc_socket[2*i+1]); /* child-side socket */ |
| 113 |
} |
| 114 |
} |
| 115 |
initialized=1; |
| 116 |
#endif /* USE_LIBWRAP_POOL */ |
| 117 |
return 0; |
| 118 |
} |
| 119 |
#ifdef __GNUC__ |
| 120 |
#pragma GCC diagnostic pop |
| 121 |
#endif /* __GNUC__ */ |
| 122 |
|
| 123 |
void libwrap_auth(CLI *c) { |
| 124 |
uint8_t result=0; /* deny by default */ |
| 125 |
#ifdef USE_LIBWRAP_POOL |
| 126 |
jmp_buf exception_buffer, *exception_backup; |
| 127 |
static volatile unsigned num_busy=0, roundrobin=0; |
| 128 |
unsigned my_process; |
| 129 |
int retval; |
| 130 |
static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; |
| 131 |
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER; |
| 132 |
#endif /* USE_LIBWRAP_POOL */ |
| 133 |
|
| 134 |
if(!c->opt->option.libwrap) /* libwrap is disabled for this service */ |
| 135 |
return; /* allow connection */ |
| 136 |
#ifdef HAVE_STRUCT_SOCKADDR_UN |
| 137 |
if(c->peer_addr.sa.sa_family==AF_UNIX) { |
| 138 |
s_log(LOG_INFO, "Libwrap is not supported on Unix sockets"); |
| 139 |
return; |
| 140 |
} |
| 141 |
#endif |
| 142 |
#ifdef USE_LIBWRAP_POOL |
| 143 |
if(num_processes) { |
| 144 |
s_log(LOG_DEBUG, "Waiting for a libwrap process"); |
| 145 |
|
| 146 |
retval=pthread_mutex_lock(&mutex); |
| 147 |
if(retval) { |
| 148 |
errno=retval; |
| 149 |
ioerror("pthread_mutex_lock"); |
| 150 |
} |
| 151 |
while(num_busy==num_processes) { /* all child processes are busy */ |
| 152 |
retval=pthread_cond_wait(&cond, &mutex); |
| 153 |
if(retval) { |
| 154 |
errno=retval; |
| 155 |
ioerror("pthread_cond_wait"); |
| 156 |
} |
| 157 |
} |
| 158 |
while(busy[roundrobin]) /* find a free child process */ |
| 159 |
roundrobin=(roundrobin+1)%num_processes; |
| 160 |
my_process=roundrobin; /* the process allocated by this thread */ |
| 161 |
++num_busy; /* the child process has been allocated */ |
| 162 |
busy[my_process]=1; /* mark the child process as busy */ |
| 163 |
retval=pthread_mutex_unlock(&mutex); |
| 164 |
if(retval) { |
| 165 |
errno=retval; |
| 166 |
ioerror("pthread_mutex_unlock"); |
| 167 |
} |
| 168 |
|
| 169 |
s_log(LOG_DEBUG, "Acquired libwrap process #%d", my_process); |
| 170 |
exception_backup=c->exception_pointer; |
| 171 |
c->exception_pointer=&exception_buffer; |
| 172 |
if(!setjmp(exception_buffer)) { |
| 173 |
write_fd(ipc_socket[2*my_process], c->opt->servname, |
| 174 |
strlen(c->opt->servname)+1, c->local_rfd.fd); |
| 175 |
s_read(c, ipc_socket[2*my_process], &result, sizeof result); |
| 176 |
} |
| 177 |
c->exception_pointer=exception_backup; |
| 178 |
s_log(LOG_DEBUG, "Releasing libwrap process #%d", my_process); |
| 179 |
|
| 180 |
retval=pthread_mutex_lock(&mutex); |
| 181 |
if(retval) { |
| 182 |
errno=retval; |
| 183 |
ioerror("pthread_mutex_lock"); |
| 184 |
} |
| 185 |
busy[my_process]=0; /* mark the child process as free */ |
| 186 |
--num_busy; /* the child process has been released */ |
| 187 |
retval=pthread_cond_signal(&cond); /* signal a waiting thread */ |
| 188 |
if(retval) { |
| 189 |
errno=retval; |
| 190 |
ioerror("pthread_cond_signal"); |
| 191 |
} |
| 192 |
retval=pthread_mutex_unlock(&mutex); |
| 193 |
if(retval) { |
| 194 |
errno=retval; |
| 195 |
ioerror("pthread_mutex_unlock"); |
| 196 |
} |
| 197 |
|
| 198 |
s_log(LOG_DEBUG, "Released libwrap process #%d", my_process); |
| 199 |
} else |
| 200 |
#endif /* USE_LIBWRAP_POOL */ |
| 201 |
{ /* use original, synchronous libwrap calls */ |
| 202 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_LIBWRAP]); |
| 203 |
result=check(c->opt->servname, c->local_rfd.fd); |
| 204 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_LIBWRAP]); |
| 205 |
} |
| 206 |
if(!result) { |
| 207 |
s_log(LOG_WARNING, "Service [%s] REFUSED by libwrap from %s", |
| 208 |
c->opt->servname, c->accepted_address); |
| 209 |
s_log(LOG_DEBUG, "See hosts_access(5) manual for details"); |
| 210 |
throw_exception(c, 1); |
| 211 |
} |
| 212 |
s_log(LOG_DEBUG, "Service [%s] permitted by libwrap from %s", |
| 213 |
c->opt->servname, c->accepted_address); |
| 214 |
} |
| 215 |
|
| 216 |
NOEXPORT uint8_t check(char *name, int fd) { |
| 217 |
struct request_info request; |
| 218 |
|
| 219 |
request_init(&request, RQ_DAEMON, name, RQ_FILE, fd, 0); |
| 220 |
fromhost(&request); |
| 221 |
return hosts_access(&request)!=0; |
| 222 |
} |
| 223 |
|
| 224 |
#ifdef USE_LIBWRAP_POOL |
| 225 |
|
| 226 |
NOEXPORT ssize_t read_fd(SOCKET fd, void *ptr, size_t nbytes, SOCKET *recvfd) { |
| 227 |
struct msghdr msg; |
| 228 |
struct iovec iov[1]; |
| 229 |
ssize_t n; |
| 230 |
|
| 231 |
#ifdef HAVE_MSGHDR_MSG_CONTROL |
| 232 |
union { |
| 233 |
struct cmsghdr cm; |
| 234 |
char control[CMSG_SPACE(sizeof(int))]; |
| 235 |
} control_un; |
| 236 |
struct cmsghdr *cmptr; |
| 237 |
|
| 238 |
msg.msg_control=control_un.control; |
| 239 |
msg.msg_controllen=sizeof control_un.control; |
| 240 |
#else |
| 241 |
int newfd; |
| 242 |
|
| 243 |
msg.msg_accrights=(caddr_t)&newfd; |
| 244 |
msg.msg_accrightslen=sizeof(int); |
| 245 |
#endif |
| 246 |
|
| 247 |
msg.msg_name=NULL; |
| 248 |
msg.msg_namelen=0; |
| 249 |
|
| 250 |
iov[0].iov_base=ptr; |
| 251 |
iov[0].iov_len=nbytes; |
| 252 |
msg.msg_iov=iov; |
| 253 |
msg.msg_iovlen=1; |
| 254 |
|
| 255 |
*recvfd=INVALID_SOCKET; /* descriptor was not passed */ |
| 256 |
n=recvmsg(fd, &msg, 0); |
| 257 |
if(n<=0) |
| 258 |
return n; |
| 259 |
|
| 260 |
#ifdef HAVE_MSGHDR_MSG_CONTROL |
| 261 |
cmptr=CMSG_FIRSTHDR(&msg); |
| 262 |
if(!cmptr || cmptr->cmsg_len!=CMSG_LEN(sizeof(int))) |
| 263 |
return n; |
| 264 |
if(cmptr->cmsg_level!=SOL_SOCKET) { |
| 265 |
s_log(LOG_ERR, "control level != SOL_SOCKET"); |
| 266 |
return -1; |
| 267 |
} |
| 268 |
if(cmptr->cmsg_type!=SCM_RIGHTS) { |
| 269 |
s_log(LOG_ERR, "control type != SCM_RIGHTS"); |
| 270 |
return -1; |
| 271 |
} |
| 272 |
memcpy(recvfd, CMSG_DATA(cmptr), sizeof(int)); |
| 273 |
#else |
| 274 |
if(msg.msg_accrightslen==sizeof(int)) |
| 275 |
*recvfd=newfd; |
| 276 |
#endif |
| 277 |
|
| 278 |
return n; |
| 279 |
} |
| 280 |
|
| 281 |
NOEXPORT ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd) { |
| 282 |
struct msghdr msg; |
| 283 |
struct iovec iov[1]; |
| 284 |
|
| 285 |
#ifdef HAVE_MSGHDR_MSG_CONTROL |
| 286 |
union { |
| 287 |
struct cmsghdr cm; |
| 288 |
char control[CMSG_SPACE(sizeof(int))]; |
| 289 |
} control_un; |
| 290 |
struct cmsghdr *cmptr; |
| 291 |
|
| 292 |
msg.msg_control=control_un.control; |
| 293 |
msg.msg_controllen=sizeof control_un.control; |
| 294 |
|
| 295 |
cmptr=CMSG_FIRSTHDR(&msg); |
| 296 |
cmptr->cmsg_len=CMSG_LEN(sizeof(int)); |
| 297 |
cmptr->cmsg_level=SOL_SOCKET; |
| 298 |
cmptr->cmsg_type=SCM_RIGHTS; |
| 299 |
memcpy(CMSG_DATA(cmptr), &sendfd, sizeof(int)); |
| 300 |
#else |
| 301 |
msg.msg_accrights=(caddr_t)&sendfd; |
| 302 |
msg.msg_accrightslen=sizeof(int); |
| 303 |
#endif |
| 304 |
|
| 305 |
msg.msg_name=NULL; |
| 306 |
msg.msg_namelen=0; |
| 307 |
|
| 308 |
iov[0].iov_base=ptr; |
| 309 |
iov[0].iov_len=nbytes; |
| 310 |
msg.msg_iov=iov; |
| 311 |
msg.msg_iovlen=1; |
| 312 |
|
| 313 |
return sendmsg(fd, &msg, 0); |
| 314 |
} |
| 315 |
|
| 316 |
#endif /* USE_LIBWRAP_POOL */ |
| 317 |
|
| 318 |
#endif /* USE_LIBWRAP */ |
| 319 |
|
| 320 |
/* end of libwrap.c */ |