Skip to content

Commit 1287910

Browse files
committed
PR/59415: ssszcmawo: group(8) can rename a group to an existing group name
1 parent 25e58ee commit 1287910

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

usr.sbin/user/user.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: user.c,v 1.135 2021/12/12 14:33:13 nia Exp $ */
1+
/* $NetBSD: user.c,v 1.136 2026/03/17 17:37:50 christos Exp $ */
22

33
/*
44
* Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
@@ -33,7 +33,7 @@
3333
#ifndef lint
3434
__COPYRIGHT("@(#) Copyright (c) 1999\
3535
The NetBSD Foundation, Inc. All rights reserved.");
36-
__RCSID("$NetBSD: user.c,v 1.135 2021/12/12 14:33:13 nia Exp $");
36+
__RCSID("$NetBSD: user.c,v 1.136 2026/03/17 17:37:50 christos Exp $");
3737
#endif
3838

3939
#include <sys/types.h>
@@ -2424,16 +2424,20 @@ groupmod(int argc, char **argv)
24242424
if (!is_local(*argv, _PATH_GROUP)) {
24252425
errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
24262426
}
2427-
if (newname != NULL && !valid_group(newname)) {
2428-
warnx("Invalid group name `%s'", newname);
2427+
if (newname != NULL) {
2428+
if (!valid_group(newname))
2429+
errx(EXIT_FAILURE, "Invalid group name `%s'", newname);
2430+
if (getgrnam(newname))
2431+
errx(EXIT_FAILURE, "Can't rename group `%s' to `%s': "
2432+
"group `%s' already exists",
2433+
*argv, newname, newname);
24292434
}
24302435
cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
2431-
(newname) ? newname : grp->gr_name,
2432-
grp->gr_passwd,
2433-
(gid < 0) ? grp->gr_gid : gid);
2434-
for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
2436+
newname ? newname : grp->gr_name, grp->gr_passwd,
2437+
gid < 0 ? grp->gr_gid : gid);
2438+
for (cpp = grp->gr_mem; *cpp && cc < sizeof(buf); cpp++) {
24352439
cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
2436-
(cpp[1] == NULL) ? "" : ",");
2440+
(cpp[1] == NULL) ? "" : ",");
24372441
}
24382442
cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
24392443
if (newname != NULL)

0 commit comments

Comments
 (0)