Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gdbusauth.c
Line
Count
Source (jump to first uncovered line)
1
/* GDBus - GLib D-Bus Library
2
 *
3
 * Copyright (C) 2008-2010 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: David Zeuthen <davidz@redhat.com>
21
 */
22
23
#include "config.h"
24
25
#include "gdbusauth.h"
26
27
#include "gdbusauthmechanismanon.h"
28
#include "gdbusauthmechanismexternal.h"
29
#include "gdbusauthmechanismsha1.h"
30
#include "gdbusauthobserver.h"
31
32
#include "gdbuserror.h"
33
#include "gdbusutils.h"
34
#include "gioenumtypes.h"
35
#include "gcredentials.h"
36
#include "gcredentialsprivate.h"
37
#include "gdbusprivate.h"
38
#include "giostream.h"
39
#include "gdatainputstream.h"
40
#include "gdataoutputstream.h"
41
42
#include "gnetworking.h"
43
#include "gunixconnection.h"
44
#include "gunixcredentialsmessage.h"
45
46
#include "glibintl.h"
47
48
G_GNUC_PRINTF(1, 2)
49
static void
50
debug_print (const gchar *message, ...)
51
0
{
52
0
  if (G_UNLIKELY (_g_dbus_debug_authentication ()))
53
0
    {
54
0
      gchar *s;
55
0
      GString *str;
56
0
      va_list var_args;
57
0
      guint n;
58
59
0
      _g_dbus_debug_print_lock ();
60
61
0
      va_start (var_args, message);
62
0
      s = g_strdup_vprintf (message, var_args);
63
0
      va_end (var_args);
64
65
0
      str = g_string_new (NULL);
66
0
      for (n = 0; s[n] != '\0'; n++)
67
0
        {
68
0
          if (G_UNLIKELY (s[n] == '\r'))
69
0
            g_string_append (str, "\\r");
70
0
          else if (G_UNLIKELY (s[n] == '\n'))
71
0
            g_string_append (str, "\\n");
72
0
          else
73
0
            g_string_append_c (str, s[n]);
74
0
        }
75
0
      g_print ("GDBus-debug:Auth: %s\n", str->str);
76
0
      g_string_free (str, TRUE);
77
0
      g_free (s);
78
79
0
      _g_dbus_debug_print_unlock ();
80
0
    }
81
0
}
82
83
typedef struct
84
{
85
  const gchar *name;
86
  gint priority;
87
  GType gtype;
88
} Mechanism;
89
90
static void mechanism_free (Mechanism *m);
91
92
struct _GDBusAuthPrivate
93
{
94
  GIOStream *stream;
95
96
  /* A list of available Mechanism, sorted according to priority  */
97
  GList *available_mechanisms;
98
};
99
100
enum
101
{
102
  PROP_0,
103
  PROP_STREAM
104
};
105
106
G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuth, _g_dbus_auth, G_TYPE_OBJECT)
107
108
/* ---------------------------------------------------------------------------------------------------- */
109
110
static void
111
_g_dbus_auth_finalize (GObject *object)
112
0
{
113
0
  GDBusAuth *auth = G_DBUS_AUTH (object);
114
115
0
  if (auth->priv->stream != NULL)
116
0
    g_object_unref (auth->priv->stream);
117
0
  g_list_free_full (auth->priv->available_mechanisms, (GDestroyNotify) mechanism_free);
118
119
0
  if (G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize != NULL)
120
0
    G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize (object);
121
0
}
122
123
static void
124
_g_dbus_auth_get_property (GObject    *object,
125
                           guint       prop_id,
126
                           GValue     *value,
127
                           GParamSpec *pspec)
128
0
{
129
0
  GDBusAuth *auth = G_DBUS_AUTH (object);
130
131
0
  switch (prop_id)
132
0
    {
133
0
    case PROP_STREAM:
134
0
      g_value_set_object (value, auth->priv->stream);
135
0
      break;
136
137
0
    default:
138
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139
0
      break;
140
0
    }
141
0
}
142
143
static void
144
_g_dbus_auth_set_property (GObject      *object,
145
                           guint         prop_id,
146
                           const GValue *value,
147
                           GParamSpec   *pspec)
148
0
{
149
0
  GDBusAuth *auth = G_DBUS_AUTH (object);
150
151
0
  switch (prop_id)
152
0
    {
153
0
    case PROP_STREAM:
154
0
      auth->priv->stream = g_value_dup_object (value);
155
0
      break;
156
157
0
    default:
158
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159
0
      break;
160
0
    }
161
0
}
162
163
static void
164
_g_dbus_auth_class_init (GDBusAuthClass *klass)
165
0
{
166
0
  GObjectClass *gobject_class;
167
168
0
  gobject_class = G_OBJECT_CLASS (klass);
169
0
  gobject_class->get_property = _g_dbus_auth_get_property;
170
0
  gobject_class->set_property = _g_dbus_auth_set_property;
171
0
  gobject_class->finalize     = _g_dbus_auth_finalize;
172
173
0
  g_object_class_install_property (gobject_class,
174
0
                                   PROP_STREAM,
175
0
                                   g_param_spec_object ("stream", NULL, NULL,
176
0
                                                        G_TYPE_IO_STREAM,
177
0
                                                        G_PARAM_READABLE |
178
0
                                                        G_PARAM_WRITABLE |
179
0
                                                        G_PARAM_CONSTRUCT_ONLY |
180
0
                                                        G_PARAM_STATIC_NAME |
181
0
                                                        G_PARAM_STATIC_BLURB |
182
0
                                                        G_PARAM_STATIC_NICK));
183
0
}
184
185
static void
186
mechanism_free (Mechanism *m)
187
0
{
188
0
  g_free (m);
189
0
}
190
191
static void
192
add_mechanism (GDBusAuth         *auth,
193
               GDBusAuthObserver *observer,
194
               GType              mechanism_type)
