xref: /titanic_41/usr/src/cmd/oamuser/user/val_lgrp.c (revision 3fb517f786391b507780c78aabb8d98bfea9efe9)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #include	<sys/types.h>
31 #include	<stdio.h>
32 #include	<stdlib.h>
33 #include	<sys/param.h>
34 #include	<unistd.h>
35 #include	<users.h>
36 #include	<userdefs.h>
37 #include	"messages.h"
38 
39 extern void exit();
40 extern char *strtok();
41 
42 static gid_t *grplist;
43 static int ngroups_max = 0;
44 
45 /* Validate a list of groups */
46 int	**
47 valid_lgroup(char *list, gid_t gid)
48 {
49 	int n_invalid = 0, i = 0, j;
50 	char *ptr;
51 	struct group *g_ptr;
52 	int warning;
53 	int dup_prim = 0; /* we don't duplicate our primary as a supplemental */
54 
55 	if( !list || !*list )
56 		return( (int **) NULL );
57 
58 	if (ngroups_max == 0) {
59 		ngroups_max = sysconf(_SC_NGROUPS_MAX);
60 		grplist = malloc((ngroups_max + 1) * sizeof (gid_t));
61 	}
62 
63 	while (ptr = strtok(((i || n_invalid || dup_prim)? NULL: list), ",")) {
64 
65 		switch (valid_group(ptr, &g_ptr, &warning)) {
66 		case INVALID:
67 			errmsg( M_INVALID, ptr, "group id" );
68 			n_invalid++;
69 			break;
70 		case TOOBIG:
71 			errmsg( M_TOOBIG, "gid", ptr );
72 			n_invalid++;
73 			break;
74 		case UNIQUE:
75 			errmsg( M_GRP_NOTUSED, ptr );
76 			n_invalid++;
77 			break;
78 		case NOTUNIQUE:
79 			/* ignore duplicated primary */
80 			if (g_ptr->gr_gid == gid) {
81 				if (!dup_prim)
82 					dup_prim++;
83 				continue;
84 			}
85 
86 			if( !i )
87 				grplist[ i++ ] = g_ptr->gr_gid;
88 			else {
89 				/* Keep out duplicates */
90 				for( j = 0; j < i; j++ )
91 					if( g_ptr->gr_gid == grplist[j] )
92 						break;
93 
94 				if( j == i )
95 					/* Not a duplicate */
96 					grplist[i++] = g_ptr->gr_gid;
97 			}
98 			break;
99 
100 		}
101 		if (warning)
102 			warningmsg(warning, ptr);
103 
104 		if( i >= ngroups_max ) {
105 			errmsg( M_MAXGROUPS, ngroups_max );
106 			break;
107 		}
108 	}
109 
110 	/* Terminate the list */
111 	grplist[ i ] = -1;
112 
113 	if( n_invalid )
114 		exit( EX_BADARG );
115 
116 	return( (int **)grplist );
117 }
118