| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | /* GDBus - GLib D-Bus Library
 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-05-09 13:14:55 -04:00
										 |  |  |  * Copyright (C) 2008-2010 Red Hat, Inc. | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU Lesser General Public | 
					
						
							|  |  |  |  * License as published by the Free Software Foundation; either | 
					
						
							|  |  |  |  * version 2 of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  |  * Lesser General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Lesser General | 
					
						
							|  |  |  |  * Public License along with this library; if not, write to the | 
					
						
							|  |  |  |  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, | 
					
						
							|  |  |  |  * Boston, MA 02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Author: David Zeuthen <davidz@redhat.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gdbusauthobserver.h"
 | 
					
						
							|  |  |  | #include "gcredentials.h"
 | 
					
						
							|  |  |  | #include "gioenumtypes.h"
 | 
					
						
							|  |  |  | #include "giostream.h"
 | 
					
						
							| 
									
										
										
										
											2011-04-08 15:14:47 -04:00
										 |  |  | #include "gdbusprivate.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 16:34:23 -04:00
										 |  |  | #include "glibintl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * SECTION:gdbusauthobserver | 
					
						
							|  |  |  |  * @short_description: Object used for authenticating connections | 
					
						
							| 
									
										
										
										
											2010-05-06 15:31:45 -04:00
										 |  |  |  * @include: gio/gio.h | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The #GDBusAuthObserver type provides a mechanism for participating | 
					
						
							|  |  |  |  * in how a #GDBusServer (or a #GDBusConnection) authenticates remote | 
					
						
							|  |  |  |  * peers. Simply instantiate a #GDBusAuthObserver and connect to the | 
					
						
							|  |  |  |  * signals you are interested in. Note that new signals may be added | 
					
						
							|  |  |  |  * in the future | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For example, if you only want to allow D-Bus connections from | 
					
						
							| 
									
										
										
										
											2010-07-20 15:02:36 -04:00
										 |  |  |  * processes owned by the same uid as the server, you would use a | 
					
						
							|  |  |  |  * signal handler like the following: | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * <example id="auth-observer"><title>Controlling Authentication</title><programlisting> | 
					
						
							|  |  |  |  * static gboolean | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |  * on_authorize_authenticated_peer (GDBusAuthObserver *observer, | 
					
						
							|  |  |  |  *                                  GIOStream         *stream, | 
					
						
							|  |  |  |  *                                  GCredentials      *credentials, | 
					
						
							|  |  |  |  *                                  gpointer           user_data) | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * { | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |  *   gboolean authorized; | 
					
						
							| 
									
										
										
										
											2010-05-12 13:01:40 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |  *   authorized = FALSE; | 
					
						
							| 
									
										
										
										
											2010-07-20 15:02:36 -04:00
										 |  |  |  *   if (credentials != NULL) | 
					
						
							|  |  |  |  *     { | 
					
						
							|  |  |  |  *       GCredentials *own_credentials; | 
					
						
							|  |  |  |  *       own_credentials = g_credentials_new (); | 
					
						
							|  |  |  |  *       if (g_credentials_is_same_user (credentials, own_credentials, NULL)) | 
					
						
							|  |  |  |  *         authorized = TRUE; | 
					
						
							|  |  |  |  *       g_object_unref (own_credentials); | 
					
						
							|  |  |  |  *     } | 
					
						
							| 
									
										
										
										
											2010-05-12 13:01:40 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |  *   return authorized; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * } | 
					
						
							|  |  |  |  * </programlisting></example> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 16:35:17 -04:00
										 |  |  | typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 15:57:37 -04:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * GDBusAuthObserverClass: | 
					
						
							|  |  |  |  * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class structure for #GDBusAuthObserverClass. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.26 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct _GDBusAuthObserverClass | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /*< private >*/ | 
					
						
							|  |  |  |   GObjectClass parent_class; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /*< public >*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Signals */ | 
					
						
							|  |  |  |   gboolean (*authorize_authenticated_peer) (GDBusAuthObserver  *observer, | 
					
						
							|  |  |  |                                             GIOStream          *stream, | 
					
						
							|  |  |  |                                             GCredentials       *credentials); | 
					
						
							| 
									
										
										
										
											2012-04-11 23:30:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   gboolean (*allow_mechanism) (GDBusAuthObserver  *observer, | 
					
						
							|  |  |  |                                const gchar        *mechanism); | 
					
						
							| 
									
										
										
										
											2010-07-07 15:57:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 16:35:17 -04:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * GDBusAuthObserver: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The #GDBusAuthObserver structure contains only private data and | 
					
						
							|  |  |  |  * should only be accessed using the provided API. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.26 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct _GDBusAuthObserver | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-07 16:35:17 -04:00
										 |  |  |   GObject parent_instance; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |   AUTHORIZE_AUTHENTICATED_PEER_SIGNAL, | 
					
						
							| 
									
										
										
										
											2012-04-11 23:30:48 -04:00
										 |  |  |   ALLOW_MECHANISM_SIGNAL, | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |   LAST_SIGNAL, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static guint signals[LAST_SIGNAL] = { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ---------------------------------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_dbus_auth_observer_finalize (GObject *object) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-12 13:01:40 -04:00
										 |  |  |   G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gboolean | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  | g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver  *observer, | 
					
						
							|  |  |  |                                                         GIOStream          *stream, | 
					
						
							|  |  |  |                                                         GCredentials       *credentials) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-11 23:30:48 -04:00
										 |  |  | static gboolean | 
					
						
							|  |  |  | g_dbus_auth_observer_allow_mechanism_real (GDBusAuthObserver  *observer, | 
					
						
							|  |  |  |                                            const gchar        *mechanism) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GObjectClass *gobject_class = G_OBJECT_CLASS (klass); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-12 13:01:40 -04:00
										 |  |  |   gobject_class->finalize = g_dbus_auth_observer_finalize; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |   klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real; | 
					
						
							| 
									
										
										
										
											2012-04-11 23:30:48 -04:00
										 |  |  |   klass->allow_mechanism = g_dbus_auth_observer_allow_mechanism_real; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |    * GDBusAuthObserver::authorize-authenticated-peer: | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |    * @observer: The #GDBusAuthObserver emitting the signal. | 
					
						
							|  |  |  |    * @stream: A #GIOStream for the #GDBusConnection. | 
					
						
							| 
									
										
										
										
											2012-03-24 23:58:45 +11:00
										 |  |  |    * @credentials: (allow-none): Credentials received from the peer or %NULL. | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |    * | 
					
						
							|  |  |  |    * Emitted to check if a peer that is successfully authenticated | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |    * is authorized. | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |    * Returns: %TRUE if the peer is authorized, %FALSE if not. | 
					
						
							| 
									
										
										
										
											2010-05-06 16:02:08 -04:00
										 |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.26 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |   signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] = | 
					
						
							|  |  |  |     g_signal_new ("authorize-authenticated-peer", | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |                   G_TYPE_DBUS_AUTH_OBSERVER, | 
					
						
							|  |  |  |                   G_SIGNAL_RUN_LAST, | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |                   G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer), | 
					
						
							|  |  |  |                   _g_signal_accumulator_false_handled, | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |                   NULL, /* accu_data */ | 
					
						
							| 
									
										
										
										
											2011-07-19 14:18:10 -03:00
										 |  |  |                   NULL, | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |                   G_TYPE_BOOLEAN, | 
					
						
							|  |  |  |                   2, | 
					
						
							|  |  |  |                   G_TYPE_IO_STREAM, | 
					
						
							|  |  |  |                   G_TYPE_CREDENTIALS); | 
					
						
							| 
									
										
										
										
											2012-04-11 23:30:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * GDBusAuthObserver::allow-mechanism: | 
					
						
							|  |  |  |    * @observer: The #GDBusAuthObserver emitting the signal. | 
					
						
							|  |  |  |    * @mechanism: The name of the mechanism, e.g. <literal>DBUS_COOKIE_SHA1</literal>. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Emitted to check if @mechanism is allowed to be used. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.34 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   signals[ALLOW_MECHANISM_SIGNAL] = | 
					
						
							|  |  |  |     g_signal_new ("allow-mechanism", | 
					
						
							|  |  |  |                   G_TYPE_DBUS_AUTH_OBSERVER, | 
					
						
							|  |  |  |                   G_SIGNAL_RUN_LAST, | 
					
						
							|  |  |  |                   G_STRUCT_OFFSET (GDBusAuthObserverClass, allow_mechanism), | 
					
						
							|  |  |  |                   _g_signal_accumulator_false_handled, | 
					
						
							|  |  |  |                   NULL, /* accu_data */ | 
					
						
							|  |  |  |                   NULL, | 
					
						
							|  |  |  |                   G_TYPE_BOOLEAN, | 
					
						
							|  |  |  |                   1, | 
					
						
							|  |  |  |                   G_TYPE_STRING); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_dbus_auth_observer_init (GDBusAuthObserver *observer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_dbus_auth_observer_new: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Creates a new #GDBusAuthObserver object. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns: A #GDBusAuthObserver. Free with g_object_unref(). | 
					
						
							| 
									
										
										
										
											2010-05-06 16:02:08 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.26 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | GDBusAuthObserver * | 
					
						
							|  |  |  | g_dbus_auth_observer_new (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ---------------------------------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |  * g_dbus_auth_observer_authorize_authenticated_peer: | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * @observer: A #GDBusAuthObserver. | 
					
						
							|  |  |  |  * @stream: A #GIOStream for the #GDBusConnection. | 
					
						
							| 
									
										
										
										
											2012-03-24 23:58:45 +11:00
										 |  |  |  * @credentials: (allow-none): Credentials received from the peer or %NULL. | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |  * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer. | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-07-20 15:02:36 -04:00
										 |  |  |  * Returns: %TRUE if the peer is authorized, %FALSE if not. | 
					
						
							| 
									
										
										
										
											2010-05-06 16:02:08 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.26 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | gboolean | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  | g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver  *observer, | 
					
						
							|  |  |  |                                                    GIOStream          *stream, | 
					
						
							|  |  |  |                                                    GCredentials       *credentials) | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | { | 
					
						
							|  |  |  |   gboolean denied; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   denied = FALSE; | 
					
						
							|  |  |  |   g_signal_emit (observer, | 
					
						
							| 
									
										
										
										
											2010-05-13 16:20:31 -04:00
										 |  |  |                  signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL], | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |                  0, | 
					
						
							|  |  |  |                  stream, | 
					
						
							|  |  |  |                  credentials, | 
					
						
							|  |  |  |                  &denied); | 
					
						
							|  |  |  |   return denied; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-04-11 23:30:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_dbus_auth_observer_allow_mechanism: | 
					
						
							|  |  |  |  * @observer: A #GDBusAuthObserver. | 
					
						
							|  |  |  |  * @mechanism: The name of the mechanism, e.g. <literal>DBUS_COOKIE_SHA1</literal>. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.34 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | gboolean | 
					
						
							|  |  |  | g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver  *observer, | 
					
						
							|  |  |  |                                       const gchar        *mechanism) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   gboolean ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ret = FALSE; | 
					
						
							|  |  |  |   g_signal_emit (observer, | 
					
						
							|  |  |  |                  signals[ALLOW_MECHANISM_SIGNAL], | 
					
						
							|  |  |  |                  0, | 
					
						
							|  |  |  |                  mechanism, | 
					
						
							|  |  |  |                  &ret); | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |