Skip to content

Create parent dirs for useradd -m#112

Merged
hallyn merged 1 commit intoshadow-maint:masterfrom
jubalh:useradd-mkdirs
May 23, 2018
Merged

Create parent dirs for useradd -m#112
hallyn merged 1 commit intoshadow-maint:masterfrom
jubalh:useradd-mkdirs

Conversation

@jubalh
Copy link
Copy Markdown
Member

@jubalh jubalh commented May 15, 2018

Equivalent of mkdir -p. It will create all parent directories.
Example: useradd -d /home2/testu1 -m testu1

Based on #2 by Thorsten Kukuk
and Thorsten Behrens which was Code from pwdutils 3.2.2 with slight adaptations.

Adapted to so it applies to current code.

Equivalent of `mkdir -p`. It will create all parent directories.
Example: `useradd -d /home2/testu1 -m testu1`

Based on shadow-maint#2 by Thorsten Kukuk
and Thorsten Behrens which was Code from pwdutils 3.2.2 with slight adaptations.

Adapted to so it applies to current code.
@jubalh
Copy link
Copy Markdown
Member Author

jubalh commented May 15, 2018

Adapted code from #2 to apply to current code base.

@hallyn please review.

@hallyn
Copy link
Copy Markdown
Member

hallyn commented May 23, 2018

Thanks! Will take a look.

@hallyn hallyn merged commit eadcb47 into shadow-maint:master May 23, 2018
@seiferteric
Copy link
Copy Markdown

FYI, there is an issue if the username contains a / which unfortunately debian at least allows.

$ sudo useradd -m 'abc/def'
useradd: warning: chown on `/home/abc/def' failed: No such file or directory
$ ls -l /home/
total 8
drwxr-xr-x  3 root     root     4096 May 23 20:22 abc
$ ls -l /home/abc/
total 4
drwxr-xr-x 2 abc/def abc/def 4096 May 23 20:22 def
$ grep abc /etc/passwd
abc/def:x:1001:1001::/home/abc/def:/bin/sh

@ikerexxe
Copy link
Copy Markdown
Collaborator

FYI, there is an issue if the username contains a / which unfortunately debian at least allows.

$ sudo useradd -m 'abc/def'
useradd: warning: chown on `/home/abc/def' failed: No such file or directory
$ ls -l /home/
total 8
drwxr-xr-x  3 root     root     4096 May 23 20:22 abc
$ ls -l /home/abc/
total 4
drwxr-xr-x 2 abc/def abc/def 4096 May 23 20:22 def
$ grep abc /etc/passwd
abc/def:x:1001:1001::/home/abc/def:/bin/sh

This seems like a Debian related bug as I'm unable to reproduce it in Fedora:

# useradd -m 'abc/def'
useradd: invalid user name 'abc/def': use --badname to ignore
# useradd -m abc/def
useradd: invalid user name 'abc/def': use --badname to ignore

I'd recommend you to open a bug in their tracking system.

@jubalh
Copy link
Copy Markdown
Member Author

jubalh commented May 25, 2023

FYI, there is an issue if the username contains a / which unfortunately debian at least allows.

Someone tested it for me now on Debian Bookworm and Bullseye and reported that it works in both cases.

@jubalh jubalh deleted the useradd-mkdirs branch May 25, 2023 06:27
@seiferteric
Copy link
Copy Markdown

seiferteric commented May 25, 2023

Yes it has to do with Debian patching chkname.c to allow almost all characters in usernames by default. That and this change to create parent directories will create the issue. My issue is that when creating the directories it will do strtok("/") on the home path (create with /home/<username>), but it does not distinguish between a "/" in the path and one in the username itself. I beelive that if the "/" is in the username itself, it should not attempt to create it as a parent directory since it is not, but rather part of the username. So that is a bug I think here.

@seiferteric
Copy link
Copy Markdown

