xref: /titanic_41/usr/src/cmd/oamuser/lib/vgroup.c (revision 1816cb7076d3ec8a78ef9ac9f895574e13c43645)
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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2 */
32 
33 /*LINTLIBRARY*/
34 
35 #include	<sys/types.h>
36 #include	<stdio.h>
37 #include	<stdlib.h>
38 #include	<ctype.h>
39 #include	<errno.h>
40 #include	<limits.h>
41 #include	<sys/param.h>
42 #include	<users.h>
43 #include	<userdefs.h>
44 
45 extern int valid_gid(gid_t, struct group **);
46 
47 static int isalldigit(char *);
48 
49 /*
50  *	validate a group name or number and return the appropriate
51  *	group structure for it.
52  */
53 int
54 valid_group(char *group, struct group **gptr, int *warning)
55 {
56 	int r, warn;
57 	long l;
58 	char *ptr;
59 	struct group *grp;
60 
61 	*warning = 0;
62 
63 	if (!isalldigit(group))
64 		return (valid_gname(group, gptr, warning));
65 
66 	/*
67 	 * There are only digits in group name.
68 	 * strtol() doesn't return negative number here.
69 	 */
70 	errno = 0;
71 	l = strtol(group, &ptr, 10);
72 	if ((l == LONG_MAX && errno == ERANGE) || l > MAXUID) {
73 		r = TOOBIG;
74 	} else {
75 		if ((r = valid_gid((gid_t)l, &grp)) == NOTUNIQUE) {
76 			/* It is a valid existing gid */
77 			if (gptr != NULL)
78 				*gptr = grp;
79 			return (NOTUNIQUE);
80 		}
81 	}
82 	/*
83 	 * It's all digit, but not a valid gid nor an existing gid.
84 	 * There might be an existing group name of all digits.
85 	 */
86 	if (valid_gname(group, &grp, &warn) == NOTUNIQUE) {
87 		/* It does exist */
88 		*warning = warn;
89 		if (gptr != NULL)
90 			*gptr = grp;
91 		return (NOTUNIQUE);
92 	}
93 	/*
94 	 * It isn't either existing gid or group name. We return the
95 	 * error code from valid_gid() assuming that given string
96 	 * represents an integer GID.
97 	 */
98 	return (r);
99 }
100 
101 static int
102 isalldigit(char *str)
103 {
104 	while (*str != '\0') {
105 		if (!isdigit((unsigned char)*str))
106 			return (0);
107 		str++;
108 	}
109 	return (1);
110 }
111