2 * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details:
27 #include <acl/libacl.h>
32 static int set_facl(const char* filename, uid_t uid, int add)
36 acl_entry_t entry = NULL;
38 acl_permset_t permset;
41 /* read current record */
42 acl = acl_get_file(filename, ACL_TYPE_ACCESS);
46 /* locate ACL_USER entry for uid */
47 get = acl_get_entry(acl, ACL_FIRST_ENTRY, &e);
51 acl_get_tag_type(e, &t);
55 u = (uid_t*)acl_get_qualifier(e);
68 get = acl_get_entry(acl, ACL_NEXT_ENTRY, &e);
71 /* remove ACL_USER entry for uid */
77 acl_delete_entry(acl, entry);
81 /* create ACL_USER entry for uid */
83 ret = acl_create_entry(&acl, &entry);
86 acl_set_tag_type(entry, ACL_USER);
87 acl_set_qualifier(entry, &uid);
90 /* add permissions for uid */
91 acl_get_permset(entry, &permset);
92 acl_add_perm(permset, ACL_READ|ACL_WRITE);
96 printf("%c%u %s\n", add ? '+' : '-', uid, filename);
98 ret = acl_set_file(filename, ACL_TYPE_ACCESS, acl);
106 /* check if a given uid is listed */
107 static int uid_in_list(GSList *list, uid_t uid)
111 for (l = list; l != NULL; l = g_slist_next(l))
112 if (uid == GPOINTER_TO_UINT(l->data))
117 /* return list of current uids of local active sessions */
118 static GSList *uids_with_local_active_session(const char *own_id)
123 keyfile = g_key_file_new();
124 if (g_key_file_load_from_file(keyfile, "/var/run/ConsoleKit/database", 0, NULL)) {
127 groups = g_key_file_get_groups(keyfile, NULL);
128 if (groups != NULL) {
131 for (i = 0; groups[i] != NULL; i++) {
134 if (!g_str_has_prefix(groups[i], "Session "))
136 if (own_id != NULL &&g_str_has_suffix(groups[i], own_id))
138 if (!g_key_file_get_boolean(keyfile, groups[i], "is_local", NULL))
140 if (!g_key_file_get_boolean(keyfile, groups[i], "is_active", NULL))
142 u = g_key_file_get_integer(keyfile, groups[i], "uid", NULL);
143 if (u > 0 && !uid_in_list(list, u))
144 list = g_slist_prepend(list, GUINT_TO_POINTER(u));
149 g_key_file_free(keyfile);
154 /* ConsoleKit calls us with special variables */
155 static int consolekit_called(const char *action, uid_t *uid, const char **own_session, int *add)
162 if (action == NULL || strcmp(action, "session_active_changed") != 0)
165 s = getenv("CK_SESSION_IS_LOCAL");
168 if (strcmp(s, "true") != 0)
171 s = getenv("CK_SESSION_IS_ACTIVE");
174 if (strcmp(s, "true") == 0)
179 session = getenv("CK_SESSION_ID");
183 s = getenv("CK_SESSION_USER_UID");
186 u = strtoul(s, NULL, 10);
190 *own_session = session;
196 /* add or remove a ACL for a given uid from all matching devices */
197 static void apply_acl_to_devices(uid_t uid, int add)
200 struct udev_enumerate *enumerate;
201 struct udev_list_entry *list_entry;
203 /* iterate over all devices tagged with ACL_SET */
205 enumerate = udev_enumerate_new(udev);
206 udev_enumerate_add_match_property(enumerate, "ACL_MANAGE", "*");
207 udev_enumerate_scan_devices(enumerate);
208 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
209 struct udev_device *device;
212 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
213 udev_list_entry_get_name(list_entry));
216 node = udev_device_get_devnode(device);
219 set_facl(node, uid, add);
220 udev_device_unref(device);
222 udev_enumerate_unref(enumerate);
226 int main (int argc, char* argv[])
228 static const struct option options[] = {
229 { "action", required_argument, NULL, 'a' },
230 { "device", required_argument, NULL, 'D' },
231 { "user", required_argument, NULL, 'u' },
232 { "debug", no_argument, NULL, 'd' },
233 { "help", no_argument, NULL, 'h' },
237 const char *device = NULL;
239 const char* own_session = NULL;
242 /* valgrind is more important to us than a slice allocator */
243 g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, 1);
248 option = getopt_long(argc, argv, "+a:D:u:dh", options, NULL);
254 if (strcmp(optarg, "add") == 0 || strcmp(optarg, "change") == 0)
256 else if (strcmp(optarg, "remove") == 0)
265 uid = strtoul(optarg, NULL, 10);
271 printf("Usage: udev-acl --action=ACTION [--device=DEVICEFILE] [--user=UID]\n\n");
277 if (add < 0 && device == NULL && uid == 0)
278 consolekit_called(argv[optind], &uid, &own_session, &add);
281 fprintf(stderr, "missing action\n\n");
286 if (device != NULL && uid != 0) {
287 fprintf(stderr, "only one option, --device=DEVICEFILE or --user=UID expected\n\n");
294 /* Add ACL for given uid to all matching devices. */
295 apply_acl_to_devices(uid, 1);
298 * Remove ACL for given uid from all matching devices
299 * when there is currently no local active session.
303 list = uids_with_local_active_session(own_session);
304 if (!uid_in_list(list, uid))
305 apply_acl_to_devices(uid, 0);
308 } else if (device != NULL) {
310 * Add ACLs for all current session uids to a given device.
312 * Or remove ACLs for uids which do not have any current local
313 * active session. Remove is not really interesting, because in
314 * most cases the device node is removed anyway.
319 list = uids_with_local_active_session(NULL);
320 for (l = list; l != NULL; l = g_slist_next(l)) {
323 u = GPOINTER_TO_UINT(l->data);
324 if (add || !uid_in_list(list, u))
325 set_facl(device, u, add);
329 fprintf(stderr, "--device=DEVICEFILE or --user=UID expected\n\n");