Add support for skeleton files from /usr/etc/skel

This patch is used by openSUSE to make useradd look for
skeleton files in /usr/etc/skel additionally to /etc/skel
in accordance with
https://uapi-group.org/specifications/specs/base_directory_specification/
This commit is contained in:
Michael Vetter 2022-11-09 14:41:31 +01:00 committed by Serge Hallyn
parent 37412f505e
commit 74c17c7167
2 changed files with 55 additions and 0 deletions

View File

@ -448,6 +448,14 @@ static int copy_entry (const struct path_info *src, const struct path_info *dst,
old_uid, new_uid, old_gid, new_gid); old_uid, new_uid, old_gid, new_gid);
} }
/*
* If the destination already exists do nothing.
* This is after the copy_dir above to still iterate into subdirectories.
*/
if (fstatat(dst->dirfd, dst->name, &sb, AT_SYMLINK_NOFOLLOW) != -1) {
return 0;
}
/* /*
* Copy any symbolic links * Copy any symbolic links
*/ */
@ -507,6 +515,7 @@ static int copy_dir (const struct path_info *src, const struct path_info *dst,
gid_t old_gid, gid_t new_gid) gid_t old_gid, gid_t new_gid)
{ {
int err = 0; int err = 0;
struct stat dst_sb;
/* /*
* Create a new target directory, make it owned by * Create a new target directory, make it owned by
@ -518,6 +527,15 @@ static int copy_dir (const struct path_info *src, const struct path_info *dst,
return -1; return -1;
} }
#endif /* WITH_SELINUX */ #endif /* WITH_SELINUX */
/*
* If the destination is already a directory, don't change it
* but copy into it (recursively).
*/
if (fstatat(dst->dirfd, dst->name, &dst_sb, AT_SYMLINK_NOFOLLOW) == 0 && S_ISDIR(dst_sb.st_mode)) {
return (copy_tree (src, dst, false, reset_selinux,
old_uid, new_uid, old_gid, new_gid) != 0);
}
if ( (mkdirat (dst->dirfd, dst->name, 0700) != 0) if ( (mkdirat (dst->dirfd, dst->name, 0700) != 0)
|| (chownat_if_needed (dst, statp, || (chownat_if_needed (dst, statp,
old_uid, new_uid, old_gid, new_gid) != 0) old_uid, new_uid, old_gid, new_gid) != 0)

View File

@ -61,6 +61,9 @@
#ifndef SKEL_DIR #ifndef SKEL_DIR
#define SKEL_DIR "/etc/skel" #define SKEL_DIR "/etc/skel"
#endif #endif
#ifndef USRSKELDIR
#define USRSKELDIR "/usr/etc/skel"
#endif
#ifndef USER_DEFAULTS_FILE #ifndef USER_DEFAULTS_FILE
#define USER_DEFAULTS_FILE "/etc/default/useradd" #define USER_DEFAULTS_FILE "/etc/default/useradd"
#define NEW_USER_FILE "/etc/default/nuaddXXXXXX" #define NEW_USER_FILE "/etc/default/nuaddXXXXXX"
@ -85,6 +88,7 @@ static const char *def_gname = "other";
static const char *def_home = "/home"; static const char *def_home = "/home";
static const char *def_shell = "/bin/bash"; static const char *def_shell = "/bin/bash";
static const char *def_template = SKEL_DIR; static const char *def_template = SKEL_DIR;
static const char *def_usrtemplate = USRSKELDIR;
static const char *def_create_mail_spool = "yes"; static const char *def_create_mail_spool = "yes";
static const char *def_log_init = "yes"; static const char *def_log_init = "yes";
@ -190,6 +194,7 @@ static bool home_added = false;
#define DINACT "INACTIVE=" #define DINACT "INACTIVE="
#define DEXPIRE "EXPIRE=" #define DEXPIRE "EXPIRE="
#define DSKEL "SKEL=" #define DSKEL "SKEL="
#define DUSRSKEL "USRSKEL="
#define DCREATE_MAIL_SPOOL "CREATE_MAIL_SPOOL=" #define DCREATE_MAIL_SPOOL "CREATE_MAIL_SPOOL="
#define DLOG_INIT "LOG_INIT=" #define DLOG_INIT "LOG_INIT="
@ -473,6 +478,29 @@ static void get_defaults (void)
} }
} }
/*
* Default Usr Skeleton information
*/
else if (MATCH (buf, DUSRSKEL)) {
if ('\0' == *cp) {
cp = USRSKELDIR; /* XXX warning: const */
}
if(prefix[0]) {
size_t len;
int wlen;
char* _def_usrtemplate; /* avoid const warning */
len = strlen(prefix) + strlen(cp) + 2;
_def_usrtemplate = xmalloc(len);
wlen = snprintf(_def_usrtemplate, len, "%s/%s", prefix, cp);
assert (wlen == (int) len -1);
def_usrtemplate = _def_usrtemplate;
}
else {
def_usrtemplate = xstrdup (cp);
}
}
/* /*
* Create by default user mail spool or not ? * Create by default user mail spool or not ?
*/ */
@ -516,6 +544,7 @@ static void show_defaults (void)
printf ("EXPIRE=%s\n", def_expire); printf ("EXPIRE=%s\n", def_expire);
printf ("SHELL=%s\n", def_shell); printf ("SHELL=%s\n", def_shell);
printf ("SKEL=%s\n", def_template); printf ("SKEL=%s\n", def_template);
printf ("USRSKEL=%s\n", def_usrtemplate);
printf ("CREATE_MAIL_SPOOL=%s\n", def_create_mail_spool); printf ("CREATE_MAIL_SPOOL=%s\n", def_create_mail_spool);
printf ("LOG_INIT=%s\n", def_log_init); printf ("LOG_INIT=%s\n", def_log_init);
} }
@ -545,6 +574,7 @@ static int set_defaults (void)
bool out_expire = false; bool out_expire = false;
bool out_shell = false; bool out_shell = false;
bool out_skel = false; bool out_skel = false;
bool out_usrskel = false;
bool out_create_mail_spool = false; bool out_create_mail_spool = false;
bool out_log_init = false; bool out_log_init = false;
size_t len; size_t len;
@ -661,6 +691,9 @@ static int set_defaults (void)
} else if (!out_skel && MATCH (buf, DSKEL)) { } else if (!out_skel && MATCH (buf, DSKEL)) {
fprintf (ofp, DSKEL "%s\n", def_template); fprintf (ofp, DSKEL "%s\n", def_template);
out_skel = true; out_skel = true;
} else if (!out_usrskel && MATCH (buf, DUSRSKEL)) {
fprintf (ofp, DUSRSKEL "%s\n", def_usrtemplate);
out_usrskel = true;
} else if (!out_create_mail_spool } else if (!out_create_mail_spool
&& MATCH (buf, DCREATE_MAIL_SPOOL)) { && MATCH (buf, DCREATE_MAIL_SPOOL)) {
fprintf (ofp, fprintf (ofp,
@ -698,6 +731,8 @@ static int set_defaults (void)
fprintf (ofp, DSHELL "%s\n", def_shell); fprintf (ofp, DSHELL "%s\n", def_shell);
if (!out_skel) if (!out_skel)
fprintf (ofp, DSKEL "%s\n", def_template); fprintf (ofp, DSKEL "%s\n", def_template);
if (!out_usrskel)
fprintf (ofp, DUSRSKEL "%s\n", def_usrtemplate);
if (!out_create_mail_spool) if (!out_create_mail_spool)
fprintf (ofp, DCREATE_MAIL_SPOOL "%s\n", def_create_mail_spool); fprintf (ofp, DCREATE_MAIL_SPOOL "%s\n", def_create_mail_spool);
@ -2745,6 +2780,8 @@ int main (int argc, char **argv)
if (home_added) { if (home_added) {
copy_tree (def_template, prefix_user_home, false, true, copy_tree (def_template, prefix_user_home, false, true,
(uid_t)-1, user_id, (gid_t)-1, user_gid); (uid_t)-1, user_id, (gid_t)-1, user_gid);
copy_tree (def_usrtemplate, prefix_user_home, false, false,
(uid_t)-1, user_id, (gid_t)-1, user_gid);
} else { } else {
fprintf (stderr, fprintf (stderr,
_("%s: warning: the home directory %s already exists.\n" _("%s: warning: the home directory %s already exists.\n"