1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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>
26 #include <gobject/gvaluecollector.h>
28 #include "gcredentials.h"
33 #include <sys/types.h>
34 #include <sys/socket.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
69 #warning Please add GCredentials support for your OS
74 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
77 g_credentials_finalize (GObject *object)
79 G_GNUC_UNUSED GCredentials *credentials = G_CREDENTIALS (object);
81 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
82 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
87 g_credentials_class_init (GCredentialsClass *klass)
89 GObjectClass *gobject_class;
91 g_type_class_add_private (klass, sizeof (GCredentialsPrivate));
93 gobject_class = G_OBJECT_CLASS (klass);
94 gobject_class->finalize = g_credentials_finalize;
98 g_credentials_init (GCredentials *credentials)
100 credentials->priv = G_TYPE_INSTANCE_GET_PRIVATE (credentials, G_TYPE_CREDENTIALS, GCredentialsPrivate);
102 credentials->priv->native.pid = getpid ();
103 credentials->priv->native.uid = getuid ();
104 credentials->priv->native.gid = getgid ();
108 /* ---------------------------------------------------------------------------------------------------- */
113 * Creates a new #GCredentials object with credentials matching the
114 * the current process.
116 * Returns: A #GCredentials. Free with g_object_unref().
121 g_credentials_new (void)
123 return g_object_new (G_TYPE_CREDENTIALS, NULL);
126 /* ---------------------------------------------------------------------------------------------------- */
129 * g_credentials_to_string:
130 * @credentials: A #GCredentials object.
132 * Creates a human-readable textual representation of @credentials
133 * that can be used in logging and debug messages. The format of the
134 * returned string may change in future GLib release.
136 * Returns: A string that should be freed with g_free().
141 g_credentials_to_string (GCredentials *credentials)
145 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
147 ret = g_string_new ("GCredentials:");
149 g_string_append (ret, "linux:");
150 if (credentials->priv->native.pid != -1)
151 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.pid);
152 if (credentials->priv->native.uid != -1)
153 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.uid);
154 if (credentials->priv->native.gid != -1)
155 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.gid);
156 if (ret->str[ret->len - 1] == ',')
157 ret->str[ret->len - 1] = '\0';
159 g_string_append (ret, "unknown");
162 return g_string_free (ret, FALSE);
165 /* ---------------------------------------------------------------------------------------------------- */
168 * g_credentials_is_same_user:
169 * @credentials: A #GCredentials.
170 * @other_credentials: A #GCredentials.
171 * @error: Return location for error or %NULL.
173 * Checks if @credentials and @other_credentials is the same user.
175 * This operation can fail if #GCredentials is not supported on the
178 * Returns: %TRUE if @credentials and @other_credentials has the same
179 * user, %FALSE otherwise or if @error is set.
184 g_credentials_is_same_user (GCredentials *credentials,
185 GCredentials *other_credentials,
190 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
191 g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
192 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
196 if (credentials->priv->native.uid == other_credentials->priv->native.uid)
199 g_set_error_literal (error,
201 G_IO_ERROR_NOT_SUPPORTED,
202 _("GCredentials is not implemented on this OS"));
209 * g_credentials_get_native:
210 * @credentials: A #GCredentials.
212 * Gets a pointer to the native credentials structure.
214 * Returns: The pointer or %NULL if there is no #GCredentials support
215 * for the OS. Do not free the returned data, it is owned by
221 g_credentials_get_native (GCredentials *credentials)
224 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
227 ret = &credentials->priv->native;
236 * g_credentials_set_native:
237 * @credentials: A #GCredentials.
238 * @native: A pointer to native credentials.
240 * Copies the native credentials from @native into @credentials.
242 * It is a programming error (which will cause an warning to be
243 * logged) to use this method if there is no #GCredentials support for
249 g_credentials_set_native (GCredentials *credentials,
253 memcpy (&credentials->priv->native, native, sizeof (struct ucred));
255 g_warning ("g_credentials_set_native: Trying to set credentials but GLib has no support "
256 "for the native credentials type. Please add support.");
260 /* ---------------------------------------------------------------------------------------------------- */
264 * g_credentials_get_unix_user:
265 * @credentials: A #GCredentials
266 * @error: Return location for error or %NULL.
268 * Tries to get the UNIX user identifier from @credentials. This
269 * method is only available on UNIX platforms.
271 * This operation can fail if #GCredentials is not supported on the
272 * OS or if the native credentials type does not contain information
273 * about the UNIX user.
275 * Returns: The UNIX user identifier or -1 if @error is set.
280 g_credentials_get_unix_user (GCredentials *credentials,
285 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
286 g_return_val_if_fail (error == NULL || *error == NULL, -1);
289 ret = credentials->priv->native.uid;
292 g_set_error_literal (error,
294 G_IO_ERROR_NOT_SUPPORTED,
295 _("There no GCredentials support for your your platform"));
302 * g_credentials_set_unix_user:
303 * @credentials: A #GCredentials.
304 * @uid: The UNIX user identifier to set.
305 * @error: Return location for error or %NULL.
307 * Tries to set the UNIX user identifier on @credentials. This method
308 * is only available on UNIX platforms.
310 * This operation can fail if #GCredentials is not supported on the
311 * OS or if the native credentials type does not contain information
312 * about the UNIX user.
314 * Returns: %TRUE if @uid was set, %FALSE if error is set.
319 g_credentials_set_unix_user (GCredentials *credentials,
325 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
326 g_return_val_if_fail (uid != -1, FALSE);
327 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
331 credentials->priv->native.uid = uid;
334 g_set_error_literal (error,
336 G_IO_ERROR_NOT_SUPPORTED,
337 _("GCredentials is not implemented on this OS"));
342 #endif /* G_OS_UNIX */
344 #define __G_CREDENTIALS_C__
345 #include "gioaliasdef.c"