head	1.4;
access;
symbols
	netbsd-11-0-RC4:1.4
	netbsd-11-0-RC3:1.4
	netbsd-11-0-RC2:1.4
	netbsd-11-0-RC1:1.4
	perseant-exfatfs-base-20250801:1.4
	netbsd-11:1.4.0.30
	netbsd-11-base:1.4
	netbsd-10-1-RELEASE:1.4
	perseant-exfatfs-base-20240630:1.4
	perseant-exfatfs:1.4.0.28
	perseant-exfatfs-base:1.4
	netbsd-8-3-RELEASE:1.4
	netbsd-9-4-RELEASE:1.4
	netbsd-10-0-RELEASE:1.4
	netbsd-10-0-RC6:1.4
	netbsd-10-0-RC5:1.4
	netbsd-10-0-RC4:1.4
	netbsd-10-0-RC3:1.4
	netbsd-10-0-RC2:1.4
	netbsd-10-0-RC1:1.4
	netbsd-10:1.4.0.26
	netbsd-10-base:1.4
	netbsd-9-3-RELEASE:1.4
	cjep_sun2x-base1:1.4
	cjep_sun2x:1.4.0.24
	cjep_sun2x-base:1.4
	cjep_staticlib_x-base1:1.4
	netbsd-9-2-RELEASE:1.4
	cjep_staticlib_x:1.4.0.22
	cjep_staticlib_x-base:1.4
	netbsd-9-1-RELEASE:1.4
	phil-wifi-20200421:1.4
	phil-wifi-20200411:1.4
	is-mlppp:1.4.0.20
	is-mlppp-base:1.4
	phil-wifi-20200406:1.4
	netbsd-8-2-RELEASE:1.4
	netbsd-9-0-RELEASE:1.4
	netbsd-9-0-RC2:1.4
	netbsd-9-0-RC1:1.4
	phil-wifi-20191119:1.4
	netbsd-9:1.4.0.18
	netbsd-9-base:1.4
	phil-wifi-20190609:1.4
	netbsd-8-1-RELEASE:1.4
	netbsd-8-1-RC1:1.4
	pgoyette-compat-merge-20190127:1.4
	pgoyette-compat-20190127:1.4
	pgoyette-compat-20190118:1.4
	pgoyette-compat-1226:1.4
	pgoyette-compat-1126:1.4
	pgoyette-compat-1020:1.4
	pgoyette-compat-0930:1.4
	pgoyette-compat-0906:1.4
	netbsd-7-2-RELEASE:1.3
	pgoyette-compat-0728:1.4
	netbsd-8-0-RELEASE:1.4
	phil-wifi:1.4.0.16
	phil-wifi-base:1.4
	pgoyette-compat-0625:1.4
	netbsd-8-0-RC2:1.4
	pgoyette-compat-0521:1.4
	pgoyette-compat-0502:1.4
	pgoyette-compat-0422:1.4
	netbsd-8-0-RC1:1.4
	pgoyette-compat-0415:1.4
	pgoyette-compat-0407:1.4
	pgoyette-compat-0330:1.4
	pgoyette-compat-0322:1.4
	pgoyette-compat-0315:1.4
	netbsd-7-1-2-RELEASE:1.3
	pgoyette-compat:1.4.0.14
	pgoyette-compat-base:1.4
	netbsd-7-1-1-RELEASE:1.3
	matt-nb8-mediatek:1.4.0.12
	matt-nb8-mediatek-base:1.4
	perseant-stdc-iso10646:1.4.0.10
	perseant-stdc-iso10646-base:1.4
	netbsd-8:1.4.0.8
	netbsd-8-base:1.4
	prg-localcount2-base3:1.4
	prg-localcount2-base2:1.4
	prg-localcount2-base1:1.4
	prg-localcount2:1.4.0.6
	prg-localcount2-base:1.4
	pgoyette-localcount-20170426:1.4
	bouyer-socketcan-base1:1.4
	pgoyette-localcount-20170320:1.4
	netbsd-7-1:1.3.0.48
	netbsd-7-1-RELEASE:1.3
	netbsd-7-1-RC2:1.3
	netbsd-7-nhusb-base-20170116:1.3
	bouyer-socketcan:1.4.0.4
	bouyer-socketcan-base:1.4
	pgoyette-localcount-20170107:1.4
	netbsd-7-1-RC1:1.3
	pgoyette-localcount-20161104:1.4
	netbsd-7-0-2-RELEASE:1.3
	localcount-20160914:1.4
	netbsd-7-nhusb:1.3.0.46
	netbsd-7-nhusb-base:1.3
	pgoyette-localcount-20160806:1.4
	pgoyette-localcount-20160726:1.4
	pgoyette-localcount:1.4.0.2
	pgoyette-localcount-base:1.4
	netbsd-7-0-1-RELEASE:1.3
	netbsd-7-0:1.3.0.44
	netbsd-7-0-RELEASE:1.3
	netbsd-7-0-RC3:1.3
	netbsd-7-0-RC2:1.3
	netbsd-7-0-RC1:1.3
	netbsd-5-2-3-RELEASE:1.3
	netbsd-5-1-5-RELEASE:1.3
	netbsd-6-0-6-RELEASE:1.3
	netbsd-6-1-5-RELEASE:1.3
	netbsd-7:1.3.0.42
	netbsd-7-base:1.3
	yamt-pagecache-base9:1.3
	yamt-pagecache-tag8:1.3
	netbsd-6-1-4-RELEASE:1.3
	netbsd-6-0-5-RELEASE:1.3
	tls-earlyentropy:1.3.0.40
	tls-earlyentropy-base:1.3
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.3
	riastradh-drm2-base3:1.3
	netbsd-6-1-3-RELEASE:1.3
	netbsd-6-0-4-RELEASE:1.3
	netbsd-5-2-2-RELEASE:1.3
	netbsd-5-1-4-RELEASE:1.3
	netbsd-6-1-2-RELEASE:1.3
	netbsd-6-0-3-RELEASE:1.3
	netbsd-5-2-1-RELEASE:1.3
	netbsd-5-1-3-RELEASE:1.3
	netbsd-6-1-1-RELEASE:1.3
	riastradh-drm2-base2:1.3
	riastradh-drm2-base1:1.3
	riastradh-drm2:1.3.0.34
	riastradh-drm2-base:1.3
	netbsd-6-1:1.3.0.38
	netbsd-6-0-2-RELEASE:1.3
	netbsd-6-1-RELEASE:1.3
	netbsd-6-1-RC4:1.3
	netbsd-6-1-RC3:1.3
	agc-symver:1.3.0.36
	agc-symver-base:1.3
	netbsd-6-1-RC2:1.3
	netbsd-6-1-RC1:1.3
	yamt-pagecache-base8:1.3
	netbsd-5-2:1.3.0.32
	netbsd-6-0-1-RELEASE:1.3
	yamt-pagecache-base7:1.3
	netbsd-5-2-RELEASE:1.3
	netbsd-5-2-RC1:1.3
	matt-nb6-plus-nbase:1.3
	yamt-pagecache-base6:1.3
	netbsd-6-0:1.3.0.30
	netbsd-6-0-RELEASE:1.3
	netbsd-6-0-RC2:1.3
	tls-maxphys:1.3.0.28
	tls-maxphys-base:1.3
	matt-nb6-plus:1.3.0.26
	matt-nb6-plus-base:1.3
	netbsd-6-0-RC1:1.3
	yamt-pagecache-base5:1.3
	yamt-pagecache-base4:1.3
	netbsd-6:1.3.0.24
	netbsd-6-base:1.3
	netbsd-5-1-2-RELEASE:1.3
	netbsd-5-1-1-RELEASE:1.3
	yamt-pagecache-base3:1.3
	yamt-pagecache-base2:1.3
	yamt-pagecache:1.3.0.6
	yamt-pagecache-base:1.3
	cherry-xenmp:1.3.0.22
	cherry-xenmp-base:1.3
	bouyer-quota2-nbase:1.3
	bouyer-quota2:1.3.0.20
	bouyer-quota2-base:1.3
	matt-mips64-premerge-20101231:1.3
	matt-nb5-mips64-premerge-20101231:1.3
	matt-nb5-pq3:1.3.0.18
	matt-nb5-pq3-base:1.3
	netbsd-5-1:1.3.0.16
	netbsd-5-1-RELEASE:1.3
	netbsd-5-1-RC4:1.3
	matt-nb5-mips64-k15:1.3
	netbsd-5-1-RC3:1.3
	netbsd-5-1-RC2:1.3
	netbsd-5-1-RC1:1.3
	netbsd-5-0-2-RELEASE:1.3
	matt-nb5-mips64-premerge-20091211:1.3
	matt-premerge-20091211:1.3
	matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.3
	matt-nb4-mips64-k7-u2a-k9b:1.3
	matt-nb5-mips64-u1-k1-k5:1.3
	matt-nb5-mips64:1.3.0.14
	netbsd-5-0-1-RELEASE:1.3
	jym-xensuspend-nbase:1.3
	netbsd-5-0:1.3.0.12
	netbsd-5-0-RELEASE:1.3
	netbsd-5-0-RC4:1.3
	netbsd-5-0-RC3:1.3
	netbsd-5-0-RC2:1.3
	jym-xensuspend:1.3.0.10
	jym-xensuspend-base:1.3
	netbsd-5-0-RC1:1.3
	netbsd-5:1.3.0.8
	netbsd-5-base:1.3
	matt-mips64-base2:1.3
	matt-mips64:1.2.0.34
	netbsd-4-0-1-RELEASE:1.2
	wrstuden-revivesa-base-3:1.3
	wrstuden-revivesa-base-2:1.3
	wrstuden-fixsa-newbase:1.2
	wrstuden-revivesa-base-1:1.3
	yamt-pf42-base4:1.3
	yamt-pf42-base3:1.3
	hpcarm-cleanup-nbase:1.3
	yamt-pf42-baseX:1.3
	yamt-pf42-base2:1.3
	wrstuden-revivesa:1.3.0.4
	wrstuden-revivesa-base:1.3
	yamt-pf42:1.3.0.2
	yamt-pf42-base:1.3
	keiichi-mipv6-nbase:1.2
	keiichi-mipv6:1.2.0.32
	keiichi-mipv6-base:1.2
	matt-armv6-nbase:1.2
	matt-armv6-prevmlocking:1.2
	wrstuden-fixsa-base-1:1.2
	netbsd-4-0:1.2.0.30
	netbsd-4-0-RELEASE:1.2
	cube-autoconf:1.2.0.28
	cube-autoconf-base:1.2
	netbsd-4-0-RC5:1.2
	netbsd-4-0-RC4:1.2
	netbsd-4-0-RC3:1.2
	netbsd-4-0-RC2:1.2
	netbsd-4-0-RC1:1.2
	matt-armv6:1.2.0.26
	matt-armv6-base:1.2
	matt-mips64-base:1.2
	hpcarm-cleanup:1.2.0.24
	hpcarm-cleanup-base:1.2
	netbsd-3-1-1-RELEASE:1.2
	netbsd-3-0-3-RELEASE:1.2
	wrstuden-fixsa:1.2.0.22
	wrstuden-fixsa-base:1.2
	abandoned-netbsd-4-base:1.2
	abandoned-netbsd-4:1.2.0.16
	netbsd-3-1:1.2.0.18
	netbsd-3-1-RELEASE:1.2
	netbsd-3-0-2-RELEASE:1.2
	netbsd-3-1-RC4:1.2
	netbsd-3-1-RC3:1.2
	netbsd-3-1-RC2:1.2
	netbsd-3-1-RC1:1.2
	netbsd-4:1.2.0.20
	netbsd-4-base:1.2
	chap-midi-nbase:1.2
	netbsd-3-0-1-RELEASE:1.2
	chap-midi:1.2.0.14
	chap-midi-base:1.2
	netbsd-3-0:1.2.0.12
	netbsd-3-0-RELEASE:1.2
	netbsd-3-0-RC6:1.2
	netbsd-3-0-RC5:1.2
	netbsd-3-0-RC4:1.2
	netbsd-3-0-RC3:1.2
	netbsd-3-0-RC2:1.2
	netbsd-3-0-RC1:1.2
	netbsd-2-0-3-RELEASE:1.2
	netbsd-2-1:1.2.0.10
	netbsd-2-1-RELEASE:1.2
	netbsd-2-1-RC6:1.2
	netbsd-2-1-RC5:1.2
	netbsd-2-1-RC4:1.2
	netbsd-2-1-RC3:1.2
	netbsd-2-1-RC2:1.2
	netbsd-2-1-RC1:1.2
	netbsd-2-0-2-RELEASE:1.2
	netbsd-3:1.2.0.8
	netbsd-3-base:1.2
	netbsd-2-0-1-RELEASE:1.2
	netbsd-2:1.2.0.6
	netbsd-2-base:1.2
	netbsd-2-0-RELEASE:1.2
	netbsd-2-0-RC5:1.2
	netbsd-2-0-RC4:1.2
	netbsd-2-0-RC3:1.2
	netbsd-2-0-RC2:1.2
	netbsd-2-0-RC1:1.2
	netbsd-2-0:1.2.0.4
	netbsd-2-0-base:1.2
	netbsd-1-6-PATCH002-RELEASE:1.2
	netbsd-1-6-PATCH002:1.2
	netbsd-1-6-PATCH002-RC4:1.2
	netbsd-1-6-PATCH002-RC3:1.2
	netbsd-1-6-PATCH002-RC2:1.2
	netbsd-1-6-PATCH002-RC1:1.2
	netbsd-1-6-PATCH001:1.2
	netbsd-1-6-PATCH001-RELEASE:1.2
	netbsd-1-6-PATCH001-RC3:1.2
	netbsd-1-6-PATCH001-RC2:1.2
	netbsd-1-6-PATCH001-RC1:1.2
	fvdl_fs64_base:1.2
	netbsd-1-6-RELEASE:1.2
	netbsd-1-6-RC3:1.2
	netbsd-1-6-RC2:1.2
	netbsd-1-6-RC1:1.2
	netbsd-1-6:1.2.0.2
	netbsd-1-6-base:1.2
	netbsd-1-5-PATCH003:1.1
	netbsd-1-5-PATCH002:1.1
	netbsd-1-5-PATCH001:1.1
	netbsd-1-5-RELEASE:1.1
	netbsd-1-5-BETA2:1.1
	netbsd-1-5-BETA:1.1
	netbsd-1-4-PATCH003:1.1
	netbsd-1-5-ALPHA2:1.1
	netbsd-1-5:1.1.0.12
	netbsd-1-5-base:1.1
	minoura-xpg4dl-base:1.1
	minoura-xpg4dl:1.1.0.10
	netbsd-1-4-PATCH002:1.1
	wrstuden-devbsize-19991221:1.1
	wrstuden-devbsize:1.1.0.8
	wrstuden-devbsize-base:1.1
	comdex-fall-1999:1.1.0.6
	comdex-fall-1999-base:1.1
	netbsd-1-4-PATCH001:1.1
	netbsd-1-4-RELEASE:1.1
	netbsd-1-4:1.1.0.4
	netbsd-1-4-base:1.1
	netbsd-1-3-PATCH003:1.1.2.2
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.2.2
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.2.2
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.2.2
	netbsd-1-3:1.1.0.2;