195
0
{
196
0
  const gchar *name;
197
198
0
  name = _g_dbus_auth_mechanism_get_name (mechanism_type);
199
0
  if (observer == NULL || g_dbus_auth_observer_allow_mechanism (observer, name))
200
0
    {
201
0
      Mechanism *m;
202
0
      m = g_new0 (Mechanism, 1);
203
0
      m->name = name;
204
0
      m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
205
0
      m->gtype = mechanism_type;
206
0
      auth->priv->available_mechanisms = g_list_prepend (auth->priv->available_mechanisms, m);
207
0
    }
208
0
}
209
210
static gint
211
mech_compare_func (Mechanism *a, Mechanism *b)
212
0
{
213
0
  gint ret;
214
  /* ensure deterministic order */
215
0
  ret = b->priority - a->priority;
216
0
  if (ret == 0)
217
0
    ret = g_strcmp0 (b->name, a->name);
218
0
  return ret;
219
0
}
220
221
static void
222
_g_dbus_auth_init (GDBusAuth *auth)
223
0
{
224
0
  auth->priv = _g_dbus_auth_get_instance_private (auth);
225
0
}
226
227
static void
228
_g_dbus_auth_add_mechs (GDBusAuth         *auth,
229
                        GDBusAuthObserver *observer)
230
0
{
231
  /* TODO: trawl extension points */
232
0
  add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
233
0
  add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
234
0
  add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
235
236
0
  auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
237
0
                                                  (GCompareFunc) mech_compare_func);
238
0
}
239
240
static GType
241
find_mech_by_name (GDBusAuth *auth,
242
                   const gchar *name)
243
0
{
244
0
  GType ret;
245
0
  GList *l;
246
247
0
  ret = (GType) 0;
248
249
0
  for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
250
0
    {
251
0
      Mechanism *m = l->data;
252
0
      if (g_strcmp0 (name, m->name) == 0)
253
0
        {
254
0
          ret = m->gtype;
255
0
          goto out;
256
0
        }
257
0
    }
258
259
0
 out:
260
0
  return ret;
261
0
}
262
263
GDBusAuth  *
264
_g_dbus_auth_new (GIOStream *stream)
265
0
{
266
0
  return g_object_new (G_TYPE_DBUS_AUTH,
267
0
                       "stream", stream,
268
0
                       NULL);
269
0
}
270
271
/* ---------------------------------------------------------------------------------------------------- */
272
/* like g_data_input_stream_read_line() but sets error if there's no content to read */
273
static gchar *
274
_my_g_data_input_stream_read_line (GDataInputStream  *dis,
275
                                   gsize             *out_line_length,
276
                                   GCancellable      *cancellable,
277
                                   GError           **error)
278
0
{
279
0
  gchar *ret;
280
281
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
282
283
0
  ret = g_data_input_stream_read_line (dis,
284
0
                                       out_line_length,
285
0
                                       cancellable,
286
0
                                       error);
287
0
  if (ret == NULL && error != NULL && *error == NULL)
288
0
    {
289
0
      g_set_error_literal (error,
290
0
                           G_IO_ERROR,
291
0
                           G_IO_ERROR_FAILED,
292
0
                           _("Unexpected lack of content trying to read a line"));
293
0
    }
294
295
0
  return ret;
296
0
}
297
298
/* This function is to avoid situations like this
299
 *
300
 * BEGIN\r\nl\0\0\1...
301
 *
302
 * e.g. where we read into the first D-Bus message while waiting for
303
 * the final line from the client (TODO: file bug against gio for
304
 * this)
305
 */
306
static gchar *
307
_my_g_input_stream_read_line_safe (GInputStream  *i,
308
                                   gsize         *out_line_length,
309
                                   GCancellable  *cancellable,
310
                                   GError       **error)
311
0
{
312
0
  GString *str;
313
0
  gchar c;
314
0
  gssize num_read;
315
0
  gboolean last_was_cr;
316
317
0
  str = g_string_new (NULL);
318
319
0
  last_was_cr = FALSE;
320
0
  while (TRUE)
321
0
    {
322
0
      num_read = g_input_stream_read (i,
323
0
                                      &c,
324
0
                                      1,
325
0
                                      cancellable,
326
0
                                      error);
327
0
      if (num_read == -1)
328
0
        goto fail;
329
0
      if (num_read == 0)
330
0
        {
331
0
          if (error != NULL && *error == NULL)
332
0
            {
333
0
              g_set_error_literal (error,
334
0
                                   G_IO_ERROR,
335
0
                                   G_IO_ERROR_FAILED,
336
0
                                   _("Unexpected lack of content trying to (safely) read a line"));
337
0
            }
338
0
          goto fail;
339
0
        }
340
341
0
      g_string_append_c (str, (gint) c);
342
0
      if (last_was_cr)
343
0
        {
344
0
          if (c == 0x0a)
345
0
            {
346
0
              g_assert (str->len >= 2);
347
0
              g_string_set_size (str, str->len - 2);
348
0
              goto out;
349
0
            }
350
0
        }
351
0
      last_was_cr = (c == 0x0d);
352
0
    }
353
354
0
 out:
355
0
  if (out_line_length != NULL)
356
0
    *out_line_length = str->len;
357
0
  return g_string_free (str, FALSE);
358
359
0
 fail:
360
0
  g_assert (error == NULL || *error != NULL);
361
0
  g_string_free (str, TRUE);
362
0
  return NULL;
363
0
}
364
365
/* ---------------------------------------------------------------------------------------------------- */
366
367
static gchar *
368
hexdecode (const gchar  *str,
369
           gsize        *out_len,
370
           GError      **error)
371
0
{
372
0
  gchar *ret;
373
0
  GString *s;
374
0
  guint n;
375
376
0
  ret = NULL;
377
0
  s = g_string_new (NULL);
378
379
0
  for (n = 0; str[n] != '\0'; n += 2)
380
0
    {
381
0
      gint upper_nibble;
382
0
      gint lower_nibble;
383
0
      guint value;
384
385
0
      upper_nibble = g_ascii_xdigit_value (str[n]);
386
0
      lower_nibble = g_ascii_xdigit_value (str[n + 1]);
387
0
      if (upper_nibble == -1 || lower_nibble == -1)
388
0
        {
389
0
          g_set_error (error,
390
0
                       G_IO_ERROR,
391
0
                       G_IO_ERROR_FAILED,
392
0
                       "Error hexdecoding string '%s' around position %d",
393
0
                       str, n);
394
0
          goto out;
395
0
        }
396
0
      value = (upper_nibble<<4) | lower_nibble;
397
0
      g_string_append_c (s, value);
398
0
    }
399
400
0
  *out_len = s->len;
401
0
  ret = g_string_free (s, FALSE);
402
0
  s = NULL;
403
404
0
 out:
405
0
  if (s != NULL)
406
0
    {
407
0
      *out_len = 0;
408
0
      g_string_free (s, TRUE);
409
0
    }
410
0
   return ret;
411
0
}
412
413
/* ---------------------------------------------------------------------------------------------------- */
414
415
static GDBusAuthMechanism *
416
client_choose_mech_and_send_initial_response (GDBusAuth           *auth,
417
                                              GCredentials        *credentials_that_were_sent,
418
                                              GDBusConnectionFlags conn_flags,
419
                                              const gchar* const  *supported_auth_mechs,
420
                                              GPtrArray           *attempted_auth_mechs,
421
                                              GDataOutputStream   *dos,
422
                                              GCancellable        *cancellable,
423
                                              GError             **error)
424
0
{
425
0
  GDBusAuthMechanism *mech;
426
0
  GType auth_mech_to_use_gtype;
427
0
  guint n;
428
0
  guint m;
429
0
  gchar *initial_response;
430
0
  gsize initial_response_len;
431
0
  gchar *encoded;
432
0
  gchar *s;
433
434
0
 again:
435
0
  mech = NULL;
436
437
0
  debug_print ("CLIENT: Trying to choose mechanism");
438
439
  /* find an authentication mechanism to try, if any */
440
0
  auth_mech_to_use_gtype = (GType) 0;
441
0
  for (n = 0; supported_auth_mechs[n] != NULL; n++)
442
0
    {
443
0
      gboolean attempted_already;
444
0
      attempted_already = FALSE;
445
0
      for (m = 0; m < attempted_auth_mechs->len; m++)
446
0
        {
447
0
          if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
448
0
            {
449
0
              attempted_already = TRUE;
450
0
              break;
451
0
            }
452
0
        }
453
0
      if (!attempted_already)
454
0
        {
455
0
          auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
456
0
          if (auth_mech_to_use_gtype != (GType) 0)
457
0
            break;
458
0
        }
459
0
    }
460
461
0
  if (auth_mech_to_use_gtype == (GType) 0)
462
0
    {
463
0
      gchar *available;
464
0
      GString *tried_str;
465
466
0
      debug_print ("CLIENT: Exhausted all available mechanisms");
467
468
0
      available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
469
470
0
      tried_str = g_string_new (NULL);
471
0
      for (n = 0; n < attempted_auth_mechs->len; n++)
472
0
        {
473
0
          if (n > 0)
474
0
            g_string_append (tried_str, ", ");
475
0
          g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
476
0
        }
477
0
      g_set_error (error,
478
0
                   G_IO_ERROR,
479
0
                   G_IO_ERROR_FAILED,
480
0
                   _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
481
0
                   tried_str->str,
482
0
                   available);
483
0
      g_string_free (tried_str, TRUE);
484
0
      g_free (available);
485
0
      goto out;
486
0
    }
487
488
  /* OK, decided on a mechanism - let's do this thing */
489
0
  mech = g_object_new (auth_mech_to_use_gtype,
490
0
                       "stream", auth->priv->stream,
491
0
                       "credentials", credentials_that_were_sent,
492
0
                       NULL);
493
0
  debug_print ("CLIENT: Trying mechanism '%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
494
0
  g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
495
496
  /* the auth mechanism may not be supported
497
   * (for example, EXTERNAL only works if credentials were exchanged)
498
   */
499
0
  if (!_g_dbus_auth_mechanism_is_supported (mech))
500
0
    {
501
0
      debug_print ("CLIENT: Mechanism '%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
502
0
      g_object_unref (mech);
503
0
      mech = NULL;
504
0
      goto again;
505
0
    }
506
507
0
  initial_response_len = 0;
508
0
  initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
509
0
                                                             conn_flags,
510
0
                                                             &initial_response_len);
511
#if 0
512
  g_printerr ("using auth mechanism with name '%s' of type '%s' with initial response '%s'\n",
513
              _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
514
              g_type_name (G_TYPE_FROM_INSTANCE (mech)),
515
              initial_response);
516
#endif
517
0
  if (initial_response != NULL)
518
0
    {
519
      //g_printerr ("initial_response = '%s'\n", initial_response);
520
0
      encoded = _g_dbus_hexencode (initial_response, initial_response_len);
521
0
      s = g_strdup_printf ("AUTH %s %s\r\n",
522
0
                           _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
523
0
                           encoded);
524
0
      g_free (initial_response);
525
0
      g_free (encoded);
526
0
    }
527
0
  else
528
0
    {
529
0
      s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
530
0
    }
531
0
  debug_print ("CLIENT: writing '%s'", s);
532
0
  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
533
0
    {
534
0
      g_object_unref (mech);
535
0
      mech = NULL;
536
0
      g_free (s);
537
0
      goto out;
538
0
    }
539
0
  g_free (s);
540
541
0
 out:
542
0
  return mech;
543
0
}
544
545
546
/* ---------------------------------------------------------------------------------------------------- */
547
548
typedef enum
549
{
550
  CLIENT_STATE_WAITING_FOR_DATA,
551
  CLIENT_STATE_WAITING_FOR_OK,
552
  CLIENT_STATE_WAITING_FOR_REJECT,
553
  CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
554
} ClientState;
555
556
gchar *
557
_g_dbus_auth_run_client (GDBusAuth     *auth,
558
                         GDBusAuthObserver     *observer,
559
                         GDBusConnectionFlags conn_flags,
560
                         GDBusCapabilityFlags offered_capabilities,
561
                         GDBusCapabilityFlags *out_negotiated_capabilities,
562
                         GCancellable  *cancellable,
563
                         GError       **error)
564
0
{
565
0
  gchar *s;
566
0
  GDataInputStream *dis;
567
0
  GDataOutputStream *dos;
568
0
  GCredentials *credentials;
569
0
  gchar *ret_guid;
570
0
  gchar *line;
571
0
  gsize line_length;
572
0
  gchar **supported_auth_mechs;
573
0
  GPtrArray *attempted_auth_mechs;
574
0
  GDBusAuthMechanism *mech;
575
0
  ClientState state;
576
0
  GDBusCapabilityFlags negotiated_capabilities;
577
578
0
  g_return_val_if_fail ((conn_flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT), NULL);
579
0
  g_return_val_if_fail (!(conn_flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER), NULL);
580
581
0
  debug_print ("CLIENT: initiating");
582
583
0
  _g_dbus_auth_add_mechs (auth, observer);
584
585
0
  ret_guid = NULL;
586
0
  supported_auth_mechs = NULL;
587
0
  attempted_auth_mechs = g_ptr_array_new ();
588
0
  mech = NULL;
589
0
  negotiated_capabilities = 0;
590
0
  credentials = NULL;
591
592
0
  dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
593
0
  dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
594
0
  g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
595
0
  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
596
597
0
  g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
598
599
0
#ifdef G_OS_UNIX
600
0
  if (G_IS_UNIX_CONNECTION (auth->priv->stream))
601
0
    {
602
0
      credentials = g_credentials_new ();
603
0
      if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth->priv->stream),
604
0
                                               cancellable,
605
0
                                               error))
