* fhandler_socket.cc (fhandler_socket::dup): If running impersonated,

revert to original account before calling fixup_before_fork_exec
	and impersonate again afterwards.  Change comment accordingly.
	Clean up error handling and debug output.
This commit is contained in:
Corinna Vinschen 2003-05-28 22:05:56 +00:00
parent b359e82cea
commit 72c1470d67
2 changed files with 24 additions and 10 deletions

View File

@ -1,3 +1,10 @@
2003-05-28 Corinna Vinschen <corinna@vinschen.de>
* fhandler_socket.cc (fhandler_socket::dup): If running impersonated,
revert to original account before calling fixup_before_fork_exec
and impersonate again afterwards. Change comment accordingly.
Clean up error handling and debug output.
2003-05-27 Thomas Pfaff <tpfaff@gmx.net> 2003-05-27 Thomas Pfaff <tpfaff@gmx.net>
* fhandler_socket.cc (sock_event::~sock_event): New method. * fhandler_socket.cc (sock_event::~sock_event): New method.

View File

@ -401,30 +401,37 @@ fhandler_socket::dup (fhandler_base *child)
fhs->set_sun_path (get_sun_path ()); fhs->set_sun_path (get_sun_path ());
fhs->set_socket_type (get_socket_type ()); fhs->set_socket_type (get_socket_type ());
/* Using WinSock2 methods for dup'ing sockets seem to collide /* Since WSADuplicateSocket() fails on NT systems when the process
with user context switches under... some... conditions. So we is currently impersonating a non-privileged account, we revert
drop this for NT systems at all and return to the good ol' to the original account before calling WSADuplicateSocket() and
DuplicateHandle way of life. This worked fine all the time on switch back afterwards as it's also in fork().
NT anyway and it's even a bit faster. */ If WSADuplicateSocket() still fails for some reason, we fall back
to DuplicateHandle(). */
WSASetLastError (0); WSASetLastError (0);
if (cygheap->user.issetuid ())
RevertToSelf ();
fhs->fixup_before_fork_exec (GetCurrentProcessId ()); fhs->fixup_before_fork_exec (GetCurrentProcessId ());
if (WSAGetLastError () != WSAEINVAL && winsock2_active) if (cygheap->user.issetuid ())
ImpersonateLoggedOnUser (cygheap->user.token);
if (winsock2_active && !WSAGetLastError ())
{ {
fhs->fixup_after_fork (hMainProc); fhs->fixup_after_fork (hMainProc);
if (WSAGetLastError () != WSAEINVAL) if (get_io_handle() != (HANDLE) INVALID_SOCKET)
return get_io_handle () == (HANDLE) INVALID_SOCKET; return 0;
} }
debug_printf ("WSADuplicateSocket failed, trying DuplicateHandle"); debug_printf ("WSADuplicateSocket failed, trying DuplicateHandle");
/* We don't call fhandler_base::dup here since that requires to /* We don't call fhandler_base::dup here since that requires to
have winsock called from fhandler_base and it creates only have winsock called from fhandler_base and it creates only
inheritable sockets which is wrong for winsock2. */ inheritable sockets which is wrong for winsock2. */
HANDLE nh; HANDLE nh;
if (!DuplicateHandle (hMainProc, get_io_handle (), hMainProc, &nh, 0, if (!DuplicateHandle (hMainProc, get_io_handle (), hMainProc, &nh, 0,
!winsock2_active, DUPLICATE_SAME_ACCESS)) !winsock2_active, DUPLICATE_SAME_ACCESS))
{ {
system_printf ("dup(%s) failed, handle %x, %E", system_printf ("!DuplicateHandle(%x) failed, %E", get_io_handle ());
get_name (), get_io_handle ());
__seterrno (); __seterrno ();
return -1; return -1;
} }