]> git.saurik.com Git - apple/system_cmds.git/blame - at.tproj/perm.c
system_cmds-196.tar.gz
[apple/system_cmds.git] / at.tproj / perm.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * perm.c - check user permission for at(1)
26 * Copyright (C) 1994 Thomas Koenig
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. The name of the author(s) may not be used to endorse or promote
34 * products derived from this software without specific prior written
35 * permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
40 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
41 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
46 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 */
48
49/* System Headers */
50
51#include <sys/types.h>
52#include <errno.h>
53#include <pwd.h>
54#include <stddef.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60/* Local headers */
61
62#include "privs.h"
63#include "at.h"
64#include "pathnames.h"
65
66/* Macros */
67
68#define MAXUSERID 10
69
70/* Structures and unions */
71
72/* File scope variables */
73
74static char rcsid[] = "$Id: perm.c,v 1.1.1.2 2000/01/11 02:10:05 wsanchez Exp $";
75
76/* Function declarations */
77
78static int check_for_user(FILE *fp,const char *name);
79
80/* Local functions */
81
82static int check_for_user(FILE *fp,const char *name)
83{
84 char *buffer;
85 size_t len;
86 int found = 0;
87
88 len = strlen(name);
89 if ((buffer = malloc(sizeof (char) * (len+2))) == NULL) {
90 fprintf(stderr, "malloc error!");
91 exit(EXIT_FAILURE);
92 }
93
94 while(fgets(buffer, len+2, fp) != NULL)
95 {
96 if ((strncmp(name, buffer, len) == 0) &&
97 (buffer[len] == '\n'))
98 {
99 found = 1;
100 break;
101 }
102 }
103 fclose(fp);
104 free(buffer);
105 return found;
106}
107/* Global functions */
108int check_permission()
109{
110 FILE *fp;
111 uid_t uid = geteuid();
112 struct passwd *pentry;
113
114 if (uid==0)
115 return 1;
116
117 if ((pentry = getpwuid(uid)) == NULL)
118 {
119 perror("Cannot access user database");
120 exit(EXIT_FAILURE);
121 }
122
123 PRIV_START
124
125 fp=fopen(_PATH_AT "at.allow","r");
126
127 PRIV_END
128
129 if (fp != NULL)
130 {
131 return check_for_user(fp, pentry->pw_name);
132 }
133 else
134 {
135
136 PRIV_START
137
138 fp=fopen(_PATH_AT "at.deny", "r");
139
140 PRIV_END
141
142 if (fp != NULL)
143 {
144 return !check_for_user(fp, pentry->pw_name);
145 }
146 perror("at.deny");
147 }
148 return 0;
149}