a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/*
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * CDDL HEADER START
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont *
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * The contents of this file are subject to the terms of the
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * Common Development and Distribution License (the "License").
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * You may not use this file except in compliance with the License.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont *
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * or http://www.opensolaris.org/os/licensing.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * See the License for the specific language governing permissions
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * and limitations under the License.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont *
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * When distributing Covered Code, include this CDDL HEADER in each
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * If applicable, add the following below this CDDL HEADER, with the
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * fields enclosed by brackets "[]" replaced with your own identifying
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * information: Portions Copyright [yyyy] [name of copyright owner]
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont *
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * CDDL HEADER END
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/*
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * Use is subject to license terms.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/* All Rights Reserved */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/*
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * Copyright (c) 2013 RackTop Systems.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont#include <errno.h>
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont#include <sys/types.h>
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont#include <stdio.h>
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont#include <userdefs.h>
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont#include <grp.h>
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont#include <libcmdutils.h>
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontstatic int findunusedgid(gid_t start, gid_t stop, gid_t *ret);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontstatic boolean_t isreservedgid(gid_t gid);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/*
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * Find the highest unused uid. If the highest unused gid is "stop",
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * then attempt to find a hole in the range. Returns 0 on success.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontint
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontfindnextgid(gid_t start, gid_t stop, gid_t *ret)
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont{
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont gid_t gid = start;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont struct group *grp;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont boolean_t overflow = B_FALSE;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont setgrent();
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont for (grp = getgrent(); grp != NULL; grp = getgrent()) {
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (isreservedgid(grp->gr_gid)) /* Skip reserved IDs */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont continue;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (grp->gr_gid >= gid) {
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (grp->gr_gid == stop) { /* Overflow check */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont overflow = B_TRUE;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont break;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont }
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont gid = grp->gr_gid + 1;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont }
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont }
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (grp == NULL && errno != 0) {
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont endgrent();
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (-1);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont }
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont endgrent();
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (overflow == B_TRUE) /* Find a hole */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (findunusedgid(start, stop, ret));
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont while (isreservedgid(gid) && gid < stop) /* Skip reserved IDs */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont gid++;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont *ret = gid;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (0);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont}
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/*
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * Check to see whether the gid is a reserved gid
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * -- nobody, noaccess or nogroup
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontstatic boolean_t
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontisreservedgid(gid_t gid)
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont{
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (gid == 60001 || gid == 60002 || gid == 65534);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont}
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont/*
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * findunusedgid() attempts to return the next valid usable id between the
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont * supplied upper and lower limits. Returns 0 on success.
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont */
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontstatic int
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormontfindunusedgid(gid_t start, gid_t stop, gid_t *ret)
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont{
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont gid_t gid;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont for (gid = start; gid <= stop; gid++) {
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (isreservedgid(gid))
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont continue;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (getgrgid(gid) == NULL) {
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (errno != 0)
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (-1);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont break;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont }
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont }
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont if (gid > stop)
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (-1);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont *ret = gid;
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont return (0);
a7fe1d5bb55904d4c79638b8778bc9dd8ed7fd7bAndy Stormont}