aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-extended/shadow/files/0001-useradd.c-create-parent-directories-when-necessary.patch
blob: faa6f68ebe2066e97897ee1ad3db57f78a950e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Subject: [PATCH] useradd.c: create parent directories when necessary

Upstream-Status: Inappropriate [OE specific]

Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
---
 src/useradd.c | 80 +++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 53 insertions(+), 27 deletions(-)

diff --git a/src/useradd.c b/src/useradd.c
index 00a3c30..9ecbb58 100644
--- a/src/useradd.c
+++ b/src/useradd.c
@@ -2021,6 +2021,35 @@ static void usr_update (void)
 }
 
 /*
+ * mkdir_p - create directories, including parent directories when needed
+ *
+ * similar to `mkdir -p'
+ */
+void mkdir_p(const char *path) {
+	int len = strlen(path);
+	char newdir[len + 1];
+	mode_t mode = 0755;
+	int i = 0;
+
+	if (path[i] == '\0') {
+		return;
+	}
+
+	/* skip the leading '/' */
+	i++;
+
+	while(path[i] != '\0') {
+		if (path[i] == '/') {
+			strncpy(newdir, path, i);
+			newdir[i] = '\0';
+			mkdir(newdir, mode);
+		}
+		i++;
+	}
+	mkdir(path, mode);
+}
+
+/*
  * create_home - create the user's home directory
  *
  *	create_home() creates the user's home directory if it does not
@@ -2038,39 +2067,36 @@ static void create_home (void)
 			fail_exit (E_HOMEDIR);
 		}
 #endif
-		/* XXX - create missing parent directories.  --marekm */
-		if (mkdir (prefix_user_home, 0) != 0) {
-			fprintf (stderr,
-			         _("%s: cannot create directory %s\n"),
-			         Prog, prefix_user_home);
+		mkdir_p(user_home);
+	}
+	if (access (prefix_user_home, F_OK) != 0) {
 #ifdef WITH_AUDIT
-			audit_logger (AUDIT_ADD_USER, Prog,
-			              "adding home directory",
-			              user_name, (unsigned int) user_id,
-			              SHADOW_AUDIT_FAILURE);
+		audit_logger (AUDIT_ADD_USER, Prog,
+			      "adding home directory",
+			      user_name, (unsigned int) user_id,
+			      SHADOW_AUDIT_FAILURE);
 #endif
-			fail_exit (E_HOMEDIR);
-		}
-		(void) chown (prefix_user_home, user_id, user_gid);
-		chmod (prefix_user_home,
-		       0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
-		home_added = true;
+		fail_exit (E_HOMEDIR);
+	}
+	(void) chown (prefix_user_home, user_id, user_gid);
+	chmod (prefix_user_home,
+	       0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
+	home_added = true;
 #ifdef WITH_AUDIT
-		audit_logger (AUDIT_ADD_USER, Prog,
-		              "adding home directory",
-		              user_name, (unsigned int) user_id,
-		              SHADOW_AUDIT_SUCCESS);
+	audit_logger (AUDIT_ADD_USER, Prog,
+		      "adding home directory",
+		      user_name, (unsigned int) user_id,
+		      SHADOW_AUDIT_SUCCESS);
 #endif
 #ifdef WITH_SELINUX
-		/* Reset SELinux to create files with default contexts */
-		if (reset_selinux_file_context () != 0) {
-			fprintf (stderr,
-			         _("%s: cannot reset SELinux file creation context\n"),
-			         Prog);
-			fail_exit (E_HOMEDIR);
-		}
-#endif
+	/* Reset SELinux to create files with default contexts */
+	if (reset_selinux_file_context () != 0) {
+		fprintf (stderr,
+			 _("%s: cannot reset SELinux file creation context\n"),
+			 Prog);
+		fail_exit (E_HOMEDIR);
 	}
+#endif
 }
 
 /*
-- 
2.11.0