summaryrefslogtreecommitdiff
path: root/tests/twisted/mcp-plugin.c
blob: 53aa1731aea4a38c54525f0b5124957391a5ec89 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * A demonstration plugin that acts as a channel filter.
 *
 * Copyright © 2008-2009 Nokia Corporation
 * Copyright © 2009-2010 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 <mission-control-plugins/mission-control-plugins.h>

#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>

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

#define DEBUG g_debug

/* ------ TestPermissionPlugin -------------------------------------- */

typedef struct {
    GObject parent;
} TestPermissionPlugin;

typedef struct {
    GObjectClass parent_class;
} TestPermissionPluginClass;

GType test_permission_plugin_get_type (void) G_GNUC_CONST;
static void cdo_policy_iface_init (McpDispatchOperationPolicyIface *,
    gpointer);
static void req_policy_iface_init (McpRequestPolicyIface *, gpointer);

G_DEFINE_TYPE_WITH_CODE (TestPermissionPlugin, test_permission_plugin,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (MCP_TYPE_REQUEST_POLICY,
      req_policy_iface_init);
    G_IMPLEMENT_INTERFACE (MCP_TYPE_DISPATCH_OPERATION_POLICY,
      cdo_policy_iface_init))

static void
test_permission_plugin_init (TestPermissionPlugin *self)
{
}

static void
test_permission_plugin_class_init (TestPermissionPluginClass *cls)
{
}

typedef struct {
    McpDispatchOperation *dispatch_operation;
    McpDispatchOperationDelay *delay;
} PermissionContext;

static void
permission_context_free (gpointer p)
{
  PermissionContext *ctx = p;

  mcp_dispatch_operation_end_delay (ctx->dispatch_operation, ctx->delay);
  g_object_unref (ctx->dispatch_operation);
  g_slice_free (PermissionContext, ctx);
}

static void
permission_cb (DBusPendingCall *pc,
    gpointer data)
{
  PermissionContext *ctx = data;
  DBusMessage *message = dbus_pending_call_steal_reply (pc);

  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
    {
      DEBUG ("Permission denied");
      mcp_dispatch_operation_leave_channels (ctx->dispatch_operation,
          TRUE, TP_CHANNEL_GROUP_CHANGE_REASON_PERMISSION_DENIED,
          "Computer says no");
    }
  else
    {
      DEBUG ("Permission granted");
    }

  dbus_message_unref (message);
  dbus_pending_call_unref (pc);
}

static void
test_permission_plugin_check_cdo (McpDispatchOperationPolicy *policy,
    McpDispatchOperation *dispatch_operation)
{
  GHashTable *properties = mcp_dispatch_operation_ref_nth_channel_properties (
      dispatch_operation, 0);
  PermissionContext *ctx = NULL;

  DEBUG ("enter");

  if (properties == NULL)
    {
      DEBUG ("no channels!?");
      return;
    }

  /* currently this example just checks the first channel */

  if (!tp_strdiff (tp_asv_get_string (properties,
          TP_IFACE_CHANNEL ".TargetID"),
        "policy@example.net"))
    {
      TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
      DBusGConnection *gconn = tp_proxy_get_dbus_connection (dbus_daemon);
      DBusConnection *libdbus = dbus_g_connection_get_connection (gconn);
      DBusPendingCall *pc = NULL;
      DBusMessage *message;

      ctx = g_slice_new0 (PermissionContext);
      ctx->dispatch_operation = g_object_ref (dispatch_operation);
      ctx->delay = mcp_dispatch_operation_start_delay (dispatch_operation);

      /* in a real policy-mechanism you'd give some details, like the
       * channel's properties or object path */
      message = dbus_message_new_method_call ("com.example.Policy",
          "/com/example/Policy", "com.example.Policy", "RequestPermission");

      if (!dbus_connection_send_with_reply (libdbus, message,
            &pc, -1))
        {
          g_error ("out of memory");
        }

      dbus_message_unref (message);

      if (pc == NULL)
        {
          DEBUG ("got disconnected from D-Bus...");

          goto finally;
        }

      /* pc is unreffed by permission_cb */

      DEBUG ("Waiting for permission");

      if (dbus_pending_call_get_completed (pc))
        {
          permission_cb (pc, ctx);
          goto finally;
        }

      if (!dbus_pending_call_set_notify (pc, permission_cb, ctx,
            permission_context_free))
        {
          g_error ("Out of memory");
        }

      /* ctx will be freed later */
      ctx = NULL;
  }

finally:
  if (ctx != NULL)
    permission_context_free (ctx);

  g_hash_table_unref (properties);
}

