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