Home | History | Annotate | Line # | Download | only in at
      1  1.4  dholland /*	$NetBSD: perm.c,v 1.4 2016/03/13 00:32:09 dholland Exp $	*/
      2  1.1  christos 
      3  1.3  christos /*
      4  1.1  christos  * perm.c - check user permission for at(1)
      5  1.1  christos  * Copyright (C) 1994  Thomas Koenig
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. The name of the author(s) may not be used to endorse or promote
     13  1.1  christos  *    products derived from this software without specific prior written
     14  1.1  christos  *    permission.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
     17  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  christos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  christos  * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  christos  */
     27  1.1  christos 
     28  1.1  christos /* System Headers */
     29  1.1  christos 
     30  1.1  christos #include <sys/types.h>
     31  1.1  christos #include <errno.h>
     32  1.1  christos #include <pwd.h>
     33  1.3  christos #include <stdbool.h>
     34  1.1  christos #include <stddef.h>
     35  1.1  christos #include <stdio.h>
     36  1.1  christos #include <stdlib.h>
     37  1.1  christos #include <string.h>
     38  1.1  christos #include <unistd.h>
     39  1.1  christos 
     40  1.1  christos /* Local headers */
     41  1.1  christos 
     42  1.1  christos #include "at.h"
     43  1.1  christos #include "panic.h"
     44  1.1  christos #include "pathnames.h"
     45  1.1  christos #include "privs.h"
     46  1.1  christos #include "perm.h"
     47  1.1  christos 
     48  1.1  christos /* File scope variables */
     49  1.1  christos 
     50  1.1  christos #ifndef lint
     51  1.1  christos #if 0
     52  1.1  christos static char rcsid[] = "$OpenBSD: perm.c,v 1.1 1997/03/01 23:40:12 millert Exp $";
     53  1.1  christos #else
     54  1.4  dholland __RCSID("$NetBSD: perm.c,v 1.4 2016/03/13 00:32:09 dholland Exp $");
     55  1.1  christos #endif
     56  1.1  christos #endif
     57  1.1  christos 
     58  1.1  christos /* Local functions */
     59  1.1  christos 
     60  1.3  christos static bool
     61  1.2       mjl check_for_user(FILE *fp, const char *name)
     62  1.1  christos {
     63  1.1  christos 	char *buffer;
     64  1.1  christos 	size_t len;
     65  1.3  christos 	bool found = false;
     66  1.1  christos 
     67  1.1  christos 	len = strlen(name);
     68  1.1  christos 	if ((buffer = malloc(len + 2)) == NULL)
     69  1.1  christos 		panic("Insufficient virtual memory");
     70  1.1  christos 
     71  1.3  christos 	while (fgets(buffer, (int)len + 2, fp) != NULL) {
     72  1.1  christos 		if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
     73  1.3  christos 			found = true;
     74  1.1  christos 			break;
     75  1.1  christos 		}
     76  1.1  christos 	}
     77  1.1  christos 	(void)fclose(fp);
     78  1.1  christos 	free(buffer);
     79  1.3  christos 	return found;
     80  1.1  christos }
     81  1.1  christos 
     82  1.1  christos /* Global functions */
     83  1.1  christos 
     84  1.3  christos bool
     85  1.2       mjl check_permission(void)
     86  1.1  christos {
     87  1.1  christos 	FILE *fp;
     88  1.1  christos 	uid_t uid = geteuid();
     89  1.1  christos 	struct passwd *pentry;
     90  1.1  christos 
     91  1.3  christos 	if (uid == 0)
     92  1.3  christos 		return true;
     93  1.1  christos 
     94  1.1  christos 	if ((pentry = getpwuid(uid)) == NULL) {
     95  1.1  christos 		perror("Cannot access user database");
     96  1.1  christos 		exit(EXIT_FAILURE);
     97  1.1  christos 	}
     98  1.1  christos 
     99  1.4  dholland 	privs_enter();
    100  1.1  christos 
    101  1.1  christos 	fp = fopen(_PATH_AT_ALLOW, "r");
    102  1.1  christos 
    103  1.4  dholland 	privs_exit();
    104  1.1  christos 
    105  1.1  christos 	if (fp != NULL) {
    106  1.3  christos 		return check_for_user(fp, pentry->pw_name);
    107  1.1  christos 	} else {
    108  1.4  dholland 		privs_enter();
    109  1.1  christos 
    110  1.1  christos 		fp = fopen(_PATH_AT_DENY, "r");
    111  1.1  christos 
    112  1.4  dholland 		privs_exit();
    113  1.1  christos 
    114  1.1  christos 		if (fp != NULL)
    115  1.3  christos 			return !check_for_user(fp, pentry->pw_name);
    116  1.1  christos 	}
    117  1.3  christos 	return false;
    118  1.1  christos }
    119