Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1 | Subject: [PATCH] useradd.c: create parent directories when necessary |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 3 | Upstream-Status: Inappropriate [OE specific] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | |
| 5 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
| 6 | --- |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 7 | src/useradd.c | 80 +++++++++++++++++++++++++++++++++++++++-------------------- |
| 8 | 1 file changed, 53 insertions(+), 27 deletions(-) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
| 10 | diff --git a/src/useradd.c b/src/useradd.c |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 11 | index 00a3c30..9ecbb58 100644 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | --- a/src/useradd.c |
| 13 | +++ b/src/useradd.c |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 14 | @@ -2021,6 +2021,35 @@ static void usr_update (void) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 50 | @@ -2038,39 +2067,36 @@ static void create_home (void) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | fail_exit (E_HOMEDIR); |
| 52 | } |
| 53 | #endif |
| 54 | - /* XXX - create missing parent directories. --marekm */ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 55 | - if (mkdir (prefix_user_home, 0) != 0) { |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | - fprintf (stderr, |
| 57 | - _("%s: cannot create directory %s\n"), |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 58 | - Prog, prefix_user_home); |
| 59 | + mkdir_p(user_home); |
| 60 | + } |
| 61 | + if (access (prefix_user_home, F_OK) != 0) { |
| 62 | #ifdef WITH_AUDIT |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | - audit_logger (AUDIT_ADD_USER, Prog, |
| 64 | - "adding home directory", |
| 65 | - user_name, (unsigned int) user_id, |
| 66 | - SHADOW_AUDIT_FAILURE); |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 67 | + audit_logger (AUDIT_ADD_USER, Prog, |
| 68 | + "adding home directory", |
| 69 | + user_name, (unsigned int) user_id, |
| 70 | + SHADOW_AUDIT_FAILURE); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | #endif |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | - fail_exit (E_HOMEDIR); |
| 73 | - } |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 74 | - (void) chown (prefix_user_home, user_id, user_gid); |
| 75 | - chmod (prefix_user_home, |
| 76 | - 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK)); |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 77 | - home_added = true; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | + fail_exit (E_HOMEDIR); |
| 79 | + } |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 80 | + (void) chown (prefix_user_home, user_id, user_gid); |
| 81 | + chmod (prefix_user_home, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | + 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK)); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | + home_added = true; |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 84 | #ifdef WITH_AUDIT |
| 85 | - audit_logger (AUDIT_ADD_USER, Prog, |
| 86 | - "adding home directory", |
| 87 | - user_name, (unsigned int) user_id, |
| 88 | - SHADOW_AUDIT_SUCCESS); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | + audit_logger (AUDIT_ADD_USER, Prog, |
| 90 | + "adding home directory", |
| 91 | + user_name, (unsigned int) user_id, |
| 92 | + SHADOW_AUDIT_SUCCESS); |
| 93 | #endif |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 94 | #ifdef WITH_SELINUX |
| 95 | - /* Reset SELinux to create files with default contexts */ |
| 96 | - if (reset_selinux_file_context () != 0) { |
| 97 | - fprintf (stderr, |
| 98 | - _("%s: cannot reset SELinux file creation context\n"), |
| 99 | - Prog); |
| 100 | - fail_exit (E_HOMEDIR); |
| 101 | - } |
| 102 | -#endif |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | + /* Reset SELinux to create files with default contexts */ |
| 104 | + if (reset_selinux_file_context () != 0) { |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 105 | + fprintf (stderr, |
| 106 | + _("%s: cannot reset SELinux file creation context\n"), |
| 107 | + Prog); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | + fail_exit (E_HOMEDIR); |
| 109 | } |
| 110 | +#endif |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | -- |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 115 | 2.11.0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |