Path: blob/main/crypto/krb5/src/tests/gss-threads/gss-misc.c
34914 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1994 by OpenVision Technologies, Inc.3*4* Permission to use, copy, modify, distribute, and sell this software5* and its documentation for any purpose is hereby granted without fee,6* provided that the above copyright notice appears in all copies and7* that both that copyright notice and this permission notice appear in8* supporting documentation, and that the name of OpenVision not be used9* in advertising or publicity pertaining to distribution of the software10* without specific, written prior permission. OpenVision makes no11* representations about the suitability of this software for any12* purpose. It is provided "as is" without express or implied warranty.13*14* OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,15* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO16* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR17* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF18* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR19* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR20* PERFORMANCE OF THIS SOFTWARE.21*/22/*23* Copyright (C) 2003, 2004 by the Massachusetts Institute of Technology.24* All rights reserved.25*26* Export of this software from the United States of America may27* require a specific license from the United States Government.28* It is the responsibility of any person or organization contemplating29* export to obtain such a license before exporting.30*31* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and32* distribute this software and its documentation for any purpose and33* without fee is hereby granted, provided that the above copyright34* notice appear in all copies and that both that copyright notice and35* this permission notice appear in supporting documentation, and that36* the name of M.I.T. not be used in advertising or publicity pertaining37* to distribution of the software without specific, written prior38* permission. Furthermore if you modify this software you must label39* your software as modified software and not distribute it in such a40* fashion that it might be confused with the original M.I.T. software.41* M.I.T. makes no representations about the suitability of42* this software for any purpose. It is provided "as is" without express43* or implied warranty.44*/4546#include "autoconf.h"47#include <stdio.h>48#ifdef _WIN3249#include <windows.h>50#include <winsock.h>51#else52#include <sys/types.h>53#include <netinet/in.h>54#include <sys/socket.h>55#endif56#include <errno.h>57#ifdef HAVE_UNISTD_H58#include <unistd.h>59#endif60#include <string.h>6162/* need struct timeval */63#include <time.h>64#if HAVE_SYS_TIME_H65#include <sys/time.h>66#endif6768#include <gssapi/gssapi_generic.h>69#include "gss-misc.h"70/* for store_32_be */71#include "k5-platform.h"7273#ifdef HAVE_STDLIB_H74#include <stdlib.h>75#else76extern char *malloc();77#endif7879FILE *display_file;8081gss_buffer_desc empty_token_buf = { 0, (void *)"" };82gss_buffer_t empty_token = &empty_token_buf;8384static void display_status_1(char *m, OM_uint32 code, int type);8586static int87write_all(int fildes, char *buf, unsigned int nbyte)88{89int ret;90char *ptr;9192for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {93ret = send(fildes, ptr, nbyte, 0);94if (ret < 0) {95if (errno == EINTR)96continue;97return ret;98} else if (ret == 0) {99return ptr - buf;100}101}102103return ptr - buf;104}105106static int107read_all(int fildes, char *buf, unsigned int nbyte)108{109int ret;110char *ptr;111fd_set rfds;112struct timeval tv;113114FD_ZERO(&rfds);115FD_SET(fildes, &rfds);116tv.tv_sec = 10;117tv.tv_usec = 0;118119for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {120if (select(FD_SETSIZE, &rfds, NULL, NULL, &tv) <= 0 ||121!FD_ISSET(fildes, &rfds))122return ptr - buf;123ret = recv(fildes, ptr, nbyte, 0);124if (ret < 0) {125if (errno == EINTR)126continue;127return ret;128} else if (ret == 0) {129return ptr - buf;130}131}132133return ptr - buf;134}135136/*137* Function: send_token138*139* Purpose: Writes a token to a file descriptor.140*141* Arguments:142*143* s (r) an open file descriptor144* flags (r) the flags to write145* tok (r) the token to write146*147* Returns: 0 on success, -1 on failure148*149* Effects:150*151* If the flags are non-null, send_token writes the token flags (a152* single byte, even though they're passed in in an integer). Next,153* the token length (as a network long) and then the token data are154* written to the file descriptor s. It returns 0 on success, and -1155* if an error occurs or if it could not write all the data.156*/157int158send_token(int s, int flags, gss_buffer_t tok)159{160int ret;161unsigned char char_flags = (unsigned char)flags;162unsigned char lenbuf[4];163164if (char_flags) {165ret = write_all(s, (char *)&char_flags, 1);166if (ret != 1) {167perror("sending token flags");168return -1;169}170}171if (tok->length > 0xffffffffUL)172abort();173store_32_be(tok->length, lenbuf);174ret = write_all(s, (char *)lenbuf, 4);175if (ret < 0) {176perror("sending token length");177return -1;178} else if (ret != 4) {179if (display_file) {180fprintf(display_file,181"sending token length: %d of %d bytes written\n", ret, 4);182}183return -1;184}185186ret = write_all(s, tok->value, tok->length);187if (ret < 0) {188perror("sending token data");189return -1;190} else if ((size_t)ret != tok->length) {191if (display_file) {192fprintf(display_file,193"sending token data: %d of %d bytes written\n",194ret, (int)tok->length);195}196return -1;197}198199return 0;200}201202/*203* Function: recv_token204*205* Purpose: Reads a token from a file descriptor.206*207* Arguments:208*209* s (r) an open file descriptor210* flags (w) the read flags211* tok (w) the read token212*213* Returns: 0 on success, -1 on failure214*215* Effects:216*217* recv_token reads the token flags (a single byte, even though218* they're stored into an integer, then reads the token length (as a219* network long), allocates memory to hold the data, and then reads220* the token data from the file descriptor s. It blocks to read the221* length and data, if necessary. On a successful return, the token222* should be freed with gss_release_buffer. It returns 0 on success,223* and -1 if an error occurs or if it could not read all the data.224*/225int226recv_token(int s, int *flags, gss_buffer_t tok)227{228int ret;229unsigned char char_flags;230unsigned char lenbuf[4];231232ret = read_all(s, (char *)&char_flags, 1);233if (ret < 0) {234perror("reading token flags");235return -1;236} else if (!ret) {237if (display_file)238fputs("reading token flags: 0 bytes read\n", display_file);239return -1;240} else {241*flags = char_flags;242}243244if (char_flags == 0) {245lenbuf[0] = 0;246ret = read_all(s, (char *)&lenbuf[1], 3);247if (ret < 0) {248perror("reading token length");249return -1;250} else if (ret != 3) {251if (display_file) {252fprintf(display_file,253"reading token length: %d of %d bytes read\n", ret, 3);254}255return -1;256}257} else {258ret = read_all(s, (char *)lenbuf, 4);259if (ret < 0) {260perror("reading token length");261return -1;262} else if (ret != 4) {263if (display_file) {264fprintf(display_file,265"reading token length: %d of %d bytes read\n", ret, 4);266}267return -1;268}269}270271tok->length = load_32_be(lenbuf);272tok->value = malloc(tok->length ? tok->length : 1);273if (tok->length && tok->value == NULL) {274if (display_file)275fprintf(display_file, "Out of memory allocating token data\n");276return -1;277}278279ret = read_all(s, (char *)tok->value, tok->length);280if (ret < 0) {281perror("reading token data");282free(tok->value);283return -1;284} else if ((size_t)ret != tok->length) {285fprintf(stderr, "sending token data: %d of %d bytes written\n",286ret, (int)tok->length);287free(tok->value);288return -1;289}290291return 0;292}293294static void295display_status_1(char *m, OM_uint32 code, int type)296{297OM_uint32 min_stat;298gss_buffer_desc msg;299OM_uint32 msg_ctx;300301msg_ctx = 0;302while (1) {303(void)gss_display_status(&min_stat, code, type, GSS_C_NULL_OID,304&msg_ctx, &msg);305if (display_file) {306fprintf(display_file, "GSS-API error %s: %s\n", m,307(char *)msg.value);308}309(void)gss_release_buffer(&min_stat, &msg);310311if (!msg_ctx)312break;313}314}315316/*317* Function: display_status318*319* Purpose: displays GSS-API messages320*321* Arguments:322*323* msg a string to be displayed with the message324* maj_stat the GSS-API major status code325* min_stat the GSS-API minor status code326*327* Effects:328*329* The GSS-API messages associated with maj_stat and min_stat are330* displayed on stderr, each preceded by "GSS-API error <msg>: " and331* followed by a newline.332*/333void334display_status(char *msg, OM_uint32 maj_stat, OM_uint32 min_stat)335{336display_status_1(msg, maj_stat, GSS_C_GSS_CODE);337display_status_1(msg, min_stat, GSS_C_MECH_CODE);338}339340/*341* Function: display_ctx_flags342*343* Purpose: displays the flags returned by context initiation in344* a human-readable form345*346* Arguments:347*348* int ret_flags349*350* Effects:351*352* Strings corresponding to the context flags are printed on353* stdout, preceded by "context flag: " and followed by a newline354*/355356void357display_ctx_flags(OM_uint32 flags)358{359if (flags & GSS_C_DELEG_FLAG)360fprintf(display_file, "context flag: GSS_C_DELEG_FLAG\n");361if (flags & GSS_C_MUTUAL_FLAG)362fprintf(display_file, "context flag: GSS_C_MUTUAL_FLAG\n");363if (flags & GSS_C_REPLAY_FLAG)364fprintf(display_file, "context flag: GSS_C_REPLAY_FLAG\n");365if (flags & GSS_C_SEQUENCE_FLAG)366fprintf(display_file, "context flag: GSS_C_SEQUENCE_FLAG\n");367if (flags & GSS_C_CONF_FLAG)368fprintf(display_file, "context flag: GSS_C_CONF_FLAG \n");369if (flags & GSS_C_INTEG_FLAG)370fprintf(display_file, "context flag: GSS_C_INTEG_FLAG \n");371}372373void374print_token(gss_buffer_t tok)375{376size_t i;377unsigned char *p = tok->value;378379if (!display_file)380return;381for (i = 0; i < tok->length; i++, p++) {382fprintf(display_file, "%02x ", *p);383if (i % 16 == 15) {384fprintf(display_file, "\n");385}386}387fprintf(display_file, "\n");388fflush(display_file);389}390391#ifdef _WIN32392#include <sys\timeb.h>393#include <time.h>394395int396gettimeofday(struct timeval *tv, void *ignore_tz)397{398struct _timeb tb;399400_tzset();401_ftime(&tb);402if (tv) {403tv->tv_sec = tb.time;404tv->tv_usec = tb.millitm * 1000;405}406return 0;407}408409#endif /* _WIN32 */410411412