1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (C) 2009 Codethink Limited
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at 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 /* ---------------------------------------------------------------------------------------------------- */
45 #include "gunixcredentialsmessage.h"
46 #include "gcredentials.h"
47 #include "gcredentialsprivate.h"
48 #include "gnetworking.h"
52 struct _GUnixCredentialsMessagePrivate
54 GCredentials
*credentials
;
63 G_DEFINE_TYPE_WITH_PRIVATE (GUnixCredentialsMessage
, g_unix_credentials_message
, G_TYPE_SOCKET_CONTROL_MESSAGE
)
66 g_unix_credentials_message_get_size (GSocketControlMessage
*message
)
68 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
69 return G_CREDENTIALS_NATIVE_SIZE
;
76 g_unix_credentials_message_get_level (GSocketControlMessage
*message
)
78 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
86 g_unix_credentials_message_get_msg_type (GSocketControlMessage
*message
)
88 #if G_CREDENTIALS_USE_LINUX_UCRED
89 return SCM_CREDENTIALS
;
90 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
92 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
94 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
96 #elif G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
97 #error "G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED is set but there is no msg_type defined for this platform"
103 static GSocketControlMessage
*
104 g_unix_credentials_message_deserialize (gint level
,
109 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
110 GSocketControlMessage
*message
;
111 GCredentials
*credentials
;
113 if (level
!= SOL_SOCKET
|| type
!= g_unix_credentials_message_get_msg_type (NULL
))
116 if (size
!= G_CREDENTIALS_NATIVE_SIZE
)
118 g_warning ("Expected a credentials struct of %" G_GSIZE_FORMAT
" bytes but "
119 "got %" G_GSIZE_FORMAT
" bytes of data",
120 G_CREDENTIALS_NATIVE_SIZE
, size
);
124 credentials
= g_credentials_new ();
125 g_credentials_set_native (credentials
, G_CREDENTIALS_NATIVE_TYPE
, data
);
127 if (g_credentials_get_unix_user (credentials
, NULL
) == (uid_t
) -1)
129 /* This happens on Linux if the remote side didn't pass the credentials */
130 g_object_unref (credentials
);
134 message
= g_unix_credentials_message_new_with_credentials (credentials
);
135 g_object_unref (credentials
);
139 #else /* !G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED */
146 g_unix_credentials_message_serialize (GSocketControlMessage
*_message
,
149 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
150 GUnixCredentialsMessage
*message
= G_UNIX_CREDENTIALS_MESSAGE (_message
);
153 g_credentials_get_native (message
->priv
->credentials
,
154 G_CREDENTIALS_NATIVE_TYPE
),
155 G_CREDENTIALS_NATIVE_SIZE
);
160 g_unix_credentials_message_finalize (GObject
*object
)
162 GUnixCredentialsMessage
*message
= G_UNIX_CREDENTIALS_MESSAGE (object
);
164 if (message
->priv
->credentials
!= NULL
)
165 g_object_unref (message
->priv
->credentials
);
167 G_OBJECT_CLASS (g_unix_credentials_message_parent_class
)->finalize (object
);
171 g_unix_credentials_message_init (GUnixCredentialsMessage
*message
)
173 message
->priv
= g_unix_credentials_message_get_instance_private (message
);
177 g_unix_credentials_message_get_property (GObject
*object
,
182 GUnixCredentialsMessage
*message
= G_UNIX_CREDENTIALS_MESSAGE (object
);
186 case PROP_CREDENTIALS
:
187 g_value_set_object (value
, message
->priv
->credentials
);
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
197 g_unix_credentials_message_set_property (GObject
*object
,
202 GUnixCredentialsMessage
*message
= G_UNIX_CREDENTIALS_MESSAGE (object
);
206 case PROP_CREDENTIALS
:
207 message
->priv
->credentials
= g_value_dup_object (value
);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
217 g_unix_credentials_message_constructed (GObject
*object
)
219 GUnixCredentialsMessage
*message
= G_UNIX_CREDENTIALS_MESSAGE (object
);
221 if (message
->priv
->credentials
== NULL
)
222 message
->priv
->credentials
= g_credentials_new ();
224 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class
)->constructed
!= NULL
)
225 G_OBJECT_CLASS (g_unix_credentials_message_parent_class
)->constructed (object
);
229 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass
*class)
231 GSocketControlMessageClass
*scm_class
;
232 GObjectClass
*gobject_class
;
234 gobject_class
= G_OBJECT_CLASS (class);
235 gobject_class
->get_property
= g_unix_credentials_message_get_property
;
236 gobject_class
->set_property
= g_unix_credentials_message_set_property
;
237 gobject_class
->finalize
= g_unix_credentials_message_finalize
;
238 gobject_class
->constructed
= g_unix_credentials_message_constructed
;
240 scm_class
= G_SOCKET_CONTROL_MESSAGE_CLASS (class);
241 scm_class
->get_size
= g_unix_credentials_message_get_size
;
242 scm_class
->get_level
= g_unix_credentials_message_get_level
;
243 scm_class
->get_type
= g_unix_credentials_message_get_msg_type
;
244 scm_class
->serialize
= g_unix_credentials_message_serialize
;
245 scm_class
->deserialize
= g_unix_credentials_message_deserialize
;
248 * GUnixCredentialsMessage:credentials:
250 * The credentials stored in the message.
254 g_object_class_install_property (gobject_class
,
256 g_param_spec_object ("credentials",
258 P_("The credentials stored in the message"),
262 G_PARAM_CONSTRUCT_ONLY
|
263 G_PARAM_STATIC_NAME
|
264 G_PARAM_STATIC_BLURB
|
265 G_PARAM_STATIC_NICK
));
269 /* ---------------------------------------------------------------------------------------------------- */
272 * g_unix_credentials_message_is_supported:
274 * Checks if passing #GCredentials on a #GSocket is supported on this platform.
276 * Returns: %TRUE if supported, %FALSE otherwise
281 g_unix_credentials_message_is_supported (void)
283 #if G_CREDENTIALS_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_with_credentials:
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: (transfer none): 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
;