summaryrefslogtreecommitdiff
path: root/gdbus/gdbusauthobserver.c
blob: f64a13f5b2d9145b7afaf68266e776d762f6425f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* GDBus - GLib D-Bus Library
 *
 * Copyright (C) 2008-2009 Red Hat, Inc.
 *
 * 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 <glib/gi18n.h>

#include "gdbusauthobserver.h"
#include "gdbus-marshal.h"
#include "gcredentials.h"
#include "gdbusenumtypes.h"

/**
 * SECTION:gdbusauthobserver
 * @short_description: Object used for authenticating connections
 * @include: gdbus/gdbus.h
 *
 * 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
 * processes owned by the same uid as the server, you would do this:
 * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
 * static gboolean
 * on_deny_authenticated_peer (GDBusAuthObserver *observer,
 *                             GIOStream         *stream,
 *                             GCredentials      *credentials,
 *                             gpointer           user_data)
 * {
 *   gboolean deny;
 *   deny = TRUE;
 *   if (credentials != NULL &&
 *       g_credentials_has_unix_user (credentials) &&
 *       g_credentials_get_unix_user (credentials) == getuid ())
 *     deny = FALSE;
 *   return deny;
 * }
 *
 * static gboolean
 * on_new_connection (GDBusServer     *server,
 *                    GDBusConnection *connection,
 *                    gpointer         user_data)
 * {
 *   /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
 * }
 *
 * [...]
 *
 * GDBusAuthObserver *observer;
 * GDBusServer *server;
 * GError *error;
 *
 * error = NULL;
 * observer = g_dbus_auth_observer_new ();
 * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
 *                                  G_DBUS_SERVER_FLAGS_NONE,
 *                                  observer,
 *                                  NULL, /<!-- -->* GCancellable *<!-- -->/
 *                                  &error);
 * g_signal_connect (observer,
 *                   "deny-authenticated-peer",
 *                   G_CALLBACK (on_deny_authenticated_peer),
 *                   NULL);
 * g_signal_connect (server,
 *                   "new-connection",
 *                   G_CALLBACK (on_new_connection),
 *                   NULL);
 * g_object_unref (observer);
 * g_dbus_server_start (server);
 * </programlisting></example>
 */

struct _GDBusAuthObserverPrivate
{
  gint foo;
};

enum
{
  DENY_AUTHENTICATED_PEER_SIGNAL,
  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)
{
  //GDBusAuthObserver *observer = G_DBUS_AUTH_OBSERVER (object);

  if (G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize != NULL)
    G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
}

static gboolean
g_dbus_auth_observer_deny_authenticated_peer_real (GDBusAuthObserver  *observer,
                                                   GIOStream          *stream,
                                                   GCredentials       *credentials)
{
  return FALSE;
}

static void
g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize     = g_dbus_auth_observer_finalize;

  klass->deny_authenticated_peer = g_dbus_auth_observer_deny_authenticated_peer_real;

  /**
   * GDBusAuthObserver::deny-authenticated-peer:
   * @stream: A #GIOStream for the #GDBusConnection.
   * @credentials: Credentials received from the peer or %NULL.
   *
   * Emitted to check if a peer that is successfully authenticated
   * should be denied.
   *
   * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
   */
  signals[DENY_AUTHENTICATED_PEER_SIGNAL] =
    g_signal_new ("deny-authenticated-peer",
                  G_TYPE_DBUS_AUTH_OBSERVER,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GDBusAuthObserverClass, deny_authenticated_peer),
                  g_signal_accumulator_true_handled,
                  NULL, /* accu_data */
                  _gdbus_marshal_BOOLEAN__OBJECT_OBJECT,
                  G_TYPE_BOOLEAN,
                  2,
                  G_TYPE_IO_STREAM,
                  G_TYPE_CREDENTIALS);


  g_type_class_add_private (klass, sizeof (GDBusAuthObserverPrivate));
}

static void
g_dbus_auth_observer_init (GDBusAuthObserver *observer)
{
  /* not used for now */
  observer->priv = G_TYPE_INSTANCE_GET_PRIVATE (observer,
                                                G_TYPE_DBUS_AUTH_OBSERVER,
                                                GDBusAuthObserverPrivate);;
}

/**
 * g_dbus_auth_observer_new:
 *
 * Creates a new #GDBusAuthObserver object.
 *
 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
 */
GDBusAuthObserver *
g_dbus_auth_observer_new (void)
{
  return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_auth_observer_deny_authenticated_peer:
 * @observer: A #GDBusAuthObserver.
 * @stream: A #GIOStream for the #GDBusConnection.
 * @credentials: Credentials received from the peer or %NULL.
 *
 * Emits the #GDBusAuthObserver::deny-authenticated-peer signal on @observer.
 *
 * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
 */
gboolean
g_dbus_auth_observer_deny_authenticated_peer (GDBusAuthObserver  *observer,
                                              GIOStream          *stream,
                                              GCredentials       *credentials)
{
  gboolean denied;

  denied = FALSE;
  g_signal_emit (observer,
                 signals[DENY_AUTHENTICATED_PEER_SIGNAL],
                 0,
                 stream,
                 credentials,
                 &denied);
  return denied;
}