1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /*LINTLIBRARY*/
31
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <sys/param.h>
39 #include <users.h>
40 #include <userdefs.h>
41
42 extern int valid_gid(gid_t, struct group **);
43
44 static int isalldigit(char *);
45
46 /*
47 * validate a group name or number and return the appropriate
48 * group structure for it.
49 */
50 int
valid_group(char * group,struct group ** gptr,int * warning)51 valid_group(char *group, struct group **gptr, int *warning)
52 {
53 int r, warn;
54 long l;
55 char *ptr;
56 struct group *grp;
57
58 *warning = 0;
59
60 if (!isalldigit(group))
61 return (valid_gname(group, gptr, warning));
62
63 /*
64 * There are only digits in group name.
65 * strtol() doesn't return negative number here.
66 */
67 errno = 0;
68 l = strtol(group, &ptr, 10);
69 if ((l == LONG_MAX && errno == ERANGE) || l > MAXUID) {
70 r = TOOBIG;
71 } else {
72 if ((r = valid_gid((gid_t)l, &grp)) == NOTUNIQUE) {
73 /* It is a valid existing gid */
74 if (gptr != NULL)
75 *gptr = grp;
76 return (NOTUNIQUE);
77 }
78 }
79 /*
80 * It's all digit, but not a valid gid nor an existing gid.
81 * There might be an existing group name of all digits.
82 */
83 if (valid_gname(group, &grp, &warn) == NOTUNIQUE) {
84 /* It does exist */
85 *warning = warn;
86 if (gptr != NULL)
87 *gptr = grp;
88 return (NOTUNIQUE);
89 }
90 /*
91 * It isn't either existing gid or group name. We return the
92 * error code from valid_gid() assuming that given string
93 * represents an integer GID.
94 */
95 return (r);
96 }
97
98 static int
isalldigit(char * str)99 isalldigit(char *str)
100 {
101 while (*str != '\0') {
102 if (!isdigit((unsigned char)*str))
103 return (0);
104 str++;
105 }
106 return (1);
107 }
108