xref: /onnv-gate/usr/src/lib/libadm/common/pkgnmchk.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"    /* SVr4.0 1.2 */
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include "libadm.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate static char	*rsvrd[] = {
40*0Sstevel@tonic-gate 	"all",
41*0Sstevel@tonic-gate 	"install",
42*0Sstevel@tonic-gate 	"new",
43*0Sstevel@tonic-gate 	NULL
44*0Sstevel@tonic-gate };
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	NMBRK	".*"
47*0Sstevel@tonic-gate #define	WILD1	".*"
48*0Sstevel@tonic-gate #define	WILD2	"*"
49*0Sstevel@tonic-gate #define	WILD3	".name"
50*0Sstevel@tonic-gate #define	ABI_NAMELNGTH		9
51*0Sstevel@tonic-gate #define	NON_ABI_NAMELNGTH	32
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static int abi_namelngth = 0;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static int
valname(char * pkg,int wild,int presvr4flg)56*0Sstevel@tonic-gate valname(char *pkg, int wild, int presvr4flg)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	int	count, i, n;
59*0Sstevel@tonic-gate 	char	*pt;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	/* wild == 1	allow wildcard specification as a name */
62*0Sstevel@tonic-gate 	if (wild && (strcmp(pkg, "all") == 0))
63*0Sstevel@tonic-gate 		return (0);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	/* check for reserved package names */
66*0Sstevel@tonic-gate 	for (i = 0; rsvrd[i]; i++) {
67*0Sstevel@tonic-gate 		n = (int)strlen(rsvrd[i]);
68*0Sstevel@tonic-gate 		if ((strncmp(pkg, rsvrd[i], n) == 0) &&
69*0Sstevel@tonic-gate 		    (!pkg[n] || strchr(NMBRK, pkg[n])))
70*0Sstevel@tonic-gate 			return (1);
71*0Sstevel@tonic-gate 	}
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/*
74*0Sstevel@tonic-gate 	 * check for valid extensions; we must do this
75*0Sstevel@tonic-gate 	 * first since we need to look for SVR3 ".name"
76*0Sstevel@tonic-gate 	 * before we validate the package abbreviation
77*0Sstevel@tonic-gate 	 */
78*0Sstevel@tonic-gate 	if (pt = strpbrk(pkg, NMBRK)) {
79*0Sstevel@tonic-gate 		if (presvr4flg && (strcmp(pt, WILD3) == 0))
80*0Sstevel@tonic-gate 			return (0); /* SVR3 packages have no validation */
81*0Sstevel@tonic-gate 		else if ((strcmp(pt, WILD1) == 0) || (strcmp(pt, WILD2) == 0)) {
82*0Sstevel@tonic-gate 			/* wildcard specification */
83*0Sstevel@tonic-gate 			if (!wild)
84*0Sstevel@tonic-gate 				return (1);
85*0Sstevel@tonic-gate 		} else {
86*0Sstevel@tonic-gate 			count = 0;
87*0Sstevel@tonic-gate 			while (*++pt) {
88*0Sstevel@tonic-gate 				count++;
89*0Sstevel@tonic-gate 				if (!isalpha((unsigned char)*pt) &&
90*0Sstevel@tonic-gate 					!isdigit((unsigned char)*pt) &&
91*0Sstevel@tonic-gate 				    !strpbrk(pt, "-+"))
92*0Sstevel@tonic-gate 					return (-1);
93*0Sstevel@tonic-gate 			}
94*0Sstevel@tonic-gate 			if (!count || (count > 4))
95*0Sstevel@tonic-gate 				return (-1);
96*0Sstevel@tonic-gate 		}
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/* check for valid package name */
100*0Sstevel@tonic-gate 	count = 0;
101*0Sstevel@tonic-gate 	if (!isalnum((unsigned char)*pkg) ||
102*0Sstevel@tonic-gate 		(!presvr4flg && !isalpha((unsigned char)*pkg)))
103*0Sstevel@tonic-gate 		return (-1);
104*0Sstevel@tonic-gate 	while (*pkg && !strchr(NMBRK, *pkg)) {
105*0Sstevel@tonic-gate 		if (!isalnum((unsigned char)*pkg) && !strpbrk(pkg, "-+"))
106*0Sstevel@tonic-gate 			return (-1);
107*0Sstevel@tonic-gate 		count++, pkg++;
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/* Check for ABI package name length */
111*0Sstevel@tonic-gate 	if (get_ABI_namelngth() == 1) {
112*0Sstevel@tonic-gate 		if (count > ABI_NAMELNGTH)
113*0Sstevel@tonic-gate 			return (-1);
114*0Sstevel@tonic-gate 	} else if (count > NON_ABI_NAMELNGTH)
115*0Sstevel@tonic-gate 			return (-1);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (0); /* pkg is valid */
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /* presvr4flg - check for pre-svr4 package names also ? */
121*0Sstevel@tonic-gate int
pkgnmchk(char * pkg,char * spec,int presvr4flg)122*0Sstevel@tonic-gate pkgnmchk(char *pkg, char *spec, int presvr4flg)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	/* pkg is assumed to be non-NULL upon entry */
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * this routine reacts based on the value passed in spec:
128*0Sstevel@tonic-gate 	 * 	NULL	pkg must be valid and may be a wildcard spec
129*0Sstevel@tonic-gate 	 *	"all"	pkg must be valid and must be an instance
130*0Sstevel@tonic-gate 	 *	"x.*"	pkg must be valid and must be an instance of "x"
131*0Sstevel@tonic-gate 	 *	"x*"	pkg must be valid and must be an instance of "x"
132*0Sstevel@tonic-gate 	 */
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (valname(pkg, ((spec == NULL) ? 1 : 0), presvr4flg))
135*0Sstevel@tonic-gate 		return (1); /* invalid or reserved name */
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	if ((spec == NULL) || (strcmp(spec, "all") == 0))
138*0Sstevel@tonic-gate 		return (0);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	while (*pkg == *spec) {
141*0Sstevel@tonic-gate 		if ((strcmp(spec, WILD1) == 0) || (strcmp(spec, WILD2) == 0) ||
142*0Sstevel@tonic-gate 		(strcmp(spec, WILD3) == 0))
143*0Sstevel@tonic-gate 			break; /* wildcard spec, so stop right here */
144*0Sstevel@tonic-gate 		else if (*pkg++ == '\0')
145*0Sstevel@tonic-gate 			return (0); /* identical match */
146*0Sstevel@tonic-gate 		spec++;
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if ((strcmp(spec, WILD1) == 0) || (strcmp(spec, WILD2) == 0) ||
150*0Sstevel@tonic-gate 	    (strcmp(spec, WILD3) == 0)) {
151*0Sstevel@tonic-gate 		if ((pkg[0] == '\0') || (pkg[0] == '.'))
152*0Sstevel@tonic-gate 			return (0);
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 	if ((spec[0] == '\0') && (strcmp(pkg, WILD3) == 0))
155*0Sstevel@tonic-gate 		return (0); /* compare pkg.name to pkg */
156*0Sstevel@tonic-gate 	return (1);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate void
set_ABI_namelngth(void)160*0Sstevel@tonic-gate set_ABI_namelngth(void)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	abi_namelngth = 1;
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate int
get_ABI_namelngth(void)166*0Sstevel@tonic-gate get_ABI_namelngth(void)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	return (abi_namelngth);
169*0Sstevel@tonic-gate }
170