xref: /illumos-gate/usr/src/cmd/oamuser/user/val_lgrp.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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"
32 
33 #include	<sys/types.h>
34 #include	<stdio.h>
35 #include	<sys/param.h>
36 #include	<users.h>
37 #include	<userdefs.h>
38 #include	"messages.h"
39 
40 extern int get_ngm();
41 extern void exit();
42 extern char *strtok();
43 
44 static gid_t grplist[ NGROUPS_UMAX + 1 ];
45 static int ngroups_max = 0;
46 
47 /* Validate a list of groups */
48 int	**
49 valid_lgroup(char *list, gid_t gid)
50 {
51 	int n_invalid = 0, i = 0, j;
52 	char *ptr;
53 	struct group *g_ptr;
54 	int warning;
55 	int dup_prim = 0; /* we don't duplicate our primary as a supplemental */
56 
57 	if( !list || !*list )
58 		return( (int **) NULL );
59 
60 	while (ptr = strtok(((i || n_invalid || dup_prim)? NULL: list), ",")) {
61 
62 		switch (valid_group(ptr, &g_ptr, &warning)) {
63 		case INVALID:
64 			errmsg( M_INVALID, ptr, "group id" );
65 			n_invalid++;
66 			break;
67 		case TOOBIG:
68 			errmsg( M_TOOBIG, "gid", ptr );
69 			n_invalid++;
70 			break;
71 		case UNIQUE:
72 			errmsg( M_GRP_NOTUSED, ptr );
73 			n_invalid++;
74 			break;
75 		case NOTUNIQUE:
76 			/* ignore duplicated primary */
77 			if (g_ptr->gr_gid == gid) {
78 				if (!dup_prim)
79 					dup_prim++;
80 				continue;
81 			}
82 
83 			if( !i )
84 				grplist[ i++ ] = g_ptr->gr_gid;
85 			else {
86 				/* Keep out duplicates */
87 				for( j = 0; j < i; j++ )
88 					if( g_ptr->gr_gid == grplist[j] )
89 						break;
90 
91 				if( j == i )
92 					/* Not a duplicate */
93 					grplist[i++] = g_ptr->gr_gid;
94 			}
95 			break;
96 
97 		}
98 		if (warning)
99 			warningmsg(warning, ptr);
100 
101 		if( !ngroups_max )
102 			ngroups_max = get_ngm();
103 
104 
105 		if( i >= ngroups_max ) {
106 			errmsg( M_MAXGROUPS, ngroups_max );
107 			break;
108 		}
109 	}
110 
111 	/* Terminate the list */
112 	grplist[ i ] = -1;
113 
114 	if( n_invalid )
115 		exit( EX_BADARG );
116 
117 	return( (int **)grplist );
118 }
119