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