summaryrefslogtreecommitdiff
path: root/test/manual-authz.c
blob: f9e3688e1218b31d0c08ab523b9a7ae59d31a61d (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
/* Simple sanity-check for authentication and authorization.
 *
 * Copyright © 2010-2011 Nokia Corporation
 * Copyright © 2012 Collabora Ltd.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <config.h>

#include <glib.h>

#include <dbus/dbus.h>

#ifdef G_OS_UNIX
#include <unistd.h>
#include <sys/types.h>
#endif

#include "test-utils.h"

typedef struct {
    DBusError e;
    TestMainContext *ctx;

    DBusServer *normal_server;
    DBusServer *anon_allowed_server;
    DBusServer *anon_only_server;
    DBusServer *anon_mech_only_server;
    DBusServer *anon_disallowed_server;
    DBusServer *permissive_server;
    DBusServer *unhappy_server;
    DBusServer *same_uid_server;
    DBusServer *same_uid_or_anon_server;
} Fixture;

static void oom (void) G_GNUC_NORETURN;
static void
oom (void)
{
  g_error ("out of memory");
}

static void
assert_no_error (const DBusError *e)
{
  if (G_UNLIKELY (dbus_error_is_set (e)))
    g_error ("expected success but got error: %s: %s", e->name, e->message);
}

static DBusHandlerResult
server_message_cb (DBusConnection *conn,
    DBusMessage *message,
    void *data)
{
  if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected"))
    {
      dbus_connection_unref (conn);

      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
    {
      DBusMessage *reply = dbus_message_new_method_return (message);
      const char *hello = "Hello, world!";
      unsigned long uid;
      char *sid;

      if (dbus_connection_get_unix_user (conn, &uid))
        {
          g_message ("message from uid %lu", uid);
        }
      else if (dbus_connection_get_windows_user (conn, &sid))
        {
          if (sid == NULL)
            oom ();

          g_message ("message from sid \"%s\"", sid);
          dbus_free (sid);
        }
      else if (dbus_connection_get_is_anonymous (conn))
        {
          g_message ("message from Anonymous");
        }
      else
        {
          g_message ("message from ... someone?");
        }

      if (reply == NULL)
        oom ();

      if (!dbus_message_append_args (reply,
            DBUS_TYPE_STRING, &hello,
            DBUS_TYPE_INVALID))
        oom ();

      if (!dbus_connection_send (conn, reply, NULL))
        oom ();

      dbus_message_unref (reply);

      return DBUS_HANDLER_RESULT_HANDLED;
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static dbus_bool_t
permissive_unix_func (DBusConnection *conn,
    unsigned long uid,
    void *data)
{
  g_message ("accepting Unix user %lu", uid);
  return TRUE;
}

static dbus_bool_t
permissive_win_func (DBusConnection *conn,
    const char *sid,
    void *data)
{
  g_message ("accepting Windows user \"%s\"", sid);
  return TRUE;
}

static dbus_bool_t
broken_unix_func (DBusConnection *conn,
    unsigned long uid,
    void *data)
{
  g_error ("libdbus called the Unix user function for an ANONYMOUS-only "
      "connection");
  return FALSE;
}

static dbus_bool_t
broken_win_func (DBusConnection *conn,
    const char *sid,
    void *data)
{
  g_error ("libdbus called the Windows user function for an ANONYMOUS-only "
      "connection");
  return FALSE;
}

static dbus_bool_t
unhappy_unix_func (DBusConnection *conn,
    unsigned long uid,
    void *data)
{
  g_message ("rejecting Unix user %lu", uid);
  return FALSE;
}

static dbus_bool_t
unhappy_win_func (DBusConnection *conn,
    const char *sid,
    void *data)
{
  g_message ("rejecting Windows user \"%s\"", sid);
  return FALSE;
}

static dbus_bool_t
same_uid_unix_func (DBusConnection *conn,
    unsigned long uid,
    void *data)
{
  g_message ("checking whether Unix user %lu owns this process", uid);
  /* I'd use _dbus_unix_user_is_process_owner(), but it's private... */
#ifdef G_OS_UNIX
  return (geteuid () == uid);
#else
  return FALSE;
#endif
}

static dbus_bool_t
same_uid_win_func (DBusConnection *conn,
    const char *sid,
    void *data)
{
  g_message ("checking whether Windows user \"%s\" owns this process", sid);
  g_message ("Stub implementation consistent with dbus-sysdeps-util-win: "
      "assume they do");
  return TRUE;
}

