xref: /illumos-gate/usr/src/lib/libpkg/common/ckparam.c (revision 32991bed)
15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland  * CDDL HEADER START
35c51f124SMoriah Waterland  *
45c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland  *
85c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland  * See the License for the specific language governing permissions
115c51f124SMoriah Waterland  * and limitations under the License.
125c51f124SMoriah Waterland  *
135c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland  *
195c51f124SMoriah Waterland  * CDDL HEADER END
205c51f124SMoriah Waterland  */
215c51f124SMoriah Waterland 
22*32991bedSPeter Tribble /*
23*32991bedSPeter Tribble  * Copyright (c) 2017 Peter Tribble.
24*32991bedSPeter Tribble  */
25*32991bedSPeter Tribble 
265c51f124SMoriah Waterland /*
275c51f124SMoriah Waterland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
285c51f124SMoriah Waterland  * Use is subject to license terms.
295c51f124SMoriah Waterland  */
305c51f124SMoriah Waterland 
315c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
325c51f124SMoriah Waterland /* All Rights Reserved */
335c51f124SMoriah Waterland 
345c51f124SMoriah Waterland 
355c51f124SMoriah Waterland 
365c51f124SMoriah Waterland #include <ctype.h>
375c51f124SMoriah Waterland #include <string.h>
38*32991bedSPeter Tribble #include <stdlib.h>
395c51f124SMoriah Waterland #include <sys/types.h>
405c51f124SMoriah Waterland #include "pkglib.h"
415c51f124SMoriah Waterland #include "pkglibmsgs.h"
425c51f124SMoriah Waterland #include "pkglocale.h"
435c51f124SMoriah Waterland 
445c51f124SMoriah Waterland #define	MAXLEN 256
455c51f124SMoriah Waterland #define	TOKLEN 16
465c51f124SMoriah Waterland 
475c51f124SMoriah Waterland static int	proc_name(char *param, char *value);
485c51f124SMoriah Waterland static int	proc_arch(char *param, char *value);
495c51f124SMoriah Waterland static int	proc_version(char *param, char *value);
505c51f124SMoriah Waterland static int	proc_category(char *param, char *value);
515c51f124SMoriah Waterland static int	bad_first_char(char *param, char *value);
525c51f124SMoriah Waterland static int	not_alnum(char *param, char *pt);
535c51f124SMoriah Waterland static int	not_ascii(char *param, char *pt);
545c51f124SMoriah Waterland static int	too_long(char *param, char *pt, int len);
555c51f124SMoriah Waterland static int	isnull(char *param, char *pt);
565c51f124SMoriah Waterland 
575c51f124SMoriah Waterland int
ckparam(char * param,char * val)585c51f124SMoriah Waterland ckparam(char *param, char *val)
595c51f124SMoriah Waterland {
605c51f124SMoriah Waterland 	char *value = strdup(val);
615c51f124SMoriah Waterland 	int ret_val = 0;	/* return value */
625c51f124SMoriah Waterland 
635c51f124SMoriah Waterland 	if (strcmp(param, "NAME") == 0)
645c51f124SMoriah Waterland 		ret_val = proc_name(param, value);
655c51f124SMoriah Waterland 
665c51f124SMoriah Waterland 	else if (strcmp(param, "ARCH") == 0)
675c51f124SMoriah Waterland 		ret_val = proc_arch(param, value);
685c51f124SMoriah Waterland 
695c51f124SMoriah Waterland 	else if (strcmp(param, "VERSION") == 0)
705c51f124SMoriah Waterland 		ret_val = proc_version(param, value);
715c51f124SMoriah Waterland 
725c51f124SMoriah Waterland 	else if (strcmp(param, "CATEGORY") == 0)
735c51f124SMoriah Waterland 		ret_val = proc_category(param, value);
745c51f124SMoriah Waterland 
755c51f124SMoriah Waterland 	/* param does not match existing parameters */
765c51f124SMoriah Waterland 	free(value);
775c51f124SMoriah Waterland 	return (ret_val);
785c51f124SMoriah Waterland }
795c51f124SMoriah Waterland 
805c51f124SMoriah Waterland static int
proc_name(char * param,char * value)815c51f124SMoriah Waterland proc_name(char *param, char *value)
825c51f124SMoriah Waterland {
835c51f124SMoriah Waterland 	int ret_val;
845c51f124SMoriah Waterland 
855c51f124SMoriah Waterland 	if (!(ret_val = isnull(param, value))) {
865c51f124SMoriah Waterland 		ret_val += too_long(param, value, MAXLEN);
875c51f124SMoriah Waterland 		ret_val += not_ascii(param, value);
885c51f124SMoriah Waterland 	}
895c51f124SMoriah Waterland 
905c51f124SMoriah Waterland 	return (ret_val);
915c51f124SMoriah Waterland }
925c51f124SMoriah Waterland 
935c51f124SMoriah Waterland static int
proc_arch(char * param,char * value)945c51f124SMoriah Waterland proc_arch(char *param, char *value)
955c51f124SMoriah Waterland {
965c51f124SMoriah Waterland 	int ret_val;
975c51f124SMoriah Waterland 	char *token;
985c51f124SMoriah Waterland 
995c51f124SMoriah Waterland 	if (!(ret_val = isnull(param, value))) {
1005c51f124SMoriah Waterland 		token = strtok(value, ", ");
1015c51f124SMoriah Waterland 
1025c51f124SMoriah Waterland 		while (token) {
1035c51f124SMoriah Waterland 			ret_val += too_long(param, token, TOKLEN);
1045c51f124SMoriah Waterland 			ret_val += not_ascii(param, token);
1055c51f124SMoriah Waterland 			token = strtok(NULL, ", ");
1065c51f124SMoriah Waterland 		}
1075c51f124SMoriah Waterland 	}
1085c51f124SMoriah Waterland 
1095c51f124SMoriah Waterland 	return (ret_val);
1105c51f124SMoriah Waterland }
1115c51f124SMoriah Waterland 
1125c51f124SMoriah Waterland static int
proc_version(char * param,char * value)1135c51f124SMoriah Waterland proc_version(char *param, char *value)
1145c51f124SMoriah Waterland {
1155c51f124SMoriah Waterland 	int ret_val;
1165c51f124SMoriah Waterland 
1175c51f124SMoriah Waterland 	if (!(ret_val = isnull(param, value))) {
1185c51f124SMoriah Waterland 		ret_val += bad_first_char(param, value);
1195c51f124SMoriah Waterland 		ret_val += too_long(param, value, MAXLEN);
1205c51f124SMoriah Waterland 		ret_val += not_ascii(param, value);
1215c51f124SMoriah Waterland 	}
1225c51f124SMoriah Waterland 
1235c51f124SMoriah Waterland 	return (ret_val);
1245c51f124SMoriah Waterland }
1255c51f124SMoriah Waterland 
1265c51f124SMoriah Waterland static int
proc_category(char * param,char * value)1275c51f124SMoriah Waterland proc_category(char *param, char *value)
1285c51f124SMoriah Waterland {
1295c51f124SMoriah Waterland 	int ret_val;
1305c51f124SMoriah Waterland 	char *token;
1315c51f124SMoriah Waterland 
1325c51f124SMoriah Waterland 	if (!(ret_val = isnull(param, value))) {
1335c51f124SMoriah Waterland 		token = strtok(value, ", ");
1345c51f124SMoriah Waterland 
1355c51f124SMoriah Waterland 		while (token) {
1365c51f124SMoriah Waterland 			ret_val += too_long(param, token, TOKLEN);
1375c51f124SMoriah Waterland 			ret_val += not_alnum(param, token);
1385c51f124SMoriah Waterland 			token = strtok(NULL, ", ");
1395c51f124SMoriah Waterland 		}
1405c51f124SMoriah Waterland 	}
1415c51f124SMoriah Waterland 
1425c51f124SMoriah Waterland 	return (ret_val);
1435c51f124SMoriah Waterland }
1445c51f124SMoriah Waterland 
1455c51f124SMoriah Waterland static int
bad_first_char(char * param,char * value)1465c51f124SMoriah Waterland bad_first_char(char *param, char *value)
1475c51f124SMoriah Waterland {
1485c51f124SMoriah Waterland 	if (*value == '(') {
1495c51f124SMoriah Waterland 		progerr(pkg_gt(ERR_CHAR), param);
1505c51f124SMoriah Waterland 		return (1);
1515c51f124SMoriah Waterland 	}
1525c51f124SMoriah Waterland 
1535c51f124SMoriah Waterland 	return (0);
1545c51f124SMoriah Waterland }
1555c51f124SMoriah Waterland 
1565c51f124SMoriah Waterland static int
isnull(char * param,char * pt)1575c51f124SMoriah Waterland isnull(char *param, char *pt)
1585c51f124SMoriah Waterland {
1595c51f124SMoriah Waterland 	if (!pt || *pt == '\0') {
1605c51f124SMoriah Waterland 		progerr(pkg_gt(ERR_UNDEF), param);
1615c51f124SMoriah Waterland 		return (1);
1625c51f124SMoriah Waterland 	}
1635c51f124SMoriah Waterland 	return (0);
1645c51f124SMoriah Waterland }
1655c51f124SMoriah Waterland 
1665c51f124SMoriah Waterland static int
too_long(char * param,char * pt,int len)1675c51f124SMoriah Waterland too_long(char *param, char *pt, int len)
1685c51f124SMoriah Waterland {
1695c51f124SMoriah Waterland 	if (strlen(pt) > (size_t)len) {
1704656d474SGarrett D'Amore 		progerr(pkg_gt(ERR_LEN), param);
1715c51f124SMoriah Waterland 		return (1);
1725c51f124SMoriah Waterland 	}
1735c51f124SMoriah Waterland 	return (0);
1745c51f124SMoriah Waterland }
1755c51f124SMoriah Waterland 
1765c51f124SMoriah Waterland static int
not_ascii(char * param,char * pt)1775c51f124SMoriah Waterland not_ascii(char *param, char *pt)
1785c51f124SMoriah Waterland {
1795c51f124SMoriah Waterland 	while (*pt) {
1805c51f124SMoriah Waterland 		if (!(isascii(*pt))) {
1815c51f124SMoriah Waterland 			progerr(pkg_gt(ERR_ASCII), param);
1825c51f124SMoriah Waterland 			return (1);
1835c51f124SMoriah Waterland 		}
1845c51f124SMoriah Waterland 		pt++;
1855c51f124SMoriah Waterland 	}
1865c51f124SMoriah Waterland 	return (0);
1875c51f124SMoriah Waterland }
1885c51f124SMoriah Waterland 
1895c51f124SMoriah Waterland static int
not_alnum(char * param,char * pt)1905c51f124SMoriah Waterland not_alnum(char *param, char *pt)
1915c51f124SMoriah Waterland {
1925c51f124SMoriah Waterland 	while (*pt) {
1935c51f124SMoriah Waterland 		if (!(isalnum(*pt))) {
1945c51f124SMoriah Waterland 			progerr(pkg_gt(ERR_ALNUM), param);
1955c51f124SMoriah Waterland 			return (1);
1965c51f124SMoriah Waterland 		}
1975c51f124SMoriah Waterland 		pt++;
1985c51f124SMoriah Waterland 	}
1995c51f124SMoriah Waterland 
2005c51f124SMoriah Waterland 	return (0);
2015c51f124SMoriah Waterland }