Blame src/tests/gss-threads/gss-misc.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 1994 by OpenVision Technologies, Inc.
Packit fd8b60
 *
Packit fd8b60
 * Permission to use, copy, modify, distribute, and sell this software
Packit fd8b60
 * and its documentation for any purpose is hereby granted without fee,
Packit fd8b60
 * provided that the above copyright notice appears in all copies and
Packit fd8b60
 * that both that copyright notice and this permission notice appear in
Packit fd8b60
 * supporting documentation, and that the name of OpenVision not be used
Packit fd8b60
 * in advertising or publicity pertaining to distribution of the software
Packit fd8b60
 * without specific, written prior permission. OpenVision makes no
Packit fd8b60
 * representations about the suitability of this software for any
Packit fd8b60
 * purpose.  It is provided "as is" without express or implied warranty.
Packit fd8b60
 *
Packit fd8b60
 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit fd8b60
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit fd8b60
 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit fd8b60
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
Packit fd8b60
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
Packit fd8b60
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit fd8b60
 * PERFORMANCE OF THIS SOFTWARE.
Packit fd8b60
 */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2003, 2004 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "autoconf.h"
Packit fd8b60
#include <stdio.h>
Packit fd8b60
#ifdef _WIN32
Packit fd8b60
#include <windows.h>
Packit fd8b60
#include <winsock.h>
Packit fd8b60
#else
Packit fd8b60
#include <sys/types.h>
Packit fd8b60
#include <netinet/in.h>
Packit fd8b60
#include <sys/socket.h>
Packit fd8b60
#endif
Packit fd8b60
#include <errno.h>
Packit fd8b60
#ifdef HAVE_UNISTD_H
Packit fd8b60
#include <unistd.h>
Packit fd8b60
#endif
Packit fd8b60
#include <string.h>
Packit fd8b60
Packit fd8b60
/* need struct timeval */
Packit fd8b60
#if HAVE_TIME_H && (!HAVE_SYS_TIME_H || TIME_WITH_SYS_TIME)
Packit fd8b60
# include <time.h>
Packit fd8b60
#endif
Packit fd8b60
#if HAVE_SYS_TIME_H
Packit fd8b60
# include <sys/time.h>
Packit fd8b60
#endif
Packit fd8b60
Packit fd8b60
#include <gssapi/gssapi_generic.h>
Packit fd8b60
#include "gss-misc.h"
Packit fd8b60
/* for store_32_be */
Packit fd8b60
#include "k5-platform.h"
Packit fd8b60
Packit fd8b60
#ifdef HAVE_STDLIB_H
Packit fd8b60
#include <stdlib.h>
Packit fd8b60
#else
Packit fd8b60
extern char *malloc();
Packit fd8b60
#endif
Packit fd8b60
Packit fd8b60
FILE *display_file;
Packit fd8b60
Packit fd8b60
gss_buffer_desc empty_token_buf = { 0, (void *)"" };
Packit fd8b60
gss_buffer_t empty_token = &empty_token_buf;
Packit fd8b60
Packit fd8b60
static void display_status_1(char *m, OM_uint32 code, int type);
Packit fd8b60
Packit fd8b60
static int
Packit fd8b60
write_all(int fildes, char *buf, unsigned int nbyte)
Packit fd8b60
{
Packit fd8b60
    int ret;
Packit fd8b60
    char *ptr;
Packit fd8b60
Packit fd8b60
    for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {
Packit fd8b60
        ret = send(fildes, ptr, nbyte, 0);
Packit fd8b60
        if (ret < 0) {
Packit fd8b60
            if (errno == EINTR)
Packit fd8b60
                continue;
Packit fd8b60
            return ret;
Packit fd8b60
        } else if (ret == 0) {
Packit fd8b60
            return ptr - buf;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    return ptr - buf;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
static int
Packit fd8b60
read_all(int fildes, char *buf, unsigned int nbyte)
Packit fd8b60
{
Packit fd8b60
    int ret;
Packit fd8b60
    char *ptr;
Packit fd8b60
    fd_set rfds;
Packit fd8b60
    struct timeval tv;
Packit fd8b60
Packit fd8b60
    FD_ZERO(&rfds);
Packit fd8b60
    FD_SET(fildes, &rfds);
Packit fd8b60
    tv.tv_sec = 10;
Packit fd8b60
    tv.tv_usec = 0;
Packit fd8b60
Packit fd8b60
    for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {
Packit fd8b60
        if (select(FD_SETSIZE, &rfds, NULL, NULL, &tv) <= 0 ||
Packit fd8b60
            !FD_ISSET(fildes, &rfds))
Packit fd8b60
            return ptr - buf;
Packit fd8b60
        ret = recv(fildes, ptr, nbyte, 0);
Packit fd8b60
        if (ret < 0) {
Packit fd8b60
            if (errno == EINTR)
Packit fd8b60
                continue;
Packit fd8b60
            return ret;
Packit fd8b60
        } else if (ret == 0) {
Packit fd8b60
            return ptr - buf;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    return ptr - buf;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Function: send_token
Packit fd8b60
 *
Packit fd8b60
 * Purpose: Writes a token to a file descriptor.
Packit fd8b60
 *
Packit fd8b60
 * Arguments:
Packit fd8b60
 *
Packit fd8b60
 *      s               (r) an open file descriptor
Packit fd8b60
 *      flags           (r) the flags to write
Packit fd8b60
 *      tok             (r) the token to write
Packit fd8b60
 *
Packit fd8b60
 * Returns: 0 on success, -1 on failure
Packit fd8b60
 *
Packit fd8b60
 * Effects:
Packit fd8b60
 *
Packit fd8b60
 * If the flags are non-null, send_token writes the token flags (a
Packit fd8b60
 * single byte, even though they're passed in in an integer). Next,
Packit fd8b60
 * the token length (as a network long) and then the token data are
Packit fd8b60
 * written to the file descriptor s.  It returns 0 on success, and -1
Packit fd8b60
 * if an error occurs or if it could not write all the data.
Packit fd8b60
 */
Packit fd8b60
int
Packit fd8b60
send_token(int s, int flags, gss_buffer_t tok)
Packit fd8b60
{
Packit fd8b60
    int ret;
Packit fd8b60
    unsigned char char_flags = (unsigned char)flags;
Packit fd8b60
    unsigned char lenbuf[4];
Packit fd8b60
Packit fd8b60
    if (char_flags) {
Packit fd8b60
        ret = write_all(s, (char *)&char_flags, 1);
Packit fd8b60
        if (ret != 1) {
Packit fd8b60
            perror("sending token flags");
Packit fd8b60
            return -1;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
    if (tok->length > 0xffffffffUL)
Packit fd8b60
        abort();
Packit fd8b60
    store_32_be(tok->length, lenbuf);
Packit fd8b60
    ret = write_all(s, (char *)lenbuf, 4);
Packit fd8b60
    if (ret < 0) {
Packit fd8b60
        perror("sending token length");
Packit fd8b60
        return -1;
Packit fd8b60
    } else if (ret != 4) {
Packit fd8b60
        if (display_file) {
Packit fd8b60
            fprintf(display_file,
Packit fd8b60
                    "sending token length: %d of %d bytes written\n", ret, 4);
Packit fd8b60
        }
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    ret = write_all(s, tok->value, tok->length);
Packit fd8b60
    if (ret < 0) {
Packit fd8b60
        perror("sending token data");
Packit fd8b60
        return -1;
Packit fd8b60
    } else if ((size_t)ret != tok->length) {
Packit fd8b60
        if (display_file) {
Packit fd8b60
            fprintf(display_file,
Packit fd8b60
                    "sending token data: %d of %d bytes written\n",
Packit fd8b60
                    ret, (int)tok->length);
Packit fd8b60
        }
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Function: recv_token
Packit fd8b60
 *
Packit fd8b60
 * Purpose: Reads a token from a file descriptor.
Packit fd8b60
 *
Packit fd8b60
 * Arguments:
Packit fd8b60
 *
Packit fd8b60
 *      s               (r) an open file descriptor
Packit fd8b60
 *      flags           (w) the read flags
Packit fd8b60
 *      tok             (w) the read token
Packit fd8b60
 *
Packit fd8b60
 * Returns: 0 on success, -1 on failure
Packit fd8b60
 *
Packit fd8b60
 * Effects:
Packit fd8b60
 *
Packit fd8b60
 * recv_token reads the token flags (a single byte, even though
Packit fd8b60
 * they're stored into an integer, then reads the token length (as a
Packit fd8b60
 * network long), allocates memory to hold the data, and then reads
Packit fd8b60
 * the token data from the file descriptor s.  It blocks to read the
Packit fd8b60
 * length and data, if necessary.  On a successful return, the token
Packit fd8b60
 * should be freed with gss_release_buffer.  It returns 0 on success,
Packit fd8b60
 * and -1 if an error occurs or if it could not read all the data.
Packit fd8b60
 */
Packit fd8b60
int
Packit fd8b60
recv_token(int s, int *flags, gss_buffer_t tok)
Packit fd8b60
{
Packit fd8b60
    int ret;
Packit fd8b60
    unsigned char char_flags;
Packit fd8b60
    unsigned char lenbuf[4];
Packit fd8b60
Packit fd8b60
    ret = read_all(s, (char *)&char_flags, 1);
Packit fd8b60
    if (ret < 0) {
Packit fd8b60
        perror("reading token flags");
Packit fd8b60
        return -1;
Packit fd8b60
    } else if (!ret) {
Packit fd8b60
        if (display_file)
Packit fd8b60
            fputs("reading token flags: 0 bytes read\n", display_file);
Packit fd8b60
        return -1;
Packit fd8b60
    } else {
Packit fd8b60
        *flags = char_flags;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (char_flags == 0) {
Packit fd8b60
        lenbuf[0] = 0;
Packit fd8b60
        ret = read_all(s, (char *)&lenbuf[1], 3);
Packit fd8b60
        if (ret < 0) {
Packit fd8b60
            perror("reading token length");
Packit fd8b60
            return -1;
Packit fd8b60
        } else if (ret != 3) {
Packit fd8b60
            if (display_file) {
Packit fd8b60
                fprintf(display_file,
Packit fd8b60
                        "reading token length: %d of %d bytes read\n", ret, 3);
Packit fd8b60
            }
Packit fd8b60
            return -1;
Packit fd8b60
        }
Packit fd8b60
    } else {
Packit fd8b60
        ret = read_all(s, (char *)lenbuf, 4);
Packit fd8b60
        if (ret < 0) {
Packit fd8b60
            perror("reading token length");
Packit fd8b60
            return -1;
Packit fd8b60
        } else if (ret != 4) {
Packit fd8b60
            if (display_file) {
Packit fd8b60
                fprintf(display_file,
Packit fd8b60
                        "reading token length: %d of %d bytes read\n", ret, 4);
Packit fd8b60
            }
Packit fd8b60
            return -1;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    tok->length = load_32_be(lenbuf);
Packit fd8b60
    tok->value = malloc(tok->length ? tok->length : 1);
Packit fd8b60
    if (tok->length && tok->value == NULL) {
Packit fd8b60
        if (display_file)
Packit fd8b60
            fprintf(display_file, "Out of memory allocating token data\n");
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    ret = read_all(s, (char *)tok->value, tok->length);
Packit fd8b60
    if (ret < 0) {
Packit fd8b60
        perror("reading token data");
Packit fd8b60
        free(tok->value);
Packit fd8b60
        return -1;
Packit fd8b60
    } else if ((size_t)ret != tok->length) {
Packit fd8b60
        fprintf(stderr, "sending token data: %d of %d bytes written\n",
Packit fd8b60
                ret, (int)tok->length);
Packit fd8b60
        free(tok->value);
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
display_status_1(char *m, OM_uint32 code, int type)
Packit fd8b60
{
Packit fd8b60
    OM_uint32 min_stat;
Packit fd8b60
    gss_buffer_desc msg;
Packit fd8b60
    OM_uint32 msg_ctx;
Packit fd8b60
Packit fd8b60
    msg_ctx = 0;
Packit fd8b60
    while (1) {
Packit fd8b60
        (void)gss_display_status(&min_stat, code, type, GSS_C_NULL_OID,
Packit fd8b60
                                 &msg_ctx, &msg;;
Packit fd8b60
        if (display_file) {
Packit fd8b60
            fprintf(display_file, "GSS-API error %s: %s\n", m,
Packit fd8b60
                    (char *)msg.value);
Packit fd8b60
        }
Packit fd8b60
        (void)gss_release_buffer(&min_stat, &msg;;
Packit fd8b60
Packit fd8b60
        if (!msg_ctx)
Packit fd8b60
            break;
Packit fd8b60
    }
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Function: display_status
Packit fd8b60
 *
Packit fd8b60
 * Purpose: displays GSS-API messages
Packit fd8b60
 *
Packit fd8b60
 * Arguments:
Packit fd8b60
 *
Packit fd8b60
 *      msg             a string to be displayed with the message
Packit fd8b60
 *      maj_stat        the GSS-API major status code
Packit fd8b60
 *      min_stat        the GSS-API minor status code
Packit fd8b60
 *
Packit fd8b60
 * Effects:
Packit fd8b60
 *
Packit fd8b60
 * The GSS-API messages associated with maj_stat and min_stat are
Packit fd8b60
 * displayed on stderr, each preceeded by "GSS-API error <msg>: " and
Packit fd8b60
 * followed by a newline.
Packit fd8b60
 */
Packit fd8b60
void
Packit fd8b60
display_status(char *msg, OM_uint32 maj_stat, OM_uint32 min_stat)
Packit fd8b60
{
Packit fd8b60
    display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
Packit fd8b60
    display_status_1(msg, min_stat, GSS_C_MECH_CODE);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Function: display_ctx_flags
Packit fd8b60
 *
Packit fd8b60
 * Purpose: displays the flags returned by context initation in
Packit fd8b60
 *          a human-readable form
Packit fd8b60
 *
Packit fd8b60
 * Arguments:
Packit fd8b60
 *
Packit fd8b60
 *      int             ret_flags
Packit fd8b60
 *
Packit fd8b60
 * Effects:
Packit fd8b60
 *
Packit fd8b60
 * Strings corresponding to the context flags are printed on
Packit fd8b60
 * stdout, preceded by "context flag: " and followed by a newline
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
void
Packit fd8b60
display_ctx_flags(OM_uint32 flags)
Packit fd8b60
{
Packit fd8b60
    if (flags & GSS_C_DELEG_FLAG)
Packit fd8b60
        fprintf(display_file, "context flag: GSS_C_DELEG_FLAG\n");
Packit fd8b60
    if (flags & GSS_C_MUTUAL_FLAG)
Packit fd8b60
        fprintf(display_file, "context flag: GSS_C_MUTUAL_FLAG\n");
Packit fd8b60
    if (flags & GSS_C_REPLAY_FLAG)
Packit fd8b60
        fprintf(display_file, "context flag: GSS_C_REPLAY_FLAG\n");
Packit fd8b60
    if (flags & GSS_C_SEQUENCE_FLAG)
Packit fd8b60
        fprintf(display_file, "context flag: GSS_C_SEQUENCE_FLAG\n");
Packit fd8b60
    if (flags & GSS_C_CONF_FLAG)
Packit fd8b60
        fprintf(display_file, "context flag: GSS_C_CONF_FLAG \n");
Packit fd8b60
    if (flags & GSS_C_INTEG_FLAG)
Packit fd8b60
        fprintf(display_file, "context flag: GSS_C_INTEG_FLAG \n");
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
void
Packit fd8b60
print_token(gss_buffer_t tok)
Packit fd8b60
{
Packit fd8b60
    size_t i;
Packit fd8b60
    unsigned char *p = tok->value;
Packit fd8b60
Packit fd8b60
    if (!display_file)
Packit fd8b60
        return;
Packit fd8b60
    for (i = 0; i < tok->length; i++, p++) {
Packit fd8b60
        fprintf(display_file, "%02x ", *p);
Packit fd8b60
        if (i % 16 == 15) {
Packit fd8b60
            fprintf(display_file, "\n");
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
    fprintf(display_file, "\n");
Packit fd8b60
    fflush(display_file);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#ifdef _WIN32
Packit fd8b60
#include <sys\timeb.h>
Packit fd8b60
#include <time.h>
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
gettimeofday(struct timeval *tv, void *ignore_tz)
Packit fd8b60
{
Packit fd8b60
    struct _timeb tb;
Packit fd8b60
Packit fd8b60
    _tzset();
Packit fd8b60
    _ftime(&tb);
Packit fd8b60
    if (tv) {
Packit fd8b60
        tv->tv_sec = tb.time;
Packit fd8b60
        tv->tv_usec = tb.millitm * 1000;
Packit fd8b60
    }
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#endif /* _WIN32 */