Hi

I think I found a scenario with useradd(8) where a warning would be nice.

Given the following setup:

foo# useradd test_user
useradd: Warning: home directory `/home/test_user' doesn't exist, and -m was 
not specified
foo# cat /etc/passwd
<snip>
cvs:*:1003:1003::/home/cvs:/bin/ksh
test_user:*:1002:1002::/home/test_user:/bin/ksh
foo# cat /etc/group
<snip>
cvs:*:1003:
test_user:*:1002:

Now if I delete the user with userdel(8):
foo# userdel test_user

and then readd the user with useradd(8):

foo# useradd test_user
useradd: Warning: home directory `/home/test_user' doesn't exist, and -m was 
not specified
foo# cat /etc/group
<snip>
cvs:*:1003:
test_user:*:1002:
foo# cat /etc/passwd
<snip>
cvs:*:1003:1003::/home/cvs:/bin/ksh
test_user:*:1004:1004::/home/test_user:/bin/ksh

the gid of the user (1004) does not match the group id (1002) with the same name
(test_user). I think it would be a good idea to print a warning if that happens,
so that subsequent operations (e.g. chown :test_user ...) involving the group
name don't have unexpected effects.

Below is a patch which implements that. Here is the result of it (starting 
again from zero):

foo# useradd test_user
useradd: Warning: home directory `/home/test_user' doesn't exist, and -m was 
not specified
foo# cat /etc/passwd
<snip>
cvs:*:1003:1003::/home/cvs:/bin/ksh
test_user:*:1002:1002::/home/test_user:/bin/ksh
foo# cat /etc/groups
<snip>
cvs:*:1003:
test_user:*:1002:
foo# useradd test_user
user: Warning: home directory `/home/test_user' doesn't exist, and -m was not 
specified
user: Warning: group with name test_user already exists with gid 1002

Comments, thoughts, feedback?

Kind regards,

Martin

Index: user.c
===================================================================
RCS file: /cvs/src/usr.sbin/user/user.c,v
retrieving revision 1.128
diff -u -p -r1.128 user.c
--- user.c      17 Oct 2019 21:54:29 -0000      1.128
+++ user.c      31 Oct 2019 18:49:45 -0000
@@ -1218,12 +1218,20 @@ adduser(char *login_name, user_t *up)
                        (void) asystem("%s -R u+w %s", CHMOD, home);
                }
        }
-       if (strcmp(up->u_primgrp, "=uid") == 0 && !group_exists(login_name) &&
-           !creategid(login_name, gid, "")) {
-               close(ptmpfd);
-               pw_abort();
-               errx(EXIT_FAILURE, "can't create gid %u for login name %s",
-                   gid, login_name);
+       if (strcmp(up->u_primgrp, "=uid") == 0) {
+               gid_t other_gid;
+               if (gid_from_group(login_name, &other_gid) != -1 &&
+                   gid != other_gid) {
+                       warnx("Warning: group with name %s already exists with 
gid %u",
+                           login_name, other_gid);
+               }
+               if (!group_exists(login_name) &&
+                   !creategid(login_name, gid, "")) {
+                       close(ptmpfd);
+                       pw_abort();
+                       errx(EXIT_FAILURE, "can't create gid %u for login name 
%s",
+                           gid, login_name);
+               }
        }
        if (up->u_groupc > 0 && !append_group(login_name, up->u_groupc, 
up->u_groupv)) {
                close(ptmpfd);

Reply via email to