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 /* ---------------------------------------------------------------------------------------------------- */
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
49 #elif defined(__FreeBSD__)
50 #include <sys/types.h>
51 #include <sys/socket.h>
55 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
57 /* TODO: please add support for your UNIX flavor */
58 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
61 /* ---------------------------------------------------------------------------------------------------- */
66 #include "gunixcredentialsmessage.h"
67 #include "gcredentials.h"
71 struct _GUnixCredentialsMessagePrivate
73 GCredentials *credentials;
82 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
85 g_unix_credentials_message_get_size (GSocketControlMessage *message)
88 return sizeof (struct ucred);
89 #elif defined(__FreeBSD__)
90 return sizeof (struct cmsgcred);
97 g_unix_credentials_message_get_level (GSocketControlMessage *message)
101 #elif defined(__FreeBSD__)
109 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
112 return SCM_CREDENTIALS;
113 #elif defined(__FreeBSD__)
120 static GSocketControlMessage *
121 g_unix_credentials_message_deserialize (gint level,
126 GSocketControlMessage *message;
132 GCredentials *credentials;
135 if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
138 if (size != sizeof (struct ucred))
140 g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
141 "got %" G_GSIZE_FORMAT " bytes of data",
142 sizeof (struct ucred),
149 credentials = g_credentials_new ();
150 g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED, ucred);
151 message = g_unix_credentials_message_new_with_credentials (credentials);
152 g_object_unref (credentials);
156 #elif defined(__FreeBSD__)
158 GCredentials *credentials;
159 struct cmsgcred *cred;
161 if (level != SOL_SOCKET || type != SCM_CREDS)
165 if (size < CMSG_LEN (sizeof *cred))
167 g_warning ("Expected a struct cmsgcred (%" G_GSIZE_FORMAT " bytes) but "
168 "got %" G_GSIZE_FORMAT " bytes of data",
169 CMSG_LEN (sizeof *cred),
176 credentials = g_credentials_new ();
177 g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED, cred);
178 message = g_unix_credentials_message_new_with_credentials (credentials);
179 g_object_unref (credentials);
189 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
192 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
195 g_credentials_get_native (message->priv->credentials,
196 G_CREDENTIALS_TYPE_LINUX_UCRED),
197 sizeof (struct ucred));
198 #elif defined(__FreeBSD__)
200 g_credentials_get_native (message->priv->credentials,
201 G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED),
202 sizeof (struct cmsgcred));
208 g_unix_credentials_message_finalize (GObject *object)
210 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
212 if (message->priv->credentials != NULL)
213 g_object_unref (message->priv->credentials);
215 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
219 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
221 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
222 G_TYPE_UNIX_CREDENTIALS_MESSAGE,
223 GUnixCredentialsMessagePrivate);
227 g_unix_credentials_message_get_property (GObject *object,
232 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
236 case PROP_CREDENTIALS:
237 g_value_set_object (value, message->priv->credentials);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 g_unix_credentials_message_set_property (GObject *object,
252 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
256 case PROP_CREDENTIALS:
257 message->priv->credentials = g_value_dup_object (value);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 g_unix_credentials_message_constructed (GObject *object)
269 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
271 if (message->priv->credentials == NULL)
272 message->priv->credentials = g_credentials_new ();
274 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
275 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
279 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
281 GSocketControlMessageClass *scm_class;
282 GObjectClass *gobject_class;
284 g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
286 gobject_class = G_OBJECT_CLASS (class);
287 gobject_class->get_property = g_unix_credentials_message_get_property;
288 gobject_class->set_property = g_unix_credentials_message_set_property;
289 gobject_class->finalize = g_unix_credentials_message_finalize;
290 gobject_class->constructed = g_unix_credentials_message_constructed;
292 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
293 scm_class->get_size = g_unix_credentials_message_get_size;
294 scm_class->get_level = g_unix_credentials_message_get_level;
295 scm_class->get_type = g_unix_credentials_message_get_msg_type;
296 scm_class->serialize = g_unix_credentials_message_serialize;
297 scm_class->deserialize = g_unix_credentials_message_deserialize;
300 * GUnixCredentialsMessage:credentials:
302 * The credentials stored in the message.
306 g_object_class_install_property (gobject_class,
308 g_param_spec_object ("credentials",
310 P_("The credentials stored in the message"),
314 G_PARAM_CONSTRUCT_ONLY |
315 G_PARAM_STATIC_NAME |
316 G_PARAM_STATIC_BLURB |
317 G_PARAM_STATIC_NICK));
321 /* ---------------------------------------------------------------------------------------------------- */
324 * g_unix_credentials_message_is_supported:
326 * Checks if passing a #GCredential on a #GSocket is supported on this platform.
328 * Returns: %TRUE if supported, %FALSE otherwise
333 g_unix_credentials_message_is_supported (void)
335 return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
338 /* ---------------------------------------------------------------------------------------------------- */
341 * g_unix_credentials_message_new:
343 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
345 * Returns: a new #GUnixCredentialsMessage
349 GSocketControlMessage *
350 g_unix_credentials_message_new (void)
352 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
353 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
358 * g_unix_credentials_message_new_with_credentials:
359 * @credentials: A #GCredentials object.
361 * Creates a new #GUnixCredentialsMessage holding @credentials.
363 * Returns: a new #GUnixCredentialsMessage
367 GSocketControlMessage *
368 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
370 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
371 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
372 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
373 "credentials", credentials,
378 * g_unix_credentials_message_get_credentials:
379 * @message: A #GUnixCredentialsMessage.
381 * Gets the credentials stored in @message.
383 * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
388 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
390 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
391 return message->priv->credentials;