xref: /netbsd/usr.bin/at/perm.c (revision bf9ec67e)
1 /*	$NetBSD: perm.c,v 1.2 2000/10/04 19:24:59 mjl 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 <stddef.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 /* Local headers */
40 
41 #include "at.h"
42 #include "panic.h"
43 #include "pathnames.h"
44 #include "privs.h"
45 #include "perm.h"
46 
47 /* File scope variables */
48 
49 #ifndef lint
50 #if 0
51 static char rcsid[] = "$OpenBSD: perm.c,v 1.1 1997/03/01 23:40:12 millert Exp $";
52 #else
53 __RCSID("$NetBSD: perm.c,v 1.2 2000/10/04 19:24:59 mjl Exp $");
54 #endif
55 #endif
56 
57 /* Function declarations */
58 
59 static int check_for_user (FILE *, const char *);
60 
61 /* Local functions */
62 
63 static int
64 check_for_user(FILE *fp, const char *name)
65 {
66 	char *buffer;
67 	size_t len;
68 	int found = 0;
69 
70 	len = strlen(name);
71 	if ((buffer = malloc(len + 2)) == NULL)
72 		panic("Insufficient virtual memory");
73 
74 	while (fgets(buffer, len + 2, fp) != NULL) {
75 		if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
76 			found = 1;
77 			break;
78 		}
79 	}
80 	(void)fclose(fp);
81 	free(buffer);
82 	return (found);
83 }
84 
85 
86 /* Global functions */
87 
88 int
89 check_permission(void)
90 {
91 	FILE *fp;
92 	uid_t uid = geteuid();
93 	struct passwd *pentry;
94 
95 	if (uid==0)
96 		return 1;
97 
98 	if ((pentry = getpwuid(uid)) == NULL) {
99 		perror("Cannot access user database");
100 		exit(EXIT_FAILURE);
101 	}
102 
103 	PRIV_START
104 
105 	fp = fopen(_PATH_AT_ALLOW, "r");
106 
107 	PRIV_END
108 
109 	if (fp != NULL) {
110 		return (check_for_user(fp, pentry->pw_name));
111 	} else {
112 		PRIV_START
113 
114 		fp = fopen(_PATH_AT_DENY, "r");
115 
116 		PRIV_END
117 
118 		if (fp != NULL)
119 			return (!check_for_user(fp, pentry->pw_name));
120 	}
121 	return (0);
122 }
123