locks; strict;
comment	@ * @;


1.4
date	2016.03.13.00.32.09;	author dholland;	state Exp;
branches;
next	1.3;
commitid	C07ZPg8tbU8vOpYy;

1.3
date	2008.04.05.16.26.57;	author christos;	state Exp;
branches;
next	1.2;

1.2
date	2000.10.04.19.24.59;	author mjl;	state Exp;
branches;
next	1.1;

1.1
date	98.06.27.21.15.08;	author christos;	state Exp;
branches
	1.1.2.1;
next	;

1.1.2.1
date	98.06.27.21.15.08;	author cgd;	state dead;
branches;
next	1.1.2.2;

1.1.2.2
date	98.10.24.00.22.56;	author cgd;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Use functions instead of preprocessor abuse.
@
text
@/*	$NetBSD: perm.c,v 1.3 2008/04/05 16:26:57 christos Exp $	*/

/*
 * perm.c - check user permission for at(1)
 * Copyright (C) 1994  Thomas Koenig
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author(s) may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* System Headers */

#include <sys/types.h>
#include <errno.h>
#include <pwd.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* Local headers */

#include "at.h"
#include "panic.h"
#include "pathnames.h"
#include "privs.h"
#include "perm.h"

/* File scope variables */

#ifndef lint
#if 0
static char rcsid[] = "$OpenBSD: perm.c,v 1.1 1997/03/01 23:40:12 millert Exp $";
#else
__RCSID("$NetBSD: perm.c,v 1.3 2008/04/05 16:26:57 christos Exp $");
#endif
#endif

