From owner-svn-src-head@freebsd.org Fri Aug 21 07:09:54 2015 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8F2DF9BD73A; Fri, 21 Aug 2015 07:09:54 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7FC1A1C58; Fri, 21 Aug 2015 07:09:54 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id t7L79scJ012999; Fri, 21 Aug 2015 07:09:54 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id t7L79sAX012997; Fri, 21 Aug 2015 07:09:54 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201508210709.t7L79sAX012997@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Fri, 21 Aug 2015 07:09:54 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r286982 - in head/usr.sbin/pw: . tests X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Aug 2015 07:09:54 -0000 Author: bapt Date: Fri Aug 21 07:09:53 2015 New Revision: 286982 URL: https://svnweb.freebsd.org/changeset/base/286982 Log: 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 Modified: head/usr.sbin/pw/pw_user.c head/usr.sbin/pw/tests/pw_useradd.sh Modified: head/usr.sbin/pw/pw_user.c ============================================================================== --- head/usr.sbin/pw/pw_user.c Fri Aug 21 06:30:13 2015 (r286981) +++ head/usr.sbin/pw/pw_user.c Fri Aug 21 07:09:53 2015 (r286982) @@ -38,6 +38,7 @@ static const char rcsid[] = #include #include #include +#include #include #include #include @@ -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++; Modified: head/usr.sbin/pw/tests/pw_useradd.sh ============================================================================== --- head/usr.sbin/pw/tests/pw_useradd.sh Fri Aug 21 06:30:13 2015 (r286981) +++ head/usr.sbin/pw/tests/pw_useradd.sh Fri Aug 21 07:09:53 2015 (r286982) @@ -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