Annotation of src/usr.bin/at/perm.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: perm.c,v 1.3 2002/05/11 23:16:44 millert Exp $ */
1.1 millert 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: #include <sys/types.h>
29: #include <errno.h>
30: #include <pwd.h>
31: #include <stddef.h>
32: #include <stdio.h>
33: #include <stdlib.h>
34: #include <string.h>
35: #include <unistd.h>
36:
37: #include "at.h"
38: #include "panic.h"
39: #include "pathnames.h"
40: #include "privs.h"
41:
42: #ifndef lint
1.4 ! millert 43: static const char rcsid[] = "$OpenBSD: perm.c,v 1.3 2002/05/11 23:16:44 millert Exp $";
1.1 millert 44: #endif
45:
1.2 millert 46: static int check_for_user(FILE *, const char *);
1.1 millert 47:
48:
49: static int
1.3 millert 50: check_for_user(FILE *fp, const char *name)
1.1 millert 51: {
52: char *buffer;
53: size_t len;
54: int found = 0;
55:
56: len = strlen(name);
57: if ((buffer = malloc(len + 2)) == NULL)
58: panic("Insufficient virtual memory");
59:
60: while (fgets(buffer, len + 2, fp) != NULL) {
61: if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
62: found = 1;
63: break;
64: }
65: }
66: (void)fclose(fp);
67: free(buffer);
68: return (found);
69: }
70:
71:
72: int
1.3 millert 73: check_permission(void)
1.1 millert 74: {
75: FILE *fp;
76: uid_t uid = geteuid();
77: struct passwd *pentry;
78:
79: if (uid==0)
80: return 1;
81:
82: if ((pentry = getpwuid(uid)) == NULL) {
1.4 ! millert 83: perror("Cannot access password database");
1.1 millert 84: exit(EXIT_FAILURE);
85: }
86:
1.3 millert 87: PRIV_START;
1.1 millert 88:
89: fp = fopen(_PATH_AT_ALLOW, "r");
90:
1.3 millert 91: PRIV_END;
1.1 millert 92:
93: if (fp != NULL) {
94: return (check_for_user(fp, pentry->pw_name));
95: } else {
1.3 millert 96: PRIV_START;
1.1 millert 97:
98: fp = fopen(_PATH_AT_DENY, "r");
99:
1.3 millert 100: PRIV_END;
1.1 millert 101:
102: if (fp != NULL)
103: return (!check_for_user(fp, pentry->pw_name));
104: else
105: perror(_PATH_AT_DENY);
106: }
107: return (0);
108: }