xref: /illumos-gate/usr/src/cmd/lp/lib/access/bang.c (revision 2a8bcb4e)
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 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "string.h"
347c478bd9Sstevel@tonic-gate #include "unistd.h"
357c478bd9Sstevel@tonic-gate #include "stdlib.h"
367c478bd9Sstevel@tonic-gate #include "sys/utsname.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "lp.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * The rules:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *	Key:	A - some system
447c478bd9Sstevel@tonic-gate  *		X - some user
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *	X	a user named X on the local system
477c478bd9Sstevel@tonic-gate  *	A!X	the user named X from the system A
487c478bd9Sstevel@tonic-gate  *	all!X	all users named X from any system
497c478bd9Sstevel@tonic-gate  *	all	all users from local system
507c478bd9Sstevel@tonic-gate  *	A!all	all users from the system A
517c478bd9Sstevel@tonic-gate  *	all!all	all users from any system
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /**
567c478bd9Sstevel@tonic-gate  ** bangequ() - LIKE STREQU, BUT HANDLES system!name CASES
577c478bd9Sstevel@tonic-gate  **/
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int
bangequ(char * user1p,char * user2p)607c478bd9Sstevel@tonic-gate bangequ (char *user1p, char *user2p)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	int	sysname1_all	= 0,
637c478bd9Sstevel@tonic-gate 		username1_all	= 0;
647c478bd9Sstevel@tonic-gate 	int	sysname2_all	= 0,
657c478bd9Sstevel@tonic-gate 		username2_all	= 0;
667c478bd9Sstevel@tonic-gate 	char	sysname1[BUFSIZ],
677c478bd9Sstevel@tonic-gate 		sysname2[BUFSIZ];
687c478bd9Sstevel@tonic-gate 	char	username1[BUFSIZ],
697c478bd9Sstevel@tonic-gate 		username2[BUFSIZ],
707c478bd9Sstevel@tonic-gate 		*sp;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	static	char *Nodenamep = (char *) 0;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (! user1p || ! user2p)
757c478bd9Sstevel@tonic-gate 		return	1;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (! Nodenamep) {
787c478bd9Sstevel@tonic-gate 		struct utsname	utsbuf;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		(void)	uname (&utsbuf);
817c478bd9Sstevel@tonic-gate 		Nodenamep = Strdup (utsbuf.nodename);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/* pattern=all */
857c478bd9Sstevel@tonic-gate 	if (STREQU (NAME_ALL, user2p) || STREQU(NAME_ALL, user1p))
867c478bd9Sstevel@tonic-gate 		return	1;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(user1p, '@')) != NULL) {	 /* user@host */
897c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
907c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", sp);
917c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
927c478bd9Sstevel@tonic-gate 		*--sp = '@';
937c478bd9Sstevel@tonic-gate 	} else if ((sp = strchr(user1p, '!')) != NULL) { /* host!user */
947c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
957c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", user1p);
967c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", sp);
977c478bd9Sstevel@tonic-gate 		*--sp = '!';
987c478bd9Sstevel@tonic-gate 	} else {					 /* user */
997c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname1, sizeof (sysname1), "%s", Nodenamep);
1007c478bd9Sstevel@tonic-gate 		(void) snprintf(username1, sizeof (username1), "%s", user1p);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	sysname1_all = STREQU (NAME_ALL, sysname1);
1047c478bd9Sstevel@tonic-gate 	username1_all = STREQU (NAME_ALL, username1);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/* user2p is simple user name */
1077c478bd9Sstevel@tonic-gate 	if (strpbrk (user2p, "!@") == NULL)
1087c478bd9Sstevel@tonic-gate 		return	(username1_all && sysname1_all) ||
1097c478bd9Sstevel@tonic-gate 			 STREQU (username1, user2p);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(user2p, '@')) != NULL) {	 /* user@host */
1127c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
1137c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", sp);
1147c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user2p);
1157c478bd9Sstevel@tonic-gate 		*--sp = '@';
1167c478bd9Sstevel@tonic-gate 	} else if ((sp = strchr(user2p, '!')) != NULL) { /* host!user */
1177c478bd9Sstevel@tonic-gate 		*sp++ = '\0';
1187c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", user2p);
1197c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", sp);
1207c478bd9Sstevel@tonic-gate 		*--sp = '!';
1217c478bd9Sstevel@tonic-gate 	} else {					 /* user */
1227c478bd9Sstevel@tonic-gate 		(void) snprintf(sysname2, sizeof (sysname2), "%s", Nodenamep);
1237c478bd9Sstevel@tonic-gate 		(void) snprintf(username2, sizeof (username2), "%s", user1p);
1247c478bd9Sstevel@tonic-gate 	}
125*48bbca81SDaniel Hoffman 
1267c478bd9Sstevel@tonic-gate 	sysname2_all = STREQU (NAME_ALL, sysname2);
1277c478bd9Sstevel@tonic-gate 	username2_all = STREQU (NAME_ALL, username2);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((sysname1_all && username1_all) ||
1307c478bd9Sstevel@tonic-gate 	    (sysname2_all && username2_all) ||
1317c478bd9Sstevel@tonic-gate 	    (sysname1_all && username2_all) ||
1327c478bd9Sstevel@tonic-gate 	    (sysname2_all && username1_all))
1337c478bd9Sstevel@tonic-gate 		return 1;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (sysname1_all || sysname2_all)
1367c478bd9Sstevel@tonic-gate 		return	STREQU (username1, username2);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (username1_all || username2_all)
1397c478bd9Sstevel@tonic-gate 		return STREQU (sysname1, sysname2);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (STREQU (sysname1, sysname2) && STREQU (username1, username2))
1427c478bd9Sstevel@tonic-gate 		return 1;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return 0;
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /**
1487c478bd9Sstevel@tonic-gate  ** bang_searchlist() - SEARCH (char **) LIST FOR "system!user" ITEM
1497c478bd9Sstevel@tonic-gate  **/
1507c478bd9Sstevel@tonic-gate int
bang_searchlist(char * item,char ** list)1517c478bd9Sstevel@tonic-gate bang_searchlist(char *item, char **list)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	if (!list || !*list)
1547c478bd9Sstevel@tonic-gate 		return (0);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * This is a linear search--we believe that the lists
1587c478bd9Sstevel@tonic-gate 	 * will be short.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	while (*list) {
1617c478bd9Sstevel@tonic-gate 		if (bangequ(item, *list))
1627c478bd9Sstevel@tonic-gate 			return (1);
1637c478bd9Sstevel@tonic-gate 		list++;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	return (0);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /**
1697c478bd9Sstevel@tonic-gate  ** bang_dellist() - REMOVE "system!name" ITEM FROM (char **) LIST
1707c478bd9Sstevel@tonic-gate  **/
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate int
bang_dellist(char *** plist,char * item)1737c478bd9Sstevel@tonic-gate bang_dellist(char ***plist, char *item)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	register char **	pl;
1767c478bd9Sstevel@tonic-gate 	register char **	ql;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	register int		n;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 				/*
1817c478bd9Sstevel@tonic-gate 				 * "hole" is a pointer guaranteed not
1827c478bd9Sstevel@tonic-gate 				 * to point to anyplace malloc'd.
1837c478bd9Sstevel@tonic-gate 				 */
1847c478bd9Sstevel@tonic-gate 	char *			hole	= "";
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * There are two ways this routine is different from the
1897c478bd9Sstevel@tonic-gate 	 * regular "dellist()" routine: First, the items are of the form
1907c478bd9Sstevel@tonic-gate 	 * ``system!name'', which means there is a two part matching
1917c478bd9Sstevel@tonic-gate 	 * for ``all'' cases (all systems and/or all names). Second,
1927c478bd9Sstevel@tonic-gate 	 * ALL matching items in the list are deleted.
1937c478bd9Sstevel@tonic-gate 	 *
1947c478bd9Sstevel@tonic-gate 	 * Now suppose the list contains just the word ``all'', and
195*48bbca81SDaniel Hoffman 	 * the item to be deleted is the name ``foo''. What will
1967c478bd9Sstevel@tonic-gate 	 * happen? The word ``all'' will be deleted, leaving the list
1977c478bd9Sstevel@tonic-gate 	 * empty (null)! This may sound odd at first, but keep in mind
1987c478bd9Sstevel@tonic-gate 	 * that this routine is paired with the regular "addlist()"
199*48bbca81SDaniel Hoffman 	 * routine; the item (``foo'') is ADDED to an opposite list
2007c478bd9Sstevel@tonic-gate 	 * (we are either deleting from a deny list and adding to an allow
2017c478bd9Sstevel@tonic-gate 	 * list or vice versa). So, to continue the example, if previously
202*48bbca81SDaniel Hoffman 	 * ``all'' were allowed, removing ``foo'' from the allow list
203*48bbca81SDaniel Hoffman 	 * does indeed empty that list, but then putting it in the deny
204*48bbca81SDaniel Hoffman 	 * list means only ``foo'' is denied, which is the effect we
2057c478bd9Sstevel@tonic-gate 	 * want.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (*plist) {
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		for (pl = *plist; *pl; pl++)
2117c478bd9Sstevel@tonic-gate 			if (bangequ(item, *pl)) {
2127c478bd9Sstevel@tonic-gate 				Free (*pl);
2137c478bd9Sstevel@tonic-gate 				*pl = hole;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		for (n = 0, ql = pl = *plist; *pl; pl++)
2177c478bd9Sstevel@tonic-gate 			if (*pl != hole) {
2187c478bd9Sstevel@tonic-gate 				*ql++ = *pl;
2197c478bd9Sstevel@tonic-gate 				n++;
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		if (n == 0) {
2237c478bd9Sstevel@tonic-gate 			Free ((char *)*plist);
2247c478bd9Sstevel@tonic-gate 			*plist = 0;
2257c478bd9Sstevel@tonic-gate 		} else {
2267c478bd9Sstevel@tonic-gate 			*plist = (char **)Realloc(
2277c478bd9Sstevel@tonic-gate 				(char *)*plist,
2287c478bd9Sstevel@tonic-gate 				(n + 1) * sizeof(char *)
2297c478bd9Sstevel@tonic-gate 			);
2307c478bd9Sstevel@tonic-gate 			if (!*plist)
2317c478bd9Sstevel@tonic-gate 				return (-1);
2327c478bd9Sstevel@tonic-gate 			(*plist)[n] = 0;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (0);
2377c478bd9Sstevel@tonic-gate }
238