Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gdbusauthmechanism.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
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Author: David Zeuthen <davidz@redhat.com>
19
 */
20
21
#include "config.h"
22
23
#include "gdbusauthmechanism.h"
24
#include "gcredentials.h"
25
#include "gdbuserror.h"
26
#include "gioenumtypes.h"
27
#include "giostream.h"
28
29
#include "glibintl.h"
30
31
/* ---------------------------------------------------------------------------------------------------- */
32
33
struct _GDBusAuthMechanismPrivate
34
{
35
  GIOStream *stream;
36
  GCredentials *credentials;
37
};
38
39
enum
40
{
41
  PROP_0,
42
  PROP_STREAM,
43
  PROP_CREDENTIALS
44
};
45
46
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GDBusAuthMechanism, _g_dbus_auth_mechanism, G_TYPE_OBJECT)
47
48
/* ---------------------------------------------------------------------------------------------------- */
49
50
static void
51
_g_dbus_auth_mechanism_finalize (GObject *object)
52
0
{
53
0
  GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
54
55
0
  if (mechanism->priv->stream != NULL)
56
0
    g_object_unref (mechanism->priv->stream);
57
0
  if (mechanism->priv->credentials != NULL)
58
0
    g_object_unref (mechanism->priv->credentials);
59
60
0
  G_OBJECT_CLASS (_g_dbus_auth_mechanism_parent_class)->finalize (object);
61
0
}
62
63
static void
64
_g_dbus_auth_mechanism_get_property (GObject    *object,
65
                                     guint       prop_id,
66
                                     GValue     *value,
67
                                     GParamSpec *pspec)
68
0
{
69
0
  GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
70
71
0
  switch (prop_id)
72
0
    {
73
0
    case PROP_STREAM:
74
0
      g_value_set_object (value, mechanism->priv->stream);
75
0
      break;
76
77
0
    case PROP_CREDENTIALS:
78
0
      g_value_set_object (value, mechanism->priv->credentials);
79
0
      break;
80
81
0
    default:
82
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83
0
      break;
84
0
    }
85
0
}
86
87
static void
88
_g_dbus_auth_mechanism_set_property (GObject      *object,
89
                                     guint         prop_id,
90
                                     const GValue *value,
91
                                     GParamSpec   *pspec)
92
0
{
93
0
  GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
94
95
0
  switch (prop_id)
96
0
    {
97
0
    case PROP_STREAM:
98
0
      mechanism->priv->stream = g_value_dup_object (value);
99
0
      break;
100
101
0
    case PROP_CREDENTIALS:
102
0
      mechanism->priv->credentials = g_value_dup_object (value);
103
0
      break;
104
105
0
    default:
106
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107
0
      break;
108
0
    }
109
0
}
110
111
static void
112
_g_dbus_auth_mechanism_class_init (GDBusAuthMechanismClass *klass)
113
0
{
114
0
  GObjectClass *gobject_class;
115
116
0
  gobject_class = G_OBJECT_CLASS (klass);
117
0
  gobject_class->get_property = _g_dbus_auth_mechanism_get_property;
118
0
  gobject_class->set_property = _g_dbus_auth_mechanism_set_property;
119
0
  gobject_class->finalize     = _g_dbus_auth_mechanism_finalize;
120
121
0
  g_object_class_install_property (gobject_class,
122
0
                                   PROP_STREAM,
123
0
                                   g_param_spec_object ("stream",
124
0
                                                        P_("IO Stream"),
125
0
                                                        P_("The underlying GIOStream used for I/O"),
126
0
                                                        G_TYPE_IO_STREAM,
127
0
                                                        G_PARAM_READABLE |
128
0
                                                        G_PARAM_WRITABLE |
129
0
                                                        G_PARAM_CONSTRUCT_ONLY |
130
0
                                                        G_PARAM_STATIC_NAME |
131
0
                                                        G_PARAM_STATIC_BLURB |
132
0
                                                        G_PARAM_STATIC_NICK));
133
134
  /**
135
   * GDBusAuthMechanism:credentials:
136
   *
137
   * If authenticating as a server, this property contains the
138
   * received credentials, if any.
139
   *
140
   * If authenticating as a client, the property contains the
141
   * credentials that were sent, if any.
142
   */
143
0
  g_object_class_install_property (gobject_class,
144
0
                                   PROP_CREDENTIALS,
145
0
                                   g_param_spec_object ("credentials",
146
0
                                                        P_("Credentials"),
147
0
                                                        P_("The credentials of the remote peer"),
148
0
                                                        G_TYPE_CREDENTIALS,
149
0
                                                        G_PARAM_READABLE |
150
0
                                                        G_PARAM_WRITABLE |
151
0
                                                        G_PARAM_CONSTRUCT_ONLY |
152
0
                                                        G_PARAM_STATIC_NAME |
153
0
                                                        G_PARAM_STATIC_BLURB |
154
0
                                                        G_PARAM_STATIC_NICK));
155
0
}
156
157
static void
158
_g_dbus_auth_mechanism_init (GDBusAuthMechanism *mechanism)
159
0
{
160
0
  mechanism->priv = _g_dbus_auth_mechanism_get_instance_private (mechanism);
161
0
}
162
163
/* ---------------------------------------------------------------------------------------------------- */
164
165
GIOStream *
166
_g_dbus_auth_mechanism_get_stream (GDBusAuthMechanism *mechanism)
167
0
{
168
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
169
0
  return mechanism->priv->stream;
170
0
}
171
172
GCredentials *
173
_g_dbus_auth_mechanism_get_credentials (GDBusAuthMechanism *mechanism)
174
0
{
175
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
176
0
  return mechanism->priv->credentials;
177
0
}
178
179
/* ---------------------------------------------------------------------------------------------------- */
180
181
const gchar *
182
_g_dbus_auth_mechanism_get_name (GType mechanism_type)
183
0
{
184
0
  const gchar *name;
185
0
  GDBusAuthMechanismClass *klass;
186
187
0
  g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), NULL);
