Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | Upstream-Status: Inappropriate [OE specific] |
| 2 | |
| 3 | Subject: useradd.c: create parent directories when necessary |
| 4 | |
| 5 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
| 6 | --- |
| 7 | src/useradd.c | 72 +++++++++++++++++++++++++++++++++++++++------------------ |
| 8 | 1 file changed, 49 insertions(+), 23 deletions(-) |
| 9 | |
| 10 | diff --git a/src/useradd.c b/src/useradd.c |
| 11 | index 4bd969d..cb5dd6c 100644 |
| 12 | --- a/src/useradd.c |
| 13 | +++ b/src/useradd.c |
| 14 | @@ -1893,6 +1893,35 @@ static void usr_update (void) |
| 15 | } |
| 16 | |
| 17 | /* |
| 18 | + * mkdir_p - create directories, including parent directories when needed |
| 19 | + * |
| 20 | + * similar to `mkdir -p' |
| 21 | + */ |
| 22 | +void mkdir_p(const char *path) { |
| 23 | + int len = strlen(path); |
| 24 | + char newdir[len + 1]; |
| 25 | + mode_t mode = 0755; |
| 26 | + int i = 0; |
| 27 | + |
| 28 | + if (path[i] == '\0') { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + /* skip the leading '/' */ |
| 33 | + i++; |
| 34 | + |
| 35 | + while(path[i] != '\0') { |
| 36 | + if (path[i] == '/') { |
| 37 | + strncpy(newdir, path, i); |
| 38 | + newdir[i] = '\0'; |
| 39 | + mkdir(newdir, mode); |
| 40 | + } |
| 41 | + i++; |
| 42 | + } |
| 43 | + mkdir(path, mode); |
| 44 | +} |
| 45 | + |
| 46 | +/* |
| 47 | * create_home - create the user's home directory |
| 48 | * |
| 49 | * create_home() creates the user's home directory if it does not |
| 50 | @@ -1907,36 +1936,33 @@ static void create_home (void) |
| 51 | fail_exit (E_HOMEDIR); |
| 52 | } |
| 53 | #endif |
| 54 | - /* XXX - create missing parent directories. --marekm */ |
| 55 | - if (mkdir (user_home, 0) != 0) { |
| 56 | - fprintf (stderr, |
| 57 | - _("%s: cannot create directory %s\n"), |
| 58 | - Prog, user_home); |
| 59 | -#ifdef WITH_AUDIT |
| 60 | - audit_logger (AUDIT_ADD_USER, Prog, |
| 61 | - "adding home directory", |
| 62 | - user_name, (unsigned int) user_id, |
| 63 | - SHADOW_AUDIT_FAILURE); |
| 64 | -#endif |
| 65 | - fail_exit (E_HOMEDIR); |
| 66 | - } |
| 67 | - chown (user_home, user_id, user_gid); |
| 68 | - chmod (user_home, |
| 69 | - 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK)); |
| 70 | - home_added = true; |
| 71 | + mkdir_p(user_home); |
| 72 | + } |
| 73 | + if (access (user_home, F_OK) != 0) { |
| 74 | #ifdef WITH_AUDIT |
| 75 | audit_logger (AUDIT_ADD_USER, Prog, |
| 76 | "adding home directory", |
| 77 | user_name, (unsigned int) user_id, |
| 78 | - SHADOW_AUDIT_SUCCESS); |
| 79 | + SHADOW_AUDIT_FAILURE); |
| 80 | #endif |
| 81 | -#ifdef WITH_SELINUX |
| 82 | - /* Reset SELinux to create files with default contexts */ |
| 83 | - if (reset_selinux_file_context () != 0) { |
| 84 | - fail_exit (E_HOMEDIR); |
| 85 | - } |
| 86 | + fail_exit (E_HOMEDIR); |
| 87 | + } |
| 88 | + chown (user_home, user_id, user_gid); |
| 89 | + chmod (user_home, |
| 90 | + 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK)); |
| 91 | + home_added = true; |
| 92 | +#ifdef WITH_AUDIT |
| 93 | + audit_logger (AUDIT_ADD_USER, Prog, |
| 94 | + "adding home directory", |
| 95 | + user_name, (unsigned int) user_id, |
| 96 | + SHADOW_AUDIT_SUCCESS); |
| 97 | #endif |
| 98 | +#ifdef WITH_SELINUX |
| 99 | + /* Reset SELinux to create files with default contexts */ |
| 100 | + if (reset_selinux_file_context () != 0) { |
| 101 | + fail_exit (E_HOMEDIR); |
| 102 | } |
| 103 | +#endif |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | -- |
| 108 | 1.7.9.5 |
| 109 | |