blob: faa6f68ebe2066e97897ee1ad3db57f78a950e80 [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 Bishop19323692019-04-05 15:28:33 -04007 src/useradd.c | 80 +++++++++++++++++++++++++++++++++++++++--------------------
8 1 file changed, 53 insertions(+), 27 deletions(-)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
10diff --git a/src/useradd.c b/src/useradd.c
Brad Bishop19323692019-04-05 15:28:33 -040011index 00a3c30..9ecbb58 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 Bishop19323692019-04-05 15:28:33 -040050@@ -2038,39 +2067,36 @@ 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));
Brad Bishop19323692019-04-05 15:28:33 -040077- home_added = true;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078+ fail_exit (E_HOMEDIR);
79+ }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080080+ (void) chown (prefix_user_home, user_id, user_gid);
81+ chmod (prefix_user_home,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082+ 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083+ home_added = true;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080084 #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 Williamsc124f4f2015-09-15 14:41:29 -050089+ audit_logger (AUDIT_ADD_USER, Prog,
90+ "adding home directory",
91+ user_name, (unsigned int) user_id,
92+ SHADOW_AUDIT_SUCCESS);
93 #endif
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080094 #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 Williamsc124f4f2015-09-15 14:41:29 -0500103+ /* Reset SELinux to create files with default contexts */
104+ if (reset_selinux_file_context () != 0) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800105+ fprintf (stderr,
106+ _("%s: cannot reset SELinux file creation context\n"),
107+ Prog);
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108+ fail_exit (E_HOMEDIR);
109 }
110+#endif
111 }
112
113 /*
114--
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001152.11.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116