Debian Codegraph @

src/polkit/polkitsubject.c

raw
1 /*
2 * Copyright (C) 2008 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: David Zeuthen <[email protected]>
20 */
21
22 #include <string.h>
23 #include <stdio.h>
24
25 #include "polkitsubject.h"
26 #include "polkitunixprocess.h"
27 #include "polkitunixsession.h"
28 #include "polkitsystembusname.h"
29 #include "polkiterror.h"
30 #include "polkitprivate.h"
31
32 /**
33 * SECTION:polkitsubject
34 * @title: PolkitSubject
35 * @short_description: Type for representing subjects
36 *
37 * #PolkitSubject is an abstract type for representing one or more
38 * processes.
39 */
40
41 static void
42 base_init (gpointer g_iface)
43 {
44 }
45
46 GType
47 polkit_subject_get_type (void)
48 {
49 static volatile gsize g_define_type_id__volatile = 0;
50
51 if (g_once_init_enter ((gsize*) &g_define_type_id__volatile))
52 {
53 static const GTypeInfo info =
54 {
55 sizeof (PolkitSubjectIface),
56 base_init, /* base_init */
57 NULL, /* base_finalize */
58 NULL, /* class_init */
59 NULL, /* class_finalize */
60 NULL, /* class_data */
61 0, /* instance_size */
62 0, /* n_preallocs */
63 NULL, /* instance_init */
64 NULL /* value_table */
65 };
66
67 GType iface_type =
68 g_type_register_static (G_TYPE_INTERFACE, "PolkitSubject", &info, 0);
69
70 g_type_interface_add_prerequisite (iface_type, G_TYPE_OBJECT);
71 g_once_init_leave (&g_define_type_id__volatile, iface_type);
72 }
73
74 return g_define_type_id__volatile;
75 }
76
77 /**
78 * polkit_subject_hash:
79 * @subject: A #PolkitSubject.
80 *
81 * Gets a hash code for @subject that can be used with e.g. g_hash_table_new().
82 *
83 * Returns: A hash code.
84 */
85 guint
86 polkit_subject_hash (PolkitSubject *subject)
87 {
88 g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), 0);
89 return POLKIT_SUBJECT_GET_IFACE (subject)->hash (subject);
90 }
91
92 /**
93 * polkit_subject_equal:
94 * @a: A #PolkitSubject.
95 * @b: A #PolkitSubject.
96 *
97 * Checks if @a and @b are equal, ie. represent the same subject.
98 * However, avoid calling polkit_subject_equal() to compare two processes;
99 * for more information see the `PolkitUnixProcess` documentation.
100 *
101 * This function can be used in e.g. g_hash_table_new().
102 *
103 * Returns: %TRUE if @a and @b are equal, %FALSE otherwise.
104 */
105 gboolean
106 polkit_subject_equal (PolkitSubject *a,
107 PolkitSubject *b)
108 {
109 g_return_val_if_fail (POLKIT_IS_SUBJECT (a), FALSE);
110 g_return_val_if_fail (POLKIT_IS_SUBJECT (b), FALSE);
111
112 if (!g_type_is_a (G_TYPE_FROM_INSTANCE (a), G_TYPE_FROM_INSTANCE (b)))
113 return FALSE;
114
115 return POLKIT_SUBJECT_GET_IFACE (a)->equal (a, b);
116 }
117
118 /**
119 * polkit_subject_to_string:
120 * @subject: A #PolkitSubject.
121 *
122 * Serializes @subject to a string that can be used in
123 * polkit_subject_from_string().
124 *
125 * Returns: A string representing @subject. Free with g_free().
126 */
127 gchar *
128 polkit_subject_to_string (PolkitSubject *subject)
129 {
130 g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), NULL);
131 return POLKIT_SUBJECT_GET_IFACE (subject)->to_string (subject);
132 }
133
134 /**
135 * polkit_subject_exists:
136 * @subject: A #PolkitSubject.
137 * @cancellable: (allow-none): A #GCancellable or %NULL.
138 * @callback: A #GAsyncReadyCallback to call when the request is satisfied
139 * @user_data: The data to pass to @callback.
140 *
141 * Asynchronously checks if @subject exists.
142 *
143 * When the operation is finished, @callback will be invoked in the
144 * <link linkend="g-main-context-push-thread-default">thread-default
145 * main loop</link> of the thread you are calling this method
146 * from. You can then call polkit_subject_exists_finish() to get the
147 * result of the operation.
148 **/
149 void
150 polkit_subject_exists (PolkitSubject *subject,
151 GCancellable *cancellable,
152 GAsyncReadyCallback callback,
153 gpointer user_data)
154 {
155 g_return_if_fail (POLKIT_IS_SUBJECT (subject));
156 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
157 POLKIT_SUBJECT_GET_IFACE (subject)->exists (subject,
158 cancellable,
159 callback,
160 user_data);
161 }
162
163 /**
164 * polkit_subject_exists_finish:
165 * @subject: A #PolkitSubject.
166 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_subject_exists().
167 * @error: (allow-none): Return location for error or %NULL.
168 *
169 * Finishes checking whether a subject exists.
170 *
171 * Returns: %TRUE if the subject exists, %FALSE if not or @error is set.
172 */
173 gboolean
174 polkit_subject_exists_finish (PolkitSubject *subject,
175 GAsyncResult *res,
176 GError **error)
177 {
178 g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), FALSE);
179 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
180 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
181 return POLKIT_SUBJECT_GET_IFACE (subject)->exists_finish (subject,
182 res,
183 error);
184 }
185
186 /**
187 * polkit_subject_exists_sync:
188 * @subject: A #PolkitSubject.
189 * @cancellable: (allow-none): A #GCancellable or %NULL.
190 * @error: (allow-none): Return location for error or %NULL.
191 *
192 * Checks if @subject exists.
193 *
194 * This is a synchronous blocking call - the calling thread is blocked
195 * until a reply is received. See polkit_subject_exists() for the
196 * asynchronous version.
197 *
198 * Returns: %TRUE if the subject exists, %FALSE if not or @error is set.
199 */
200 gboolean
201 polkit_subject_exists_sync (PolkitSubject *subject,
202 GCancellable *cancellable,
203 GError **error)
204 {
205 g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), FALSE);
206 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
207 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
208 return POLKIT_SUBJECT_GET_IFACE (subject)->exists_sync (subject,
209 cancellable,
210 error);
211 }
212
213 /**
214 * polkit_subject_from_string:
215 * @str: A string obtained from polkit_subject_to_string().
216 * @error: (allow-none): Return location for error or %NULL.
217 *
218 * Creates an object from @str that implements the #PolkitSubject
219 * interface.
220 *
221 * Returns: (transfer full): A #PolkitSubject or %NULL if @error is
222 * set. Free with g_object_unref().
223 */
224 PolkitSubject *
225 polkit_subject_from_string (const gchar *str,
226 GError **error)
227 {
228 PolkitSubject *subject;
229
230 g_return_val_if_fail (str != NULL, NULL);
231 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
232
233 /* TODO: we could do something with VFuncs like in g_icon_from_string() */
234
235 subject = NULL;
236
237 if (g_str_has_prefix (str, "unix-process:"))
238 {
239 gint scanned_pid;
240 guint64 scanned_starttime;
241 gint scanned_uid;
242 gint scanned_pidfd;
243 if (sscanf (str, "unix-process:%d:%" G_GUINT64_FORMAT ":%d:%d", &scanned_pid, &scanned_starttime, &scanned_uid, &scanned_pidfd) == 4)
244 {
245 subject = polkit_unix_process_new_pidfd (scanned_pidfd, scanned_uid, NULL);
246 if (subject)
247 polkit_unix_process_set_start_time (POLKIT_UNIX_PROCESS (subject), scanned_starttime);
248 }
249 else if (sscanf (str, "unix-process:%d:%" G_GUINT64_FORMAT ":%d", &scanned_pid, &scanned_starttime, &scanned_uid) == 3)
250 {
251 subject = polkit_unix_process_new_for_owner (scanned_pid, scanned_starttime, scanned_uid);
252 }
253 else if (sscanf (str, "unix-process:%d:%" G_GUINT64_FORMAT, &scanned_pid, &scanned_starttime) == 2)
254 {
255 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
256 subject = polkit_unix_process_new_full (scanned_pid, scanned_starttime);
257 G_GNUC_END_IGNORE_DEPRECATIONS
258 }
259 else if (sscanf (str, "unix-process:%d", &scanned_pid) == 1)
260 {
261 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
262 subject = polkit_unix_process_new (scanned_pid);
263 G_GNUC_END_IGNORE_DEPRECATIONS
264 if (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject)) == 0)
265 {
266 g_object_unref (subject);
267 subject = NULL;
268 g_set_error (error,
269 POLKIT_ERROR,
270 POLKIT_ERROR_FAILED,
271 "Unable to determine start time for process with pid %d",
272 scanned_pid);
273 }
274 }
275 }
276 else if (g_str_has_prefix (str, "unix-session:"))
277 {
278 subject = polkit_unix_session_new (str + sizeof "unix-session:" - 1);
279 }
280 else if (g_str_has_prefix (str, "system-bus-name:"))
281 {
282 subject = polkit_system_bus_name_new (str + sizeof "system-bus-name:" - 1);
283 }
284
285 if (subject == NULL && (error != NULL && *error == NULL))
286 {
287 g_set_error (error,
288 POLKIT_ERROR,
289 POLKIT_ERROR_FAILED,
290 "Malformed subject string `%s'",
291 str);
292 }
293
294
295 return subject;
296 }
297
298 /* Note that this returns a floating value. */
299 GVariant *
300 polkit_subject_to_gvariant (PolkitSubject *subject)
301 {
302 GVariantBuilder builder;
303 GVariant *dict;
304 const gchar *kind;
305
306 kind = "";
307
308 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
309 if (POLKIT_IS_UNIX_PROCESS (subject))
310 {
311 kind = "unix-process";
312 g_variant_builder_add (&builder, "{sv}", "pid",
313 g_variant_new_uint32 (polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))));
314 g_variant_builder_add (&builder, "{sv}", "start-time",
315 g_variant_new_uint64 (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject))));
316 g_variant_builder_add (&builder, "{sv}", "uid",
317 g_variant_new_int32 (polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject))));
318 if (polkit_unix_process_get_pidfd_is_safe(POLKIT_UNIX_PROCESS (subject)))
319 g_variant_builder_add (&builder, "{sv}", "pidfd",
320 g_variant_new_handle (polkit_unix_process_get_pidfd (POLKIT_UNIX_PROCESS (subject))));
321 }
322 else if (POLKIT_IS_UNIX_SESSION (subject))
323 {
324 kind = "unix-session";
325 g_variant_builder_add (&builder, "{sv}", "session-id",
326 g_variant_new_string (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject))));
327 }
328 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
329 {
330 kind = "system-bus-name";
331 g_variant_builder_add (&builder, "{sv}", "name",
332 g_variant_new_string (polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))));
333 }
334 else
335 {
336 g_warning ("Unknown class %s implementing PolkitSubject", g_type_name (G_TYPE_FROM_INSTANCE (subject)));
337 }
338
339 dict = g_variant_builder_end (&builder);
340 return g_variant_new ("(s@a{sv})", kind, dict);
341 }
342
343 static GVariant *
344 lookup_asv (GVariant *dict,
345 const gchar *given_key,
346 const GVariantType *given_type,
347 GError **error)
348 {
349 GVariantIter iter;
350 const gchar *key;
351 GVariant *value;
352 GVariant *ret;
353
354 ret = NULL;
355
356 g_variant_iter_init (&iter, dict);
357 while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
358 {
359 if (g_strcmp0 (key, given_key) == 0)
360 {
361 if (!g_variant_is_of_type (value, given_type))
362 {
363 gchar *type_string;
364 type_string = g_variant_type_dup_string (given_type);
365 g_set_error (error,
366 POLKIT_ERROR,
367 POLKIT_ERROR_FAILED,
368 "Value for key `%s' found but is of type %s and type %s was expected",
369 given_key,
370 g_variant_get_type_string (value),
371 type_string);
372 g_free (type_string);
373 g_variant_unref (value);
374 goto out;
375 }
376 ret = value;
377 goto out;
378 }
379 g_variant_unref (value);
380 }
381
382 out:
383 if (ret == NULL)
384 {
385 gchar *type_string;
386 type_string = g_variant_type_dup_string (given_type);
387 g_set_error (error,
388 POLKIT_ERROR,
389 POLKIT_ERROR_FAILED,
390 "Didn't find value for key `%s' of type %s",
391 given_key,
392 type_string);
393 g_free (type_string);
394 }
395
396 return ret;
397 }
398
399 PolkitSubject *
400 polkit_subject_new_for_gvariant_invocation (GVariant *variant,
401 GDBusMethodInvocation *invocation,
402 GError **error)
403 {
404 PolkitSubject *ret;
405 const gchar *kind;
406 GVariant *details_gvariant;
407
408 ret = NULL;
409
410 g_variant_get (variant,
411 "(&s@a{sv})",
412 &kind,
413 &details_gvariant);
414
415 if (g_strcmp0 (kind, "unix-process") == 0)
416 {
417 GUnixFDList *fd_list;
418 GVariant *v;
419 gint index, pidfd;
420 guint32 pid;
421 guint64 start_time;
422 gint32 uid;
423
424 v = lookup_asv (details_gvariant, "uid", G_VARIANT_TYPE_INT32, NULL);
425 if (v != NULL)
426 {
427 uid = g_variant_get_int32 (v);
428 g_variant_unref (v);
429 }
430 else
431 {
432 uid = -1;
433 }
434
435 if (invocation) {
436 fd_list = g_dbus_message_get_unix_fd_list (g_dbus_method_invocation_get_message (invocation));
437 if (fd_list)
438 {
439 v = lookup_asv (details_gvariant, "pidfd", G_VARIANT_TYPE_HANDLE, NULL);
440 if (v != NULL)
441 {
442 if (uid == -1)
443 {
444 g_set_error (error,
445 POLKIT_ERROR,
446 POLKIT_ERROR_FAILED,
447 "Error parsing unix-process subject: 'pidfd' specified withtout 'uid'");
448 goto out;
449 }
450
451 index = g_variant_get_handle (v);
452 pidfd = g_unix_fd_list_get (fd_list, index, NULL);
453 g_variant_unref (v);
454
455 ret = polkit_unix_process_new_pidfd (pidfd, uid, NULL);
456 }
457 }
458 }
459
460 if (!ret)
461 {
462 v = lookup_asv (details_gvariant, "pid", G_VARIANT_TYPE_UINT32, error);
463 if (v == NULL)
464 {
465 g_prefix_error (error, "Error parsing unix-process subject: ");
466 goto out;
467 }
468 pid = g_variant_get_uint32 (v);
469 g_variant_unref (v);
470
471 v = lookup_asv (details_gvariant, "start-time", G_VARIANT_TYPE_UINT64, error);
472 if (v == NULL)
473 {
474 g_prefix_error (error, "Error parsing unix-process subject: ");
475 goto out;
476 }
477 start_time = g_variant_get_uint64 (v);
478 g_variant_unref (v);
479
480 ret = polkit_unix_process_new_for_owner (pid, start_time, uid);
481 }
482 }
483 else if (g_strcmp0 (kind, "unix-session") == 0)
484 {
485 GVariant *v;
486 const gchar *session_id;
487
488 v = lookup_asv (details_gvariant, "session-id", G_VARIANT_TYPE_STRING, error);
489 if (v == NULL)
490 {
491 g_prefix_error (error, "Error parsing unix-session subject: ");
492 goto out;
493 }
494 session_id = g_variant_get_string (v, NULL);
495 ret = polkit_unix_session_new (session_id);
496 g_variant_unref (v);
497 }
498 else if (g_strcmp0 (kind, "system-bus-name") == 0)
499 {
500 GVariant *v;
501 const gchar *name;
502
503 v = lookup_asv (details_gvariant, "name", G_VARIANT_TYPE_STRING, error);
504 if (v == NULL)
505 {
506 g_prefix_error (error, "Error parsing system-bus-name subject: ");
507 goto out;
508 }
509 name = g_variant_get_string (v, NULL);
510 if (!g_dbus_is_unique_name (name))
511 {
512 g_set_error (error,
513 POLKIT_ERROR,
514 POLKIT_ERROR_FAILED,
515 "Error parsing system-bus-name subject: `%s' is not a valid unique name",
516 name);
517 goto out;
518 }
519 ret = polkit_system_bus_name_new (name);
520 g_variant_unref (v);
521 }
522 else
523 {
524 g_set_error (error,
525 POLKIT_ERROR,
526 POLKIT_ERROR_FAILED,
527 "Unknown subject of kind `%s'",
528 kind);
529 }
530
531 out:
532 g_variant_unref (details_gvariant);
533 return ret;
534 }
535
536 PolkitSubject *
537 polkit_subject_new_for_gvariant (GVariant *variant,
538 GError **error)
539 {
540 return polkit_subject_new_for_gvariant_invocation (variant, NULL, error);
541 }