static void
cdo_policy_iface_init (McpDispatchOperationPolicyIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  mcp_dispatch_operation_policy_iface_implement_check (iface,
      test_permission_plugin_check_cdo);
}

typedef struct {
    McpRequest *request;
    McpRequestDelay *delay;
} RequestPermissionContext;

static void
request_permission_context_free (gpointer p)
{
  RequestPermissionContext *ctx = p;

  mcp_request_end_delay (ctx->request, ctx->delay);
  g_object_unref (ctx->request);
  g_slice_free (RequestPermissionContext, ctx);
}

static void
request_permission_cb (DBusPendingCall *pc,
    gpointer data)
{
  RequestPermissionContext *ctx = data;
  DBusMessage *message = dbus_pending_call_steal_reply (pc);

  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
    {
      DEBUG ("Permission denied");
      mcp_request_deny (ctx->request,
          TP_ERRORS, TP_CHANNEL_GROUP_CHANGE_REASON_PERMISSION_DENIED,
          "Computer says no");
    }
  else
    {
      DEBUG ("Permission granted");
    }

  dbus_message_unref (message);
  dbus_pending_call_unref (pc);
}

static void
test_permission_plugin_check_request (McpRequestPolicy *policy,
    McpRequest *request)
{
  GHashTable *properties = mcp_request_ref_nth_request (request, 0);
  RequestPermissionContext *ctx = NULL;

  DEBUG ("%s", G_STRFUNC);

  if (mcp_request_find_request_by_type (request,
        0, g_quark_from_static_string ("com.example.QuestionableChannel"),
        NULL, NULL))
    {
      TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
      DBusGConnection *gconn = tp_proxy_get_dbus_connection (dbus_daemon);
      DBusConnection *libdbus = dbus_g_connection_get_connection (gconn);
      DBusPendingCall *pc = NULL;
      DBusMessage *message;

      DEBUG ("Questionable channel detected, asking for permission");

      ctx = g_slice_new0 (RequestPermissionContext);
      ctx->request = g_object_ref (request);
      ctx->delay = mcp_request_start_delay (request);

      /* in a real policy-mechanism you'd give some details, like the
       * channel's properties or object path */
      message = dbus_message_new_method_call ("com.example.Policy",
          "/com/example/Policy", "com.example.Policy", "RequestRequest");

      if (!dbus_connection_send_with_reply (libdbus, message,
            &pc, -1))
        {
          g_error ("out of memory");
        }

      dbus_message_unref (message);

      if (pc == NULL)
        {
          DEBUG ("got disconnected from D-Bus...");

          goto finally;
        }

      /* pc is unreffed by permission_cb */

      DEBUG ("Waiting for permission");

      if (dbus_pending_call_get_completed (pc))
        {
          request_permission_cb (pc, ctx);
          goto finally;
        }

      if (!dbus_pending_call_set_notify (pc, request_permission_cb, ctx,
            request_permission_context_free))
        {
          g_error ("Out of memory");
        }

      /* ctx will be freed later */
      ctx = NULL;
    }

finally:
  if (ctx != NULL)
    request_permission_context_free (ctx);

  g_hash_table_unref (properties);
}

static void
req_policy_iface_init (McpRequestPolicyIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  mcp_request_policy_iface_implement_check (iface,
      test_permission_plugin_check_request);
}

