xref: /netbsd-src/usr.bin/at/perm.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: perm.c,v 1.1 1998/06/27 21:15:08 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 <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.1 1998/06/27 21:15:08 christos Exp $");
54 #endif
55 #endif
56 
57 /* Function declarations */
58 
59 static int check_for_user __P((FILE *, const char *));
60 
61 /* Local functions */
62 
63 static int
64 check_for_user(fp, name)
65 	FILE *fp;
66 	const char *name;
67 {
68 	char *buffer;
69 	size_t len;
70 	int found = 0;
71 
72 	len = strlen(name);
73 	if ((buffer = malloc(len + 2)) == NULL)
74 		panic("Insufficient virtual memory");
75 
76 	while (fgets(buffer, len + 2, fp) != NULL) {
77 		if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
78 			found = 1;
79 			break;
80 		}
81 	}
82 	(void)fclose(fp);
83 	free(buffer);
84 	return (found);
85 }
86 
87 
88 /* Global functions */
89 
90 int
91 check_permission()
92 {
93 	FILE *fp;
94 	uid_t uid = geteuid();
95 	struct passwd *pentry;
96 
97 	if (uid==0)
98 		return 1;
99 
100 	if ((pentry = getpwuid(uid)) == NULL) {
101 		perror("Cannot access user database");
102 		exit(EXIT_FAILURE);
103 	}
104 
105 	PRIV_START
106 
107 	fp = fopen(_PATH_AT_ALLOW, "r");
108 
109 	PRIV_END
110 
111 	if (fp != NULL) {
112 		return (check_for_user(fp, pentry->pw_name));
113 	} else {
114 		PRIV_START
115 
116 		fp = fopen(_PATH_AT_DENY, "r");
117 
118 		PRIV_END
119 
120 		if (fp != NULL)
121 			return (!check_for_user(fp, pentry->pw_name));
122 	}
123 	return (0);
124 }
125