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