@jubalh in Bullseye and Bookworm, I just tested in docker, and I don't se the "chown" error, but behavior is the same. The other issue is due to the ambiguity of "/" in username and home path, it doesn't remove correctly when removing the user with "userdel -r":

eseifert@bilbo:~$ docker run -it debian:bullseye
root@ba14121520fb:/# 
root@ba14121520fb:/# 
root@ba14121520fb:/# useradd -m "abc/def"
root@ba14121520fb:/# ls -l /home/
total 4
drwxr-xr-x 3 root root 4096 May 25 14:41 abc
root@ba14121520fb:/# grep abc /etc/passwd
abc/def:x:1000:1000::/home/abc/def:/bin/sh
root@ba14121520fb:/# userdel -r "abc/def"
userdel: abc/def mail spool (/var/mail/abc/def) not found
root@ba14121520fb:/# ls -l /home/
total 4
drwxr-xr-x 2 root root 4096 May 25 14:42 abc
root@ba14121520fb:/# grep abc /etc/passwd
root@ba14121520fb:/# 

@jubalh
Copy link
Copy Markdown
Member Author

jubalh commented May 28, 2023

Someone tested it for me now on Debian Bookworm and Bullseye and reported that it works in both cases.

Correction: they confused useradd and adduser ;)

alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Mar 1, 2026
'path' will only copy contents from 'bhome', which itself is a dup of
'prefix_user_home'.  Thus it only needs to be able to hold the contents
of that string (thus, strlen(3) + 1 for the NUL).

We don't add any characters that were not present in the original
string (but we do collapse adjacent slashes), and thus the 'path' string
may be shorter or as long as 'bhome', but certainly not longer.

This seems to have been an off-by-one calculation, as this code has
never needed space for an extra byte, AFAICS.

Fixes: b3b6d9d (2018-05-15; "Create parent dirs for useradd -m")
Link: <shadow-maint#112>
Link: <shadow-maint#2>
Cc: Michael Vetter <jubalh@iodoru.org>
Cc: Thorsten Behrens <thb@documentfoundation.org>
Cc: Thorsten Kukuk <kukuk@suse.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Eric Seifert <seiferteric@gmail.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Mar 1, 2026
'path' will only copy contents from 'bhome', which itself is a dup of
'prefix_user_home'.  Thus it only needs to be able to hold the contents
of that string (thus, strlen(3) + 1 for the NUL).

We don't add any characters that were not present in the original
string (but we do collapse adjacent slashes), and thus the 'path' string
may be shorter or as long as 'bhome', but certainly not longer.

This seems to have been an off-by-one calculation, as this code has
never needed space for an extra byte, AFAICS.

Fixes: b3b6d9d (2018-05-15; "Create parent dirs for useradd -m")
Link: <shadow-maint#112>
Link: <shadow-maint#2>
Cc: Michael Vetter <jubalh@iodoru.org>
Cc: Thorsten Behrens <thb@documentfoundation.org>
Cc: Thorsten Kukuk <kukuk@suse.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Eric Seifert <seiferteric@gmail.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Mar 4, 2026
'path' will only copy contents from 'bhome', which itself is a dup of
'prefix_user_home'.  Thus it only needs to be able to hold the contents
of that string (thus, strlen(3) + 1 for the NUL).

We don't add any characters that were not present in the original
string (but we do collapse adjacent slashes), and thus the 'path' string
may be shorter or as long as 'bhome', but certainly not longer.

This seems to have been an off-by-one calculation, as this code has
never needed space for an extra byte, AFAICS.

Fixes: b3b6d9d (2018-05-15; "Create parent dirs for useradd -m")
Link: <shadow-maint#112>
Link: <shadow-maint#2>
Cc: Michael Vetter <jubalh@iodoru.org>
Cc: Thorsten Behrens <thb@documentfoundation.org>
Cc: Thorsten Kukuk <kukuk@suse.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Eric Seifert <seiferteric@gmail.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants