]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
49e942b2 | 2 | |
49e942b2 | 3 | #include <errno.h> |
11c3a366 | 4 | #include <stdbool.h> |
cf0fbc49 | 5 | #include <string.h> |
11c3a366 | 6 | #include <sys/stat.h> |
49e942b2 | 7 | |
c31ad024 | 8 | #include "alloc-util.h" |
f4f15635 | 9 | #include "fs-util.h" |
93cc7779 | 10 | #include "macro.h" |
4ad49000 | 11 | #include "mkdir.h" |
f4f15635 | 12 | #include "path-util.h" |
8fcde012 | 13 | #include "stat-util.h" |
37c1d5e9 | 14 | #include "stdio-util.h" |
ee104e11 | 15 | #include "user-util.h" |
49e942b2 | 16 | |
d50b5839 | 17 | int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) { |
49e942b2 | 18 | struct stat st; |
8f2c2f20 | 19 | int r; |
49e942b2 | 20 | |
dae8b82e ZJS |
21 | assert(_mkdir != mkdir); |
22 | ||
8f2c2f20 LP |
23 | if (_mkdir(path, mode) >= 0) { |
24 | r = chmod_and_chown(path, mode, uid, gid); | |
25 | if (r < 0) | |
26 | return r; | |
27 | } | |
49e942b2 KS |
28 | |
29 | if (lstat(path, &st) < 0) | |
30 | return -errno; | |
31 | ||
d50b5839 | 32 | if ((flags & MKDIR_FOLLOW_SYMLINK) && S_ISLNK(st.st_mode)) { |
c31ad024 YW |
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) | |
d50b5839 ZJS |
39 | return mkdir_safe_internal(p, mode, uid, gid, |
40 | flags & ~MKDIR_FOLLOW_SYMLINK, | |
41 | _mkdir); | |
c31ad024 YW |
42 | |
43 | if (lstat(p, &st) < 0) | |
44 | return -errno; | |
45 | } | |
46 | ||
37c1d5e9 ZJS |
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 | } | |
0cb9fbcd LP |
52 | if ((st.st_mode & 0007) > (mode & 0007) || |
53 | (st.st_mode & 0070) > (mode & 0070) || | |
37c1d5e9 ZJS |
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); | |
2e6534a9 | 71 | return -EEXIST; |
37c1d5e9 | 72 | } |
49e942b2 KS |
73 | |
74 | return 0; | |
75 | } | |
76 | ||
dae8b82e ZJS |
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 | ||
d50b5839 ZJS |
83 | int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) { |
84 | return mkdir_safe_internal(path, mode, uid, gid, flags, mkdir_errno_wrapper); | |
c66e7f04 KS |
85 | } |
86 | ||
39bdfa31 | 87 | int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { |
49e942b2 | 88 | const char *p, *e; |
4ad49000 | 89 | int r; |
49e942b2 KS |
90 | |
91 | assert(path); | |
dae8b82e | 92 | assert(_mkdir != mkdir); |
49e942b2 | 93 | |
4ad49000 LP |
94 | if (prefix && !path_startswith(path, prefix)) |
95 | return -ENOTDIR; | |
96 | ||
9e13dbae KS |
97 | /* return immediately if directory exists */ |
98 | e = strrchr(path, '/'); | |
99 | if (!e) | |
100 | return -EINVAL; | |
4ad49000 LP |
101 | |
102 | if (e == path) | |
103 | return 0; | |
104 | ||
9e13dbae | 105 | p = strndupa(path, e - path); |
e73a03e0 | 106 | r = is_dir(p, true); |
4ad49000 LP |
107 | if (r > 0) |
108 | return 0; | |
109 | if (r == 0) | |
110 | return -ENOTDIR; | |
49e942b2 | 111 | |
9e13dbae | 112 | /* create every parent directory in the path, except the last component */ |
49e942b2 KS |
113 | p = path + strspn(path, "/"); |
114 | for (;;) { | |
4ad49000 | 115 | char t[strlen(path) + 1]; |
49e942b2 KS |
116 | |
117 | e = p + strcspn(p, "/"); | |
118 | p = e + strspn(e, "/"); | |
119 | ||
dae8b82e | 120 | /* Is this the last component? If so, then we're done */ |
49e942b2 KS |
121 | if (*p == 0) |
122 | return 0; | |
123 | ||
4ad49000 LP |
124 | memcpy(t, path, e - path); |
125 | t[e-path] = 0; | |
49e942b2 | 126 | |
4ad49000 LP |
127 | if (prefix && path_startswith(prefix, t)) |
128 | continue; | |
49e942b2 | 129 | |
39bdfa31 | 130 | r = _mkdir(t, mode); |
dae8b82e ZJS |
131 | if (r < 0 && r != -EEXIST) |
132 | return r; | |
49e942b2 KS |
133 | } |
134 | } | |
135 | ||
c66e7f04 | 136 | int mkdir_parents(const char *path, mode_t mode) { |
dae8b82e | 137 | return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper); |
5b585b53 ZJS |
138 | } |
139 | ||
39bdfa31 | 140 | int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { |
49e942b2 KS |
141 | int r; |
142 | ||
143 | /* Like mkdir -p */ | |
144 | ||
dae8b82e ZJS |
145 | assert(_mkdir != mkdir); |
146 | ||
39bdfa31 | 147 | r = mkdir_parents_internal(prefix, path, mode, _mkdir); |
c66e7f04 | 148 | if (r < 0) |
49e942b2 KS |
149 | return r; |
150 | ||
39bdfa31 | 151 | r = _mkdir(path, mode); |
dae8b82e ZJS |
152 | if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0)) |
153 | return r; | |
49e942b2 KS |
154 | |
155 | return 0; | |
156 | } | |
c66e7f04 KS |
157 | |
158 | int mkdir_p(const char *path, mode_t mode) { | |
dae8b82e | 159 | return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper); |
4ad49000 | 160 | } |