Debian Codegraph @

src/polkitbackend/polkitbackendauthority.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 <string.h>
25 #include <syslog.h>
26 #include <stdarg.h>
27
28 #include <polkit/polkit.h>
29 #include <polkit/polkitprivate.h>
30
31 #include "polkitbackendauthority.h"
32 #include "polkitbackendjsauthority.h"
33
34 #include "polkitbackendprivate.h"
35
36 /**
37 * SECTION:polkitbackendauthority
38 * @title: PolkitBackendAuthority
39 * @short_description: Abstract base class for authority backends
40 * @stability: Unstable
41 * @see_also: PolkitBackendJsAuthority
42 *
43 * To implement an authority backend, simply subclass #PolkitBackendAuthority
44 * and implement the required VFuncs.
45 */
46
47 enum
48 {
49 CHANGED_SIGNAL,
50 SESSIONS_CHANGED_SIGNAL,
51 LAST_SIGNAL,
52 };
53
54 static guint signals[LAST_SIGNAL] = {0};
55 static guint polkit_authority_log_level = LOG_LEVEL_NOTICE;
56
57 G_DEFINE_ABSTRACT_TYPE (PolkitBackendAuthority, polkit_backend_authority, G_TYPE_OBJECT);
58
59 static void
60 polkit_backend_authority_init (PolkitBackendAuthority *authority)
61 {
62 }
63
64 static void
65 polkit_backend_authority_class_init (PolkitBackendAuthorityClass *klass)
66 {
67 /**
68 * PolkitBackendAuthority::changed:
69 * @authority: A #PolkitBackendAuthority.
70 *
71 * Emitted when actions and/or authorizations change.
72 */
73 signals[CHANGED_SIGNAL] = g_signal_new ("changed",
74 POLKIT_BACKEND_TYPE_AUTHORITY,
75 G_SIGNAL_RUN_LAST,
76 G_STRUCT_OFFSET (PolkitBackendAuthorityClass, changed),
77 NULL, /* accumulator */
78 NULL, /* accumulator data */
79 g_cclosure_marshal_VOID__VOID,
80 G_TYPE_NONE,
81 0);
82 signals[SESSIONS_CHANGED_SIGNAL] = g_signal_new ("sessions-changed",
83 POLKIT_BACKEND_TYPE_AUTHORITY,
84 G_SIGNAL_RUN_LAST,
85 G_STRUCT_OFFSET (PolkitBackendAuthorityClass, changed),
86 NULL, /* accumulator */
87 NULL, /* accumulator data */
88 g_cclosure_marshal_VOID__VOID,
89 G_TYPE_NONE,
90 0);
91 }
92
93 /**
94 * polkit_backend_authority_get_name:
95 * @authority: A #PolkitBackendAuthority.
96 *
97 * Gets the name of the authority backend.
98 *
99 * Returns: The name of the backend.
100 */
101 const gchar *
102 polkit_backend_authority_get_name (PolkitBackendAuthority *authority)
103 {
104 PolkitBackendAuthorityClass *klass;
105 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
106 if (klass->get_name == NULL)
107 return "(not set)";
108 return klass->get_name (authority);
109 }
110
111 /**
112 * polkit_backend_authority_get_version:
113 * @authority: A #PolkitBackendAuthority.
114 *
115 * Gets the version of the authority backend.
116 *
117 * Returns: The name of the backend.
118 */
119 const gchar *
120 polkit_backend_authority_get_version (PolkitBackendAuthority *authority)
121 {
122 PolkitBackendAuthorityClass *klass;
123 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
124 if (klass->get_version == NULL)
125 return "(not set)";
126 return klass->get_version (authority);
127 }
128
129 /**
130 * polkit_backend_authority_get_features:
131 * @authority: A #PolkitBackendAuthority.
132 *
133 * Gets the features supported by the authority backend.
134 *
135 * Returns: Flags from #PolkitAuthorityFeatures.
136 */
137 PolkitAuthorityFeatures
138 polkit_backend_authority_get_features (PolkitBackendAuthority *authority)
139 {
140 PolkitBackendAuthorityClass *klass;
141 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
142 if (klass->get_features == NULL)
143 return POLKIT_AUTHORITY_FEATURES_NONE;
144 return klass->get_features (authority);
145 }
146
147 /**
148 * polkit_backend_authority_enumerate_actions:
149 * @authority: A #PolkitBackendAuthority.
150 * @caller: The system bus name that initiated the query.
151 * @locale: The locale to retrieve descriptions for.
152 * @error: Return location for error or %NULL.
153 *
154 * Retrieves all registered actions.
155 *
156 * Returns: A list of #PolkitActionDescription objects or %NULL if @error is set. The returned list
157 * should be freed with g_list_free() after each element have been freed with g_object_unref().
158 **/
159 GList *
160 polkit_backend_authority_enumerate_actions (PolkitBackendAuthority *authority,
161 PolkitSubject *caller,
162 const gchar *locale,
163 GError **error)
164 {
165 PolkitBackendAuthorityClass *klass;
166
167 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
168
169 if (klass->enumerate_actions == NULL)
170 {
171 g_warning ("enumerate_actions is not implemented (it is not optional)");
172 g_set_error (error,
173 POLKIT_ERROR,
174 POLKIT_ERROR_NOT_SUPPORTED,
175 "Operation not supported (bug in backend)");
176 return NULL;
177 }
178 else
179 {
180 return klass->enumerate_actions (authority, caller, locale, error);
181 }
182 }
183
184 /* ---------------------------------------------------------------------------------------------------- */
185
186 /**
187 * polkit_backend_authority_check_authorization:
188 * @authority: A #PolkitBackendAuthority.
189 * @caller: The system bus name that initiated the query.
190 * @subject: A #PolkitSubject.
191 * @action_id: The action to check for.
192 * @details: Details about the action or %NULL.
193 * @flags: A set of #PolkitCheckAuthorizationFlags.
194 * @cancellable: A #GCancellable.
195 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
196 * @user_data: The data to pass to @callback.
197 *
198 * Asynchronously checks if @subject is authorized to perform the action represented
199 * by @action_id.
200 *
201 * When the operation is finished, @callback will be invoked. You can then
202 * call polkit_backend_authority_check_authorization_finish() to get the result of
203 * the operation.
204 **/
205 void
206 polkit_backend_authority_check_authorization (PolkitBackendAuthority *authority,
207 PolkitSubject *caller,
208 PolkitSubject *subject,
209 const gchar *action_id,
210 PolkitDetails *details,
211 PolkitCheckAuthorizationFlags flags,
212 GCancellable *cancellable,
213 GAsyncReadyCallback callback,
214 gpointer user_data)
215 {
216 PolkitBackendAuthorityClass *klass;
217
218 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
219
220 if (klass->check_authorization == NULL)
221 {
222 GSimpleAsyncResult *simple;
223
224 g_warning ("check_authorization is not implemented (it is not optional)");
225
226 simple = g_simple_async_result_new_error (G_OBJECT (authority),
227 callback,
228 user_data,
229 POLKIT_ERROR,
230 POLKIT_ERROR_NOT_SUPPORTED,
231 "Operation not supported (bug in backend)");
232 g_simple_async_result_complete (simple);
233 g_object_unref (simple);
234 }
235 else
236 {
237 klass->check_authorization (authority, caller, subject, action_id, details, flags, cancellable, callback, user_data);
238 }
239 }
240
241 /**
242 * polkit_backend_authority_check_authorization_finish:
243 * @authority: A #PolkitBackendAuthority.
244 * @res: A #GAsyncResult obtained from the callback.
245 * @error: Return location for error or %NULL.
246 *
247 * Finishes checking if a subject is authorized for an action.
248 *
249 * Returns: A #PolkitAuthorizationResult or %NULL if @error is set. Free with g_object_unref().
250 **/
251 PolkitAuthorizationResult *
252 polkit_backend_authority_check_authorization_finish (PolkitBackendAuthority *authority,
253 GAsyncResult *res,
254 GError **error)
255 {
256 PolkitBackendAuthorityClass *klass;
257
258 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
259
260 if (klass->check_authorization_finish == NULL)
261 {
262 g_warning ("check_authorization_finish is not implemented (it is not optional)");
263 g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
264 return NULL;
265 }
266 else
267 {
268 return klass->check_authorization_finish (authority, res, error);
269 }
270 }
271
272 /* ---------------------------------------------------------------------------------------------------- */
273
274 /**
275 * polkit_backend_authority_register_authentication_agent:
276 * @authority: A #PolkitBackendAuthority.
277 * @caller: The system bus name that initiated the query.
278 * @subject: The subject the authentication agent wants to register for.
279 * @locale: The locale of the authentication agent.
280 * @object_path: The object path for the authentication agent.
281 * @options: A #GVariant with options or %NULL.
282 * @error: Return location for error or %NULL.
283 *
284 * Registers an authentication agent.
285 *
286 * Returns: %TRUE if the authentication agent was successfully registered, %FALSE if @error is set.
287 **/
288 gboolean
289 polkit_backend_authority_register_authentication_agent (PolkitBackendAuthority *authority,
290 PolkitSubject *caller,
291 PolkitSubject *subject,
292 const gchar *locale,
293 const gchar *object_path,
294 GVariant *options,
295 GError **error)
296 {
297 PolkitBackendAuthorityClass *klass;
298
299 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
300
301 if (klass->register_authentication_agent == NULL)
302 {
303 g_set_error (error,
304 POLKIT_ERROR,
305 POLKIT_ERROR_NOT_SUPPORTED,
306 "Operation not supported");
307 return FALSE;
308 }
309 else
310 {
311 return klass->register_authentication_agent (authority, caller, subject, locale, object_path, options, error);
312 }
313 }
314
315 /**
316 * polkit_backend_authority_unregister_authentication_agent:
317 * @authority: A #PolkitBackendAuthority.
318 * @caller: The system bus name that initiated the query.
319 * @subject: The subject the agent claims to be registered at.
320 * @object_path: The object path that the authentication agent is registered at.
321 * @error: Return location for error or %NULL.
322 *
323 * Unregisters an authentication agent.
324 *
325 * Returns: %TRUE if the authentication agent was successfully unregistered, %FALSE if @error is set.
326 **/
327 gboolean
328 polkit_backend_authority_unregister_authentication_agent (PolkitBackendAuthority *authority,
329 PolkitSubject *caller,
330 PolkitSubject *subject,
331 const gchar *object_path,
332 GError **error)
333 {
334 PolkitBackendAuthorityClass *klass;
335
336 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
337
338 if (klass->unregister_authentication_agent == NULL)
339 {
340 g_set_error (error,
341 POLKIT_ERROR,
342 POLKIT_ERROR_NOT_SUPPORTED,
343 "Operation not supported");
344 return FALSE;
345 }
346 else
347 {
348 return klass->unregister_authentication_agent (authority, caller, subject, object_path, error);
349 }
350 }
351
352 /**
353 * polkit_backend_authority_authentication_agent_response:
354 * @authority: A #PolkitBackendAuthority.
355 * @caller: The system bus name that initiated the query.
356 * @uid: The real UID of the registered agent, or (uid_t)-1 if unknown.
357 * @cookie: The cookie passed to the authentication agent from the authority.
358 * @identity: The identity that was authenticated.
359 * @error: Return location for error or %NULL.
360 *
361 * Provide response that @identity successfully authenticated for the
362 * authentication request identified by @cookie.
363 *
364 * Returns: %TRUE if @authority acknowledged the call, %FALSE if @error is set.
365 **/
366 gboolean
367 polkit_backend_authority_authentication_agent_response (PolkitBackendAuthority *authority,
368 PolkitSubject *caller,
369 uid_t uid,
370 const gchar *cookie,
371 PolkitIdentity *identity,
372 GError **error)
373 {
374 PolkitBackendAuthorityClass *klass;
375
376 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
377
378 if (klass->authentication_agent_response == NULL)
379 {
380 g_set_error (error,
381 POLKIT_ERROR,
382 POLKIT_ERROR_NOT_SUPPORTED,
383 "Operation not supported");
384 return FALSE;
385 }
386 else
387 {
388 return klass->authentication_agent_response (authority, caller, uid, cookie, identity, error);
389 }
390 }
391
392 /* ---------------------------------------------------------------------------------------------------- */
393
394 /**
395 * polkit_backend_authority_enumerate_temporary_authorizations:
396 * @authority: A #PolkitBackendAuthority.
397 * @caller: The system bus name that initiated the query.
398 * @subject: The subject to get temporary authorizations for.
399 * @error: Return location for error.
400 *
401 * Gets temporary authorizations for @subject.
402 *
403 * Returns: A list of #PolkitTemporaryAuthorization objects or %NULL if @error is set. The returned list
404 * should be freed with g_list_free() after each element have been freed with g_object_unref().
405 */
406 GList *
407 polkit_backend_authority_enumerate_temporary_authorizations (PolkitBackendAuthority *authority,
408 PolkitSubject *caller,
409 PolkitSubject *subject,
410 GError **error)
411 {
412 PolkitBackendAuthorityClass *klass;
413
414 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
415
416 if (klass->enumerate_temporary_authorizations == NULL)
417 {
418 g_set_error (error,
419 POLKIT_ERROR,
420 POLKIT_ERROR_NOT_SUPPORTED,
421 "Operation not supported");
422 return NULL;
423 }
424 else
425 {
426 return klass->enumerate_temporary_authorizations (authority, caller, subject, error);
427 }
428 }
429
430 /**
431 * polkit_backend_authority_revoke_temporary_authorizations:
432 * @authority: A #PolkitBackendAuthority.
433 * @caller: The system bus name that initiated the query.
434 * @subject: The subject to revoke temporary authorizations for.
435 * @error: Return location for error.
436 *
437 * Revokes temporary authorizations for @subject.
438 *
439 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
440 **/
441 gboolean
442 polkit_backend_authority_revoke_temporary_authorizations (PolkitBackendAuthority *authority,
443 PolkitSubject *caller,
444 PolkitSubject *subject,
445 GError **error)
446 {
447 PolkitBackendAuthorityClass *klass;
448
449 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
450
451 if (klass->revoke_temporary_authorizations == NULL)
452 {
453 g_set_error (error,
454 POLKIT_ERROR,
455 POLKIT_ERROR_NOT_SUPPORTED,
456 "Operation not supported");
457 return FALSE;
458 }
459 else
460 {
461 return klass->revoke_temporary_authorizations (authority, caller, subject, error);
462 }
463 }
464
465 /**
466 * polkit_backend_authority_revoke_temporary_authorization_by_id:
467 * @authority: A #PolkitBackendAuthority.
468 * @caller: The system bus name that initiated the query.
469 * @id: The opaque identifier of the temporary authorization.
470 * @error: Return location for error.
471 *
472 * Revokes a temporary authorizations with opaque identifier @id.
473 *
474 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
475 **/
476 gboolean
477 polkit_backend_authority_revoke_temporary_authorization_by_id (PolkitBackendAuthority *authority,
478 PolkitSubject *caller,
479 const gchar *id,
480 GError **error)
481 {
482 PolkitBackendAuthorityClass *klass;
483
484 klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority);
485
486 if (klass->revoke_temporary_authorization_by_id == NULL)
487 {
488 g_set_error (error,
489 POLKIT_ERROR,
490 POLKIT_ERROR_NOT_SUPPORTED,
491 "Operation not supported");
492 return FALSE;
493 }
494 else
495 {
496 return klass->revoke_temporary_authorization_by_id (authority, caller, id, error);
497 }
498 }
499
500 /* ---------------------------------------------------------------------------------------------------- */
501
502 typedef struct
503 {
504 guint authority_registration_id;
505 guint log_control_registration_id;
506
507 GDBusNodeInfo *introspection_info;
508
509 PolkitBackendAuthority *authority;
510
511 GDBusConnection *connection;
512
513 gulong authority_changed_id;
514
515 gulong authority_session_monitor_signaller;
516
517 gchar *object_path;
518
519 GHashTable *cancellation_id_to_check_auth_data;
520 } Server;
521
522 static void
523 server_free (Server *server)
524 {
525 g_free (server->object_path);
526
527 if (server->authority_registration_id > 0)
528 g_dbus_connection_unregister_object (server->connection, server->authority_registration_id);
529
530 if (server->log_control_registration_id > 0)
531 g_dbus_connection_unregister_object (server->connection, server->log_control_registration_id);
532
533 if (server->connection != NULL)
534 g_object_unref (server->connection);
535
536 if (server->introspection_info != NULL)
537 g_dbus_node_info_unref (server->introspection_info);
538
539 if (server->authority != NULL && server->authority_changed_id > 0)
540 g_signal_handler_disconnect (server->authority, server->authority_changed_id);
541
542 if (server->authority != NULL && server->authority_session_monitor_signaller > 0)
543 g_signal_handler_disconnect (server->authority, server->authority_session_monitor_signaller);
544
545 if (server->cancellation_id_to_check_auth_data != NULL)
546 g_hash_table_unref (server->cancellation_id_to_check_auth_data);
547
548 g_object_unref (server->authority);
549
550 g_free (server);
551 }
552
553 static void changed_dbus_call_handler(PolkitBackendAuthority *authority,
554 gpointer user_data,
555 guint16 msg_mask)
556 {
557 Server *server = user_data;
558 GError *error;
559 GVariant *parameters;
560
561 error = NULL;
562
563 parameters = g_variant_new("(q)", msg_mask);
564 if (!g_dbus_connection_emit_signal (server->connection,
565 NULL, /* destination bus name */
566 server->object_path,
567 "org.freedesktop.PolicyKit1.Authority",
568 "Changed",
569 parameters,
570 &error))
571 {
572 g_warning ("Error emitting Changed() signal: %s", error->message);
573 g_error_free (error);
574 }
575 }
576
577
578 static void
579 on_authority_changed (PolkitBackendAuthority *authority,
580 gpointer user_data)
581 {
582 guint16 msg_mask;
583
584 msg_mask = (guint16) CHANGED_SIGNAL;
585 changed_dbus_call_handler(authority, user_data, msg_mask);
586 }
587
588
589 static void
590 on_sessions_changed (PolkitBackendAuthority *authority,
591 gpointer user_data)
592 {
593 guint16 msg_mask;
594
595 msg_mask = (guint16) SESSIONS_CHANGED_SIGNAL;
596 changed_dbus_call_handler(authority, user_data, msg_mask);
597 }
598
599
600 static const gchar *server_introspection_data =
601 "<node>"
602 " <interface name='org.freedesktop.PolicyKit1.Authority'>"
603 " <method name='EnumerateActions'>"
604 " <arg type='s' name='locale' direction='in'/>"
605 " <arg type='a(ssssssuuua{ss})' name='action_descriptions' direction='out'/>"
606 " </method>"
607 " <method name='CheckAuthorization'>"
608 " <arg type='(sa{sv})' name='subject' direction='in'/>"
609 " <arg type='s' name='action_id' direction='in'/>"
610 " <arg type='a{ss}' name='details' direction='in'/>"
611 " <arg type='u' name='flags' direction='in'/>"
612 " <arg type='s' name='cancellation_id' direction='in'/>"
613 " <arg type='(bba{ss})' name='result' direction='out'/>"
614 " </method>"
615 " <method name='CancelCheckAuthorization'>"
616 " <arg type='s' name='cancellation_id' direction='in'/>"
617 " </method>"
618 " <method name='RegisterAuthenticationAgent'>"
619 " <arg type='(sa{sv})' name='subject' direction='in'/>"
620 " <arg type='s' name='locale' direction='in'/>"
621 " <arg type='s' name='object_path' direction='in'/>"
622 " </method>"
623 " <method name='RegisterAuthenticationAgentWithOptions'>"
624 " <arg type='(sa{sv})' name='subject' direction='in'/>"
625 " <arg type='s' name='locale' direction='in'/>"
626 " <arg type='s' name='object_path' direction='in'/>"
627 " <arg type='a{sv}' name='options' direction='in'/>"
628 " </method>"
629 " <method name='UnregisterAuthenticationAgent'>"
630 " <arg type='(sa{sv})' name='subject' direction='in'/>"
631 " <arg type='s' name='object_path' direction='in'/>"
632 " </method>"
633 " <method name='AuthenticationAgentResponse'>"
634 " <arg type='s' name='cookie' direction='in'/>"
635 " <arg type='(sa{sv})' name='identity' direction='in'/>"
636 " </method>"
637 " <method name='AuthenticationAgentResponse2'>"
638 " <arg type='u' name='uid' direction='in'/>"
639 " <arg type='s' name='cookie' direction='in'/>"
640 " <arg type='(sa{sv})' name='identity' direction='in'/>"
641 " </method>"
642 " <method name='AuthenticationAgentResponse3'>"
643 " <arg type='s' name='cookie' direction='in'/>"
644 " <arg type='(sa{sv})' name='identity' direction='in'/>"
645 " <arg type='(sa{sv})' name='subject' direction='in'/>"
646 " </method>"
647 " <method name='EnumerateTemporaryAuthorizations'>"
648 " <arg type='(sa{sv})' name='subject' direction='in'/>"
649 " <arg type='a(ss(sa{sv})tt)' name='temporary_authorizations' direction='out'/>"
650 " </method>"
651 " <method name='RevokeTemporaryAuthorizations'>"
652 " <arg type='(sa{sv})' name='subject' direction='in'/>"
653 " </method>"
654 " <method name='RevokeTemporaryAuthorizationById'>"
655 " <arg type='s' name='id' direction='in'/>"
656 " </method>"
657 " <signal name='Changed'/>"
658 " <property type='s' name='BackendName' access='read'/>"
659 " <property type='s' name='BackendVersion' access='read'/>"
660 " <property type='u' name='BackendFeatures' access='read'/>"
661 " </interface>"
662 " <interface name='org.freedesktop.LogControl1'>"
663 " <property type='s' name='LogLevel' access='readwrite'>"
664 " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>"
665 " </property>"
666 " <property type='s' name='LogTarget' access='readwrite'>"
667 " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>"
668 " </property>"
669 " <property type='s' name='SyslogIdentifier' access='read'>"
670 " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>"
671 " </property>"
672 " </interface>"
673 "</node>";
674
675 /* ---------------------------------------------------------------------------------------------------- */
676
677 static void
678 server_handle_enumerate_actions (Server *server,
679 GVariant *parameters,
680 PolkitSubject *caller,
681 GDBusMethodInvocation *invocation)
682 {
683 GVariantBuilder builder;
684 GError *error;
685 GList *actions;
686 GList *l;
687 const gchar *locale;
688
689 actions = NULL;
690
691 g_variant_get (parameters, "(&s)", &locale);
692
693 error = NULL;
694 actions = polkit_backend_authority_enumerate_actions (server->authority,
695 caller,
696 locale,
697 &error);
698 if (error != NULL)
699 {
700 g_dbus_method_invocation_return_gerror (invocation, error);
701 g_error_free (error);
702 goto out;
703 }
704
705 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ssssssuuua{ss})"));
706 for (l = actions; l != NULL; l = l->next)
707 {
708 PolkitActionDescription *ad = POLKIT_ACTION_DESCRIPTION (l->data);
709 g_variant_builder_add_value (&builder,
710 polkit_action_description_to_gvariant (ad)); /* A floating value */
711 }
712 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(ssssssuuua{ss}))", &builder));
713
714 out:
715 g_list_foreach (actions, (GFunc) g_object_unref, NULL);
716 g_list_free (actions);
717 }
718
719 /* ---------------------------------------------------------------------------------------------------- */
720
721 typedef struct
722 {
723 GDBusMethodInvocation *invocation;
724 Server *server;
725 PolkitSubject *caller;
726 PolkitSubject *subject;
727 GCancellable *cancellable;
728 gchar *cancellation_id;
729 } CheckAuthData;
730
731 static void
732 check_auth_data_free (CheckAuthData *data)
733 {
734 if (data->invocation != NULL)
735 g_object_unref (data->invocation);
736 if (data->caller != NULL)
737 g_object_unref (data->caller);
738 if (data->subject != NULL)
739 g_object_unref (data->subject);
740 if (data->cancellable != NULL)
741 g_object_unref (data->cancellable);
742 g_free (data->cancellation_id);
743 g_free (data);
744 }
745
746 static void
747 check_auth_cb (GObject *source_object,
748 GAsyncResult *res,
749 gpointer user_data)
750 {
751 CheckAuthData *data = user_data;
752 PolkitAuthorizationResult *result;
753 GError *error;
754
755 error = NULL;
756 result = polkit_backend_authority_check_authorization_finish (POLKIT_BACKEND_AUTHORITY (source_object),
757 res,
758 &error);
759
760 if (data->cancellation_id != NULL)
761 g_hash_table_remove (data->server->cancellation_id_to_check_auth_data, data->cancellation_id);
762
763 if (error != NULL)
764 {
765 g_dbus_method_invocation_return_gerror (data->invocation, error);
766 g_error_free (error);
767 }
768 else
769 {
770 g_dbus_method_invocation_return_value (data->invocation,
771 g_variant_new ("(@(bba{ss}))",
772 polkit_authorization_result_to_gvariant (result))); /* A floating value */
773 g_object_unref (result);
774 }
775
776 check_auth_data_free (data);
777 }
778
779 static void
780 server_handle_check_authorization (Server *server,
781 GVariant *parameters,
782 PolkitSubject *caller,
783 GDBusMethodInvocation *invocation)
784 {
785 GVariant *subject_gvariant;
786 const gchar *action_id;
787 GVariant *details_gvariant;
788 guint32 flags;
789 const gchar *cancellation_id;
790 GError *error;
791 PolkitSubject *subject;
792 PolkitDetails *details;
793
794 subject = NULL;
795 details = NULL;
796
797 g_variant_get (parameters,
798 "(@(sa{sv})&s@a{ss}u&s)",
799 &subject_gvariant,
800 &action_id,
801 &details_gvariant,
802 &flags,
803 &cancellation_id);
804
805 error = NULL;
806 subject = polkit_subject_new_for_gvariant_invocation (subject_gvariant, invocation, &error);
807 if (subject == NULL)
808 {
809 g_prefix_error (&error, "Error getting subject: ");
810 g_dbus_method_invocation_return_gerror (invocation, error);
811 g_error_free (error);
812 goto out;
813 }
814
815 details = polkit_details_new_for_gvariant (details_gvariant);
816
817 CheckAuthData *data;
818 data = g_new0 (CheckAuthData, 1);
819
820 data->server = server;
821 data->caller = g_object_ref (caller);
822 data->subject = g_object_ref (subject);
823 data->invocation = g_object_ref (invocation);
824
825 if (strlen (cancellation_id) > 0)
826 {
827 data->cancellation_id = g_strdup_printf ("%s-%s",
828 g_dbus_method_invocation_get_sender (invocation),
829 cancellation_id);
830 if (g_hash_table_lookup (server->cancellation_id_to_check_auth_data, data->cancellation_id) != NULL)
831 {
832 gchar *message;
833 message = g_strdup_printf ("Given cancellation_id %s is already in use for name %s",
834 cancellation_id,
835 g_dbus_method_invocation_get_sender (invocation));
836 /* Don't want this error in our GError enum since libpolkit-gobject-1 users will never see it */
837 g_dbus_method_invocation_return_dbus_error (invocation,
838 "org.freedesktop.PolicyKit1.Error.CancellationIdNotUnique",
839 message);
840 g_free (message);
841 check_auth_data_free (data);
842 goto out;
843 }
844
845 data->cancellable = g_cancellable_new ();
846 g_hash_table_insert (server->cancellation_id_to_check_auth_data,
847 data->cancellation_id,
848 data);
849 }
850
851 polkit_backend_authority_check_authorization (server->authority,
852 caller,
853 subject,
854 action_id,
855 details,
856 flags,
857 data->cancellable,
858 check_auth_cb,
859 data);
860
861 out:
862
863 g_variant_unref (subject_gvariant);
864 g_variant_unref (details_gvariant);
865
866 if (details != NULL)
867 g_object_unref (details);
868 if (subject != NULL)
869 g_object_unref (subject);
870 }
871
872 /* ---------------------------------------------------------------------------------------------------- */
873
874 static void
875 server_handle_cancel_check_authorization (Server *server,
876 GVariant *parameters,
877 PolkitSubject *caller,
878 GDBusMethodInvocation *invocation)
879 {
880 CheckAuthData *data;
881 const gchar *cancellation_id;
882 gchar *full_cancellation_id;
883
884 g_variant_get (parameters, "(&s)", &cancellation_id);
885
886 full_cancellation_id = g_strdup_printf ("%s-%s",
887 g_dbus_method_invocation_get_sender (invocation),
888 cancellation_id);
889
890 data = g_hash_table_lookup (server->cancellation_id_to_check_auth_data, full_cancellation_id);
891 if (data == NULL)
892 {
893 g_dbus_method_invocation_return_error (invocation,
894 POLKIT_ERROR,
895 POLKIT_ERROR_FAILED,
896 "No such cancellation_id `%s' for name %s",
897 cancellation_id,
898 g_dbus_method_invocation_get_sender (invocation));
899 goto out;
900 }
901
902 g_cancellable_cancel (data->cancellable);
903
904 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
905
906 out:
907 g_free (full_cancellation_id);
908 }
909
910 /* ---------------------------------------------------------------------------------------------------- */
911
912 static void
913 server_handle_register_authentication_agent (Server *server,
914 GVariant *parameters,
915 PolkitSubject *caller,
916 GDBusMethodInvocation *invocation)
917 {
918 GVariant *subject_gvariant;
919 GError *error;
920 PolkitSubject *subject;
921 const gchar *locale;
922 const gchar *object_path;
923
924 subject = NULL;
925
926 g_variant_get (parameters,
927 "(@(sa{sv})&s&s)",
928 &subject_gvariant,
929 &locale,
930 &object_path);
931
932 error = NULL;
933 subject = polkit_subject_new_for_gvariant (subject_gvariant, &error);
934 if (subject == NULL)
935 {
936 g_prefix_error (&error, "Error getting subject: ");
937 g_dbus_method_invocation_return_gerror (invocation, error);
938 g_error_free (error);
939 goto out;
940 }
941
942 error = NULL;
943 if (!polkit_backend_authority_register_authentication_agent (server->authority,
944 caller,
945 subject,
946 locale,
947 object_path,
948 NULL,
949 &error))
950 {
951 g_dbus_method_invocation_return_gerror (invocation, error);
952 g_error_free (error);
953 goto out;
954 }
955
956 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
957
958 out:
959 g_variant_unref (subject_gvariant);
960 if (subject != NULL)
961 g_object_unref (subject);
962 }
963
964 /* ---------------------------------------------------------------------------------------------------- */
965
966 static void
967 server_handle_register_authentication_agent_with_options (Server *server,
968 GVariant *parameters,
969 PolkitSubject *caller,
970 GDBusMethodInvocation *invocation)
971 {
972 GVariant *subject_gvariant;
973 GError *error;
974 PolkitSubject *subject;
975 const gchar *locale;
976 const gchar *object_path;
977 GVariant *options;
978
979 subject = NULL;
980
981 g_variant_get (parameters,
982 "(@(sa{sv})&s&s@a{sv})",
983 &subject_gvariant,
984 &locale,
985 &object_path,
986 &options);
987
988 error = NULL;
989 subject = polkit_subject_new_for_gvariant (subject_gvariant, &error);
990 if (subject == NULL)
991 {
992 g_prefix_error (&error, "Error getting subject: ");
993 g_dbus_method_invocation_return_gerror (invocation, error);
994 g_error_free (error);
995 goto out;
996 }
997
998 error = NULL;
999 if (!polkit_backend_authority_register_authentication_agent (server->authority,
1000 caller,
1001 subject,
1002 locale,
1003 object_path,
1004 options,
1005 &error))
1006 {
1007 g_dbus_method_invocation_return_gerror (invocation, error);
1008 g_error_free (error);
1009 goto out;
1010 }
1011
1012 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1013
1014 out:
1015 g_variant_unref (subject_gvariant);
1016 if (options != NULL)
1017 g_variant_unref (options);
1018 if (subject != NULL)
1019 g_object_unref (subject);
1020 }
1021
1022 /* ---------------------------------------------------------------------------------------------------- */
1023
1024 static void
1025 server_handle_unregister_authentication_agent (Server *server,
1026 GVariant *parameters,
1027 PolkitSubject *caller,
1028 GDBusMethodInvocation *invocation)
1029 {
1030 GVariant *subject_gvariant;
1031 GError *error;
1032 PolkitSubject *subject;
1033 const gchar *object_path;
1034
1035 subject = NULL;
1036
1037 g_variant_get (parameters,
1038 "(@(sa{sv})&s)",
1039 &subject_gvariant,
1040 &object_path);
1041
1042 error = NULL;
1043 subject = polkit_subject_new_for_gvariant (subject_gvariant, &error);
1044 if (subject == NULL)
1045 {
1046 g_prefix_error (&error, "Error getting subject: ");
1047 g_dbus_method_invocation_return_gerror (invocation, error);
1048 g_error_free (error);
1049 goto out;
1050 }
1051
1052 error = NULL;
1053 if (!polkit_backend_authority_unregister_authentication_agent (server->authority,
1054 caller,
1055 subject,
1056 object_path,
1057 &error))
1058 {
1059 g_dbus_method_invocation_return_gerror (invocation, error);
1060 g_error_free (error);
1061 goto out;
1062 }
1063
1064 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1065
1066 out:
1067 g_variant_unref (subject_gvariant);
1068 if (subject != NULL)
1069 g_object_unref (subject);
1070 }
1071
1072 /* ---------------------------------------------------------------------------------------------------- */
1073
1074 static void
1075 server_handle_authentication_agent_response (Server *server,
1076 GVariant *parameters,
1077 PolkitSubject *caller,
1078 GDBusMethodInvocation *invocation)
1079 {
1080 const gchar *cookie;
1081 GVariant *identity_gvariant;
1082 PolkitIdentity *identity;
1083 GError *error;
1084
1085 identity = NULL;
1086
1087 g_variant_get (parameters,
1088 "(&s@(sa{sv}))",
1089 &cookie,
1090 &identity_gvariant);
1091
1092 error = NULL;
1093 identity = polkit_identity_new_for_gvariant (identity_gvariant, &error);
1094 if (identity == NULL)
1095 {
1096 g_prefix_error (&error, "Error getting identity: ");
1097 g_dbus_method_invocation_return_gerror (invocation, error);
1098 g_error_free (error);
1099 goto out;
1100 }
1101
1102 error = NULL;
1103 if (!polkit_backend_authority_authentication_agent_response (server->authority,
1104 caller,
1105 (uid_t)-1,
1106 cookie,
1107 identity,
1108 &error))
1109 {
1110 g_dbus_method_invocation_return_gerror (invocation, error);
1111 g_error_free (error);
1112 goto out;
1113 }
1114
1115 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1116
1117 out:
1118 g_variant_unref (identity_gvariant);
1119 if (identity != NULL)
1120 g_object_unref (identity);
1121 }
1122
1123 static void
1124 server_handle_authentication_agent_response2 (Server *server,
1125 GVariant *parameters,
1126 PolkitSubject *caller,
1127 GDBusMethodInvocation *invocation)
1128 {
1129 const gchar *cookie;
1130 GVariant *identity_gvariant;
1131 PolkitIdentity *identity;
1132 GError *error;
1133 guint32 uid;
1134
1135 identity = NULL;
1136
1137 g_variant_get (parameters,
1138 "(u&s@(sa{sv}))",
1139 &uid,
1140 &cookie,
1141 &identity_gvariant);
1142
1143 error = NULL;
1144 identity = polkit_identity_new_for_gvariant (identity_gvariant, &error);
1145 if (identity == NULL)
1146 {
1147 g_prefix_error (&error, "Error getting identity: ");
1148 g_dbus_method_invocation_return_gerror (invocation, error);
1149 g_error_free (error);
1150 goto out;
1151 }
1152
1153 error = NULL;
1154 if (!polkit_backend_authority_authentication_agent_response (server->authority,
1155 caller,
1156 (uid_t)uid,
1157 cookie,
1158 identity,
1159 &error))
1160 {
1161 g_dbus_method_invocation_return_gerror (invocation, error);
1162 g_error_free (error);
1163 goto out;
1164 }
1165
1166 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1167
1168 out:
1169 g_variant_unref (identity_gvariant);
1170 if (identity != NULL)
1171 g_object_unref (identity);
1172 }
1173
1174 static void
1175 server_handle_authentication_agent_response3 (Server *server,
1176 GVariant *parameters,
1177 PolkitSubject *caller,
1178 GDBusMethodInvocation *invocation)
1179 {
1180 const gchar *cookie;
1181 GVariant *identity_gvariant;
1182 GVariant *subject_gvariant;
1183 PolkitIdentity *identity;
1184 PolkitSubject *subject;
1185 GError *error;
1186
1187 identity = NULL;
1188 subject = NULL;
1189
1190 g_variant_get (parameters,
1191 "(&s@(sa{sv})@(sa{sv}))",
1192 &cookie,
1193 &identity_gvariant,
1194 &subject_gvariant);
1195
1196 error = NULL;
1197 identity = polkit_identity_new_for_gvariant (identity_gvariant, &error);
1198 if (identity == NULL)
1199 {
1200 g_prefix_error (&error, "Error getting identity: ");
1201 g_dbus_method_invocation_return_gerror (invocation, error);
1202 g_error_free (error);
1203 goto out;
1204 }
1205
1206 error = NULL;
1207 subject = polkit_subject_new_for_gvariant (subject_gvariant, &error);
1208 if (subject == NULL)
1209 {
1210 g_prefix_error (&error, "Error getting subject: ");
1211 g_dbus_method_invocation_return_gerror (invocation, error);
1212 g_error_free (error);
1213 goto out;
1214 }
1215
1216 error = NULL;
1217 if (polkit_unix_process_get_pidfd (POLKIT_UNIX_PROCESS (subject)) < 0 ||
1218 polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject)) <= 0)
1219 {
1220 g_set_error (&error,
1221 POLKIT_ERROR,
1222 POLKIT_ERROR_FAILED,
1223 "Subject's PIDFD '%d' for PID '%d' is no longer valid.",
1224 polkit_unix_process_get_pidfd (POLKIT_UNIX_PROCESS (subject)),
1225 polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject)));
1226 g_dbus_method_invocation_return_gerror (invocation, error);
1227 g_error_free (error);
1228 goto out;
1229 }
1230
1231 error = NULL;
1232 if (!polkit_backend_authority_authentication_agent_response (server->authority,
1233 caller,
1234 polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject)),
1235 cookie,
1236 identity,
1237 &error))
1238 {
1239 g_dbus_method_invocation_return_gerror (invocation, error);
1240 g_error_free (error);
1241 goto out;
1242 }
1243
1244 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1245
1246 out:
1247 g_variant_unref (identity_gvariant);
1248 g_variant_unref (subject_gvariant);
1249 if (identity != NULL)
1250 g_object_unref (identity);
1251 if (subject != NULL)
1252 g_object_unref (subject);
1253 }
1254
1255 /* ---------------------------------------------------------------------------------------------------- */
1256
1257 static void
1258 server_handle_enumerate_temporary_authorizations (Server *server,
1259 GVariant *parameters,
1260 PolkitSubject *caller,
1261 GDBusMethodInvocation *invocation)
1262 {
1263 GVariant *subject_gvariant;
1264 GError *error;
1265 PolkitSubject *subject;
1266 GList *authorizations;
1267 GList *l;
1268 GVariantBuilder builder;
1269
1270 subject = NULL;
1271
1272 g_variant_get (parameters,
1273 "(@(sa{sv}))",
1274 &subject_gvariant);
1275
1276 error = NULL;
1277 subject = polkit_subject_new_for_gvariant (subject_gvariant, &error);
1278 if (subject == NULL)
1279 {
1280 g_prefix_error (&error, "Error getting subject: ");
1281 g_dbus_method_invocation_return_gerror (invocation, error);
1282 g_error_free (error);
1283 goto out;
1284 }
1285
1286 error = NULL;
1287 authorizations = polkit_backend_authority_enumerate_temporary_authorizations (server->authority,
1288 caller,
1289 subject,
1290 &error);
1291 if (error != NULL)
1292 {
1293 g_dbus_method_invocation_return_gerror (invocation, error);
1294 g_error_free (error);
1295 goto out;
1296 }
1297
1298 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ss(sa{sv})tt)"));
1299 for (l = authorizations; l != NULL; l = l->next)
1300 {
1301 PolkitTemporaryAuthorization *a = POLKIT_TEMPORARY_AUTHORIZATION (l->data);
1302 g_variant_builder_add_value (&builder,
1303 polkit_temporary_authorization_to_gvariant (a)); /* A floating value */
1304 }
1305 g_list_foreach (authorizations, (GFunc) g_object_unref, NULL);
1306 g_list_free (authorizations);
1307 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(ss(sa{sv})tt))", &builder));
1308
1309 out:
1310 g_variant_unref (subject_gvariant);
1311 if (subject != NULL)
1312 g_object_unref (subject);
1313 }
1314
1315 /* ---------------------------------------------------------------------------------------------------- */
1316
1317 static void
1318 server_handle_revoke_temporary_authorizations (Server *server,
1319 GVariant *parameters,
1320 PolkitSubject *caller,
1321 GDBusMethodInvocation *invocation)
1322 {
1323 GVariant *subject_gvariant;
1324 GError *error;
1325 PolkitSubject *subject;
1326
1327 subject = NULL;
1328
1329 g_variant_get (parameters,
1330 "(@(sa{sv}))",
1331 &subject_gvariant);
1332
1333 error = NULL;
1334 subject = polkit_subject_new_for_gvariant (subject_gvariant, &error);
1335 if (subject == NULL)
1336 {
1337 g_prefix_error (&error, "Error getting subject: ");
1338 g_dbus_method_invocation_return_gerror (invocation, error);
1339 g_error_free (error);
1340 goto out;
1341 }
1342
1343 error = NULL;
1344 if (!polkit_backend_authority_revoke_temporary_authorizations (server->authority,
1345 caller,
1346 subject,
1347 &error))
1348 {
1349 g_dbus_method_invocation_return_gerror (invocation, error);
1350 g_error_free (error);
1351 goto out;
1352 }
1353
1354 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1355
1356 out:
1357 g_variant_unref (subject_gvariant);
1358 if (subject != NULL)
1359 g_object_unref (subject);
1360 }
1361
1362 /* ---------------------------------------------------------------------------------------------------- */
1363
1364 static void
1365 server_handle_revoke_temporary_authorization_by_id (Server *server,
1366 GVariant *parameters,
1367 PolkitSubject *caller,
1368 GDBusMethodInvocation *invocation)
1369 {
1370 GError *error;
1371 const gchar *id;
1372
1373 g_variant_get (parameters,
1374 "(&s)",
1375 &id);
1376
1377 error = NULL;
1378 if (!polkit_backend_authority_revoke_temporary_authorization_by_id (server->authority,
1379 caller,
1380 id,
1381 &error))
1382 {
1383 g_dbus_method_invocation_return_gerror (invocation, error);
1384 g_error_free (error);
1385 goto out;
1386 }
1387
1388 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1389
1390 out:
1391 ;
1392 }
1393
1394 /* ---------------------------------------------------------------------------------------------------- */
1395
1396 static void
1397 server_handle_method_call (GDBusConnection *connection,
1398 const gchar *sender,
1399 const gchar *object_path,
1400 const gchar *interface_name,
1401 const gchar *method_name,
1402 GVariant *parameters,
1403 GDBusMethodInvocation *invocation,
1404 gpointer user_data)
1405 {
1406 Server *server = user_data;
1407 PolkitSubject *caller;
1408
1409 caller = polkit_system_bus_name_new (g_dbus_method_invocation_get_sender (invocation));
1410
1411 if (g_strcmp0 (method_name, "EnumerateActions") == 0)
1412 server_handle_enumerate_actions (server, parameters, caller, invocation);
1413 else if (g_strcmp0 (method_name, "CheckAuthorization") == 0)
1414 server_handle_check_authorization (server, parameters, caller, invocation);
1415 else if (g_strcmp0 (method_name, "CancelCheckAuthorization") == 0)
1416 server_handle_cancel_check_authorization (server, parameters, caller, invocation);
1417 else if (g_strcmp0 (method_name, "RegisterAuthenticationAgent") == 0)
1418 server_handle_register_authentication_agent (server, parameters, caller, invocation);
1419 else if (g_strcmp0 (method_name, "RegisterAuthenticationAgentWithOptions") == 0)
1420 server_handle_register_authentication_agent_with_options (server, parameters, caller, invocation);
1421 else if (g_strcmp0 (method_name, "UnregisterAuthenticationAgent") == 0)
1422 server_handle_unregister_authentication_agent (server, parameters, caller, invocation);
1423 else if (g_strcmp0 (method_name, "AuthenticationAgentResponse") == 0)
1424 server_handle_authentication_agent_response (server, parameters, caller, invocation);
1425 else if (g_strcmp0 (method_name, "AuthenticationAgentResponse2") == 0)
1426 server_handle_authentication_agent_response2 (server, parameters, caller, invocation);
1427 else if (g_strcmp0 (method_name, "AuthenticationAgentResponse3") == 0)
1428 server_handle_authentication_agent_response3 (server, parameters, caller, invocation);
1429 else if (g_strcmp0 (method_name, "EnumerateTemporaryAuthorizations") == 0)
1430 server_handle_enumerate_temporary_authorizations (server, parameters, caller, invocation);
1431 else if (g_strcmp0 (method_name, "RevokeTemporaryAuthorizations") == 0)
1432 server_handle_revoke_temporary_authorizations (server, parameters, caller, invocation);
1433 else if (g_strcmp0 (method_name, "RevokeTemporaryAuthorizationById") == 0)
1434 server_handle_revoke_temporary_authorization_by_id (server, parameters, caller, invocation);
1435 else
1436 g_assert_not_reached ();
1437
1438 g_object_unref (caller);
1439 }
1440
1441 static GVariant *
1442 server_handle_get_property (GDBusConnection *connection,
1443 const gchar *sender,
1444 const gchar *object_path,
1445 const gchar *interface_name,
1446 const gchar *property_name,
1447 GError **error,
1448 gpointer user_data)
1449 {
1450 Server *server = user_data;
1451 GVariant *result;
1452
1453 result = NULL;
1454
1455 if (g_strcmp0 (interface_name, "org.freedesktop.PolicyKit1.Authority") == 0)
1456 {
1457 if (g_strcmp0 (property_name, "BackendName") == 0)
1458 {
1459 result = g_variant_new_string (polkit_backend_authority_get_name (server->authority));
1460 }
1461 else if (g_strcmp0 (property_name, "BackendVersion") == 0)
1462 {
1463 result = g_variant_new_string (polkit_backend_authority_get_version (server->authority));
1464 }
1465 else if (g_strcmp0 (property_name, "BackendFeatures") == 0)
1466 {
1467 result = g_variant_new_uint32 (polkit_backend_authority_get_features (server->authority));
1468 }
1469 else
1470 g_assert_not_reached ();
1471 }
1472 else if (g_strcmp0 (interface_name, "org.freedesktop.LogControl1") == 0)
1473 {
1474 if (g_strcmp0 (property_name, "LogLevel") == 0)
1475 {
1476 switch (polkit_authority_log_level)
1477 {
1478 case LOG_LEVEL_EMERG:
1479 result = g_variant_new_string ("emerg");
1480 break;
1481 case LOG_LEVEL_ALERT:
1482 result = g_variant_new_string ("alert");
1483 break;
1484 case LOG_LEVEL_CRIT:
1485 result = g_variant_new_string ("crit");
1486 break;
1487 case LOG_LEVEL_ERROR:
1488 result = g_variant_new_string ("err");
1489 break;
1490 case LOG_LEVEL_WARNING:
1491 result = g_variant_new_string ("warn");
1492 break;
1493 case LOG_LEVEL_NOTICE:
1494 result = g_variant_new_string ("notice");
1495 break;
1496 case LOG_LEVEL_INFO:
1497 result = g_variant_new_string ("info");
1498 break;
1499 case LOG_LEVEL_DEBUG:
1500 result = g_variant_new_string ("debug");
1501 break;
1502 }
1503 }
1504 else if (g_strcmp0 (property_name, "LogTarget") == 0)
1505 {
1506 result = g_variant_new_string ("syslog");
1507 }
1508 else if (g_strcmp0 (property_name, "SyslogIdentifier") == 0)
1509 {
1510 result = g_variant_new_string ("polkitd");
1511 }
1512 else
1513 g_assert_not_reached ();
1514
1515 }
1516
1517 return result;
1518 }
1519
1520 static gboolean
1521 server_handle_set_property (GDBusConnection *connection,
1522 const gchar *sender,
1523 const gchar *object_path,
1524 const gchar *interface_name,
1525 const gchar *property_name,
1526 GVariant *value,
1527 GError **error,
1528 gpointer user_data)
1529 {
1530 PolkitSubject *caller_subject;
1531 PolkitUnixUser *caller_user;
1532 const gchar *level;
1533
1534 if (g_strcmp0 (interface_name, "org.freedesktop.LogControl1") != 0)
1535 {
1536 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Only properties of org.freedesktop.LogControl1 can be modified");
1537 return FALSE;
1538 }
1539
1540 if (g_strcmp0 (property_name, "LogLevel") != 0)
1541 {
1542 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Only LogLevel can be modified");
1543 return FALSE;
1544 }
1545
1546 caller_subject = polkit_system_bus_name_new (sender);
1547 if (!caller_subject)
1548 {
1549 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Could not obtain caller's credentials");
1550 return FALSE;
1551 }
1552 caller_user = polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (caller_subject), NULL, error);
1553 if (!caller_user)
1554 {
1555 g_object_unref (caller_subject);
1556 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Could not obtain caller's credentials");
1557 return FALSE;
1558 }
1559 if ((uid_t)polkit_unix_user_get_uid (caller_user) != 0)
1560 {
1561 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED, "Only root can change the log level");
1562 g_object_unref (caller_user);
1563 g_object_unref (caller_subject);
1564 return FALSE;
1565 }
1566
1567 g_variant_get (value, "&s", &level);
1568 polkit_backend_authority_set_log_level (level);
1569
1570 g_object_unref (caller_user);
1571 g_object_unref (caller_subject);
1572
1573 return TRUE;
1574 }
1575
1576 /* ---------------------------------------------------------------------------------------------------- */
1577
1578 static const GDBusInterfaceVTable server_vtable =
1579 {
1580 server_handle_method_call,
1581 server_handle_get_property,
1582 NULL, /* server_handle_set_property */
1583 };
1584
1585 static const GDBusInterfaceVTable logcontrol_vtable =
1586 {
1587 NULL, /* server_handle_method_call */
1588 server_handle_get_property,
1589 server_handle_set_property,
1590 };
1591
1592 /**
1593 * polkit_backend_authority_unregister:
1594 * @registration_id: A #gpointer obtained from polkit_backend_authority_register().
1595 *
1596 * Unregisters a #PolkitBackendAuthority registered with polkit_backend_authority_register().
1597 */
1598 void
1599 polkit_backend_authority_unregister (gpointer registration_id)
1600 {
1601 Server *server = registration_id;
1602 server_free (server);
1603 }
1604
1605 /**
1606 * polkit_backend_authority_register:
1607 * @connection: The #GDBusConnection to register the authority on.
1608 * @authority: A #PolkitBackendAuthority.
1609 * @object_path: Object path of the authority.
1610 * @error: Return location for error.
1611 *
1612 * Registers @authority on a #GDBusConnection.
1613 *
1614 * Returns: A #gpointer that can be used with polkit_backend_authority_unregister() or %NULL if @error is set.
1615 */
1616 gpointer
1617 polkit_backend_authority_register (PolkitBackendAuthority *authority,
1618 GDBusConnection *connection,
1619 const gchar *object_path,
1620 GError **error)
1621 {
1622 Server *server;
1623
1624 server = g_new0 (Server, 1);
1625
1626 server->cancellation_id_to_check_auth_data = g_hash_table_new (g_str_hash, g_str_equal);
1627
1628 server->connection = g_object_ref (connection);
1629 server->object_path = g_strdup (object_path);
1630
1631 server->introspection_info = g_dbus_node_info_new_for_xml (server_introspection_data, error);
1632 if (server->introspection_info == NULL)
1633 goto error;
1634
1635 server->authority_registration_id = g_dbus_connection_register_object (server->connection,
1636 object_path,
1637 g_dbus_node_info_lookup_interface (server->introspection_info, "org.freedesktop.PolicyKit1.Authority"),
1638 &server_vtable,
1639 server,
1640 NULL,
1641 error);
1642 if (server->authority_registration_id == 0)
1643 {
1644 goto error;
1645 }
1646
1647 server->log_control_registration_id = g_dbus_connection_register_object (server->connection,
1648 "/org/freedesktop/LogControl1",
1649 g_dbus_node_info_lookup_interface (server->introspection_info, "org.freedesktop.LogControl1"),
1650 &logcontrol_vtable,
1651 server,
1652 NULL,
1653 error);
1654 if (server->log_control_registration_id == 0)
1655 {
1656 goto error;
1657 }
1658
1659 server->authority = g_object_ref (authority);
1660
1661 server->authority_changed_id = g_signal_connect (server->authority,
1662 "changed",
1663 G_CALLBACK (on_authority_changed),
1664 server);
1665
1666 server->authority_session_monitor_signaller = g_signal_connect (server->authority,
1667 "sessions-changed",
1668 G_CALLBACK (on_sessions_changed),
1669 server);
1670
1671 return server;
1672
1673 error:
1674 server_free (server);
1675 return NULL;
1676 }
1677
1678
1679 /**
1680 * polkit_backend_authority_get:
1681 *
1682 * Gets the #PolkitBackendAuthority to use.
1683 *
1684 * Returns: A #PolkitBackendAuthority. Free with g_object_unref().
1685 */
1686 PolkitBackendAuthority *
1687 polkit_backend_authority_get (void)
1688 {
1689 PolkitBackendAuthority *authority;
1690
1691 /* TODO: move to polkitd/main.c */
1692
1693 /* Announce that we've started in the generic log */
1694 openlog ("polkitd",
1695 LOG_PID,
1696 LOG_DAEMON); /* system daemons without separate facility value */
1697 syslog (LOG_INFO, "Started polkitd version %s", VERSION);
1698 closelog ();
1699
1700 /* then start logging to the secure log */
1701 openlog ("polkitd",
1702 LOG_PID,
1703 LOG_AUTHPRIV); /* security/authorization messages (private) */
1704
1705 authority = POLKIT_BACKEND_AUTHORITY (g_object_new (POLKIT_BACKEND_TYPE_JS_AUTHORITY, NULL));
1706
1707 return authority;
1708 }
1709
1710 /* ---------------------------------------------------------------------------------------------------- */
1711
1712 typedef enum
1713 {
1714 _COLOR_RESET,
1715 _COLOR_BOLD_ON,
1716 _COLOR_INVERSE_ON,
1717 _COLOR_BOLD_OFF,
1718 _COLOR_FG_BLACK,
1719 _COLOR_FG_RED,
1720 _COLOR_FG_GREEN,
1721 _COLOR_FG_YELLOW,
1722 _COLOR_FG_BLUE,
1723 _COLOR_FG_MAGENTA,
1724 _COLOR_FG_CYAN,
1725 _COLOR_FG_WHITE,
1726 _COLOR_BG_RED,
1727 _COLOR_BG_GREEN,
1728 _COLOR_BG_YELLOW,
1729 _COLOR_BG_BLUE,
1730 _COLOR_BG_MAGENTA,
1731 _COLOR_BG_CYAN,
1732 _COLOR_BG_WHITE
1733 } _Color;
1734
1735 static gboolean _color_stdin_is_tty = FALSE;
1736 static gboolean _color_initialized = FALSE;
1737
1738 static void
1739 _color_init (void)
1740 {
1741 if (_color_initialized)
1742 return;
1743 _color_initialized = TRUE;
1744 _color_stdin_is_tty = (isatty (STDIN_FILENO) != 0 && isatty (STDOUT_FILENO) != 0);
1745 }
1746
1747 static const gchar *
1748 _color_get (_Color color)
1749 {
1750 const gchar *str;
1751
1752 _color_init ();
1753
1754 if (!_color_stdin_is_tty)
1755 return "";
1756
1757 str = NULL;
1758 switch (color)
1759 {
1760 case _COLOR_RESET: str="\x1b[0m"; break;
1761 case _COLOR_BOLD_ON: str="\x1b[1m"; break;
1762 case _COLOR_INVERSE_ON: str="\x1b[7m"; break;
1763 case _COLOR_BOLD_OFF: str="\x1b[22m"; break;
1764 case _COLOR_FG_BLACK: str="\x1b[30m"; break;
1765 case _COLOR_FG_RED: str="\x1b[31m"; break;
1766 case _COLOR_FG_GREEN: str="\x1b[32m"; break;
1767 case _COLOR_FG_YELLOW: str="\x1b[33m"; break;
1768 case _COLOR_FG_BLUE: str="\x1b[34m"; break;
1769 case _COLOR_FG_MAGENTA: str="\x1b[35m"; break;
1770 case _COLOR_FG_CYAN: str="\x1b[36m"; break;
1771 case _COLOR_FG_WHITE: str="\x1b[37m"; break;
1772 case _COLOR_BG_RED: str="\x1b[41m"; break;
1773 case _COLOR_BG_GREEN: str="\x1b[42m"; break;
1774 case _COLOR_BG_YELLOW: str="\x1b[43m"; break;
1775 case _COLOR_BG_BLUE: str="\x1b[44m"; break;
1776 case _COLOR_BG_MAGENTA: str="\x1b[45m"; break;
1777 case _COLOR_BG_CYAN: str="\x1b[46m"; break;
1778 case _COLOR_BG_WHITE: str="\x1b[47m"; break;
1779 default:
1780 g_assert_not_reached ();
1781 break;
1782 }
1783 return str;
1784 }
1785
1786 /* ---------------------------------------------------------------------------------------------------- */
1787
1788 G_GNUC_PRINTF(3, 4)
1789 void
1790 polkit_backend_authority_log (PolkitBackendAuthority *authority,
1791 const guint message_log_level,
1792 const gchar *format,
1793 ...)
1794 {
1795 guint64 now;
1796 time_t now_time;
1797 struct tm *now_tm;
1798 gchar time_buf[128];
1799 gchar *message;
1800 va_list var_args;
1801
1802 if (message_log_level > polkit_authority_log_level)
1803 {
1804 return;
1805 }
1806
1807 g_return_if_fail (POLKIT_BACKEND_IS_AUTHORITY (authority));
1808
1809 va_start (var_args, format);
1810 message = g_strdup_vprintf (format, var_args);
1811 va_end (var_args);
1812
1813 syslog (message_log_level, "%s", message);
1814
1815 now = g_get_real_time ();
1816 now_time = (time_t) now / G_TIME_SPAN_SECOND;
1817 now_tm = localtime (&now_time);
1818 strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
1819 g_print ("%s%s%s.%03d%s: %s\n",
1820 _color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_YELLOW),
1821 time_buf, (gint) (now % G_TIME_SPAN_SECOND / G_TIME_SPAN_MILLISECOND),
1822 _color_get (_COLOR_RESET),
1823 message);
1824
1825 g_free (message);
1826 }
1827
1828 void
1829 polkit_backend_authority_set_log_level (const gchar *level)
1830 {
1831 /* Match syslog names so that they are the same across journalct, systemctl
1832 * et al, but also accept more readable aliases for abbreviated levels. */
1833 if (g_strcmp0 (level, "debug") == 0)
1834 polkit_authority_log_level = (guint) LOG_LEVEL_DEBUG;
1835 else if (g_strcmp0 (level, "info") == 0)
1836 polkit_authority_log_level = (guint) LOG_LEVEL_INFO;
1837 else if (g_strcmp0 (level, "notice") == 0)
1838 polkit_authority_log_level = (guint) LOG_LEVEL_NOTICE;
1839 else if (g_strcmp0 (level, "warning") == 0)
1840 polkit_authority_log_level = (guint) LOG_LEVEL_WARNING;
1841 else if (g_strcmp0 (level, "err") == 0 || g_strcmp0 (level, "error") == 0)
1842 polkit_authority_log_level = (guint) LOG_LEVEL_ERROR;
1843 else if (g_strcmp0 (level, "crit") == 0 || g_strcmp0 (level, "critical") == 0)
1844 polkit_authority_log_level = (guint) LOG_LEVEL_CRIT;
1845 else if (g_strcmp0 (level, "alert") == 0)
1846 polkit_authority_log_level = (guint) LOG_LEVEL_ALERT;
1847 else if (g_strcmp0 (level, "emerg") == 0 || g_strcmp0 (level, "emergency") == 0)
1848 polkit_authority_log_level = (guint) LOG_LEVEL_EMERG;
1849 }