606
0
        goto out;
607
0
    }
608
0
  else
609
0
    {
610
0
      if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
611
0
        goto out;
612
0
    }
613
#else
614
  if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
615
    goto out;
616
#endif
617
618
0
  if (credentials != NULL)
619
0
    {
620
0
      if (G_UNLIKELY (_g_dbus_debug_authentication ()))
621
0
        {
622
0
          s = g_credentials_to_string (credentials);
623
0
          debug_print ("CLIENT: sent credentials '%s'", s);
624
0
          g_free (s);
625
0
        }
626
0
    }
627
0
  else
628
0
    {
629
0
      debug_print ("CLIENT: didn't send any credentials");
630
0
    }
631
632
  /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
633
634
  /* Get list of supported authentication mechanisms */
635
0
  s = "AUTH\r\n";
636
0
  debug_print ("CLIENT: writing '%s'", s);
637
0
  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
638
0
    goto out;
639
0
  state = CLIENT_STATE_WAITING_FOR_REJECT;
640
641
0
  while (TRUE)
642
0
    {
643
0
      switch (state)
644
0
        {
645
0
        case CLIENT_STATE_WAITING_FOR_REJECT:
646
0
          debug_print ("CLIENT: WaitingForReject");
647
0
          line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
648
0
          if (line == NULL)
649
0
            goto out;
650
0
          debug_print ("CLIENT: WaitingForReject, read '%s'", line);
651
652
0
        choose_mechanism:
653
0
          if (!g_str_has_prefix (line, "REJECTED "))
654
0
            {
655
0
              g_set_error (error,
656
0
                           G_IO_ERROR,
657
0
                           G_IO_ERROR_FAILED,
658
0
                           "In WaitingForReject: Expected 'REJECTED am1 am2 ... amN', got '%s'",
659
0
                           line);
660
0
              g_free (line);
661
0
              goto out;
662
0
            }
663
0
          if (supported_auth_mechs == NULL)
664
0
            {
665
0
              supported_auth_mechs = g_strsplit (line + sizeof ("REJECTED ") - 1, " ", 0);
666
#if 0
667
              for (n = 0; supported_auth_mechs != NULL && supported_auth_mechs[n] != NULL; n++)
668
                g_printerr ("supported_auth_mechs[%d] = '%s'\n", n, supported_auth_mechs[n]);
669
#endif
670
0
            }
671
0
          g_free (line);
672
0
          mech = client_choose_mech_and_send_initial_response (auth,
673
0
                                                               credentials,
674
0
                                                               conn_flags,
675
0
                                                               (const gchar* const *) supported_auth_mechs,
676
0
                                                               attempted_auth_mechs,
677
0
                                                               dos,
678
0
                                                               cancellable,
679
0
                                                               error);
680
0
          if (mech == NULL)
681
0
            goto out;
682
0
          if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA)
683
0
            state = CLIENT_STATE_WAITING_FOR_DATA;
684
0
          else
685
0
            state = CLIENT_STATE_WAITING_FOR_OK;
686
0
          break;
687
688
0
        case CLIENT_STATE_WAITING_FOR_OK:
689
0
          debug_print ("CLIENT: WaitingForOK");
690
0
          line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
691
0
          if (line == NULL)
692
0
            goto out;
693
0
          debug_print ("CLIENT: WaitingForOK, read '%s'", line);
694
0
          if (g_str_has_prefix (line, "OK "))
695
0
            {
696
0
              if (!g_dbus_is_guid (line + 3))
697
0
                {
698
0
                  g_set_error (error,
699
0
                               G_IO_ERROR,
700
0
                               G_IO_ERROR_FAILED,
701
0
                               "Invalid OK response '%s'",
702
0
                               line);
703
0
                  g_free (line);
704
0
                  goto out;
705
0
                }
706
0
              ret_guid = g_strdup (line + 3);
707
0
              g_free (line);
708
709
0
              if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
710
0
                {
711
0
                  s = "NEGOTIATE_UNIX_FD\r\n";
712
0
                  debug_print ("CLIENT: writing '%s'", s);
713
0
                  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
714
0
                    goto out;
715
0
                  state = CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD;
716
0
                }
717
0
              else
718
0
                {
719
0
                  s = "BEGIN\r\n";
720
0
                  debug_print ("CLIENT: writing '%s'", s);
721
0
                  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
722
0
                    goto out;
723
                  /* and we're done! */
724
0
                  goto out;
725
0
                }
726
0
            }
727
0
          else if (g_str_has_prefix (line, "REJECTED "))
728
0
            {
729
0
              goto choose_mechanism;
730
0
            }
731
0
          else
732
0
            {
733
              /* TODO: handle other valid responses */
734
0
              g_set_error (error,
735
0
                           G_IO_ERROR,
736
0
                           G_IO_ERROR_FAILED,
737
0
                           "In WaitingForOk: unexpected response '%s'",
738
0
                           line);
739
0
              g_free (line);
740
0
              goto out;
741
0
            }
742
0
          break;
743
744
0
        case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD:
745
0
          debug_print ("CLIENT: WaitingForAgreeUnixFD");
746
0
          line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
747
0
          if (line == NULL)
748
0
            goto out;
749
0
          debug_print ("CLIENT: WaitingForAgreeUnixFD, read='%s'", line);
750
0
          if (g_strcmp0 (line, "AGREE_UNIX_FD") == 0)
751
0
            {
752
0
              g_free (line);
753
0
              negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
754
0
              s = "BEGIN\r\n";
755
0
              debug_print ("CLIENT: writing '%s'", s);
756
0
              if (!g_data_output_stream_put_string (dos, s, cancellable, error))
757
0
                goto out;
758
              /* and we're done! */
759
0
              goto out;
760
0
            }
761
0
          else if (g_str_has_prefix (line, "ERROR") && (line[5] == 0 || g_ascii_isspace (line[5])))
762
0
            {
763
              //g_strstrip (line + 5); g_debug ("bah, no unix_fd: '%s'", line + 5);
764
0
              g_free (line);
765
0
              s = "BEGIN\r\n";
766
0
              debug_print ("CLIENT: writing '%s'", s);
767
0
              if (!g_data_output_stream_put_string (dos, s, cancellable, error))
768
0
                goto out;
769
              /* and we're done! */
770
0
              goto out;
771
0
            }
772
0
          else
773
0
            {
774
              /* TODO: handle other valid responses */
775
0
              g_set_error (error,
776
0
                           G_IO_ERROR,
777
0
                           G_IO_ERROR_FAILED,
778
0
                           "In WaitingForAgreeUnixFd: unexpected response '%s'",
779
0
                           line);
780
0
              g_free (line);
781
0
              goto out;
782
0
            }
783
0
          break;
784
785
0
        case CLIENT_STATE_WAITING_FOR_DATA:
786
0
          debug_print ("CLIENT: WaitingForData");
787
0
          line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
788
0
          if (line == NULL)
789
0
            goto out;
790
0
          debug_print ("CLIENT: WaitingForData, read='%s'", line);
791
0
          if (g_str_equal (line, "DATA") || g_str_has_prefix (line, "DATA "))
792
0
            {
793
0
              gchar *encoded;
794
0
              gchar *decoded_data;
795
0
              gsize decoded_data_len = 0;
796
797
0
              encoded = g_strdup (line + 4);
798
0
              g_free (line);
799
0
              g_strstrip (encoded);
800
0
              decoded_data = hexdecode (encoded, &decoded_data_len, error);
801
0
              g_free (encoded);
802
0
              if (decoded_data == NULL)
803
0
                {
804
0
                  g_prefix_error (error, "DATA response is malformed: ");
805
                  /* invalid encoding, disconnect! */
806
0
                  goto out;
807
0
                }
808
0
              _g_dbus_auth_mechanism_client_data_receive (mech, decoded_data, decoded_data_len);
809
0
              g_free (decoded_data);
810
811
0
              if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND)
812
0
                {
813
0
                  gchar *data;
814
0
                  gsize data_len;
815
816
0
                  data = _g_dbus_auth_mechanism_client_data_send (mech, &data_len);
817
818
0
                  if (data_len == 0)
819
0
                    {
820
0
                      s = g_strdup ("DATA\r\n");
821
0
                    }
822
0
                  else
823
0
                    {
824
0
                      gchar *encoded_data = _g_dbus_hexencode (data, data_len);
825
826
0
                      s = g_strdup_printf ("DATA %s\r\n", encoded_data);
827
0
                      g_free (encoded_data);
828
0
                    }
829
830
0
                  g_free (data);
831
0
                  debug_print ("CLIENT: writing '%s'", s);
832
0
                  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
833
0
                    {
834
0
                      g_free (s);
835
0
                      goto out;
836
0
                    }
837
0
                  g_free (s);
838
0
                }
839
0
              state = CLIENT_STATE_WAITING_FOR_OK;
840
0
            }
