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