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 * @see_also: #GUnixConnection, #GSocketControlMessage
22 * This #GSocketControlMessage contains a #GCredentials instance. It
23 * may be sent using g_socket_send_message() and received using
24 * g_socket_receive_message() over UNIX sockets (ie: sockets in the
25 * %G_SOCKET_ADDRESS_UNIX family).
27 * For an easier way to send and receive credentials over
28 * stream-oriented UNIX sockets, see g_unix_connection_send_credentials() and
29 * g_unix_connection_receive_credentials().
34 /* ---------------------------------------------------------------------------------------------------- */
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)
92 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
95 return SCM_CREDENTIALS;
101 static GSocketControlMessage *
102 g_unix_credentials_message_deserialize (gint level,
107 GSocketControlMessage *message;
113 GCredentials *credentials;
116 if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
119 if (size != sizeof (struct ucred))
121 g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
122 "got %" G_GSIZE_FORMAT " bytes of data",
123 sizeof (struct ucred),
130 credentials = g_credentials_new ();
131 g_credentials_set_native (credentials, ucred);
132 message = g_unix_credentials_message_new_with_credentials (credentials);
133 g_object_unref (credentials);
143 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
146 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
148 memcpy (data, g_credentials_get_native (message->priv->credentials), sizeof (struct ucred));
153 g_unix_credentials_message_finalize (GObject *object)
155 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
157 if (message->priv->credentials != NULL)
158 g_object_unref (message->priv->credentials);
160 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
164 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
166 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
167 G_TYPE_UNIX_CREDENTIALS_MESSAGE,
168 GUnixCredentialsMessagePrivate);
172 g_unix_credentials_message_get_property (GObject *object,
177 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
181 case PROP_CREDENTIALS:
182 g_value_set_object (value, message->priv->credentials);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 g_unix_credentials_message_set_property (GObject *object,
197 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
201 case PROP_CREDENTIALS:
202 message->priv->credentials = g_value_dup_object (value);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 g_unix_credentials_message_constructed (GObject *object)
214 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
216 if (message->priv->credentials == NULL)
217 message->priv->credentials = g_credentials_new ();
219 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
220 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
224 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
226 GSocketControlMessageClass *scm_class;
227 GObjectClass *gobject_class;
229 g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
231 gobject_class = G_OBJECT_CLASS (class);
232 gobject_class->get_property = g_unix_credentials_message_get_property;
233 gobject_class->set_property = g_unix_credentials_message_set_property;
234 gobject_class->finalize = g_unix_credentials_message_finalize;
235 gobject_class->constructed = g_unix_credentials_message_constructed;
237 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
238 scm_class->get_size = g_unix_credentials_message_get_size;
239 scm_class->get_level = g_unix_credentials_message_get_level;
240 scm_class->get_type = g_unix_credentials_message_get_msg_type;
241 scm_class->serialize = g_unix_credentials_message_serialize;
242 scm_class->deserialize = g_unix_credentials_message_deserialize;
245 * GUnixCredentialsMessage:credentials:
247 * The credentials stored in the message.
251 g_object_class_install_property (gobject_class,
253 g_param_spec_object ("credentials",
255 P_("The credentials stored in the message"),
259 G_PARAM_CONSTRUCT_ONLY |
260 G_PARAM_STATIC_NAME |
261 G_PARAM_STATIC_BLURB |
262 G_PARAM_STATIC_NICK));
266 /* ---------------------------------------------------------------------------------------------------- */
269 * g_unix_credentials_message_is_supported:
271 * Checks if passing a #GCredential on a #GSocket is supported on this platform.
273 * Returns: %TRUE if supported, %FALSE otherwise
278 g_unix_credentials_message_is_supported (void)
280 return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
283 /* ---------------------------------------------------------------------------------------------------- */
286 * g_unix_credentials_message_new:
288 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
290 * Returns: a new #GUnixCredentialsMessage
294 GSocketControlMessage *
295 g_unix_credentials_message_new (void)
297 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
298 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
303 * g_unix_credentials_message_new:
304 * @credentials: A #GCredentials object.
306 * Creates a new #GUnixCredentialsMessage holding @credentials.
308 * Returns: a new #GUnixCredentialsMessage
312 GSocketControlMessage *
313 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
315 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
316 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
317 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
318 "credentials", credentials,
323 * g_unix_credentials_message_get_credentials:
324 * @message: A #GUnixCredentialsMessage.
326 * Gets the credentials stored in @message.
328 * Returns: A #GCredentials instance. Do not free, it is owned by @message.
333 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
335 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
336 return message->priv->credentials;
340 #define __G_UNIX_CREDENTIALS_MESSAGE_C__
341 #include "gioaliasdef.c"