110154Sache/*
27767Sache *  perm.c - check user permission for at(1)
37767Sache *  Copyright (C) 1994  Thomas Koenig
47767Sache *
57767Sache * Redistribution and use in source and binary forms, with or without
67767Sache * modification, are permitted provided that the following conditions
77767Sache * are met:
87767Sache * 1. Redistributions of source code must retain the above copyright
97767Sache *    notice, this list of conditions and the following disclaimer.
107767Sache * 2. The name of the author(s) may not be used to endorse or promote
117767Sache *    products derived from this software without specific prior written
127767Sache *    permission.
137767Sache *
147767Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
157767Sache * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
167767Sache * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1710154Sache * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
187767Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
197767Sache * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
207767Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
217767Sache * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
227767Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
237767Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247767Sache */
257767Sache
2687230Smarkm#include <sys/cdefs.h>
2787230Smarkm__FBSDID("$FreeBSD$");
2887230Smarkm
297767Sache/* System Headers */
307767Sache
317767Sache#include <sys/types.h>
3254158Scharnier#include <err.h>
337767Sache#include <errno.h>
347767Sache#include <pwd.h>
357767Sache#include <stddef.h>
367767Sache#include <stdio.h>
377767Sache#include <stdlib.h>
387767Sache#include <string.h>
397767Sache#include <unistd.h>
407767Sache
417767Sache/* Local headers */
427767Sache
4387208Smarkm#include "at.h"
4487208Smarkm#include "perm.h"
457767Sache#include "privs.h"
467767Sache
477767Sache/* Macros */
487767Sache
497767Sache#define MAXUSERID 10
507767Sache
517767Sache/* Structures and unions */
527767Sache
537767Sache/* Function declarations */
547767Sache
557767Sachestatic int check_for_user(FILE *fp,const char *name);
567767Sache
577767Sache/* Local functions */
587767Sache
597767Sachestatic int check_for_user(FILE *fp,const char *name)
607767Sache{
617767Sache    char *buffer;
627767Sache    size_t len;
637767Sache    int found = 0;
647767Sache
657767Sache    len = strlen(name);
6680294Sobrien    if ((buffer = malloc(len+2)) == NULL)
6780294Sobrien	errx(EXIT_FAILURE, "virtual memory exhausted");
687767Sache
697767Sache    while(fgets(buffer, len+2, fp) != NULL)
707767Sache    {
717767Sache	if ((strncmp(name, buffer, len) == 0) &&
727767Sache	    (buffer[len] == '\n'))
737767Sache	{
747767Sache	    found = 1;
757767Sache	    break;
767767Sache	}
777767Sache    }
787767Sache    fclose(fp);
797767Sache    free(buffer);
807767Sache    return found;
817767Sache}
827767Sache/* Global functions */
8387208Smarkmint check_permission(void)
847767Sache{
857767Sache    FILE *fp;
867767Sache    uid_t uid = geteuid();
877767Sache    struct passwd *pentry;
887767Sache
897767Sache    if (uid==0)
907767Sache	return 1;
917767Sache
927767Sache    if ((pentry = getpwuid(uid)) == NULL)
9354158Scharnier	err(EXIT_FAILURE, "cannot access user database");
947767Sache
957767Sache    PRIV_START
967767Sache
977767Sache    fp=fopen(PERM_PATH "at.allow","r");
987767Sache
997767Sache    PRIV_END
1007767Sache
1017767Sache    if (fp != NULL)
1027767Sache    {
1037767Sache	return check_for_user(fp, pentry->pw_name);
1047767Sache    }
10548386Sbillf    else if (errno == ENOENT)
1067767Sache    {
1077767Sache
1087767Sache	PRIV_START
1097767Sache
1107767Sache	fp=fopen(PERM_PATH "at.deny", "r");
1117767Sache
1127767Sache	PRIV_END
1137767Sache
1147767Sache	if (fp != NULL)
1157767Sache	{
1167767Sache	    return !check_for_user(fp, pentry->pw_name);
1177767Sache	}
11848386Sbillf	else if (errno != ENOENT)
11954158Scharnier	    warn("at.deny");
1207767Sache    }
12148386Sbillf    else
12254158Scharnier	warn("at.allow");
1237767Sache    return 0;
1247767Sache}
125