/*-
- * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
- * All rights reserved.
+ * Copyright (C) 1996
+ * David L. Nugent. All rights reserved.
*
* 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
- * notice, this list of conditions and the following disclaimer as
- * the first lines of this file unmodified.
+ * notice, this list of conditions and the following disclaimer.
* 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.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by David L. Nugent.
- * 4. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
*
- * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
+ * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
+ * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
* 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)
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id$
+ * $Id: pw_group.c,v 1.2 1996/12/19 15:22:44 davidn Exp $
*/
#include <unistd.h>
#include "pw.h"
#include "bitmap.h"
+#include "pwupd.h"
static int print_group(struct group * grp, int pretty);
struct carg *a_gid = getarg(args, 'g');
struct carg *arg;
struct group *grp = NULL;
- char *members[_UC_MAXGROUPS];
+ int grmembers = 0;
+ char **members = NULL;
static struct group fakegroup =
{
if (grp == NULL) {
if (mode == M_PRINT && getarg(args, 'F')) {
+ char *fmems[1];
+ fmems[0] = NULL;
fakegroup.gr_name = a_name ? a_name->val : "nogroup";
fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1;
+ fakegroup.gr_mem = fmems;
return print_group(&fakegroup, getarg(args, 'P') != NULL);
}
cmderr(EX_DATAERR, "unknown group `%s'\n", a_name ? a_name->val : a_gid->val);
grp->gr_gid = (gid_t) atoi(a_gid->val);
if ((arg = getarg(args, 'l')) != NULL)
- grp->gr_name = arg->val;
+ grp->gr_name = pw_checkname((u_char *)arg->val, 0);
} else {
if (a_name == NULL) /* Required */
cmderr(EX_DATAERR, "group name required\n");
else if (grp != NULL) /* Exists */
cmderr(EX_DATAERR, "group name `%s' already exists\n", a_name->val);
- memset(members, 0, sizeof members);
+ extendarray(&members, &grmembers, 200);
+ members[0] = NULL;
grp = &fakegroup;
- grp->gr_name = a_name->val;
+ grp->gr_name = pw_checkname((u_char *)a_name->val, 0);
grp->gr_passwd = "*";
grp->gr_gid = gr_gidpolicy(cnf, args);
grp->gr_mem = members;
char *p;
struct passwd *pwd;
+ /* Make sure this is not stay NULL with -M "" */
+ extendarray(&members, &grmembers, 200);
if (arg->ch == 'm') {
- while (i < _UC_MAXGROUPS && grp->gr_mem[i] != NULL) {
- members[i] = grp->gr_mem[i];
- i++;
+ int k = 0;
+
+ while (grp->gr_mem[k] != NULL) {
+ if (extendarray(&members, &grmembers, i + 2) != -1) {
+ members[i++] = grp->gr_mem[k];
+ }
+ k++;
}
}
- for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) {
+ for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
int j;
if ((pwd = getpwnam(p)) == NULL) {
if (!isdigit(*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL)
*/
for (j = 0; j < i && strcmp(members[j], pwd->pw_name)!=0; j++)
;
- if (j == i)
+ if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
members[i++] = newstr(pwd->pw_name);
}
- while (i < _UC_MAXGROUPS)
+ while (i < grmembers)
members[i++] = NULL;
grp->gr_mem = members;
}
pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
+ if (members)
+ free(members);
+
return EXIT_SUCCESS;
}
print_group(struct group * grp, int pretty)
{
if (!pretty) {
- char buf[_UC_MAXLINE];
+ int buflen = 0;
+ char *buf = NULL;
- fmtgrent(buf, grp);
+ fmtgrent(&buf, &buflen, grp);
fputs(buf, stdout);
+ free(buf);
} else {
int i;
- printf("Group Name : %-10s #%lu\n"
- " Members : ",
+ printf("Group Name: %-10s #%lu\n"
+ " Members: ",
grp->gr_name, (long) grp->gr_gid);
- for (i = 0; i < _UC_MAXGROUPS && grp->gr_mem[i]; i++)
+ for (i = 0; grp->gr_mem[i]; i++)
printf("%s%s", i ? "," : "", grp->gr_mem[i]);
fputs("\n\n", stdout);
}