Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / at / perm.c
blob34ea40f51739975bad94bda2ee20365f6bb16e4a
1 /* $NetBSD: perm.c,v 1.2 2000/10/04 19:24:59 mjl Exp $ */
3 /*
4 * perm.c - check user permission for at(1)
5 * Copyright (C) 1994 Thomas Koenig
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.
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.
28 /* System Headers */
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>
40 /* Local headers */
42 #include "at.h"
43 #include "panic.h"
44 #include "pathnames.h"
45 #include "privs.h"
46 #include "perm.h"
48 /* File scope variables */
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.2 2000/10/04 19:24:59 mjl Exp $");
55 #endif
56 #endif
58 /* Local functions */
60 static bool
61 check_for_user(FILE *fp, const char *name)
63 char *buffer;
64 size_t len;
65 bool found = false;
67 len = strlen(name);
68 if ((buffer = malloc(len + 2)) == NULL)
69 panic("Insufficient virtual memory");
71 while (fgets(buffer, (int)len + 2, fp) != NULL) {
72 if (strncmp(name, buffer, len) == 0 && buffer[len] == '\n') {
73 found = true;
74 break;
77 (void)fclose(fp);
78 free(buffer);
79 return found;
82 /* Global functions */
84 bool
85 check_permission(void)
87 FILE *fp;
88 uid_t uid = geteuid();
89 struct passwd *pentry;
91 if (uid == 0)
92 return true;
94 if ((pentry = getpwuid(uid)) == NULL) {
95 perror("Cannot access user database");
96 exit(EXIT_FAILURE);
99 PRIV_START;
101 fp = fopen(_PATH_AT_ALLOW, "r");
103 PRIV_END;
105 if (fp != NULL) {
106 return check_for_user(fp, pentry->pw_name);
107 } else {
108 PRIV_START;
110 fp = fopen(_PATH_AT_DENY, "r");
112 PRIV_END;
114 if (fp != NULL)
115 return !check_for_user(fp, pentry->pw_name);
117 return false;