1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (C) 2009 Codethink Limited
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2 of the licence or (at
9 * your option) any later version.
11 * See the included COPYING file for more information.
13 * Authors: David Zeuthen <davidz@redhat.com>
17 * SECTION:gunixcredentialsmessage
18 * @title: GUnixCredentialsMessage
19 * @short_description: A GSocketControlMessage containing credentials
20 * @include: gio/gunixcredentialsmessage.h
21 * @see_also: #GUnixConnection, #GSocketControlMessage
23 * This #GSocketControlMessage contains a #GCredentials instance. It
24 * may be sent using g_socket_send_message() and received using
25 * g_socket_receive_message() over UNIX sockets (ie: sockets in the
26 * %G_SOCKET_FAMILY_UNIX family).
28 * For an easier way to send and receive credentials over
29 * stream-oriented UNIX sockets, see
30 * g_unix_connection_send_credentials() and
31 * g_unix_connection_receive_credentials(). To receive credentials of
32 * a foreign process connected to a socket, use
33 * g_socket_get_credentials().
38 /* ---------------------------------------------------------------------------------------------------- */
40 #include <sys/types.h>
41 #include <sys/socket.h>
45 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
47 #elif defined(__FreeBSD__)
48 #include <sys/types.h>
49 #include <sys/socket.h>
53 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
55 /* TODO: please add support for your UNIX flavor */
56 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
59 /* ---------------------------------------------------------------------------------------------------- */
64 #include "gunixcredentialsmessage.h"
65 #include "gcredentials.h"
69 struct _GUnixCredentialsMessagePrivate
71 GCredentials *credentials;
80 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
83 g_unix_credentials_message_get_size (GSocketControlMessage *message)
86 return sizeof (struct ucred);
87 #elif defined(__FreeBSD__)
88 return sizeof (struct cmsgcred);
95 g_unix_credentials_message_get_level (GSocketControlMessage *message)
99 #elif defined(__FreeBSD__)
107 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
110 return SCM_CREDENTIALS;
111 #elif defined(__FreeBSD__)
118 static GSocketControlMessage *
119 g_unix_credentials_message_deserialize (gint level,
124 GSocketControlMessage *message;
130 GCredentials *credentials;
133 if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
136 if (size != sizeof (struct ucred))
138 g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
139 "got %" G_GSIZE_FORMAT " bytes of data",
140 sizeof (struct ucred),
147 if (ucred->uid == (uid_t)-1 &&
148 ucred->gid == (gid_t)-1)
150 /* This happens if the remote side didn't pass the credentials */
154 credentials = g_credentials_new ();
155 g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED, ucred);
156 message = g_unix_credentials_message_new_with_credentials (credentials);
157 g_object_unref (credentials);
161 #elif defined(__FreeBSD__)
163 GCredentials *credentials;
164 struct cmsgcred *cred;
166 if (level != SOL_SOCKET || type != SCM_CREDS)
170 if (size < sizeof *cred)
172 g_warning ("Expected a struct cmsgcred (%" G_GSIZE_FORMAT " bytes) but "
173 "got %" G_GSIZE_FORMAT " bytes of data",
174 CMSG_LEN (sizeof *cred),
181 credentials = g_credentials_new ();
182 g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED, cred);
183 message = g_unix_credentials_message_new_with_credentials (credentials);
184 g_object_unref (credentials);
194 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
197 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
200 g_credentials_get_native (message->priv->credentials,
201 G_CREDENTIALS_TYPE_LINUX_UCRED),
202 sizeof (struct ucred));
203 #elif defined(__FreeBSD__)
205 g_credentials_get_native (message->priv->credentials,
206 G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED),
207 sizeof (struct cmsgcred));
213 g_unix_credentials_message_finalize (GObject *object)
215 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
217 if (message->priv->credentials != NULL)
218 g_object_unref (message->priv->credentials);
220 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
224 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
226 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
227 G_TYPE_UNIX_CREDENTIALS_MESSAGE,
228 GUnixCredentialsMessagePrivate);
232 g_unix_credentials_message_get_property (GObject *object,
237 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
241 case PROP_CREDENTIALS:
242 g_value_set_object (value, message->priv->credentials);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 g_unix_credentials_message_set_property (GObject *object,
257 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
261 case PROP_CREDENTIALS:
262 message->priv->credentials = g_value_dup_object (value);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 g_unix_credentials_message_constructed (GObject *object)
274 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
276 if (message->priv->credentials == NULL)
277 message->priv->credentials = g_credentials_new ();
279 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
280 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
284 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
286 GSocketControlMessageClass *scm_class;
287 GObjectClass *gobject_class;
289 g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
291 gobject_class = G_OBJECT_CLASS (class);
292 gobject_class->get_property = g_unix_credentials_message_get_property;
293 gobject_class->set_property = g_unix_credentials_message_set_property;
294 gobject_class->finalize = g_unix_credentials_message_finalize;
295 gobject_class->constructed = g_unix_credentials_message_constructed;
297 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
298 scm_class->get_size = g_unix_credentials_message_get_size;
299 scm_class->get_level = g_unix_credentials_message_get_level;
300 scm_class->get_type = g_unix_credentials_message_get_msg_type;
301 scm_class->serialize = g_unix_credentials_message_serialize;
302 scm_class->deserialize = g_unix_credentials_message_deserialize;
305 * GUnixCredentialsMessage:credentials:
307 * The credentials stored in the message.
311 g_object_class_install_property (gobject_class,
313 g_param_spec_object ("credentials",
315 P_("The credentials stored in the message"),
319 G_PARAM_CONSTRUCT_ONLY |
320 G_PARAM_STATIC_NAME |
321 G_PARAM_STATIC_BLURB |
322 G_PARAM_STATIC_NICK));
326 /* ---------------------------------------------------------------------------------------------------- */
329 * g_unix_credentials_message_is_supported:
331 * Checks if passing #GCredentials on a #GSocket is supported on this platform.
333 * Returns: %TRUE if supported, %FALSE otherwise
338 g_unix_credentials_message_is_supported (void)
340 return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
343 /* ---------------------------------------------------------------------------------------------------- */
346 * g_unix_credentials_message_new:
348 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
350 * Returns: a new #GUnixCredentialsMessage
354 GSocketControlMessage *
355 g_unix_credentials_message_new (void)
357 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
358 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
363 * g_unix_credentials_message_new_with_credentials:
364 * @credentials: A #GCredentials object.
366 * Creates a new #GUnixCredentialsMessage holding @credentials.
368 * Returns: a new #GUnixCredentialsMessage
372 GSocketControlMessage *
373 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
375 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
376 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
377 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
378 "credentials", credentials,
383 * g_unix_credentials_message_get_credentials:
384 * @message: A #GUnixCredentialsMessage.
386 * Gets the credentials stored in @message.
388 * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
393 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
395 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
396 return message->priv->credentials;