1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
29 #include <sys/types.h>
30 #include <sys/socket.h>
35 #include <gobject/gvaluecollector.h>
37 #include "gcredentials.h"
43 * SECTION:gcredentials
44 * @short_description: An object containing credentials
47 * The #GCredentials type is a reference-counted wrapper for the
48 * native credentials type. This information is typically used for
49 * identifying, authenticating and authorizing other processes.
51 * Some operating systems supports looking up the credentials of the
52 * remote peer of a communication endpoint - see e.g.
53 * g_socket_get_credentials().
55 * Some operating systems supports securely sending and receiving
56 * credentials over a Unix Domain Socket, see
57 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
58 * g_unix_connection_receive_credentials() for details.
60 * On Linux, the native credential type is a <literal>struct ucred</literal> - see
61 * the <literal>unix(7)</literal> man page for details.
64 struct _GCredentialsPrivate
70 #warning Please add GCredentials support for your OS
76 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
79 g_credentials_finalize (GObject *object)
81 G_GNUC_UNUSED GCredentials *credentials = G_CREDENTIALS (object);
83 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
84 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
89 g_credentials_class_init (GCredentialsClass *klass)
91 GObjectClass *gobject_class;
93 g_type_class_add_private (klass, sizeof (GCredentialsPrivate));
95 gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->finalize = g_credentials_finalize;
100 g_credentials_init (GCredentials *credentials)
102 credentials->priv = G_TYPE_INSTANCE_GET_PRIVATE (credentials, G_TYPE_CREDENTIALS, GCredentialsPrivate);
104 credentials->priv->native.pid = getpid ();
105 credentials->priv->native.uid = getuid ();
106 credentials->priv->native.gid = getgid ();
110 /* ---------------------------------------------------------------------------------------------------- */
115 * Creates a new #GCredentials object with credentials matching the
116 * the current process.
118 * Returns: A #GCredentials. Free with g_object_unref().
123 g_credentials_new (void)
125 return g_object_new (G_TYPE_CREDENTIALS, NULL);
128 /* ---------------------------------------------------------------------------------------------------- */
131 * g_credentials_to_string:
132 * @credentials: A #GCredentials object.
134 * Creates a human-readable textual representation of @credentials
135 * that can be used in logging and debug messages. The format of the
136 * returned string may change in future GLib release.
138 * Returns: A string that should be freed with g_free().
143 g_credentials_to_string (GCredentials *credentials)
147 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
149 ret = g_string_new ("GCredentials:");
151 g_string_append (ret, "linux:");
152 if (credentials->priv->native.pid != -1)
153 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.pid);
154 if (credentials->priv->native.uid != -1)
155 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.uid);
156 if (credentials->priv->native.gid != -1)
157 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.gid);
158 if (ret->str[ret->len - 1] == ',')
159 ret->str[ret->len - 1] = '\0';
161 g_string_append (ret, "unknown");
164 return g_string_free (ret, FALSE);
167 /* ---------------------------------------------------------------------------------------------------- */
170 * g_credentials_is_same_user:
171 * @credentials: A #GCredentials.
172 * @other_credentials: A #GCredentials.
173 * @error: Return location for error or %NULL.
175 * Checks if @credentials and @other_credentials is the same user.
177 * This operation can fail if #GCredentials is not supported on the
180 * Returns: %TRUE if @credentials and @other_credentials has the same
181 * user, %FALSE otherwise or if @error is set.
186 g_credentials_is_same_user (GCredentials *credentials,
187 GCredentials *other_credentials,
192 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
193 g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
194 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
198 if (credentials->priv->native.uid == other_credentials->priv->native.uid)
201 g_set_error_literal (error,
203 G_IO_ERROR_NOT_SUPPORTED,
204 _("GCredentials is not implemented on this OS"));
211 * g_credentials_get_native:
212 * @credentials: A #GCredentials.
214 * Gets a pointer to the native credentials structure.
216 * Returns: The pointer or %NULL if there is no #GCredentials support
217 * for the OS. Do not free the returned data, it is owned by
223 g_credentials_get_native (GCredentials *credentials)
226 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
229 ret = &credentials->priv->native;
238 * g_credentials_set_native:
239 * @credentials: A #GCredentials.
240 * @native: A pointer to native credentials.
242 * Copies the native credentials from @native into @credentials.
244 * It is a programming error (which will cause an warning to be
245 * logged) to use this method if there is no #GCredentials support for
251 g_credentials_set_native (GCredentials *credentials,
255 memcpy (&credentials->priv->native, native, sizeof (struct ucred));
257 g_warning ("g_credentials_set_native: Trying to set credentials but GLib has no support "
258 "for the native credentials type. Please add support.");
262 /* ---------------------------------------------------------------------------------------------------- */
266 * g_credentials_get_unix_user:
267 * @credentials: A #GCredentials
268 * @error: Return location for error or %NULL.
270 * Tries to get the UNIX user identifier from @credentials. This
271 * method is only available on UNIX platforms.
273 * This operation can fail if #GCredentials is not supported on the
274 * OS or if the native credentials type does not contain information
275 * about the UNIX user.
277 * Returns: The UNIX user identifier or -1 if @error is set.
282 g_credentials_get_unix_user (GCredentials *credentials,
287 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
288 g_return_val_if_fail (error == NULL || *error == NULL, -1);
291 ret = credentials->priv->native.uid;
294 g_set_error_literal (error,
296 G_IO_ERROR_NOT_SUPPORTED,
297 _("There is no GCredentials support for your platform"));
304 * g_credentials_set_unix_user:
305 * @credentials: A #GCredentials.
306 * @uid: The UNIX user identifier to set.
307 * @error: Return location for error or %NULL.
309 * Tries to set the UNIX user identifier on @credentials. This method
310 * is only available on UNIX platforms.
312 * This operation can fail if #GCredentials is not supported on the
313 * OS or if the native credentials type does not contain information
314 * about the UNIX user.
316 * Returns: %TRUE if @uid was set, %FALSE if error is set.
321 g_credentials_set_unix_user (GCredentials *credentials,
327 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
328 g_return_val_if_fail (uid != -1, FALSE);
329 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
333 credentials->priv->native.uid = uid;
336 g_set_error_literal (error,
338 G_IO_ERROR_NOT_SUPPORTED,
339 _("GCredentials is not implemented on this OS"));
344 #endif /* G_OS_UNIX */