Coverage Report

Created: 2026-05-23 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/gdbusauthmechanismanon.c
Line
Count
Source
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_or_client_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
0
G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismAnon, _g_dbus_auth_mechanism_anon, G_TYPE_DBUS_AUTH_MECHANISM)
75
0
76
0
/* ---------------------------------------------------------------------------------------------------- */
77
0
78
0
static void
79
0
_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_or_client_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_get_reject_reason  = mechanism_server_or_client_get_reject_reason;
113
0
  mechanism_class->client_shutdown           = mechanism_client_shutdown;
114
0
}
115
116
static void
117
_g_dbus_auth_mechanism_anon_init (GDBusAuthMechanismAnon *mechanism)
118
0
{
119
0
  mechanism->priv = _g_dbus_auth_mechanism_anon_get_instance_private (mechanism);
120
0
}
121
122
/* ---------------------------------------------------------------------------------------------------- */
123
124
125
static gint
126
mechanism_get_priority (void)
127
0
{
128
  /* We prefer ANONYMOUS to most other mechanism (such as DBUS_COOKIE_SHA1) but not to EXTERNAL */
129
0
  return 50;
130
0
}
131
132
133
static const gchar *
134
mechanism_get_name (void)
135
0
{
136
0
  return "ANONYMOUS";
137
0
}
138
139
static gboolean
140
mechanism_is_supported (GDBusAuthMechanism *mechanism)
141
0
{
142
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), FALSE);
143
0
  return TRUE;
144
0
}
145
146
static gchar *
147
mechanism_encode_data (GDBusAuthMechanism   *mechanism,
148
                       const gchar          *data,
149
                       gsize                 data_len,
150
                       gsize                *out_data_len)
151
0
{
152
0
  return NULL;
153
0
}
154
155
156
static gchar *
157
mechanism_decode_data (GDBusAuthMechanism   *mechanism,
158
                       const gchar          *data,
159
                       gsize                 data_len,
160
                       gsize                *out_data_len)
161
0
{
162
0
  return NULL;
163
0
}
164
165
/* ---------------------------------------------------------------------------------------------------- */
166
167
static GDBusAuthMechanismState
168
mechanism_server_get_state (GDBusAuthMechanism   *mechanism)
169
0
{
170
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
171
172
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
173
0
  g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
174
175
0
  return m->priv->state;
176
0
}
177
178
static void
179
mechanism_server_initiate (GDBusAuthMechanism   *mechanism,
180
                           const gchar          *initial_response,
181
                           gsize                 initial_response_len)
182
0
{
183
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
184
185
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
186
0
  g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
187
188
  //g_debug ("ANONYMOUS: initial_response was '%s'", initial_response);
189
190
0
  m->priv->is_server = TRUE;
191
0
  m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
192
0
}
193
194
static void
195
mechanism_server_data_receive (GDBusAuthMechanism   *mechanism,
196
                               const gchar          *data,
197
                               gsize                 data_len)
198
0
{
199
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
200
201
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
202
0
  g_return_if_fail (m->priv->is_server && !m->priv->is_client);
203
0
  g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
204
205
  /* can never end up here because we are never in the WAITING_FOR_DATA state */
206
0
  g_assert_not_reached ();
207
0
}
208
209
static gchar *
210
mechanism_server_data_send (GDBusAuthMechanism   *mechanism,
211
                            gsize                *out_data_len)
212
0
{
213
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
214
215
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
216
0
  g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
217
0
  g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
218
219
  /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
220
0
  g_assert_not_reached ();
221
222
0
  return NULL;
223
0
}
224
225
static gchar *
226
mechanism_server_or_client_get_reject_reason (GDBusAuthMechanism   *mechanism)
227
0
{
228
0
  GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
229
230
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), 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
/* ---------------------------------------------------------------------------------------------------- */