]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <stdbool.h> | |
5 | #include <string.h> | |
6 | #include <sys/stat.h> | |
7 | ||
8 | #include "alloc-util.h" | |
9 | #include "fs-util.h" | |
10 | #include "macro.h" | |
11 | #include "mkdir.h" | |
12 | #include "path-util.h" | |
13 | #include "stat-util.h" | |
14 | #include "stdio-util.h" | |
15 | #include "user-util.h" | |
16 | ||
17 | int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) { | |
18 | struct stat st; | |
19 | int r; | |
20 | ||
21 | assert(_mkdir != mkdir); | |
22 | ||
23 | if (_mkdir(path, mode) >= 0) { | |
24 | r = chmod_and_chown(path, mode, uid, gid); | |
25 | if (r < 0) | |
26 | return r; | |
27 | } | |
28 | ||
29 | if (lstat(path, &st) < 0) | |
30 | return -errno; | |
31 | ||
32 | if ((flags & MKDIR_FOLLOW_SYMLINK) && S_ISLNK(st.st_mode)) { | |
33 | _cleanup_free_ char *p = NULL; | |
34 | ||
35 | r = chase_symlinks(path, NULL, CHASE_NONEXISTENT, &p); | |
36 | if (r < 0) | |
37 | return r; | |
38 | if (r == 0) | |
39 | return mkdir_safe_internal(p, mode, uid, gid, | |
40 | flags & ~MKDIR_FOLLOW_SYMLINK, | |
41 | _mkdir); | |
42 | ||
43 | if (lstat(p, &st) < 0) | |
44 | return -errno; | |
45 | } | |
46 | ||
47 | if (!S_ISDIR(st.st_mode)) { | |
48 | log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, | |
49 | "Path \"%s\" already exists and is not a directory, refusing.", path); | |
50 | return -ENOTDIR; | |
51 | } | |
52 | if ((st.st_mode & 0007) > (mode & 0007) || | |
53 | (st.st_mode & 0070) > (mode & 0070) || | |
54 | (st.st_mode & 0700) > (mode & 0700)) { | |
55 | log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, | |
56 | "Directory \"%s\" already exists, but has mode %04o that is too permissive (%04o was requested), refusing.", | |
57 | path, st.st_mode & 0777, mode); | |
58 | return -EEXIST; | |
59 | } | |
60 | if ((uid != UID_INVALID && st.st_uid != uid) || | |
61 | (gid != GID_INVALID && st.st_gid != gid)) { | |
62 | char u[DECIMAL_STR_MAX(uid_t)] = "-", g[DECIMAL_STR_MAX(gid_t)] = "-"; | |
63 | ||
64 | if (uid != UID_INVALID) | |
65 | xsprintf(u, UID_FMT, uid); | |
66 | if (gid != UID_INVALID) | |
67 | xsprintf(g, GID_FMT, gid); | |
68 | log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, | |
69 | "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.", | |
70 | path, st.st_uid, st.st_gid, u, g); | |
71 | return -EEXIST; | |
72 | } | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | int mkdir_errno_wrapper(const char *pathname, mode_t mode) { | |
78 | if (mkdir(pathname, mode) < 0) | |
79 | return -errno; | |
80 | return 0; | |
81 | } | |
82 | ||
83 | int mkdirat_errno_wrapper(int dirfd, const char *pathname, mode_t mode) { | |
84 | if (mkdirat(dirfd, pathname, mode) < 0) | |
85 | return -errno; | |
86 | return 0; | |
87 | } | |
88 | ||
89 | int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) { | |
90 | return mkdir_safe_internal(path, mode, uid, gid, flags, mkdir_errno_wrapper); | |
91 | } | |
92 | ||
93 | int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { | |
94 | const char *p, *e; | |
95 | int r; | |
96 | ||
97 | assert(path); | |
98 | assert(_mkdir != mkdir); | |
99 | ||
100 | if (prefix && !path_startswith(path, prefix)) | |
101 | return -ENOTDIR; | |
102 | ||
103 | /* return immediately if directory exists */ | |
104 | e = strrchr(path, '/'); | |
105 | if (!e) | |
106 | return -EINVAL; | |
107 | ||
108 | if (e == path) | |
109 | return 0; | |
110 | ||
111 | p = strndupa(path, e - path); | |
112 | r = is_dir(p, true); | |
113 | if (r > 0) | |
114 | return 0; | |
115 | if (r == 0) | |
116 | return -ENOTDIR; | |
117 | ||
118 | /* create every parent directory in the path, except the last component */ | |
119 | p = path + strspn(path, "/"); | |
120 | for (;;) { | |
121 | char t[strlen(path) + 1]; | |
122 | ||
123 | e = p + strcspn(p, "/"); | |
124 | p = e + strspn(e, "/"); | |
125 | ||
126 | /* Is this the last component? If so, then we're done */ | |
127 | if (*p == 0) | |
128 | return 0; | |
129 | ||
130 | memcpy(t, path, e - path); | |
131 | t[e-path] = 0; | |
132 | ||
133 | if (prefix && path_startswith(prefix, t)) | |
134 | continue; | |
135 | ||
136 | r = _mkdir(t, mode); | |
137 | if (r < 0 && r != -EEXIST) | |
138 | return r; | |
139 | } | |
140 | } | |
141 | ||
142 | int mkdir_parents(const char *path, mode_t mode) { | |
143 | return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper); | |
144 | } | |
145 | ||
146 | int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { | |
147 | int r; | |
148 | ||
149 | /* Like mkdir -p */ | |
150 | ||
151 | assert(_mkdir != mkdir); | |
152 | ||
153 | r = mkdir_parents_internal(prefix, path, mode, _mkdir); | |
154 | if (r < 0) | |
155 | return r; | |
156 | ||
157 | r = _mkdir(path, mode); | |
158 | if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0)) | |
159 | return r; | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | int mkdir_p(const char *path, mode_t mode) { | |
165 | return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper); | |
166 | } |