841
0
          else if (g_str_has_prefix (line, "REJECTED "))
842
0
            {
843
              /* could be the chosen authentication method just doesn't work. Try
844
               * another one...
845
               */
846
0
              goto choose_mechanism;
847
0
            }
848
0
          else
849
0
            {
850
0
              g_set_error (error,
851
0
                           G_IO_ERROR,
852
0
                           G_IO_ERROR_FAILED,
853
0
                           "In WaitingForData: unexpected response '%s'",
854
0
                           line);
855
0
              g_free (line);
856
0
              goto out;
857
0
            }
858
0
          break;
859
860
0
        default:
861
0
          g_assert_not_reached ();
862
0
          break;
863
0
        }
864
865
0
    }; /* main authentication client loop */
866
867
0
 out:
868
0
  if (mech != NULL)
869
0
    g_object_unref (mech);
870
0
  g_ptr_array_unref (attempted_auth_mechs);
871
0
  g_strfreev (supported_auth_mechs);
872
0
  g_object_unref (dis);
873
0
  g_object_unref (dos);
874
875
  /* ensure return value is NULL if error is set */
876
0
  if (error != NULL && *error != NULL)
877
0
    {
878
0
      g_free (ret_guid);
879
0
      ret_guid = NULL;
880
0
    }
881
882
0
  if (ret_guid != NULL)
883
0
    {
884
0
      if (out_negotiated_capabilities != NULL)
885
0
        *out_negotiated_capabilities = negotiated_capabilities;
886
0
    }
887
888
0
  if (credentials != NULL)
889
0
    g_object_unref (credentials);
890
891
0
  debug_print ("CLIENT: Done, authenticated=%d", ret_guid != NULL);
892
893
0
  return ret_guid;
894
0
}
895
896
/* ---------------------------------------------------------------------------------------------------- */
897
898
static gchar *
899
get_auth_mechanisms (GDBusAuth     *auth,
900
                     gboolean       allow_anonymous,
901
                     const gchar   *prefix,
902
                     const gchar   *suffix,
903
                     const gchar   *separator)
