/*
 *  This file is part of ixnet.library for the Amiga.
 *  Copyright (C) 1996 Jeff Shepherd
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id: $
 *
 *  $Log:$
 *
 */


/* stubs for group-file access functions */

#define KERNEL
#include "ixnet.h"
#include "kprintf.h"
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int getpgrp(int pid) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;
    int err;

    switch (p->u_networkprotocol) {

	case IX_NETWORK_AMITCP:
	    err = UG_getpgrp(p->u_UserGroupBase);

	    if (err == -1) {
		errno = ug_GetErr(p->u_UserGroupBase);
		err = (pid ? pid : err);
	    }
	break;
	default:
	    err = (pid ? pid : (int) FindTask(0));
    }
    return err;
}


int getgroups(int gidsetlen, int *gidset) {
    int err = -1;
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    switch (p->u_networkprotocol) {

	case IX_NETWORK_AMITCP:
	    err = UG_getgroups(p->u_UserGroupBase,gidsetlen,gidset);

	    if (err == -1) {
		errno = ug_GetErr(p->u_UserGroupBase);
	    }
	break;

	case IX_NETWORK_AS225:
	    err = SOCK_getgroups(p->u_SockBase,gidsetlen,gidset);
	break;
    }
    return err;
}

#include <pwd.h>

extern char *getenv(const char *);

int gethostname (char *name, int namelen) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;
    char *host;

    switch (p->u_networkprotocol) {

	case IX_NETWORK_AMITCP:
	    return TCP_GetHostName(p->u_TCPBase,name,namelen);

	default: /* case IX_NETWORK_AS225: */
	    host = getenv("HOSTNAME");
	    if (host)
		strncpy (name, host, namelen);
	    else
		strncpy (name, "localhost",namelen);
	    return 0;
    }
}

static char hostname[64] = "localhost";

int sethostname (const char *name, int namelen) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    if (p->u_networkprotocol != IX_NETWORK_AMITCP) {
	int len = namelen < sizeof (hostname) - 1 ? namelen : sizeof (hostname) - 1;

	strncpy (hostname, name, len);
	hostname[len] = 0;
    }
    return 0;
}

char *crypt (const char *key, const char *setting) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
	return UG_crypt(p->u_UserGroupBase,key,setting);
    }
    else {
	return NULL;
    }
}

int setuid(uid_t uid) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
       return UG_setuid(p->u_UserGroupBase, uid);
    }
    else {
	return 0;
    }
}

int seteuid(uid_t uid) {
    return setreuid(getuid(),uid);
}

int setgid(gid_t gid) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
       return UG_setgid(p->u_UserGroupBase, gid);
    }
    else {
	return 0;
    }
}

int setegid(gid_t gid) {
    return setregid(getgid(),gid);
}

int setreuid(int ruid, int euid) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
       return UG_setreuid(p->u_UserGroupBase, ruid, euid);
    }
    else {
	return 0;
    }
}


int setregid(int rgid, int egid) {
    register struct ixnet *p = (struct ixnet *)u.u_ixnet;

    if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
       return UG_setregid(p->u_UserGroupBase, rgid, egid);
    }
    else {
	return 0;
    }
}

