1996-12-09 14:05:35 +00:00
|
|
|
/*-
|
1996-12-10 23:59:04 +00:00
|
|
|
* Copyright (C) 1996
|
|
|
|
* David L. Nugent. All rights reserved.
|
1996-12-09 14:05:35 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
1996-12-10 23:59:04 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
1996-12-09 14:05:35 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
1996-12-10 23:59:04 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
|
1996-12-09 14:05:35 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1996-12-10 23:59:04 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
|
1996-12-09 14:05:35 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-10 06:23:42 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1997-10-10 06:23:42 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-12-09 14:05:35 +00:00
|
|
|
#include <ctype.h>
|
1997-10-10 06:23:42 +00:00
|
|
|
#include <err.h>
|
1996-12-09 14:05:35 +00:00
|
|
|
#include <termios.h>
|
2008-02-23 01:25:22 +00:00
|
|
|
#include <stdbool.h>
|
1997-10-10 06:23:42 +00:00
|
|
|
#include <unistd.h>
|
2012-10-30 08:00:53 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <libutil.h>
|
1996-12-09 14:05:35 +00:00
|
|
|
|
|
|
|
#include "pw.h"
|
|
|
|
#include "bitmap.h"
|
|
|
|
|
|
|
|
|
2008-02-23 01:25:22 +00:00
|
|
|
static struct passwd *lookup_pwent(const char *user);
|
|
|
|
static void delete_members(char ***members, int *grmembers, int *i,
|
|
|
|
struct carg *arg, struct group *grp);
|
2015-06-07 15:27:17 +00:00
|
|
|
static int print_group(struct group * grp);
|
2015-06-07 19:59:01 +00:00
|
|
|
static gid_t gr_gidpolicy(struct userconf * cnf, long id);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-07-04 15:54:11 +00:00
|
|
|
static void
|
|
|
|
set_passwd(struct group *grp, bool update)
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
int istty;
|
|
|
|
struct termios t, n;
|
|
|
|
char *p, line[256];
|
|
|
|
|
|
|
|
if (conf.fd == '-') {
|
|
|
|
grp->gr_passwd = "*"; /* No access */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((istty = isatty(conf.fd))) {
|
|
|
|
n = t;
|
|
|
|
/* Disable echo */
|
|
|
|
n.c_lflag &= ~(ECHO);
|
|
|
|
tcsetattr(conf.fd, TCSANOW, &n);
|
|
|
|
printf("%sassword for group %s:", update ? "New p" : "P",
|
|
|
|
grp->gr_name);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
b = read(conf.fd, line, sizeof(line) - 1);
|
|
|
|
if (istty) { /* Restore state */
|
|
|
|
tcsetattr(conf.fd, TCSANOW, &t);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
if (b < 0)
|
|
|
|
err(EX_OSERR, "-h file descriptor");
|
|
|
|
line[b] = '\0';
|
|
|
|
if ((p = strpbrk(line, " \t\r\n")) != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
if (!*line)
|
|
|
|
errx(EX_DATAERR, "empty password read on file descriptor %d",
|
|
|
|
conf.fd);
|
|
|
|
if (conf.precrypted) {
|
|
|
|
if (strchr(line, ':') != 0)
|
|
|
|
errx(EX_DATAERR, "wrong encrypted passwrd");
|
|
|
|
grp->gr_passwd = line;
|
|
|
|
} else
|
|
|
|
grp->gr_passwd = pw_pwcrypt(line);
|
|
|
|
}
|
|
|
|
|
2015-07-11 16:58:47 +00:00
|
|
|
int
|
|
|
|
pw_groupnext(struct userconf *cnf, bool quiet)
|
|
|
|
{
|
|
|
|
gid_t next = gr_gidpolicy(cnf, -1);
|
|
|
|
|
|
|
|
if (quiet)
|
|
|
|
return (next);
|
|
|
|
printf("%u\n", next);
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2015-07-11 18:09:27 +00:00
|
|
|
static int
|
|
|
|
pw_groupshow(const char *name, long id, struct group *fakegroup)
|
|
|
|
{
|
|
|
|
struct group *grp = NULL;
|
|
|
|
|
|
|
|
if (id < 0 && name == NULL && !conf.all)
|
|
|
|
errx(EX_DATAERR, "groupname or id or '-a' required");
|
|
|
|
|
|
|
|
if (conf.all) {
|
|
|
|
SETGRENT();
|
|
|
|
while ((grp = GETGRENT()) != NULL)
|
|
|
|
print_group(grp);
|
|
|
|
ENDGRENT();
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
|
|
|
|
if (grp == NULL) {
|
|
|
|
if (conf.force) {
|
|
|
|
grp = fakegroup;
|
|
|
|
} else {
|
|
|
|
if (name == NULL)
|
|
|
|
errx(EX_DATAERR, "unknown gid `%ld'", id);
|
|
|
|
errx(EX_DATAERR, "unknown group `%s'", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (print_group(grp));
|
|
|
|
}
|
|
|
|
|
1996-12-09 14:05:35 +00:00
|
|
|
int
|
2015-06-07 19:03:41 +00:00
|
|
|
pw_group(int mode, char *name, long id, struct cargs * args)
|
1996-12-09 14:05:35 +00:00
|
|
|
{
|
1999-10-26 04:27:14 +00:00
|
|
|
int rc;
|
1996-12-09 14:05:35 +00:00
|
|
|
struct carg *arg;
|
|
|
|
struct group *grp = NULL;
|
1996-12-21 15:35:45 +00:00
|
|
|
int grmembers = 0;
|
1999-10-26 04:27:14 +00:00
|
|
|
char **members = NULL;
|
2015-06-07 14:34:38 +00:00
|
|
|
struct userconf *cnf = conf.userconf;
|
1996-12-09 14:05:35 +00:00
|
|
|
|
|
|
|
static struct group fakegroup =
|
|
|
|
{
|
|
|
|
"nogroup",
|
|
|
|
"*",
|
|
|
|
-1,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-07-11 16:58:47 +00:00
|
|
|
if (mode == M_NEXT)
|
2015-07-11 17:01:08 +00:00
|
|
|
return (pw_groupnext(cnf, conf.quiet));
|
2015-07-11 16:58:47 +00:00
|
|
|
|
2015-07-11 18:09:27 +00:00
|
|
|
if (mode == M_PRINT)
|
|
|
|
return (pw_groupshow(name, id, &fakegroup));
|
|
|
|
|
1999-10-26 08:34:09 +00:00
|
|
|
if (mode == M_LOCK || mode == M_UNLOCK)
|
|
|
|
errx(EX_USAGE, "'lock' command is not available for groups");
|
|
|
|
|
2015-06-07 19:03:41 +00:00
|
|
|
if (id < 0 && name == NULL)
|
|
|
|
errx(EX_DATAERR, "group name or id required");
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-06-07 19:03:41 +00:00
|
|
|
grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-07-11 18:09:27 +00:00
|
|
|
if (mode == M_UPDATE || mode == M_DELETE) {
|
2015-06-07 19:03:41 +00:00
|
|
|
if (name == NULL && grp == NULL) /* Try harder */
|
|
|
|
grp = GETGRGID(id);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
|
|
|
if (grp == NULL) {
|
2015-06-07 19:03:41 +00:00
|
|
|
if (name == NULL)
|
|
|
|
errx(EX_DATAERR, "unknown group `%s'", name);
|
|
|
|
else
|
|
|
|
errx(EX_DATAERR, "unknown group `%ld'", id);
|
1996-12-09 14:05:35 +00:00
|
|
|
}
|
2015-06-07 19:03:41 +00:00
|
|
|
if (name == NULL) /* Needed later */
|
|
|
|
name = grp->gr_name;
|
1996-12-09 14:05:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle deletions now
|
|
|
|
*/
|
|
|
|
if (mode == M_DELETE) {
|
1999-10-26 04:27:14 +00:00
|
|
|
rc = delgrent(grp);
|
|
|
|
if (rc == -1)
|
2015-06-07 19:03:41 +00:00
|
|
|
err(EX_IOERR, "group '%s' not available (NIS?)",
|
|
|
|
name);
|
1999-10-26 04:27:14 +00:00
|
|
|
else if (rc != 0) {
|
2015-05-31 11:23:19 +00:00
|
|
|
err(EX_IOERR, "group update");
|
1999-10-26 04:27:14 +00:00
|
|
|
}
|
2015-06-07 19:03:41 +00:00
|
|
|
pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", name, id);
|
1996-12-09 23:55:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
1996-12-09 14:05:35 +00:00
|
|
|
} else if (mode == M_PRINT)
|
2015-06-07 15:27:17 +00:00
|
|
|
return print_group(grp);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-06-07 19:03:41 +00:00
|
|
|
if (id > 0)
|
|
|
|
grp->gr_gid = (gid_t) id;
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-06-07 19:33:25 +00:00
|
|
|
if (conf.newname != NULL)
|
|
|
|
grp->gr_name = pw_checkname(conf.newname, 0);
|
1996-12-09 14:05:35 +00:00
|
|
|
} else {
|
2015-06-07 19:03:41 +00:00
|
|
|
if (name == NULL) /* Required */
|
1997-10-10 06:23:42 +00:00
|
|
|
errx(EX_DATAERR, "group name required");
|
1996-12-09 14:05:35 +00:00
|
|
|
else if (grp != NULL) /* Exists */
|
2015-06-07 19:03:41 +00:00
|
|
|
errx(EX_DATAERR, "group name `%s' already exists", name);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
1996-12-21 15:35:45 +00:00
|
|
|
extendarray(&members, &grmembers, 200);
|
|
|
|
members[0] = NULL;
|
1996-12-09 14:05:35 +00:00
|
|
|
grp = &fakegroup;
|
2015-06-07 19:03:41 +00:00
|
|
|
grp->gr_name = pw_checkname(name, 0);
|
1996-12-09 14:05:35 +00:00
|
|
|
grp->gr_passwd = "*";
|
2015-06-07 19:59:01 +00:00
|
|
|
grp->gr_gid = gr_gidpolicy(cnf, id);
|
1996-12-09 14:05:35 +00:00
|
|
|
grp->gr_mem = members;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This allows us to set a group password Group passwords is an
|
|
|
|
* antique idea, rarely used and insecure (no secure database) Should
|
|
|
|
* be discouraged, but it is apparently still supported by some
|
|
|
|
* software.
|
|
|
|
*/
|
|
|
|
|
2015-07-09 14:14:44 +00:00
|
|
|
if (conf.which == W_GROUP && conf.fd != -1)
|
2015-07-04 15:54:11 +00:00
|
|
|
set_passwd(grp, mode == M_UPDATE);
|
1996-12-09 23:55:27 +00:00
|
|
|
|
2008-02-23 01:25:22 +00:00
|
|
|
if (((arg = getarg(args, 'M')) != NULL ||
|
|
|
|
(arg = getarg(args, 'd')) != NULL ||
|
|
|
|
(arg = getarg(args, 'm')) != NULL) && arg->val) {
|
1996-12-09 23:55:27 +00:00
|
|
|
int i = 0;
|
|
|
|
char *p;
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
1996-12-21 15:35:45 +00:00
|
|
|
/* Make sure this is not stay NULL with -M "" */
|
|
|
|
extendarray(&members, &grmembers, 200);
|
2008-02-23 01:25:22 +00:00
|
|
|
if (arg->ch == 'd')
|
|
|
|
delete_members(&members, &grmembers, &i, arg, grp);
|
|
|
|
else if (arg->ch == 'm') {
|
1996-12-21 15:35:45 +00:00
|
|
|
int k = 0;
|
|
|
|
|
2014-03-06 19:26:08 +00:00
|
|
|
if (grp->gr_mem != NULL) {
|
|
|
|
while (grp->gr_mem[k] != NULL) {
|
|
|
|
if (extendarray(&members, &grmembers, i + 2) != -1)
|
|
|
|
members[i++] = grp->gr_mem[k];
|
|
|
|
k++;
|
|
|
|
}
|
1996-12-09 23:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-23 01:25:22 +00:00
|
|
|
|
|
|
|
if (arg->ch != 'd')
|
|
|
|
for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for duplicates
|
|
|
|
*/
|
|
|
|
pwd = lookup_pwent(p);
|
|
|
|
for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
|
|
|
|
;
|
|
|
|
if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
|
|
|
|
members[i++] = newstr(pwd->pw_name);
|
1996-12-09 23:55:27 +00:00
|
|
|
}
|
1996-12-21 15:35:45 +00:00
|
|
|
while (i < grmembers)
|
1996-12-09 23:55:27 +00:00
|
|
|
members[i++] = NULL;
|
|
|
|
grp->gr_mem = members;
|
|
|
|
}
|
|
|
|
|
2015-06-07 15:09:53 +00:00
|
|
|
if (conf.dryrun)
|
2015-06-07 15:27:17 +00:00
|
|
|
return print_group(grp);
|
1996-12-09 23:55:27 +00:00
|
|
|
|
1999-10-26 04:27:14 +00:00
|
|
|
if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
|
|
|
|
if (rc == -1)
|
2015-05-31 11:23:19 +00:00
|
|
|
errx(EX_IOERR, "group '%s' already exists",
|
|
|
|
grp->gr_name);
|
1999-10-26 04:27:14 +00:00
|
|
|
else
|
2015-05-31 11:23:19 +00:00
|
|
|
err(EX_IOERR, "group update");
|
2015-06-07 19:03:41 +00:00
|
|
|
} else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) {
|
1999-10-26 04:27:14 +00:00
|
|
|
if (rc == -1)
|
2015-05-31 11:23:19 +00:00
|
|
|
errx(EX_IOERR, "group '%s' not available (NIS?)",
|
|
|
|
grp->gr_name);
|
1999-10-26 04:27:14 +00:00
|
|
|
else
|
2015-05-31 11:23:19 +00:00
|
|
|
err(EX_IOERR, "group update");
|
1996-12-09 14:05:35 +00:00
|
|
|
}
|
2014-10-28 11:20:30 +00:00
|
|
|
|
2015-06-07 19:33:25 +00:00
|
|
|
if (conf.newname != NULL)
|
|
|
|
name = conf.newname;
|
1996-12-09 14:05:35 +00:00
|
|
|
/* grp may have been invalidated */
|
2015-06-07 19:03:41 +00:00
|
|
|
if ((grp = GETGRNAM(name)) == NULL)
|
1997-10-10 06:23:42 +00:00
|
|
|
errx(EX_SOFTWARE, "group disappeared during update");
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-05-31 22:07:03 +00:00
|
|
|
pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2012-12-05 13:56:43 +00:00
|
|
|
free(members);
|
1996-12-21 15:35:45 +00:00
|
|
|
|
1996-12-09 23:55:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
1996-12-09 14:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-23 01:25:22 +00:00
|
|
|
/*
|
|
|
|
* Lookup a passwd entry using a name or UID.
|
|
|
|
*/
|
|
|
|
static struct passwd *
|
|
|
|
lookup_pwent(const char *user)
|
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
|
|
|
if ((pwd = GETPWNAM(user)) == NULL &&
|
|
|
|
(!isdigit((unsigned char)*user) ||
|
|
|
|
(pwd = getpwuid((uid_t) atoi(user))) == NULL))
|
|
|
|
errx(EX_NOUSER, "user `%s' does not exist", user);
|
|
|
|
|
|
|
|
return (pwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete requested members from a group.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
|
|
|
|
struct group *grp)
|
|
|
|
{
|
|
|
|
bool matchFound;
|
|
|
|
char *user;
|
|
|
|
char *valueCopy;
|
|
|
|
char *valuePtr;
|
|
|
|
int k;
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
2014-03-06 19:26:08 +00:00
|
|
|
if (grp->gr_mem == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-02-23 01:25:22 +00:00
|
|
|
k = 0;
|
|
|
|
while (grp->gr_mem[k] != NULL) {
|
|
|
|
matchFound = false;
|
|
|
|
if ((valueCopy = strdup(arg->val)) == NULL)
|
|
|
|
errx(EX_UNAVAILABLE, "out of memory");
|
|
|
|
valuePtr = valueCopy;
|
|
|
|
while ((user = strsep(&valuePtr, ", \t")) != NULL) {
|
|
|
|
pwd = lookup_pwent(user);
|
|
|
|
if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
|
|
|
|
matchFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(valueCopy);
|
|
|
|
|
|
|
|
if (!matchFound &&
|
|
|
|
extendarray(members, grmembers, *i + 2) != -1)
|
|
|
|
(*members)[(*i)++] = grp->gr_mem[k];
|
|
|
|
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 14:05:35 +00:00
|
|
|
static gid_t
|
2015-06-07 19:59:01 +00:00
|
|
|
gr_gidpolicy(struct userconf * cnf, long id)
|
1996-12-09 14:05:35 +00:00
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
gid_t gid = (gid_t) - 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the given gid, if any
|
|
|
|
*/
|
2015-06-07 19:59:01 +00:00
|
|
|
if (id > 0) {
|
|
|
|
gid = (gid_t) id;
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-06-07 19:59:01 +00:00
|
|
|
if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate)
|
2015-05-31 22:07:03 +00:00
|
|
|
errx(EX_DATAERR, "gid `%u' has already been allocated", grp->gr_gid);
|
1996-12-09 14:05:35 +00:00
|
|
|
} else {
|
|
|
|
struct bitmap bm;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to allocate the next available gid under one of
|
|
|
|
* two policies a) Grab the first unused gid b) Grab the
|
|
|
|
* highest possible unused gid
|
|
|
|
*/
|
|
|
|
if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
|
|
|
|
cnf->min_gid = 1000;
|
|
|
|
cnf->max_gid = 32000;
|
|
|
|
}
|
|
|
|
bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now, let's fill the bitmap from the password file
|
|
|
|
*/
|
1999-02-23 07:15:11 +00:00
|
|
|
SETGRENT();
|
|
|
|
while ((grp = GETGRENT()) != NULL)
|
2000-01-15 00:20:22 +00:00
|
|
|
if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
|
|
|
|
(gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
|
1996-12-09 14:05:35 +00:00
|
|
|
bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
|
1999-02-23 07:15:11 +00:00
|
|
|
ENDGRENT();
|
1996-12-09 14:05:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Then apply the policy, with fallback to reuse if necessary
|
|
|
|
*/
|
|
|
|
if (cnf->reuse_gids)
|
|
|
|
gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
|
|
|
|
else {
|
|
|
|
gid = (gid_t) (bm_lastset(&bm) + 1);
|
|
|
|
if (!bm_isset(&bm, gid))
|
|
|
|
gid += cnf->min_gid;
|
|
|
|
else
|
|
|
|
gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Another sanity check
|
|
|
|
*/
|
|
|
|
if (gid < cnf->min_gid || gid > cnf->max_gid)
|
1997-10-10 06:23:42 +00:00
|
|
|
errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
|
1996-12-09 14:05:35 +00:00
|
|
|
bm_dealloc(&bm);
|
|
|
|
}
|
|
|
|
return gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2015-06-07 15:27:17 +00:00
|
|
|
print_group(struct group * grp)
|
1996-12-09 14:05:35 +00:00
|
|
|
{
|
2015-06-07 15:27:17 +00:00
|
|
|
if (!conf.pretty) {
|
1996-12-21 15:35:45 +00:00
|
|
|
char *buf = NULL;
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2012-10-30 08:00:53 +00:00
|
|
|
buf = gr_make(grp);
|
2012-12-27 14:44:13 +00:00
|
|
|
printf("%s\n", buf);
|
1996-12-21 15:35:45 +00:00
|
|
|
free(buf);
|
1996-12-09 14:05:35 +00:00
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
|
1997-02-07 11:31:42 +00:00
|
|
|
printf("Group Name: %-15s #%lu\n"
|
1996-12-21 15:35:45 +00:00
|
|
|
" Members: ",
|
1996-12-09 14:05:35 +00:00
|
|
|
grp->gr_name, (long) grp->gr_gid);
|
2014-03-06 19:26:08 +00:00
|
|
|
if (grp->gr_mem != NULL) {
|
|
|
|
for (i = 0; grp->gr_mem[i]; i++)
|
|
|
|
printf("%s%s", i ? "," : "", grp->gr_mem[i]);
|
|
|
|
}
|
1996-12-09 14:05:35 +00:00
|
|
|
fputs("\n\n", stdout);
|
|
|
|
}
|
1996-12-09 23:55:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
1996-12-09 14:05:35 +00:00
|
|
|
}
|