summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pw
diff options
context:
space:
mode:
authorbapt <bapt@FreeBSD.org>2015-08-21 07:09:53 +0000
committerbapt <bapt@FreeBSD.org>2015-08-21 07:09:53 +0000
commit75130e0bf33fa6ddc5766900f6ea00360ae8aac5 (patch)
treeb5078970353add72711e1da37b430b8ca62b92c5 /usr.sbin/pw
parentb982178f6ba05816de1e0c6ee4115e784cf91b7f (diff)
downloadFreeBSD-src-75130e0bf33fa6ddc5766900f6ea00360ae8aac5.zip
FreeBSD-src-75130e0bf33fa6ddc5766900f6ea00360ae8aac5.tar.gz
Fix useradd regression:
Readd the function to create the parents home directory if it does not exists. if it is only a directory at the top level of the hierarchy symlink it into /usr as it used to be done before. Reported by: kevlo, adrian
Diffstat (limited to 'usr.sbin/pw')
-rw-r--r--usr.sbin/pw/pw_user.c66
-rwxr-xr-xusr.sbin/pw/tests/pw_useradd.sh3
2 files changed, 67 insertions, 2 deletions
diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c
index 5ccbd53..35eb1fb 100644
--- a/usr.sbin/pw/pw_user.c
+++ b/usr.sbin/pw/pw_user.c
@@ -38,6 +38,7 @@ static const char rcsid[] =
#include <ctype.h>
#include <dirent.h>
#include <err.h>
+#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
@@ -85,11 +86,76 @@ static void rmat(uid_t uid);
static void rmopie(char const * name);
static void
+mkdir_home_parents(int dfd, const char *dir)
+{
+ struct stat st;
+ char *dirs, *tmp;
+
+ if (*dir != '/')
+ errx(EX_DATAERR, "invalid base directory for home '%s'", dir);
+
+ dir++;
+
+ if (fstatat(dfd, dir, &st, 0) != -1) {
+ if (S_ISDIR(st.st_mode))
+ return;
+ errx(EX_OSFILE, "root home `/%s' is not a directory", dir);
+ }
+
+ dirs = strdup(dir);
+ if (dirs == NULL)
+ errx(EX_UNAVAILABLE, "out of memory");
+
+ tmp = strrchr(dirs, '/');
+ if (tmp == NULL)
+ return;
+ tmp[0] = '\0';
+
+ /*
+ * This is a kludge especially for Joerg :)
+ * If the home directory would be created in the root partition, then
+ * we really create it under /usr which is likely to have more space.
+ * But we create a symlink from cnf->home -> "/usr" -> cnf->home
+ */
+ if (strchr(dirs, '/') == NULL) {
+ asprintf(&tmp, "usr/%s", dirs);
+ if (tmp == NULL)
+ errx(EX_UNAVAILABLE, "out of memory");
+ if (mkdirat(dfd, tmp, _DEF_DIRMODE) != -1 || errno == EEXIST) {
+ fchownat(dfd, tmp, 0, 0, 0);
+ symlinkat(tmp, dfd, dirs + 1);
+ }
+ free(tmp);
+ }
+ tmp = dirs;
+ if (fstatat(dfd, dirs, &st, 0) == -1) {
+ while ((tmp = strchr(tmp + 1, '/')) != NULL) {
+ *tmp = '\0';
+ if (fstatat(dfd, dirs, &st, 0) == -1) {
+ if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
+ err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs);
+ }
+ *tmp = '/';
+ }
+ }
+ if (fstatat(dfd, dirs, &st, 0) == -1) {
+ if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
+ err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs);
+ fchownat(dfd, dirs, 0, 0, 0);
+ }
+
+ free(dirs);
+}
+
+static void
create_and_populate_homedir(struct userconf *cnf, struct passwd *pwd,
const char *skeldir, mode_t homemode, bool update)
{
int skelfd = -1;
+ /* Create home parents directories */
+ mkdir_home_parents(conf.rootfd, pwd->pw_dir);
+
if (skeldir != NULL && *skeldir != '\0') {
if (*skeldir == '/')
skeldir++;
diff --git a/usr.sbin/pw/tests/pw_useradd.sh b/usr.sbin/pw/tests/pw_useradd.sh
index f126bf0..d27df73 100755
--- a/usr.sbin/pw/tests/pw_useradd.sh
+++ b/usr.sbin/pw/tests/pw_useradd.sh
@@ -245,7 +245,6 @@ user_add_R_body() {
populate_root_etc_skel
atf_check -s exit:0 ${RPW} useradd foo
- mkdir -p ${HOME}/home
atf_check -s exit:0 ${RPW} useradd bar -m
test -d ${HOME}/home/bar || atf_fail "Directory not created"
atf_check -s exit:0 ${RPW} userdel bar
@@ -260,7 +259,7 @@ user_add_skel_body() {
populate_root_etc_skel
mkdir ${HOME}/skel
- echo "a" > ${HOME}/skel/.a
+ echo "a" > ${HOME}/skel/.ae
echo "b" > ${HOME}/skel/b
mkdir ${HOME}/skel/c
mkdir ${HOME}/skel/c/d
OpenPOWER on IntegriCloud