Chef: linux_user: when modifying a users' homedir, tries to create it even if it exists

Created on 8 Nov 2019  Â·  5Comments  Â·  Source: chef/chef

Description

When linux_user modifies a user to set it's homedir, if that homedir exists, it tries to create it, fails, and fails the run.

It looks like this is from passing -m always

Chef Version

14.13.11

Platform Version

Ubuntu 18.04

Replication Case

setup a user. change it's homedir to something that exists.

Client Output

---- Begin output of ["usermod", "-d", "/var/lib/openvpn/chroot", "-m", "nm-openvpn"]
 ----
STDOUT: 
STDERR: usermod: directory /var/lib/openvpn/chroot exists
---- End output of ["usermod", "-d", "/var/lib/openvpn/chroot", "-m", "nm-openvpn"] -
---
Ran ["usermod", "-d", "/var/lib/openvpn/chroot", "-m", "nm-openvpn"] returned 12

Stacktrace

Low Sustaining Backlog Bug

Most helpful comment

For reference, the entry reference in usermod that corresponds to code 12: https://github.com/shadow-maint/shadow/blob/b49712ed328ded0cd8161542ca13cf5d4cf55e5f/src/usermod.c#L92

"/* unable to complete home dir move */"

All 5 comments

Based on what I know about the "usermod" command, that message is purely informational and should not be evaluating the " directory already exists" message as an error. The manpage of usermod specifically addresses what happens if the passed "-d" directory already exists: https://linux.die.net/man/8/usermod

Chef is not actually attempting to re-create the directory, it is simply reading the resulting message as an error when the directory already exists.

[root@dev ~]# useradd test_user
[root@dev ~]# mkdir /home/test_user
mkdir: cannot create directory ‘/home/test_user’: File exists
[root@dev ~]# mkdir /home/new_test_user_dir
[root@dev ~]# usermod -d /home/new_test_user_dir -m test_user
usermod: directory /home/new_test_user_dir exists
[root@dev ~]# echo $?
12

Yeah, it's def usermod. But the question is can we handle this better. The man page doesn't list the exit codes, but if 12 is unique to this, then we could set the allowed exit codes to [0,12].

Yeah I agree. I haven't actively contributed to this project before, but I'll take a crack at it.

For reference, the entry reference in usermod that corresponds to code 12: https://github.com/shadow-maint/shadow/blob/b49712ed328ded0cd8161542ca13cf5d4cf55e5f/src/usermod.c#L92

"/* unable to complete home dir move */"

Was this page helpful?
0 / 5 - 0 ratings