xref: /illumos-gate/usr/src/lib/libpkg/common/ckparam.c (revision df23f1c1c0a6fa2a4bddd7a14cfc75933bbd5917)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2017 Peter Tribble.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
33 
34 
35 
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include "pkglib.h"
41 #include "pkglibmsgs.h"
42 #include "pkglocale.h"
43 
44 #define	MAXLEN 256
45 #define	TOKLEN 16
46 
47 static int	proc_name(char *param, char *value);
48 static int	proc_arch(char *param, char *value);
49 static int	proc_version(char *param, char *value);
50 static int	proc_category(char *param, char *value);
51 static int	bad_first_char(char *param, char *value);
52 static int	not_alnum(char *param, char *pt);
53 static int	not_ascii(char *param, char *pt);
54 static int	too_long(char *param, char *pt, int len);
55 static int	isnull(char *param, char *pt);
56 
57 int
58 ckparam(char *param, char *val)
59 {
60 	char *value = strdup(val);
61 	int ret_val = 0;	/* return value */
62 
63 	if (strcmp(param, "NAME") == 0)
64 		ret_val = proc_name(param, value);
65 
66 	else if (strcmp(param, "ARCH") == 0)
67 		ret_val = proc_arch(param, value);
68 
69 	else if (strcmp(param, "VERSION") == 0)
70 		ret_val = proc_version(param, value);
71 
72 	else if (strcmp(param, "CATEGORY") == 0)
73 		ret_val = proc_category(param, value);
74 
75 	/* param does not match existing parameters */
76 	free(value);
77 	return (ret_val);
78 }
79 
80 static int
81 proc_name(char *param, char *value)
82 {
83 	int ret_val;
84 
85 	if (!(ret_val = isnull(param, value))) {
86 		ret_val += too_long(param, value, MAXLEN);
87 		ret_val += not_ascii(param, value);
88 	}
89 
90 	return (ret_val);
91 }
92 
93 static int
94 proc_arch(char *param, char *value)
95 {
96 	int ret_val;
97 	char *token;
98 
99 	if (!(ret_val = isnull(param, value))) {
100 		token = strtok(value, ", ");
101 
102 		while (token) {
103 			ret_val += too_long(param, token, TOKLEN);
104 			ret_val += not_ascii(param, token);
105 			token = strtok(NULL, ", ");
106 		}
107 	}
108 
109 	return (ret_val);
110 }
111 
112 static int
113 proc_version(char *param, char *value)
114 {
115 	int ret_val;
116 
117 	if (!(ret_val = isnull(param, value))) {
118 		ret_val += bad_first_char(param, value);
119 		ret_val += too_long(param, value, MAXLEN);
120 		ret_val += not_ascii(param, value);
121 	}
122 
123 	return (ret_val);
124 }
125 
126 static int
127 proc_category(char *param, char *value)
128 {
129 	int ret_val;
130 	char *token;
131 
132 	if (!(ret_val = isnull(param, value))) {
133 		token = strtok(value, ", ");
134 
135 		while (token) {
136 			ret_val += too_long(param, token, TOKLEN);
137 			ret_val += not_alnum(param, token);
138 			token = strtok(NULL, ", ");
139 		}
140 	}
141 
142 	return (ret_val);
143 }
144 
145 static int
146 bad_first_char(char *param, char *value)
147 {
148 	if (*value == '(') {
149 		progerr(pkg_gt(ERR_CHAR), param);
150 		return (1);
151 	}
152 
153 	return (0);
154 }
155 
156 static int
157 isnull(char *param, char *pt)
158 {
159 	if (!pt || *pt == '\0') {
160 		progerr(pkg_gt(ERR_UNDEF), param);
161 		return (1);
162 	}
163 	return (0);
164 }
165 
166 static int
167 too_long(char *param, char *pt, int len)
168 {
169 	if (strlen(pt) > (size_t)len) {
170 		progerr(pkg_gt(ERR_LEN), param);
171 		return (1);
172 	}
173 	return (0);
174 }
175 
176 static int
177 not_ascii(char *param, char *pt)
178 {
179 	while (*pt) {
180 		if (!(isascii(*pt))) {
181 			progerr(pkg_gt(ERR_ASCII), param);
182 			return (1);
183 		}
184 		pt++;
185 	}
186 	return (0);
187 }
188 
189 static int
190 not_alnum(char *param, char *pt)
191 {
192 	while (*pt) {
193 		if (!(isalnum(*pt))) {
194 			progerr(pkg_gt(ERR_ALNUM), param);
195 			return (1);
196 		}
197 		pt++;
198 	}
199 
200 	return (0);
201 }
202