Blame src/iceauth.c

Packit e18bd3
/******************************************************************************
Packit e18bd3
Packit e18bd3
Packit e18bd3
Copyright 1993, 1998  The Open Group
Packit e18bd3
Packit e18bd3
Permission to use, copy, modify, distribute, and sell this software and its
Packit e18bd3
documentation for any purpose is hereby granted without fee, provided that
Packit e18bd3
the above copyright notice appear in all copies and that both that
Packit e18bd3
copyright notice and this permission notice appear in supporting
Packit e18bd3
documentation.
Packit e18bd3
Packit e18bd3
The above copyright notice and this permission notice shall be included in
Packit e18bd3
all copies or substantial portions of the Software.
Packit e18bd3
Packit e18bd3
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit e18bd3
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit e18bd3
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit e18bd3
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit e18bd3
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit e18bd3
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit e18bd3
Packit e18bd3
Except as contained in this notice, the name of The Open Group shall not be
Packit e18bd3
used in advertising or otherwise to promote the sale, use or other dealings
Packit e18bd3
in this Software without prior written authorization from The Open Group.
Packit e18bd3
Packit e18bd3
Author: Ralph Mor, X Consortium
Packit e18bd3
******************************************************************************/
Packit e18bd3
Packit e18bd3
#ifdef HAVE_CONFIG_H
Packit e18bd3
#include <config.h>
Packit e18bd3
#endif
Packit e18bd3
#include <X11/ICE/ICElib.h>
Packit e18bd3
#include "ICElibint.h"
Packit e18bd3
#include <X11/ICE/ICEutil.h>
Packit e18bd3
Packit e18bd3
#include <time.h>
Packit e18bd3
#define Time_t time_t
Packit e18bd3
Packit e18bd3
#ifdef HAVE_LIBBSD
Packit e18bd3
#include <bsd/stdlib.h>	/* for arc4random_buf() */
Packit e18bd3
#endif
Packit e18bd3
Packit e18bd3
static int was_called_state;
Packit e18bd3
Packit Service d9b92f
#ifndef HAVE_ARC4RANDOM_BUF
Packit e18bd3
Packit Service d9b92f
static void
Packit Service d9b92f
emulate_getrandom_buf (
Packit Service d9b92f
	char *auth,
Packit e18bd3
	int len
Packit e18bd3
)
Packit e18bd3
{
Packit e18bd3
    long    ldata[2];
Packit e18bd3
    int	    seed;
Packit e18bd3
    int	    value;
Packit e18bd3
    int	    i;
Packit Service e7a084
Packit e18bd3
#ifdef ITIMER_REAL
Packit e18bd3
    {
Packit e18bd3
	struct timeval  now;
Packit e18bd3
	X_GETTIMEOFDAY (&now;;
Packit e18bd3
	ldata[0] = now.tv_sec;
Packit e18bd3
	ldata[1] = now.tv_usec;
Packit e18bd3
    }
Packit Service d9b92f
#else /* ITIMER_REAL */
Packit e18bd3
    {
Packit e18bd3
	long    time ();
Packit e18bd3
	ldata[0] = time ((long *) 0);
Packit e18bd3
	ldata[1] = getpid ();
Packit e18bd3
    }
Packit Service d9b92f
#endif /* ITIMER_REAL */
Packit e18bd3
    seed = (ldata[0]) + (ldata[1] << 16);
Packit e18bd3
    srand (seed);
Packit e18bd3
    for (i = 0; i < len; i++)
Packit e18bd3
    {
Packit e18bd3
	value = rand ();
Packit e18bd3
	auth[i] = value & 0xff;
Packit e18bd3
    }
Packit Service d9b92f
}
Packit Service d9b92f
Packit Service 1808f4
#ifndef HAVE_GETENTROPY
Packit Service 1808f4
#include <sys/syscall.h>
Packit Service 1808f4
#include <errno.h>
Packit Service 1808f4
Packit Service 1808f4
/* code taken from libressl, license: */
Packit Service 1808f4
/*
Packit Service 1808f4
 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
Packit Service 1808f4
 * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
Packit Service 1808f4
 *
Packit Service 1808f4
 * Permission to use, copy, modify, and distribute this software for any
Packit Service 1808f4
 * purpose with or without fee is hereby granted, provided that the above
Packit Service 1808f4
 * copyright notice and this permission notice appear in all copies.
Packit Service 1808f4
 *
Packit Service 1808f4
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Packit Service 1808f4
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit Service 1808f4
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Packit Service 1808f4
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit Service 1808f4
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit Service 1808f4
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Packit Service 1808f4
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit Service 1808f4
 *
Packit Service 1808f4
 * Emulation of getentropy(2) as documented at:
Packit Service 1808f4
 * http://man.openbsd.org/getentropy.2
Packit Service 1808f4
 */
Packit Service 1808f4
#ifdef __NR_getrandom
Packit Service 1808f4
static int
Packit Service 1808f4
getentropy(void *buf, size_t len)
Packit Service 1808f4
{
Packit Service 1808f4
	int pre_errno = errno;
Packit Service 1808f4
	int ret;
Packit Service 1808f4
	if (len > 256)
Packit Service 1808f4
		return (-1);
Packit Service 1808f4
	do {
Packit Service 1808f4
		ret = syscall(__NR_getrandom, buf, len, 0);
Packit Service 1808f4
	} while (ret == -1 && errno == EINTR);
Packit Service 1808f4
Packit Service 1808f4
	if (ret != len)
Packit Service 1808f4
		return (-1);
Packit Service 1808f4
	errno = pre_errno;
Packit Service 1808f4
Packit Service 1808f4
	fprintf(stderr, "generating cookie with syscall\n");
Packit Service 1808f4
Packit Service 1808f4
	return (0);
Packit Service 1808f4
}
Packit Service 1808f4
#define HAVE_GETENTROPY 1
Packit Service 1808f4
#endif /* __NR_getrandom */
Packit Service 1808f4
Packit Service 1808f4
#endif /* HAVE_GETENTROPY */
Packit Service 1808f4
Packit Service d9b92f
static void
Packit Service d9b92f
arc4random_buf (
Packit Service d9b92f
	char *auth,
Packit Service d9b92f
	int len
Packit Service d9b92f
)
Packit Service d9b92f
{
Packit Service d9b92f
    int	    ret;
Packit Service d9b92f
Packit Service d9b92f
#if HAVE_GETENTROPY
Packit Service d9b92f
    /* weak emulation of arc4random through the entropy libc */
Packit Service d9b92f
    ret = getentropy (auth, len);
Packit Service d9b92f
    if (ret == 0)
Packit Service d9b92f
	return;
Packit Service d9b92f
#endif /* HAVE_GETENTROPY */
Packit Service d9b92f
Packit Service d9b92f
    emulate_getrandom_buf (auth, len);
Packit Service d9b92f
}
Packit Service d9b92f
Packit Service d9b92f
#endif /* !defined(HAVE_ARC4RANDOM_BUF) */
Packit Service d9b92f
Packit Service d9b92f
/*
Packit Service d9b92f
 * MIT-MAGIC-COOKIE-1 is a sample authentication method implemented by
Packit Service d9b92f
 * the SI.  It is not part of standard ICElib.
Packit Service d9b92f
 */
Packit Service d9b92f
Packit Service d9b92f

Packit Service d9b92f
char *
Packit Service d9b92f
IceGenerateMagicCookie (
Packit Service d9b92f
	int len
Packit Service d9b92f
)
Packit Service d9b92f
{
Packit Service d9b92f
    char    *auth;
Packit Service d9b92f
Packit Service d9b92f
    if ((auth = malloc (len + 1)) == NULL)
Packit Service d9b92f
	return (NULL);
Packit Service d9b92f
Packit Service d9b92f
    arc4random_buf (auth, len);
Packit Service d9b92f
Packit e18bd3
    auth[len] = '\0';
Packit e18bd3
    return (auth);
Packit e18bd3
}
Packit e18bd3
Packit e18bd3
Packit e18bd3

Packit e18bd3
IcePoAuthStatus
Packit e18bd3
_IcePoMagicCookie1Proc (
Packit e18bd3
	IceConn		iceConn,
Packit e18bd3
	IcePointer	*authStatePtr,
Packit e18bd3
	Bool 		cleanUp,
Packit e18bd3
	Bool		swap,
Packit e18bd3
	int     	authDataLen,
Packit e18bd3
	IcePointer	authData,
Packit e18bd3
	int 		*replyDataLenRet,
Packit e18bd3
	IcePointer	*replyDataRet,
Packit e18bd3
	char    	**errorStringRet
Packit e18bd3
)
Packit e18bd3
{
Packit e18bd3
    if (cleanUp)
Packit e18bd3
    {
Packit e18bd3
	/*
Packit e18bd3
	 * We didn't allocate any state.  We're done.
Packit e18bd3
	 */
Packit e18bd3
Packit e18bd3
	return (IcePoAuthDoneCleanup);
Packit e18bd3
    }
Packit e18bd3
Packit e18bd3
    *errorStringRet = NULL;
Packit e18bd3
Packit e18bd3
    if (*authStatePtr == NULL)
Packit e18bd3
    {
Packit e18bd3
	/*
Packit e18bd3
	 * This is the first time we're being called.  Search the
Packit e18bd3
	 * authentication data for the first occurence of
Packit e18bd3
	 * MIT-MAGIC-COOKIE-1 that matches iceConn->connection_string.
Packit e18bd3
	 */
Packit e18bd3
Packit e18bd3
	unsigned short  length;
Packit e18bd3
	char		*data;
Packit e18bd3
Packit e18bd3
	_IceGetPoAuthData ("ICE", iceConn->connection_string,
Packit e18bd3
	    "MIT-MAGIC-COOKIE-1", &length, &data);
Packit e18bd3
Packit e18bd3
	if (!data)
Packit e18bd3
	{
Packit e18bd3
	    const char *tempstr =
Packit e18bd3
		"Could not find correct MIT-MAGIC-COOKIE-1 authentication";
Packit e18bd3
Packit e18bd3
	    *errorStringRet = strdup(tempstr);
Packit e18bd3
Packit e18bd3
	    return (IcePoAuthFailed);
Packit e18bd3
	}
Packit e18bd3
	else
Packit e18bd3
	{
Packit e18bd3
	    *authStatePtr = (IcePointer) &was_called_state;
Packit e18bd3
Packit e18bd3
	    *replyDataLenRet = length;
Packit e18bd3
	    *replyDataRet = data;
Packit e18bd3
Packit e18bd3
	    return (IcePoAuthHaveReply);
Packit e18bd3
	}
Packit e18bd3
    }
Packit e18bd3
    else
Packit e18bd3
    {
Packit e18bd3
	/*
Packit e18bd3
	 * We should never get here for MIT-MAGIC-COOKIE-1 since it is
Packit e18bd3
	 * a single pass authentication method.
Packit e18bd3
	 */
Packit e18bd3
Packit e18bd3
	const char *tempstr =
Packit e18bd3
	    "MIT-MAGIC-COOKIE-1 authentication internal error";
Packit e18bd3
Packit e18bd3
	*errorStringRet = strdup(tempstr);
Packit e18bd3
Packit e18bd3
	return (IcePoAuthFailed);
Packit e18bd3
    }
Packit e18bd3
}
Packit e18bd3
Packit e18bd3
IcePoAuthProc	_IcePoAuthProcs[] = {_IcePoMagicCookie1Proc};
Packit e18bd3
Packit e18bd3
Packit e18bd3
IcePaAuthStatus
Packit e18bd3
_IcePaMagicCookie1Proc (
Packit e18bd3
	IceConn		iceConn,
Packit e18bd3
	IcePointer	*authStatePtr,
Packit e18bd3
	Bool		swap,
Packit e18bd3
	int     	authDataLen,
Packit e18bd3
	IcePointer	authData,
Packit e18bd3
	int 		*replyDataLenRet,
Packit e18bd3
	IcePointer	*replyDataRet,
Packit e18bd3
	char    	**errorStringRet
Packit e18bd3
)
Packit e18bd3
{
Packit e18bd3
    *errorStringRet = NULL;
Packit e18bd3
    *replyDataLenRet = 0;
Packit e18bd3
    *replyDataRet = NULL;
Packit e18bd3
Packit e18bd3
    if (*authStatePtr == NULL)
Packit e18bd3
    {
Packit e18bd3
	/*
Packit e18bd3
	 * This is the first time we're being called.  We don't have
Packit e18bd3
	 * any data to pass to the other client.
Packit e18bd3
	 */
Packit e18bd3
Packit e18bd3
	*authStatePtr = (IcePointer) &was_called_state;
Packit e18bd3
Packit e18bd3
	return (IcePaAuthContinue);
Packit e18bd3
    }
Packit e18bd3
    else
Packit e18bd3
    {
Packit e18bd3
	/*
Packit e18bd3
	 * Search the authentication data for the first occurence of
Packit e18bd3
	 * MIT-MAGIC-COOKIE-1 that matches iceConn->connection_string.
Packit e18bd3
	 */
Packit e18bd3
Packit e18bd3
	unsigned short  length;
Packit e18bd3
	char		*data;
Packit e18bd3
Packit e18bd3
	_IceGetPaAuthData ("ICE", iceConn->connection_string,
Packit e18bd3
	    "MIT-MAGIC-COOKIE-1", &length, &data);
Packit e18bd3
Packit e18bd3
	if (data)
Packit e18bd3
	{
Packit e18bd3
	    IcePaAuthStatus stat;
Packit e18bd3
Packit e18bd3
	    if (authDataLen == length &&
Packit e18bd3
	        memcmp (authData, data, authDataLen) == 0)
Packit e18bd3
	    {
Packit e18bd3
		stat = IcePaAuthAccepted;
Packit e18bd3
	    }
Packit e18bd3
	    else
Packit e18bd3
	    {
Packit e18bd3
		const char *tempstr
Packit e18bd3
		    = "MIT-MAGIC-COOKIE-1 authentication rejected";
Packit e18bd3
Packit e18bd3
		*errorStringRet = strdup(tempstr);
Packit e18bd3
Packit e18bd3
		stat = IcePaAuthRejected;
Packit e18bd3
	    }
Packit e18bd3
Packit e18bd3
	    free (data);
Packit e18bd3
	    return (stat);
Packit e18bd3
	}
Packit e18bd3
	else
Packit e18bd3
	{
Packit e18bd3
	    /*
Packit e18bd3
	     * We should never get here because in the ConnectionReply
Packit e18bd3
	     * we should have passed all the valid methods.  So we should
Packit e18bd3
	     * always find a valid entry.
Packit e18bd3
	     */
Packit e18bd3
Packit e18bd3
	    const char *tempstr =
Packit e18bd3
		"MIT-MAGIC-COOKIE-1 authentication internal error";
Packit e18bd3
Packit e18bd3
	    *errorStringRet = strdup(tempstr);
Packit e18bd3
Packit e18bd3
	    return (IcePaAuthFailed);
Packit e18bd3
	}
Packit e18bd3
    }
Packit e18bd3
}
Packit e18bd3
Packit e18bd3
IcePaAuthProc	_IcePaAuthProcs[] = {_IcePaMagicCookie1Proc};