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