xref: /illumos-gate/usr/src/cmd/lp/lib/access/bang.c (revision 48bbca81)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
337c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "string.h"
367c478bd9Sstevel@tonic-gate #include "unistd.h"
377c478bd9Sstevel@tonic-gate #include "stdlib.h"
387c478bd9Sstevel@tonic-gate #include "sys/utsname.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "lp.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * The rules:
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *	Key:	A - some system
467c478bd9Sstevel@tonic-gate  *		X - some user
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  *	X	a user named X on the local system
497c478bd9Sstevel@tonic-gate  *	A!X	the user named X from the system A
507c478bd9Sstevel@tonic-gate  *	all!X	all users named X from any system
517c478bd9Sstevel@tonic-gate  *	all	all users from local system
527c478bd9Sstevel@tonic-gate  *	A!all	all users from the system A
537c478bd9Sstevel@tonic-gate  *	all!all	all users from any system
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /**
587c478bd9Sstevel@tonic-gate  ** bangequ() - LIKE STREQU, BUT HANDLES system!name CASES
597c478bd9Sstevel@tonic-gate  **/
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int
bangequ(char * user1p,char * user2p)627c478bd9Sstevel@tonic-gate bangequ (char *user1p, char *user2p)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	int	sysname1_all	= 0,
657c478bd9Sstevel@tonic-gate 		username1_all	= 0;
667c478bd9Sstevel@tonic-gate 	int	sysname2_all	= 0,
677c478bd9Sstevel@tonic-gate 		username2_all	= 0;
687c478bd9Sstevel@tonic-gate 	char	sysname1[BUFSIZ],
697c478bd9Sstevel@tonic-gate 		sysname2[BUFSIZ];
707c478bd9Sstevel@tonic-gate 	char	username1[BUFSIZ],
717c478bd9Sstevel@tonic-gate 		username2[BUFSIZ],
727c478bd9Sstevel@tonic-gate 		*sp;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	static	char *Nodenamep = (char *) 0;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (! user1p || ! user2p)
777c478bd9Sstevel@tonic-gate 		return	1;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (! Nodenamep) {
807c478bd9Sstevel@tonic-gate 		struct utsname	utsbuf;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		(void)	uname (&utsbuf);
837c478bd9Sstevel@tonic-gate 		Nodenamep = Strdup (utsbuf.nodename);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/* pattern=all */
877c478bd9Sstevel@tonic-gate 	if (STREQU (NAME_ALL, user2p) || STREQU(NAME_ALL, user1p))
887c478bd9Sstevel@tonic-gate 		return	1;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(user1p, '@')) != NULL) {	 /* user@host */
917c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
927c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", sp);
937c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
947c478bd9Sstevel@tonic-gate 		*--sp = '@';
957c478bd9Sstevel@tonic-gate 	} else if ((sp = strchr(user1p, '!')) != NULL) { /* host!user */
967c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
977c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", user1p);
987c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", sp);
997c478bd9Sstevel@tonic-gate 		*--sp = '!';
1007c478bd9Sstevel@tonic-gate 	} else {					 /* user */
1017c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", Nodenamep);
1027c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	sysname1_all = STREQU (NAME_ALL, sysname1);
1067c478bd9Sstevel@tonic-gate 	username1_all = STREQU (NAME_ALL, username1);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/* user2p is simple user name */
1097c478bd9Sstevel@tonic-gate 	if (strpbrk (user2p, "!@") == NULL)
1107c478bd9Sstevel@tonic-gate 		return	(username1_all && sysname1_all) ||
1117c478bd9Sstevel@tonic-gate 			 STREQU (username1, user2p);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(user2p, '@')) != NULL) {	 /* user@host */
1147c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
1157c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", sp);
1167c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user2p);
1177c478bd9Sstevel@tonic-gate 		*--sp = '@';
1187c478bd9Sstevel@tonic-gate 	} else if ((sp = strchr(user2p, '!')) != NULL) { /* host!user */
1197c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
1207c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", user2p);
1217c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", sp);
1227c478bd9Sstevel@tonic-gate 		*--sp = '!';
1237c478bd9Sstevel@tonic-gate 	} else {					 /* user */
1247c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", Nodenamep);
1257c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user1p);
1267c478bd9Sstevel@tonic-gate 	}
127*48bbca81SDaniel Hoffman 
1287c478bd9Sstevel@tonic-gate 	sysname2_all = STREQU (NAME_ALL, sysname2);
1297c478bd9Sstevel@tonic-gate 	username2_all = STREQU (NAME_ALL, username2);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((sysname1_all && username1_all) ||
1327c478bd9Sstevel@tonic-gate 	    (sysname2_all && username2_all) ||
1337c478bd9Sstevel@tonic-gate 	    (sysname1_all && username2_all) ||
1347c478bd9Sstevel@tonic-gate 	    (sysname2_all && username1_all))
1357c478bd9Sstevel@tonic-gate 		return 1;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (sysname1_all || sysname2_all)
1387c478bd9Sstevel@tonic-gate 		return	STREQU (username1, username2);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (username1_all || username2_all)
1417c478bd9Sstevel@tonic-gate 		return STREQU (sysname1, sysname2);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (STREQU (sysname1, sysname2) && STREQU (username1, username2))
1447c478bd9Sstevel@tonic-gate 		return 1;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return 0;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /**
1507c478bd9Sstevel@tonic-gate  ** bang_searchlist() - SEARCH (char **) LIST FOR "system!user" ITEM
1517c478bd9Sstevel@tonic-gate  **/
1527c478bd9Sstevel@tonic-gate int
bang_searchlist(char * item,char ** list)1537c478bd9Sstevel@tonic-gate bang_searchlist(char *item, char **list)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	if (!list || !*list)
1567c478bd9Sstevel@tonic-gate 		return (0);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * This is a linear search--we believe that the lists
1607c478bd9Sstevel@tonic-gate 	 * will be short.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	while (*list) {
1637c478bd9Sstevel@tonic-gate 		if (bangequ(item, *list))
1647c478bd9Sstevel@tonic-gate 			return (1);
1657c478bd9Sstevel@tonic-gate 		list++;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	return (0);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /**
1717c478bd9Sstevel@tonic-gate  ** bang_dellist() - REMOVE "system!name" ITEM FROM (char **) LIST
1727c478bd9Sstevel@tonic-gate  **/
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate int
bang_dellist(char *** plist,char * item)1757c478bd9Sstevel@tonic-gate bang_dellist(char ***plist, char *item)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	register char **	pl;
1787c478bd9Sstevel@tonic-gate 	register char **	ql;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	register int		n;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 				/*
1837c478bd9Sstevel@tonic-gate 				 * "hole" is a pointer guaranteed not
1847c478bd9Sstevel@tonic-gate 				 * to point to anyplace malloc'd.
1857c478bd9Sstevel@tonic-gate 				 */
1867c478bd9Sstevel@tonic-gate 	char *			hole	= "";
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * There are two ways this routine is different from the
1917c478bd9Sstevel@tonic-gate 	 * regular "dellist()" routine: First, the items are of the form
1927c478bd9Sstevel@tonic-gate 	 * ``system!name'', which means there is a two part matching
1937c478bd9Sstevel@tonic-gate 	 * for ``all'' cases (all systems and/or all names). Second,
1947c478bd9Sstevel@tonic-gate 	 * ALL matching items in the list are deleted.
1957c478bd9Sstevel@tonic-gate 	 *
1967c478bd9Sstevel@tonic-gate 	 * Now suppose the list contains just the word ``all'', and
197*48bbca81SDaniel Hoffman 	 * the item to be deleted is the name ``foo''. What will
1987c478bd9Sstevel@tonic-gate 	 * happen? The word ``all'' will be deleted, leaving the list
1997c478bd9Sstevel@tonic-gate 	 * empty (null)! This may sound odd at first, but keep in mind
2007c478bd9Sstevel@tonic-gate 	 * that this routine is paired with the regular "addlist()"
201*48bbca81SDaniel Hoffman 	 * routine; the item (``foo'') is ADDED to an opposite list
2027c478bd9Sstevel@tonic-gate 	 * (we are either deleting from a deny list and adding to an allow
2037c478bd9Sstevel@tonic-gate 	 * list or vice versa). So, to continue the example, if previously
204*48bbca81SDaniel Hoffman 	 * ``all'' were allowed, removing ``foo'' from the allow list
205*48bbca81SDaniel Hoffman 	 * does indeed empty that list, but then putting it in the deny
206*48bbca81SDaniel Hoffman 	 * list means only ``foo'' is denied, which is the effect we
2077c478bd9Sstevel@tonic-gate 	 * want.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (*plist) {
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		for (pl = *plist; *pl; pl++)
2137c478bd9Sstevel@tonic-gate 			if (bangequ(item, *pl)) {
2147c478bd9Sstevel@tonic-gate 				Free (*pl);
2157c478bd9Sstevel@tonic-gate 				*pl = hole;
2167c478bd9Sstevel@tonic-gate 			}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		for (n = 0, ql = pl = *plist; *pl; pl++)
2197c478bd9Sstevel@tonic-gate 			if (*pl != hole) {
2207c478bd9Sstevel@tonic-gate 				*ql++ = *pl;
2217c478bd9Sstevel@tonic-gate 				n++;
2227c478bd9Sstevel@tonic-gate 			}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		if (n == 0) {
2257c478bd9Sstevel@tonic-gate 			Free ((char *)*plist);
2267c478bd9Sstevel@tonic-gate 			*plist = 0;
2277c478bd9Sstevel@tonic-gate 		} else {
2287c478bd9Sstevel@tonic-gate 			*plist = (char **)Realloc(
2297c478bd9Sstevel@tonic-gate 				(char *)*plist,
2307c478bd9Sstevel@tonic-gate 				(n + 1) * sizeof(char *)
2317c478bd9Sstevel@tonic-gate 			);
2327c478bd9Sstevel@tonic-gate 			if (!*plist)
2337c478bd9Sstevel@tonic-gate 				return (-1);
2347c478bd9Sstevel@tonic-gate 			(*plist)[n] = 0;
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
240