xref: /illumos-gate/usr/src/cmd/lp/lib/access/loadaccess.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
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 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9	*/
32*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "stdio.h"
35*7c478bd9Sstevel@tonic-gate #include "errno.h"
36*7c478bd9Sstevel@tonic-gate #include "string.h"
37*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "lp.h"
40*7c478bd9Sstevel@tonic-gate #include "access.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static char		**_loadaccess ( char * );
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /**
45*7c478bd9Sstevel@tonic-gate  ** load_userform_access() - LOAD ALLOW/DENY LISTS FOR USER+FORM
46*7c478bd9Sstevel@tonic-gate  **/
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int
49*7c478bd9Sstevel@tonic-gate load_userform_access(char *form, char ***pallow, char ***pdeny)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Forms, form, "", pallow, pdeny));
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /**
55*7c478bd9Sstevel@tonic-gate  ** load_userprinter_access() - LOAD ALLOW/DENY LISTS FOR USER+PRINTER
56*7c478bd9Sstevel@tonic-gate  **/
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate int
59*7c478bd9Sstevel@tonic-gate load_userprinter_access(char *printer, char ***pallow, char ***pdeny)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Printers, printer, UACCESSPREFIX, pallow,
62*7c478bd9Sstevel@tonic-gate 		pdeny));
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /**
66*7c478bd9Sstevel@tonic-gate  ** load_formprinter_access() - LOAD ALLOW/DENY LISTS FOR FORM+PRINTER
67*7c478bd9Sstevel@tonic-gate  **/
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate int
70*7c478bd9Sstevel@tonic-gate load_formprinter_access(char *printer, char ***pallow, char ***pdeny)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Printers, printer, FACCESSPREFIX, pallow,
73*7c478bd9Sstevel@tonic-gate 		pdeny));
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /**
77*7c478bd9Sstevel@tonic-gate  ** load_paperprinter_access() - LOAD ALLOW/DENY LISTS FOR FORM+PRINTER
78*7c478bd9Sstevel@tonic-gate  **/
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate int
81*7c478bd9Sstevel@tonic-gate load_paperprinter_access(char *printer, char ***pallow, char ***pdeny)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	return (loadaccess(Lp_A_Printers, printer, PACCESSPREFIX, pallow,
84*7c478bd9Sstevel@tonic-gate 		pdeny));
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /**
88*7c478bd9Sstevel@tonic-gate  ** loadaccess() - LOAD ALLOW OR DENY LISTS
89*7c478bd9Sstevel@tonic-gate  **/
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate int
92*7c478bd9Sstevel@tonic-gate loadaccess(char *dir, char *name, char *prefix, char ***pallow, char ***pdeny)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	register char		*allow_file	= 0,
95*7c478bd9Sstevel@tonic-gate 				*deny_file	= 0;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	int			ret;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if (
100*7c478bd9Sstevel@tonic-gate 		!(allow_file = getaccessfile(dir, name, prefix, ALLOWFILE))
101*7c478bd9Sstevel@tonic-gate 	     || !(*pallow = _loadaccess(allow_file)) && errno != ENOENT
102*7c478bd9Sstevel@tonic-gate 	     || !(deny_file = getaccessfile(dir, name, prefix, DENYFILE))
103*7c478bd9Sstevel@tonic-gate 	     || !(*pdeny = _loadaccess(deny_file)) && errno != ENOENT
104*7c478bd9Sstevel@tonic-gate 	)
105*7c478bd9Sstevel@tonic-gate 		ret = -1;
106*7c478bd9Sstevel@tonic-gate 	else
107*7c478bd9Sstevel@tonic-gate 		ret = 0;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (allow_file)
110*7c478bd9Sstevel@tonic-gate 		Free (allow_file);
111*7c478bd9Sstevel@tonic-gate 	if (deny_file)
112*7c478bd9Sstevel@tonic-gate 		Free (deny_file);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	return (ret);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /**
118*7c478bd9Sstevel@tonic-gate  ** _loadaccess() - LOAD ALLOW OR DENY FILE
119*7c478bd9Sstevel@tonic-gate  **/
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate static char **
122*7c478bd9Sstevel@tonic-gate _loadaccess(char *file)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	register size_t		nalloc,
125*7c478bd9Sstevel@tonic-gate 				nlist;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	register char		**list;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	int fd;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(file, "r", 0)) < 0)
135*7c478bd9Sstevel@tonic-gate 		return (0);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/*
138*7c478bd9Sstevel@tonic-gate 	 * Preallocate space for the initial list. We'll always
139*7c478bd9Sstevel@tonic-gate 	 * allocate one more than the list size, for the terminating null.
140*7c478bd9Sstevel@tonic-gate 	 */
141*7c478bd9Sstevel@tonic-gate 	nalloc = ACC_MAX_GUESS;
142*7c478bd9Sstevel@tonic-gate 	list = (char **)Malloc((nalloc + 1) * sizeof(char *));
143*7c478bd9Sstevel@tonic-gate 	if (!list) {
144*7c478bd9Sstevel@tonic-gate 		close(fd);
145*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
146*7c478bd9Sstevel@tonic-gate 		return (0);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	errno = 0;
150*7c478bd9Sstevel@tonic-gate 	for (nlist = 0; fdgets(buf, BUFSIZ, fd); ) {
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 		/*
155*7c478bd9Sstevel@tonic-gate 		 * Allocate more space if needed.
156*7c478bd9Sstevel@tonic-gate 		 */
157*7c478bd9Sstevel@tonic-gate 		if (nlist >= nalloc) {
158*7c478bd9Sstevel@tonic-gate 			nalloc += ACC_MAX_GUESS;
159*7c478bd9Sstevel@tonic-gate 			list = (char **)Realloc(
160*7c478bd9Sstevel@tonic-gate 				(char *)list,
161*7c478bd9Sstevel@tonic-gate 				(nalloc + 1) * sizeof(char *)
162*7c478bd9Sstevel@tonic-gate 			);
163*7c478bd9Sstevel@tonic-gate 			if (!list) {
164*7c478bd9Sstevel@tonic-gate 				close(fd);
165*7c478bd9Sstevel@tonic-gate 				return (0);
166*7c478bd9Sstevel@tonic-gate 			}
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		list[nlist] = Strdup(buf);   /* if fail, minor problem */
170*7c478bd9Sstevel@tonic-gate 		list[++nlist] = 0;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	if (errno != 0) {
174*7c478bd9Sstevel@tonic-gate 		int			save_errno = errno;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 		close(fd);
177*7c478bd9Sstevel@tonic-gate 		freelist (list);
178*7c478bd9Sstevel@tonic-gate 		errno = save_errno;
179*7c478bd9Sstevel@tonic-gate 		return (0);
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 	close(fd);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	/*
184*7c478bd9Sstevel@tonic-gate 	 * If we have more space allocated than we need,
185*7c478bd9Sstevel@tonic-gate 	 * return the extra.
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	if (nlist != nalloc) {
188*7c478bd9Sstevel@tonic-gate 		list = (char **)Realloc(
189*7c478bd9Sstevel@tonic-gate 			(char *)list,
190*7c478bd9Sstevel@tonic-gate 			(nlist + 1) * sizeof(char *)
191*7c478bd9Sstevel@tonic-gate 		);
192*7c478bd9Sstevel@tonic-gate 		if (!list) {
193*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
194*7c478bd9Sstevel@tonic-gate 			return (0);
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 	list[nlist] = 0;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	return (list);
200*7c478bd9Sstevel@tonic-gate }
201