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