chiark / gitweb /
build-sys: add one more Makefile symlink
[elogind.git] / src / udev / udev-builtin-uaccess.c
1 /*
2  * manage device node user ACL
3  *
4  * Copyright 2010-2012 Kay Sievers <kay@vrfy.org>
5  * Copyright 2010 Lennart Poettering
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24
25 #include "systemd/sd-login.h"
26 #include "logind-acl.h"
27 #include "udev.h"
28 #include "util.h"
29
30 static int builtin_uaccess(struct udev_device *dev, int argc, char *argv[], bool test) {
31         int r;
32         const char *path = NULL, *seat;
33         bool changed_acl = false;
34         uid_t uid;
35
36         umask(0022);
37
38         /* don't muck around with ACLs when the system is not running systemd */
39         if (!logind_running())
40                 return 0;
41
42         path = udev_device_get_devnode(dev);
43         seat = udev_device_get_property_value(dev, "ID_SEAT");
44         if (!seat)
45                 seat = "seat0";
46
47         r = sd_seat_get_active(seat, NULL, &uid);
48         if (r == -ENOENT) {
49                 /* No active session on this seat */
50                 r = 0;
51                 goto finish;
52         } else if (r < 0) {
53                 log_error("Failed to determine active user on seat %s.", seat);
54                 goto finish;
55         }
56
57         r = devnode_acl(path, true, false, 0, true, uid);
58         if (r < 0) {
59                 log_error_errno(r, "Failed to apply ACL on %s: %m", path);
60                 goto finish;
61         }
62
63         changed_acl = true;
64         r = 0;
65
66 finish:
67         if (path && !changed_acl) {
68                 int k;
69
70                 /* Better be safe than sorry and reset ACL */
71                 k = devnode_acl(path, true, false, 0, false, 0);
72                 if (k < 0) {
73                         log_error_errno(k, "Failed to apply ACL on %s: %m", path);
74                         if (r >= 0)
75                                 r = k;
76                 }
77         }
78
79         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
80 }
81
82 const struct udev_builtin udev_builtin_uaccess = {
83         .name = "uaccess",
84         .cmd = builtin_uaccess,
85         .help = "Manage device node user ACL",
86 };