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 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
41 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
42 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
44 /* TODO: please add support for your UNIX flavor */
45 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
48 /* ---------------------------------------------------------------------------------------------------- */
55 #include "gunixcredentialsmessage.h"
56 #include "gcredentials.h"
57 #include "gnetworking.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);
79 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
80 return sizeof (struct cmsgcred);
87 g_unix_credentials_message_get_level (GSocketControlMessage *message)
91 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
99 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
102 return SCM_CREDENTIALS;
103 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
110 static GSocketControlMessage *
111 g_unix_credentials_message_deserialize (gint level,
116 GSocketControlMessage *message;
122 GCredentials *credentials;
125 if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
128 if (size != sizeof (struct ucred))
130 g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
131 "got %" G_GSIZE_FORMAT " bytes of data",
132 sizeof (struct ucred),
139 if (ucred->uid == (uid_t)-1 &&
140 ucred->gid == (gid_t)-1)
142 /* This happens if the remote side didn't pass the credentials */
146 credentials = g_credentials_new ();
147 g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED, ucred);
148 message = g_unix_credentials_message_new_with_credentials (credentials);
149 g_object_unref (credentials);
153 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
155 GCredentials *credentials;
156 struct cmsgcred *cred;
158 if (level != SOL_SOCKET || type != SCM_CREDS)
162 if (size < sizeof *cred)
164 g_warning ("Expected a struct cmsgcred (%" G_GSIZE_FORMAT " bytes) but "
165 "got %" G_GSIZE_FORMAT " bytes of data",
166 CMSG_LEN (sizeof *cred),
173 credentials = g_credentials_new ();
174 g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED, cred);
175 message = g_unix_credentials_message_new_with_credentials (credentials);
176 g_object_unref (credentials);
186 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
189 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
192 g_credentials_get_native (message->priv->credentials,
193 G_CREDENTIALS_TYPE_LINUX_UCRED),
194 sizeof (struct ucred));
195 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
197 g_credentials_get_native (message->priv->credentials,
198 G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED),
199 sizeof (struct cmsgcred));
205 g_unix_credentials_message_finalize (GObject *object)
207 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
209 if (message->priv->credentials != NULL)
210 g_object_unref (message->priv->credentials);
212 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
216 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
218 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
219 G_TYPE_UNIX_CREDENTIALS_MESSAGE,
220 GUnixCredentialsMessagePrivate);
224 g_unix_credentials_message_get_property (GObject *object,
229 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
233 case PROP_CREDENTIALS:
234 g_value_set_object (value, message->priv->credentials);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 g_unix_credentials_message_set_property (GObject *object,
249 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
253 case PROP_CREDENTIALS:
254 message->priv->credentials = g_value_dup_object (value);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 g_unix_credentials_message_constructed (GObject *object)
266 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
268 if (message->priv->credentials == NULL)
269 message->priv->credentials = g_credentials_new ();
271 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
272 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
276 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
278 GSocketControlMessageClass *scm_class;
279 GObjectClass *gobject_class;
281 g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
283 gobject_class = G_OBJECT_CLASS (class);
284 gobject_class->get_property = g_unix_credentials_message_get_property;
285 gobject_class->set_property = g_unix_credentials_message_set_property;
286 gobject_class->finalize = g_unix_credentials_message_finalize;
287 gobject_class->constructed = g_unix_credentials_message_constructed;
289 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
290 scm_class->get_size = g_unix_credentials_message_get_size;
291 scm_class->get_level = g_unix_credentials_message_get_level;
292 scm_class->get_type = g_unix_credentials_message_get_msg_type;
293 scm_class->serialize = g_unix_credentials_message_serialize;
294 scm_class->deserialize = g_unix_credentials_message_deserialize;
297 * GUnixCredentialsMessage:credentials:
299 * The credentials stored in the message.
303 g_object_class_install_property (gobject_class,
305 g_param_spec_object ("credentials",
307 P_("The credentials stored in the message"),
311 G_PARAM_CONSTRUCT_ONLY |
312 G_PARAM_STATIC_NAME |
313 G_PARAM_STATIC_BLURB |
314 G_PARAM_STATIC_NICK));
318 /* ---------------------------------------------------------------------------------------------------- */
321 * g_unix_credentials_message_is_supported:
323 * Checks if passing #GCredentials on a #GSocket is supported on this platform.
325 * Returns: %TRUE if supported, %FALSE otherwise
330 g_unix_credentials_message_is_supported (void)
332 return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
335 /* ---------------------------------------------------------------------------------------------------- */
338 * g_unix_credentials_message_new:
340 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
342 * Returns: a new #GUnixCredentialsMessage
346 GSocketControlMessage *
347 g_unix_credentials_message_new (void)
349 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
350 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
355 * g_unix_credentials_message_new_with_credentials:
356 * @credentials: A #GCredentials object.
358 * Creates a new #GUnixCredentialsMessage holding @credentials.
360 * Returns: a new #GUnixCredentialsMessage
364 GSocketControlMessage *
365 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
367 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
368 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
369 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
370 "credentials", credentials,
375 * g_unix_credentials_message_get_credentials:
376 * @message: A #GUnixCredentialsMessage.
378 * Gets the credentials stored in @message.
380 * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
385 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
387 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
388 return message->priv->credentials;