bin/82058: [PATCH] Ability for pw groupmod to delete members
Seán Farley
sean-freebsd at farley.org
Thu Jun 9 01:10:09 GMT 2005
>Number: 82058
>Category: bin
>Synopsis: [PATCH] Ability for pw groupmod to delete members
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: update
>Submitter-Id: current-users
>Arrival-Date: Thu Jun 09 01:10:08 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator: Seán Farley
>Release: FreeBSD 5.4-STABLE i386
>Organization:
>Environment:
System: FreeBSD thor.farley.org 5.4-STABLE FreeBSD 5.4-STABLE #2: Fri Jun 3 08:54:24 CDT 2005 root at thor.farley.org:/usr/obj/usr/src/sys/THOR i386
>Description:
pw has the ability to add to a group's members with the -m flag. I
wrote a patch to give it the ability to delete specified group members
using the -d flag.
>How-To-Repeat:
Example to add member1 and member2 to group1's member list:
pw groupmod group1 -d member1,member2
>Fix:
This patch can also be found here:
http://www.farley.org/freebsd/tmp/pw.patch
diff -u /usr/src/usr.sbin/pw/pw.8 ./pw.8
--- /usr/src/usr.sbin/pw/pw.8 Fri Jul 2 18:12:54 2004
+++ ./pw.8 Wed Jun 8 19:51:57 2005
@@ -152,6 +152,7 @@
.Op Fl l Ar name
.Op Fl M Ar members
.Op Fl m Ar newmembers
+.Op Fl d Ar oldmembers
.Op Fl h Ar fd | Fl H Ar fd
.Op Fl N
.Op Fl P
@@ -754,6 +755,15 @@
.Fl M ,
this option allows the
.Em addition
+of existing users to a group without replacing the existing list of
+members.
+Login names or user ids may be used, and duplicate users are
+silently eliminated.
+.It Fl d Ar oldmembers
+Similar to
+.Fl M ,
+this option allows the
+.Em deletion
of existing users to a group without replacing the existing list of
members.
Login names or user ids may be used, and duplicate users are
diff -u /usr/src/usr.sbin/pw/pw.c ./pw.c
--- /usr/src/usr.sbin/pw/pw.c Sun Jan 11 12:28:08 2004
+++ ./pw.c Wed Jun 8 19:51:57 2005
@@ -117,7 +117,7 @@
{ /* grp */
"V:C:qn:g:h:H:M:pNPY",
"V:C:qn:g:Y",
- "V:C:qn:g:l:h:H:FM:m:NPY",
+ "V:C:qn:d:g:l:h:H:FM:m:NPY",
"V:C:qn:g:FPa",
"V:C:q"
}
diff -u /usr/src/usr.sbin/pw/pw_group.c ./pw_group.c
--- /usr/src/usr.sbin/pw/pw_group.c Sun Jan 11 12:28:08 2004
+++ ./pw_group.c Wed Jun 8 19:51:57 2005
@@ -207,37 +207,55 @@
}
}
- if (((arg = getarg(args, 'M')) != NULL || (arg = getarg(args, 'm')) != NULL) && arg->val) {
+ if (((arg = getarg(args, 'M')) != NULL ||
+ (arg = getarg(args, 'd')) != NULL ||
+ (arg = getarg(args, 'm')) != NULL) && arg->val) {
int i = 0;
char *p;
struct passwd *pwd;
/* Make sure this is not stay NULL with -M "" */
extendarray(&members, &grmembers, 200);
- if (arg->ch == 'm') {
+ if (arg->ch == 'd' || arg->ch == 'm') {
int k = 0;
+ int matchFound;
while (grp->gr_mem[k] != NULL) {
- if (extendarray(&members, &grmembers, i + 2) != -1) {
- members[i++] = grp->gr_mem[k];
+ /* Delete requested members. */
+ for (matchFound = 0,
+ p = strtok(arg->val, ", \t"); p != NULL;
+ p = strtok(NULL, ", \t")) {
+ if ((pwd = GETPWNAM(p)) == NULL)
+ if (!isdigit((unsigned char)*p) ||
+ (pwd = getpwuid((uid_t) atoi(p))) == NULL)
+ errx(EX_NOUSER, "user `%s' does not exist", p);
+ if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
+ matchFound = 1;
+ break;
+ }
}
+ if (! matchFound)
+ if (extendarray(&members, &grmembers, i + 2) != -1) {
+ members[i++] = grp->gr_mem[k];
+ }
k++;
}
}
- for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
- int j;
- if ((pwd = GETPWNAM(p)) == NULL) {
- if (!isdigit((unsigned char)*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL)
- errx(EX_NOUSER, "user `%s' does not exist", p);
+ if (arg->ch != 'd')
+ for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
+ int j;
+ if ((pwd = GETPWNAM(p)) == NULL) {
+ if (!isdigit((unsigned char)*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL)
+ errx(EX_NOUSER, "user `%s' does not exist", p);
+ }
+ /*
+ * Check for duplicates
+ */
+ 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);
}
- /*
- * Check for duplicates
- */
- 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);
- }
while (i < grmembers)
members[i++] = NULL;
grp->gr_mem = members;
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-bugs
mailing list