904
0
{
905
0
  GList *l;
906
0
  GString *str;
907
0
  gboolean need_sep;
908
909
0
  str = g_string_new (prefix);
910
0
  need_sep = FALSE;
911
0
  for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
912
0
    {
913
0
      Mechanism *m = l->data;
914
915
0
      if (!allow_anonymous && g_strcmp0 (m->name, "ANONYMOUS") == 0)
916
0
        continue;
917
918
0
      if (need_sep)
919
0
        g_string_append (str, separator);
920
0
      g_string_append (str, m->name);
921
0
      need_sep = TRUE;
922
0
    }
923
924
0
  g_string_append (str, suffix);
925
0
  return g_string_free (str, FALSE);
926
0
}
927
928
929
typedef enum
930
{
931
  SERVER_STATE_WAITING_FOR_AUTH,
932
  SERVER_STATE_WAITING_FOR_DATA,
933
  SERVER_STATE_WAITING_FOR_BEGIN
934
} ServerState;
935
936
gboolean
937
_g_dbus_auth_run_server (GDBusAuth              *auth,
938
                         GDBusAuthObserver      *observer,
939
                         const gchar            *guid,
940
                         gboolean                allow_anonymous,
941
                         gboolean                require_same_user,
942
                         GDBusCapabilityFlags    offered_capabilities,
943
                         GDBusCapabilityFlags   *out_negotiated_capabilities,
944
                         GCredentials          **out_received_credentials,
945
                         GCancellable           *cancellable,
946
                         GError                **error)
947
0
{
948
0
  gboolean ret;
949
0
  ServerState state;
950
0
  GDataOutputStream *dos;
951
0
  GError *local_error;
952
0
  gchar *line;
953
0
  gsize line_length;
954
0
  GDBusAuthMechanism *mech;
955
0
  gchar *s;
956
0
  GDBusCapabilityFlags negotiated_capabilities;
957
0
  GCredentials *credentials;
958
0
  GCredentials *own_credentials = NULL;
959
960
0
  debug_print ("SERVER: initiating");
961
962
0
  _g_dbus_auth_add_mechs (auth, observer);
963
964
0
  ret = FALSE;
965
0
  dos = NULL;
966
0
  mech = NULL;
967
0
  negotiated_capabilities = 0;
968
0
  credentials = NULL;
969
970
0
  if (!g_dbus_is_guid (guid))
971
0
    {
972
0
      g_set_error (error,
973
0
                   G_IO_ERROR,
974
0
                   G_IO_ERROR_FAILED,
975
0
                   "The given GUID '%s' is not valid",
976
0
                   guid);
977
0
      goto out;
978
0
    }
979
980
  /* We use an extremely slow (but reliable) line reader for input
981
   * instead of something buffered - this basically does a recvfrom()
982
   * system call per character
983
   *
984
   * (the problem with using GDataInputStream's read_line is that
985
   * because of buffering it might start reading into the first D-Bus
986
   * message that appears after "BEGIN\r\n"....)
987
   */
988
989
0
  dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
990
0
  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
991
992
  /* read the NUL-byte, possibly with credentials attached */
993
0
#ifndef G_CREDENTIALS_PREFER_MESSAGE_PASSING
994
0
  if (G_IS_SOCKET_CONNECTION (auth->priv->stream))
995
0
    {
996
0
      GSocket *sock = g_socket_connection_get_socket (G_SOCKET_CONNECTION (auth->priv->stream));
997
998
0
      local_error = NULL;
999
0
      credentials = g_socket_get_credentials (sock, &local_error);
1000
1001
0
      if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1002
0
        {
1003
0
          g_propagate_error (error, local_error);
1004
0
          goto out;
1005
0
        }
1006
0
      else
1007
0
        {
1008
          /* Clear the error indicator, so we can retry with
1009
           * g_unix_connection_receive_credentials() if necessary */
1010
0
          g_clear_error (&local_error);
1011
0
        }
1012
0
    }
1013
0
#endif
1014
1015
0
  if (credentials == NULL && G_IS_UNIX_CONNECTION (auth->priv->stream))
1016
0
    {
1017
0
      local_error = NULL;
1018
0
      credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
1019
0
                                                           cancellable,
1020
0
                                                           &local_error);
1021
0
      if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1022
0
        {
1023
0
          g_propagate_error (error, local_error);
1024
0
          goto out;
1025
0
        }
1026
0
      g_clear_error (&local_error);
1027
0
    }
1028
0
  else
1029
0
    {
1030
0
      gchar c;
1031
0
      gssize num_read;
1032
1033
0
      local_error = NULL;
1034
0
      num_read = g_input_stream_read (g_io_stream_get_input_stream (auth->priv->stream),
1035
0
                                      &c, 1,
1036
0
                                      cancellable, &local_error);
1037
0
      if (num_read != 1 || local_error != NULL)
1038
0
        {
1039
0
          if (local_error == NULL)
1040
0
            g_set_error_literal (error,
1041
0
                                 G_IO_ERROR,
1042
0
                                 G_IO_ERROR_FAILED,
1043
0
                                 _ ("Unexpected lack of content trying to read a byte"));
1044
0
          else
1045
0
            g_propagate_error (error, local_error);
1046
0
          goto out;
1047
0
        }
1048
0
    }
1049
1050
0
  if (credentials != NULL)
1051
0
    {
1052
0
      if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1053
0
        {
1054
0
          s = g_credentials_to_string (credentials);
1055
0
          debug_print ("SERVER: received credentials '%s'", s);
1056
0
          g_free (s);
1057
0
        }
1058
0
    }
1059
0
  else
1060
0
    {
1061
0
      debug_print ("SERVER: didn't receive any credentials");
1062
0
    }
1063
1064
0
  own_credentials = g_credentials_new ();
1065
1066
0
  state = SERVER_STATE_WAITING_FOR_AUTH;
1067
0
  while (TRUE)
