xref: /freebsd/sys/contrib/openzfs/lib/libuutil/uu_ident.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License, Version 1.0 only
7eda14cbcSMatt Macy  * (the "License").  You may not use this file except in compliance
8eda14cbcSMatt Macy  * with the License.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
12eda14cbcSMatt Macy  * See the License for the specific language governing permissions
13eda14cbcSMatt Macy  * and limitations under the License.
14eda14cbcSMatt Macy  *
15eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
16eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
18eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
19eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
20eda14cbcSMatt Macy  *
21eda14cbcSMatt Macy  * CDDL HEADER END
22eda14cbcSMatt Macy  */
23eda14cbcSMatt Macy /*
24eda14cbcSMatt Macy  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25eda14cbcSMatt Macy  * Use is subject to license terms.
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #include "libuutil_common.h"
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #include <string.h>
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy /*
35eda14cbcSMatt Macy  * We require names of the form:
36eda14cbcSMatt Macy  *	[provider,]identifier[/[provider,]identifier]...
37eda14cbcSMatt Macy  *
38eda14cbcSMatt Macy  * Where provider is either a stock symbol (SUNW) or a java-style reversed
39eda14cbcSMatt Macy  * domain name (com.sun).
40eda14cbcSMatt Macy  *
41eda14cbcSMatt Macy  * Both providers and identifiers must start with a letter, and may
42eda14cbcSMatt Macy  * only contain alphanumerics, dashes, and underlines.  Providers
43eda14cbcSMatt Macy  * may also contain periods.
44eda14cbcSMatt Macy  *
45eda14cbcSMatt Macy  * Note that we do _not_ use the macros in <ctype.h>, since they are affected
46eda14cbcSMatt Macy  * by the current locale settings.
47eda14cbcSMatt Macy  */
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy #define	IS_ALPHA(c) \
50eda14cbcSMatt Macy 	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy #define	IS_DIGIT(c) \
53eda14cbcSMatt Macy 	((c) >= '0' && (c) <= '9')
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy static int
is_valid_ident(const char * s,const char * e,int allowdot)56eda14cbcSMatt Macy is_valid_ident(const char *s, const char *e, int allowdot)
57eda14cbcSMatt Macy {
58eda14cbcSMatt Macy 	char c;
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy 	if (s >= e)
61eda14cbcSMatt Macy 		return (0);		/* name is empty */
62eda14cbcSMatt Macy 
63eda14cbcSMatt Macy 	c = *s++;
64eda14cbcSMatt Macy 	if (!IS_ALPHA(c))
65eda14cbcSMatt Macy 		return (0);		/* does not start with letter */
66eda14cbcSMatt Macy 
67eda14cbcSMatt Macy 	while (s < e && (c = *s++) != 0) {
68eda14cbcSMatt Macy 		if (IS_ALPHA(c) || IS_DIGIT(c) || c == '-' || c == '_' ||
69eda14cbcSMatt Macy 		    (allowdot && c == '.'))
70eda14cbcSMatt Macy 			continue;
71eda14cbcSMatt Macy 		return (0);		/* invalid character */
72eda14cbcSMatt Macy 	}
73eda14cbcSMatt Macy 	return (1);
74eda14cbcSMatt Macy }
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy static int
is_valid_component(const char * b,const char * e,uint_t flags)77eda14cbcSMatt Macy is_valid_component(const char *b, const char *e, uint_t flags)
78eda14cbcSMatt Macy {
79eda14cbcSMatt Macy 	char *sp;
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	if (flags & UU_NAME_DOMAIN) {
82eda14cbcSMatt Macy 		sp = strchr(b, ',');
83eda14cbcSMatt Macy 		if (sp != NULL && sp < e) {
84eda14cbcSMatt Macy 			if (!is_valid_ident(b, sp, 1))
85eda14cbcSMatt Macy 				return (0);
86eda14cbcSMatt Macy 			b = sp + 1;
87eda14cbcSMatt Macy 		}
88eda14cbcSMatt Macy 	}
89eda14cbcSMatt Macy 
90eda14cbcSMatt Macy 	return (is_valid_ident(b, e, 0));
91eda14cbcSMatt Macy }
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy int
uu_check_name(const char * name,uint_t flags)94eda14cbcSMatt Macy uu_check_name(const char *name, uint_t flags)
95eda14cbcSMatt Macy {
96eda14cbcSMatt Macy 	const char *end = name + strlen(name);
97eda14cbcSMatt Macy 	const char *p;
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	if (flags & ~(UU_NAME_DOMAIN | UU_NAME_PATH)) {
100eda14cbcSMatt Macy 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
101eda14cbcSMatt Macy 		return (-1);
102eda14cbcSMatt Macy 	}
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 	if (!(flags & UU_NAME_PATH)) {
105eda14cbcSMatt Macy 		if (!is_valid_component(name, end, flags))
106eda14cbcSMatt Macy 			goto bad;
107eda14cbcSMatt Macy 		return (0);
108eda14cbcSMatt Macy 	}
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy 	while ((p = strchr(name, '/')) != NULL) {
111eda14cbcSMatt Macy 		if (!is_valid_component(name, p - 1, flags))
112eda14cbcSMatt Macy 			goto bad;
113eda14cbcSMatt Macy 		name = p + 1;
114eda14cbcSMatt Macy 	}
115eda14cbcSMatt Macy 	if (!is_valid_component(name, end, flags))
116eda14cbcSMatt Macy 		goto bad;
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy 	return (0);
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy bad:
121eda14cbcSMatt Macy 	uu_set_error(UU_ERROR_INVALID_ARGUMENT);
122eda14cbcSMatt Macy 	return (-1);
123eda14cbcSMatt Macy }
124