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