xref: /illumos-gate/usr/src/cmd/lp/lib/access/change.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 1993 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 "errno.h"
33 #include "string.h"
34 #include "stdlib.h"
35 
36 #include "lp.h"
37 #include "access.h"
38 
39 static int		chgaccess ( int , char ** , char * , char * , char * );
40 static char **		empty_list ( void );
41 
42 /**
43  ** deny_user_form() - DENY USER ACCESS TO FORM
44  **/
45 
46 int
47 deny_user_form(char **user_list, char *form)
48 {
49 	return (chgaccess(0, user_list, form, Lp_A_Forms, ""));
50 }
51 
52 /**
53  ** allow_user_form() - ALLOW USER ACCESS TO FORM
54  **/
55 
56 int
57 allow_user_form(char **user_list, char *form)
58 {
59 	return (chgaccess(1, user_list, form, Lp_A_Forms, ""));
60 }
61 
62 /**
63  ** deny_user_printer() - DENY USER ACCESS TO PRINTER
64  **/
65 
66 int
67 deny_user_printer(char **user_list, char *printer)
68 {
69 	return (chgaccess(0, user_list, printer, Lp_A_Printers, UACCESSPREFIX));
70 }
71 
72 /**
73  ** allow_user_printer() - ALLOW USER ACCESS TO PRINTER
74  **/
75 
76 int
77 allow_user_printer(char **user_list, char *printer)
78 {
79 	return (chgaccess(1, user_list, printer, Lp_A_Printers, UACCESSPREFIX));
80 }
81 
82 /**
83  ** deny_form_printer() - DENY FORM USE ON PRINTER
84  **/
85 
86 int
87 deny_form_printer(char **form_list, char *printer)
88 {
89 	return (chgaccess(0, form_list, printer, Lp_A_Printers, FACCESSPREFIX));
90 }
91 
92 /**
93  ** allow_form_printer() - ALLOW FORM USE ON PRINTER
94  **/
95 
96 int
97 allow_form_printer(char **form_list, char *printer)
98 {
99 	return (chgaccess(1, form_list, printer, Lp_A_Printers, FACCESSPREFIX));
100 }
101 
102 /**
103  ** remove_paper_from_printer() - DENY FORM USE ON PRINTER
104  **/
105 
106 int
107 remove_paper_from_printer(char **form_list, char *printer)
108 {
109 	return (chgaccess(0, form_list, printer, Lp_A_Printers, PACCESSPREFIX));
110 }
111 
112 /**
113  ** add_paper_to_printer() - ALLOW FORM USE ON PRINTER
114  **/
115 
116 int
117 add_paper_to_printer(char **form_list, char *printer)
118 {
119 	return (chgaccess(1, form_list, printer, Lp_A_Printers, PACCESSPREFIX));
120 }
121 
122 /**
123  ** chgaccess() - UPDATE ALLOW/DENY ACCESS OF ITEM TO RESOURCE
124  **/
125 
126 static int
127 chgaccess(int isallow, char **list, char *name, char *dir, char *prefix)
128 {
129 	register char		***padd_list,
130 				***prem_list,
131 				**pl;
132 
133 	char			**allow_list,
134 				**deny_list;
135 
136 	if (loadaccess(dir, name, prefix, &allow_list, &deny_list) == -1)
137 		return (-1);
138 
139 	if (isallow) {
140 		padd_list = &allow_list;
141 		prem_list = &deny_list;
142 	} else {
143 		padd_list = &deny_list;
144 		prem_list = &allow_list;
145 	}
146 
147 	for (pl = list; *pl; pl++) {
148 
149 		/*
150 		 * Do the ``all'' and ``none'' cases explicitly,
151 		 * so that we can clean up the lists nicely.
152 		 */
153 		if (STREQU(*pl, NAME_NONE)) {
154 			isallow = !isallow;
155 			goto AllCase;
156 		}
157 		if (
158 			STREQU(*pl, NAME_ALL)
159 		     || STREQU(*pl, NAME_ANY)
160 		     || STREQU(*pl, ALL_BANG_ALL)
161 		) {
162 AllCase:
163 			freelist (allow_list);
164 			freelist (deny_list);
165 			if (isallow) {
166 				allow_list = 0;
167 				deny_list = empty_list();
168 			} else {
169 				allow_list = 0;
170 				deny_list = 0;
171 			}
172 			break;
173 
174 		} else {
175 
176 			/*
177 			 * For each regular item in the list,
178 			 * we add it to the ``add list'' and remove it
179 			 * from the ``remove list''. This is not
180 			 * efficient, especially if there are a lot of
181 			 * items in the caller's list; doing it the
182 			 * way we do, however, has the side effect
183 			 * of skipping duplicate names in the caller's
184 			 * list.
185 			 *
186 			 * Do a regular "addlist()"--the resulting
187 			 * list may have redundancies, but it will
188 			 * still be correct.
189 			 */
190 			if (addlist(padd_list, *pl) == -1)
191 				return (-1);
192 			if (bang_dellist(prem_list, *pl) == -1)
193 				return (-1);
194 
195 		}
196 
197 	}
198 
199 	return (dumpaccess(dir, name, prefix, &allow_list, &deny_list));
200 }
201 
202 /**
203  ** empty_list() - CREATE AN EMPTY LIST
204  **/
205 
206 static char **
207 empty_list(void)
208 {
209 	register char		**empty;
210 
211 
212 	if (!(empty = (char **)Malloc(sizeof(char *)))) {
213 		errno = ENOMEM;
214 		return (0);
215 	}
216 	*empty = 0;
217 	return (empty);
218 }
219