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