10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11134SCasper.Dik@Sun.COM * Common Development and Distribution License (the "License").
6*11134SCasper.Dik@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11134SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <stdio.h>
32*11134SCasper.Dik@Sun.COM #include <stdlib.h>
330Sstevel@tonic-gate #include <sys/param.h>
34*11134SCasper.Dik@Sun.COM #include <unistd.h>
350Sstevel@tonic-gate #include <users.h>
360Sstevel@tonic-gate #include <userdefs.h>
370Sstevel@tonic-gate #include "messages.h"
380Sstevel@tonic-gate
390Sstevel@tonic-gate extern void exit();
400Sstevel@tonic-gate extern char *strtok();
410Sstevel@tonic-gate
42*11134SCasper.Dik@Sun.COM static gid_t *grplist;
43108Sbasabi static int ngroups_max = 0;
440Sstevel@tonic-gate
450Sstevel@tonic-gate /* Validate a list of groups */
460Sstevel@tonic-gate int **
valid_lgroup(char * list,gid_t gid)47108Sbasabi valid_lgroup(char *list, gid_t gid)
480Sstevel@tonic-gate {
49108Sbasabi int n_invalid = 0, i = 0, j;
500Sstevel@tonic-gate char *ptr;
510Sstevel@tonic-gate struct group *g_ptr;
520Sstevel@tonic-gate int warning;
530Sstevel@tonic-gate int dup_prim = 0; /* we don't duplicate our primary as a supplemental */
540Sstevel@tonic-gate
550Sstevel@tonic-gate if( !list || !*list )
560Sstevel@tonic-gate return( (int **) NULL );
570Sstevel@tonic-gate
58*11134SCasper.Dik@Sun.COM if (ngroups_max == 0) {
59*11134SCasper.Dik@Sun.COM ngroups_max = sysconf(_SC_NGROUPS_MAX);
60*11134SCasper.Dik@Sun.COM grplist = malloc((ngroups_max + 1) * sizeof (gid_t));
61*11134SCasper.Dik@Sun.COM }
62*11134SCasper.Dik@Sun.COM
630Sstevel@tonic-gate while (ptr = strtok(((i || n_invalid || dup_prim)? NULL: list), ",")) {
640Sstevel@tonic-gate
650Sstevel@tonic-gate switch (valid_group(ptr, &g_ptr, &warning)) {
660Sstevel@tonic-gate case INVALID:
670Sstevel@tonic-gate errmsg( M_INVALID, ptr, "group id" );
680Sstevel@tonic-gate n_invalid++;
690Sstevel@tonic-gate break;
700Sstevel@tonic-gate case TOOBIG:
710Sstevel@tonic-gate errmsg( M_TOOBIG, "gid", ptr );
720Sstevel@tonic-gate n_invalid++;
730Sstevel@tonic-gate break;
740Sstevel@tonic-gate case UNIQUE:
750Sstevel@tonic-gate errmsg( M_GRP_NOTUSED, ptr );
760Sstevel@tonic-gate n_invalid++;
770Sstevel@tonic-gate break;
780Sstevel@tonic-gate case NOTUNIQUE:
790Sstevel@tonic-gate /* ignore duplicated primary */
800Sstevel@tonic-gate if (g_ptr->gr_gid == gid) {
810Sstevel@tonic-gate if (!dup_prim)
820Sstevel@tonic-gate dup_prim++;
830Sstevel@tonic-gate continue;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate if( !i )
870Sstevel@tonic-gate grplist[ i++ ] = g_ptr->gr_gid;
880Sstevel@tonic-gate else {
890Sstevel@tonic-gate /* Keep out duplicates */
900Sstevel@tonic-gate for( j = 0; j < i; j++ )
910Sstevel@tonic-gate if( g_ptr->gr_gid == grplist[j] )
920Sstevel@tonic-gate break;
930Sstevel@tonic-gate
940Sstevel@tonic-gate if( j == i )
950Sstevel@tonic-gate /* Not a duplicate */
960Sstevel@tonic-gate grplist[i++] = g_ptr->gr_gid;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate break;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate if (warning)
1020Sstevel@tonic-gate warningmsg(warning, ptr);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if( i >= ngroups_max ) {
1050Sstevel@tonic-gate errmsg( M_MAXGROUPS, ngroups_max );
1060Sstevel@tonic-gate break;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /* Terminate the list */
1110Sstevel@tonic-gate grplist[ i ] = -1;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if( n_invalid )
1140Sstevel@tonic-gate exit( EX_BADARG );
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate return( (int **)grplist );
1170Sstevel@tonic-gate }
118