chiark / gitweb /
udev-acl: properly handle CK change events for root user
[elogind.git] / extras / udev-acl / udev-acl.c
1 /*
2  * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
8  *
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:
13  */
14
15 #include <stdio.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <inttypes.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <sys/stat.h>
26 #include <glib.h>
27 #include <acl/libacl.h>
28 #include <libudev.h>
29
30 static int debug;
31
32 enum{
33         ACTION_NONE = 0,
34         ACTION_REMOVE,
35         ACTION_ADD,
36         ACTION_CHANGE
37 };
38
39 static int set_facl(const char* filename, uid_t uid, int add)
40 {
41         int get;
42         acl_t acl;
43         acl_entry_t entry = NULL;
44         acl_entry_t e;
45         acl_permset_t permset;
46         int ret;
47
48         /* read current record */
49         acl = acl_get_file(filename, ACL_TYPE_ACCESS);
50         if (!acl)
51                 return -1;
52
53         /* locate ACL_USER entry for uid */
54         get = acl_get_entry(acl, ACL_FIRST_ENTRY, &e);
55         while (get == 1) {
56                 acl_tag_t t;
57
58                 acl_get_tag_type(e, &t);
59                 if (t == ACL_USER) {
60                         uid_t *u;
61
62                         u = (uid_t*)acl_get_qualifier(e);
63                         if (u == NULL) {
64                                 ret = -1;
65                                 goto out;
66                         }
67                         if (*u == uid) {
68                                 entry = e;
69                                 acl_free(u);
70                                 break;
71                         }
72                         acl_free(u);
73                 }
74
75                 get = acl_get_entry(acl, ACL_NEXT_ENTRY, &e);
76         }
77
78         /* remove ACL_USER entry for uid */
79         if (!add) {
80                 if (entry == NULL) {
81                         ret = 0;
82                         goto out;
83                 }
84                 acl_delete_entry(acl, entry);
85                 goto update;
86         }
87
88         /* create ACL_USER entry for uid */
89         if (entry == NULL) {
90                 ret = acl_create_entry(&acl, &entry);
91                 if (ret != 0)
92                         goto out;
93                 acl_set_tag_type(entry, ACL_USER);
94                 acl_set_qualifier(entry, &uid);
95         }
96
97         /* add permissions for uid */
98         acl_get_permset(entry, &permset);
99         acl_add_perm(permset, ACL_READ|ACL_WRITE);
100 update:
101         /* update record */
102         if (debug)
103                 printf("%c%u %s\n", add ? '+' : '-', uid, filename);
104         acl_calc_mask(&acl);
105         ret = acl_set_file(filename, ACL_TYPE_ACCESS, acl);
106         if (ret != 0)
107                 goto out;
108 out:
109         acl_free(acl);
110         return ret;
111 }
112
113 /* check if a given uid is listed */
114 static int uid_in_list(GSList *list, uid_t uid)
115 {
116         GSList *l;
117
118         for (l = list; l != NULL; l = g_slist_next(l))
119                 if (uid == GPOINTER_TO_UINT(l->data))
120                         return 1;
121         return 0;
122 }
123
124 /* return list of current uids of local active sessions */
125 static GSList *uids_with_local_active_session(const char *own_id)
126 {
127         GSList *list = NULL;
128         GKeyFile *keyfile;
129
130         keyfile = g_key_file_new();
131         if (g_key_file_load_from_file(keyfile, "/var/run/ConsoleKit/database", 0, NULL)) {
132                 gchar **groups;
133
134                 groups = g_key_file_get_groups(keyfile, NULL);
135                 if (groups != NULL) {
136                         int i;
137
138                         for (i = 0; groups[i] != NULL; i++) {
139                                 uid_t u;
140
141                                 if (!g_str_has_prefix(groups[i], "Session "))
142                                         continue;
143                                 if (own_id != NULL &&g_str_has_suffix(groups[i], own_id))
144                                         continue;
145                                 if (!g_key_file_get_boolean(keyfile, groups[i], "is_local", NULL))
146                                         continue;
147                                 if (!g_key_file_get_boolean(keyfile, groups[i], "is_active", NULL))
148                                         continue;
149                                 u = g_key_file_get_integer(keyfile, groups[i], "uid", NULL);
150                                 if (u > 0 && !uid_in_list(list, u))
151                                         list = g_slist_prepend(list, GUINT_TO_POINTER(u));
152                         }
153                         g_strfreev(groups);
154                 }
155         }
156         g_key_file_free(keyfile);
157
158         return list;
159 }
160
161 /* ConsoleKit calls us with special variables */
162 static int consolekit_called(const char *ck_action, uid_t *uid, uid_t *uid2, const char **remove_session_id, int *action)
163 {
164         int a = ACTION_NONE;
165         uid_t u = 0;
166         uid_t u2 = 0;
167         const char *s;
168         const char *s2;
169         const char *old_session = NULL;
170
171         if (ck_action == NULL || strcmp(ck_action, "seat_active_session_changed") != 0)
172                 return -1;
173
174         /* We can have one of: remove, add, change, no-change */
175         s = getenv("CK_SEAT_OLD_SESSION_ID");
176         s2 = getenv("CK_SEAT_SESSION_ID");
177         if (s == NULL && s2 == NULL) {
178                 return -1;
179         } else if (s2 == NULL) {
180                 a = ACTION_REMOVE;
181         } else if (s == NULL) {
182                 a = ACTION_ADD;
183         } else {
184                 a = ACTION_CHANGE;
185         }
186
187         switch (a) {
188         case ACTION_ADD:
189                 s = getenv("CK_SEAT_SESSION_USER_UID");
190                 if (s == NULL)
191                         return -1;
192                 u = strtoul(s, NULL, 10);
193                 if (u == 0)
194                         return 0;
195
196                 s = getenv("CK_SEAT_SESSION_IS_LOCAL");
197                 if (s == NULL)
198                         return -1;
199                 if (strcmp(s, "true") != 0)
200                         return 0;
201
202                 break;
203         case ACTION_REMOVE:
204                 s = getenv("CK_SEAT_OLD_SESSION_USER_UID");
205                 if (s == NULL)
206                         return -1;
207                 u = strtoul(s, NULL, 10);
208                 if (u == 0)
209                         return 0;
210
211                 s = getenv("CK_SEAT_OLD_SESSION_IS_LOCAL");
212                 if (s == NULL)
213                         return -1;
214                 if (strcmp(s, "true") != 0)
215                         return 0;
216
217                 old_session = getenv("CK_SEAT_OLD_SESSION_ID");
218                 if (old_session == NULL)
219                         return -1;
220
221                 break;
222         case ACTION_CHANGE:
223                 s = getenv("CK_SEAT_OLD_SESSION_USER_UID");
224                 if (s == NULL)
225                         return -1;
226                 u = strtoul(s, NULL, 10);
227                 s = getenv("CK_SEAT_SESSION_USER_UID");
228                 if (s == NULL)
229                         return -1;
230                 u2 = strtoul(s, NULL, 10);
231
232                 s = getenv("CK_SEAT_OLD_SESSION_IS_LOCAL");
233                 s2 = getenv("CK_SEAT_SESSION_IS_LOCAL");
234                 if (s == NULL || s2 == NULL)
235                         return -1;
236                 /* don't process non-local session changes */
237                 if (strcmp(s, "true") != 0 && strcmp(s2, "true") != 0)
238                         return 0;
239
240                 if (strcmp(s, "true") == 0 && strcmp(s, "true") == 0) {
241                         /* process the change */
242                         if (u == u2) {
243                                 /* special case: we noop if we are
244                                  * changing between local sessions for
245                                  * the same uid */
246                                 a = ACTION_NONE;
247                         }
248                         old_session = getenv("CK_SEAT_OLD_SESSION_ID");
249                         if (old_session == NULL)
250                                 return -1;
251                 } else if (strcmp(s, "true") == 0) {
252                         /* only process the removal */
253                         a = ACTION_REMOVE;
254                         old_session = getenv("CK_SEAT_OLD_SESSION_ID");
255                         if (old_session == NULL)
256                                 return -1;
257                 } else if (strcmp(s2, "true") == 0) {
258                         /* only process the addition */
259                         a = ACTION_ADD;
260                         u = u2;
261                 }
262                 break;
263         case ACTION_NONE:
264                 break;
265         default:
266                 g_assert_not_reached();
267                 break;
268         }
269
270         *remove_session_id = old_session;
271         *uid = u;
272         *uid2 = u2;
273         *action = a;
274         return 0;
275 }
276
277 /* add or remove a ACL for a given uid from all matching devices */
278 static void apply_acl_to_devices(uid_t uid, int add)
279 {
280         struct udev *udev;
281         struct udev_enumerate *enumerate;
282         struct udev_list_entry *list_entry;
283
284         /* iterate over all devices tagged with ACL_SET */
285         udev = udev_new();
286         enumerate = udev_enumerate_new(udev);
287         udev_enumerate_add_match_tag(enumerate, "udev-acl");
288         udev_enumerate_scan_devices(enumerate);
289         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
290                 struct udev_device *device;
291                 const char *node;
292
293                 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
294                                                       udev_list_entry_get_name(list_entry));
295                 if (device == NULL)
296                         continue;
297                 node = udev_device_get_devnode(device);
298                 if (node == NULL)
299                         continue;
300                 set_facl(node, uid, add);
301                 udev_device_unref(device);
302         }
303         udev_enumerate_unref(enumerate);
304         udev_unref(udev);
305 }
306
307 static void
308 remove_uid (uid_t uid, const char *remove_session_id)
309 {
310         /*
311          * Remove ACL for given uid from all matching devices
312          * when there is currently no local active session.
313          */
314         GSList *list;
315
316         list = uids_with_local_active_session(remove_session_id);
317         if (!uid_in_list(list, uid))
318                 apply_acl_to_devices(uid, 0);
319         g_slist_free(list);
320 }
321
322 int main (int argc, char* argv[])
323 {
324         static const struct option options[] = {
325                 { "action", required_argument, NULL, 'a' },
326                 { "device", required_argument, NULL, 'D' },
327                 { "user", required_argument, NULL, 'u' },
328                 { "debug", no_argument, NULL, 'd' },
329                 { "help", no_argument, NULL, 'h' },
330                 {}
331         };
332         int action = -1;
333         const char *device = NULL;
334         uid_t uid = 0;
335         uid_t uid2 = 0;
336         const char* remove_session_id = NULL;
337         int rc = 0;
338
339         /* valgrind is more important to us than a slice allocator */
340         g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, 1);
341
342         while (1) {
343                 int option;
344
345                 option = getopt_long(argc, argv, "+a:D:u:dh", options, NULL);
346                 if (option == -1)
347                         break;
348
349                 switch (option) {
350                 case 'a':
351                         if (strcmp(optarg, "remove") == 0)
352                                 action = ACTION_REMOVE;
353                         else
354                                 action = ACTION_ADD;
355                         break;
356                 case 'D':
357                         device = optarg;
358                         break;
359                 case 'u':
360                         uid = strtoul(optarg, NULL, 10);
361                         break;
362                 case 'd':
363                         debug = 1;
364                         break;
365                 case 'h':
366                         printf("Usage: udev-acl --action=ACTION [--device=DEVICEFILE] [--user=UID]\n\n");
367                 default:
368                         goto out;
369                 }
370         }
371
372         if (action < 0 && device == NULL && uid == 0)
373                 consolekit_called(argv[optind], &uid, &uid2, &remove_session_id, &action);
374
375         if (action < 0) {
376                 fprintf(stderr, "missing action\n\n");
377                 rc = 2;
378                 goto out;
379         }
380
381         if (device != NULL && uid != 0) {
382                 fprintf(stderr, "only one option, --device=DEVICEFILE or --user=UID expected\n\n");
383                 rc = 3;
384                 goto out;
385         }
386
387         if (uid != 0) {
388                 switch (action) {
389                 case ACTION_ADD:
390                         /* Add ACL for given uid to all matching devices. */
391                         apply_acl_to_devices(uid, 1);
392                         break;
393                 case ACTION_REMOVE:
394                         remove_uid(uid, remove_session_id);
395                         break;
396                 case ACTION_CHANGE:
397                         remove_uid(uid, remove_session_id);
398                         apply_acl_to_devices(uid2, 1);
399                         break;
400                 case ACTION_NONE:
401                         goto out;
402                         break;
403                 default:
404                         g_assert_not_reached();
405                         break;
406                 }
407         } else if (device != NULL) {
408                 /*
409                  * Add ACLs for all current session uids to a given device.
410                  *
411                  * Or remove ACLs for uids which do not have any current local
412                  * active session. Remove is not really interesting, because in
413                  * most cases the device node is removed anyway.
414                  */
415                 GSList *list;
416                 GSList *l;
417
418                 list = uids_with_local_active_session(NULL);
419                 for (l = list; l != NULL; l = g_slist_next(l)) {
420                         uid_t u;
421
422                         u = GPOINTER_TO_UINT(l->data);
423                         if (action == ACTION_ADD || !uid_in_list(list, u))
424                                 set_facl(device, u, action == ACTION_ADD);
425                 }
426                 g_slist_free(list);
427         } else {
428                 fprintf(stderr, "--device=DEVICEFILE or --user=UID expected\n\n");
429                 rc = 3;
430         }
431 out:
432         return rc;
433 }