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