Path: blob/main/crypto/heimdal/appl/test/gss_common.c
34889 views
/*1* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "test_locl.h"34#include <gssapi/gssapi.h>35#include <gssapi/gssapi_krb5.h>36#include <gssapi/gssapi_spnego.h>37#include "gss_common.h"38RCSID("$Id$");3940void41write_token (int sock, gss_buffer_t buf)42{43uint32_t len, net_len;44OM_uint32 min_stat;4546len = buf->length;4748net_len = htonl(len);4950if (net_write (sock, &net_len, 4) != 4)51err (1, "write");52if (net_write (sock, buf->value, len) != len)53err (1, "write");5455gss_release_buffer (&min_stat, buf);56}5758static void59enet_read(int fd, void *buf, size_t len)60{61ssize_t ret;6263ret = net_read (fd, buf, len);64if (ret == 0)65errx (1, "EOF in read");66else if (ret < 0)67errx (1, "read");68}6970void71read_token (int sock, gss_buffer_t buf)72{73uint32_t len, net_len;7475enet_read (sock, &net_len, 4);76len = ntohl(net_len);77buf->length = len;78buf->value = emalloc(len);79enet_read (sock, buf->value, len);80}8182void83gss_print_errors (int min_stat)84{85OM_uint32 new_stat;86OM_uint32 msg_ctx = 0;87gss_buffer_desc status_string;88OM_uint32 ret;8990do {91ret = gss_display_status (&new_stat,92min_stat,93GSS_C_MECH_CODE,94GSS_C_NO_OID,95&msg_ctx,96&status_string);97fprintf (stderr, "%.*s\n", (int)status_string.length,98(char *)status_string.value);99gss_release_buffer (&new_stat, &status_string);100} while (!GSS_ERROR(ret) && msg_ctx != 0);101}102103void104gss_verr(int exitval, int status, const char *fmt, va_list ap)105{106vwarnx (fmt, ap);107gss_print_errors (status);108exit (exitval);109}110111void112gss_err(int exitval, int status, const char *fmt, ...)113{114va_list args;115116va_start(args, fmt);117gss_verr (exitval, status, fmt, args);118va_end(args);119}120121gss_OID122select_mech(const char *mech)123{124if (strcasecmp(mech, "krb5") == 0)125return GSS_KRB5_MECHANISM;126else if (strcasecmp(mech, "spnego") == 0)127return GSS_SPNEGO_MECHANISM;128else if (strcasecmp(mech, "no-oid") == 0)129return GSS_C_NO_OID;130else131errx (1, "Unknown mechanism '%s' (spnego, krb5, no-oid)", mech);132}133134void135print_gss_name(const char *prefix, gss_name_t name)136{137OM_uint32 maj_stat, min_stat;138gss_buffer_desc name_token;139140maj_stat = gss_display_name (&min_stat,141name,142&name_token,143NULL);144if (GSS_ERROR(maj_stat))145gss_err (1, min_stat, "gss_display_name");146147fprintf (stderr, "%s `%.*s'\n", prefix,148(int)name_token.length,149(char *)name_token.value);150151gss_release_buffer (&min_stat, &name_token);152153}154155156