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