]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a1164ae3 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2017 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | ||
25 | #include "user-util.h" | |
26 | #include "macro.h" | |
27 | #include "fd-util.h" | |
28 | #include "dirent-util.h" | |
29 | #include "chown-recursive.h" | |
30 | ||
31 | static int chown_one(int fd, const char *name, const struct stat *st, uid_t uid, gid_t gid) { | |
32 | int r; | |
33 | ||
34 | assert(fd >= 0); | |
35 | assert(st); | |
36 | ||
37 | if ((!uid_is_valid(uid) || st->st_uid == uid) && | |
38 | (!gid_is_valid(gid) || st->st_gid == gid)) | |
39 | return 0; | |
40 | ||
41 | if (name) | |
42 | r = fchownat(fd, name, uid, gid, AT_SYMLINK_NOFOLLOW); | |
43 | else | |
44 | r = fchown(fd, uid, gid); | |
45 | if (r < 0) | |
46 | return -errno; | |
47 | ||
48 | /* The linux kernel alters the mode in some cases of chown(). Let's undo this. */ | |
49 | if (name) { | |
50 | if (!S_ISLNK(st->st_mode)) | |
51 | r = fchmodat(fd, name, st->st_mode, 0); | |
52 | else /* There's currently no AT_SYMLINK_NOFOLLOW for fchmodat() */ | |
53 | r = 0; | |
54 | } else | |
55 | r = fchmod(fd, st->st_mode); | |
56 | if (r < 0) | |
57 | return -errno; | |
58 | ||
59 | return 1; | |
60 | } | |
61 | ||
62 | static int chown_recursive_internal(int fd, const struct stat *st, uid_t uid, gid_t gid) { | |
63 | bool changed = false; | |
64 | int r; | |
65 | ||
66 | assert(fd >= 0); | |
67 | assert(st); | |
68 | ||
69 | if (S_ISDIR(st->st_mode)) { | |
70 | _cleanup_closedir_ DIR *d = NULL; | |
71 | struct dirent *de; | |
72 | ||
73 | d = fdopendir(fd); | |
74 | if (!d) { | |
75 | r = -errno; | |
76 | goto finish; | |
77 | } | |
78 | fd = -1; | |
79 | ||
80 | FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) { | |
81 | struct stat fst; | |
82 | ||
83 | if (dot_or_dot_dot(de->d_name)) | |
84 | continue; | |
85 | ||
86 | if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0) { | |
87 | r = -errno; | |
88 | goto finish; | |
89 | } | |
90 | ||
91 | if (S_ISDIR(fst.st_mode)) { | |
92 | int subdir_fd; | |
93 | ||
94 | subdir_fd = openat(dirfd(d), de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); | |
95 | if (subdir_fd < 0) { | |
96 | r = -errno; | |
97 | goto finish; | |
98 | } | |
99 | ||
100 | r = chown_recursive_internal(subdir_fd, &fst, uid, gid); | |
101 | if (r < 0) | |
102 | goto finish; | |
103 | if (r > 0) | |
104 | changed = true; | |
105 | } else { | |
106 | r = chown_one(dirfd(d), de->d_name, &fst, uid, gid); | |
107 | if (r < 0) | |
108 | goto finish; | |
109 | if (r > 0) | |
110 | changed = true; | |
111 | } | |
112 | } | |
113 | ||
114 | r = chown_one(dirfd(d), NULL, st, uid, gid); | |
115 | } else | |
116 | r = chown_one(fd, NULL, st, uid, gid); | |
117 | if (r < 0) | |
118 | goto finish; | |
119 | ||
120 | r = r > 0 || changed; | |
121 | ||
122 | finish: | |
123 | safe_close(fd); | |
124 | return r; | |
125 | } | |
126 | ||
127 | int path_chown_recursive(const char *path, uid_t uid, gid_t gid) { | |
128 | _cleanup_close_ int fd = -1; | |
129 | struct stat st; | |
130 | int r; | |
131 | ||
132 | fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); | |
133 | if (fd < 0) | |
134 | return -errno; | |
135 | ||
136 | if (!uid_is_valid(uid) && !gid_is_valid(gid)) | |
137 | return 0; /* nothing to do */ | |
138 | ||
139 | if (fstat(fd, &st) < 0) | |
140 | return -errno; | |
141 | ||
142 | /* Let's take a shortcut: if the top-level directory is properly owned, we don't descend into the whole tree, | |
143 | * under the assumption that all is OK anyway. */ | |
144 | ||
145 | if ((!uid_is_valid(uid) || st.st_uid == uid) && | |
146 | (!gid_is_valid(gid) || st.st_gid == gid)) | |
147 | return 0; | |
148 | ||
149 | r = chown_recursive_internal(fd, &st, uid, gid); | |
150 | fd = -1; /* we donated the fd to the call, regardless if it succeeded or failed */ | |
151 | ||
152 | return r; | |
153 | } |