/* ------ TestRejectionPlugin --------------------------------------- */

typedef struct {
    GObject parent;
} TestRejectionPlugin;

typedef struct {
    GObjectClass parent_class;
} TestRejectionPluginClass;

GType test_rejection_plugin_get_type (void) G_GNUC_CONST;
static void rej_cdo_policy_iface_init (McpDispatchOperationPolicyIface *,
    gpointer);
static void rej_req_policy_iface_init (McpRequestPolicyIface *,
    gpointer);

G_DEFINE_TYPE_WITH_CODE (TestRejectionPlugin, test_rejection_plugin,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (MCP_TYPE_REQUEST_POLICY,
      rej_req_policy_iface_init);
    G_IMPLEMENT_INTERFACE (MCP_TYPE_DISPATCH_OPERATION_POLICY,
      rej_cdo_policy_iface_init))

static void
test_rejection_plugin_init (TestRejectionPlugin *self)
{
}

static void
test_rejection_plugin_class_init (TestRejectionPluginClass *cls)
{
}

static void
test_rejection_plugin_check_cdo (McpDispatchOperationPolicy *policy,
    McpDispatchOperation *dispatch_operation)
{
  GHashTable *properties = mcp_dispatch_operation_ref_nth_channel_properties (
      dispatch_operation, 0);
  const gchar *target_id;

  DEBUG ("enter");

  if (properties == NULL)
    {
      DEBUG ("no channels!?");
      return;
    }

  /* currently this example just checks the first channel */

  target_id = tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID");

  if (!tp_strdiff (target_id, "rick.astley@example.net"))
    {
      DEBUG ("rickrolling detected, destroying channels immediately!");
      mcp_dispatch_operation_destroy_channels (dispatch_operation, FALSE);
    }
  else if (!tp_strdiff (target_id, "mc.hammer@example.net"))
    {
      DEBUG ("MC Hammer detected, leaving channels when observers have run");
      mcp_dispatch_operation_leave_channels (dispatch_operation, TRUE,
          TP_CHANNEL_GROUP_CHANGE_REASON_PERMISSION_DENIED,
          "Can't touch this");
    }

  g_hash_table_unref (properties);
}

static void
rej_cdo_policy_iface_init (McpDispatchOperationPolicyIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  mcp_dispatch_operation_policy_iface_implement_check (iface,
      test_rejection_plugin_check_cdo);
}

static void
test_rejection_plugin_check_request (McpRequestPolicy *policy,
    McpRequest *request)
{
  GHashTable *properties = mcp_request_ref_nth_request (request, 0);

  DEBUG ("%s", G_STRFUNC);

  if (!tp_strdiff (
        tp_asv_get_string (properties, TP_IFACE_CHANNEL ".ChannelType"),
        "com.example.ForbiddenChannel"))
    {
      DEBUG ("Forbidden channel detected, denying request");
      mcp_request_deny (request, TP_ERRORS, TP_ERROR_PERMISSION_DENIED,
          "No, you don't");
    }

  if (mcp_request_find_request_by_type (request,
        0, g_quark_from_static_string ("com.example.ForbiddenChannel"),
        NULL, NULL))
    {
      DEBUG ("Forbidden channel detected, denying request");
      mcp_request_deny (request, TP_ERRORS, TP_ERROR_PERMISSION_DENIED,
          "No, you don't");
    }

  g_hash_table_unref (properties);
}

static void
rej_req_policy_iface_init (McpRequestPolicyIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  mcp_request_policy_iface_implement_check (iface,
      test_rejection_plugin_check_request);
}

/* ------ Initialization -------------------------------------------- */

GObject *
mcp_plugin_ref_nth_object (guint n)
{
  DEBUG ("Initializing mcp-plugin (n=%u)", n);

  switch (n)
    {
    case 0:
      return g_object_new (test_permission_plugin_get_type (),
          NULL);

    case 1:
      return g_object_new (test_rejection_plugin_get_type (),
          NULL);

    default:
      return NULL;
    }
}