summaryrefslogtreecommitdiff
path: root/plugins/mcp-dbus-aegis-acl.c
blob: fc0daabb86cd8792a8ef120cbd0a704d7eb0a85c (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * A pseudo-plugin that checks the caller's Aegis permission tokens
 *
 * Copyright © 2010-2011 Nokia Corporation
 * Copyright © 2010-2011 Collabora Ltd.
 *
 * 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "mission-control-DBus-Access-ACL"

#define DEBUG(_f, ...) MCP_DEBUG (MCP_DEBUG_DBUS_ACL, _f, ##__VA_ARGS__)

#include <dbus/dbus-glib.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/util.h>
#include <telepathy-glib/defs.h>

#include <mission-control-plugins/mission-control-plugins.h>

#include <sys/types.h>
#include <sys/creds.h>

typedef struct _AegisAcl AegisAcl;
typedef struct _AegisAclClass AegisAclClass;

struct _AegisAcl {
  GObject parent;
};

struct _AegisAclClass {
  GObjectClass parent_class;
};

#define CREATE_CHANNEL TP_IFACE_CONNECTION_INTERFACE_REQUESTS ".CreateChannel"
#define ENSURE_CHANNEL TP_IFACE_CONNECTION_INTERFACE_REQUESTS ".EnsureChannel"
#define SEND_MESSAGE \
  TP_IFACE_CHANNEL_DISPATCHER ".Interface.Messages.DRAFT.SendMessage"

#define AEGIS_CALL_TOKEN "Cellular"

/* implemented by the Aegis-patched dbus-daemon */
#define AEGIS_INTERFACE "com.meego.DBus.Creds"

#define PLUGIN_NAME "dbus-aegis-acl"
#define PLUGIN_DESCRIPTION \
  "This plugin uses libcreds to check the aegis security tokens " \
  "associated with the calling process ID and determine whether " \
  "the DBus call or property access should be allowed"

static creds_value_t aegis_token = CREDS_BAD;
static creds_type_t aegis_type = CREDS_BAD;

static void aegis_acl_iface_init (McpDBusAclIface *,
    gpointer);
static void aegis_cdo_policy_iface_init (McpDispatchOperationPolicyIface *,
    gpointer);

static GType aegis_acl_get_type (void);

G_DEFINE_TYPE_WITH_CODE (AegisAcl, aegis_acl,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (MCP_TYPE_DBUS_ACL, aegis_acl_iface_init);
    G_IMPLEMENT_INTERFACE (MCP_TYPE_DISPATCH_OPERATION_POLICY,
      aegis_cdo_policy_iface_init))

static void
aegis_acl_init (AegisAcl *self)
{
}

static void
aegis_acl_class_init (AegisAclClass *cls)
{
  if (aegis_type != CREDS_BAD)
    return;

  aegis_type = creds_str2creds (AEGIS_CALL_TOKEN, &aegis_token);
}

static gchar *restricted_methods[] =
  {
    CREATE_CHANNEL,
    ENSURE_CHANNEL,
    SEND_MESSAGE,
    NULL
  };

static gboolean
method_is_filtered (const gchar *method)
{
  guint i;

  for (i = 0; restricted_methods[i] != NULL; i++)
    {
      if (!tp_strdiff (method, restricted_methods[i]))
        return TRUE;
    }

  return FALSE;
}

static gboolean
is_filtered (DBusAclType type,
    const gchar *name,
    const GHashTable *params)
{
  const GValue *account = NULL;
  const gchar *path = NULL;

  /* only  bothered with method calls */
  if (type != DBUS_ACL_TYPE_METHOD)
    return FALSE;

  /* only create/ensure channel concern us (and send message, now): */
  if (!method_is_filtered (name))
    return FALSE;

  /* must have at least the account-path to check */
  if (params == NULL)
    return FALSE;

  account = g_hash_table_lookup ((GHashTable *) params, "account-path");

  if (account == NULL)
    return FALSE;

  path = g_value_get_string (account);

  DEBUG ("should we check account %s?", path);
  /* account must belong to the ring or MMS connection manager: */
  if (g_str_has_prefix (path, TP_ACCOUNT_OBJECT_PATH_BASE "ring/") ||
      g_str_has_prefix (path, TP_ACCOUNT_OBJECT_PATH_BASE "mmscm/"))
    return TRUE;

  return FALSE;
}

/* For simplicity we don't implement non-trivial conversion between
 * dbus-glib's arrays of guint, and libcreds' arrays of uint32_t.
 * If this assertion fails on your platform, you'll need to implement it. */
G_STATIC_ASSERT (sizeof (guint) == sizeof (uint32_t));

static gboolean
caller_creds_are_enough (const gchar *name,
    const GArray *au)
{
  creds_t caller_creds = creds_import ((const uint32_t *) au->data, au->len);
  gboolean ok = creds_have_p (caller_creds, aegis_type, aegis_token);

#ifdef ENABLE_DEBUG
  if (ok)
    {
      DEBUG ("Caller %s is appropriately privileged", name);
    }
  else
    {
      char buf[1024];
      creds_type_t debug_type;
      creds_value_t debug_value;
      int i = 0;

      DEBUG ("Caller %s has these credentials:", name);

      while ((debug_type = creds_list (caller_creds, i++, &debug_value))
          != CREDS_BAD)
        {
          creds_creds2str (debug_type, debug_value, buf, sizeof (buf));
          DEBUG ("- %s", buf);
        }

      DEBUG ("but they are insufficient");
    }
#endif

  creds_free (caller_creds);
  return ok;
}

static gboolean
check_peer_creds_sync (DBusGConnection *dgc,
    const gchar *bus_name)
{
  DBusGProxy *proxy = dbus_g_proxy_new_for_name (dgc,
      DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS,
      AEGIS_INTERFACE);
  GArray *au = NULL;
  GError *error = NULL;
  gboolean ok;

  if (dbus_g_proxy_call (proxy, "GetConnectionCredentials", &error,
      G_TYPE_STRING, bus_name,
      G_TYPE_INVALID,
      DBUS_TYPE_G_UINT_ARRAY, &au,
      G_TYPE_INVALID))
    {
      ok = caller_creds_are_enough (bus_name, au);
      g_array_unref (au);
    }
  else
    {
      DEBUG ("GetConnectionCredentials failed: %s", error->message);
      g_clear_error (&error);
      ok = FALSE;
    }

  g_object_unref (proxy);
  return ok;
}

static gboolean
caller_authorised (const McpDBusAcl *self,
    const TpDBusDaemon *dbus,
    const DBusGMethodInvocation *call,
    DBusAclType type,
    const gchar *name,
    const GHashTable *params)
{
  DBusGConnection *dgc = tp_proxy_get_dbus_connection ((TpDBusDaemon *)dbus);
  gboolean ok = TRUE;

  if (is_filtered (type, name, params))
    {
      gchar *caller = dbus_g_method_get_sender ((DBusGMethodInvocation *) call);

      ok = check_peer_creds_sync (dgc, caller);

      g_free (caller);
    }

  DEBUG ("sync Aegis ACL check [%s]", ok ? "Allowed" : "Forbidden");

  return ok;
}

static void
async_authorised_cb (DBusGProxy *proxy,
    DBusGProxyCall *call,
    gpointer data)
{
  GError *error = NULL;
  DBusAclAuthData *ad = data;
  GArray *au = NULL;
  const McpDBusAcl *self = ad->acl;
  gboolean permitted = FALSE;

  /* if this returns FALSE, there are no credentials, which means something
   * untrustworthy is going on, which in turn means we must deny: can't
   * authorise without first authenticating */
  permitted = dbus_g_proxy_end_call (proxy, call, &error,
      DBUS_TYPE_G_UINT_ARRAY, &au,
      G_TYPE_INVALID);

  if (permitted)
    {
      permitted = caller_creds_are_enough (ad->name, au);
      g_array_unref (au);
    }
  else
    {
      DEBUG ("GetConnectionCredentials failed: %s", error->message);
      g_clear_error (&error);
    }

  DEBUG ("finished async Aegis ACL check [%s]",
      permitted ? "Allowed" : "Forbidden");

  mcp_dbus_acl_authorised_async_step (ad, permitted);

  g_object_unref (proxy);
}

static void
caller_async_authorised (const McpDBusAcl *self,
    DBusAclAuthData *data)
{
  DEBUG ("starting async caller-permission ACL check");

  if (is_filtered (data->type, data->name, data->params))
    {
      DBusGConnection *dgc;
      DBusGProxy *proxy;
      gchar *caller = dbus_g_method_get_sender (data->context);

      dgc = tp_proxy_get_dbus_connection (data->dbus);
      proxy = dbus_g_proxy_new_for_name (dgc,
          DBUS_SERVICE_DBUS,
          DBUS_PATH_DBUS,
          AEGIS_INTERFACE);

      dbus_g_proxy_begin_call (proxy, "GetConnectionCredentials",
          async_authorised_cb,
          data,
          NULL,
          G_TYPE_STRING, caller,
          G_TYPE_INVALID);

      g_free (caller);
    }
  else
    {
      mcp_dbus_acl_authorised_async_step (data, TRUE);
    }
}


static void
aegis_acl_iface_init (McpDBusAclIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  mcp_dbus_acl_iface_set_name (iface, PLUGIN_NAME);
  mcp_dbus_acl_iface_set_desc (iface, PLUGIN_DESCRIPTION);

  mcp_dbus_acl_iface_implement_authorised (iface, caller_authorised);
  mcp_dbus_acl_iface_implement_authorised_async (iface, caller_async_authorised);
}

static gchar *restricted_cms[] = { "ring", "mmscm", NULL };

static inline gboolean
cm_is_restricted (const gchar *cm_name)
{
  guint i;

  for (i = 0; restricted_cms[i] != NULL; i++)
    {
      if (!tp_strdiff (restricted_cms[i], cm_name))
        return TRUE;
    }

  return FALSE;
}

static void
handler_is_suitable_async (McpDispatchOperationPolicy *self,
    TpClient *recipient,
    const gchar *unique_name,
    McpDispatchOperation *dispatch_op,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  const gchar *manager = mcp_dispatch_operation_get_cm_name (dispatch_op);
  GSimpleAsyncResult *simple = g_simple_async_result_new ((GObject *) self,
      callback, user_data, handler_is_suitable_async);
  gboolean ok = TRUE;

  if (cm_is_restricted (manager))
    {
      TpDBusDaemon *dbus = tp_dbus_daemon_dup (NULL);

      /* if MC started successfully, we ought to have one */
      g_assert (dbus != NULL);

      if (!tp_str_empty (unique_name))
        {
          ok = check_peer_creds_sync (tp_proxy_get_dbus_connection (dbus),
              unique_name);
        }
      else
        {
          g_assert (recipient != NULL);

          ok = check_peer_creds_sync (tp_proxy_get_dbus_connection (dbus),
              tp_proxy_get_bus_name (recipient));
        }

      if (!ok)
        {
          g_simple_async_result_set_error (simple, TP_ERRORS,
              TP_ERROR_PERMISSION_DENIED, "insufficient Aegis credentials");
        }

      g_object_unref (dbus);
    }

  DEBUG ("sync Aegis CDO policy check [%s]", ok ? "Allowed" : "Forbidden");

  g_simple_async_result_complete_in_idle (simple);
  g_object_unref (simple);
}

static void
aegis_cdo_policy_iface_init (McpDispatchOperationPolicyIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  iface->handler_is_suitable_async = handler_is_suitable_async;
  /* the default finish function accepts our GSimpleAsyncResult */
}

GObject *
aegis_acl_new (void)
{
  return g_object_new (aegis_acl_get_type (), NULL);
}