perm.c revision 8874
11558Srgrimes/*
21558Srgrimes *  perm.c - check user permission for at(1)
31558Srgrimes *  Copyright (C) 1994  Thomas Koenig
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. The name of the author(s) may not be used to endorse or promote
111558Srgrimes *    products derived from this software without specific prior written
121558Srgrimes *    permission.
131558Srgrimes *
141558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151558Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161558Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171558Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181558Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191558Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201558Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211558Srgrimes * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221558Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231558Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241558Srgrimes */
251558Srgrimes
261558Srgrimes/* System Headers */
271558Srgrimes
281558Srgrimes#include <sys/types.h>
291558Srgrimes#include <errno.h>
301558Srgrimes#include <pwd.h>
311558Srgrimes#include <stddef.h>
321558Srgrimes#include <stdio.h>
331558Srgrimes#include <stdlib.h>
3437663Scharnier#include <string.h>
351558Srgrimes#include <unistd.h>
361558Srgrimes
372999Swollman/* Local headers */
381558Srgrimes
39105267Scharnier#include "privs.h"
401558Srgrimes#include "at.h"
4137663Scharnier
42105267Scharnier/* Macros */
4337663Scharnier
441558Srgrimes#define MAXUSERID 10
45105267Scharnier
46105267Scharnier/* Structures and unions */
47105267Scharnier
481558Srgrimes
49192934Srmacklem/* File scope variables */
50192934Srmacklem
51192934Srmacklemstatic char rcsid[] = "$Id: perm.c,v 1.1 1995/04/12 02:42:37 ache Exp $";
521558Srgrimes
531558Srgrimes/* Function declarations */
54192934Srmacklem
551558Srgrimesstatic int check_for_user(FILE *fp,const char *name);
561558Srgrimes
571558Srgrimes/* Local functions */
58109363Smbr
591558Srgrimesstatic int check_for_user(FILE *fp,const char *name)
6074462Salfred{
6174462Salfred    char *buffer;
629336Sdfr    size_t len;
63192934Srmacklem    int found = 0;
6483653Speter
651558Srgrimes    len = strlen(name);
66192934Srmacklem    buffer = mymalloc(len+2);
67192934Srmacklem
681558Srgrimes    while(fgets(buffer, len+2, fp) != NULL)
691558Srgrimes    {
701558Srgrimes	if ((strncmp(name, buffer, len) == 0) &&
7137663Scharnier	    (buffer[len] == '\n'))
721558Srgrimes	{
731558Srgrimes	    found = 1;
74149433Spjd	    break;
75103949Smike	}
761558Srgrimes    }
771558Srgrimes    fclose(fp);
781558Srgrimes    free(buffer);
791558Srgrimes    return found;
801558Srgrimes}
811558Srgrimes/* Global functions */
821558Srgrimesint check_permission()
831558Srgrimes{
84158857Srodrigc    FILE *fp;
851558Srgrimes    uid_t uid = geteuid();
861558Srgrimes    struct passwd *pentry;
871558Srgrimes
881558Srgrimes    if (uid==0)
891558Srgrimes	return 1;
901558Srgrimes
911558Srgrimes    if ((pentry = getpwuid(uid)) == NULL)
921558Srgrimes    {
931558Srgrimes	perror("Cannot access user database");
941558Srgrimes	exit(EXIT_FAILURE);
95194880Sdfr    }
96194880Sdfr
971558Srgrimes    PRIV_START
981558Srgrimes
991558Srgrimes    fp=fopen(PERM_PATH "at.allow","r");
1001558Srgrimes
1011558Srgrimes    PRIV_END
1021558Srgrimes
1031558Srgrimes    if (fp != NULL)
1041558Srgrimes    {
1051558Srgrimes	return check_for_user(fp, pentry->pw_name);
1061558Srgrimes    }
1071558Srgrimes    else
1089336Sdfr    {
1091558Srgrimes
1101558Srgrimes	PRIV_START
1111558Srgrimes
1121558Srgrimes	fp=fopen(PERM_PATH "at.deny", "r");
1131558Srgrimes
1141558Srgrimes	PRIV_END
1151558Srgrimes
1161558Srgrimes	if (fp != NULL)
11727447Sdfr	{
118184588Sdfr	    return !check_for_user(fp, pentry->pw_name);
119184588Sdfr	}
1201558Srgrimes	perror("at.deny");
1211558Srgrimes    }
1221558Srgrimes    return 0;
1231558Srgrimes}
1241558Srgrimes