xref: /titanic_50/usr/src/cmd/lp/lib/access/bang.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
32*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "string.h"
35*7c478bd9Sstevel@tonic-gate #include "unistd.h"
36*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
37*7c478bd9Sstevel@tonic-gate #include "sys/utsname.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "lp.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * The rules:
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  *	Key:	A - some system
45*7c478bd9Sstevel@tonic-gate  *		X - some user
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  *	X	a user named X on the local system
48*7c478bd9Sstevel@tonic-gate  *	A!X	the user named X from the system A
49*7c478bd9Sstevel@tonic-gate  *	all!X	all users named X from any system
50*7c478bd9Sstevel@tonic-gate  *	all	all users from local system
51*7c478bd9Sstevel@tonic-gate  *	A!all	all users from the system A
52*7c478bd9Sstevel@tonic-gate  *	all!all	all users from any system
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /**
57*7c478bd9Sstevel@tonic-gate  ** bangequ() - LIKE STREQU, BUT HANDLES system!name CASES
58*7c478bd9Sstevel@tonic-gate  **/
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate int
bangequ(char * user1p,char * user2p)61*7c478bd9Sstevel@tonic-gate bangequ (char *user1p, char *user2p)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	int	sysname1_all	= 0,
64*7c478bd9Sstevel@tonic-gate 		username1_all	= 0;
65*7c478bd9Sstevel@tonic-gate 	int	sysname2_all	= 0,
66*7c478bd9Sstevel@tonic-gate 		username2_all	= 0;
67*7c478bd9Sstevel@tonic-gate 	char	sysname1[BUFSIZ],
68*7c478bd9Sstevel@tonic-gate 		sysname2[BUFSIZ];
69*7c478bd9Sstevel@tonic-gate 	char	username1[BUFSIZ],
70*7c478bd9Sstevel@tonic-gate 		username2[BUFSIZ],
71*7c478bd9Sstevel@tonic-gate 		*sp;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	static	char *Nodenamep = (char *) 0;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (! user1p || ! user2p)
76*7c478bd9Sstevel@tonic-gate 		return	1;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if (! Nodenamep) {
79*7c478bd9Sstevel@tonic-gate 		struct utsname	utsbuf;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 		(void)	uname (&utsbuf);
82*7c478bd9Sstevel@tonic-gate 		Nodenamep = Strdup (utsbuf.nodename);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	/* pattern=all */
86*7c478bd9Sstevel@tonic-gate 	if (STREQU (NAME_ALL, user2p) || STREQU(NAME_ALL, user1p))
87*7c478bd9Sstevel@tonic-gate 		return	1;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(user1p, '@')) != NULL) {	 /* user@host */
90*7c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
91*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", sp);
92*7c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
93*7c478bd9Sstevel@tonic-gate 		*--sp = '@';
94*7c478bd9Sstevel@tonic-gate 	} else if ((sp = strchr(user1p, '!')) != NULL) { /* host!user */
95*7c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
96*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", user1p);
97*7c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", sp);
98*7c478bd9Sstevel@tonic-gate 		*--sp = '!';
99*7c478bd9Sstevel@tonic-gate 	} else {					 /* user */
100*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", Nodenamep);
101*7c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	sysname1_all = STREQU (NAME_ALL, sysname1);
105*7c478bd9Sstevel@tonic-gate 	username1_all = STREQU (NAME_ALL, username1);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	/* user2p is simple user name */
108*7c478bd9Sstevel@tonic-gate 	if (strpbrk (user2p, "!@") == NULL)
109*7c478bd9Sstevel@tonic-gate 		return	(username1_all && sysname1_all) ||
110*7c478bd9Sstevel@tonic-gate 			 STREQU (username1, user2p);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(user2p, '@')) != NULL) {	 /* user@host */
113*7c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
114*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", sp);
115*7c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user2p);
116*7c478bd9Sstevel@tonic-gate 		*--sp = '@';
117*7c478bd9Sstevel@tonic-gate 	} else if ((sp = strchr(user2p, '!')) != NULL) { /* host!user */
118*7c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
119*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", user2p);
120*7c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", sp);
121*7c478bd9Sstevel@tonic-gate 		*--sp = '!';
122*7c478bd9Sstevel@tonic-gate 	} else {					 /* user */
123*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", Nodenamep);
124*7c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user1p);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	sysname2_all = STREQU (NAME_ALL, sysname2);
128*7c478bd9Sstevel@tonic-gate 	username2_all = STREQU (NAME_ALL, username2);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if ((sysname1_all && username1_all) ||
131*7c478bd9Sstevel@tonic-gate 	    (sysname2_all && username2_all) ||
132*7c478bd9Sstevel@tonic-gate 	    (sysname1_all && username2_all) ||
133*7c478bd9Sstevel@tonic-gate 	    (sysname2_all && username1_all))
134*7c478bd9Sstevel@tonic-gate 		return 1;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	if (sysname1_all || sysname2_all)
137*7c478bd9Sstevel@tonic-gate 		return	STREQU (username1, username2);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if (username1_all || username2_all)
140*7c478bd9Sstevel@tonic-gate 		return STREQU (sysname1, sysname2);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (STREQU (sysname1, sysname2) && STREQU (username1, username2))
143*7c478bd9Sstevel@tonic-gate 		return 1;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	return 0;
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate /**
149*7c478bd9Sstevel@tonic-gate  ** bang_searchlist() - SEARCH (char **) LIST FOR "system!user" ITEM
150*7c478bd9Sstevel@tonic-gate  **/
151*7c478bd9Sstevel@tonic-gate int
bang_searchlist(char * item,char ** list)152*7c478bd9Sstevel@tonic-gate bang_searchlist(char *item, char **list)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	if (!list || !*list)
155*7c478bd9Sstevel@tonic-gate 		return (0);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	/*
158*7c478bd9Sstevel@tonic-gate 	 * This is a linear search--we believe that the lists
159*7c478bd9Sstevel@tonic-gate 	 * will be short.
160*7c478bd9Sstevel@tonic-gate 	 */
161*7c478bd9Sstevel@tonic-gate 	while (*list) {
162*7c478bd9Sstevel@tonic-gate 		if (bangequ(item, *list))
163*7c478bd9Sstevel@tonic-gate 			return (1);
164*7c478bd9Sstevel@tonic-gate 		list++;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 	return (0);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /**
170*7c478bd9Sstevel@tonic-gate  ** bang_dellist() - REMOVE "system!name" ITEM FROM (char **) LIST
171*7c478bd9Sstevel@tonic-gate  **/
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate int
bang_dellist(char *** plist,char * item)174*7c478bd9Sstevel@tonic-gate bang_dellist(char ***plist, char *item)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	register char **	pl;
177*7c478bd9Sstevel@tonic-gate 	register char **	ql;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	register int		n;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 				/*
182*7c478bd9Sstevel@tonic-gate 				 * "hole" is a pointer guaranteed not
183*7c478bd9Sstevel@tonic-gate 				 * to point to anyplace malloc'd.
184*7c478bd9Sstevel@tonic-gate 				 */
185*7c478bd9Sstevel@tonic-gate 	char *			hole	= "";
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/*
189*7c478bd9Sstevel@tonic-gate 	 * There are two ways this routine is different from the
190*7c478bd9Sstevel@tonic-gate 	 * regular "dellist()" routine: First, the items are of the form
191*7c478bd9Sstevel@tonic-gate 	 * ``system!name'', which means there is a two part matching
192*7c478bd9Sstevel@tonic-gate 	 * for ``all'' cases (all systems and/or all names). Second,
193*7c478bd9Sstevel@tonic-gate 	 * ALL matching items in the list are deleted.
194*7c478bd9Sstevel@tonic-gate 	 *
195*7c478bd9Sstevel@tonic-gate 	 * Now suppose the list contains just the word ``all'', and
196*7c478bd9Sstevel@tonic-gate 	 * the item to be deleted is the name ``fred''. What will
197*7c478bd9Sstevel@tonic-gate 	 * happen? The word ``all'' will be deleted, leaving the list
198*7c478bd9Sstevel@tonic-gate 	 * empty (null)! This may sound odd at first, but keep in mind
199*7c478bd9Sstevel@tonic-gate 	 * that this routine is paired with the regular "addlist()"
200*7c478bd9Sstevel@tonic-gate 	 * routine; the item (``fred'') is ADDED to an opposite list
201*7c478bd9Sstevel@tonic-gate 	 * (we are either deleting from a deny list and adding to an allow
202*7c478bd9Sstevel@tonic-gate 	 * list or vice versa). So, to continue the example, if previously
203*7c478bd9Sstevel@tonic-gate 	 * ``all'' were allowed, removing ``fred'' from the allow list
204*7c478bd9Sstevel@tonic-gate 	 * does indeed empty that list, but then putting him in the deny
205*7c478bd9Sstevel@tonic-gate 	 * list means only ``fred'' is denied, which is the effect we
206*7c478bd9Sstevel@tonic-gate 	 * want.
207*7c478bd9Sstevel@tonic-gate 	 */
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	if (*plist) {
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 		for (pl = *plist; *pl; pl++)
212*7c478bd9Sstevel@tonic-gate 			if (bangequ(item, *pl)) {
213*7c478bd9Sstevel@tonic-gate 				Free (*pl);
214*7c478bd9Sstevel@tonic-gate 				*pl = hole;
215*7c478bd9Sstevel@tonic-gate 			}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 		for (n = 0, ql = pl = *plist; *pl; pl++)
218*7c478bd9Sstevel@tonic-gate 			if (*pl != hole) {
219*7c478bd9Sstevel@tonic-gate 				*ql++ = *pl;
220*7c478bd9Sstevel@tonic-gate 				n++;
221*7c478bd9Sstevel@tonic-gate 			}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		if (n == 0) {
224*7c478bd9Sstevel@tonic-gate 			Free ((char *)*plist);
225*7c478bd9Sstevel@tonic-gate 			*plist = 0;
226*7c478bd9Sstevel@tonic-gate 		} else {
227*7c478bd9Sstevel@tonic-gate 			*plist = (char **)Realloc(
228*7c478bd9Sstevel@tonic-gate 				(char *)*plist,
229*7c478bd9Sstevel@tonic-gate 				(n + 1) * sizeof(char *)
230*7c478bd9Sstevel@tonic-gate 			);
231*7c478bd9Sstevel@tonic-gate 			if (!*plist)
232*7c478bd9Sstevel@tonic-gate 				return (-1);
233*7c478bd9Sstevel@tonic-gate 			(*plist)[n] = 0;
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	return (0);
238*7c478bd9Sstevel@tonic-gate }
239