static void
new_conn_cb (DBusServer *server,
    DBusConnection *conn,
    void *data)
{
  Fixture *f = data;

  dbus_connection_ref (conn);
  test_connection_setup (f->ctx, conn);

  if (!dbus_connection_add_filter (conn, server_message_cb, f, NULL))
    oom ();

  if (server == f->normal_server)
    {
    }
  else if (server == f->anon_allowed_server)
    {
      dbus_connection_set_allow_anonymous (conn, TRUE);
    }
  else if (server == f->anon_only_server)
    {
      dbus_connection_set_allow_anonymous (conn, TRUE);

      dbus_connection_set_unix_user_function (conn, unhappy_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, unhappy_win_func,
          f, NULL);
    }
  else if (server == f->anon_mech_only_server)
    {
      dbus_connection_set_allow_anonymous (conn, TRUE);

      /* should never get called */
      dbus_connection_set_unix_user_function (conn, broken_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, broken_win_func,
          f, NULL);
    }
  else if (server == f->anon_disallowed_server)
    {
      dbus_connection_set_allow_anonymous (conn, FALSE);

      /* should never get called */
      dbus_connection_set_unix_user_function (conn, broken_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, broken_win_func,
          f, NULL);
    }
  else if (server == f->permissive_server)
    {
      dbus_connection_set_unix_user_function (conn, permissive_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, permissive_win_func,
          f, NULL);
    }
  else if (server == f->unhappy_server)
    {
      dbus_connection_set_unix_user_function (conn, unhappy_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, unhappy_win_func,
          f, NULL);
    }
  else if (server == f->same_uid_server)
    {
      dbus_connection_set_unix_user_function (conn, same_uid_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, same_uid_win_func,
          f, NULL);
    }
  else if (server == f->same_uid_or_anon_server)
    {
      dbus_connection_set_allow_anonymous (conn, TRUE);

      dbus_connection_set_unix_user_function (conn, same_uid_unix_func,
          f, NULL);
      dbus_connection_set_windows_user_function (conn, same_uid_win_func,
          f, NULL);
    }
  else
    {
      g_assert_not_reached ();
    }
}

static void
setup (Fixture *f,
    const gchar *listen_addr)
{
  const char *only_anon[] = { "ANONYMOUS", NULL };
  char *connect_addr;

  f->normal_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->normal_server != NULL);
  dbus_server_set_new_connection_function (f->normal_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->normal_server);
  connect_addr = dbus_server_get_address (f->normal_server);
  g_message ("Normal server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->anon_allowed_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->anon_allowed_server != NULL);
  dbus_server_set_new_connection_function (f->anon_allowed_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->anon_allowed_server);
  connect_addr = dbus_server_get_address (f->anon_allowed_server);
  g_message ("Anonymous-allowed server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->anon_only_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->anon_only_server != NULL);
  dbus_server_set_new_connection_function (f->anon_only_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->anon_only_server);
  connect_addr = dbus_server_get_address (f->anon_only_server);
  g_message ("Anonymous-only server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->anon_mech_only_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->anon_mech_only_server != NULL);
  dbus_server_set_auth_mechanisms (f->anon_mech_only_server, only_anon);
  dbus_server_set_new_connection_function (f->anon_mech_only_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->anon_mech_only_server);
  connect_addr = dbus_server_get_address (f->anon_mech_only_server);
  g_message ("Anon mech only server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->anon_disallowed_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->anon_disallowed_server != NULL);
  dbus_server_set_auth_mechanisms (f->anon_disallowed_server, only_anon);
  dbus_server_set_new_connection_function (f->anon_disallowed_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->anon_disallowed_server);
  connect_addr = dbus_server_get_address (f->anon_disallowed_server);
  g_message ("Anonymous-disallowed server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->permissive_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->permissive_server != NULL);
  dbus_server_set_new_connection_function (f->permissive_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->permissive_server);
  connect_addr = dbus_server_get_address (f->permissive_server);
  g_message ("Permissive server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->unhappy_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->unhappy_server != NULL);
  dbus_server_set_new_connection_function (f->unhappy_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->unhappy_server);
  connect_addr = dbus_server_get_address (f->unhappy_server);
  g_message ("Unhappy server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->same_uid_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->same_uid_server != NULL);
  dbus_server_set_new_connection_function (f->same_uid_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->same_uid_server);
  connect_addr = dbus_server_get_address (f->same_uid_server);
  g_message ("Same-UID server:\n%s", connect_addr);
  dbus_free (connect_addr);

  f->same_uid_or_anon_server = dbus_server_listen (listen_addr, &f->e);
  assert_no_error (&f->e);
  g_assert (f->same_uid_or_anon_server != NULL);
  dbus_server_set_new_connection_function (f->same_uid_or_anon_server,
      new_conn_cb, f, NULL);
  test_server_setup (f->ctx, f->same_uid_or_anon_server);
  connect_addr = dbus_server_get_address (f->same_uid_or_anon_server);
  g_message ("Same-UID-or-anon server:\n%s", connect_addr);
  dbus_free (connect_addr);
}

int
main (int argc,
    char **argv)
{
  Fixture f = { DBUS_ERROR_INIT, test_main_context_get () };

  if (argc >= 2)
    setup (&f, argv[1]);
  else
    setup (&f, "tcp:host=127.0.0.1");

  for (;;)
    test_main_context_iterate (f.ctx, TRUE);

  /* never returns */
}