/*
 * Copyright 1993 Network Computing Devices, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name Network Computing Devices, Inc. not be
 * used in advertising or publicity pertaining to distribution of this 
 * software without specific, written prior permission.
 * 
 * THIS SOFTWARE IS PROVIDED `AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
 * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
 * COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
 * SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
 * OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
 * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * $NCDId: @(#)mitauth.c,v 1.2 1993/08/28 00:34:18 lemke Exp $
 */
/*
 * Some portions derived from:
 *
 * MIT-MAGIC-COOKIE-1 authorization scheme
 *
 *
 * Copyright 1988 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  M.I.T. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * Author:  Keith Packard, MIT X Consortium
 */

#include <audio/audio.h>
#include "os.h"

static struct auth {
    struct auth *next;
    unsigned short len;
    char *data;
    AuID id;
}  *mit_auth;

int
MitAddCookie(data_length, data, id)
unsigned short data_length;
char *data;
AuID id;
{
    struct auth *new;

    new = (struct auth *) xalloc(sizeof(struct auth));
    if (!new)
        return 0;
    new->data = (char *) xalloc((unsigned) data_length);
    if (!new->data) {
        xfree(new);
        return 0;
    }
    new->next = mit_auth;
    mit_auth = new;
    bcopy(data, new->data, (int) data_length);
    new->len = data_length;
    new->id = id;
    return 1;
}

AuID
MitCheckCookie(data_length, data)
unsigned short data_length;
char *data;
{
    struct auth *auth;

    for (auth = mit_auth; auth; auth = auth->next) {
        if (data_length == auth->len &&
            bcmp(data, auth->data, (int) data_length) == 0)
            return auth->id;
    }
    return (AuID) - 1;
}

int
MitResetCookie()
{
    struct auth *auth, *next;

    for (auth = mit_auth; auth; auth = next) {
        next = auth->next;
        xfree(auth->data);
        xfree(auth);
    }
    mit_auth = 0;
}

AuID
MitToID(data_length, data)
unsigned short data_length;
char *data;
{
    struct auth *auth;

    for (auth = mit_auth; auth; auth = auth->next) {
        if (data_length == auth->len &&
            bcmp(data, auth->data, data_length) == 0)
            return auth->id;
    }
    return (AuID) - 1;
}

MitFromID(id, data_lenp, datap)
AuID id;
unsigned short *data_lenp;
char **datap;
{
    struct auth *auth;

    for (auth = mit_auth; auth; auth = auth->next) {
        if (id == auth->id) {
            *data_lenp = auth->len;
            *datap = auth->data;
            return 1;
        }
    }
    return 0;
}

MitRemoveCookie(data_length, data)
unsigned short data_length;
char *data;
{
    struct auth *auth, *prev;

    prev = 0;
    for (auth = mit_auth; auth; auth = auth->next) {
        if (data_length == auth->len &&
            bcmp(data, auth->data, data_length) == 0) {
            if (prev)
                prev->next = auth->next;
            else
                mit_auth = auth->next;
            xfree(auth->data);
            xfree(auth);
            return 1;
        }
    }
    return 0;
}
