Debian Codegraph @

src/polkitbackend/polkitbackendinteractiveauthority.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 <errno.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #ifdef HAVE_NETGROUP_H
26 #include <netgroup.h>
27 #else
28 #include <netdb.h>
29 #endif
30 #include <string.h>
31 #include <glib/gstdio.h>
32 #include <locale.h>
33 #ifdef __linux__
34 #include <fcntl.h>
35 #include <sys/stat.h>
36 #endif
37
38 #include <polkit/polkit.h>
39 #include "polkitbackendinteractiveauthority.h"
40 #include "polkitbackendactionpool.h"
41 #include "polkitbackendcommon.h"
42 #include "polkitbackendsessionmonitor.h"
43
44 #include <polkit/polkitprivate.h>
45
46 /**
47 * SECTION:polkitbackendinteractiveauthority
48 * @title: PolkitBackendInteractiveAuthority
49 * @short_description: Interactive Authority
50 * @stability: Unstable
51 *
52 * An subclass of #PolkitBackendAuthority that supports interaction
53 * with authentication agents.
54 */
55
56 /* ---------------------------------------------------------------------------------------------------- */
57
58 typedef struct TemporaryAuthorizationStore TemporaryAuthorizationStore;
59
60 static TemporaryAuthorizationStore *temporary_authorization_store_new (PolkitBackendInteractiveAuthority *authority);
61 static void temporary_authorization_store_free (TemporaryAuthorizationStore *store);
62
63 static gboolean temporary_authorization_store_has_authorization (TemporaryAuthorizationStore *store,
64 PolkitSubject *subject,
65 const gchar *action_id,
66 const gchar **out_tmp_authz_id);
67
68 static const gchar *temporary_authorization_store_add_authorization (TemporaryAuthorizationStore *store,
69 guint expiration_seconds,
70 PolkitSubject *subject,
71 PolkitSubject *session,
72 const gchar *action_id);
73
74 static void temporary_authorization_store_remove_authorizations_for_system_bus_name (TemporaryAuthorizationStore *store,
75 const gchar *name);
76
77 /* ---------------------------------------------------------------------------------------------------- */
78
79 struct AuthenticationAgent;
80 typedef struct AuthenticationAgent AuthenticationAgent;
81
82 struct AuthenticationSession;
83 typedef struct AuthenticationSession AuthenticationSession;
84
85 typedef void (*AuthenticationAgentCallback) (AuthenticationAgent *agent,
86 PolkitSubject *subject,
87 PolkitIdentity *user_of_subject,
88 PolkitSubject *caller,
89 PolkitBackendInteractiveAuthority *authority,
90 const gchar *action_id,
91 PolkitDetails *details,
92 PolkitImplicitAuthorization implicit_authorization,
93 gboolean authentication_success,
94 gboolean was_dismissed,
95 PolkitIdentity *authenticated_identity,
96 gpointer user_data);
97
98 static AuthenticationAgent *authentication_agent_ref (AuthenticationAgent *agent);
99 static void authentication_agent_unref (AuthenticationAgent *agent);
100
101 static void authentication_agent_initiate_challenge (AuthenticationAgent *agent,
102 PolkitSubject *subject,
103 PolkitIdentity *user_of_subject,
104 PolkitBackendInteractiveAuthority *authority,
105 const gchar *action_id,
106 PolkitDetails *details,
107 PolkitSubject *caller,
108 PolkitImplicitAuthorization implicit_authorization,
109 GCancellable *cancellable,
110 AuthenticationAgentCallback callback,
111 gpointer user_data);
112
113 static PolkitSubject *authentication_agent_get_scope (AuthenticationAgent *agent);
114
115 static AuthenticationAgent *get_authentication_agent_for_subject (PolkitBackendInteractiveAuthority *authority,
116 PolkitSubject *subject);
117
118
119 static AuthenticationSession *get_authentication_session_for_uid_and_cookie (PolkitBackendInteractiveAuthority *authority,
120 uid_t uid,
121 const gchar *cookie);
122
123 static GList *get_authentication_sessions_initiated_by_system_bus_unique_name (PolkitBackendInteractiveAuthority *authority,
124 const gchar *system_bus_unique_name);
125
126 static void authentication_session_cancel (AuthenticationSession *session);
127
128 /* ---------------------------------------------------------------------------------------------------- */
129
130 static void polkit_backend_interactive_authority_system_bus_name_owner_changed (PolkitBackendInteractiveAuthority *authority,
131 const gchar *name,
132 const gchar *old_owner,
133 const gchar *new_owner);
134
135 static GList *polkit_backend_interactive_authority_enumerate_actions (PolkitBackendAuthority *authority,
136 PolkitSubject *caller,
137 const gchar *locale,
138 GError **error);
139
140 static void polkit_backend_interactive_authority_check_authorization (PolkitBackendAuthority *authority,
141 PolkitSubject *caller,
142 PolkitSubject *subject,
143 const gchar *action_id,
144 PolkitDetails *details,
145 PolkitCheckAuthorizationFlags flags,
146 GCancellable *cancellable,
147 GAsyncReadyCallback callback,
148 gpointer user_data);
149
150 static PolkitAuthorizationResult *polkit_backend_interactive_authority_check_authorization_finish (
151 PolkitBackendAuthority *authority,
152 GAsyncResult *res,
153 GError **error);
154
155 static PolkitAuthorizationResult *check_authorization_sync (PolkitBackendAuthority *authority,
156 PolkitSubject *caller,
157 PolkitSubject *subject,
158 const gchar *action_id,
159 PolkitDetails *details,
160 PolkitCheckAuthorizationFlags flags,
161 PolkitImplicitAuthorization *out_implicit_authorization,
162 gboolean checking_imply,
163 GError **error);
164
165 static gboolean polkit_backend_interactive_authority_register_authentication_agent (PolkitBackendAuthority *authority,
166 PolkitSubject *caller,
167 PolkitSubject *subject,
168 const gchar *locale,
169 const gchar *object_path,
170 GVariant *options,
171 GError **error);
172
173 static gboolean polkit_backend_interactive_authority_unregister_authentication_agent (PolkitBackendAuthority *authority,
174 PolkitSubject *caller,
175 PolkitSubject *subject,
176 const gchar *object_path,
177 GError **error);
178
179 static gboolean polkit_backend_interactive_authority_authentication_agent_response (PolkitBackendAuthority *authority,
180 PolkitSubject *caller,
181 uid_t uid,
182 const gchar *cookie,
183 PolkitIdentity *identity,
184 GError **error);
185
186 static GList *polkit_backend_interactive_authority_enumerate_temporary_authorizations (PolkitBackendAuthority *authority,
187 PolkitSubject *caller,
188 PolkitSubject *subject,
189 GError **error);
190
191
192 static gboolean polkit_backend_interactive_authority_revoke_temporary_authorizations (PolkitBackendAuthority *authority,
193 PolkitSubject *caller,
194 PolkitSubject *subject,
195 GError **error);
196
197 static gboolean polkit_backend_interactive_authority_revoke_temporary_authorization_by_id (PolkitBackendAuthority *authority,
198 PolkitSubject *caller,
199 const gchar *id,
200 GError **error);
201
202
203 /* ---------------------------------------------------------------------------------------------------- */
204
205 typedef struct
206 {
207 PolkitBackendActionPool *action_pool;
208
209 PolkitBackendSessionMonitor *session_monitor;
210
211 TemporaryAuthorizationStore *temporary_authorization_store;
212
213 /* Maps from PolkitSubject* to AuthenticationAgent* - currently the
214 * following PolkitSubject-derived types are used
215 *
216 * - PolkitSystemBusName - for authentication agents handling interaction for a single well-known name
217 * - typically pkexec(1) launched via e.g. ssh(1) or login(1)
218 *
219 * - PolkitUnixSession - for authentication agents handling interaction for a whole login session
220 * - typically a desktop environment session
221 *
222 */
223 GHashTable *hash_scope_to_authentication_agent;
224
225 GDBusConnection *system_bus_connection;
226 guint name_owner_changed_signal_id;
227
228 guint64 agent_serial;
229
230 guint expiration_seconds;
231 } PolkitBackendInteractiveAuthorityPrivate;
232
233 enum
234 {
235 PROP_EXPIRATION_SECONDS = 1,
236 };
237
238 /* ---------------------------------------------------------------------------------------------------- */
239
240 G_DEFINE_TYPE_WITH_PRIVATE (PolkitBackendInteractiveAuthority,
241 polkit_backend_interactive_authority,
242 POLKIT_BACKEND_TYPE_AUTHORITY);
243
244 static gboolean
245 identity_is_root_user (PolkitIdentity *user)
246 {
247 if (!POLKIT_IS_UNIX_USER (user))
248 return FALSE;
249 return polkit_unix_user_get_uid (POLKIT_UNIX_USER (user)) == 0;
250 }
251
252 /* ---------------------------------------------------------------------------------------------------- */
253
254 static void
255 action_pool_changed (PolkitBackendActionPool *action_pool,
256 PolkitBackendInteractiveAuthority *authority)
257 {
258 g_signal_emit_by_name (authority, "changed");
259 }
260
261
262 /* ---------------------------------------------------------------------------------------------------- */
263
264 static void
265 on_name_owner_changed_signal (GDBusConnection *connection,
266 const gchar *sender_name,
267 const gchar *object_path,
268 const gchar *interface_name,
269 const gchar *signal_name,
270 GVariant *parameters,
271 gpointer user_data)
272 {
273 PolkitBackendInteractiveAuthority *authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (user_data);
274 const gchar *name;
275 const gchar *old_owner;
276 const gchar *new_owner;
277
278 g_variant_get (parameters,
279 "(&s&s&s)",
280 &name,
281 &old_owner,
282 &new_owner);
283
284 polkit_backend_interactive_authority_system_bus_name_owner_changed (authority,
285 name,
286 old_owner,
287 new_owner);
288 }
289
290 /* ---------------------------------------------------------------------------------------------------- */
291
292 static void
293 on_session_monitor_changed (PolkitBackendSessionMonitor *monitor,
294 gpointer user_data)
295 {
296 PolkitBackendInteractiveAuthority *authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (user_data);
297 g_signal_emit_by_name (authority, "sessions-changed");
298 }
299
300 static void
301 polkit_backend_interactive_authority_init (PolkitBackendInteractiveAuthority *authority)
302 {
303 PolkitBackendInteractiveAuthorityPrivate *priv;
304 GError *error;
305
306 const gchar* directories[] = {
307 PACKAGE_SYSCONF_DIR "/polkit-1/actions",
308 "/run/polkit-1/actions",
309 "/usr/local/share/polkit-1/actions",
310 PACKAGE_DATA_DIR "/polkit-1/actions",
311 NULL
312 };
313
314 /* Force registering error domain */
315 (void)POLKIT_ERROR;
316
317 priv = polkit_backend_interactive_authority_get_instance_private (authority);
318
319 priv->action_pool = polkit_backend_action_pool_new (directories);
320 g_signal_connect (priv->action_pool,
321 "changed",
322 (GCallback) action_pool_changed,
323 authority);
324
325 priv->temporary_authorization_store = temporary_authorization_store_new (authority);
326
327 priv->hash_scope_to_authentication_agent = g_hash_table_new_full ((GHashFunc) polkit_subject_hash,
328 (GEqualFunc) polkit_subject_equal,
329 (GDestroyNotify) g_object_unref,
330 (GDestroyNotify) authentication_agent_unref);
331
332 priv->session_monitor = polkit_backend_session_monitor_new ();
333 g_signal_connect (priv->session_monitor,
334 "changed",
335 G_CALLBACK (on_session_monitor_changed),
336 authority);
337
338 error = NULL;
339 priv->system_bus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
340 if (priv->system_bus_connection == NULL)
341 {
342 g_warning ("Error getting system bus: %s", error->message);
343 g_error_free (error);
344 }
345 else
346 {
347 /* TODO: this is a bit inefficient */
348 priv->name_owner_changed_signal_id =
349 g_dbus_connection_signal_subscribe (priv->system_bus_connection,
350 "org.freedesktop.DBus", /* sender */
351 "org.freedesktop.DBus", /* interface */
352 "NameOwnerChanged", /* member */
353 "/org/freedesktop/DBus", /* path */
354 NULL, /* arg0 */
355 G_DBUS_SIGNAL_FLAGS_NONE,
356 on_name_owner_changed_signal,
357 authority,
358 NULL); /* GDestroyNotify */
359 }
360 }
361
362 static void
363 polkit_backend_interactive_authority_get_property (GObject *object,
364 guint prop_id,
365 GValue *value,
366 GParamSpec *pspec)
367 {
368 PolkitBackendInteractiveAuthority *authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (object);
369
370 if (prop_id == PROP_EXPIRATION_SECONDS)
371 g_value_set_uint (value, polkit_backend_interactive_authority_get_expiration_seconds (authority));
372 else
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 }
375
376 static void
377 polkit_backend_interactive_authority_set_property (GObject *object,
378 guint prop_id,
379 const GValue *value,
380 GParamSpec *pspec)
381 {
382 PolkitBackendInteractiveAuthority *authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (object);
383
384 if (prop_id == PROP_EXPIRATION_SECONDS)
385 polkit_backend_interactive_authority_set_expiration_seconds (authority, g_value_get_uint (value));
386 else
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388 }
389
390
391 /**
392 * polkit_backend_interactive_authority_set_expiration_seconds:
393 * @authority: A #PolkitBackendInteractiveAuthority.
394 * @seconds: The expiration time in seconds.
395 *
396 * Sets the expiration time for interactive authorizations by @authority.
397 */
398 void
399 polkit_backend_interactive_authority_set_expiration_seconds (PolkitBackendInteractiveAuthority *authority,
400 guint seconds)
401 {
402 PolkitBackendInteractiveAuthorityPrivate *priv;
403 g_return_if_fail (POLKIT_BACKEND_IS_INTERACTIVE_AUTHORITY (authority));
404
405 priv = polkit_backend_interactive_authority_get_instance_private (authority);
406 priv->expiration_seconds = seconds;
407 }
408
409 /**
410 * polkit_backend_interactive_authority_get_expiration_seconds:
411 * @authority: A #PolkitBackendInteractiveAuthority.
412 *
413 * Gets the expiration time for interactive authorizations by @authority.
414 *
415 * Returns: The expiration time in seconds.
416 */
417 guint
418 polkit_backend_interactive_authority_get_expiration_seconds (PolkitBackendInteractiveAuthority *authority)
419 {
420 PolkitBackendInteractiveAuthorityPrivate *priv;
421 g_return_val_if_fail (POLKIT_BACKEND_IS_INTERACTIVE_AUTHORITY (authority), 0);
422
423 priv = polkit_backend_interactive_authority_get_instance_private (authority);
424 return priv->expiration_seconds;
425 }
426
427 static void
428 polkit_backend_interactive_authority_finalize (GObject *object)
429 {
430 PolkitBackendInteractiveAuthority *interactive_authority;
431 PolkitBackendInteractiveAuthorityPrivate *priv;
432
433 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (object);
434 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
435
436 if (priv->name_owner_changed_signal_id > 0)
437 g_dbus_connection_signal_unsubscribe (priv->system_bus_connection, priv->name_owner_changed_signal_id);
438
439 if (priv->system_bus_connection != NULL)
440 g_object_unref (priv->system_bus_connection);
441
442 if (priv->action_pool != NULL)
443 g_object_unref (priv->action_pool);
444
445 if (priv->session_monitor != NULL)
446 g_object_unref (priv->session_monitor);
447
448 temporary_authorization_store_free (priv->temporary_authorization_store);
449
450 g_hash_table_unref (priv->hash_scope_to_authentication_agent);
451
452 G_OBJECT_CLASS (polkit_backend_interactive_authority_parent_class)->finalize (object);
453 }
454
455 static const gchar *
456 polkit_backend_interactive_authority_get_name (PolkitBackendAuthority *authority)
457 {
458 return "interactive";
459 }
460
461 static const gchar *
462 polkit_backend_interactive_authority_get_version (PolkitBackendAuthority *authority)
463 {
464 return PACKAGE_VERSION;
465 }
466
467 static PolkitAuthorityFeatures
468 polkit_backend_interactive_authority_get_features (PolkitBackendAuthority *authority)
469 {
470 return POLKIT_AUTHORITY_FEATURES_TEMPORARY_AUTHORIZATION;
471 }
472
473 static void
474 polkit_backend_interactive_authority_class_init (PolkitBackendInteractiveAuthorityClass *klass)
475 {
476 GObjectClass *gobject_class;
477 PolkitBackendAuthorityClass *authority_class;
478
479 gobject_class = G_OBJECT_CLASS (klass);
480 authority_class = POLKIT_BACKEND_AUTHORITY_CLASS (klass);
481
482 gobject_class->finalize = polkit_backend_interactive_authority_finalize;
483 gobject_class->get_property = polkit_backend_interactive_authority_get_property;
484 gobject_class->set_property = polkit_backend_interactive_authority_set_property;
485
486 authority_class->get_name = polkit_backend_interactive_authority_get_name;
487 authority_class->get_version = polkit_backend_interactive_authority_get_version;
488 authority_class->get_features = polkit_backend_interactive_authority_get_features;
489 authority_class->enumerate_actions = polkit_backend_interactive_authority_enumerate_actions;
490 authority_class->check_authorization = polkit_backend_interactive_authority_check_authorization;
491 authority_class->check_authorization_finish = polkit_backend_interactive_authority_check_authorization_finish;
492 authority_class->register_authentication_agent = polkit_backend_interactive_authority_register_authentication_agent;
493 authority_class->unregister_authentication_agent = polkit_backend_interactive_authority_unregister_authentication_agent;
494 authority_class->authentication_agent_response = polkit_backend_interactive_authority_authentication_agent_response;
495 authority_class->enumerate_temporary_authorizations = polkit_backend_interactive_authority_enumerate_temporary_authorizations;
496 authority_class->revoke_temporary_authorizations = polkit_backend_interactive_authority_revoke_temporary_authorizations;
497 authority_class->revoke_temporary_authorization_by_id = polkit_backend_interactive_authority_revoke_temporary_authorization_by_id;
498
499 /**
500 * PolkitBackendInteractiveAuthority:expiration_seconds:
501 *
502 * The expiration time of interactive authorizations.
503 */
504 g_object_class_install_property (gobject_class,
505 PROP_EXPIRATION_SECONDS,
506 g_param_spec_uint ("expiration-seconds",
507 "Expiration Seconds",
508 "The expiration time of interactive authorizations in seconds",
509 0,
510 G_MAXUINT,
511 5 * 60, /* default to 5 minutes */
512 G_PARAM_CONSTRUCT |
513 G_PARAM_READWRITE |
514 G_PARAM_STATIC_NAME |
515 G_PARAM_STATIC_BLURB |
516 G_PARAM_STATIC_NICK));
517 }
518
519 /* ---------------------------------------------------------------------------------------------------- */
520
521 static GList *
522 polkit_backend_interactive_authority_enumerate_actions (PolkitBackendAuthority *authority,
523 PolkitSubject *caller,
524 const gchar *interactivee,
525 GError **error)
526 {
527 PolkitBackendInteractiveAuthority *interactive_authority;
528 PolkitBackendInteractiveAuthorityPrivate *priv;
529 GList *actions;
530
531 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
532 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
533
534 actions = polkit_backend_action_pool_get_all_actions (priv->action_pool, interactivee);
535
536 return actions;
537 }
538
539 /* ---------------------------------------------------------------------------------------------------- */
540
541 struct AuthenticationAgent
542 {
543 volatile gint ref_count;
544
545 uid_t creator_uid;
546 PolkitSubject *scope;
547 guint64 serial;
548
549 gchar *locale;
550 GVariant *registration_options;
551 gchar *object_path;
552 gchar *unique_system_bus_name;
553 GRand *cookie_pool;
554 gchar *cookie_prefix;
555 guint64 cookie_serial;
556
557 GDBusProxy *proxy;
558
559 GList *active_sessions;
560 };
561
562 /* TODO: should probably move to PolkitSubject
563 * (also see copy in src/programs/pkcheck.c)
564 *
565 * Also, can't really trust the cmdline... but might be useful in the logs anyway.
566 */
567 static gchar *
568 _polkit_subject_get_cmdline (PolkitSubject *subject)
569 {
570 PolkitSubject *process;
571 gchar *ret;
572 gint pid;
573 gchar *filename;
574 gchar *contents;
575 gsize contents_len;
576 GError *error;
577 guint n;
578
579 g_return_val_if_fail (subject != NULL, NULL);
580
581 error = NULL;
582
583 ret = NULL;
584 process = NULL;
585 filename = NULL;
586 contents = NULL;
587
588 if (POLKIT_IS_UNIX_PROCESS (subject))
589 {
590 process = g_object_ref (subject);
591 }
592 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
593 {
594 process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject),
595 NULL,
596 &error);
597 if (process == NULL)
598 {
599 g_printerr ("Error getting process for system bus name `%s': %s\n",
600 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject)),
601 error->message);
602 g_error_free (error);
603 goto out;
604 }
605 }
606 else
607 {
608 g_warning ("Unknown subject type passed to _polkit_subject_get_cmdline()");
609 goto out;
610 }
611
612 pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process));
613 if (pid <= 0)
614 {
615 g_debug ("Process is no longer active, cannot fetch cmdline.");
616 goto out;
617 }
618
619 filename = g_strdup_printf ("/proc/%d/cmdline", pid);
620
621 if (!g_file_get_contents (filename,
622 &contents,
623 &contents_len,
624 &error))
625 {
626 g_printerr ("Error opening `%s': %s\n",
627 filename,
628 error->message);
629 g_error_free (error);
630 goto out;
631 }
632
633 if (contents == NULL || contents_len == 0)
634 {
635 goto out;
636 }
637 else
638 {
639 /* The kernel uses '\0' to separate arguments - replace those with a space. */
640 for (n = 0; n < contents_len - 1; n++)
641 {
642 if (contents[n] == '\0')
643 contents[n] = ' ';
644 }
645 ret = g_strdup (contents);
646 g_strstrip (ret);
647 }
648
649 out:
650 g_free (filename);
651 g_free (contents);
652 if (process != NULL)
653 g_object_unref (process);
654 return ret;
655 }
656
657 /* TODO: possibly remove this function altogether */
658 G_GNUC_UNUSED static void
659 log_result (PolkitBackendInteractiveAuthority *authority,
660 const gchar *action_id,
661 PolkitSubject *subject,
662 PolkitSubject *caller,
663 PolkitAuthorizationResult *result)
664 {
665 PolkitBackendInteractiveAuthorityPrivate *priv;
666 PolkitIdentity *user_of_subject;
667 const gchar *log_result_str;
668 gchar *subject_str;
669 gchar *user_of_subject_str;
670 gchar *caller_str;
671 gchar *subject_cmdline;
672 gchar *caller_cmdline;
673
674 priv = polkit_backend_interactive_authority_get_instance_private (authority);
675
676 log_result_str = "DENYING";
677 if (polkit_authorization_result_get_is_authorized (result))
678 log_result_str = "ALLOWING";
679
680 user_of_subject = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor, subject, NULL, NULL);
681
682 subject_str = polkit_subject_to_string (subject);
683
684 if (user_of_subject != NULL)
685 user_of_subject_str = polkit_identity_to_string (user_of_subject);
686 else
687 user_of_subject_str = g_strdup ("<unknown>");
688 caller_str = polkit_subject_to_string (caller);
689
690 subject_cmdline = _polkit_subject_get_cmdline (subject);
691 if (subject_cmdline == NULL)
692 subject_cmdline = g_strdup ("<unknown>");
693
694 caller_cmdline = _polkit_subject_get_cmdline (caller);
695 if (caller_cmdline == NULL)
696 caller_cmdline = g_strdup ("<unknown>");
697
698 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
699 LOG_LEVEL_INFO,
700 "%s action %s for %s [%s] owned by %s (check requested by %s [%s])",
701 log_result_str,
702 action_id,
703 subject_str,
704 subject_cmdline,
705 user_of_subject_str,
706 caller_str,
707 caller_cmdline);
708
709 if (user_of_subject != NULL)
710 g_object_unref (user_of_subject);
711 g_free (subject_str);
712 g_free (user_of_subject_str);
713 g_free (caller_str);
714 g_free (subject_cmdline);
715 g_free (caller_cmdline);
716 }
717
718 static void
719 check_authorization_challenge_cb (AuthenticationAgent *agent,
720 PolkitSubject *subject,
721 PolkitIdentity *user_of_subject,
722 PolkitSubject *caller,
723 PolkitBackendInteractiveAuthority *authority,
724 const gchar *action_id,
725 PolkitDetails *details,
726 PolkitImplicitAuthorization implicit_authorization,
727 gboolean authentication_success,
728 gboolean was_dismissed,
729 PolkitIdentity *authenticated_identity,
730 gpointer user_data)
731 {
732 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
733 PolkitBackendInteractiveAuthorityPrivate *priv;
734 PolkitAuthorizationResult *result;
735 gchar *scope_str;
736 gchar *subject_str;
737 gchar *user_of_subject_str;
738 gchar *authenticated_identity_str;
739 gchar *subject_cmdline;
740 gboolean is_temp;
741
742 priv = polkit_backend_interactive_authority_get_instance_private (authority);
743
744 result = NULL;
745
746 scope_str = polkit_subject_to_string (agent->scope);
747 subject_str = polkit_subject_to_string (subject);
748 user_of_subject_str = polkit_identity_to_string (user_of_subject);
749 authenticated_identity_str = NULL;
750 if (authenticated_identity != NULL)
751 authenticated_identity_str = polkit_identity_to_string (authenticated_identity);
752
753 subject_cmdline = _polkit_subject_get_cmdline (subject);
754 if (subject_cmdline == NULL)
755 subject_cmdline = g_strdup ("<unknown>");
756
757 g_debug ("In check_authorization_challenge_cb\n"
758 " subject %s\n"
759 " action_id %s\n"
760 " was_dismissed %d\n"
761 " authentication_success %d\n",
762 subject_str,
763 action_id,
764 was_dismissed,
765 authentication_success);
766
767 if (implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED_RETAINED ||
768 implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_ADMINISTRATOR_AUTHENTICATION_REQUIRED_RETAINED)
769 polkit_details_insert (details, "polkit.retains_authorization_after_challenge", "true");
770
771 is_temp = FALSE;
772 if (authentication_success)
773 {
774 /* store temporary authorization depending on value of implicit_authorization */
775 if (implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED_RETAINED ||
776 implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_ADMINISTRATOR_AUTHENTICATION_REQUIRED_RETAINED)
777 {
778 const gchar *id;
779
780 is_temp = TRUE;
781
782 id = temporary_authorization_store_add_authorization (priv->temporary_authorization_store,
783 priv->expiration_seconds,
784 subject,
785 authentication_agent_get_scope (agent),
786 action_id);
787
788 polkit_details_insert (details, "polkit.temporary_authorization_id", id);
789
790 /* we've added a temporary authorization, let the user know */
791 g_signal_emit_by_name (authority, "changed");
792 }
793 result = polkit_authorization_result_new (TRUE, FALSE, details);
794 }
795 else
796 {
797 /* TODO: maybe return set is_challenge? */
798 if (was_dismissed)
799 polkit_details_insert (details, "polkit.dismissed", "true");
800 result = polkit_authorization_result_new (FALSE, FALSE, details);
801 }
802
803 /* Log the event */
804 if (authentication_success)
805 {
806 if (is_temp)
807 {
808 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
809 LOG_LEVEL_INFO,
810 "Operator of %s successfully authenticated as %s to gain "
811 "TEMPORARY authorization for action %s for %s [%s] (owned by %s)",
812 scope_str,
813 authenticated_identity_str,
814 action_id,
815 subject_str,
816 subject_cmdline,
817 user_of_subject_str);
818 }
819 else
820 {
821 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
822 LOG_LEVEL_INFO,
823 "Operator of %s successfully authenticated as %s to gain "
824 "ONE-SHOT authorization for action %s for %s [%s] (owned by %s)",
825 scope_str,
826 authenticated_identity_str,
827 action_id,
828 subject_str,
829 subject_cmdline,
830 user_of_subject_str);
831 }
832 }
833 else
834 {
835 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
836 LOG_LEVEL_NOTICE,
837 "Operator of %s FAILED to authenticate to gain "
838 "authorization for action %s for %s [%s] (owned by %s)",
839 scope_str,
840 action_id,
841 subject_str,
842 subject_cmdline,
843 user_of_subject_str);
844 }
845
846 /* log_result (authority, action_id, subject, caller, result); */
847
848 g_simple_async_result_set_op_res_gpointer (simple,
849 result,
850 g_object_unref);
851 g_simple_async_result_complete (simple);
852 g_object_unref (simple);
853
854 g_free (subject_cmdline);
855 g_free (authenticated_identity_str);
856 g_free (user_of_subject_str);
857 g_free (subject_str);
858 g_free (scope_str);
859 }
860
861 static PolkitAuthorizationResult *
862 polkit_backend_interactive_authority_check_authorization_finish (PolkitBackendAuthority *authority,
863 GAsyncResult *res,
864 GError **error)
865 {
866 GSimpleAsyncResult *simple;
867 PolkitAuthorizationResult *result;
868
869 simple = G_SIMPLE_ASYNC_RESULT (res);
870
871 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_backend_interactive_authority_check_authorization);
872
873 result = NULL;
874
875 if (g_simple_async_result_propagate_error (simple, error))
876 goto out;
877
878 result = g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
879
880 out:
881 return result;
882 }
883
884 static gboolean
885 may_identity_check_authorization (PolkitBackendInteractiveAuthority *interactive_authority,
886 const gchar *action_id,
887 PolkitIdentity *identity)
888 {
889 PolkitBackendInteractiveAuthorityPrivate *priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
890 gboolean ret = FALSE;
891 PolkitActionDescription *action_desc = NULL;
892 const gchar *owners = NULL;
893 gchar **tokens = NULL;
894 guint n;
895
896 /* uid 0 may check anything */
897 if (identity_is_root_user (identity))
898 {
899 ret = TRUE;
900 goto out;
901 }
902
903 action_desc = polkit_backend_action_pool_get_action (priv->action_pool, action_id, NULL);
904 if (action_desc == NULL)
905 goto out;
906
907 owners = polkit_action_description_get_annotation (action_desc, "org.freedesktop.policykit.owner");
908 if (owners == NULL)
909 goto out;
910
911 tokens = g_strsplit (owners, " ", 0);
912 for (n = 0; tokens != NULL && tokens[n] != NULL; n++)
913 {
914 PolkitIdentity *owner_identity;
915 GError *error = NULL;
916 owner_identity = polkit_identity_from_string (tokens[n], &error);
917 if (owner_identity == NULL)
918 {
919 g_warning ("Error parsing owner identity %d of action_id %s: %s (%s, %d)",
920 n, action_id, error->message, g_quark_to_string (error->domain), error->code);
921 g_error_free (error);
922 continue;
923 }
924 if (polkit_identity_equal (identity, owner_identity))
925 {
926 ret = TRUE;
927 g_object_unref (owner_identity);
928 goto out;
929 }
930 g_object_unref (owner_identity);
931 }
932
933 out:
934 g_clear_object (&action_desc);
935 g_strfreev (tokens);
936
937 return ret;
938 }
939
940 static void
941 polkit_backend_interactive_authority_check_authorization (PolkitBackendAuthority *authority,
942 PolkitSubject *caller,
943 PolkitSubject *subject,
944 const gchar *action_id,
945 PolkitDetails *details,
946 PolkitCheckAuthorizationFlags flags,
947 GCancellable *cancellable,
948 GAsyncReadyCallback callback,
949 gpointer user_data)
950 {
951 PolkitBackendInteractiveAuthority *interactive_authority;
952 PolkitBackendInteractiveAuthorityPrivate *priv;
953 gchar *caller_str;
954 gchar *subject_str;
955 PolkitIdentity *user_of_caller;
956 PolkitIdentity *user_of_subject;
957 gboolean user_of_subject_matches;
958 gchar *user_of_caller_str;
959 gchar *user_of_subject_str;
960 PolkitAuthorizationResult *result;
961 PolkitImplicitAuthorization implicit_authorization;
962 GError *error;
963 GSimpleAsyncResult *simple;
964 gboolean has_details;
965 gchar **detail_keys;
966
967 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
968 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
969
970 error = NULL;
971 caller_str = NULL;
972 subject_str = NULL;
973 user_of_caller = NULL;
974 user_of_subject = NULL;
975 user_of_caller_str = NULL;
976 user_of_subject_str = NULL;
977 result = NULL;
978
979 simple = g_simple_async_result_new (G_OBJECT (authority),
980 callback,
981 user_data,
982 polkit_backend_interactive_authority_check_authorization);
983
984 /* handle being called from ourselves */
985 if (caller == NULL)
986 {
987 /* TODO: this is kind of a hack */
988 GDBusConnection *system_bus;
989 system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
990 caller = polkit_system_bus_name_new (g_dbus_connection_get_unique_name (system_bus));
991 g_object_unref (system_bus);
992 }
993
994 caller_str = polkit_subject_to_string (caller);
995 subject_str = polkit_subject_to_string (subject);
996
997 g_debug ("%s is inquiring whether %s is authorized for %s",
998 caller_str,
999 subject_str,
1000 action_id);
1001
1002 user_of_caller = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor,
1003 caller, NULL,
1004 &error);
1005 if (error != NULL)
1006 {
1007 g_simple_async_result_set_from_error (simple, error);
1008 g_simple_async_result_complete (simple);
1009 g_object_unref (simple);
1010 g_error_free (error);
1011 goto out;
1012 }
1013
1014 user_of_caller_str = polkit_identity_to_string (user_of_caller);
1015 g_debug (" user of caller is %s", user_of_caller_str);
1016
1017 user_of_subject = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor,
1018 subject, &user_of_subject_matches,
1019 &error);
1020 if (error != NULL)
1021 {
1022 g_simple_async_result_set_from_error (simple, error);
1023 g_simple_async_result_complete (simple);
1024 g_object_unref (simple);
1025 g_error_free (error);
1026 goto out;
1027 }
1028
1029 user_of_subject_str = polkit_identity_to_string (user_of_subject);
1030 g_debug (" user of subject is %s", user_of_subject_str);
1031
1032 has_details = FALSE;
1033 if (details != NULL)
1034 {
1035 detail_keys = polkit_details_get_keys (details);
1036 if (detail_keys != NULL)
1037 {
1038 if (g_strv_length (detail_keys) > 0)
1039 has_details = TRUE;
1040 g_strfreev (detail_keys);
1041 }
1042 }
1043
1044 /* Not anyone is allowed to check that process XYZ is allowed to do ABC.
1045 * We allow this if, and only if,
1046 *
1047 * - processes may check for another process owned by the *same* user but not
1048 * if details are passed (otherwise you'd be able to spoof the dialog);
1049 * the caller supplies the user_of_subject value, so we additionally
1050 * require it to match at least at one point in time (via
1051 * user_of_subject_matches).
1052 *
1053 * - processes running as uid 0 may check anything and pass any details
1054 *
1055 * - if the action_id has the "org.freedesktop.policykit.owner" annotation
1056 * then any uid referenced by that annotation is also allowed to check
1057 * anything and pass any details
1058 */
1059 if (!user_of_subject_matches
1060 || !polkit_identity_equal (user_of_caller, user_of_subject)
1061 || has_details)
1062 {
1063 if (!may_identity_check_authorization (interactive_authority, action_id, user_of_caller))
1064 {
1065 if (has_details)
1066 {
1067 g_simple_async_result_set_error (simple,
1068 POLKIT_ERROR,
1069 POLKIT_ERROR_NOT_AUTHORIZED,
1070 "Only trusted callers (e.g. uid 0 or an action owner) can use CheckAuthorization() and "
1071 "pass details");
1072 }
1073 else
1074 {
1075 g_simple_async_result_set_error (simple,
1076 POLKIT_ERROR,
1077 POLKIT_ERROR_NOT_AUTHORIZED,
1078 "Only trusted callers (e.g. uid 0 or an action owner) can use CheckAuthorization() for "
1079 "subjects belonging to other identities");
1080 }
1081 g_simple_async_result_complete (simple);
1082 g_object_unref (simple);
1083 goto out;
1084 }
1085 }
1086
1087 implicit_authorization = POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED;
1088 result = check_authorization_sync (authority,
1089 caller,
1090 subject,
1091 action_id,
1092 details,
1093 flags,
1094 &implicit_authorization,
1095 FALSE, /* checking_imply */
1096 &error);
1097 if (error != NULL)
1098 {
1099 g_simple_async_result_set_from_error (simple, error);
1100 g_simple_async_result_complete (simple);
1101 g_object_unref (simple);
1102 g_error_free (error);
1103 goto out;
1104 }
1105
1106 /* Caller is up for a challenge! With light sabers! Use an authentication agent if one exists... */
1107 if (polkit_authorization_result_get_is_challenge (result) &&
1108 (flags & POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION))
1109 {
1110 AuthenticationAgent *agent;
1111
1112 agent = get_authentication_agent_for_subject (interactive_authority, subject);
1113 if (agent != NULL)
1114 {
1115 g_object_unref (result);
1116 result = NULL;
1117
1118 g_debug (" using authentication agent for challenge");
1119
1120 authentication_agent_initiate_challenge (agent,
1121 subject,
1122 user_of_subject,
1123 interactive_authority,
1124 action_id,
1125 details,
1126 caller,
1127 implicit_authorization,
1128 cancellable,
1129 check_authorization_challenge_cb,
1130 simple);
1131
1132 /* keep going */
1133 goto out;
1134 }
1135 }
1136
1137 /* log_result (interactive_authority, action_id, subject, caller, result); */
1138
1139 /* Otherwise just return the result */
1140 g_simple_async_result_set_op_res_gpointer (simple,
1141 g_object_ref (result),
1142 g_object_unref);
1143 g_simple_async_result_complete (simple);
1144 g_object_unref (simple);
1145
1146 out:
1147
1148 if (user_of_caller != NULL)
1149 g_object_unref (user_of_caller);
1150
1151 if (user_of_subject != NULL)
1152 g_object_unref (user_of_subject);
1153
1154 g_free (caller_str);
1155 g_free (subject_str);
1156 g_free (user_of_caller_str);
1157 g_free (user_of_subject_str);
1158
1159 if (result != NULL)
1160 g_object_unref (result);
1161 }
1162
1163 /* ---------------------------------------------------------------------------------------------------- */
1164
1165 static PolkitAuthorizationResult *
1166 check_authorization_sync (PolkitBackendAuthority *authority,
1167 PolkitSubject *caller,
1168 PolkitSubject *subject,
1169 const gchar *action_id,
1170 PolkitDetails *details,
1171 PolkitCheckAuthorizationFlags flags,
1172 PolkitImplicitAuthorization *out_implicit_authorization,
1173 gboolean checking_imply,
1174 GError **error)
1175 {
1176 PolkitBackendInteractiveAuthority *interactive_authority;
1177 PolkitBackendInteractiveAuthorityPrivate *priv;
1178 PolkitAuthorizationResult *result;
1179 PolkitIdentity *user_of_subject;
1180 PolkitSubject *session_for_subject;
1181 gchar *subject_str;
1182 GList *groups_of_user;
1183 PolkitActionDescription *action_desc;
1184 gboolean session_is_local;
1185 gboolean session_is_active;
1186 PolkitImplicitAuthorization implicit_authorization;
1187 const gchar *tmp_authz_id;
1188 GList *actions;
1189 GList *l;
1190
1191 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
1192 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
1193
1194 result = NULL;
1195
1196 actions = NULL;
1197 user_of_subject = NULL;
1198 groups_of_user = NULL;
1199 subject_str = NULL;
1200 session_for_subject = NULL;
1201
1202 session_is_local = FALSE;
1203 session_is_active = FALSE;
1204
1205 subject_str = polkit_subject_to_string (subject);
1206
1207 g_debug ("checking whether %s is authorized for %s",
1208 subject_str,
1209 action_id);
1210
1211 /* get the action description */
1212 action_desc = polkit_backend_action_pool_get_action (priv->action_pool,
1213 action_id,
1214 NULL);
1215
1216 if (action_desc == NULL)
1217 {
1218 g_set_error (error,
1219 POLKIT_ERROR,
1220 POLKIT_ERROR_FAILED,
1221 "Action %s is not registered",
1222 action_id);
1223 goto out;
1224 }
1225
1226 /* every subject has a user; this is supplied by the client, so we rely
1227 * on the caller to validate its acceptability. */
1228 user_of_subject = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor,
1229 subject, NULL,
1230 error);
1231 if (user_of_subject == NULL)
1232 goto out;
1233
1234 /* special case: uid 0, root, is _always_ authorized for anything */
1235 if (!(flags & POLKIT_CHECK_AUTHORIZATION_FLAGS_ALWAYS_CHECK) && identity_is_root_user (user_of_subject))
1236 {
1237 result = polkit_authorization_result_new (TRUE, FALSE, NULL);
1238 goto out;
1239 }
1240
1241 /* a subject *may* be in a session */
1242 session_for_subject = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
1243 subject,
1244 NULL);
1245 g_debug (" %p", session_for_subject);
1246 if (session_for_subject != NULL)
1247 {
1248 session_is_local = polkit_backend_session_monitor_is_session_local (priv->session_monitor, session_for_subject);
1249 session_is_active = polkit_backend_session_monitor_is_session_active (priv->session_monitor, session_for_subject);
1250
1251 g_debug (" subject is in session %s (local=%d active=%d)",
1252 polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session_for_subject)),
1253 session_is_local,
1254 session_is_active);
1255 }
1256
1257 /* find the implicit authorization to use; it depends on is_local and is_active */
1258 if (session_is_local)
1259 {
1260 if (session_is_active)
1261 implicit_authorization = polkit_action_description_get_implicit_active (action_desc);
1262 else
1263 implicit_authorization = polkit_action_description_get_implicit_inactive (action_desc);
1264 }
1265 else
1266 {
1267 implicit_authorization = polkit_action_description_get_implicit_any (action_desc);
1268 }
1269
1270 /* allow subclasses to rewrite implicit_authorization */
1271 implicit_authorization = polkit_backend_interactive_authority_check_authorization_sync (interactive_authority,
1272 caller,
1273 subject,
1274 user_of_subject,
1275 session_is_local,
1276 session_is_active,
1277 action_id,
1278 details,
1279 implicit_authorization);
1280 /* first see if there's an implicit authorization for subject available */
1281 if (implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_AUTHORIZED)
1282 {
1283 g_debug (" is authorized (has implicit authorization local=%d active=%d)",
1284 session_is_local,
1285 session_is_active);
1286 result = polkit_authorization_result_new (TRUE, FALSE, details);
1287 goto out;
1288 }
1289
1290 /* then see if there's a temporary authorization for the subject */
1291 if (temporary_authorization_store_has_authorization (priv->temporary_authorization_store,
1292 subject,
1293 action_id,
1294 &tmp_authz_id))
1295 {
1296
1297 g_debug (" is authorized (has temporary authorization)");
1298 polkit_details_insert (details, "polkit.temporary_authorization_id", tmp_authz_id);
1299 result = polkit_authorization_result_new (TRUE, FALSE, details);
1300 goto out;
1301 }
1302
1303 /* then see if implied by another action that the subject is authorized for
1304 * (but only one level deep to avoid infinite recursion)
1305 *
1306 * TODO: if this is slow, we can maintain a hash table for looking up what
1307 * actions implies a given action
1308 */
1309 if (!checking_imply)
1310 {
1311 actions = polkit_backend_action_pool_get_all_actions (priv->action_pool, NULL);
1312 for (l = actions; l != NULL; l = l->next)
1313 {
1314 PolkitActionDescription *imply_ad = POLKIT_ACTION_DESCRIPTION (l->data);
1315 const gchar *imply;
1316 imply = polkit_action_description_get_annotation (imply_ad, "org.freedesktop.policykit.imply");
1317 if (imply != NULL)
1318 {
1319 gchar **tokens;
1320 guint n;
1321 tokens = g_strsplit (imply, " ", 0);
1322 for (n = 0; tokens[n] != NULL; n++)
1323 {
1324 if (g_strcmp0 (tokens[n], action_id) == 0)
1325 {
1326 PolkitAuthorizationResult *implied_result = NULL;
1327 PolkitImplicitAuthorization implied_implicit_authorization;
1328 GError *implied_error = NULL;
1329 const gchar *imply_action_id;
1330
1331 imply_action_id = polkit_action_description_get_action_id (imply_ad);
1332
1333 /* g_debug ("%s is implied by %s, checking", action_id, imply_action_id); */
1334 implied_result = check_authorization_sync (authority, caller, subject,
1335 imply_action_id,
1336 details, flags,
1337 &implied_implicit_authorization, TRUE,
1338 &implied_error);
1339 if (implied_result != NULL)
1340 {
1341 if (polkit_authorization_result_get_is_authorized (implied_result))
1342 {
1343 g_debug (" is authorized (implied by %s)", imply_action_id);
1344 result = implied_result;
1345 /* cleanup */
1346 g_strfreev (tokens);
1347 goto out;
1348 }
1349 g_object_unref (implied_result);
1350 }
1351 if (implied_error != NULL)
1352 g_error_free (implied_error);
1353 }
1354 }
1355 g_strfreev (tokens);
1356 }
1357 }
1358 }
1359
1360 if (implicit_authorization != POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED)
1361 {
1362 if (implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED_RETAINED ||
1363 implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_ADMINISTRATOR_AUTHENTICATION_REQUIRED_RETAINED)
1364 {
1365 polkit_details_insert (details, "polkit.retains_authorization_after_challenge", "1");
1366 }
1367
1368 polkit_details_insert (details, "polkit.result", polkit_implicit_authorization_to_string (implicit_authorization));
1369
1370 result = polkit_authorization_result_new (FALSE, TRUE, details);
1371
1372 /* return implicit_authorization so the caller can use an authentication agent if applicable */
1373 if (out_implicit_authorization != NULL)
1374 *out_implicit_authorization = implicit_authorization;
1375
1376 g_debug (" challenge (implicit_authorization = %s)",
1377 polkit_implicit_authorization_to_string (implicit_authorization));
1378 }
1379 else
1380 {
1381 result = polkit_authorization_result_new (FALSE, FALSE, details);
1382 g_debug (" not authorized");
1383 }
1384 out:
1385 g_list_foreach (actions, (GFunc) g_object_unref, NULL);
1386 g_list_free (actions);
1387
1388 g_free (subject_str);
1389
1390 g_list_foreach (groups_of_user, (GFunc) g_object_unref, NULL);
1391 g_list_free (groups_of_user);
1392
1393 if (user_of_subject != NULL)
1394 g_object_unref (user_of_subject);
1395
1396 if (session_for_subject != NULL)
1397 g_object_unref (session_for_subject);
1398
1399 if (action_desc != NULL)
1400 g_object_unref (action_desc);
1401
1402 g_debug (" ");
1403
1404 return result;
1405 }
1406
1407 /* ---------------------------------------------------------------------------------------------------- */
1408
1409 /**
1410 * polkit_backend_interactive_authority_get_admin_identities:
1411 * @authority: A #PolkitBackendInteractiveAuthority.
1412 * @caller: The subject that is inquiring whether @subject is authorized.
1413 * @subject: The subject we are about to authenticate for.
1414 * @user_for_subject: The user of the subject we are about to authenticate for.
1415 * @subject_is_local: %TRUE if the session for @subject is local.
1416 * @subject_is_active: %TRUE if the session for @subject is active.
1417 * @action_id: The action we are about to authenticate for.
1418 * @details: Details about the action.
1419 *
1420 * Gets a list of identities to use for administrator authentication.
1421 *
1422 * The default implementation returns a list with a single element for the super user.
1423 *
1424 * Returns: A list of #PolkitIdentity objects. Free each element
1425 * g_object_unref(), then free the list with g_list_free().
1426 */
1427 GList *
1428 polkit_backend_interactive_authority_get_admin_identities (PolkitBackendInteractiveAuthority *authority,
1429 PolkitSubject *caller,
1430 PolkitSubject *subject,
1431 PolkitIdentity *user_for_subject,
1432 gboolean subject_is_local,
1433 gboolean subject_is_active,
1434 const gchar *action_id,
1435 PolkitDetails *details)
1436 {
1437 PolkitBackendInteractiveAuthorityClass *klass;
1438 GList *ret = NULL;
1439
1440 klass = POLKIT_BACKEND_INTERACTIVE_AUTHORITY_GET_CLASS (authority);
1441
1442 if (klass->get_admin_identities != NULL)
1443 {
1444 ret = klass->get_admin_identities (authority,
1445 caller,
1446 subject,
1447 user_for_subject,
1448 subject_is_local,
1449 subject_is_active,
1450 action_id,
1451 details);
1452 }
1453
1454 return ret;
1455 }
1456
1457 /**
1458 * polkit_backend_interactive_authority_check_authorization_sync:
1459 * @authority: A #PolkitBackendInteractiveAuthority.
1460 * @caller: The subject that is inquiring whether @subject is authorized.
1461 * @subject: The subject we are checking an authorization for.
1462 * @user_for_subject: The user of the subject we are checking an authorization for.
1463 * @subject_is_local: %TRUE if the session for @subject is local.
1464 * @subject_is_active: %TRUE if the session for @subject is active.
1465 * @action_id: The action we are checking an authorization for.
1466 * @details: Details about the action.
1467 * @implicit: A #PolkitImplicitAuthorization value computed from the policy file and @subject.
1468 *
1469 * Checks whether @subject is authorized to perform the action
1470 * specified by @action_id and @details. The implementation may append
1471 * key/value pairs to @details to return extra information to @caller.
1472 *
1473 * The default implementation of this method simply returns @implicit.
1474 *
1475 * Returns: A #PolkitImplicitAuthorization that specifies if the subject is authorized or whether
1476 * authentication is required.
1477 */
1478 PolkitImplicitAuthorization
1479 polkit_backend_interactive_authority_check_authorization_sync (PolkitBackendInteractiveAuthority *authority,
1480 PolkitSubject *caller,
1481 PolkitSubject *subject,
1482 PolkitIdentity *user_for_subject,
1483 gboolean subject_is_local,
1484 gboolean subject_is_active,
1485 const gchar *action_id,
1486 PolkitDetails *details,
1487 PolkitImplicitAuthorization implicit)
1488 {
1489 PolkitBackendInteractiveAuthorityClass *klass;
1490 PolkitImplicitAuthorization ret;
1491
1492 klass = POLKIT_BACKEND_INTERACTIVE_AUTHORITY_GET_CLASS (authority);
1493
1494 if (klass->check_authorization_sync == NULL)
1495 {
1496 ret = implicit;
1497 }
1498 else
1499 {
1500 ret = klass->check_authorization_sync (authority,
1501 caller,
1502 subject,
1503 user_for_subject,
1504 subject_is_local,
1505 subject_is_active,
1506 action_id,
1507 details,
1508 implicit);
1509 }
1510
1511 return ret;
1512 }
1513
1514 /**
1515 * polkit_backend_interactive_authority_reload:
1516 * @authority: A #PolkitBackendInteractiveAuthority.
1517 *
1518 * Reload configuration files.
1519 */
1520 void
1521 polkit_backend_interactive_authority_reload (PolkitBackendInteractiveAuthority *authority)
1522 {
1523 PolkitBackendInteractiveAuthorityPrivate *priv;
1524 PolkitBackendJsAuthority *jsauthority;
1525
1526 if (!authority)
1527 return;
1528
1529 priv = polkit_backend_interactive_authority_get_instance_private (authority);
1530 polkit_backend_action_pool_reload (priv->action_pool);
1531
1532 jsauthority = POLKIT_BACKEND_JS_AUTHORITY (authority);
1533 polkit_backend_common_reload_scripts (jsauthority);
1534 }
1535
1536 /* ---------------------------------------------------------------------------------------------------- */
1537
1538 struct AuthenticationSession
1539 {
1540 AuthenticationAgent *agent;
1541
1542 gchar *cookie;
1543
1544 PolkitSubject *subject;
1545
1546 PolkitIdentity *user_of_subject;
1547
1548 PolkitSubject *caller;
1549
1550 PolkitBackendInteractiveAuthority *authority;
1551
1552 GList *identities;
1553
1554 gchar *action_id;
1555
1556 PolkitDetails *details;
1557
1558 gchar *initiated_by_system_bus_unique_name;
1559
1560 PolkitImplicitAuthorization implicit_authorization;
1561
1562 AuthenticationAgentCallback callback;
1563
1564 gpointer user_data;
1565
1566 guint call_id;
1567
1568 gboolean is_authenticated;
1569 PolkitIdentity *authenticated_identity;
1570
1571 GCancellable *cancellable;
1572
1573 gulong cancellable_signal_handler_id;
1574 };
1575
1576 static void
1577 authentication_session_cancelled_cb (GCancellable *cancellable,
1578 AuthenticationSession *session)
1579 {
1580 authentication_session_cancel (session);
1581 }
1582
1583 /* We're not calling this a UUID, but it's basically
1584 * the same thing, just not formatted that way because:
1585 *
1586 * - I'm too lazy to do it
1587 * - If we did, people might think it was actually
1588 * generated from /dev/random, which we're not doing
1589 * because this value doesn't actually need to be
1590 * globally unique.
1591 */
1592 static void
1593 append_rand_u128_str (GString *buf,
1594 GRand *pool)
1595 {
1596 g_string_append_printf (buf, "%08x%08x%08x%08x",
1597 g_rand_int (pool),
1598 g_rand_int (pool),
1599 g_rand_int (pool),
1600 g_rand_int (pool));
1601 }
1602
1603 /* A value that should be unique to the (AuthenticationAgent, AuthenticationSession)
1604 * pair, and not guessable by other agents.
1605 *
1606 * <agent serial> - <agent uuid> - <session serial> - <session uuid>
1607 *
1608 * See http://lists.freedesktop.org/archives/polkit-devel/2015-June/000425.html
1609 *
1610 */
1611 static gchar *
1612 authentication_agent_generate_cookie (AuthenticationAgent *agent)
1613 {
1614 GString *buf = g_string_new ("");
1615
1616 g_string_append (buf, agent->cookie_prefix);
1617
1618 g_string_append_c (buf, '-');
1619 agent->cookie_serial++;
1620 g_string_append_printf (buf, "%" G_GUINT64_FORMAT,
1621 agent->cookie_serial);
1622 g_string_append_c (buf, '-');
1623 append_rand_u128_str (buf, agent->cookie_pool);
1624
1625 return g_string_free (buf, FALSE);
1626 }
1627
1628
1629 static AuthenticationSession *
1630 authentication_session_new (AuthenticationAgent *agent,
1631 PolkitSubject *subject,
1632 PolkitIdentity *user_of_subject,
1633 PolkitSubject *caller,
1634 PolkitBackendInteractiveAuthority *authority,
1635 GList *identities,
1636 const gchar *action_id,
1637 PolkitDetails *details,
1638 const gchar *initiated_by_system_bus_unique_name,
1639 PolkitImplicitAuthorization implicit_authorization,
1640 GCancellable *cancellable,
1641 AuthenticationAgentCallback callback,
1642 gpointer user_data)
1643 {
1644 AuthenticationSession *session;
1645
1646 session = g_new0 (AuthenticationSession, 1);
1647 session->agent = authentication_agent_ref (agent);
1648 session->cookie = authentication_agent_generate_cookie (agent);
1649 session->subject = g_object_ref (subject);
1650 session->user_of_subject = g_object_ref (user_of_subject);
1651 session->caller = g_object_ref (caller);
1652 session->authority = g_object_ref (authority);
1653 session->identities = g_list_copy (identities);
1654 g_list_foreach (session->identities, (GFunc) g_object_ref, NULL);
1655 session->action_id = g_strdup (action_id);
1656 session->details = g_object_ref (details);
1657 session->initiated_by_system_bus_unique_name = g_strdup (initiated_by_system_bus_unique_name);
1658 session->implicit_authorization = implicit_authorization;
1659 session->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1660 session->callback = callback;
1661 session->user_data = user_data;
1662
1663 if (session->cancellable != NULL)
1664 {
1665 session->cancellable_signal_handler_id = g_signal_connect (session->cancellable,
1666 "cancelled",
1667 G_CALLBACK (authentication_session_cancelled_cb),
1668 session);
1669 }
1670
1671 return session;
1672 }
1673
1674 static void
1675 authentication_session_free (AuthenticationSession *session)
1676 {
1677 authentication_agent_unref (session->agent);
1678 g_free (session->cookie);
1679 g_list_foreach (session->identities, (GFunc) g_object_unref, NULL);
1680 g_list_free (session->identities);
1681 g_object_unref (session->subject);
1682 g_object_unref (session->user_of_subject);
1683 g_object_unref (session->caller);
1684 g_object_unref (session->authority);
1685 g_free (session->action_id);
1686 g_object_unref (session->details);
1687 g_free (session->initiated_by_system_bus_unique_name);
1688 if (session->cancellable_signal_handler_id > 0)
1689 g_signal_handler_disconnect (session->cancellable, session->cancellable_signal_handler_id);
1690 if (session->authenticated_identity != NULL)
1691 g_object_unref (session->authenticated_identity);
1692 if (session->cancellable != NULL)
1693 g_object_unref (session->cancellable);
1694 g_free (session);
1695 }
1696
1697 static PolkitSubject *
1698 authentication_agent_get_scope (AuthenticationAgent *agent)
1699 {
1700 return agent->scope;
1701 }
1702
1703 static void
1704 authentication_agent_cancel_all_sessions (AuthenticationAgent *agent)
1705 {
1706 /* cancel all active authentication sessions; use a copy of the list since
1707 * callbacks will modify the list
1708 */
1709 if (agent->active_sessions != NULL)
1710 {
1711 GList *l;
1712 GList *active_sessions;
1713
1714 active_sessions = g_list_copy (agent->active_sessions);
1715 for (l = active_sessions; l != NULL; l = l->next)
1716 {
1717 AuthenticationSession *session = l->data;
1718 authentication_session_cancel (session);
1719 }
1720 g_list_free (active_sessions);
1721 }
1722 }
1723
1724 static AuthenticationAgent *
1725 authentication_agent_ref (AuthenticationAgent *agent)
1726 {
1727 g_atomic_int_inc (&agent->ref_count);
1728 return agent;
1729 }
1730
1731 static void
1732 authentication_agent_unref (AuthenticationAgent *agent)
1733 {
1734 if (g_atomic_int_dec_and_test (&agent->ref_count))
1735 {
1736 if (agent->proxy != NULL)
1737 g_object_unref (agent->proxy);
1738 g_object_unref (agent->scope);
1739 g_free (agent->locale);
1740 g_free (agent->object_path);
1741 g_free (agent->unique_system_bus_name);
1742 if (agent->registration_options != NULL)
1743 g_variant_unref (agent->registration_options);
1744 g_rand_free (agent->cookie_pool);
1745 g_free (agent->cookie_prefix);
1746 g_free (agent);
1747 }
1748 }
1749
1750 static AuthenticationAgent *
1751 authentication_agent_new (guint64 serial,
1752 PolkitSubject *scope,
1753 PolkitIdentity *creator,
1754 const gchar *unique_system_bus_name,
1755 const gchar *locale,
1756 const gchar *object_path,
1757 GVariant *registration_options,
1758 GError **error)
1759 {
1760 AuthenticationAgent *agent;
1761 GDBusProxy *proxy;
1762 PolkitUnixUser *creator_user;
1763
1764 g_assert (POLKIT_IS_UNIX_USER (creator));
1765 creator_user = POLKIT_UNIX_USER (creator);
1766
1767 if (!g_variant_is_object_path (object_path))
1768 {
1769 g_set_error (error, POLKIT_ERROR, POLKIT_ERROR_FAILED,
1770 "Invalid object path '%s'", object_path);
1771 return NULL;
1772 }
1773
1774 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
1775 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1776 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1777 NULL, /* GDBusInterfaceInfo* */
1778 unique_system_bus_name,
1779 object_path,
1780 "org.freedesktop.PolicyKit1.AuthenticationAgent",
1781 NULL, /* GCancellable* */
1782 error);
1783 if (proxy == NULL)
1784 {
1785 g_prefix_error (error, "Failed to construct proxy for agent: " );
1786 return NULL;
1787 }
1788
1789 agent = g_new0 (AuthenticationAgent, 1);
1790 agent->ref_count = 1;
1791 agent->serial = serial;
1792 agent->scope = g_object_ref (scope);
1793 agent->creator_uid = (uid_t)polkit_unix_user_get_uid (creator_user);
1794 agent->object_path = g_strdup (object_path);
1795 agent->unique_system_bus_name = g_strdup (unique_system_bus_name);
1796 agent->locale = g_strdup (locale);
1797 agent->registration_options = registration_options != NULL ? g_variant_ref (registration_options) : NULL;
1798 agent->proxy = proxy;
1799
1800 {
1801 GString *cookie_prefix = g_string_new ("");
1802 GRand *agent_private_rand = g_rand_new ();
1803
1804 g_string_append_printf (cookie_prefix, "%" G_GUINT64_FORMAT "-", agent->serial);
1805
1806 /* Use a uniquely seeded PRNG to get a prefix cookie for this agent,
1807 * whose sequence will not correlate with the per-authentication session
1808 * cookies.
1809 */
1810 append_rand_u128_str (cookie_prefix, agent_private_rand);
1811 g_rand_free (agent_private_rand);
1812
1813 agent->cookie_prefix = g_string_free (cookie_prefix, FALSE);
1814
1815 /* And a newly seeded pool for per-session cookies */
1816 agent->cookie_pool = g_rand_new ();
1817 }
1818
1819 return agent;
1820 }
1821
1822 static AuthenticationAgent *
1823 get_authentication_agent_for_subject (PolkitBackendInteractiveAuthority *authority,
1824 PolkitSubject *subject)
1825 {
1826 PolkitBackendInteractiveAuthorityPrivate *priv;
1827 PolkitSubject *session_for_subject = NULL;
1828 AuthenticationAgent *agent = NULL;
1829 AuthenticationAgent *agent_fallback = NULL;
1830 gboolean fallback = FALSE;
1831
1832 priv = polkit_backend_interactive_authority_get_instance_private (authority);
1833
1834 agent = g_hash_table_lookup (priv->hash_scope_to_authentication_agent, subject);
1835
1836 if (agent == NULL && POLKIT_IS_SYSTEM_BUS_NAME (subject))
1837 {
1838 PolkitSubject *process;
1839 process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject),
1840 NULL,
1841 NULL);
1842 if (process != NULL)
1843 {
1844 agent = g_hash_table_lookup (priv->hash_scope_to_authentication_agent, process);
1845 g_object_unref (process);
1846 }
1847 }
1848
1849 if (agent != NULL)
1850 {
1851 /* We have an agent! Now see if we should use this as a fallback only */
1852 if (agent->registration_options != NULL &&
1853 g_variant_lookup (agent->registration_options, "fallback", "b", &fallback) &&
1854 fallback)
1855 {
1856 agent_fallback = agent;
1857 agent = NULL;
1858 }
1859 else
1860 {
1861 /* Nope, use it */
1862 goto out;
1863 }
1864 }
1865
1866 /* Now, we should also cover the case where @subject is a
1867 * UnixProcess but the agent is a SystemBusName. However, this can't
1868 * happen because we only allow registering agents for UnixProcess
1869 * and UnixSession subjects!
1870 */
1871
1872 session_for_subject = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
1873 subject,
1874 NULL);
1875 if (session_for_subject == NULL)
1876 goto out;
1877
1878 agent = g_hash_table_lookup (priv->hash_scope_to_authentication_agent, session_for_subject);
1879
1880 /* use fallback, if available */
1881 if (agent == NULL && agent_fallback != NULL)
1882 agent = agent_fallback;
1883
1884 out:
1885 if (session_for_subject != NULL)
1886 g_object_unref (session_for_subject);
1887
1888 return agent;
1889 }
1890
1891 static AuthenticationSession *
1892 get_authentication_session_for_uid_and_cookie (PolkitBackendInteractiveAuthority *authority,
1893 uid_t uid,
1894 const gchar *cookie)
1895 {
1896 PolkitBackendInteractiveAuthorityPrivate *priv;
1897 GHashTableIter hash_iter;
1898 AuthenticationAgent *agent;
1899 AuthenticationSession *result;
1900
1901 result = NULL;
1902
1903 /* TODO: perhaps use a hash on the cookie to speed this up */
1904
1905 priv = polkit_backend_interactive_authority_get_instance_private (authority);
1906
1907 g_hash_table_iter_init (&hash_iter, priv->hash_scope_to_authentication_agent);
1908 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &agent))
1909 {
1910 GList *l;
1911
1912 /* We need to ensure that if somehow we have duplicate cookies
1913 * due to wrapping, that the cookie used is matched to the user
1914 * who called AuthenticationAgentResponse2. See
1915 * http://lists.freedesktop.org/archives/polkit-devel/2015-June/000425.html
1916 *
1917 * Except if the legacy AuthenticationAgentResponse is invoked,
1918 * we don't know the uid and hence use -1. Continue to support
1919 * the old behavior for backwards compatibility, although everyone
1920 * who is using our own setuid helper will automatically be updated
1921 * to the new API.
1922 */
1923 if (uid != (uid_t)-1)
1924 {
1925 if (agent->creator_uid != uid)
1926 continue;
1927 }
1928
1929 for (l = agent->active_sessions; l != NULL; l = l->next)
1930 {
1931 AuthenticationSession *session = l->data;
1932
1933 if (strcmp (session->cookie, cookie) == 0)
1934 {
1935 result = session;
1936 goto out;
1937 }
1938 }
1939 }
1940
1941 out:
1942 return result;
1943 }
1944
1945 static GList *
1946 get_authentication_sessions_initiated_by_system_bus_unique_name (PolkitBackendInteractiveAuthority *authority,
1947 const gchar *system_bus_unique_name)
1948 {
1949 PolkitBackendInteractiveAuthorityPrivate *priv;
1950 GHashTableIter hash_iter;
1951 AuthenticationAgent *agent;
1952 GList *result;
1953
1954 result = NULL;
1955
1956 /* TODO: perhaps use a hash on the cookie to speed this up */
1957
1958 priv = polkit_backend_interactive_authority_get_instance_private (authority);
1959
1960 g_hash_table_iter_init (&hash_iter, priv->hash_scope_to_authentication_agent);
1961 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &agent))
1962 {
1963 GList *l;
1964
1965 for (l = agent->active_sessions; l != NULL; l = l->next)
1966 {
1967 AuthenticationSession *session = l->data;
1968
1969 if (strcmp (session->initiated_by_system_bus_unique_name, system_bus_unique_name) == 0)
1970 {
1971 result = g_list_prepend (result, session);
1972 }
1973 }
1974 }
1975
1976 return result;
1977 }
1978
1979 static GList *
1980 get_authentication_sessions_for_system_bus_unique_name_subject (PolkitBackendInteractiveAuthority *authority,
1981 const gchar *system_bus_unique_name)
1982 {
1983 PolkitBackendInteractiveAuthorityPrivate *priv;
1984 GHashTableIter hash_iter;
1985 AuthenticationAgent *agent;
1986 GList *result;
1987
1988 result = NULL;
1989
1990 /* TODO: perhaps use a hash on the cookie to speed this up */
1991
1992 priv = polkit_backend_interactive_authority_get_instance_private (authority);
1993
1994 g_hash_table_iter_init (&hash_iter, priv->hash_scope_to_authentication_agent);
1995 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &agent))
1996 {
1997 GList *l;
1998
1999 for (l = agent->active_sessions; l != NULL; l = l->next)
2000 {
2001 AuthenticationSession *session = l->data;
2002
2003 if (POLKIT_IS_SYSTEM_BUS_NAME (session->subject) &&
2004 strcmp (polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (session->subject)),
2005 system_bus_unique_name) == 0)
2006 {
2007 result = g_list_prepend (result, session);
2008 }
2009 }
2010 }
2011
2012 return result;
2013 }
2014
2015
2016 static AuthenticationAgent *
2017 get_authentication_agent_by_unique_system_bus_name (PolkitBackendInteractiveAuthority *authority,
2018 const gchar *unique_system_bus_name)
2019 {
2020 PolkitBackendInteractiveAuthorityPrivate *priv;
2021 GHashTableIter hash_iter;
2022 AuthenticationAgent *agent;
2023
2024 priv = polkit_backend_interactive_authority_get_instance_private (authority);
2025
2026 g_hash_table_iter_init (&hash_iter, priv->hash_scope_to_authentication_agent);
2027 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &agent))
2028 {
2029 if (strcmp (agent->unique_system_bus_name, unique_system_bus_name) == 0)
2030 goto out;
2031 }
2032
2033 agent = NULL;
2034
2035 out:
2036 return agent;
2037 }
2038
2039 static void
2040 authentication_agent_begin_cb (GDBusProxy *proxy,
2041 GAsyncResult *res,
2042 gpointer user_data)
2043 {
2044 AuthenticationSession *session = user_data;
2045 gboolean gained_authorization;
2046 gboolean was_dismissed;
2047 GVariant *result;
2048 GError *error;
2049
2050 was_dismissed = FALSE;
2051 gained_authorization = FALSE;
2052
2053 error = NULL;
2054 result = g_dbus_proxy_call_finish (proxy, res, &error);
2055 if (result == NULL)
2056 {
2057 g_printerr ("Error performing authentication: %s (%s %d)\n",
2058 error->message,
2059 g_quark_to_string (error->domain),
2060 error->code);
2061 if (error->domain == POLKIT_ERROR && error->code == POLKIT_ERROR_CANCELLED)
2062 was_dismissed = TRUE;
2063 g_error_free (error);
2064 }
2065 else
2066 {
2067 g_variant_unref (result);
2068 gained_authorization = session->is_authenticated;
2069 g_debug ("Authentication complete, is_authenticated = %d", session->is_authenticated);
2070 }
2071
2072 session->agent->active_sessions = g_list_remove (session->agent->active_sessions, session);
2073
2074 session->callback (session->agent,
2075 session->subject,
2076 session->user_of_subject,
2077 session->caller,
2078 session->authority,
2079 session->action_id,
2080 session->details,
2081 session->implicit_authorization,
2082 gained_authorization,
2083 was_dismissed,
2084 session->authenticated_identity,
2085 session->user_data);
2086
2087 authentication_session_free (session);
2088 }
2089
2090 static void
2091 append_property (GString *dest,
2092 PolkitDetails *details,
2093 const gchar *key,
2094 PolkitBackendInteractiveAuthority *authority,
2095 const gchar *message,
2096 const gchar *action_id)
2097 {
2098 const gchar *value;
2099
2100 value = polkit_details_lookup (details, key);
2101 if (value != NULL)
2102 {
2103 g_string_append (dest, value);
2104 }
2105 else
2106 {
2107 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
2108 LOG_LEVEL_ERROR,
2109 "Error substituting value for property $(%s) when preparing message `%s' for action-id %s",
2110 key,
2111 message,
2112 action_id);
2113 g_string_append (dest, "$(");
2114 g_string_append (dest, key);
2115 g_string_append (dest, ")");
2116 }
2117 }
2118
2119 static gchar *
2120 expand_properties (const gchar *message,
2121 PolkitDetails *details,
2122 PolkitBackendInteractiveAuthority *authority,
2123 const gchar *action_id)
2124 {
2125 GString *ret;
2126 GString *var;
2127 guint n;
2128 gboolean in_resolve;
2129
2130 ret = g_string_new (NULL);
2131 var = g_string_new (NULL);
2132
2133 in_resolve = FALSE;
2134 for (n = 0; message[n] != '\0'; n++)
2135 {
2136 gint c = message[n];
2137 if (c == '$' && message[n+1] == '(')
2138 {
2139 in_resolve = TRUE;
2140 n += 1;
2141 }
2142 else
2143 {
2144 if (in_resolve)
2145 {
2146 if (c == ')')
2147 {
2148 append_property (ret, details, var->str, authority, message, action_id);
2149 g_string_set_size (var, 0);
2150 in_resolve = FALSE;
2151 }
2152 else
2153 {
2154 g_string_append_c (var, c);
2155 }
2156 }
2157 else
2158 {
2159 g_string_append_c (ret, c);
2160 }
2161 }
2162 }
2163 g_string_free (var, TRUE);
2164
2165 return g_string_free (ret, FALSE);
2166 }
2167
2168 static void
2169 get_localized_data_for_challenge (PolkitBackendInteractiveAuthority *authority,
2170 PolkitSubject *caller,
2171 PolkitSubject *subject,
2172 PolkitIdentity *user_of_subject,
2173 const gchar *action_id,
2174 PolkitDetails *details,
2175 const gchar *locale,
2176 gchar **out_localized_message,
2177 gchar **out_localized_icon_name,
2178 PolkitDetails **out_localized_details)
2179 {
2180 PolkitBackendInteractiveAuthorityPrivate *priv;
2181 PolkitActionDescription *action_desc;
2182 gchar *message;
2183 gchar *icon_name;
2184 PolkitDetails *localized_details;
2185 const gchar *message_to_use;
2186 const gchar *gettext_domain;
2187 gchar *s;
2188
2189 priv = polkit_backend_interactive_authority_get_instance_private (authority);
2190
2191 message = NULL;
2192 icon_name = NULL;
2193 localized_details = NULL;
2194 action_desc = NULL;
2195
2196 *out_localized_message = NULL;
2197 *out_localized_icon_name = NULL;
2198 *out_localized_details = NULL;
2199
2200 action_desc = polkit_backend_action_pool_get_action (priv->action_pool,
2201 action_id,
2202 locale);
2203 if (action_desc == NULL)
2204 goto out;
2205
2206 /* Set LANG and locale so g_dgettext() + friends work below */
2207 if (setlocale (LC_ALL, locale) == NULL)
2208 {
2209 g_printerr ("Invalid locale '%s'\n", locale);
2210 }
2211 /* if LANGUAGE have been set in /etc/default, set LANG is invalid. */
2212 g_setenv ("LANGUAGE", locale, TRUE);
2213
2214 gettext_domain = polkit_details_lookup (details, "polkit.gettext_domain");
2215 message_to_use = polkit_details_lookup (details, "polkit.message");
2216 if (message_to_use != NULL)
2217 {
2218 message = g_strdup (g_dgettext (gettext_domain, message_to_use));
2219 /* g_print ("locale=%s, domain=%s, msg=`%s' -> `%s'\n", locale, gettext_domain, message_to_use, message); */
2220 }
2221 icon_name = g_strdup (polkit_details_lookup (details, "polkit.icon_name"));
2222
2223 /* fall back to action description */
2224 if (message == NULL)
2225 {
2226 message = g_strdup (polkit_action_description_get_message (action_desc));
2227 }
2228 if (icon_name == NULL)
2229 {
2230 icon_name = g_strdup (polkit_action_description_get_icon_name (action_desc));
2231 }
2232
2233 /* replace $(property) with values */
2234 if (message != NULL)
2235 {
2236 s = message;
2237 message = expand_properties (message, details, authority, action_id);
2238 g_free (s);
2239 }
2240
2241 /* Back to C! */
2242 setlocale (LC_ALL, "C");
2243 g_setenv ("LANGUAGE", "C", TRUE);
2244
2245 out:
2246 if (message == NULL)
2247 message = g_strdup ("");
2248 if (icon_name == NULL)
2249 icon_name = g_strdup ("");
2250 *out_localized_message = message;
2251 *out_localized_icon_name = icon_name;
2252 *out_localized_details = localized_details;
2253 if (action_desc != NULL)
2254 g_object_unref (action_desc);
2255 }
2256
2257 static void
2258 add_pid (PolkitDetails *details,
2259 PolkitSubject *subject,
2260 const gchar *key)
2261 {
2262 gchar buf[32];
2263 gint pid;
2264
2265 if (POLKIT_IS_UNIX_PROCESS (subject))
2266 {
2267 pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
2268 }
2269 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
2270 {
2271 PolkitSubject *process;
2272 GError *error;
2273
2274 error = NULL;
2275 process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject),
2276 NULL,
2277 &error);
2278 if (process == NULL)
2279 {
2280 g_printerr ("Error getting process for system bus name `%s': %s\n",
2281 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject)),
2282 error->message);
2283 g_error_free (error);
2284 goto out;
2285 }
2286 pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process));
2287 g_object_unref (process);
2288 }
2289 else if (POLKIT_IS_UNIX_SESSION (subject))
2290 {
2291 goto out;
2292 }
2293 else
2294 {
2295 gchar *s;
2296 s = polkit_subject_to_string (subject);
2297 g_printerr ("Don't know how to get pid from subject of type %s: %s\n",
2298 g_type_name (G_TYPE_FROM_INSTANCE (subject)),
2299 s);
2300 g_free (s);
2301 goto out;
2302 }
2303
2304 g_snprintf (buf, sizeof (buf), "%d", pid);
2305 polkit_details_insert (details, key, buf);
2306
2307 out:
2308 ;
2309 }
2310
2311 /* ---------------------------------------------------------------------------------------------------- */
2312
2313 /* ---------------------------------------------------------------------------------------------------- */
2314
2315 static GList *
2316 get_users_in_group (PolkitIdentity *group,
2317 PolkitIdentity *user_of_subject,
2318 gboolean include_root)
2319 {
2320 gid_t gid;
2321 uid_t uid_of_subject;
2322 struct group *grp;
2323 GList *ret;
2324 guint n;
2325
2326 ret = NULL;
2327
2328 gid = polkit_unix_group_get_gid (POLKIT_UNIX_GROUP (group));
2329
2330 /* Check if group is subject's primary group. */
2331 uid_of_subject = polkit_unix_user_get_uid (POLKIT_UNIX_USER (user_of_subject));
2332 if (uid_of_subject != 0 || include_root)
2333 {
2334 struct passwd *pwd;
2335
2336 pwd = getpwuid (uid_of_subject);
2337 if (pwd != NULL && pwd->pw_gid == gid)
2338 ret = g_list_prepend (ret, g_object_ref (user_of_subject));
2339 }
2340
2341 /* Add supplemental group members. */
2342 grp = getgrgid (gid);
2343 if (grp == NULL)
2344 {
2345 g_warning ("Error looking up group with gid %d: %s", gid, g_strerror (errno));
2346 goto out;
2347 }
2348
2349 for (n = 0; grp->gr_mem != NULL && grp->gr_mem[n] != NULL; n++)
2350 {
2351 PolkitIdentity *user;
2352 GError *error;
2353
2354 if (!include_root && g_strcmp0 (grp->gr_mem[n], "root") == 0)
2355 continue;
2356
2357 error = NULL;
2358 user = polkit_unix_user_new_for_name (grp->gr_mem[n], &error);
2359 if (user == NULL)
2360 {
2361 g_warning ("Unknown username '%s' in group: %s", grp->gr_mem[n], error->message);
2362 g_error_free (error);
2363 }
2364 else
2365 {
2366 ret = g_list_prepend (ret, user);
2367 }
2368 }
2369
2370 ret = g_list_reverse (ret);
2371
2372 out:
2373 return ret;
2374 }
2375
2376 static GList *
2377 get_users_in_net_group (PolkitIdentity *group,
2378 gboolean include_root)
2379 {
2380 GList *ret = NULL;
2381 #ifdef HAVE_SETNETGRENT
2382 const gchar *name;
2383
2384 name = polkit_unix_netgroup_get_name (POLKIT_UNIX_NETGROUP (group));
2385
2386 # ifdef HAVE_SETNETGRENT_RETURN
2387 if (setnetgrent (name) == 0)
2388 {
2389 g_warning ("Error looking up net group with name %s: %s", name, g_strerror (errno));
2390 goto out;
2391 }
2392 # else
2393 setnetgrent (name);
2394 # endif /* HAVE_SETNETGRENT_RETURN */
2395
2396 for (;;)
2397 {
2398 # if defined(HAVE_NETBSD) || defined(HAVE_OPENBSD)
2399 const char *hostname, *username, *domainname;
2400 # else
2401 char *hostname, *username, *domainname;
2402 # endif /* defined(HAVE_NETBSD) || defined(HAVE_OPENBSD) */
2403 PolkitIdentity *user;
2404 GError *error = NULL;
2405
2406 if (getnetgrent (&hostname, &username, &domainname) == 0)
2407 break;
2408
2409 /* Skip NULL entries since we never want to make everyone an admin
2410 * Skip "-" entries which mean "no match ever" in netgroup land */
2411 if (username == NULL || g_strcmp0 (username, "-") == 0)
2412 continue;
2413
2414 /* TODO: Should we match on hostname? Maybe only allow "-" as a hostname
2415 * for safety. */
2416
2417 user = polkit_unix_user_new_for_name (username, &error);
2418 if (user == NULL)
2419 {
2420 g_warning ("Unknown username '%s' in unix-netgroup: %s", username, error->message);
2421 g_error_free (error);
2422 }
2423 else
2424 {
2425 ret = g_list_prepend (ret, user);
2426 }
2427 }
2428
2429 ret = g_list_reverse (ret);
2430
2431 out:
2432 endnetgrent ();
2433 #endif /* HAVE_SETNETGRENT */
2434 return ret;
2435 }
2436
2437 /* ---------------------------------------------------------------------------------------------------- */
2438
2439 static void
2440 authentication_agent_initiate_challenge (AuthenticationAgent *agent,
2441 PolkitSubject *subject,
2442 PolkitIdentity *user_of_subject,
2443 PolkitBackendInteractiveAuthority *authority,
2444 const gchar *action_id,
2445 PolkitDetails *details,
2446 PolkitSubject *caller,
2447 PolkitImplicitAuthorization implicit_authorization,
2448 GCancellable *cancellable,
2449 AuthenticationAgentCallback callback,
2450 gpointer user_data)
2451 {
2452 PolkitBackendInteractiveAuthorityPrivate *priv = polkit_backend_interactive_authority_get_instance_private (authority);
2453 AuthenticationSession *session;
2454 GList *l;
2455 GList *identities;
2456 gchar *localized_message;
2457 gchar *localized_icon_name;
2458 PolkitDetails *localized_details;
2459 GList *user_identities = NULL;
2460 GVariantBuilder identities_builder;
2461 GVariant *parameters;
2462
2463 get_localized_data_for_challenge (authority,
2464 caller,
2465 subject,
2466 user_of_subject,
2467 action_id,
2468 details,
2469 agent->locale,
2470 &localized_message,
2471 &localized_icon_name,
2472 &localized_details);
2473
2474 identities = NULL;
2475
2476 /* select admin user if required by the implicit authorization */
2477 if (implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_ADMINISTRATOR_AUTHENTICATION_REQUIRED ||
2478 implicit_authorization == POLKIT_IMPLICIT_AUTHORIZATION_ADMINISTRATOR_AUTHENTICATION_REQUIRED_RETAINED)
2479 {
2480 gboolean is_local = FALSE;
2481 gboolean is_active = FALSE;
2482 PolkitSubject *session_for_subject = NULL;
2483
2484 session_for_subject = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
2485 subject,
2486 NULL);
2487 if (session_for_subject != NULL)
2488 {
2489 is_local = polkit_backend_session_monitor_is_session_local (priv->session_monitor, session_for_subject);
2490 is_active = polkit_backend_session_monitor_is_session_active (priv->session_monitor, session_for_subject);
2491 }
2492
2493 identities = polkit_backend_interactive_authority_get_admin_identities (authority,
2494 caller,
2495 subject,
2496 user_of_subject,
2497 is_local,
2498 is_active,
2499 action_id,
2500 details);
2501 g_clear_object (&session_for_subject);
2502 }
2503 else
2504 {
2505 identities = g_list_prepend (identities, g_object_ref (user_of_subject));
2506 }
2507
2508 /* expand groups/netgroups to users */
2509 user_identities = NULL;
2510 for (l = identities; l != NULL; l = l->next)
2511 {
2512 PolkitIdentity *identity = POLKIT_IDENTITY (l->data);
2513 if (POLKIT_IS_UNIX_USER (identity))
2514 {
2515 user_identities = g_list_append (user_identities, g_object_ref (identity));
2516 }
2517 else if (POLKIT_IS_UNIX_GROUP (identity))
2518 {
2519 user_identities = g_list_concat (user_identities, get_users_in_group (identity, user_of_subject, FALSE));
2520 }
2521 else if (POLKIT_IS_UNIX_NETGROUP (identity))
2522 {
2523 user_identities = g_list_concat (user_identities, get_users_in_net_group (identity, FALSE));
2524 }
2525 else
2526 {
2527 g_warning ("Unsupported identity");
2528 }
2529 }
2530
2531 /* Fall back to uid 0 if no users are available (rhbz #834494) */
2532 if (user_identities == NULL)
2533 user_identities = g_list_prepend (NULL, polkit_unix_user_new (0));
2534
2535 session = authentication_session_new (agent,
2536 subject,
2537 user_of_subject,
2538 caller,
2539 authority,
2540 user_identities,
2541 action_id,
2542 details,
2543 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (caller)),
2544 implicit_authorization,
2545 cancellable,
2546 callback,
2547 user_data);
2548
2549 agent->active_sessions = g_list_prepend (agent->active_sessions, session);
2550
2551 if (localized_details == NULL)
2552 localized_details = polkit_details_new ();
2553 add_pid (localized_details, caller, "polkit.caller-pid");
2554 add_pid (localized_details, subject, "polkit.subject-pid");
2555
2556 g_variant_builder_init (&identities_builder, G_VARIANT_TYPE ("a(sa{sv})"));
2557 for (l = user_identities; l != NULL; l = l->next)
2558 {
2559 PolkitIdentity *identity = POLKIT_IDENTITY (l->data);
2560 g_variant_builder_add_value (&identities_builder,
2561 polkit_identity_to_gvariant (identity)); /* A floating value */
2562 }
2563
2564 parameters = g_variant_new ("(sss@a{ss}sa(sa{sv}))",
2565 action_id,
2566 localized_message,
2567 localized_icon_name,
2568 polkit_details_to_gvariant (localized_details), /* A floating value */
2569 session->cookie,
2570 &identities_builder);
2571
2572 g_dbus_proxy_call (agent->proxy,
2573 "BeginAuthentication",
2574 parameters, /* consumes the floating GVariant */
2575 G_DBUS_CALL_FLAGS_NONE,
2576 G_MAXINT, /* timeout_msec - no timeout */
2577 session->cancellable,
2578 (GAsyncReadyCallback) authentication_agent_begin_cb,
2579 session);
2580
2581 g_list_free_full (user_identities, g_object_unref);
2582 g_list_foreach (identities, (GFunc) g_object_unref, NULL);
2583 g_list_free (identities);
2584
2585 g_free (localized_message);
2586 g_free (localized_icon_name);
2587 if (localized_details != NULL)
2588 g_object_unref (localized_details);
2589 }
2590
2591 static void
2592 authentication_agent_cancel_cb (GDBusProxy *proxy,
2593 GAsyncResult *res,
2594 gpointer user_data)
2595 {
2596 GVariant *result;
2597 GError *error;
2598
2599 error = NULL;
2600 result = g_dbus_proxy_call_finish (proxy, res, &error);
2601 if (result == NULL)
2602 {
2603 g_printerr ("Error cancelling authentication: %s\n", error->message);
2604 g_error_free (error);
2605 }
2606 else
2607 g_variant_unref (result);
2608 }
2609
2610 static void
2611 authentication_session_cancel (AuthenticationSession *session)
2612 {
2613 g_dbus_proxy_call (session->agent->proxy,
2614 "CancelAuthentication",
2615 g_variant_new ("(s)", session->cookie),
2616 G_DBUS_CALL_FLAGS_NONE,
2617 -1, /* timeout_msec */
2618 NULL, /* GCancellable* */
2619 (GAsyncReadyCallback) authentication_agent_cancel_cb,
2620 NULL);
2621 }
2622
2623 /* ---------------------------------------------------------------------------------------------------- */
2624
2625 static gboolean
2626 polkit_backend_interactive_authority_register_authentication_agent (PolkitBackendAuthority *authority,
2627 PolkitSubject *caller,
2628 PolkitSubject *subject,
2629 const gchar *locale,
2630 const gchar *object_path,
2631 GVariant *options,
2632 GError **error)
2633 {
2634 PolkitBackendInteractiveAuthority *interactive_authority;
2635 PolkitBackendInteractiveAuthorityPrivate *priv;
2636 PolkitSubject *session_for_caller;
2637 PolkitIdentity *user_of_caller;
2638 PolkitIdentity *user_of_subject;
2639 gboolean user_of_subject_matches;
2640 AuthenticationAgent *agent;
2641 gboolean ret;
2642 gchar *caller_cmdline;
2643 gchar *subject_as_string;
2644
2645 ret = FALSE;
2646
2647 session_for_caller = NULL;
2648 user_of_caller = NULL;
2649 user_of_subject = NULL;
2650 subject_as_string = NULL;
2651 caller_cmdline = NULL;
2652 agent = NULL;
2653
2654 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
2655 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
2656
2657 if (POLKIT_IS_UNIX_SESSION (subject))
2658 {
2659 session_for_caller = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
2660 caller,
2661 NULL);
2662 if (session_for_caller == NULL)
2663 {
2664 g_set_error (error,
2665 POLKIT_ERROR,
2666 POLKIT_ERROR_FAILED,
2667 "Cannot determine session the caller is in");
2668 goto out;
2669 }
2670 if (!polkit_subject_equal (session_for_caller, subject))
2671 {
2672 g_set_error (error,
2673 POLKIT_ERROR,
2674 POLKIT_ERROR_FAILED,
2675 "Passed session and the session the caller is in differs. They must be equal for now.");
2676 goto out;
2677 }
2678 }
2679 else if (POLKIT_IS_UNIX_PROCESS (subject))
2680 {
2681 /* explicitly OK */
2682 }
2683 else
2684 {
2685 g_set_error (error,
2686 POLKIT_ERROR,
2687 POLKIT_ERROR_FAILED,
2688 "Only unix-process and unix-session subjects can be used for authentication agents.");
2689 goto out;
2690 }
2691
2692 user_of_caller = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor, caller, NULL, NULL);
2693 if (user_of_caller == NULL)
2694 {
2695 g_set_error (error,
2696 POLKIT_ERROR,
2697 POLKIT_ERROR_FAILED,
2698 "Cannot determine user of caller");
2699 goto out;
2700 }
2701 user_of_subject = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor, subject, &user_of_subject_matches, NULL);
2702 if (user_of_subject == NULL)
2703 {
2704 g_set_error (error,
2705 POLKIT_ERROR,
2706 POLKIT_ERROR_FAILED,
2707 "Cannot determine user of subject");
2708 goto out;
2709 }
2710 if (!user_of_subject_matches
2711 || !polkit_identity_equal (user_of_caller, user_of_subject))
2712 {
2713 if (identity_is_root_user (user_of_caller))
2714 {
2715 /* explicitly allow uid 0 to register for other users */
2716 }
2717 else
2718 {
2719 g_set_error (error,
2720 POLKIT_ERROR,
2721 POLKIT_ERROR_FAILED,
2722 "User of caller and user of subject differs.");
2723 goto out;
2724 }
2725 }
2726
2727 agent = g_hash_table_lookup (priv->hash_scope_to_authentication_agent, subject);
2728 if (agent != NULL)
2729 {
2730 g_set_error (error,
2731 POLKIT_ERROR,
2732 POLKIT_ERROR_FAILED,
2733 "An authentication agent already exists for the given subject");
2734 goto out;
2735 }
2736
2737 priv->agent_serial++;
2738 agent = authentication_agent_new (priv->agent_serial,
2739 subject,
2740 user_of_subject,/*fix pkexec fails with No session for cookie,
2741 upstream issue:https://gitlab.freedesktop.org/polkit/polkit/issues/17*/
2742 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (caller)),
2743 locale,
2744 object_path,
2745 options,
2746 error);
2747 if (!agent)
2748 goto out;
2749
2750 g_hash_table_insert (priv->hash_scope_to_authentication_agent,
2751 g_object_ref (subject),
2752 agent);
2753
2754 caller_cmdline = _polkit_subject_get_cmdline (caller);
2755 if (caller_cmdline == NULL)
2756 caller_cmdline = g_strdup ("<unknown>");
2757
2758 subject_as_string = polkit_subject_to_string (subject);
2759
2760 g_debug ("Added authentication agent for %s at name %s [%s], object path %s, locale %s",
2761 subject_as_string,
2762 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (caller)),
2763 caller_cmdline,
2764 object_path,
2765 locale);
2766
2767 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
2768 LOG_LEVEL_INFO,
2769 "Registered Authentication Agent for %s "
2770 "(system bus name %s [%s], object path %s, locale %s)",
2771 subject_as_string,
2772 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (caller)),
2773 caller_cmdline,
2774 object_path,
2775 locale);
2776
2777 g_signal_emit_by_name (authority, "changed");
2778
2779 ret = TRUE;
2780
2781 out:
2782 g_free (caller_cmdline);
2783 g_free (subject_as_string);
2784 if (user_of_caller != NULL)
2785 g_object_unref (user_of_caller);
2786 if (user_of_subject != NULL)
2787 g_object_unref (user_of_subject);
2788 if (session_for_caller != NULL)
2789 g_object_unref (session_for_caller);
2790
2791 return ret;
2792 }
2793
2794 static gboolean
2795 polkit_backend_interactive_authority_unregister_authentication_agent (PolkitBackendAuthority *authority,
2796 PolkitSubject *caller,
2797 PolkitSubject *subject,
2798 const gchar *object_path,
2799 GError **error)
2800 {
2801 PolkitBackendInteractiveAuthority *interactive_authority;
2802 PolkitBackendInteractiveAuthorityPrivate *priv;
2803 PolkitSubject *session_for_caller;
2804 PolkitIdentity *user_of_caller;
2805 PolkitIdentity *user_of_subject;
2806 gboolean user_of_subject_matches;
2807 AuthenticationAgent *agent;
2808 gboolean ret;
2809 gchar *scope_str;
2810
2811 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
2812 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
2813
2814 ret = FALSE;
2815 session_for_caller = NULL;
2816 user_of_caller = NULL;
2817 user_of_subject = NULL;
2818
2819 if (POLKIT_IS_UNIX_SESSION (subject))
2820 {
2821 session_for_caller = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
2822 caller,
2823 NULL);
2824 if (session_for_caller == NULL)
2825 {
2826 g_set_error (error,
2827 POLKIT_ERROR,
2828 POLKIT_ERROR_FAILED,
2829 "Cannot determine session the caller is in");
2830 goto out;
2831 }
2832
2833 if (!polkit_subject_equal (session_for_caller, subject))
2834 {
2835 g_set_error (error,
2836 POLKIT_ERROR,
2837 POLKIT_ERROR_FAILED,
2838 "Passed session and the session the caller is in differs. They must be equal for now.");
2839 goto out;
2840 }
2841 }
2842 else if (POLKIT_IS_UNIX_PROCESS (subject))
2843 {
2844 /* explicitly OK */
2845 }
2846 else
2847 {
2848 g_set_error (error,
2849 POLKIT_ERROR,
2850 POLKIT_ERROR_FAILED,
2851 "Only unix-process and unix-session subjects can be used for authentication agents.");
2852 goto out;
2853 }
2854
2855 user_of_caller = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor, caller, NULL, NULL);
2856 if (user_of_caller == NULL)
2857 {
2858 g_set_error (error,
2859 POLKIT_ERROR,
2860 POLKIT_ERROR_FAILED,
2861 "Cannot determine user of caller");
2862 goto out;
2863 }
2864 user_of_subject = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor, subject, &user_of_subject_matches, NULL);
2865 if (user_of_subject == NULL)
2866 {
2867 g_set_error (error,
2868 POLKIT_ERROR,
2869 POLKIT_ERROR_FAILED,
2870 "Cannot determine user of subject");
2871 goto out;
2872 }
2873 if (!user_of_subject_matches
2874 || !polkit_identity_equal (user_of_caller, user_of_subject))
2875 {
2876 if (identity_is_root_user (user_of_caller))
2877 {
2878 /* explicitly allow uid 0 to register for other users */
2879 }
2880 else
2881 {
2882 g_set_error (error,
2883 POLKIT_ERROR,
2884 POLKIT_ERROR_FAILED,
2885 "User of caller and user of subject differs.");
2886 goto out;
2887 }
2888 }
2889
2890 agent = g_hash_table_lookup (priv->hash_scope_to_authentication_agent, subject);
2891 if (agent == NULL)
2892 {
2893 g_set_error (error,
2894 POLKIT_ERROR,
2895 POLKIT_ERROR_FAILED,
2896 "No such agent registered");
2897 goto out;
2898 }
2899
2900 if (g_strcmp0 (agent->unique_system_bus_name, polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (caller))) != 0)
2901 {
2902 g_set_error (error,
2903 POLKIT_ERROR,
2904 POLKIT_ERROR_FAILED,
2905 "System bus names do not match");
2906 goto out;
2907 }
2908
2909 if (g_strcmp0 (agent->object_path, object_path) != 0)
2910 {
2911 g_set_error (error,
2912 POLKIT_ERROR,
2913 POLKIT_ERROR_FAILED,
2914 "Object paths do not match");
2915 goto out;
2916 }
2917
2918 scope_str = polkit_subject_to_string (agent->scope);
2919 g_debug ("Removing authentication agent for %s at name %s, object path %s, locale %s",
2920 scope_str,
2921 agent->unique_system_bus_name,
2922 agent->object_path,
2923 agent->locale);
2924
2925 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
2926 LOG_LEVEL_INFO,
2927 "Unregistered Authentication Agent for %s "
2928 "(system bus name %s, object path %s, locale %s)",
2929 scope_str,
2930 agent->unique_system_bus_name,
2931 agent->object_path,
2932 agent->locale);
2933 g_free (scope_str);
2934
2935 authentication_agent_cancel_all_sessions (agent);
2936 /* this works because we have exactly one agent per session */
2937 /* this frees agent... */
2938 g_hash_table_remove (priv->hash_scope_to_authentication_agent, agent->scope);
2939
2940 g_signal_emit_by_name (authority, "changed");
2941
2942 ret = TRUE;
2943
2944 out:
2945 if (user_of_caller != NULL)
2946 g_object_unref (user_of_caller);
2947 if (user_of_subject != NULL)
2948 g_object_unref (user_of_subject);
2949 if (session_for_caller != NULL)
2950 g_object_unref (session_for_caller);
2951 return ret;
2952 }
2953
2954 /* ---------------------------------------------------------------------------------------------------- */
2955
2956 static gboolean
2957 polkit_backend_interactive_authority_authentication_agent_response (PolkitBackendAuthority *authority,
2958 PolkitSubject *caller,
2959 uid_t uid,
2960 const gchar *cookie,
2961 PolkitIdentity *identity,
2962 GError **error)
2963 {
2964 PolkitBackendInteractiveAuthority *interactive_authority;
2965 PolkitBackendInteractiveAuthorityPrivate *priv;
2966 PolkitIdentity *user_of_caller;
2967 gchar *identity_str;
2968 AuthenticationSession *session;
2969 GList *l;
2970 gboolean ret;
2971
2972 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
2973 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
2974
2975 ret = FALSE;
2976 user_of_caller = NULL;
2977
2978 identity_str = polkit_identity_to_string (identity);
2979
2980 g_debug ("In authentication_agent_response for cookie '%s' and identity %s",
2981 cookie,
2982 identity_str);
2983
2984 user_of_caller = polkit_backend_session_monitor_get_user_for_subject (priv->session_monitor,
2985 caller, NULL,
2986 error);
2987 if (user_of_caller == NULL)
2988 goto out;
2989
2990 /* only uid 0 is allowed to invoke this method */
2991 if (!identity_is_root_user (user_of_caller))
2992 {
2993 g_set_error (error,
2994 POLKIT_ERROR,
2995 POLKIT_ERROR_FAILED,
2996 "Only uid 0 may invoke this method. This incident has been logged.");
2997 /* TODO: actually log this */
2998 goto out;
2999 }
3000
3001 /* find the authentication session */
3002 session = get_authentication_session_for_uid_and_cookie (interactive_authority, uid, cookie);
3003 if (session == NULL)
3004 {
3005 g_set_error (error,
3006 POLKIT_ERROR,
3007 POLKIT_ERROR_FAILED,
3008 "No session for cookie");
3009 goto out;
3010 }
3011
3012 /* check that the authentication identity was one of the possibilities we allowed */
3013 for (l = session->identities; l != NULL; l = l->next)
3014 {
3015 PolkitIdentity *i = POLKIT_IDENTITY (l->data);
3016
3017 if (polkit_identity_equal (i, identity))
3018 break;
3019 }
3020
3021 if (l == NULL)
3022 {
3023 g_set_error (error,
3024 POLKIT_ERROR,
3025 POLKIT_ERROR_FAILED,
3026 "The authenticated identity is wrong");
3027 goto out;
3028 }
3029
3030 /* checks out, mark the session as authenticated */
3031 session->is_authenticated = TRUE;
3032 session->authenticated_identity = g_object_ref (identity);
3033
3034 ret = TRUE;
3035
3036 out:
3037 g_free (identity_str);
3038
3039 if (user_of_caller != NULL)
3040 g_object_unref (user_of_caller);
3041
3042 return ret;
3043 }
3044
3045 /* ---------------------------------------------------------------------------------------------------- */
3046
3047 static void
3048 polkit_backend_interactive_authority_system_bus_name_owner_changed (PolkitBackendInteractiveAuthority *authority,
3049 const gchar *name,
3050 const gchar *old_owner,
3051 const gchar *new_owner)
3052 {
3053 PolkitBackendInteractiveAuthority *interactive_authority;
3054 PolkitBackendInteractiveAuthorityPrivate *priv;
3055
3056 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
3057 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
3058
3059 //g_debug ("name-owner-changed: '%s' '%s' '%s'", name, old_owner, new_owner);
3060
3061 if (name[0] == ':' && strlen (new_owner) == 0)
3062 {
3063 AuthenticationAgent *agent;
3064 GList *sessions;
3065 GList *l;
3066
3067 agent = get_authentication_agent_by_unique_system_bus_name (interactive_authority, name);
3068 if (agent != NULL)
3069 {
3070 gchar *scope_str;
3071
3072 scope_str = polkit_subject_to_string (agent->scope);
3073 g_debug ("Removing authentication agent for %s at name %s, object path %s (disconnected from bus)",
3074 scope_str,
3075 agent->unique_system_bus_name,
3076 agent->object_path);
3077
3078 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
3079 LOG_LEVEL_INFO,
3080 "Unregistered Authentication Agent for %s "
3081 "(system bus name %s, object path %s, locale %s) (disconnected from bus)",
3082 scope_str,
3083 agent->unique_system_bus_name,
3084 agent->object_path,
3085 agent->locale);
3086 g_free (scope_str);
3087
3088 authentication_agent_cancel_all_sessions (agent);
3089 /* this works because we have exactly one agent per session */
3090 /* this frees agent... */
3091 g_hash_table_remove (priv->hash_scope_to_authentication_agent, agent->scope);
3092
3093 g_signal_emit_by_name (authority, "changed");
3094 }
3095
3096 /* cancel all authentication sessions initiated by the process owning the vanished name */
3097 sessions = get_authentication_sessions_initiated_by_system_bus_unique_name (interactive_authority, name);
3098 for (l = sessions; l != NULL; l = l->next)
3099 {
3100 AuthenticationSession *session = l->data;
3101
3102 authentication_session_cancel (session);
3103 }
3104 g_list_free (sessions);
3105
3106 /* cancel all authentication sessions that is about the vanished name */
3107 sessions = get_authentication_sessions_for_system_bus_unique_name_subject (interactive_authority, name);
3108 for (l = sessions; l != NULL; l = l->next)
3109 {
3110 AuthenticationSession *session = l->data;
3111
3112 authentication_session_cancel (session);
3113 }
3114 g_list_free (sessions);
3115
3116 /* remove all temporary authorizations that applies to the vanished name
3117 * (temporary_authorization_store_add_authorization for the code path for handling processes)
3118 */
3119 temporary_authorization_store_remove_authorizations_for_system_bus_name (priv->temporary_authorization_store,
3120 name);
3121
3122 }
3123
3124 }
3125
3126 /* ---------------------------------------------------------------------------------------------------- */
3127
3128 typedef struct TemporaryAuthorization TemporaryAuthorization;
3129
3130 struct TemporaryAuthorizationStore
3131 {
3132 GList *authorizations;
3133 PolkitBackendInteractiveAuthority *authority;
3134 guint64 serial;
3135 };
3136
3137 struct TemporaryAuthorization
3138 {
3139 TemporaryAuthorizationStore *store;
3140 PolkitSubject *subject;
3141 PolkitSubject *scope;
3142 gchar *id;
3143 gchar *action_id;
3144 /* both of these are obtained using g_get_monotonic_time(),
3145 * so the resolution is usec
3146 */
3147 gint64 time_granted;
3148 gint64 time_expires;
3149 guint expiration_timeout_id;
3150 guint check_vanished_timeout_id;
3151 };
3152
3153 static void
3154 temporary_authorization_free (TemporaryAuthorization *authorization)
3155 {
3156 g_free (authorization->id);
3157 g_object_unref (authorization->subject);
3158 g_object_unref (authorization->scope);
3159 g_free (authorization->action_id);
3160 if (authorization->expiration_timeout_id > 0)
3161 g_source_remove (authorization->expiration_timeout_id);
3162 if (authorization->check_vanished_timeout_id > 0)
3163 g_source_remove (authorization->check_vanished_timeout_id);
3164 g_free (authorization);
3165 }
3166
3167 static TemporaryAuthorizationStore *
3168 temporary_authorization_store_new (PolkitBackendInteractiveAuthority *authority)
3169 {
3170 TemporaryAuthorizationStore *store;
3171
3172 store = g_new0 (TemporaryAuthorizationStore, 1);
3173 store->authority = authority;
3174 store->authorizations = NULL;
3175
3176 return store;
3177 }
3178
3179 static void
3180 temporary_authorization_store_free (TemporaryAuthorizationStore *store)
3181 {
3182 g_list_foreach (store->authorizations, (GFunc) temporary_authorization_free, NULL);
3183 g_list_free (store->authorizations);
3184 g_free (store);
3185 }
3186
3187 /* XXX: for now, prefer to store the process; see
3188 * https://bugs.freedesktop.org/show_bug.cgi?id=23867
3189 */
3190 static PolkitSubject *
3191 convert_temporary_authorization_subject (PolkitSubject *subject)
3192 {
3193 PolkitSubject *ret;
3194 if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
3195 {
3196 GError *error = NULL;
3197 ret = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject),
3198 NULL,
3199 &error);
3200 if (ret == NULL)
3201 {
3202 g_printerr ("Error getting process for system bus name `%s': %s\n",
3203 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject)),
3204 error->message);
3205 g_error_free (error);
3206 return g_object_ref (subject);
3207 }
3208 else
3209 {
3210 return ret;
3211 }
3212 }
3213 else
3214 {
3215 return g_object_ref (subject);
3216 }
3217 }
3218
3219 #ifdef __linux__
3220 static time_t
3221 parse_boottime (void)
3222 {
3223 static time_t btime = 0;
3224 gchar *contents;
3225 gchar **lines;
3226 guint n;
3227
3228 if (btime != 0)
3229 return btime;
3230
3231 lines = NULL;
3232 contents = NULL;
3233
3234 if (!g_file_get_contents ("/proc/stat", &contents, NULL, NULL))
3235 return -1;
3236
3237 lines = g_strsplit (contents, "\n", -1);
3238 for (n = 0; lines != NULL && lines[n] != NULL; n++)
3239 {
3240 if (!g_str_has_prefix (lines[n], "btime "))
3241 continue;
3242 if (sscanf (lines[n] + 6, "%ld", &btime) != 1)
3243 {
3244 g_strfreev (lines);
3245 g_free (contents);
3246 return -1;
3247 }
3248 break;
3249 }
3250
3251 g_strfreev (lines);
3252 g_free (contents);
3253
3254 return btime;
3255 }
3256 #endif /* __linux__ */
3257
3258 /* See the comment at the top of polkitunixprocess.c */
3259 static gboolean
3260 subject_equal_for_authz (PolkitSubject *a,
3261 PolkitSubject *b)
3262 {
3263 if (!polkit_subject_equal (a, b))
3264 {
3265 #ifdef __linux__
3266 PolkitSubject *parent = NULL;
3267 gchar *ctty_path = NULL;
3268
3269 /*
3270 * If we have two processes, and we can safely track them via PID FDs,
3271 * then we also consider them equal for auth purposes if they share the
3272 * same UID, PPID, Control Group and controlling terminal.
3273 */
3274 if (!POLKIT_IS_UNIX_PROCESS (a) || !POLKIT_IS_UNIX_PROCESS (b))
3275 goto error;
3276
3277 if (!polkit_unix_process_get_pidfd_is_safe (POLKIT_UNIX_PROCESS (a)) ||
3278 !polkit_unix_process_get_pidfd_is_safe (POLKIT_UNIX_PROCESS (b)))
3279 goto error;
3280
3281 int uid_a = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (a));
3282 int uid_b = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (b));
3283 if (uid_a == -1 || uid_b == -1 || uid_a != uid_b)
3284 goto error;
3285
3286 if (polkit_unix_process_get_pidfd (POLKIT_UNIX_PROCESS (a)) < 0 ||
3287 polkit_unix_process_get_pidfd (POLKIT_UNIX_PROCESS (b)) < 0)
3288 goto error;
3289
3290 /* Ensure the parent process is still running and still the same, and not PID 1
3291 * (due to reparenting) */
3292 gint ppid_a = polkit_unix_process_get_ppid (POLKIT_UNIX_PROCESS (a));
3293 gint ppid_b = polkit_unix_process_get_ppid (POLKIT_UNIX_PROCESS (b));
3294 if (ppid_a <= 1 || ppid_a != ppid_b)
3295 goto error;
3296
3297 gint ppid_fd = polkit_unix_process_get_ppidfd (POLKIT_UNIX_PROCESS (a));
3298 if (ppid_fd < 0)
3299 goto error;
3300
3301 parent = polkit_unix_process_new_pidfd (ppid_fd, -1, NULL);
3302 if (!parent)
3303 goto error;
3304
3305 /* Ensure all processes are in the same cgroup */
3306 if (polkit_unix_process_get_cgroupid (POLKIT_UNIX_PROCESS (a)) != polkit_unix_process_get_cgroupid (POLKIT_UNIX_PROCESS (b)) ||
3307 polkit_unix_process_get_cgroupid (POLKIT_UNIX_PROCESS (a)) != polkit_unix_process_get_cgroupid (POLKIT_UNIX_PROCESS (parent)) ||
3308 polkit_unix_process_get_cgroupid (POLKIT_UNIX_PROCESS (a)) == 0)
3309 goto error;
3310
3311 /*
3312 * Ensure the controlling terminal is the same and older than both processes,
3313 * otherwise it might have simply been reopened with the same number.
3314 */
3315
3316 guint ttynr_parent = polkit_unix_process_get_ctty (POLKIT_UNIX_PROCESS (parent));
3317 if (ttynr_parent != polkit_unix_process_get_ctty (POLKIT_UNIX_PROCESS (a)) ||
3318 ttynr_parent != polkit_unix_process_get_ctty (POLKIT_UNIX_PROCESS (b)) ||
3319 ttynr_parent == 0)
3320 goto error;
3321
3322 ctty_path = g_strdup_printf ("/dev/pts/%u", ((ttynr_parent & 0xfff00000) >> 12) | (ttynr_parent & 0xff));
3323
3324 struct statx st;
3325 if (statx (AT_FDCWD, ctty_path, 0, STATX_CTIME, &st) != 0)
3326 goto error;
3327
3328 time_t btime = parse_boottime ();
3329 if (btime < 0)
3330 goto error;
3331
3332 if (st.stx_ctime.tv_sec < btime)
3333 goto error;
3334
3335 /* TTY creation time is a unix timestamp, while process start time is monotonic,
3336 * subtract the boot timestamp to compare them */
3337 st.stx_ctime.tv_sec -= btime;
3338
3339 /* Process start time is in jiffies, so get the jiffies-per-second to
3340 * compare it with the TTY ctime */
3341 int jiffies = sysconf(_SC_CLK_TCK);
3342 if (jiffies <= 0)
3343 goto error;
3344
3345 struct statx_timestamp start_time_a;
3346 struct statx_timestamp start_time_b;
3347
3348 start_time_a.tv_sec = polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (a)) / jiffies;
3349 start_time_a.tv_nsec = (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (a)) % jiffies) * 1000000000 / jiffies;
3350 start_time_b.tv_sec = polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (b)) / jiffies;
3351 start_time_b.tv_nsec = (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (b)) % jiffies) * 1000000000 / jiffies;
3352
3353 if (start_time_a.tv_sec < st.stx_ctime.tv_sec ||
3354 (start_time_a.tv_sec == st.stx_ctime.tv_sec && start_time_a.tv_nsec < st.stx_ctime.tv_nsec) ||
3355 start_time_b.tv_sec < st.stx_ctime.tv_sec ||
3356 (start_time_b.tv_sec == st.stx_ctime.tv_sec && start_time_b.tv_nsec < st.stx_ctime.tv_nsec))
3357 goto error;
3358
3359 /* Check that the parent is still running at the very end, to avoid races */
3360 if (polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (parent)) <= 0)
3361 goto error;
3362
3363 g_free (ctty_path);
3364 g_object_unref (parent);
3365 return TRUE;
3366
3367 error:
3368 g_free (ctty_path);
3369 g_object_unref (parent);
3370 return FALSE;
3371 #else /* __linux__ */
3372 return FALSE;
3373 #endif /* __linux__ */
3374 }
3375
3376 /* Now special case unix processes, as we want to protect against
3377 * pid reuse by including the PID FDs or UIDs as a fallback.
3378 */
3379 if (POLKIT_IS_UNIX_PROCESS (a) && POLKIT_IS_UNIX_PROCESS (b)) {
3380 /* If both objects are tracking via PID FD then we can rely on that,
3381 * as the PID is resolved on-the-fly via the pinned file descriptor,
3382 * and it will be -1 if the process exited in the meanwhile. */
3383 if (polkit_unix_process_get_pidfd ((PolkitUnixProcess*)a) >= 0 &&
3384 polkit_unix_process_get_pidfd ((PolkitUnixProcess*)b) >= 0)
3385 {
3386 int pid_a = polkit_unix_process_get_pid ((PolkitUnixProcess*)a);
3387 int pid_b = polkit_unix_process_get_pid ((PolkitUnixProcess*)b);
3388
3389 return pid_a > 0 && pid_b > 0 && pid_a == pid_b;
3390 }
3391
3392 int uid_a = polkit_unix_process_get_uid ((PolkitUnixProcess*)a);
3393 int uid_b = polkit_unix_process_get_uid ((PolkitUnixProcess*)b);
3394
3395 if (uid_a != -1 && uid_b != -1)
3396 {
3397 if (uid_a == uid_b)
3398 {
3399 return TRUE;
3400 }
3401 else
3402 {
3403 g_printerr ("denying slowfork; pid %d uid %d != %d!\n",
3404 polkit_unix_process_get_pid ((PolkitUnixProcess*)a),
3405 uid_a, uid_b);
3406 return FALSE;
3407 }
3408 }
3409 /* Fall through; one of the uids is unset so we can't reliably compare */
3410 }
3411
3412 return TRUE;
3413 }
3414
3415 static gboolean
3416 temporary_authorization_store_has_authorization (TemporaryAuthorizationStore *store,
3417 PolkitSubject *subject,
3418 const gchar *action_id,
3419 const gchar **out_tmp_authz_id)
3420 {
3421 GList *l;
3422 gboolean ret;
3423 PolkitSubject *subject_to_use;
3424
3425 g_return_val_if_fail (store != NULL, FALSE);
3426 g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), FALSE);
3427 g_return_val_if_fail (action_id != NULL, FALSE);
3428
3429 subject_to_use = convert_temporary_authorization_subject (subject);
3430
3431 ret = FALSE;
3432
3433 for (l = store->authorizations; l != NULL; l = l->next) {
3434 TemporaryAuthorization *authorization = l->data;
3435
3436 if (strcmp (action_id, authorization->action_id) == 0 &&
3437 subject_equal_for_authz (subject_to_use, authorization->subject))
3438 {
3439 ret = TRUE;
3440 if (out_tmp_authz_id != NULL)
3441 *out_tmp_authz_id = authorization->id;
3442 goto out;
3443 }
3444 }
3445
3446 out:
3447 g_object_unref (subject_to_use);
3448 return ret;
3449 }
3450
3451 static gboolean
3452 on_expiration_timeout (gpointer user_data)
3453 {
3454 TemporaryAuthorization *authorization = user_data;
3455 gchar *s;
3456
3457 s = polkit_subject_to_string (authorization->subject);
3458 g_debug ("Removing tempoary authorization with id `%s' for action-id `%s' for subject `%s': "
3459 "authorization has expired",
3460 authorization->id,
3461 authorization->action_id,
3462 s);
3463 g_free (s);
3464
3465 authorization->store->authorizations = g_list_remove (authorization->store->authorizations,
3466 authorization);
3467 authorization->expiration_timeout_id = 0;
3468 g_signal_emit_by_name (authorization->store->authority, "changed");
3469 temporary_authorization_free (authorization);
3470
3471 /* remove source */
3472 return FALSE;
3473 }
3474
3475 static gboolean
3476 on_unix_process_check_vanished_timeout (gpointer user_data)
3477 {
3478 TemporaryAuthorization *authorization = user_data;
3479 GError *error;
3480
3481 /* we know that this is a PolkitUnixProcess so the check is fast (no IPC involved) */
3482 error = NULL;
3483 if (!polkit_subject_exists_sync (authorization->subject,
3484 NULL,
3485 &error))
3486 {
3487 if (error != NULL)
3488 {
3489 g_printerr ("Error checking if process exists: %s\n", error->message);
3490 g_error_free (error);
3491 }
3492 else
3493 {
3494 gchar *s;
3495
3496 s = polkit_subject_to_string (authorization->subject);
3497 g_debug ("Removing tempoary authorization with id `%s' for action-id `%s' for subject `%s': "
3498 "subject has vanished",
3499 authorization->id,
3500 authorization->action_id,
3501 s);
3502 g_free (s);
3503
3504 authorization->store->authorizations = g_list_remove (authorization->store->authorizations,
3505 authorization);
3506 g_signal_emit_by_name (authorization->store->authority, "changed");
3507 temporary_authorization_free (authorization);
3508 }
3509 }
3510
3511 /* keep source around */
3512 return TRUE;
3513 }
3514
3515 static void
3516 temporary_authorization_store_remove_authorizations_for_system_bus_name (TemporaryAuthorizationStore *store,
3517 const gchar *name)
3518 {
3519 guint num_removed;
3520 GList *l, *ll;
3521
3522 num_removed = 0;
3523 for (l = store->authorizations; l != NULL; l = ll)
3524 {
3525 TemporaryAuthorization *ta = l->data;
3526 gchar *s;
3527
3528 ll = l->next;
3529
3530 if (!POLKIT_IS_SYSTEM_BUS_NAME (ta->subject))
3531 continue;
3532
3533 if (g_strcmp0 (name, polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (ta->subject))) != 0)
3534 continue;
3535
3536
3537 s = polkit_subject_to_string (ta->subject);
3538 g_debug ("Removing tempoary authorization with id `%s' for action-id `%s' for subject `%s': "
3539 "subject has vanished",
3540 ta->id,
3541 ta->action_id,
3542 s);
3543 g_free (s);
3544
3545 store->authorizations = g_list_remove (store->authorizations, ta);
3546 temporary_authorization_free (ta);
3547
3548 num_removed++;
3549 }
3550
3551 if (num_removed > 0)
3552 g_signal_emit_by_name (store->authority, "changed");
3553 }
3554
3555 static const gchar *
3556 temporary_authorization_store_add_authorization (TemporaryAuthorizationStore *store,
3557 guint expiration_seconds,
3558 PolkitSubject *subject,
3559 PolkitSubject *scope,
3560 const gchar *action_id)
3561 {
3562 TemporaryAuthorization *authorization;
3563 PolkitSubject *subject_to_use;
3564
3565 g_return_val_if_fail (store != NULL, NULL);
3566 g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), NULL);
3567 g_return_val_if_fail (action_id != NULL, NULL);
3568 g_return_val_if_fail (!temporary_authorization_store_has_authorization (store, subject, action_id, NULL), NULL);
3569
3570 subject_to_use = convert_temporary_authorization_subject (subject);
3571
3572 authorization = g_new0 (TemporaryAuthorization, 1);
3573 authorization->id = g_strdup_printf ("tmpauthz%" G_GUINT64_FORMAT, store->serial++);
3574 authorization->store = store;
3575 authorization->subject = g_object_ref (subject_to_use);
3576 authorization->scope = g_object_ref (scope);
3577 authorization->action_id = g_strdup (action_id);
3578 /* store monotonic time and convert to secs-since-epoch when returning TemporaryAuthorization structs */
3579 authorization->time_granted = g_get_monotonic_time ();
3580 authorization->time_expires = authorization->time_granted + expiration_seconds * G_USEC_PER_SEC;
3581 /* g_timeout_add() is using monotonic time since 2.28 */
3582 authorization->expiration_timeout_id = g_timeout_add (expiration_seconds * 1000,
3583 on_expiration_timeout,
3584 authorization);
3585
3586 if (POLKIT_IS_UNIX_PROCESS (authorization->subject) && !polkit_unix_process_get_pidfd_is_safe (POLKIT_UNIX_PROCESS (authorization->subject)))
3587 {
3588 /* For now, set up a timer to poll every two seconds - this is used to determine
3589 * when the process vanishes. We want to do this so we can remove the temporary
3590 * authorization - this is because we want agents to update e.g. a notification
3591 * area icon saying the user has temporary authorizations (e.g. remove the icon).
3592 *
3593 * Ideally we'd just do
3594 *
3595 * g_signal_connect (kernel, "process-exited", G_CALLBACK (on_process_exited), user_data);
3596 *
3597 * but that is not how things work right now (and, hey, it's not like the kernel
3598 * is a GObject either!) - so we poll.
3599 *
3600 * TODO: On Linux, it might be possible to obtain notifications by connecting
3601 * to the netlink socket. Needs looking into.
3602 */
3603
3604 authorization->check_vanished_timeout_id = g_timeout_add_seconds (2,
3605 on_unix_process_check_vanished_timeout,
3606 authorization);
3607 }
3608 #if 0
3609 else if (POLKIT_IS_SYSTEM_BUS_NAME (authorization->subject))
3610 {
3611 /* This is currently handled in polkit_backend_interactive_authority_system_bus_name_owner_changed() */
3612 }
3613 #endif
3614
3615
3616 store->authorizations = g_list_prepend (store->authorizations, authorization);
3617
3618 g_object_unref (subject_to_use);
3619
3620 return authorization->id;
3621 }
3622
3623 /* ---------------------------------------------------------------------------------------------------- */
3624
3625 static GList *
3626 polkit_backend_interactive_authority_enumerate_temporary_authorizations (PolkitBackendAuthority *authority,
3627 PolkitSubject *caller,
3628 PolkitSubject *subject,
3629 GError **error)
3630 {
3631 PolkitBackendInteractiveAuthority *interactive_authority;
3632 PolkitBackendInteractiveAuthorityPrivate *priv;
3633 PolkitSubject *session_for_caller;
3634 GList *ret;
3635 GList *l;
3636 gint64 monotonic_now;
3637 gint64 real_now;
3638
3639 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
3640 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
3641
3642 ret = NULL;
3643 session_for_caller = NULL;
3644
3645 if (!POLKIT_IS_UNIX_SESSION (subject))
3646 {
3647 g_set_error (error,
3648 POLKIT_ERROR,
3649 POLKIT_ERROR_FAILED,
3650 "Can only handle PolkitUnixSession objects for now.");
3651 goto out;
3652 }
3653
3654 session_for_caller = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
3655 caller,
3656 NULL);
3657 if (session_for_caller == NULL)
3658 {
3659 g_set_error (error,
3660 POLKIT_ERROR,
3661 POLKIT_ERROR_FAILED,
3662 "Cannot determine session the caller is in");
3663 goto out;
3664 }
3665
3666 if (!polkit_subject_equal (session_for_caller, subject))
3667 {
3668 g_set_error (error,
3669 POLKIT_ERROR,
3670 POLKIT_ERROR_FAILED,
3671 "Passed session and the session the caller is in differs. They must be equal for now.");
3672 goto out;
3673 }
3674
3675 monotonic_now = g_get_monotonic_time ();
3676 real_now = g_get_real_time () / G_TIME_SPAN_SECOND;
3677
3678 for (l = priv->temporary_authorization_store->authorizations; l != NULL; l = l->next)
3679 {
3680 TemporaryAuthorization *ta = l->data;
3681 PolkitTemporaryAuthorization *tmp_authz;
3682 guint64 real_granted;
3683 guint64 real_expires;
3684
3685 if (!polkit_subject_equal (ta->scope, subject))
3686 continue;
3687
3688 real_granted = (ta->time_granted - monotonic_now) / G_USEC_PER_SEC + real_now;
3689 real_expires = (ta->time_expires - monotonic_now) / G_USEC_PER_SEC + real_now;
3690
3691 tmp_authz = polkit_temporary_authorization_new (ta->id,
3692 ta->action_id,
3693 ta->subject,
3694 real_granted,
3695 real_expires);
3696
3697 ret = g_list_prepend (ret, tmp_authz);
3698 }
3699
3700 out:
3701 if (session_for_caller != NULL)
3702 g_object_unref (session_for_caller);
3703
3704 return ret;
3705 }
3706
3707 /* ---------------------------------------------------------------------------------------------------- */
3708
3709 static gboolean
3710 polkit_backend_interactive_authority_revoke_temporary_authorizations (PolkitBackendAuthority *authority,
3711 PolkitSubject *caller,
3712 PolkitSubject *subject,
3713 GError **error)
3714 {
3715 PolkitBackendInteractiveAuthority *interactive_authority;
3716 PolkitBackendInteractiveAuthorityPrivate *priv;
3717 PolkitSubject *session_for_caller;
3718 gboolean ret;
3719 GList *l;
3720 GList *ll;
3721 guint num_removed;
3722
3723 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
3724 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
3725
3726 ret = FALSE;
3727 session_for_caller = NULL;
3728
3729 if (!POLKIT_IS_UNIX_SESSION (subject))
3730 {
3731 g_set_error (error,
3732 POLKIT_ERROR,
3733 POLKIT_ERROR_FAILED,
3734 "Can only handle PolkitUnixSession objects for now.");
3735 goto out;
3736 }
3737
3738 session_for_caller = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
3739 caller,
3740 NULL);
3741 if (session_for_caller == NULL)
3742 {
3743 g_set_error (error,
3744 POLKIT_ERROR,
3745 POLKIT_ERROR_FAILED,
3746 "Cannot determine session the caller is in");
3747 goto out;
3748 }
3749
3750 if (!polkit_subject_equal (session_for_caller, subject))
3751 {
3752 g_set_error (error,
3753 POLKIT_ERROR,
3754 POLKIT_ERROR_FAILED,
3755 "Passed session and the session the caller is in differs. They must be equal for now.");
3756 goto out;
3757 }
3758
3759 num_removed = 0;
3760 for (l = priv->temporary_authorization_store->authorizations; l != NULL; l = ll)
3761 {
3762 TemporaryAuthorization *ta = l->data;
3763
3764 ll = l->next;
3765
3766 if (!polkit_subject_equal (ta->scope, subject))
3767 continue;
3768
3769 priv->temporary_authorization_store->authorizations = g_list_remove (priv->temporary_authorization_store->authorizations, ta);
3770 temporary_authorization_free (ta);
3771
3772 num_removed++;
3773 }
3774
3775 if (num_removed > 0)
3776 g_signal_emit_by_name (authority, "changed");
3777
3778 ret = TRUE;
3779
3780 out:
3781 if (session_for_caller != NULL)
3782 g_object_unref (session_for_caller);
3783
3784 return ret;
3785 }
3786
3787 /* ---------------------------------------------------------------------------------------------------- */
3788
3789 static gboolean
3790 polkit_backend_interactive_authority_revoke_temporary_authorization_by_id (PolkitBackendAuthority *authority,
3791 PolkitSubject *caller,
3792 const gchar *id,
3793 GError **error)
3794 {
3795 PolkitBackendInteractiveAuthority *interactive_authority;
3796 PolkitBackendInteractiveAuthorityPrivate *priv;
3797 PolkitSubject *session_for_caller;
3798 gboolean ret;
3799 GList *l;
3800 GList *ll;
3801 guint num_removed;
3802
3803 interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority);
3804 priv = polkit_backend_interactive_authority_get_instance_private (interactive_authority);
3805
3806 ret = FALSE;
3807 session_for_caller = NULL;
3808
3809 session_for_caller = polkit_backend_session_monitor_get_session_for_subject (priv->session_monitor,
3810 caller,
3811 NULL);
3812 if (session_for_caller == NULL)
3813 {
3814 g_set_error (error,
3815 POLKIT_ERROR,
3816 POLKIT_ERROR_FAILED,
3817 "Cannot determine session the caller is in");
3818 goto out;
3819 }
3820
3821 num_removed = 0;
3822 for (l = priv->temporary_authorization_store->authorizations; l != NULL; l = ll)
3823 {
3824 TemporaryAuthorization *ta = l->data;
3825
3826 ll = l->next;
3827
3828 if (strcmp (ta->id, id) != 0)
3829 continue;
3830
3831 if (!polkit_subject_equal (session_for_caller, ta->scope))
3832 {
3833 g_set_error (error,
3834 POLKIT_ERROR,
3835 POLKIT_ERROR_FAILED,
3836 "Cannot remove a temporary authorization belonging to another subject.");
3837 goto out;
3838 }
3839
3840 priv->temporary_authorization_store->authorizations = g_list_remove (priv->temporary_authorization_store->authorizations, ta);
3841 temporary_authorization_free (ta);
3842
3843 num_removed++;
3844 }
3845
3846 if (num_removed > 0)
3847 {
3848 g_signal_emit_by_name (authority, "changed");
3849 }
3850 else
3851 {
3852 g_set_error (error,
3853 POLKIT_ERROR,
3854 POLKIT_ERROR_FAILED,
3855 "No such authorization with id `%s'",
3856 id);
3857 goto out;
3858 }
3859
3860 ret = TRUE;
3861
3862 out:
3863 if (session_for_caller != NULL)
3864 g_object_unref (session_for_caller);
3865
3866 return ret;
3867 }
3868
3869 /* ---------------------------------------------------------------------------------------------------- */