perm.c revision 80294
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
2654158Scharnier#ifndef lint
2754158Scharnierstatic const char rcsid[] =
2854158Scharnier  "$FreeBSD: head/usr.bin/at/perm.c 80294 2001-07-24 14:15:51Z obrien $";
2954158Scharnier#endif /* not lint */
3054158Scharnier
317767Sache/* System Headers */
327767Sache
337767Sache#include <sys/types.h>
3454158Scharnier#include <err.h>
357767Sache#include <errno.h>
367767Sache#include <pwd.h>
377767Sache#include <stddef.h>
387767Sache#include <stdio.h>
397767Sache#include <stdlib.h>
407767Sache#include <string.h>
417767Sache#include <unistd.h>
427767Sache
437767Sache/* Local headers */
447767Sache
457767Sache#include "privs.h"
467767Sache#include "at.h"
477767Sache
487767Sache/* Macros */
497767Sache
507767Sache#define MAXUSERID 10
517767Sache
527767Sache/* Structures and unions */
537767Sache
547767Sache/* Function declarations */
557767Sache
567767Sachestatic int check_for_user(FILE *fp,const char *name);
577767Sache
587767Sache/* Local functions */
597767Sache
607767Sachestatic int check_for_user(FILE *fp,const char *name)
617767Sache{
627767Sache    char *buffer;
637767Sache    size_t len;
647767Sache    int found = 0;
657767Sache
667767Sache    len = strlen(name);
6780294Sobrien    if ((buffer = malloc(len+2)) == NULL)
6880294Sobrien	errx(EXIT_FAILURE, "virtual memory exhausted");
697767Sache
707767Sache    while(fgets(buffer, len+2, fp) != NULL)
717767Sache    {
727767Sache	if ((strncmp(name, buffer, len) == 0) &&
737767Sache	    (buffer[len] == '\n'))
747767Sache	{
757767Sache	    found = 1;
767767Sache	    break;
777767Sache	}
787767Sache    }
797767Sache    fclose(fp);
807767Sache    free(buffer);
817767Sache    return found;
827767Sache}
837767Sache/* Global functions */
847767Sacheint check_permission()
857767Sache{
867767Sache    FILE *fp;
877767Sache    uid_t uid = geteuid();
887767Sache    struct passwd *pentry;
897767Sache
907767Sache    if (uid==0)
917767Sache	return 1;
927767Sache
937767Sache    if ((pentry = getpwuid(uid)) == NULL)
9454158Scharnier	err(EXIT_FAILURE, "cannot access user database");
957767Sache
967767Sache    PRIV_START
977767Sache
987767Sache    fp=fopen(PERM_PATH "at.allow","r");
997767Sache
1007767Sache    PRIV_END
1017767Sache
1027767Sache    if (fp != NULL)
1037767Sache    {
1047767Sache	return check_for_user(fp, pentry->pw_name);
1057767Sache    }
10648386Sbillf    else if (errno == ENOENT)
1077767Sache    {
1087767Sache
1097767Sache	PRIV_START
1107767Sache
1117767Sache	fp=fopen(PERM_PATH "at.deny", "r");
1127767Sache
1137767Sache	PRIV_END
1147767Sache
1157767Sache	if (fp != NULL)
1167767Sache	{
1177767Sache	    return !check_for_user(fp, pentry->pw_name);
1187767Sache	}
11948386Sbillf	else if (errno != ENOENT)
12054158Scharnier	    warn("at.deny");
1217767Sache    }
12248386Sbillf    else
12354158Scharnier	warn("at.allow");
1247767Sache    return 0;
1257767Sache}
126