Introduce and apply krb5_storage_from_socket

On Windows a file descriptor is an int value allocated by the
local module instance of the C Run Time Library.  A socket handle is a
SOCKET value allocated by a Winsock Provider for the requested family and
protocol.   These two values cannot be mixed and there is no mechanism for
converting between the two.   The _get_osfhandle() and _open_osfhandle()
functions can work with a standard HANDLE (file, pipe, etc) but cannot be
used for a SOCKET.

The Heimdal krb5_storage_from_fd() routine counted on the osf conversion
functions working on SOCKET values.  Since they do not any attempt to call
krb5_storage_from_fd() on a socket resulted in an assertion being thrown
by the C RTL.

Another problem is SOCKET value truncation when storing a 64-bit value
into a 32-bit int.

To address these problems a new krb5_storage_from_socket() routine is
introduced.  This routine setups a krb5_storage that stores a socket value
as a rk_socket_t and provides a set of helper routines that always use
network ready functions.

The krb5_storage_from_fd() routines no longer use net_read() and
net_write() but provide helpers that follow their logic so that pipes can
be processed.

All call sites that allocate a socket now store the socket as rk_socket_t
and call krb5_storage_from_socket().

All locations that previously called the bare close() on a socket value
now call rk_closesocket().

Change-Id: I045f775b2a5dbf5cf803751409490bc27fffe597
This commit is contained in:
Jeffrey Altman
2014-02-04 23:02:01 -05:00
parent fdabfd6040
commit dba026b5ef
13 changed files with 252 additions and 53 deletions

View File

@@ -43,13 +43,47 @@ typedef struct fd_storage {
static ssize_t
fd_fetch(krb5_storage * sp, void *data, size_t size)
{
return net_read(FD(sp), data, size);
char *cbuf = (char *)data;
ssize_t count;
size_t rem = size;
/* similar pattern to net_read() to support pipes */
while (rem > 0) {
count = read (FD(sp), cbuf, rem);
if (count < 0) {
if (errno == EINTR)
continue;
else
return count;
} else if (count == 0) {
return count;
}
cbuf += count;
rem -= count;
}
return size;
}
static ssize_t
fd_store(krb5_storage * sp, const void *data, size_t size)
{
return net_write(FD(sp), data, size);
const char *cbuf = (const char *)data;
ssize_t count;
size_t rem = size;
/* similar pattern to net_write() to support pipes */
while (rem > 0) {
count = write(FD(sp), cbuf, rem);
if (count < 0) {
if (errno == EINTR)
continue;
else
return count;
}
cbuf += count;
rem -= count;
}
return size;
}
static off_t
@@ -91,26 +125,26 @@ fd_free(krb5_storage * sp)
* @sa krb5_storage_from_mem()
* @sa krb5_storage_from_readonly_mem()
* @sa krb5_storage_from_data()
* @sa krb5_storage_from_socket()
*/
KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
krb5_storage_from_fd(krb5_socket_t fd_in)
krb5_storage_from_fd(int fd_in)
{
krb5_storage *sp;
int saved_errno;
int fd;
#ifdef SOCKET_IS_NOT_AN_FD
#ifdef _MSC_VER
if (_get_osfhandle(fd_in) != -1) {
fd = dup(fd_in);
} else {
fd = _open_osfhandle(fd_in, 0);
}
/*
* This function used to try to pass the input to
* _get_osfhandle() to test if the value is a HANDLE
* but this doesn't work because doing so throws an
* exception that will result in Watson being triggered
* to file a Windows Error Report.
*/
fd = _dup(fd_in);
#else
#error Dont know how to deal with fd that may or may not be a socket.
#endif
#else /* SOCKET_IS_NOT_AN_FD */
fd = dup(fd_in);
#endif