xref: /illumos-gate/usr/src/cmd/lp/lib/access/loadaccess.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "stdio.h"
33*7c478bd9Sstevel@tonic-gate #include "errno.h"
34*7c478bd9Sstevel@tonic-gate #include "string.h"
35*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "lp.h"
38*7c478bd9Sstevel@tonic-gate #include "access.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static char		**_loadaccess ( char * );
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /**
43*7c478bd9Sstevel@tonic-gate  ** load_userform_access() - LOAD ALLOW/DENY LISTS FOR USER+FORM
44*7c478bd9Sstevel@tonic-gate  **/
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate int
load_userform_access(char * form,char *** pallow,char *** pdeny)47*7c478bd9Sstevel@tonic-gate load_userform_access(char *form, char ***pallow, char ***pdeny)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Forms, form, "", pallow, pdeny));
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /**
53*7c478bd9Sstevel@tonic-gate  ** load_userprinter_access() - LOAD ALLOW/DENY LISTS FOR USER+PRINTER
54*7c478bd9Sstevel@tonic-gate  **/
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate int
load_userprinter_access(char * printer,char *** pallow,char *** pdeny)57*7c478bd9Sstevel@tonic-gate load_userprinter_access(char *printer, char ***pallow, char ***pdeny)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Printers, printer, UACCESSPREFIX, pallow,
60*7c478bd9Sstevel@tonic-gate 		pdeny));
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /**
64*7c478bd9Sstevel@tonic-gate  ** load_formprinter_access() - LOAD ALLOW/DENY LISTS FOR FORM+PRINTER
65*7c478bd9Sstevel@tonic-gate  **/
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate int
load_formprinter_access(char * printer,char *** pallow,char *** pdeny)68*7c478bd9Sstevel@tonic-gate load_formprinter_access(char *printer, char ***pallow, char ***pdeny)
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Printers, printer, FACCESSPREFIX, pallow,
71*7c478bd9Sstevel@tonic-gate 		pdeny));
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /**
75*7c478bd9Sstevel@tonic-gate  ** load_paperprinter_access() - LOAD ALLOW/DENY LISTS FOR FORM+PRINTER
76*7c478bd9Sstevel@tonic-gate  **/
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate int
load_paperprinter_access(char * printer,char *** pallow,char *** pdeny)79*7c478bd9Sstevel@tonic-gate load_paperprinter_access(char *printer, char ***pallow, char ***pdeny)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Printers, printer, PACCESSPREFIX, pallow,
82*7c478bd9Sstevel@tonic-gate 		pdeny));
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /**
86*7c478bd9Sstevel@tonic-gate  ** loadaccess() - LOAD ALLOW OR DENY LISTS
87*7c478bd9Sstevel@tonic-gate  **/
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate int
loadaccess(char * dir,char * name,char * prefix,char *** pallow,char *** pdeny)90*7c478bd9Sstevel@tonic-gate loadaccess(char *dir, char *name, char *prefix, char ***pallow, char ***pdeny)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	register char		*allow_file	= 0,
93*7c478bd9Sstevel@tonic-gate 				*deny_file	= 0;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	int			ret;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if (
98*7c478bd9Sstevel@tonic-gate 		!(allow_file = getaccessfile(dir, name, prefix, ALLOWFILE))
99*7c478bd9Sstevel@tonic-gate 	     || !(*pallow = _loadaccess(allow_file)) && errno != ENOENT
100*7c478bd9Sstevel@tonic-gate 	     || !(deny_file = getaccessfile(dir, name, prefix, DENYFILE))
101*7c478bd9Sstevel@tonic-gate 	     || !(*pdeny = _loadaccess(deny_file)) && errno != ENOENT
102*7c478bd9Sstevel@tonic-gate 	)
103*7c478bd9Sstevel@tonic-gate 		ret = -1;
104*7c478bd9Sstevel@tonic-gate 	else
105*7c478bd9Sstevel@tonic-gate 		ret = 0;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (allow_file)
108*7c478bd9Sstevel@tonic-gate 		Free (allow_file);
109*7c478bd9Sstevel@tonic-gate 	if (deny_file)
110*7c478bd9Sstevel@tonic-gate 		Free (deny_file);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	return (ret);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate /**
116*7c478bd9Sstevel@tonic-gate  ** _loadaccess() - LOAD ALLOW OR DENY FILE
117*7c478bd9Sstevel@tonic-gate  **/
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate static char **
_loadaccess(char * file)120*7c478bd9Sstevel@tonic-gate _loadaccess(char *file)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	register size_t		nalloc,
123*7c478bd9Sstevel@tonic-gate 				nlist;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	register char		**list;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	int fd;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(file, "r", 0)) < 0)
133*7c478bd9Sstevel@tonic-gate 		return (0);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	/*
136*7c478bd9Sstevel@tonic-gate 	 * Preallocate space for the initial list. We'll always
137*7c478bd9Sstevel@tonic-gate 	 * allocate one more than the list size, for the terminating null.
138*7c478bd9Sstevel@tonic-gate 	 */
139*7c478bd9Sstevel@tonic-gate 	nalloc = ACC_MAX_GUESS;
140*7c478bd9Sstevel@tonic-gate 	list = (char **)Malloc((nalloc + 1) * sizeof(char *));
141*7c478bd9Sstevel@tonic-gate 	if (!list) {
142*7c478bd9Sstevel@tonic-gate 		close(fd);
143*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
144*7c478bd9Sstevel@tonic-gate 		return (0);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	errno = 0;
148*7c478bd9Sstevel@tonic-gate 	for (nlist = 0; fdgets(buf, BUFSIZ, fd); ) {
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 		/*
153*7c478bd9Sstevel@tonic-gate 		 * Allocate more space if needed.
154*7c478bd9Sstevel@tonic-gate 		 */
155*7c478bd9Sstevel@tonic-gate 		if (nlist >= nalloc) {
156*7c478bd9Sstevel@tonic-gate 			nalloc += ACC_MAX_GUESS;
157*7c478bd9Sstevel@tonic-gate 			list = (char **)Realloc(
158*7c478bd9Sstevel@tonic-gate 				(char *)list,
159*7c478bd9Sstevel@tonic-gate 				(nalloc + 1) * sizeof(char *)
160*7c478bd9Sstevel@tonic-gate 			);
161*7c478bd9Sstevel@tonic-gate 			if (!list) {
162*7c478bd9Sstevel@tonic-gate 				close(fd);
163*7c478bd9Sstevel@tonic-gate 				return (0);
164*7c478bd9Sstevel@tonic-gate 			}
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 		list[nlist] = Strdup(buf);   /* if fail, minor problem */
168*7c478bd9Sstevel@tonic-gate 		list[++nlist] = 0;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate 	if (errno != 0) {
172*7c478bd9Sstevel@tonic-gate 		int			save_errno = errno;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		close(fd);
175*7c478bd9Sstevel@tonic-gate 		freelist (list);
176*7c478bd9Sstevel@tonic-gate 		errno = save_errno;
177*7c478bd9Sstevel@tonic-gate 		return (0);
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 	close(fd);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/*
182*7c478bd9Sstevel@tonic-gate 	 * If we have more space allocated than we need,
183*7c478bd9Sstevel@tonic-gate 	 * return the extra.
184*7c478bd9Sstevel@tonic-gate 	 */
185*7c478bd9Sstevel@tonic-gate 	if (nlist != nalloc) {
186*7c478bd9Sstevel@tonic-gate 		list = (char **)Realloc(
187*7c478bd9Sstevel@tonic-gate 			(char *)list,
188*7c478bd9Sstevel@tonic-gate 			(nlist + 1) * sizeof(char *)
189*7c478bd9Sstevel@tonic-gate 		);
190*7c478bd9Sstevel@tonic-gate 		if (!list) {
191*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
192*7c478bd9Sstevel@tonic-gate 			return (0);
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 	list[nlist] = 0;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	return (list);
198*7c478bd9Sstevel@tonic-gate }
199