Coverage Report

Created: 2025-06-13 06:20

/src/glib/gio/gunixcredentialsmessage.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright (C) 2010 Red Hat, Inc.
4
 * Copyright (C) 2009 Codethink Limited
5
 *
6
 * SPDX-License-Identifier: LGPL-2.1-or-later
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * See the included COPYING file for more information.
14
 *
15
 * Authors: David Zeuthen <davidz@redhat.com>
16
 */
17
18
/**
19
 * GUnixCredentialsMessage:
20
 *
21
 * This [class@Gio.SocketControlMessage] contains a [class@Gio.Credentials]
22
 * instance.  It may be sent using [method@Gio.Socket.send_message] and received
23
 * using [method@Gio.Socket.receive_message] over UNIX sockets (ie: sockets in
24
 * the `G_SOCKET_FAMILY_UNIX` family).
25
 *
26
 * For an easier way to send and receive credentials over
27
 * stream-oriented UNIX sockets, see
28
 * [method@Gio.UnixConnection.send_credentials] and
29
 * [method@Gio.UnixConnection.receive_credentials]. To receive credentials of
30
 * a foreign process connected to a socket, use
31
 * [method@Gio.Socket.get_credentials].
32
 *
33
 * Since GLib 2.72, `GUnixCredentialMessage` is available on all platforms. It
34
 * requires underlying system support (such as Windows 10 with `AF_UNIX`) at run
35
 * time.
36
 *
37
 * Before GLib 2.72, `<gio/gunixcredentialsmessage.h>` belonged to the UNIX-specific
38
 * GIO interfaces, thus you had to use the `gio-unix-2.0.pc` pkg-config file
39
 * when using it. This is no longer necessary since GLib 2.72.
40
 *
41
 * Since: 2.26
42
 */
43
44
#include "config.h"
45
46
/* ---------------------------------------------------------------------------------------------------- */
47
48
#include <fcntl.h>
49
#include <errno.h>
50
#include <string.h>
51
#ifdef HAVE_UNISTD_H
52
#include <unistd.h>
53
#endif
54
55
#include "gunixcredentialsmessage.h"
56
#include "gcredentials.h"
57
#include "gcredentialsprivate.h"
58
#include "gnetworking.h"
59
60
#include "glibintl.h"
61
62
struct _GUnixCredentialsMessagePrivate
63
{
64
  GCredentials *credentials;
65
};
66
67
enum
68
{
69
  PROP_0,
70
  PROP_CREDENTIALS
71
};
72
73
G_DEFINE_TYPE_WITH_PRIVATE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
74
75
static gsize
76
g_unix_credentials_message_get_size (GSocketControlMessage *message)
77
0
{
78
0
#if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
79
0
  return G_CREDENTIALS_NATIVE_SIZE;
80
#else
81
  return 0;
82
#endif
83
0
}
84
85
static int
86
g_unix_credentials_message_get_level (GSocketControlMessage *message)
87
0
{
88
0
#if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
89
0
  return SOL_SOCKET;
90
#else
91
  return 0;
92
#endif
93
0
}
94
95
static int
96
g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
97
0
{
98
0
#if G_CREDENTIALS_USE_LINUX_UCRED
99
0
  return SCM_CREDENTIALS;
100
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
101
  return SCM_CREDS;
102
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
103
  return SCM_CREDS;
104
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
105
  return SCM_UCRED;
106
#elif G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
107
  #error "G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED is set but there is no msg_type defined for this platform"
108
#else
109
  /* includes G_CREDENTIALS_USE_APPLE_XUCRED */
110
  return 0;
111
#endif
112
0
}
113
114
static GSocketControlMessage *
115
g_unix_credentials_message_deserialize (gint     level,
116
                                        gint     type,
117
                                        gsize    size,
118
                                        gpointer data)
119
0
{
120
0
#if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
121
0
  GSocketControlMessage *message;
122
0
  GCredentials *credentials;
123
124
0
  if (level != SOL_SOCKET || type != g_unix_credentials_message_get_msg_type (NULL))
125
0
    return NULL;
126
127
0
  if (size != G_CREDENTIALS_NATIVE_SIZE)
128
0
    {
129
0
      g_warning ("Expected a credentials struct of %" G_GSIZE_FORMAT " bytes but "
130
0
                 "got %" G_GSIZE_FORMAT " bytes of data",
131
0
                 G_CREDENTIALS_NATIVE_SIZE, size);
132
0
      return NULL;
133
0
    }
134
135
0
  credentials = g_credentials_new ();
136
0
  g_credentials_set_native (credentials, G_CREDENTIALS_NATIVE_TYPE, data);
137
138
0
  if (g_credentials_get_unix_user (credentials, NULL) == (uid_t) -1)
139
0
    {
140
      /* This happens on Linux if the remote side didn't pass the credentials */
141
0
      g_object_unref (credentials);
142
0
      return NULL;
143
0
    }
144
145
0
  message = g_unix_credentials_message_new_with_credentials (credentials);
146
0
  g_object_unref (credentials);
147
148
0
  return message;
149
150
#else /* !G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED */
151
152
  return NULL;
153
#endif
154
0
}
155
156
static void
157
g_unix_credentials_message_serialize (GSocketControlMessage *_message,
158
                                      gpointer               data)
