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