xref: /onnv-gate/usr/src/cmd/lp/lib/access/bang.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 1999 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.2	*/
32*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "string.h"
35*0Sstevel@tonic-gate #include "unistd.h"
36*0Sstevel@tonic-gate #include "stdlib.h"
37*0Sstevel@tonic-gate #include "sys/utsname.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "lp.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * The rules:
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  *	Key:	A - some system
45*0Sstevel@tonic-gate  *		X - some user
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  *	X	a user named X on the local system
48*0Sstevel@tonic-gate  *	A!X	the user named X from the system A
49*0Sstevel@tonic-gate  *	all!X	all users named X from any system
50*0Sstevel@tonic-gate  *	all	all users from local system
51*0Sstevel@tonic-gate  *	A!all	all users from the system A
52*0Sstevel@tonic-gate  *	all!all	all users from any system
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /**
57*0Sstevel@tonic-gate  ** bangequ() - LIKE STREQU, BUT HANDLES system!name CASES
58*0Sstevel@tonic-gate  **/
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate int
bangequ(char * user1p,char * user2p)61*0Sstevel@tonic-gate bangequ (char *user1p, char *user2p)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	int	sysname1_all	= 0,
64*0Sstevel@tonic-gate 		username1_all	= 0;
65*0Sstevel@tonic-gate 	int	sysname2_all	= 0,
66*0Sstevel@tonic-gate 		username2_all	= 0;
67*0Sstevel@tonic-gate 	char	sysname1[BUFSIZ],
68*0Sstevel@tonic-gate 		sysname2[BUFSIZ];
69*0Sstevel@tonic-gate 	char	username1[BUFSIZ],
70*0Sstevel@tonic-gate 		username2[BUFSIZ],
71*0Sstevel@tonic-gate 		*sp;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	static	char *Nodenamep = (char *) 0;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (! user1p || ! user2p)
76*0Sstevel@tonic-gate 		return	1;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (! Nodenamep) {
79*0Sstevel@tonic-gate 		struct utsname	utsbuf;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		(void)	uname (&utsbuf);
82*0Sstevel@tonic-gate 		Nodenamep = Strdup (utsbuf.nodename);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/* pattern=all */
86*0Sstevel@tonic-gate 	if (STREQU (NAME_ALL, user2p) || STREQU(NAME_ALL, user1p))
87*0Sstevel@tonic-gate 		return	1;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if ((sp = strrchr(user1p, '@')) != NULL) {	 /* user@host */
90*0Sstevel@tonic-gate 		*sp++ = '\0';
91*0Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", sp);
92*0Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
93*0Sstevel@tonic-gate 		*--sp = '@';
94*0Sstevel@tonic-gate 	} else if ((sp = strchr(user1p, '!')) != NULL) { /* host!user */
95*0Sstevel@tonic-gate 		*sp++ = '\0';
96*0Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", user1p);
97*0Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", sp);
98*0Sstevel@tonic-gate 		*--sp = '!';
99*0Sstevel@tonic-gate 	} else {					 /* user */
100*0Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", Nodenamep);
101*0Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	sysname1_all = STREQU (NAME_ALL, sysname1);
105*0Sstevel@tonic-gate 	username1_all = STREQU (NAME_ALL, username1);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	/* user2p is simple user name */
108*0Sstevel@tonic-gate 	if (strpbrk (user2p, "!@") == NULL)
109*0Sstevel@tonic-gate 		return	(username1_all && sysname1_all) ||
110*0Sstevel@tonic-gate 			 STREQU (username1, user2p);
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	if ((sp = strrchr(user2p, '@')) != NULL) {	 /* user@host */
113*0Sstevel@tonic-gate 		*sp++ = '\0';
114*0Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", sp);
115*0Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user2p);
116*0Sstevel@tonic-gate 		*--sp = '@';
117*0Sstevel@tonic-gate 	} else if ((sp = strchr(user2p, '!')) != NULL) { /* host!user */
118*0Sstevel@tonic-gate 		*sp++ = '\0';
119*0Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", user2p);
120*0Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", sp);
121*0Sstevel@tonic-gate 		*--sp = '!';
122*0Sstevel@tonic-gate 	} else {					 /* user */
123*0Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", Nodenamep);
124*0Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user1p);
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	sysname2_all = STREQU (NAME_ALL, sysname2);
128*0Sstevel@tonic-gate 	username2_all = STREQU (NAME_ALL, username2);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if ((sysname1_all && username1_all) ||
131*0Sstevel@tonic-gate 	    (sysname2_all && username2_all) ||
132*0Sstevel@tonic-gate 	    (sysname1_all && username2_all) ||
133*0Sstevel@tonic-gate 	    (sysname2_all && username1_all))
134*0Sstevel@tonic-gate 		return 1;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (sysname1_all || sysname2_all)
137*0Sstevel@tonic-gate 		return	STREQU (username1, username2);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (username1_all || username2_all)
140*0Sstevel@tonic-gate 		return STREQU (sysname1, sysname2);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	if (STREQU (sysname1, sysname2) && STREQU (username1, username2))
143*0Sstevel@tonic-gate 		return 1;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	return 0;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate /**
149*0Sstevel@tonic-gate  ** bang_searchlist() - SEARCH (char **) LIST FOR "system!user" ITEM
150*0Sstevel@tonic-gate  **/
151*0Sstevel@tonic-gate int
bang_searchlist(char * item,char ** list)152*0Sstevel@tonic-gate bang_searchlist(char *item, char **list)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	if (!list || !*list)
155*0Sstevel@tonic-gate 		return (0);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	/*
158*0Sstevel@tonic-gate 	 * This is a linear search--we believe that the lists
159*0Sstevel@tonic-gate 	 * will be short.
160*0Sstevel@tonic-gate 	 */
161*0Sstevel@tonic-gate 	while (*list) {
162*0Sstevel@tonic-gate 		if (bangequ(item, *list))
163*0Sstevel@tonic-gate 			return (1);
164*0Sstevel@tonic-gate 		list++;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 	return (0);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate /**
170*0Sstevel@tonic-gate  ** bang_dellist() - REMOVE "system!name" ITEM FROM (char **) LIST
171*0Sstevel@tonic-gate  **/
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate int
bang_dellist(char *** plist,char * item)174*0Sstevel@tonic-gate bang_dellist(char ***plist, char *item)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	register char **	pl;
177*0Sstevel@tonic-gate 	register char **	ql;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	register int		n;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 				/*
182*0Sstevel@tonic-gate 				 * "hole" is a pointer guaranteed not
183*0Sstevel@tonic-gate 				 * to point to anyplace malloc'd.
184*0Sstevel@tonic-gate 				 */
185*0Sstevel@tonic-gate 	char *			hole	= "";
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/*
189*0Sstevel@tonic-gate 	 * There are two ways this routine is different from the
190*0Sstevel@tonic-gate 	 * regular "dellist()" routine: First, the items are of the form
191*0Sstevel@tonic-gate 	 * ``system!name'', which means there is a two part matching
192*0Sstevel@tonic-gate 	 * for ``all'' cases (all systems and/or all names). Second,
193*0Sstevel@tonic-gate 	 * ALL matching items in the list are deleted.
194*0Sstevel@tonic-gate 	 *
195*0Sstevel@tonic-gate 	 * Now suppose the list contains just the word ``all'', and
196*0Sstevel@tonic-gate 	 * the item to be deleted is the name ``fred''. What will
197*0Sstevel@tonic-gate 	 * happen? The word ``all'' will be deleted, leaving the list
198*0Sstevel@tonic-gate 	 * empty (null)! This may sound odd at first, but keep in mind
199*0Sstevel@tonic-gate 	 * that this routine is paired with the regular "addlist()"
200*0Sstevel@tonic-gate 	 * routine; the item (``fred'') is ADDED to an opposite list
201*0Sstevel@tonic-gate 	 * (we are either deleting from a deny list and adding to an allow
202*0Sstevel@tonic-gate 	 * list or vice versa). So, to continue the example, if previously
203*0Sstevel@tonic-gate 	 * ``all'' were allowed, removing ``fred'' from the allow list
204*0Sstevel@tonic-gate 	 * does indeed empty that list, but then putting him in the deny
205*0Sstevel@tonic-gate 	 * list means only ``fred'' is denied, which is the effect we
206*0Sstevel@tonic-gate 	 * want.
207*0Sstevel@tonic-gate 	 */
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	if (*plist) {
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		for (pl = *plist; *pl; pl++)
212*0Sstevel@tonic-gate 			if (bangequ(item, *pl)) {
213*0Sstevel@tonic-gate 				Free (*pl);
214*0Sstevel@tonic-gate 				*pl = hole;
215*0Sstevel@tonic-gate 			}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 		for (n = 0, ql = pl = *plist; *pl; pl++)
218*0Sstevel@tonic-gate 			if (*pl != hole) {
219*0Sstevel@tonic-gate 				*ql++ = *pl;
220*0Sstevel@tonic-gate 				n++;
221*0Sstevel@tonic-gate 			}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		if (n == 0) {
224*0Sstevel@tonic-gate 			Free ((char *)*plist);
225*0Sstevel@tonic-gate 			*plist = 0;
226*0Sstevel@tonic-gate 		} else {
227*0Sstevel@tonic-gate 			*plist = (char **)Realloc(
228*0Sstevel@tonic-gate 				(char *)*plist,
229*0Sstevel@tonic-gate 				(n + 1) * sizeof(char *)
230*0Sstevel@tonic-gate 			);
231*0Sstevel@tonic-gate 			if (!*plist)
232*0Sstevel@tonic-gate 				return (-1);
233*0Sstevel@tonic-gate 			(*plist)[n] = 0;
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	return (0);
238*0Sstevel@tonic-gate }
239