Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gdbusauthmechanismanon.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 "gdbusauthmechanismanon.h"
26
#include "gdbuserror.h"
27
#include "gioenumtypes.h"
28
29
#include "glibintl.h"
30
31
struct _GDBusAuthMechanismAnonPrivate
32
{
33
  gboolean is_client;
34
  gboolean is_server;
35
  GDBusAuthMechanismState state;
36
};
37
38
static gint                     mechanism_get_priority              (void);
39
static const gchar             *mechanism_get_name                  (void);
40
41
static gboolean                 mechanism_is_supported              (GDBusAuthMechanism   *mechanism);
42
static gchar                   *mechanism_encode_data               (GDBusAuthMechanism   *mechanism,
43
                                                                     const gchar          *data,
44
                                                                     gsize                 data_len,
45
                                                                     gsize                *out_data_len);
46
static gchar                   *mechanism_decode_data               (GDBusAuthMechanism   *mechanism,
47
                                                                     const gchar          *data,
48
                                                                     gsize                 data_len,
49
                                                                     gsize                *out_data_len);
50
static GDBusAuthMechanismState  mechanism_server_get_state          (GDBusAuthMechanism   *mechanism);
51
static void                     mechanism_server_initiate           (GDBusAuthMechanism   *mechanism,
52
                                                                     const gchar          *initial_response,
53
                                                                     gsize                 initial_response_len);
54
static void                     mechanism_server_data_receive       (GDBusAuthMechanism   *mechanism,
55
                                                                     const gchar          *data,
56
                                                                     gsize                 data_len);
57
static gchar                   *mechanism_server_data_send          (GDBusAuthMechanism   *mechanism,
58
                                                                     gsize                *out_data_len);
59
static gchar                   *mechanism_server_get_reject_reason  (GDBusAuthMechanism   *mechanism);
60
static void                     mechanism_server_shutdown           (GDBusAuthMechanism   *mechanism);
61
static GDBusAuthMechanismState  mechanism_client_get_state          (GDBusAuthMechanism   *mechanism);
62
static gchar                   *mechanism_client_initiate           (GDBusAuthMechanism   *mechanism,
63
                                                                     GDBusConnectionFlags  conn_flags,
64
                                                                     gsize                *out_initial_response_len);
65
static void                     mechanism_client_data_receive       (GDBusAuthMechanism   *mechanism,
66
                                                                     const gchar          *data,
67
                                                                     gsize                 data_len);
68
static gchar                   *mechanism_client_data_send          (GDBusAuthMechanism   *mechanism,
69
                                                                     gsize                *out_data_len);
70
static void                     mechanism_client_shutdown           (GDBusAuthMechanism   *mechanism);
71
72
/* ---------------------------------------------------------------------------------------------------- */
73
74
G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismAnon, _g_dbus_auth_mechanism_anon, G_TYPE_DBUS_AUTH_MECHANISM)
75
76
/* ---------------------------------------------------------------------------------------------------- */
77
78
static void
79
_g_dbus_auth_mechanism_anon_finalize (GObject *object)
80
0
{
81
  //GDBusAuthMechanismAnon *mechanism = G_DBUS_AUTH_MECHANISM_ANON (object);
82
83
0
  if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_anon_parent_class)->finalize != NULL)
84
0
    G_OBJECT_CLASS (_g_dbus_auth_mechanism_anon_parent_class)->finalize (object);
85
0
}
86
87
static void
88
_g_dbus_auth_mechanism_anon_class_init (GDBusAuthMechanismAnonClass *klass)
89
0
{
90
0
  GObjectClass *gobject_class;
91
0
  GDBusAuthMechanismClass *mechanism_class;
92
93
0
  gobject_class = G_OBJECT_CLASS (klass);
94
0
  gobject_class->finalize = _g_dbus_auth_mechanism_anon_finalize;
95
96
0
  mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
97
0
  mechanism_class->get_priority              = mechanism_get_priority;
98
0
  mechanism_class->get_name                  = mechanism_get_name;
99
0
  mechanism_class->is_supported              = mechanism_is_supported;
100
0
  mechanism_class->encode_data               = mechanism_encode_data;
101
0
  mechanism_class->decode_data               = mechanism_decode_data;
102
0
  mechanism_class->server_get_state          = mechanism_server_get_state;
103
0
  mechanism_class->server_initiate           = mechanism_server_initiate;
104
0
  mechanism_class->server_data_receive       = mechanism_server_data_receive;
105
0
  mechanism_class->server_data_send          = mechanism_server_data_send;
106
0
  mechanism_class->server_get_reject_reason  = mechanism_server_get_reject_reason;
107
0
  mechanism_class->server_shutdown           = mechanism_server_shutdown;
108
0
  mechanism_class->client_get_state          = mechanism_client_get_state;
109
0
  mechanism_class->client_initiate           = mechanism_client_initiate;
110
0
  mechanism_class->client_data_receive       = mechanism_client_data_receive;
111
0
  mechanism_class->client_data_send          = mechanism_client_data_send;
112
0
  mechanism_class->client_shutdown           = mechanism_client_shutdown;
113
0
}
114
115
static void
116
_g_dbus_auth_mechanism_anon_init (GDBusAuthMechanismAnon *mechanism)
117
0
{
118
0
  mechanism->priv = _g_dbus_auth_mechanism_anon_get_instance_private (mechanism);
119
0
}
120
121
/* ---------------------------------------------------------------------------------------------------- */
122
123
124
static gint
125
mechanism_get_priority (void)
126
0
{
127
  /* We prefer ANONYMOUS to most other mechanism (such as DBUS_COOKIE_SHA1) but not to EXTERNAL */
128
0
  return 50;
129
0
}
130
131
132
static const gchar *
133
mechanism_get_name (void)
134
0
{
135
0
  return "ANONYMOUS";
136
0
}
137
138
static gboolean
139
mechanism_is_supported (GDBusAuthMechanism *mechanism)
140
0
{
141
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), FALSE);
142
0
  return TRUE;
