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 2004 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 ngroups_max = 0; 46 47 /* Validate a list of groups */ 48 int ** 49 valid_lgroup( list, gid ) 50 char *list; 51 gid_t gid; 52 { 53 register n_invalid = 0, i = 0, j; 54 char *ptr; 55 struct group *g_ptr; 56 int warning; 57 int dup_prim = 0; /* we don't duplicate our primary as a supplemental */ 58 59 if( !list || !*list ) 60 return( (int **) NULL ); 61 62 while (ptr = strtok(((i || n_invalid || dup_prim)? NULL: list), ",")) { 63 64 switch (valid_group(ptr, &g_ptr, &warning)) { 65 case INVALID: 66 errmsg( M_INVALID, ptr, "group id" ); 67 n_invalid++; 68 break; 69 case TOOBIG: 70 errmsg( M_TOOBIG, "gid", ptr ); 71 n_invalid++; 72 break; 73 case UNIQUE: 74 errmsg( M_GRP_NOTUSED, ptr ); 75 n_invalid++; 76 break; 77 case NOTUNIQUE: 78 /* ignore duplicated primary */ 79 if (g_ptr->gr_gid == gid) { 80 if (!dup_prim) 81 dup_prim++; 82 continue; 83 } 84 85 if( !i ) 86 grplist[ i++ ] = g_ptr->gr_gid; 87 else { 88 /* Keep out duplicates */ 89 for( j = 0; j < i; j++ ) 90 if( g_ptr->gr_gid == grplist[j] ) 91 break; 92 93 if( j == i ) 94 /* Not a duplicate */ 95 grplist[i++] = g_ptr->gr_gid; 96 } 97 break; 98 99 } 100 if (warning) 101 warningmsg(warning, ptr); 102 103 if( !ngroups_max ) 104 ngroups_max = get_ngm(); 105 106 107 if( i >= ngroups_max ) { 108 errmsg( M_MAXGROUPS, ngroups_max ); 109 break; 110 } 111 } 112 113 /* Terminate the list */ 114 grplist[ i ] = -1; 115 116 if( n_invalid ) 117 exit( EX_BADARG ); 118 119 return( (int **)grplist ); 120 } 121