blob: 702413659386f3684fdc570cd3558097ec66bfdb [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001Subject: [PATCH] useradd.c: create parent directories when necessary
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08003Upstream-Status: Inappropriate [OE specific]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004
5Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
6---
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08007 src/useradd.c | 82 +++++++++++++++++++++++++++++++++++++++--------------------
8 1 file changed, 54 insertions(+), 28 deletions(-)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
10diff --git a/src/useradd.c b/src/useradd.c
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080011index 7214e72..3aaf45c 100644
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012--- a/src/useradd.c
13+++ b/src/useradd.c
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080014@@ -2021,6 +2021,35 @@ static void usr_update (void)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015 }
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 Bishop1a4b7ee2018-12-16 17:11:34 -080050@@ -2038,42 +2067,39 @@ static void create_home (void)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 fail_exit (E_HOMEDIR);
52 }
53 #endif
54- /* XXX - create missing parent directories. --marekm */
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080055- if (mkdir (prefix_user_home, 0) != 0) {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056- fprintf (stderr,
57- _("%s: cannot create directory %s\n"),
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080058- Prog, prefix_user_home);
59+ mkdir_p(user_home);
60+ }
61+ if (access (prefix_user_home, F_OK) != 0) {
62 #ifdef WITH_AUDIT
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063- audit_logger (AUDIT_ADD_USER, Prog,
64- "adding home directory",
65- user_name, (unsigned int) user_id,
66- SHADOW_AUDIT_FAILURE);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080067+ audit_logger (AUDIT_ADD_USER, Prog,
68+ "adding home directory",
69+ user_name, (unsigned int) user_id,
70+ SHADOW_AUDIT_FAILURE);
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 #endif
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072- fail_exit (E_HOMEDIR);
73- }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080074- (void) chown (prefix_user_home, user_id, user_gid);
75- chmod (prefix_user_home,
76- 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077+ fail_exit (E_HOMEDIR);
78+ }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080079+ (void) chown (prefix_user_home, user_id, user_gid);
80+ chmod (prefix_user_home,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081+ 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080082 #ifdef WITH_ATTR
83- attr_copy_file (def_template, user_home, NULL, NULL);
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084+ attr_copy_file (def_template, user_home, NULL, NULL);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080085 #endif
86- home_added = true;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087+ home_added = true;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080088 #ifdef WITH_AUDIT
89- audit_logger (AUDIT_ADD_USER, Prog,
90- "adding home directory",
91- user_name, (unsigned int) user_id,
92- SHADOW_AUDIT_SUCCESS);
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093+ audit_logger (AUDIT_ADD_USER, Prog,
94+ "adding home directory",
95+ user_name, (unsigned int) user_id,
96+ SHADOW_AUDIT_SUCCESS);
97 #endif
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080098 #ifdef WITH_SELINUX
99- /* Reset SELinux to create files with default contexts */
100- if (reset_selinux_file_context () != 0) {
101- fprintf (stderr,
102- _("%s: cannot reset SELinux file creation context\n"),
103- Prog);
104- fail_exit (E_HOMEDIR);
105- }
106-#endif
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107+ /* Reset SELinux to create files with default contexts */
108+ if (reset_selinux_file_context () != 0) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800109+ fprintf (stderr,
110+ _("%s: cannot reset SELinux file creation context\n"),
111+ Prog);
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112+ fail_exit (E_HOMEDIR);
113 }
114+#endif
115 }
116
117 /*
118--
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001192.11.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120