143
0
}
144
145
static gchar *
146
mechanism_encode_data (GDBusAuthMechanism   *mechanism,
147
                       const gchar          *data,
148
                       gsize                 data_len,
149
                       gsize                *out_data_len)
150
0
{
151
0
  return NULL;
152
0
}
153
154
155
static gchar *
156
mechanism_decode_data (GDBusAuthMechanism   *mechanism,
157
                       const gchar          *data,
158
                       gsize                 data_len,
159
                       gsize                *out_data_len)
160
0
{
161
0
  return NULL;
162
0
}
163
164
/* ---------------------------------------------------------------------------------------------------- */
165
166
static GDBusAuthMechanismState
167
mechanism_server_get_state (GDBusAuthMechanism   *mechanism)
168
0
{
169
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
170
171
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
172
0
  g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
173
174
0
  return m->priv->state;
175
0
}
176
177
static void
178
mechanism_server_initiate (GDBusAuthMechanism   *mechanism,
179
                           const gchar          *initial_response,
180
                           gsize                 initial_response_len)
181
0
{
182
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
183
184
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
185
0
  g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
186
187
  //g_debug ("ANONYMOUS: initial_response was '%s'", initial_response);
188
189
0
  m->priv->is_server = TRUE;
190
0
  m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
191
0
}
192
193
static void
194
mechanism_server_data_receive (GDBusAuthMechanism   *mechanism,
195
                               const gchar          *data,
196
                               gsize                 data_len)
197
0
{
198
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
199
200
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
201
0
  g_return_if_fail (m->priv->is_server && !m->priv->is_client);
202
0
  g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
203
204
  /* can never end up here because we are never in the WAITING_FOR_DATA state */
205
0
  g_assert_not_reached ();
206
0
}
207
208
static gchar *
209
mechanism_server_data_send (GDBusAuthMechanism   *mechanism,
210
                            gsize                *out_data_len)
211
0
{
212
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
213
214
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
215
0
  g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
216
0
  g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
217
218
  /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
219
0
  g_assert_not_reached ();
220
221
0
  return NULL;
222
0
}
223
224
static gchar *
225
mechanism_server_get_reject_reason (GDBusAuthMechanism   *mechanism)
226
0
{
227
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
228
229
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
230
0
  g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
231
0
  g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
232
233
  /* can never end up here because we are never in the REJECTED state */
234
0
  g_assert_not_reached ();
235
236
0
  return NULL;
237
0
}
238
239
static void
240
mechanism_server_shutdown (GDBusAuthMechanism   *mechanism)
241
0
{
242
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
243
244
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
245
0
  g_return_if_fail (m->priv->is_server && !m->priv->is_client);
246
247
0
  m->priv->is_server = FALSE;
248
0
}
249
250
/* ---------------------------------------------------------------------------------------------------- */
251
252
static GDBusAuthMechanismState
253
mechanism_client_get_state (GDBusAuthMechanism   *mechanism)
254
0
{
255
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
256
257
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
258
0
  g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
259
260
0
  return m->priv->state;
261
0
}
262
263
static gchar *
264
mechanism_client_initiate (GDBusAuthMechanism   *mechanism,
265
                           GDBusConnectionFlags  conn_flags,
266
                           gsize                *out_initial_response_len)
267
0
{
268
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
269
0
  gchar *result;
270
271
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
272
0
  g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
273
274
0
  m->priv->is_client = TRUE;
275
0
  m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
276
277
  /* just return our library name and version */
278
0
  result = g_strdup ("GDBus 0.1");
279
0
  *out_initial_response_len = strlen (result);
280
281
0
  return result;
282
0
}
283
284
static void
285
mechanism_client_data_receive (GDBusAuthMechanism   *mechanism,
286
                               const gchar          *data,
287
                               gsize                 data_len)
288
0
{
289
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
290
291
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
292
0
  g_return_if_fail (m->priv->is_client && !m->priv->is_server);
293
0
  g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
294
295
  /* can never end up here because we are never in the WAITING_FOR_DATA state */
296
0
  g_assert_not_reached ();
297
0
}
298
299
static gchar *
300
mechanism_client_data_send (GDBusAuthMechanism   *mechanism,
301
                            gsize                *out_data_len)
302
0
{
303
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
304
305
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
306
0
  g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
307
0
  g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
308
309
  /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
310
0
  g_assert_not_reached ();
311
312
0
  return NULL;
313
0
}
314
315
static void
316
mechanism_client_shutdown (GDBusAuthMechanism   *mechanism)
317
0
{
318
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
319
320
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
321
0
  g_return_if_fail (m->priv->is_client && !m->priv->is_server);
322
323
0
  m->priv->is_client = FALSE;
324
0
}
325
326
/* ---------------------------------------------------------------------------------------------------- */