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>
27 #include <gobject/gvaluecollector.h>
29 #include "gcredentials.h"
30 #include "gnetworkingprivate.h"
36 * SECTION:gcredentials
37 * @short_description: An object containing credentials
40 * The #GCredentials type is a reference-counted wrapper for native
41 * credentials. This information is typically used for identifying,
42 * authenticating and authorizing other processes.
44 * Some operating systems supports looking up the credentials of the
45 * remote peer of a communication endpoint - see e.g.
46 * g_socket_get_credentials().
48 * Some operating systems supports securely sending and receiving
49 * credentials over a Unix Domain Socket, see
50 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
51 * g_unix_connection_receive_credentials() for details.
53 * On Linux, the native credential type is a <type>struct ucred</type>
55 * <citerefentry><refentrytitle>unix</refentrytitle><manvolnum>7</manvolnum></citerefentry>
56 * man page for details. This corresponds to
57 * %G_CREDENTIALS_TYPE_LINUX_UCRED.
63 * The #GCredentials structure contains only private data and
64 * should only be accessed using the provided API.
71 GObject parent_instance;
77 #warning Please add GCredentials support for your OS
85 * Class structure for #GCredentials.
89 struct _GCredentialsClass
92 GObjectClass parent_class;
95 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
98 g_credentials_finalize (GObject *object)
100 G_GNUC_UNUSED GCredentials *credentials = G_CREDENTIALS (object);
102 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
103 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
108 g_credentials_class_init (GCredentialsClass *klass)
110 GObjectClass *gobject_class;
112 gobject_class = G_OBJECT_CLASS (klass);
113 gobject_class->finalize = g_credentials_finalize;
117 g_credentials_init (GCredentials *credentials)
120 credentials->native.pid = getpid ();
121 credentials->native.uid = getuid ();
122 credentials->native.gid = getgid ();
126 /* ---------------------------------------------------------------------------------------------------- */
131 * Creates a new #GCredentials object with credentials matching the
132 * the current process.
134 * Returns: A #GCredentials. Free with g_object_unref().
139 g_credentials_new (void)
141 return g_object_new (G_TYPE_CREDENTIALS, NULL);
144 /* ---------------------------------------------------------------------------------------------------- */
147 * g_credentials_to_string:
148 * @credentials: A #GCredentials object.
150 * Creates a human-readable textual representation of @credentials
151 * that can be used in logging and debug messages. The format of the
152 * returned string may change in future GLib release.
154 * Returns: A string that should be freed with g_free().
159 g_credentials_to_string (GCredentials *credentials)
163 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
165 ret = g_string_new ("GCredentials:");
167 g_string_append (ret, "linux-ucred:");
168 if (credentials->native.pid != -1)
169 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
170 if (credentials->native.uid != -1)
171 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
172 if (credentials->native.gid != -1)
173 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
174 if (ret->str[ret->len - 1] == ',')
175 ret->str[ret->len - 1] = '\0';
177 g_string_append (ret, "unknown");
180 return g_string_free (ret, FALSE);
183 /* ---------------------------------------------------------------------------------------------------- */
186 * g_credentials_is_same_user:
187 * @credentials: A #GCredentials.
188 * @other_credentials: A #GCredentials.
189 * @error: Return location for error or %NULL.
191 * Checks if @credentials and @other_credentials is the same user.
193 * This operation can fail if #GCredentials is not supported on the
196 * Returns: %TRUE if @credentials and @other_credentials has the same
197 * user, %FALSE otherwise or if @error is set.
202 g_credentials_is_same_user (GCredentials *credentials,
203 GCredentials *other_credentials,
208 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
209 g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
210 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
214 if (credentials->native.uid == other_credentials->native.uid)
217 g_set_error_literal (error,
219 G_IO_ERROR_NOT_SUPPORTED,
220 _("GCredentials is not implemented on this OS"));
227 * g_credentials_get_native:
228 * @credentials: A #GCredentials.
229 * @native_type: The type of native credentials to get.
231 * Gets a pointer to native credentials of type @native_type from
234 * It is a programming error (which will cause an warning to be
235 * logged) to use this method if there is no #GCredentials support for
236 * the OS or if @native_type isn't supported by the OS.
238 * Returns: The pointer to native credentials or %NULL if the
239 * operation there is no #GCredentials support for the OS or if
240 * @native_type isn't supported by the OS. Do not free the returned
241 * data, it is owned by @credentials.
246 g_credentials_get_native (GCredentials *credentials,
247 GCredentialsType native_type)
251 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
256 if (native_type != G_CREDENTIALS_TYPE_LINUX_UCRED)
258 g_warning ("g_credentials_get_native: Trying to get credentials of type %d but only "
259 "G_CREDENTIALS_TYPE_LINUX_UCRED is supported.",
264 ret = &credentials->native;
267 g_warning ("g_credentials_get_native: Trying to get credentials but GLib has no support "
268 "for the native credentials type. Please add support.");
275 * g_credentials_set_native:
276 * @credentials: A #GCredentials.
277 * @native_type: The type of native credentials to set.
278 * @native: A pointer to native credentials.
280 * Copies the native credentials of type @native_type from @native
283 * It is a programming error (which will cause an warning to be
284 * logged) to use this method if there is no #GCredentials support for
285 * the OS or if @native_type isn't supported by the OS.
290 g_credentials_set_native (GCredentials *credentials,
291 GCredentialsType native_type,
295 if (native_type != G_CREDENTIALS_TYPE_LINUX_UCRED)
297 g_warning ("g_credentials_set_native: Trying to set credentials of type %d "
298 "but only G_CREDENTIALS_TYPE_LINUX_UCRED is supported.",
303 memcpy (&credentials->native, native, sizeof (struct ucred));
306 g_warning ("g_credentials_set_native: Trying to set credentials but GLib has no support "
307 "for the native credentials type. Please add support.");
311 /* ---------------------------------------------------------------------------------------------------- */
315 * g_credentials_get_unix_user:
316 * @credentials: A #GCredentials
317 * @error: Return location for error or %NULL.
319 * Tries to get the UNIX user identifier from @credentials. This
320 * method is only available on UNIX platforms.
322 * This operation can fail if #GCredentials is not supported on the
323 * OS or if the native credentials type does not contain information
324 * about the UNIX user.
326 * Returns: The UNIX user identifier or -1 if @error is set.
331 g_credentials_get_unix_user (GCredentials *credentials,
336 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
337 g_return_val_if_fail (error == NULL || *error == NULL, -1);
340 ret = credentials->native.uid;
343 g_set_error_literal (error,
345 G_IO_ERROR_NOT_SUPPORTED,
346 _("There is no GCredentials support for your platform"));
353 * g_credentials_set_unix_user:
354 * @credentials: A #GCredentials.
355 * @uid: The UNIX user identifier to set.
356 * @error: Return location for error or %NULL.
358 * Tries to set the UNIX user identifier on @credentials. This method
359 * is only available on UNIX platforms.
361 * This operation can fail if #GCredentials is not supported on the
362 * OS or if the native credentials type does not contain information
363 * about the UNIX user.
365 * Returns: %TRUE if @uid was set, %FALSE if error is set.
370 g_credentials_set_unix_user (GCredentials *credentials,
376 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
377 g_return_val_if_fail (uid != -1, FALSE);
378 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
382 credentials->native.uid = uid;
385 g_set_error_literal (error,
387 G_IO_ERROR_NOT_SUPPORTED,
388 _("GCredentials is not implemented on this OS"));
393 #endif /* G_OS_UNIX */