chiark / gitweb /
remove unused includes
[elogind.git] / src / udev / udev-node.c
1 /*
2  * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <string.h>
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27
28 #include "udev.h"
29 #include "smack-util.h"
30 #include "selinux-util.h"
31
32 static int node_symlink(struct udev_device *dev, const char *node, const char *slink) {
33         struct stat stats;
34         char target[UTIL_PATH_SIZE];
35         char *s;
36         size_t l;
37         char slink_tmp[UTIL_PATH_SIZE + 32];
38         int i = 0;
39         int tail = 0;
40         int err = 0;
41
42         /* use relative link */
43         target[0] = '\0';
44         while (node[i] && (node[i] == slink[i])) {
45                 if (node[i] == '/')
46                         tail = i+1;
47                 i++;
48         }
49         s = target;
50         l = sizeof(target);
51         while (slink[i] != '\0') {
52                 if (slink[i] == '/')
53                         l = strpcpy(&s, l, "../");
54                 i++;
55         }
56         l = strscpy(s, l, &node[tail]);
57         if (l == 0) {
58                 err = -EINVAL;
59                 goto exit;
60         }
61
62         /* preserve link with correct target, do not replace node of other device */
63         if (lstat(slink, &stats) == 0) {
64                 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
65                         log_error("conflicting device node '%s' found, link to '%s' will not be created", slink, node);
66                         goto exit;
67                 } else if (S_ISLNK(stats.st_mode)) {
68                         char buf[UTIL_PATH_SIZE];
69                         int len;
70
71                         len = readlink(slink, buf, sizeof(buf));
72                         if (len > 0 && len < (int)sizeof(buf)) {
73                                 buf[len] = '\0';
74                                 if (streq(target, buf)) {
75                                         log_debug("preserve already existing symlink '%s' to '%s'", slink, target);
76                                         label_fix(slink, true, false);
77                                         utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
78                                         goto exit;
79                                 }
80                         }
81                 }
82         } else {
83                 log_debug("creating symlink '%s' to '%s'", slink, target);
84                 do {
85                         err = mkdir_parents_label(slink, 0755);
86                         if (err != 0 && err != -ENOENT)
87                                 break;
88                         mac_selinux_create_file_prepare(slink, S_IFLNK);
89                         err = symlink(target, slink);
90                         if (err != 0)
91                                 err = -errno;
92                         mac_selinux_create_file_clear();
93                 } while (err == -ENOENT);
94                 if (err == 0)
95                         goto exit;
96         }
97
98         log_debug("atomically replace '%s'", slink);
99         strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
100         unlink(slink_tmp);
101         do {
102                 err = mkdir_parents_label(slink_tmp, 0755);
103                 if (err != 0 && err != -ENOENT)
104                         break;
105                 mac_selinux_create_file_prepare(slink_tmp, S_IFLNK);
106                 err = symlink(target, slink_tmp);
107                 if (err != 0)
108                         err = -errno;
109                 mac_selinux_create_file_clear();
110         } while (err == -ENOENT);
111         if (err != 0) {
112                 log_error_errno(errno, "symlink '%s' '%s' failed: %m", target, slink_tmp);
113                 goto exit;
114         }
115         err = rename(slink_tmp, slink);
116         if (err != 0) {
117                 log_error_errno(errno, "rename '%s' '%s' failed: %m", slink_tmp, slink);
118                 unlink(slink_tmp);
119         }
120 exit:
121         return err;
122 }
123
124 /* find device node of device with highest priority */
125 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
126         struct udev *udev = udev_device_get_udev(dev);
127         DIR *dir;
128         int priority = 0;
129         const char *target = NULL;
130
131         if (add) {
132                 priority = udev_device_get_devlink_priority(dev);
133                 strscpy(buf, bufsize, udev_device_get_devnode(dev));
134                 target = buf;
135         }
136
137         dir = opendir(stackdir);
138         if (dir == NULL)
139                 return target;
140         for (;;) {
141                 struct udev_device *dev_db;
142                 struct dirent *dent;
143
144                 dent = readdir(dir);
145                 if (dent == NULL || dent->d_name[0] == '\0')
146                         break;
147                 if (dent->d_name[0] == '.')
148                         continue;
149
150                 log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
151
152                 /* did we find ourself? */
153                 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
154                         continue;
155
156                 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
157                 if (dev_db != NULL) {
158                         const char *devnode;
159
160                         devnode = udev_device_get_devnode(dev_db);
161                         if (devnode != NULL) {
162                                 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
163                                         log_debug("'%s' claims priority %i for '%s'",
164                                                   udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
165                                         priority = udev_device_get_devlink_priority(dev_db);
166                                         strscpy(buf, bufsize, devnode);
167                                         target = buf;
168                                 }
169                         }
170                         udev_device_unref(dev_db);
171                 }
172         }
173         closedir(dir);
174         return target;
175 }
176
177 /* manage "stack of names" with possibly specified device priorities */
178 static void link_update(struct udev_device *dev, const char *slink, bool add) {
179         char name_enc[UTIL_PATH_SIZE];
180         char filename[UTIL_PATH_SIZE * 2];
181         char dirname[UTIL_PATH_SIZE];
182         const char *target;
183         char buf[UTIL_PATH_SIZE];
184
185         util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
186         strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
187         strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
188
189         if (!add && unlink(filename) == 0)
190                 rmdir(dirname);
191
192         target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
193         if (target == NULL) {
194                 log_debug("no reference left, remove '%s'", slink);
195                 if (unlink(slink) == 0)
196                         rmdir_parents(slink, "/");
197         } else {
198                 log_debug("creating link '%s' to '%s'", slink, target);
199                 node_symlink(dev, target, slink);
200         }
201
202         if (add) {
203                 int err;
204
205                 do {
206                         int fd;
207
208                         err = mkdir_parents(filename, 0755);
209                         if (err != 0 && err != -ENOENT)
210                                 break;
211                         fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
212                         if (fd >= 0)
213                                 close(fd);
214                         else
215                                 err = -errno;
216                 } while (err == -ENOENT);
217         }
218 }
219
220 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) {
221         struct udev_list_entry *list_entry;
222
223         /* update possible left-over symlinks */
224         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
225                 const char *name = udev_list_entry_get_name(list_entry);
226                 struct udev_list_entry *list_entry_current;
227                 int found;
228
229                 /* check if old link name still belongs to this device */
230                 found = 0;
231                 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
232                         const char *name_current = udev_list_entry_get_name(list_entry_current);
233
234                         if (streq(name, name_current)) {
235                                 found = 1;
236                                 break;
237                         }
238                 }
239                 if (found)
240                         continue;
241
242                 log_debug("update old name, '%s' no longer belonging to '%s'",
243                      name, udev_device_get_devpath(dev));
244                 link_update(dev, name, false);
245         }
246 }
247
248 static int node_permissions_apply(struct udev_device *dev, bool apply,
249                                   mode_t mode, uid_t uid, gid_t gid,
250                                   struct udev_list *seclabel_list) {
251         const char *devnode = udev_device_get_devnode(dev);
252         dev_t devnum = udev_device_get_devnum(dev);
253         struct stat stats;
254         struct udev_list_entry *entry;
255         int err = 0;
256
257         if (streq(udev_device_get_subsystem(dev), "block"))
258                 mode |= S_IFBLK;
259         else
260                 mode |= S_IFCHR;
261
262         if (lstat(devnode, &stats) != 0) {
263                 err = -errno;
264                 log_debug_errno(errno, "can not stat() node '%s' (%m)", devnode);
265                 goto out;
266         }
267
268         if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
269                 err = -EEXIST;
270                 log_debug("found node '%s' with non-matching devnum %s, skip handling",
271                           udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
272                 goto out;
273         }
274
275         if (apply) {
276                 bool selinux = false;
277                 bool smack = false;
278
279                 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
280                         log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
281                         err = chmod(devnode, mode);
282                         if (err < 0)
283                                 log_warning_errno(errno, "setting mode of %s to %#o failed: %m", devnode, mode);
284                         err = chown(devnode, uid, gid);
285                         if (err < 0)
286                                 log_warning_errno(errno, "setting owner of %s to uid=%u, gid=%u failed: %m", devnode, uid, gid);
287                 } else {
288                         log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
289                 }
290
291                 /* apply SECLABEL{$module}=$label */
292                 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
293                         const char *name, *label;
294                         int r;
295
296                         name = udev_list_entry_get_name(entry);
297                         label = udev_list_entry_get_value(entry);
298
299                         if (streq(name, "selinux")) {
300                                 selinux = true;
301
302                                 r = mac_selinux_apply(devnode, label);
303                                 if (r < 0)
304                                         log_error_errno(r, "SECLABEL: failed to set SELinux label '%s': %m", label);
305                                 else
306                                         log_debug("SECLABEL: set SELinux label '%s'", label);
307
308                         } else if (streq(name, "smack")) {
309                                 smack = true;
310
311                                 r = mac_smack_apply(devnode, label);
312                                 if (r < 0)
313                                         log_error_errno(r, "SECLABEL: failed to set SMACK label '%s': %m", label);
314                                 else
315                                         log_debug("SECLABEL: set SMACK label '%s'", label);
316
317                         } else
318                                 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
319                 }
320
321                 /* set the defaults */
322                 if (!selinux)
323                         mac_selinux_fix(devnode, true, false);
324                 if (!smack)
325                         mac_smack_apply(devnode, NULL);
326         }
327
328         /* always update timestamp when we re-use the node, like on media change events */
329         utimensat(AT_FDCWD, devnode, NULL, 0);
330 out:
331         return err;
332 }
333
334 void udev_node_add(struct udev_device *dev, bool apply,
335                    mode_t mode, uid_t uid, gid_t gid,
336                    struct udev_list *seclabel_list) {
337         char filename[UTIL_PATH_SIZE];
338         struct udev_list_entry *list_entry;
339
340         log_debug("handling device node '%s', devnum=%s, mode=%#o, uid="UID_FMT", gid="GID_FMT,
341                   udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
342
343         if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
344                 return;
345
346         /* always add /dev/{block,char}/$major:$minor */
347         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
348                  streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
349                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
350         node_symlink(dev, udev_device_get_devnode(dev), filename);
351
352         /* create/update symlinks, add symlinks to name index */
353         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
354                         link_update(dev, udev_list_entry_get_name(list_entry), true);
355 }
356
357 void udev_node_remove(struct udev_device *dev) {
358         struct udev_list_entry *list_entry;
359         char filename[UTIL_PATH_SIZE];
360
361         /* remove/update symlinks, remove symlinks from name index */
362         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
363                 link_update(dev, udev_list_entry_get_name(list_entry), false);
364
365         /* remove /dev/{block,char}/$major:$minor */
366         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
367                  streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
368                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
369         unlink(filename);
370 }