188
189
0
  klass = g_type_class_ref (mechanism_type);
190
0
  g_assert (klass != NULL);
191
0
  name = klass->get_name ();
192
  //g_type_class_unref (klass);
193
194
0
  return name;
195
0
}
196
197
gint
198
_g_dbus_auth_mechanism_get_priority (GType mechanism_type)
199
0
{
200
0
  gint priority;
201
0
  GDBusAuthMechanismClass *klass;
202
203
0
  g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), 0);
204
205
0
  klass = g_type_class_ref (mechanism_type);
206
0
  g_assert (klass != NULL);
207
0
  priority = klass->get_priority ();
208
  //g_type_class_unref (klass);
209
210
0
  return priority;
211
0
}
212
213
/* ---------------------------------------------------------------------------------------------------- */
214
215
gboolean
216
_g_dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
217
0
{
218
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), FALSE);
219
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
220
0
}
221
222
gchar *
223
_g_dbus_auth_mechanism_encode_data (GDBusAuthMechanism *mechanism,
224
                                    const gchar        *data,
225
                                    gsize               data_len,
226
                                    gsize              *out_data_len)
227
0
{
228
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
229
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->encode_data (mechanism, data, data_len, out_data_len);
230
0
}
231
232
233
gchar *
234
_g_dbus_auth_mechanism_decode_data (GDBusAuthMechanism *mechanism,
235
                                    const gchar        *data,
236
                                    gsize               data_len,
237
                                    gsize              *out_data_len)
238
0
{
239
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
240
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->decode_data (mechanism, data, data_len, out_data_len);
241
0
}
242
243
/* ---------------------------------------------------------------------------------------------------- */
244
245
GDBusAuthMechanismState
246
_g_dbus_auth_mechanism_server_get_state (GDBusAuthMechanism *mechanism)
247
0
{
248
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
249
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_state (mechanism);
250
0
}
251
252
void
253
_g_dbus_auth_mechanism_server_initiate (GDBusAuthMechanism *mechanism,
254
                                        const gchar        *initial_response,
255
                                        gsize               initial_response_len)
256
0
{
257
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
258
0
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_initiate (mechanism, initial_response, initial_response_len);
259
0
}
260
261
void
262
_g_dbus_auth_mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
263
                                            const gchar        *data,
264
                                            gsize               data_len)
265
0
{
266
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
267
0
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_receive (mechanism, data, data_len);
268
0
}
269
270
gchar *
271
_g_dbus_auth_mechanism_server_data_send (GDBusAuthMechanism *mechanism,
272
                                         gsize              *out_data_len)
273
0
{
274
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
275
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_send (mechanism, out_data_len);
276
0
}
277
278
gchar *
279
_g_dbus_auth_mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
280
0
{
281
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
282
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_reject_reason (mechanism);
283
0
}
284
285
void
286
_g_dbus_auth_mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
287
0
{
288
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
289
0
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_shutdown (mechanism);
290
0
}
291
292
/* ---------------------------------------------------------------------------------------------------- */
293
294
GDBusAuthMechanismState
295
_g_dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
296
0
{
297
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
298
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
299
0
}
300
301
gchar *
302
_g_dbus_auth_mechanism_client_initiate (GDBusAuthMechanism *mechanism,
303
                                        gsize              *out_initial_response_len)
304
0
{
305
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
306
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
307
0
                                                                       out_initial_response_len);
308
0
}
309
310
void
311
_g_dbus_auth_mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
312
                                            const gchar        *data,
313
                                            gsize               data_len)
314
0
{
315
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
316
0
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_receive (mechanism, data, data_len);
317
0
}
318
319
gchar *
320
_g_dbus_auth_mechanism_client_data_send (GDBusAuthMechanism *mechanism,
321
                                         gsize              *out_data_len)
322
0
{
323
0
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
324
0
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_send (mechanism, out_data_len);
325
0
}
326
327
void
328
_g_dbus_auth_mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
329
0
{
330
0
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
331
0
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_shutdown (mechanism);
332
0
}
333
334
/* ---------------------------------------------------------------------------------------------------- */