1068
0
    {
1069
0
      switch (state)
1070
0
        {
1071
0
        case SERVER_STATE_WAITING_FOR_AUTH:
1072
0
          debug_print ("SERVER: WaitingForAuth");
1073
0
          line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1074
0
                                                    &line_length,
1075
0
                                                    cancellable,
1076
0
                                                    error);
1077
0
          debug_print ("SERVER: WaitingForAuth, read '%s'", line);
1078
0
          if (line == NULL)
1079
0
            goto out;
1080
0
          if (g_strcmp0 (line, "AUTH") == 0)
1081
0
            {
1082
0
              s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1083
0
              debug_print ("SERVER: writing '%s'", s);
1084
0
              if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1085
0
                {
1086
0
                  g_free (s);
1087
0
                  g_free (line);
1088
0
                  goto out;
1089
0
                }
1090
0
              g_free (s);
1091
0
              g_free (line);
1092
0
            }
1093
0
          else if (g_str_has_prefix (line, "AUTH "))
1094
0
            {
1095
0
              gchar **tokens;
1096
0
              const gchar *encoded;
1097
0
              const gchar *mech_name;
1098
0
              GType auth_mech_to_use_gtype;
1099
1100
0
              tokens = g_strsplit (line, " ", 0);
1101
1102
0
              switch (g_strv_length (tokens))
1103
0
                {
1104
0
                case 2:
1105
                  /* no initial response */
1106
0
                  mech_name = tokens[1];
1107
0
                  encoded = NULL;
1108
0
                  break;
1109
1110
0
                case 3:
1111
                  /* initial response */
1112
0
                  mech_name = tokens[1];
1113
0
                  encoded = tokens[2];
1114
0
                  break;
1115
1116
0
                default:
1117
0
                  g_set_error (error,
1118
0
                               G_IO_ERROR,
1119
0
                               G_IO_ERROR_FAILED,
1120
0
                               "Unexpected line '%s' while in WaitingForAuth state",
1121
0
                               line);
1122
0
                  g_strfreev (tokens);
1123
0
                  g_free (line);
1124
0
                  goto out;
1125
0
                }
1126
1127
0
              g_free (line);
1128
1129
              /* TODO: record that the client has attempted to use this mechanism */
1130
              //g_debug ("client is trying '%s'", mech_name);
1131
1132
0
              auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1133
0
              if ((auth_mech_to_use_gtype == (GType) 0) ||
1134
0
                  (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1135
0
                {
1136
                  /* We don't support this auth mechanism */
1137
0
                  g_strfreev (tokens);
1138
0
                  s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1139
0
                  debug_print ("SERVER: writing '%s'", s);
1140
0
                  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1141
0
                    {
1142
0
                      g_free (s);
1143
0
                      goto out;
1144
0
                    }
1145
0
                  g_free (s);
1146
1147
                  /* stay in WAITING FOR AUTH */
1148
0
                  state = SERVER_STATE_WAITING_FOR_AUTH;
1149
0
                }
1150
0
              else
1151
0
                {
1152
0
                  gchar *initial_response;
1153
0
                  gsize initial_response_len;
1154
1155
0
                  g_clear_object (&mech);
1156
0
                  mech = g_object_new (auth_mech_to_use_gtype,
1157
0
                                       "stream", auth->priv->stream,
1158
0
                                       "credentials", credentials,
1159
0
                                       NULL);
1160
1161
0
                  initial_response = NULL;
1162
0
                  initial_response_len = 0;
1163
0
                  if (encoded != NULL)
1164
0
                    {
1165
0
                      initial_response = hexdecode (encoded, &initial_response_len, error);
1166
0
                      if (initial_response == NULL)
1167
0
                        {
1168
0
                          g_prefix_error (error, "Initial response is malformed: ");
1169
                          /* invalid encoding, disconnect! */
1170
0
                          g_strfreev (tokens);
1171
0
                          goto out;
1172
0
                        }
1173
0
                    }
1174
1175
0
                  _g_dbus_auth_mechanism_server_initiate (mech,
1176
0
                                                          initial_response,
1177
0
                                                          initial_response_len);
1178
0
                  g_free (initial_response);
1179
0
                  g_strfreev (tokens);
1180
1181
0
                change_state:
1182
0
                  switch (_g_dbus_auth_mechanism_server_get_state (mech))
1183
0
                    {
1184
0
                    case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1185
0
                      if (require_same_user &&
1186
0
                          (credentials == NULL ||
1187
0
                           !g_credentials_is_same_user (credentials, own_credentials, NULL)))
1188
0
                        {
1189
                          /* disconnect */
1190
0
                          g_set_error_literal (error,
1191
0
                                               G_IO_ERROR,
1192
0
                                               G_IO_ERROR_FAILED,
1193
0
                                               _("User IDs must be the same for peer and server"));
1194
0
                          goto out;
1195
0
                        }
1196
0
                      else if (observer != NULL &&
1197
0
                               !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1198
0
                                                                                   auth->priv->stream,
1199
0
                                                                                   credentials))
1200
0
                        {
1201
                          /* disconnect */
1202
0
                          g_set_error_literal (error,
1203
0
                                               G_IO_ERROR,
1204
0
                                               G_IO_ERROR_FAILED,
1205
0
                                               _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1206
0
                          goto out;
1207
0
                        }
1208
0
                      else
1209
0
                        {
1210
0
                          s = g_strdup_printf ("OK %s\r\n", guid);
1211
0
                          debug_print ("SERVER: writing '%s'", s);
1212
0
                          if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1213
0
                            {
1214
0
                              g_free (s);
1215
0
                              goto out;
1216
0
                            }
1217
0
                          g_free (s);
1218
0
                          state = SERVER_STATE_WAITING_FOR_BEGIN;
1219
0
                        }
1220
0
                      break;
1221
1222
0
                    case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1223
0
                      s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1224
0
                      debug_print ("SERVER: writing '%s'", s);
1225
0
                      if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1226
0
                        {
1227
0
                          g_free (s);
1228
0
                          goto out;
1229
0
                        }
1230
0
                      g_free (s);
1231
0
                      state = SERVER_STATE_WAITING_FOR_AUTH;
1232
0
                      break;
1233
1234
0
                    case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1235
0
                      state = SERVER_STATE_WAITING_FOR_DATA;
1236
0
                      break;
1237
1238
0
                    case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1239
0
                      {
1240
0
                        gchar *data;
1241
0
                        gsize data_len;
1242
1243
0
                        data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1244
1245
0
                        if (data != NULL)
1246
0
                          {
1247
0
                            if (data_len == 0)
1248
0
                              {
1249
0
                                s = g_strdup ("DATA\r\n");
1250
0
                              }
1251
0
                            else
1252
0
                              {
1253
0
                                gchar *encoded_data = _g_dbus_hexencode (data, data_len);
1254
1255
0
                                s = g_strdup_printf ("DATA %s\r\n", encoded_data);
1256
0
                                g_free (encoded_data);
1257
0
                              }
1258
1259
0
                            g_free (data);
1260
1261
0
                            debug_print ("SERVER: writing '%s'", s);
1262
0
                            if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1263
0
                              {
1264
0
                                g_free (s);
1265
0
                                goto out;
1266
0
                              }
1267
0
                            g_free (s);
1268
0
                          }
1269
0
                      }
1270
0
                      goto change_state;
1271
0
                      break;
1272
1273
0
                    default:
1274
                      /* TODO */
1275
0
                      g_assert_not_reached ();
1276
0
                      break;
1277
0
                    }
1278
0
                }
1279
0
            }
1280
0
          else
1281
0
            {
1282
0
              g_set_error (error,
1283
0
                           G_IO_ERROR,
1284
0
                           G_IO_ERROR_FAILED,
1285
0
                           "Unexpected line '%s' while in WaitingForAuth state",
1286
0
                           line);
1287
0
              g_free (line);
1288
0
              goto out;
1289
0
            }
1290
0
          break;
1291
1292
0
        case SERVER_STATE_WAITING_FOR_DATA:
1293
0
          debug_print ("SERVER: WaitingForData");
1294
0
          line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1295
0
                                                    &line_length,
1296
0
                                                    cancellable,
1297
0
                                                    error);
1298
0
          debug_print ("SERVER: WaitingForData, read '%s'", line);
1299
0
          if (line == NULL)
1300
0
            goto out;
1301
0
          if (g_str_equal (line, "DATA") || g_str_has_prefix (line, "DATA "))
1302
0
            {
1303
0
              gchar *encoded;
1304
0
              gchar *decoded_data;
1305
0
              gsize decoded_data_len = 0;
1306
1307
0
              encoded = g_strdup (line + 4);
1308
0
              g_free (line);
1309
0
              g_strstrip (encoded);
1310
0
              decoded_data = hexdecode (encoded, &decoded_data_len, error);
1311
0
              g_free (encoded);
1312
0
              if (decoded_data == NULL)
1313
0
                {
1314
0
                  g_prefix_error (error, "DATA response is malformed: ");
1315
                  /* invalid encoding, disconnect! */
1316
0
                  goto out;
1317
0
                }
1318
0
              _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1319
0
              g_free (decoded_data);
1320
              /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1321
0
              goto change_state;
1322
0
            }
1323
0
          else
1324
0
            {
1325
0
              g_set_error (error,
1326
0
                           G_IO_ERROR,
1327
0
                           G_IO_ERROR_FAILED,
1328
0
                           "Unexpected line '%s' while in WaitingForData state",
1329
0
                           line);
1330
0
              g_free (line);
1331
0
            }
1332
0
          goto out;
1333
1334
0
        case SERVER_STATE_WAITING_FOR_BEGIN:
1335
0
          debug_print ("SERVER: WaitingForBegin");
1336
0
          line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1337
0
                                                    &line_length,
1338
0
                                                    cancellable,
1339
0
                                                    error);
1340
0
          if (line == NULL)
1341
0
            goto out;
1342
0
          debug_print ("SERVER: WaitingForBegin, read '%s'", line);
1343
0
          if (g_strcmp0 (line, "BEGIN") == 0)
1344
0
            {
1345
              /* YAY, done! */
1346
0
              ret = TRUE;
1347
0
              g_free (line);
1348
0
              goto out;
1349
0
            }
1350
0
          else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1351
0
            {
1352
0
              g_free (line);
1353
0
              if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1354
0
                {
1355
0
                  negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1356
0
                  s = "AGREE_UNIX_FD\r\n";
1357
0
                  debug_print ("SERVER: writing '%s'", s);
1358
0
                  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1359
0
                    goto out;
1360
0
                }
1361
0
              else
1362
0
                {
1363
0
                  s = "ERROR \"fd passing not offered\"\r\n";
1364
0
                  debug_print ("SERVER: writing '%s'", s);
1365
0
                  if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1366
0
                    goto out;
1367
0
                }
1368
0
            }
1369
0
          else
1370
0
            {
1371
0
              g_debug ("Unexpected line '%s' while in WaitingForBegin state", line);
1372
0
              g_free (line);
1373
0
              s = "ERROR \"Unknown Command\"\r\n";
1374
0
              debug_print ("SERVER: writing '%s'", s);
1375
0
              if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1376
0
                goto out;
1377
0
            }
1378
0
          break;
1379
1380
0
        default:
1381
0
          g_assert_not_reached ();
1382
0
          break;
1383
0
        }
1384
0
    }
1385
1386
1387
0
  g_set_error_literal (error,
1388
0
                       G_IO_ERROR,
1389
0
                       G_IO_ERROR_FAILED,
1390
0
                       "Not implemented (server)");
1391
1392
0
 out:
1393
0
  g_clear_object (&mech);
1394
0
  g_clear_object (&dos);
1395
0
  g_clear_object (&own_credentials);
1396
1397
  /* ensure return value is FALSE if error is set */
1398
0
  if (error != NULL && *error != NULL)
1399
0
    {
1400
0
      ret = FALSE;
1401
0
    }
1402
1403
0
  if (ret)
1404
0
    {
1405
0
      if (out_negotiated_capabilities != NULL)
1406
0
        *out_negotiated_capabilities = negotiated_capabilities;
1407
0
      if (out_received_credentials != NULL)
1408
0
        *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1409
0
    }
1410
1411
0
  if (credentials != NULL)
1412
0
    g_object_unref (credentials);
1413
1414
0
  debug_print ("SERVER: Done, authenticated=%d", ret);
1415
1416
0
  return ret;
1417
0
}
1418
1419
/* ---------------------------------------------------------------------------------------------------- */