Blame AuGetBest.c

Packit 56e0ee
/*
Packit 56e0ee
Packit 56e0ee
Copyright 1988, 1998  The Open Group
Packit 56e0ee
Packit 56e0ee
Permission to use, copy, modify, distribute, and sell this software and its
Packit 56e0ee
documentation for any purpose is hereby granted without fee, provided that
Packit 56e0ee
the above copyright notice appear in all copies and that both that
Packit 56e0ee
copyright notice and this permission notice appear in supporting
Packit 56e0ee
documentation.
Packit 56e0ee
Packit 56e0ee
The above copyright notice and this permission notice shall be included in
Packit 56e0ee
all copies or substantial portions of the Software.
Packit 56e0ee
Packit 56e0ee
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 56e0ee
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 56e0ee
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit 56e0ee
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit 56e0ee
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 56e0ee
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 56e0ee
Packit 56e0ee
Except as contained in this notice, the name of The Open Group shall not be
Packit 56e0ee
used in advertising or otherwise to promote the sale, use or other dealings
Packit 56e0ee
in this Software without prior written authorization from The Open Group.
Packit 56e0ee
Packit 56e0ee
*/
Packit 56e0ee
Packit 56e0ee
#ifdef HAVE_CONFIG_H
Packit 56e0ee
#include <config.h>
Packit 56e0ee
#endif
Packit 56e0ee
#include <X11/Xauth.h>
Packit 56e0ee
#include <X11/Xos.h>
Packit 56e0ee
#ifdef XTHREADS
Packit 56e0ee
#include <X11/Xthreads.h>
Packit 56e0ee
#endif
Packit 56e0ee
#ifdef hpux
Packit 56e0ee
#define X_INCLUDE_NETDB_H
Packit 56e0ee
#define XOS_USE_NO_LOCKING
Packit 56e0ee
#include <X11/Xos_r.h>
Packit 56e0ee
#endif
Packit 56e0ee
Packit 56e0ee
#define binaryEqual(a, b, len) (memcmp(a, b, len) == 0)
Packit 56e0ee
Packit 56e0ee
Xauth *
Packit 56e0ee
XauGetBestAuthByAddr (
Packit 56e0ee
#if NeedWidePrototypes
Packit 56e0ee
    unsigned int	family,
Packit 56e0ee
    unsigned int	address_length,
Packit 56e0ee
#else
Packit 56e0ee
    unsigned short	family,
Packit 56e0ee
    unsigned short	address_length,
Packit 56e0ee
#endif
Packit 56e0ee
    _Xconst char*	address,
Packit 56e0ee
#if NeedWidePrototypes
Packit 56e0ee
    unsigned int	number_length,
Packit 56e0ee
#else
Packit 56e0ee
    unsigned short	number_length,
Packit 56e0ee
#endif
Packit 56e0ee
    _Xconst char*	number,
Packit 56e0ee
    int			types_length,
Packit 56e0ee
    char**		types,
Packit 56e0ee
    _Xconst int*	type_lengths)