/* Local functions */

static bool
check_for_user(FILE *fp, const char *name)
{
	char *buffer;
	size_t len;
	bool found = false;

	len = strlen(name);
	if ((buffer = malloc(len + 2)) == NULL)
		panic("Insufficient virtual memory");

	while (fgets(buffer, (int)len + 2, fp) != NULL) {
		if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
			found = true;
			break;
		}
	}
	(void)fclose(fp);
	free(buffer);
	return found;
}

/* Global functions */

bool
check_permission(void)
{
	FILE *fp;
	uid_t uid = geteuid();
	struct passwd *pentry;

	if (uid == 0)
		return true;

	if ((pentry = getpwuid(uid)) == NULL) {
		perror("Cannot access user database");
		exit(EXIT_FAILURE);
	}

	privs_enter();

	fp = fopen(_PATH_AT_ALLOW, "r");

	privs_exit();

	if (fp != NULL) {
		return check_for_user(fp, pentry->pw_name);
	} else {
		privs_enter();

		fp = fopen(_PATH_AT_DENY, "r");

		privs_exit();

		if (fp != NULL)
			return !check_for_user(fp, pentry->pw_name);
	}
	return false;
}
@


1.3
log
@Style cleanups from Anon Ymous
@
text
@d1 1
a1 1
/*	$NetBSD: perm.c,v 1.2 2000/10/04 19:24:59 mjl Exp $	*/
d54 1
a54 1
__RCSID("$NetBSD: perm.c,v 1.2 2000/10/04 19:24:59 mjl Exp $");
d99 1
a99 1
	PRIV_START;
