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_ADDRESS_UNIX family).
28 * For an easier way to send and receive credentials over
29 * stream-oriented UNIX sockets, see g_unix_connection_send_credentials() and
30 * g_unix_connection_receive_credentials().
35 /* ---------------------------------------------------------------------------------------------------- */
39 #include <sys/types.h>
40 #include <sys/socket.h>
44 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
47 /* TODO: please add support for your UNIX flavor */
48 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
51 /* ---------------------------------------------------------------------------------------------------- */
56 #include "gunixcredentialsmessage.h"
57 #include "gcredentials.h"
62 struct _GUnixCredentialsMessagePrivate
64 GCredentials *credentials;
73 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
76 g_unix_credentials_message_get_size (GSocketControlMessage *message)
79 return sizeof (struct ucred);
86 g_unix_credentials_message_get_level (GSocketControlMessage *message)
96 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
99 return SCM_CREDENTIALS;
105 static GSocketControlMessage *
106 g_unix_credentials_message_deserialize (gint level,
111 GSocketControlMessage *message;
117 GCredentials *credentials;
120 if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
123 if (size != sizeof (struct ucred))
125 g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
126 "got %" G_GSIZE_FORMAT " bytes of data",
127 sizeof (struct ucred),
134 credentials = g_credentials_new ();
135 g_credentials_set_native (credentials, ucred);
136 message = g_unix_credentials_message_new_with_credentials (credentials);
137 g_object_unref (credentials);
147 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
150 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
152 memcpy (data, g_credentials_get_native (message->priv->credentials), sizeof (struct ucred));
157 g_unix_credentials_message_finalize (GObject *object)
159 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
161 if (message->priv->credentials != NULL)
162 g_object_unref (message->priv->credentials);
164 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
168 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
170 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
171 G_TYPE_UNIX_CREDENTIALS_MESSAGE,
172 GUnixCredentialsMessagePrivate);
176 g_unix_credentials_message_get_property (GObject *object,
181 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
185 case PROP_CREDENTIALS:
186 g_value_set_object (value, message->priv->credentials);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 g_unix_credentials_message_set_property (GObject *object,
201 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
205 case PROP_CREDENTIALS:
206 message->priv->credentials = g_value_dup_object (value);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 g_unix_credentials_message_constructed (GObject *object)
218 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
220 if (message->priv->credentials == NULL)
221 message->priv->credentials = g_credentials_new ();
223 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
224 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
228 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
230 GSocketControlMessageClass *scm_class;
231 GObjectClass *gobject_class;
233 g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
235 gobject_class = G_OBJECT_CLASS (class);
236 gobject_class->get_property = g_unix_credentials_message_get_property;
237 gobject_class->set_property = g_unix_credentials_message_set_property;
238 gobject_class->finalize = g_unix_credentials_message_finalize;
239 gobject_class->constructed = g_unix_credentials_message_constructed;
241 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
242 scm_class->get_size = g_unix_credentials_message_get_size;
243 scm_class->get_level = g_unix_credentials_message_get_level;
244 scm_class->get_type = g_unix_credentials_message_get_msg_type;
245 scm_class->serialize = g_unix_credentials_message_serialize;
246 scm_class->deserialize = g_unix_credentials_message_deserialize;
249 * GUnixCredentialsMessage:credentials:
251 * The credentials stored in the message.
255 g_object_class_install_property (gobject_class,
257 g_param_spec_object ("credentials",
259 P_("The credentials stored in the message"),
263 G_PARAM_CONSTRUCT_ONLY |
264 G_PARAM_STATIC_NAME |
265 G_PARAM_STATIC_BLURB |
266 G_PARAM_STATIC_NICK));
270 /* ---------------------------------------------------------------------------------------------------- */
273 * g_unix_credentials_message_is_supported:
275 * Checks if passing a #GCredential on a #GSocket is supported on this platform.
277 * Returns: %TRUE if supported, %FALSE otherwise
282 g_unix_credentials_message_is_supported (void)
284 return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
287 /* ---------------------------------------------------------------------------------------------------- */
290 * g_unix_credentials_message_new:
292 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
294 * Returns: a new #GUnixCredentialsMessage
298 GSocketControlMessage *
299 g_unix_credentials_message_new (void)
301 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
302 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
307 * g_unix_credentials_message_new_with_credentials:
308 * @credentials: A #GCredentials object.
310 * Creates a new #GUnixCredentialsMessage holding @credentials.
312 * Returns: a new #GUnixCredentialsMessage
316 GSocketControlMessage *
317 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
319 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
320 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
321 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
322 "credentials", credentials,
327 * g_unix_credentials_message_get_credentials:
328 * @message: A #GUnixCredentialsMessage.
330 * Gets the credentials stored in @message.
332 * Returns: A #GCredentials instance. Do not free, it is owned by @message.
337 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
339 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
340 return message->priv->credentials;
344 #define __G_UNIX_CREDENTIALS_MESSAGE_C__
345 #include "gioaliasdef.c"