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