xref: /onnv-gate/usr/src/cmd/oamuser/group/gid.c (revision 4321:a8930ec16e52)
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*4321Scasper  * Common Development and Distribution License (the "License").
6*4321Scasper  * 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  */
21*4321Scasper /*
22*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*4321Scasper  * Use is subject to license terms.
24*4321Scasper  */
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
29*4321Scasper #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5 */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <userdefs.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #ifndef	MAXUID
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #ifdef UID_MAX
390Sstevel@tonic-gate #define	MAXUID	UID_MAX
400Sstevel@tonic-gate #else
410Sstevel@tonic-gate #define	MAXUID	60000
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Check to see that the gid is not a reserved gid
470Sstevel@tonic-gate  * -- nobody, noaccess or nogroup
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate static int
isvalidgid(gid_t gid)500Sstevel@tonic-gate isvalidgid(gid_t gid)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	return (gid != 60001 && gid != 60002 && gid != 65534);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate gid_t
findnextgid()560Sstevel@tonic-gate findnextgid()
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	FILE *fptr;
590Sstevel@tonic-gate 	gid_t last, next;
600Sstevel@tonic-gate 	gid_t gid;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/*
630Sstevel@tonic-gate 	 * Sort the used GIDs in decreasing order to return MAXUSED + 1
640Sstevel@tonic-gate 	 */
650Sstevel@tonic-gate 	if ((fptr = popen("exec sh -c "
660Sstevel@tonic-gate 	    "\"getent group|cut -f3 -d:|sort -nr|uniq \" 2>/dev/null",
670Sstevel@tonic-gate 	    "r")) == NULL)
680Sstevel@tonic-gate 		return (-1);
690Sstevel@tonic-gate 
70*4321Scasper 	if (fscanf(fptr, "%u\n", &next) == EOF) {
710Sstevel@tonic-gate 		(void) pclose(fptr);
720Sstevel@tonic-gate 		return (DEFRID + 1);
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * 'next' is now the highest allocated gid.
770Sstevel@tonic-gate 	 *
780Sstevel@tonic-gate 	 * The simplest allocation is where we just add one, and obtain
790Sstevel@tonic-gate 	 * a valid gid.  If this fails look for a hole in the gid range ..
800Sstevel@tonic-gate 	 */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	last = MAXUID;		/* upper limit */
830Sstevel@tonic-gate 	gid = -1;		/* start invalid */
840Sstevel@tonic-gate 	do {
850Sstevel@tonic-gate 		if (!isvalidgid(next))
860Sstevel@tonic-gate 			continue;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		if (next <= DEFRID) {
890Sstevel@tonic-gate 			if (last != DEFRID + 1)
900Sstevel@tonic-gate 				gid = DEFRID + 1;
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		if ((gid = next + 1) != last) {
950Sstevel@tonic-gate 			while (!isvalidgid(gid))
960Sstevel@tonic-gate 				gid++;
970Sstevel@tonic-gate 			if (gid > 0 && gid < last)
980Sstevel@tonic-gate 				break;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		gid = -1;
1020Sstevel@tonic-gate 		last = next;
1030Sstevel@tonic-gate 
104*4321Scasper 	} while (fscanf(fptr, "%u\n", &next) != EOF);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	(void) pclose(fptr);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	return (gid);
1090Sstevel@tonic-gate }
110