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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
287c478bd9Sstevel@tonic-gate  * All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*9ab815e1SGarrett D'Amore #include <note.h>
377c478bd9Sstevel@tonic-gate #include "libadm.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static char	*rsvrd[] = {
407c478bd9Sstevel@tonic-gate 	"all",
417c478bd9Sstevel@tonic-gate 	"install",
427c478bd9Sstevel@tonic-gate 	"new",
437c478bd9Sstevel@tonic-gate 	NULL
447c478bd9Sstevel@tonic-gate };
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	NMBRK	".*"
477c478bd9Sstevel@tonic-gate #define	WILD1	".*"
487c478bd9Sstevel@tonic-gate #define	WILD2	"*"
497c478bd9Sstevel@tonic-gate #define	ABI_NAMELNGTH		9
507c478bd9Sstevel@tonic-gate #define	NON_ABI_NAMELNGTH	32
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int abi_namelngth = 0;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int
valname(char * pkg,int wild)55*9ab815e1SGarrett D'Amore valname(char *pkg, int wild)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	int	count, i, n;
587c478bd9Sstevel@tonic-gate 	char	*pt;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	/* wild == 1	allow wildcard specification as a name */
617c478bd9Sstevel@tonic-gate 	if (wild && (strcmp(pkg, "all") == 0))
627c478bd9Sstevel@tonic-gate 		return (0);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* check for reserved package names */
657c478bd9Sstevel@tonic-gate 	for (i = 0; rsvrd[i]; i++) {
667c478bd9Sstevel@tonic-gate 		n = (int)strlen(rsvrd[i]);
677c478bd9Sstevel@tonic-gate 		if ((strncmp(pkg, rsvrd[i], n) == 0) &&
687c478bd9Sstevel@tonic-gate 		    (!pkg[n] || strchr(NMBRK, pkg[n])))
697c478bd9Sstevel@tonic-gate 			return (1);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/*
73*9ab815e1SGarrett D'Amore 	 * check for valid extensions; we used to do this
74*9ab815e1SGarrett D'Amore 	 * first since we needed to look for SVR3 ".name"
757c478bd9Sstevel@tonic-gate 	 * before we validate the package abbreviation
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	if (pt = strpbrk(pkg, NMBRK)) {
78*9ab815e1SGarrett D'Amore 		if ((strcmp(pt, WILD1) == 0) || (strcmp(pt, WILD2) == 0)) {
797c478bd9Sstevel@tonic-gate 			/* wildcard specification */
807c478bd9Sstevel@tonic-gate 			if (!wild)
817c478bd9Sstevel@tonic-gate 				return (1);
827c478bd9Sstevel@tonic-gate 		} else {
837c478bd9Sstevel@tonic-gate 			count = 0;
847c478bd9Sstevel@tonic-gate 			while (*++pt) {
857c478bd9Sstevel@tonic-gate 				count++;
867c478bd9Sstevel@tonic-gate 				if (!isalpha((unsigned char)*pt) &&
877c478bd9Sstevel@tonic-gate 					!isdigit((unsigned char)*pt) &&
887c478bd9Sstevel@tonic-gate 				    !strpbrk(pt, "-+"))
897c478bd9Sstevel@tonic-gate 					return (-1);
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 			if (!count || (count > 4))
927c478bd9Sstevel@tonic-gate 				return (-1);
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/* check for valid package name */
977c478bd9Sstevel@tonic-gate 	count = 0;
98*9ab815e1SGarrett D'Amore 	if (!isalpha((unsigned char)*pkg))
997c478bd9Sstevel@tonic-gate 		return (-1);
1007c478bd9Sstevel@tonic-gate 	while (*pkg && !strchr(NMBRK, *pkg)) {
1017c478bd9Sstevel@tonic-gate 		if (!isalnum((unsigned char)*pkg) && !strpbrk(pkg, "-+"))
1027c478bd9Sstevel@tonic-gate 			return (-1);
1037c478bd9Sstevel@tonic-gate 		count++, pkg++;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/* Check for ABI package name length */
1077c478bd9Sstevel@tonic-gate 	if (get_ABI_namelngth() == 1) {
1087c478bd9Sstevel@tonic-gate 		if (count > ABI_NAMELNGTH)
1097c478bd9Sstevel@tonic-gate 			return (-1);
1107c478bd9Sstevel@tonic-gate 	} else if (count > NON_ABI_NAMELNGTH)
1117c478bd9Sstevel@tonic-gate 			return (-1);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (0); /* pkg is valid */
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* presvr4flg - check for pre-svr4 package names also ? */
1177c478bd9Sstevel@tonic-gate int
pkgnmchk(char * pkg,char * spec,int presvr4flg)1187c478bd9Sstevel@tonic-gate pkgnmchk(char *pkg, char *spec, int presvr4flg)
1197c478bd9Sstevel@tonic-gate {
120*9ab815e1SGarrett D'Amore 	_NOTE(ARGUNUSED(presvr4flg));
121*9ab815e1SGarrett D'Amore 
1227c478bd9Sstevel@tonic-gate 	/* pkg is assumed to be non-NULL upon entry */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * this routine reacts based on the value passed in spec:
1267c478bd9Sstevel@tonic-gate 	 * 	NULL	pkg must be valid and may be a wildcard spec
1277c478bd9Sstevel@tonic-gate 	 *	"all"	pkg must be valid and must be an instance
1287c478bd9Sstevel@tonic-gate 	 *	"x.*"	pkg must be valid and must be an instance of "x"
1297c478bd9Sstevel@tonic-gate 	 *	"x*"	pkg must be valid and must be an instance of "x"
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 
132*9ab815e1SGarrett D'Amore 	if (valname(pkg, ((spec == NULL) ? 1 : 0)))
1337c478bd9Sstevel@tonic-gate 		return (1); /* invalid or reserved name */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if ((spec == NULL) || (strcmp(spec, "all") == 0))
1367c478bd9Sstevel@tonic-gate 		return (0);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	while (*pkg == *spec) {
139*9ab815e1SGarrett D'Amore 		if ((strcmp(spec, WILD1) == 0) || (strcmp(spec, WILD2) == 0))
1407c478bd9Sstevel@tonic-gate 			break; /* wildcard spec, so stop right here */
1417c478bd9Sstevel@tonic-gate 		else if (*pkg++ == '\0')
1427c478bd9Sstevel@tonic-gate 			return (0); /* identical match */
1437c478bd9Sstevel@tonic-gate 		spec++;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
146*9ab815e1SGarrett D'Amore 	if ((strcmp(spec, WILD1) == 0) || (strcmp(spec, WILD2) == 0))
1477c478bd9Sstevel@tonic-gate 		if ((pkg[0] == '\0') || (pkg[0] == '.'))
1487c478bd9Sstevel@tonic-gate 			return (0);
1497c478bd9Sstevel@tonic-gate 	return (1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate void
set_ABI_namelngth(void)1537c478bd9Sstevel@tonic-gate set_ABI_namelngth(void)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	abi_namelngth = 1;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate int
get_ABI_namelngth(void)1597c478bd9Sstevel@tonic-gate get_ABI_namelngth(void)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	return (abi_namelngth);
1627c478bd9Sstevel@tonic-gate }
163