10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6418Snakanon * Common Development and Distribution License (the "License").
6*6418Snakanon * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6418Snakanon
220Sstevel@tonic-gate /*
23*6418Snakanon * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*6418Snakanon * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*LINTLIBRARY*/
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <ctype.h>
39*6418Snakanon #include <errno.h>
40*6418Snakanon #include <limits.h>
41*6418Snakanon #include <sys/param.h>
420Sstevel@tonic-gate #include <users.h>
430Sstevel@tonic-gate #include <userdefs.h>
440Sstevel@tonic-gate
45*6418Snakanon extern int valid_gid(gid_t, struct group **);
46*6418Snakanon
47*6418Snakanon static int isalldigit(char *);
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * validate a group name or number and return the appropriate
510Sstevel@tonic-gate * group structure for it.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate int
valid_group(char * group,struct group ** gptr,int * warning)540Sstevel@tonic-gate valid_group(char *group, struct group **gptr, int *warning)
550Sstevel@tonic-gate {
56*6418Snakanon int r, warn;
57*6418Snakanon long l;
580Sstevel@tonic-gate char *ptr;
59*6418Snakanon struct group *grp;
600Sstevel@tonic-gate
610Sstevel@tonic-gate *warning = 0;
62*6418Snakanon
63*6418Snakanon if (!isalldigit(group))
64*6418Snakanon return (valid_gname(group, gptr, warning));
65*6418Snakanon
66*6418Snakanon /*
67*6418Snakanon * There are only digits in group name.
68*6418Snakanon * strtol() doesn't return negative number here.
69*6418Snakanon */
70*6418Snakanon errno = 0;
71*6418Snakanon l = strtol(group, &ptr, 10);
72*6418Snakanon if ((l == LONG_MAX && errno == ERANGE) || l > MAXUID) {
73*6418Snakanon r = TOOBIG;
74*6418Snakanon } else {
75*6418Snakanon if ((r = valid_gid((gid_t)l, &grp)) == NOTUNIQUE) {
76*6418Snakanon /* It is a valid existing gid */
77*6418Snakanon if (gptr != NULL)
78*6418Snakanon *gptr = grp;
79*6418Snakanon return (NOTUNIQUE);
80*6418Snakanon }
810Sstevel@tonic-gate }
82*6418Snakanon /*
83*6418Snakanon * It's all digit, but not a valid gid nor an existing gid.
84*6418Snakanon * There might be an existing group name of all digits.
85*6418Snakanon */
86*6418Snakanon if (valid_gname(group, &grp, &warn) == NOTUNIQUE) {
87*6418Snakanon /* It does exist */
88*6418Snakanon *warning = warn;
89*6418Snakanon if (gptr != NULL)
90*6418Snakanon *gptr = grp;
91*6418Snakanon return (NOTUNIQUE);
920Sstevel@tonic-gate }
93*6418Snakanon /*
94*6418Snakanon * It isn't either existing gid or group name. We return the
95*6418Snakanon * error code from valid_gid() assuming that given string
96*6418Snakanon * represents an integer GID.
97*6418Snakanon */
98*6418Snakanon return (r);
99*6418Snakanon }
1000Sstevel@tonic-gate
101*6418Snakanon static int
isalldigit(char * str)102*6418Snakanon isalldigit(char *str)
103*6418Snakanon {
104*6418Snakanon while (*str != '\0') {
105*6418Snakanon if (!isdigit((unsigned char)*str))
106*6418Snakanon return (0);
107*6418Snakanon str++;
108*6418Snakanon }
109*6418Snakanon return (1);
1100Sstevel@tonic-gate }
111