perm.c revision 23012
11590Srgrimes/*
21590Srgrimes *  perm.c - check user permission for at(1)
31590Srgrimes *  Copyright (C) 1994  Thomas Koenig
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. The name of the author(s) may not be used to endorse or promote
111590Srgrimes *    products derived from this software without specific prior written
121590Srgrimes *    permission.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151590Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161590Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171590Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
181590Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191590Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201590Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211590Srgrimes * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221590Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231590Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241590Srgrimes */
251590Srgrimes
261590Srgrimes/* System Headers */
271590Srgrimes
281590Srgrimes#include <sys/types.h>
291590Srgrimes#include <errno.h>
3087701Smarkm#include <pwd.h>
3187701Smarkm#include <stddef.h>
3287701Smarkm#include <stdio.h>
3387701Smarkm#include <stdlib.h>
341590Srgrimes#include <string.h>
3587701Smarkm#include <unistd.h>
3628370Scharnier
371590Srgrimes/* Local headers */
381590Srgrimes
3987701Smarkm#include "privs.h"
4028370Scharnier#include "at.h"
4128370Scharnier
421590Srgrimes/* Macros */
431590Srgrimes
4487701Smarkm#define MAXUSERID 10
451590Srgrimes
461590Srgrimes/* Structures and unions */
471590Srgrimes
481590Srgrimes
491590Srgrimes/* File scope variables */
501590Srgrimes
511590Srgrimesstatic char rcsid[] = "$Id$";
52200419Sdelphij
531590Srgrimes/* Function declarations */
541590Srgrimes
551590Srgrimesstatic int check_for_user(FILE *fp,const char *name);
561590Srgrimes
571590Srgrimes/* Local functions */
581590Srgrimes
59229403Sedstatic int check_for_user(FILE *fp,const char *name)
6028370Scharnier{
611590Srgrimes    char *buffer;
621590Srgrimes    size_t len;
631590Srgrimes    int found = 0;
6487701Smarkm
651590Srgrimes    len = strlen(name);
661590Srgrimes    buffer = mymalloc(len+2);
671590Srgrimes
6887701Smarkm    while(fgets(buffer, len+2, fp) != NULL)
691590Srgrimes    {
701590Srgrimes	if ((strncmp(name, buffer, len) == 0) &&
711590Srgrimes	    (buffer[len] == '\n'))
721590Srgrimes	{
731590Srgrimes	    found = 1;
741590Srgrimes	    break;
751590Srgrimes	}
761590Srgrimes    }
771590Srgrimes    fclose(fp);
781590Srgrimes    free(buffer);
791590Srgrimes    return found;
801590Srgrimes}
811590Srgrimes/* Global functions */
821590Srgrimesint check_permission()
831590Srgrimes{
841590Srgrimes    FILE *fp;
851590Srgrimes    uid_t uid = geteuid();
861590Srgrimes    struct passwd *pentry;
871590Srgrimes
881590Srgrimes    if (uid==0)
891590Srgrimes	return 1;
901590Srgrimes
911590Srgrimes    if ((pentry = getpwuid(uid)) == NULL)
921590Srgrimes    {
931590Srgrimes	perror("Cannot access user database");
941590Srgrimes	exit(EXIT_FAILURE);
951590Srgrimes    }
961590Srgrimes
971590Srgrimes    PRIV_START
981590Srgrimes
991590Srgrimes    fp=fopen(PERM_PATH "at.allow","r");
1001590Srgrimes
1011590Srgrimes    PRIV_END
1021590Srgrimes
1031590Srgrimes    if (fp != NULL)
1041590Srgrimes    {
1051590Srgrimes	return check_for_user(fp, pentry->pw_name);
1061590Srgrimes    }
1071590Srgrimes    else
1081590Srgrimes    {
1091590Srgrimes
1101590Srgrimes	PRIV_START
1111590Srgrimes
1121590Srgrimes	fp=fopen(PERM_PATH "at.deny", "r");
1131590Srgrimes
1141590Srgrimes	PRIV_END
115
116	if (fp != NULL)
117	{
118	    return !check_for_user(fp, pentry->pw_name);
119	}
120	perror("at.deny");
121    }
122    return 0;
123}
124