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"
61 struct _GUnixCredentialsMessagePrivate
63 GCredentials *credentials;
72 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
75 g_unix_credentials_message_get_size (GSocketControlMessage *message)
78 return sizeof (struct ucred);
85 g_unix_credentials_message_get_level (GSocketControlMessage *message)
91 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
94 return SCM_CREDENTIALS;
100 static GSocketControlMessage *
101 g_unix_credentials_message_deserialize (gint level,
106 GSocketControlMessage *message;
112 GCredentials *credentials;
115 if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
118 if (size != sizeof (struct ucred))
120 g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
121 "got %" G_GSIZE_FORMAT " bytes of data",
122 sizeof (struct ucred),
129 credentials = g_credentials_new ();
130 g_credentials_set_unix_user (credentials, ucred->uid);
131 g_credentials_set_unix_group (credentials, ucred->gid);
132 g_credentials_set_unix_process (credentials, ucred->pid);
133 message = g_unix_credentials_message_new_with_credentials (credentials);
134 g_object_unref (credentials);
144 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
147 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
150 struct ucred *ucred = data;
151 ucred->uid = g_credentials_get_unix_user (message->priv->credentials);
152 ucred->gid = g_credentials_get_unix_group (message->priv->credentials);
153 ucred->pid = g_credentials_get_unix_process (message->priv->credentials);
159 g_unix_credentials_message_finalize (GObject *object)
161 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
163 if (message->priv->credentials != NULL)
164 g_object_unref (message->priv->credentials);
166 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize != NULL)
167 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
171 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
173 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
174 G_TYPE_UNIX_CREDENTIALS_MESSAGE,
175 GUnixCredentialsMessagePrivate);
179 g_unix_credentials_message_get_property (GObject *object,
184 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
188 case PROP_CREDENTIALS:
189 g_value_set_object (value, message->priv->credentials);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 g_unix_credentials_message_set_property (GObject *object,
204 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
208 case PROP_CREDENTIALS:
209 message->priv->credentials = g_value_dup_object (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 g_unix_credentials_message_constructed (GObject *object)
221 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
223 if (message->priv->credentials == NULL)
224 message->priv->credentials = g_credentials_new_for_process ();
226 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
227 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
231 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
233 GSocketControlMessageClass *scm_class;
234 GObjectClass *gobject_class;
236 g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
238 gobject_class = G_OBJECT_CLASS (class);
239 gobject_class->get_property = g_unix_credentials_message_get_property;
240 gobject_class->set_property = g_unix_credentials_message_set_property;
241 gobject_class->finalize = g_unix_credentials_message_finalize;
242 gobject_class->constructed = g_unix_credentials_message_constructed;
244 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
245 scm_class->get_size = g_unix_credentials_message_get_size;
246 scm_class->get_level = g_unix_credentials_message_get_level;
247 scm_class->get_type = g_unix_credentials_message_get_msg_type;
248 scm_class->serialize = g_unix_credentials_message_serialize;
249 scm_class->deserialize = g_unix_credentials_message_deserialize;
252 * GUnixCredentialsMessage:credentials:
254 * The credentials stored in the message.
258 g_object_class_install_property (gobject_class,
260 g_param_spec_object ("credentials",
262 _("The credentials stored in the message"),
266 G_PARAM_CONSTRUCT_ONLY |
267 G_PARAM_STATIC_NAME |
268 G_PARAM_STATIC_BLURB |
269 G_PARAM_STATIC_NICK));
273 /* ---------------------------------------------------------------------------------------------------- */
276 * g_unix_credentials_message_is_supported:
278 * Checks if passing a #GCredential on a #GSocket is supported on this platform.
280 * Returns: %TRUE if supported, %FALSE otherwise
285 g_unix_credentials_message_is_supported (void)
287 return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
290 /* ---------------------------------------------------------------------------------------------------- */
293 * g_unix_credentials_message_new:
295 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
297 * Returns: a new #GUnixCredentialsMessage
301 GSocketControlMessage *
302 g_unix_credentials_message_new (void)
304 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
305 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
310 * g_unix_credentials_message_new:
311 * @credentials: A #GCredentials object.
313 * Creates a new #GUnixCredentialsMessage holding @credentials.
315 * Returns: a new #GUnixCredentialsMessage
319 GSocketControlMessage *
320 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
322 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
323 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
324 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
325 "credentials", credentials,
330 * g_unix_credentials_message_get_credentials:
331 * @message: A #GUnixCredentialsMessage.
333 * Gets the credentials stored in @message.
335 * Returns: A #GCredentials instance. Do not free, it is owned by @message.
340 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
342 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
343 return message->priv->credentials;