159
0
{
160
0
#if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
161
0
  GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
162
163
0
  memcpy (data,
164
0
          g_credentials_get_native (message->priv->credentials,
165
0
                                    G_CREDENTIALS_NATIVE_TYPE),
166
0
          G_CREDENTIALS_NATIVE_SIZE);
167
0
#endif
168
0
}
169
170
static void
171
g_unix_credentials_message_finalize (GObject *object)
172
0
{
173
0
  GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
174
175
0
  if (message->priv->credentials != NULL)
176
0
    g_object_unref (message->priv->credentials);
177
178
0
  G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
179
0
}
180
181
static void
182
g_unix_credentials_message_init (GUnixCredentialsMessage *message)
183
0
{
184
0
  message->priv = g_unix_credentials_message_get_instance_private (message);
185
0
}
186
187
static void
188
g_unix_credentials_message_get_property (GObject    *object,
189
                                         guint       prop_id,
190
                                         GValue     *value,
191
                                         GParamSpec *pspec)
192
0
{
193
0
  GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
194
195
0
  switch (prop_id)
196
0
    {
197
0
    case PROP_CREDENTIALS:
198
0
      g_value_set_object (value, message->priv->credentials);
199
0
      break;
200
201
0
    default:
202
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203
0
      break;
204
0
    }
205
0
}
206
207
static void
208
g_unix_credentials_message_set_property (GObject      *object,
209
                                         guint         prop_id,
210
                                         const GValue *value,
211
                                         GParamSpec   *pspec)
212
0
{
213
0
  GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
214
215
0
  switch (prop_id)
216
0
    {
217
0
    case PROP_CREDENTIALS:
218
0
      message->priv->credentials = g_value_dup_object (value);
219
0
      break;
220
221
0
    default:
222
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223
0
      break;
224
0
    }
225
0
}
226
227
static void
228
g_unix_credentials_message_constructed (GObject *object)
229
0
{
230
0
  GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
231
232
0
  if (message->priv->credentials == NULL)
233
0
    message->priv->credentials = g_credentials_new ();
234
235
0
  if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
236
0
    G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
237
0
}
238
239
static void
240
g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
241
0
{
242
0
  GSocketControlMessageClass *scm_class;
243
0
  GObjectClass *gobject_class;
244
245
0
  gobject_class = G_OBJECT_CLASS (class);
246
0
  gobject_class->get_property = g_unix_credentials_message_get_property;
247
0
  gobject_class->set_property = g_unix_credentials_message_set_property;
248
0
  gobject_class->finalize = g_unix_credentials_message_finalize;
249
0
  gobject_class->constructed = g_unix_credentials_message_constructed;
250
251
0
  scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
252
0
  scm_class->get_size = g_unix_credentials_message_get_size;
253
0
  scm_class->get_level = g_unix_credentials_message_get_level;
254
0
  scm_class->get_type = g_unix_credentials_message_get_msg_type;
255
0
  scm_class->serialize = g_unix_credentials_message_serialize;
256
0
  scm_class->deserialize = g_unix_credentials_message_deserialize;
257
258
  /**
259
   * GUnixCredentialsMessage:credentials:
260
   *
261
   * The credentials stored in the message.
262
   *
263
   * Since: 2.26
264
   */
265
0
  g_object_class_install_property (gobject_class,
266
0
                                   PROP_CREDENTIALS,
267
0
                                   g_param_spec_object ("credentials", NULL, NULL,
268
0
                                                        G_TYPE_CREDENTIALS,
269
0
                                                        G_PARAM_READABLE |
270
0
                                                        G_PARAM_WRITABLE |
271
0
                                                        G_PARAM_CONSTRUCT_ONLY |
272
0
                                                        G_PARAM_STATIC_NAME |
273
0
                                                        G_PARAM_STATIC_BLURB |
274
0
                                                        G_PARAM_STATIC_NICK));
275
276
0
}
277
278
/* ---------------------------------------------------------------------------------------------------- */
279
280
/**
281
 * g_unix_credentials_message_is_supported:
282
 *
283
 * Checks if passing #GCredentials on a #GSocket is supported on this platform.
284
 *
285
 * Returns: %TRUE if supported, %FALSE otherwise
286
 *
287
 * Since: 2.26
288
 */
289
gboolean
290
g_unix_credentials_message_is_supported (void)
291
0
{
292
0
#if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
293
0
  return TRUE;
294
#else
295
  return FALSE;
296
#endif
297
0
}
298
299
/* ---------------------------------------------------------------------------------------------------- */
300
301
/**
302
 * g_unix_credentials_message_new:
303
 *
304
 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
305
 *
306
 * Returns: a new #GUnixCredentialsMessage
307
 *
308
 * Since: 2.26
309
 */
310
GSocketControlMessage *
311
g_unix_credentials_message_new (void)
312
0
{
313
0
  g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
314
0
  return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
315
0
                       NULL);
316
0
}
317
318
/**
319
 * g_unix_credentials_message_new_with_credentials:
320
 * @credentials: A #GCredentials object.
321
 *
322
 * Creates a new #GUnixCredentialsMessage holding @credentials.
323
 *
324
 * Returns: a new #GUnixCredentialsMessage
325
 *
326
 * Since: 2.26
327
 */
328
GSocketControlMessage *
329
g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
330
0
{
331
0
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
332
0
  g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
333
0
  return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
334
0
                       "credentials", credentials,
335
0
                       NULL);
336
0
}
337
338
/**
339
 * g_unix_credentials_message_get_credentials:
340
 * @message: A #GUnixCredentialsMessage.
341
 *
342
 * Gets the credentials stored in @message.
343
 *
344
 * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
345
 *
346
 * Since: 2.26
347
 */
348
GCredentials *
349
g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
350
0
{
351
0
  g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
352
0
  return message->priv->credentials;
353
0
}