17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*a776e906Snakanon * Common Development and Distribution License (the "License").
6*a776e906Snakanon * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*a776e906Snakanon
227c478bd9Sstevel@tonic-gate /*
23*a776e906Snakanon * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*a776e906Snakanon * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
36*a776e906Snakanon #include <errno.h>
37*a776e906Snakanon #include <limits.h>
38*a776e906Snakanon #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <users.h>
407c478bd9Sstevel@tonic-gate #include <userdefs.h>
417c478bd9Sstevel@tonic-gate
42*a776e906Snakanon extern int valid_gid(gid_t, struct group **);
43*a776e906Snakanon
44*a776e906Snakanon static int isalldigit(char *);
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * validate a group name or number and return the appropriate
487c478bd9Sstevel@tonic-gate * group structure for it.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate int
valid_group(char * group,struct group ** gptr,int * warning)517c478bd9Sstevel@tonic-gate valid_group(char *group, struct group **gptr, int *warning)
527c478bd9Sstevel@tonic-gate {
53*a776e906Snakanon int r, warn;
54*a776e906Snakanon long l;
557c478bd9Sstevel@tonic-gate char *ptr;
56*a776e906Snakanon struct group *grp;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate *warning = 0;
59*a776e906Snakanon
60*a776e906Snakanon if (!isalldigit(group))
61*a776e906Snakanon return (valid_gname(group, gptr, warning));
62*a776e906Snakanon
63*a776e906Snakanon /*
64*a776e906Snakanon * There are only digits in group name.
65*a776e906Snakanon * strtol() doesn't return negative number here.
66*a776e906Snakanon */
67*a776e906Snakanon errno = 0;
68*a776e906Snakanon l = strtol(group, &ptr, 10);
69*a776e906Snakanon if ((l == LONG_MAX && errno == ERANGE) || l > MAXUID) {
70*a776e906Snakanon r = TOOBIG;
71*a776e906Snakanon } else {
72*a776e906Snakanon if ((r = valid_gid((gid_t)l, &grp)) == NOTUNIQUE) {
73*a776e906Snakanon /* It is a valid existing gid */
74*a776e906Snakanon if (gptr != NULL)
75*a776e906Snakanon *gptr = grp;
76*a776e906Snakanon return (NOTUNIQUE);
77*a776e906Snakanon }
787c478bd9Sstevel@tonic-gate }
79*a776e906Snakanon /*
80*a776e906Snakanon * It's all digit, but not a valid gid nor an existing gid.
81*a776e906Snakanon * There might be an existing group name of all digits.
82*a776e906Snakanon */
83*a776e906Snakanon if (valid_gname(group, &grp, &warn) == NOTUNIQUE) {
84*a776e906Snakanon /* It does exist */
85*a776e906Snakanon *warning = warn;
86*a776e906Snakanon if (gptr != NULL)
87*a776e906Snakanon *gptr = grp;
88*a776e906Snakanon return (NOTUNIQUE);
897c478bd9Sstevel@tonic-gate }
90*a776e906Snakanon /*
91*a776e906Snakanon * It isn't either existing gid or group name. We return the
92*a776e906Snakanon * error code from valid_gid() assuming that given string
93*a776e906Snakanon * represents an integer GID.
94*a776e906Snakanon */
95*a776e906Snakanon return (r);
96*a776e906Snakanon }
977c478bd9Sstevel@tonic-gate
98*a776e906Snakanon static int
isalldigit(char * str)99*a776e906Snakanon isalldigit(char *str)
100*a776e906Snakanon {
101*a776e906Snakanon while (*str != '\0') {
102*a776e906Snakanon if (!isdigit((unsigned char)*str))
103*a776e906Snakanon return (0);
104*a776e906Snakanon str++;
105*a776e906Snakanon }
106*a776e906Snakanon return (1);
1077c478bd9Sstevel@tonic-gate }
108