]>
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 | ||
1e912631 FB |
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 | ||
d50b5839 ZJS |
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); | |
c66e7f04 KS |
91 | } |
92 | ||
39bdfa31 | 93 | int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { |
49e942b2 | 94 | const char *p, *e; |
4ad49000 | 95 | int r; |
49e942b2 KS |
96 | |
97 | assert(path); | |
dae8b82e | 98 | assert(_mkdir != mkdir); |
49e942b2 | 99 | |
4ad49000 LP |
100 | if (prefix && !path_startswith(path, prefix)) |
101 | return -ENOTDIR; | |
102 | ||
9e13dbae KS |
103 | /* return immediately if directory exists */ |
104 | e = strrchr(path, '/'); | |
105 | if (!e) | |
106 | return -EINVAL; | |
4ad49000 LP |
107 | |
108 | if (e == path) | |
109 | return 0; | |
110 | ||
9e13dbae | 111 | p = strndupa(path, e - path); |
e73a03e0 | 112 | r = is_dir(p, true); |
4ad49000 LP |
113 | if (r > 0) |
114 | return 0; | |
115 | if (r == 0) | |
116 | return -ENOTDIR; | |
49e942b2 | 117 | |
9e13dbae | 118 | /* create every parent directory in the path, except the last component */ |
49e942b2 KS |
119 | p = path + strspn(path, "/"); |
120 | for (;;) { | |
4ad49000 | 121 | char t[strlen(path) + 1]; |
49e942b2 KS |
122 | |
123 | e = p + strcspn(p, "/"); | |
124 | p = e + strspn(e, "/"); | |
125 | ||
dae8b82e | 126 | /* Is this the last component? If so, then we're done */ |
49e942b2 KS |
127 | if (*p == 0) |
128 | return 0; | |
129 | ||
4ad49000 LP |
130 | memcpy(t, path, e - path); |
131 | t[e-path] = 0; | |
49e942b2 | 132 | |
4ad49000 LP |
133 | if (prefix && path_startswith(prefix, t)) |
134 | continue; | |
49e942b2 | 135 | |
39bdfa31 | 136 | r = _mkdir(t, mode); |
dae8b82e ZJS |
137 | if (r < 0 && r != -EEXIST) |
138 | return r; | |
49e942b2 KS |
139 | } |
140 | } | |
141 | ||
c66e7f04 | 142 | int mkdir_parents(const char *path, mode_t mode) { |
dae8b82e | 143 | return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper); |
5b585b53 ZJS |
144 | } |
145 | ||
39bdfa31 | 146 | int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { |
49e942b2 KS |
147 | int r; |
148 | ||
149 | /* Like mkdir -p */ | |
150 | ||
dae8b82e ZJS |
151 | assert(_mkdir != mkdir); |
152 | ||
39bdfa31 | 153 | r = mkdir_parents_internal(prefix, path, mode, _mkdir); |
c66e7f04 | 154 | if (r < 0) |
49e942b2 KS |
155 | return r; |
156 | ||
39bdfa31 | 157 | r = _mkdir(path, mode); |
dae8b82e ZJS |
158 | if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0)) |
159 | return r; | |
49e942b2 KS |
160 | |
161 | return 0; | |
162 | } | |
c66e7f04 KS |
163 | |
164 | int mkdir_p(const char *path, mode_t mode) { | |
dae8b82e | 165 | return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper); |
4ad49000 | 166 | } |