Packit 56e0ee
{
Packit 56e0ee
    FILE    *auth_file;
Packit 56e0ee
    char    *auth_name;
Packit 56e0ee
    Xauth   *entry;
Packit 56e0ee
    Xauth   *best;
Packit 56e0ee
    int	    best_type;
Packit 56e0ee
    int	    type;
Packit 56e0ee
#ifdef hpux
Packit 56e0ee
    char		*fully_qual_address;
Packit 56e0ee
    unsigned short	fully_qual_address_length;
Packit 56e0ee
#endif
Packit 56e0ee
Packit 56e0ee
    auth_name = XauFileName ();
Packit 56e0ee
    if (!auth_name)
Packit 56e0ee
	return NULL;
Packit 56e0ee
    if (access (auth_name, R_OK) != 0)		/* checks REAL id */
Packit 56e0ee
	return NULL;
Packit 56e0ee
    auth_file = fopen (auth_name, "rb");
Packit 56e0ee
    if (!auth_file)
Packit 56e0ee
	return NULL;
Packit 56e0ee
Packit 56e0ee
#ifdef hpux
Packit 56e0ee
    if (family == FamilyLocal) {
Packit 56e0ee
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
Packit 56e0ee
	_Xgethostbynameparams hparams;
Packit 56e0ee
#endif
Packit 56e0ee
	struct hostent *hostp;
Packit 56e0ee
Packit 56e0ee
	/* make sure we try fully-qualified hostname */
Packit 56e0ee
	if ((hostp = _XGethostbyname(address,hparams)) != NULL) {
Packit 56e0ee
	    fully_qual_address = hostp->h_name;
Packit 56e0ee
	    fully_qual_address_length = strlen(fully_qual_address);
Packit 56e0ee
	}
Packit 56e0ee
	else
Packit 56e0ee
	{
Packit 56e0ee
	    fully_qual_address = NULL;
Packit 56e0ee
	    fully_qual_address_length = 0;
Packit 56e0ee
	}
Packit 56e0ee
    }
Packit 56e0ee
#endif /* hpux */
Packit 56e0ee
Packit 56e0ee
    best = NULL;
Packit 56e0ee
    best_type = types_length;
Packit 56e0ee
    for (;;) {
Packit 56e0ee
	entry = XauReadAuth (auth_file);
Packit 56e0ee
	if (!entry)
Packit 56e0ee
	    break;
Packit 56e0ee
	/*
Packit 56e0ee
	 * Match when:
Packit 56e0ee
	 *   either family or entry->family are FamilyWild or
Packit 56e0ee
	 *    family and entry->family are the same and
Packit 56e0ee
	 *     address and entry->address are the same
Packit 56e0ee
	 *  and
Packit 56e0ee
	 *   either number or entry->number are empty or
Packit 56e0ee
	 *    number and entry->number are the same
Packit 56e0ee
	 *  and
Packit 56e0ee
	 *   either name or entry->name are empty or
Packit 56e0ee
	 *    name and entry->name are the same
Packit 56e0ee
	 */
Packit 56e0ee
Packit 56e0ee
	if ((family == FamilyWild || entry->family == FamilyWild ||
Packit 56e0ee
	     (entry->family == family &&
Packit 56e0ee
	     ((address_length == entry->address_length &&
Packit 56e0ee
	      binaryEqual (entry->address, address, address_length))
Packit 56e0ee
#ifdef hpux
Packit 56e0ee
	     || (family == FamilyLocal &&
Packit 56e0ee
		fully_qual_address_length == entry->address_length &&
Packit 56e0ee
	     	binaryEqual (entry->address, fully_qual_address,
Packit 56e0ee
		    fully_qual_address_length))
Packit 56e0ee
#endif
Packit 56e0ee
	    ))) &&
Packit 56e0ee
	    (number_length == 0 || entry->number_length == 0 ||
Packit 56e0ee
	     (number_length == entry->number_length &&
Packit 56e0ee
	      binaryEqual (entry->number, number, number_length))))
Packit 56e0ee
	{
Packit 56e0ee
	    if (best_type == 0)
Packit 56e0ee
	    {
Packit 56e0ee
		best = entry;
Packit 56e0ee
		break;
Packit 56e0ee
	    }
Packit 56e0ee
	    for (type = 0; type < best_type; type++)
Packit 56e0ee
		if (type_lengths[type] == entry->name_length &&
Packit 56e0ee
		    !(strncmp (types[type], entry->name, entry->name_length)))
Packit 56e0ee
		{
Packit 56e0ee
		    break;
Packit 56e0ee
		}
Packit 56e0ee
	    if (type < best_type)
Packit 56e0ee
	    {
Packit 56e0ee
		if (best)
Packit 56e0ee
		    XauDisposeAuth (best);
Packit 56e0ee
		best = entry;
Packit 56e0ee
		best_type = type;
Packit 56e0ee
		if (type == 0)
Packit 56e0ee
		    break;
Packit 56e0ee
		continue;
Packit 56e0ee
	    }
Packit 56e0ee
	}
Packit 56e0ee
	XauDisposeAuth (entry);
Packit 56e0ee
    }
Packit 56e0ee
    (void) fclose (auth_file);
Packit 56e0ee
    return best;
Packit 56e0ee
}