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