Debian Codegraph @

src/polkitbackend/polkitbackendsessionmonitor-systemd.c

raw
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * Author: Matthias Clasen
20 */
21
22 #include <errno.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <string.h>
26 #include <glib/gstdio.h>
27 #include <systemd/sd-login.h>
28 #include <stdlib.h>
29
30 #include <polkit/polkit.h>
31 #include <polkit/polkitprivate.h>
32 #include "polkitbackendsessionmonitor.h"
33
34 /* <internal>
35 * SECTION:polkitbackendsessionmonitor
36 * @title: PolkitBackendSessionMonitor
37 * @short_description: Monitor sessions
38 *
39 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
40 */
41
42 typedef struct
43 {
44 GSource source;
45 GPollFD pollfd;
46 sd_login_monitor *monitor;
47 } SdSource;
48
49 static gboolean
50 sd_source_prepare (GSource *source,
51 gint *timeout)
52 {
53 *timeout = -1;
54 return FALSE;
55 }
56
57 static gboolean
58 sd_source_check (GSource *source)
59 {
60 SdSource *sd_source = (SdSource *)source;
61
62 return sd_source->pollfd.revents != 0;
63 }
64
65 static gboolean
66 sd_source_dispatch (GSource *source,
67 GSourceFunc callback,
68 gpointer user_data)
69
70 {
71 SdSource *sd_source = (SdSource *)source;
72 gboolean ret;
73
74 g_warn_if_fail (callback != NULL);
75
76 if (callback == NULL)
77 return G_SOURCE_CONTINUE;
78
79 ret = (*callback) (user_data);
80
81 sd_login_monitor_flush (sd_source->monitor);
82
83 return ret;
84 }
85
86 static void
87 sd_source_finalize (GSource *source)
88 {
89 SdSource *sd_source = (SdSource*)source;
90
91 sd_login_monitor_unref (sd_source->monitor);
92 }
93
94 static GSourceFuncs sd_source_funcs = {
95 sd_source_prepare,
96 sd_source_check,
97 sd_source_dispatch,
98 sd_source_finalize
99 };
100
101 static GSource *
102 sd_source_new (void)
103 {
104 GSource *source;
105 SdSource *sd_source;
106 int ret;
107
108 source = g_source_new (&sd_source_funcs, sizeof (SdSource));
109 sd_source = (SdSource *)source;
110
111 if ((ret = sd_login_monitor_new ("session", &sd_source->monitor)) < 0)
112 {
113 g_printerr ("Error getting login monitor: %d\n", ret);
114 }
115 else
116 {
117 sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
118 sd_source->pollfd.events = G_IO_IN;
119 g_source_add_poll (source, &sd_source->pollfd);
120 }
121
122 return source;
123 }
124
125 struct _PolkitBackendSessionMonitor
126 {
127 GObject parent_instance;
128
129 GDBusConnection *system_bus;
130
131 GSource *sd_source;
132 };
133
134 struct _PolkitBackendSessionMonitorClass
135 {
136 GObjectClass parent_class;
137
138 void (*changed) (PolkitBackendSessionMonitor *monitor);
139 };
140
141
142 enum
143 {
144 CHANGED_SIGNAL,
145 LAST_SIGNAL,
146 };
147
148 static guint signals[LAST_SIGNAL] = {0};
149
150 G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
151
152 /* ---------------------------------------------------------------------------------------------------- */
153
154 static gboolean
155 sessions_changed (gpointer user_data)
156 {
157 PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
158
159 g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
160
161 return TRUE;
162 }
163
164
165 static void
166 polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
167 {
168 GError *error;
169
170 error = NULL;
171 monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
172 if (monitor->system_bus == NULL)
173 {
174 g_printerr ("Error getting system bus: %s", error->message);
175 g_error_free (error);
176 }
177
178 monitor->sd_source = sd_source_new ();
179 g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
180 g_source_attach (monitor->sd_source, NULL);
181 }
182
183 static void
184 polkit_backend_session_monitor_finalize (GObject *object)
185 {
186 PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
187
188 if (monitor->system_bus != NULL)
189 g_object_unref (monitor->system_bus);
190
191 if (monitor->sd_source != NULL)
192 {
193 g_source_destroy (monitor->sd_source);
194 g_source_unref (monitor->sd_source);
195 }
196
197 if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
198 G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
199 }
200
201 static void
202 polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
203 {
204 GObjectClass *gobject_class;
205
206 gobject_class = G_OBJECT_CLASS (klass);
207
208 gobject_class->finalize = polkit_backend_session_monitor_finalize;
209
210 /**
211 * PolkitBackendSessionMonitor::changed:
212 * @monitor: A #PolkitBackendSessionMonitor
213 *
214 * Emitted when something changes.
215 */
216 signals[CHANGED_SIGNAL] = g_signal_new ("changed",
217 POLKIT_BACKEND_TYPE_SESSION_MONITOR,
218 G_SIGNAL_RUN_LAST,
219 G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
220 NULL, /* accumulator */
221 NULL, /* accumulator data */
222 g_cclosure_marshal_VOID__VOID,
223 G_TYPE_NONE,
224 0);
225 }
226
227 PolkitBackendSessionMonitor *
228 polkit_backend_session_monitor_new (void)
229 {
230 PolkitBackendSessionMonitor *monitor;
231
232 monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
233
234 return monitor;
235 }
236
237 /* ---------------------------------------------------------------------------------------------------- */
238
239 GList *
240 polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
241 {
242 /* TODO */
243 return NULL;
244 }
245
246 /* ---------------------------------------------------------------------------------------------------- */
247
248 /**
249 * polkit_backend_session_monitor_get_user:
250 * @monitor: A #PolkitBackendSessionMonitor.
251 * @subject: A #PolkitSubject.
252 * @result_matches: If not %NULL, set to indicate whether the return value matches current (RACY) state.
253 * @error: Return location for error.
254 *
255 * Gets the user corresponding to @subject or %NULL if no user exists.
256 *
257 * NOTE: For a #PolkitUnixProcess, the UID is read from @subject (which may
258 * come from e.g. a D-Bus client), so it may not correspond to the actual UID
259 * of the referenced process (at any point in time). This is indicated by
260 * setting @result_matches to %FALSE; the caller may reject such subjects or
261 * require additional privileges. @result_matches == %TRUE only indicates that
262 * the UID matched the underlying process at ONE point in time, it may not match
263 * later.
264 *
265 * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
266 */
267 PolkitIdentity *
268 polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor,
269 PolkitSubject *subject,
270 gboolean *result_matches,
271 GError **error)
272 {
273 PolkitIdentity *ret;
274 gboolean matches;
275
276 ret = NULL;
277 matches = FALSE;
278
279 if (POLKIT_IS_UNIX_PROCESS (subject))
280 {
281 gint subject_uid, current_uid;
282 GError *local_error;
283
284 subject_uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
285 if (subject_uid == -1)
286 {
287 g_set_error (error,
288 POLKIT_ERROR,
289 POLKIT_ERROR_FAILED,
290 "Unix process subject does not have uid set");
291 goto out;
292 }
293 local_error = NULL;
294 current_uid = polkit_unix_process_get_racy_uid__ (POLKIT_UNIX_PROCESS (subject), &local_error);
295 if (local_error != NULL)
296 {
297 g_propagate_error (error, local_error);
298 goto out;
299 }
300 ret = polkit_unix_user_new (subject_uid);
301 matches = (subject_uid == current_uid);
302 }
303 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
304 {
305 ret = (PolkitIdentity*)polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
306 matches = TRUE;
307 }
308 else if (POLKIT_IS_UNIX_SESSION (subject))
309 {
310 uid_t uid;
311
312 if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
313 {
314 g_set_error (error,
315 POLKIT_ERROR,
316 POLKIT_ERROR_FAILED,
317 "Error getting uid for session");
318 goto out;
319 }
320
321 ret = polkit_unix_user_new (uid);
322 matches = TRUE;
323 }
324
325 out:
326 if (result_matches != NULL)
327 {
328 *result_matches = matches;
329 }
330 return ret;
331 }
332
333 /**
334 * polkit_backend_session_monitor_get_session_for_subject:
335 * @monitor: A #PolkitBackendSessionMonitor.
336 * @subject: A #PolkitSubject.
337 * @error: Return location for error.
338 *
339 * Gets the session corresponding to @subject or %NULL if no session exists.
340 *
341 * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
342 */
343 PolkitSubject *
344 polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
345 PolkitSubject *subject,
346 GError **error)
347 {
348 PolkitUnixProcess *tmp_process = NULL;
349 PolkitUnixProcess *process = NULL;
350 PolkitSubject *session = NULL;
351 char *session_id = NULL;
352 pid_t pid;
353 #if HAVE_SD_UID_GET_DISPLAY
354 uid_t uid;
355 #endif
356 #if HAVE_SD_PIDFD_GET_SESSION
357 int pidfd;
358 #endif
359
360 if (POLKIT_IS_UNIX_PROCESS (subject))
361 process = POLKIT_UNIX_PROCESS (subject); /* We already have a process */
362 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
363 {
364 /* Convert bus name to process */
365 tmp_process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
366 if (!tmp_process)
367 goto out;
368 process = tmp_process;
369 }
370 else
371 {
372 g_set_error (error,
373 POLKIT_ERROR,
374 POLKIT_ERROR_NOT_SUPPORTED,
375 "Cannot get session for subject of type %s",
376 g_type_name (G_TYPE_FROM_INSTANCE (subject)));
377 goto out;
378 }
379
380 #if HAVE_SD_PIDFD_GET_SESSION
381 /* First try to get the session from the pidfd (systemd version 253) */
382 pidfd = polkit_unix_process_get_pidfd (process);
383 if (pidfd >= 0)
384 {
385 if (sd_pidfd_get_session (pidfd, &session_id) >= 0)
386 {
387 session = polkit_unix_session_new (session_id);
388 goto out;
389 }
390 }
391 #endif
392
393 /* Now do process -> pid -> same session */
394 g_assert (process != NULL);
395 pid = polkit_unix_process_get_pid (process);
396
397 if (sd_pid_get_session (pid, &session_id) >= 0)
398 {
399 session = polkit_unix_session_new (session_id);
400 goto out;
401 }
402
403 #if HAVE_SD_PIDFD_GET_SESSION
404 /* Now do process fd -> uid -> graphical session (systemd version 253) */
405 pidfd = polkit_unix_process_get_pidfd (process);
406 if (pidfd >= 0)
407 {
408 if (sd_pidfd_get_owner_uid (pidfd, &uid) < 0)
409 goto out;
410
411 if (sd_uid_get_display (uid, &session_id) >= 0)
412 {
413 session = polkit_unix_session_new (session_id);
414 goto out;
415 }
416 }
417 #endif
418
419 #if HAVE_SD_UID_GET_DISPLAY
420 /* Now do process -> uid -> graphical session (systemd version 213)*/
421 if (sd_pid_get_owner_uid (pid, &uid) < 0)
422 goto out;
423
424 if (sd_uid_get_display (uid, &session_id) >= 0)
425 {
426 session = polkit_unix_session_new (session_id);
427 goto out;
428 }
429 #endif
430
431 out:
432 free (session_id);
433 if (tmp_process) g_object_unref (tmp_process);
434 return session;
435 }
436
437 gboolean
438 polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
439 PolkitSubject *session)
440 {
441 char *seat;
442
443 if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
444 {
445 free (seat);
446 return TRUE;
447 }
448
449 return FALSE;
450 }
451
452
453 gboolean
454 polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
455 PolkitSubject *session)
456 {
457 const char *session_id;
458 char *state;
459 uid_t uid;
460 gboolean is_active = FALSE;
461
462 session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
463
464 g_debug ("Checking whether session %s is active.", session_id);
465
466 /* Check whether *any* of the user's current sessions are active. */
467 if (sd_session_get_uid (session_id, &uid) < 0)
468 goto fallback;
469
470 g_debug ("Session %s has UID %u.", session_id, uid);
471
472 if (sd_uid_get_state (uid, &state) < 0)
473 goto fallback;
474
475 g_debug ("UID %u has state %s.", uid, state);
476
477 is_active = (g_strcmp0 (state, "active") == 0);
478 free (state);
479
480 return is_active;
481
482 fallback:
483 /* Fall back to checking the session. This is not ideal, since the user
484 * might have multiple sessions, and we cannot guarantee to have chosen
485 * the active one.
486 *
487 * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
488 return sd_session_is_active (session_id);
489 }
490