d103 1
a103 1
	PRIV_END;
d108 1
a108 1
		PRIV_START;
d112 1
a112 1
		PRIV_END;
@


1.2
log
@Un-__P and ANSIfy.
@
text
@d1 1
a1 1
/*	$NetBSD: perm.c,v 1.1 1998/06/27 21:15:08 christos Exp $	*/
d3 1
a3 1
/* 
d33 1
d54 1
a54 1
__RCSID("$NetBSD: perm.c,v 1.1 1998/06/27 21:15:08 christos Exp $");
a57 4
/* Function declarations */

static int check_for_user (FILE *, const char *);

d60 1
a60 1
static int
d65 1
a65 1
	int found = 0;
d71 1
a71 1
	while (fgets(buffer, len + 2, fp) != NULL) {
d73 1
a73 1
			found = 1;
d79 1
a79 1
	return (found);
a81 1

d84 1
a84 1
int
d91 2
a92 2
	if (uid==0)
		return 1;
d99 1
a99 1
	PRIV_START
d103 1
a103 1
	PRIV_END
d106 1
a106 1
		return (check_for_user(fp, pentry->pw_name));
d108 1
a108 1
		PRIV_START
d112 1
a112 1
		PRIV_END
d115 1
a115 1
			return (!check_for_user(fp, pentry->pw_name));
d117 1
a117 1
	return (0);
@


1.1
log
@Merge in changes from OpenBSD; adds support for at.allow and at.deny, plus
fixes a lot of bugs.
@
text
@d1 1
a1 1
/*	$NetBSD: perm.c,v 1.1 1997/03/01 23:40:12 millert Exp $	*/
d53 1
a53 1
__RCSID("$NetBSD$");
d59 1
a59 1
static int check_for_user __P((FILE *, const char *));
d64 1
a64 3
check_for_user(fp, name)
	FILE *fp;
	const char *name;
d89 1
a89 1
check_permission()
@


1.1.2.1
log
@file perm.c was added on branch netbsd-1-3 on 1998-10-24 00:22:56 +0000
@
text
@d1 124
@


1.1.2.2
log
@pull up rev 1.1 (new) from trunk (mrg)
@
text
@a0 124
/*	$NetBSD: perm.c,v 1.1.2.1 1998/10/24 00:22:56 cgd Exp $	*/

/* 
 * perm.c - check user permission for at(1)
 * Copyright (C) 1994  Thomas Koenig
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author(s) may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* System Headers */

#include <sys/types.h>
#include <errno.h>
#include <pwd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* Local headers */

#include "at.h"
#include "panic.h"
#include "pathnames.h"
#include "privs.h"
#include "perm.h"

/* File scope variables */

#ifndef lint
#if 0
static char rcsid[] = "$OpenBSD: perm.c,v 1.1 1997/03/01 23:40:12 millert Exp $";
#else
__RCSID("$NetBSD: perm.c,v 1.1.2.1 1998/10/24 00:22:56 cgd Exp $");
#endif
#endif

/* Function declarations */

static int check_for_user __P((FILE *, const char *));

/* Local functions */

static int
check_for_user(fp, name)
	FILE *fp;
	const char *name;
{
	char *buffer;
	size_t len;
	int found = 0;

	len = strlen(name);
	if ((buffer = malloc(len + 2)) == NULL)
		panic("Insufficient virtual memory");

	while (fgets(buffer, len + 2, fp) != NULL) {
		if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
			found = 1;
			break;
		}
	}
	(void)fclose(fp);
	free(buffer);
	return (found);
}


/* Global functions */

int
check_permission()
{
	FILE *fp;
	uid_t uid = geteuid();
	struct passwd *pentry;

	if (uid==0)
		return 1;

	if ((pentry = getpwuid(uid)) == NULL) {
		perror("Cannot access user database");
		exit(EXIT_FAILURE);
	}

	PRIV_START

	fp = fopen(_PATH_AT_ALLOW, "r");

	PRIV_END

	if (fp != NULL) {
		return (check_for_user(fp, pentry->pw_name));
	} else {
		PRIV_START

		fp = fopen(_PATH_AT_DENY, "r");

		PRIV_END

		if (fp != NULL)
			return (!check_for_user(fp, pentry->pw_name));
	}
	return (0);
}
@
