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