xref: /titanic_51/usr/src/lib/libadm/common/pkgnmchk.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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
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 /*LINTLIBRARY*/
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <ctype.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include "libadm.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate static char	*rsvrd[] = {
40*7c478bd9Sstevel@tonic-gate 	"all",
41*7c478bd9Sstevel@tonic-gate 	"install",
42*7c478bd9Sstevel@tonic-gate 	"new",
43*7c478bd9Sstevel@tonic-gate 	NULL
44*7c478bd9Sstevel@tonic-gate };
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	NMBRK	".*"
47*7c478bd9Sstevel@tonic-gate #define	WILD1	".*"
48*7c478bd9Sstevel@tonic-gate #define	WILD2	"*"
49*7c478bd9Sstevel@tonic-gate #define	WILD3	".name"
50*7c478bd9Sstevel@tonic-gate #define	ABI_NAMELNGTH		9
51*7c478bd9Sstevel@tonic-gate #define	NON_ABI_NAMELNGTH	32
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static int abi_namelngth = 0;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static int
56*7c478bd9Sstevel@tonic-gate valname(char *pkg, int wild, int presvr4flg)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	int	count, i, n;
59*7c478bd9Sstevel@tonic-gate 	char	*pt;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	/* wild == 1	allow wildcard specification as a name */
62*7c478bd9Sstevel@tonic-gate 	if (wild && (strcmp(pkg, "all") == 0))
63*7c478bd9Sstevel@tonic-gate 		return (0);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/* check for reserved package names */
66*7c478bd9Sstevel@tonic-gate 	for (i = 0; rsvrd[i]; i++) {
67*7c478bd9Sstevel@tonic-gate 		n = (int)strlen(rsvrd[i]);
68*7c478bd9Sstevel@tonic-gate 		if ((strncmp(pkg, rsvrd[i], n) == 0) &&
69*7c478bd9Sstevel@tonic-gate 		    (!pkg[n] || strchr(NMBRK, pkg[n])))
70*7c478bd9Sstevel@tonic-gate 			return (1);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	 * check for valid extensions; we must do this
75*7c478bd9Sstevel@tonic-gate 	 * first since we need to look for SVR3 ".name"
76*7c478bd9Sstevel@tonic-gate 	 * before we validate the package abbreviation
77*7c478bd9Sstevel@tonic-gate 	 */
78*7c478bd9Sstevel@tonic-gate 	if (pt = strpbrk(pkg, NMBRK)) {
79*7c478bd9Sstevel@tonic-gate 		if (presvr4flg && (strcmp(pt, WILD3) == 0))
80*7c478bd9Sstevel@tonic-gate 			return (0); /* SVR3 packages have no validation */
81*7c478bd9Sstevel@tonic-gate 		else if ((strcmp(pt, WILD1) == 0) || (strcmp(pt, WILD2) == 0)) {
82*7c478bd9Sstevel@tonic-gate 			/* wildcard specification */
83*7c478bd9Sstevel@tonic-gate 			if (!wild)
84*7c478bd9Sstevel@tonic-gate 				return (1);
85*7c478bd9Sstevel@tonic-gate 		} else {
86*7c478bd9Sstevel@tonic-gate 			count = 0;
87*7c478bd9Sstevel@tonic-gate 			while (*++pt) {
88*7c478bd9Sstevel@tonic-gate 				count++;
89*7c478bd9Sstevel@tonic-gate 				if (!isalpha((unsigned char)*pt) &&
90*7c478bd9Sstevel@tonic-gate 					!isdigit((unsigned char)*pt) &&
91*7c478bd9Sstevel@tonic-gate 				    !strpbrk(pt, "-+"))
92*7c478bd9Sstevel@tonic-gate 					return (-1);
93*7c478bd9Sstevel@tonic-gate 			}
94*7c478bd9Sstevel@tonic-gate 			if (!count || (count > 4))
95*7c478bd9Sstevel@tonic-gate 				return (-1);
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/* check for valid package name */
100*7c478bd9Sstevel@tonic-gate 	count = 0;
101*7c478bd9Sstevel@tonic-gate 	if (!isalnum((unsigned char)*pkg) ||
102*7c478bd9Sstevel@tonic-gate 		(!presvr4flg && !isalpha((unsigned char)*pkg)))
103*7c478bd9Sstevel@tonic-gate 		return (-1);
104*7c478bd9Sstevel@tonic-gate 	while (*pkg && !strchr(NMBRK, *pkg)) {
105*7c478bd9Sstevel@tonic-gate 		if (!isalnum((unsigned char)*pkg) && !strpbrk(pkg, "-+"))
106*7c478bd9Sstevel@tonic-gate 			return (-1);
107*7c478bd9Sstevel@tonic-gate 		count++, pkg++;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/* Check for ABI package name length */
111*7c478bd9Sstevel@tonic-gate 	if (get_ABI_namelngth() == 1) {
112*7c478bd9Sstevel@tonic-gate 		if (count > ABI_NAMELNGTH)
113*7c478bd9Sstevel@tonic-gate 			return (-1);
114*7c478bd9Sstevel@tonic-gate 	} else if (count > NON_ABI_NAMELNGTH)
115*7c478bd9Sstevel@tonic-gate 			return (-1);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	return (0); /* pkg is valid */
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /* presvr4flg - check for pre-svr4 package names also ? */
121*7c478bd9Sstevel@tonic-gate int
122*7c478bd9Sstevel@tonic-gate pkgnmchk(char *pkg, char *spec, int presvr4flg)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	/* pkg is assumed to be non-NULL upon entry */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	/*
127*7c478bd9Sstevel@tonic-gate 	 * this routine reacts based on the value passed in spec:
128*7c478bd9Sstevel@tonic-gate 	 * 	NULL	pkg must be valid and may be a wildcard spec
129*7c478bd9Sstevel@tonic-gate 	 *	"all"	pkg must be valid and must be an instance
130*7c478bd9Sstevel@tonic-gate 	 *	"x.*"	pkg must be valid and must be an instance of "x"
131*7c478bd9Sstevel@tonic-gate 	 *	"x*"	pkg must be valid and must be an instance of "x"
132*7c478bd9Sstevel@tonic-gate 	 */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if (valname(pkg, ((spec == NULL) ? 1 : 0), presvr4flg))
135*7c478bd9Sstevel@tonic-gate 		return (1); /* invalid or reserved name */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if ((spec == NULL) || (strcmp(spec, "all") == 0))
138*7c478bd9Sstevel@tonic-gate 		return (0);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	while (*pkg == *spec) {
141*7c478bd9Sstevel@tonic-gate 		if ((strcmp(spec, WILD1) == 0) || (strcmp(spec, WILD2) == 0) ||
142*7c478bd9Sstevel@tonic-gate 		(strcmp(spec, WILD3) == 0))
143*7c478bd9Sstevel@tonic-gate 			break; /* wildcard spec, so stop right here */
144*7c478bd9Sstevel@tonic-gate 		else if (*pkg++ == '\0')
145*7c478bd9Sstevel@tonic-gate 			return (0); /* identical match */
146*7c478bd9Sstevel@tonic-gate 		spec++;
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if ((strcmp(spec, WILD1) == 0) || (strcmp(spec, WILD2) == 0) ||
150*7c478bd9Sstevel@tonic-gate 	    (strcmp(spec, WILD3) == 0)) {
151*7c478bd9Sstevel@tonic-gate 		if ((pkg[0] == '\0') || (pkg[0] == '.'))
152*7c478bd9Sstevel@tonic-gate 			return (0);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	if ((spec[0] == '\0') && (strcmp(pkg, WILD3) == 0))
155*7c478bd9Sstevel@tonic-gate 		return (0); /* compare pkg.name to pkg */
156*7c478bd9Sstevel@tonic-gate 	return (1);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate void
160*7c478bd9Sstevel@tonic-gate set_ABI_namelngth(void)
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	abi_namelngth = 1;
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate int
166*7c478bd9Sstevel@tonic-gate get_ABI_namelngth(void)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	return (abi_namelngth);
169*7c478bd9Sstevel@tonic-gate }
170