/*-1* perm.c - check user permission for at(1)2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (C) 1994 Thomas Koenig6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. The name of the author(s) may not be used to endorse or promote13* products derived from this software without specific prior written14* permission.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/cdefs.h>29/* System Headers */3031#include <sys/types.h>32#include <err.h>33#include <errno.h>34#include <pwd.h>35#include <stddef.h>36#include <stdio.h>37#include <stdlib.h>38#include <string.h>39#include <unistd.h>4041/* Local headers */4243#include "at.h"44#include "perm.h"45#include "privs.h"4647/* Macros */4849#define MAXUSERID 105051/* Structures and unions */5253/* Function declarations */5455static int check_for_user(FILE *fp,const char *name);5657/* Local functions */5859static int check_for_user(FILE *fp,const char *name)60{61char *buffer;62size_t len;63int found = 0;6465len = strlen(name);66if ((buffer = malloc(len+2)) == NULL)67errx(EXIT_FAILURE, "virtual memory exhausted");6869while(fgets(buffer, len+2, fp) != NULL)70{71if ((strncmp(name, buffer, len) == 0) &&72(buffer[len] == '\n'))73{74found = 1;75break;76}77}78fclose(fp);79free(buffer);80return found;81}82/* Global functions */83int check_permission(void)84{85FILE *fp;86uid_t uid = geteuid();87struct passwd *pentry;8889if (uid==0)90return 1;9192if ((pentry = getpwuid(uid)) == NULL)93err(EXIT_FAILURE, "cannot access user database");9495PRIV_START9697fp=fopen(PERM_PATH "at.allow","r");9899PRIV_END100101if (fp != NULL)102{103return check_for_user(fp, pentry->pw_name);104}105else if (errno == ENOENT)106{107108PRIV_START109110fp=fopen(PERM_PATH "at.deny", "r");111112PRIV_END113114if (fp != NULL)115{116return !check_for_user(fp, pentry->pw_name);117}118else if (errno != ENOENT)119warn("at.deny");120}121else122warn("at.allow");123return 0;124}125126127