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>
26 #include <sys/types.h>
27 #include <sys/socket.h>
32 #include <gobject/gvaluecollector.h>
34 #include "gcredentials.h"
35 #include "gnetworkingprivate.h"
41 * SECTION:gcredentials
42 * @short_description: An object containing credentials
45 * The #GCredentials type is a reference-counted wrapper for native
46 * credentials. This information is typically used for identifying,
47 * authenticating and authorizing other processes.
49 * Some operating systems supports looking up the credentials of the
50 * remote peer of a communication endpoint - see e.g.
51 * g_socket_get_credentials().
53 * Some operating systems supports securely sending and receiving
54 * credentials over a Unix Domain Socket, see
55 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
56 * g_unix_connection_receive_credentials() for details.
58 * On Linux, the native credential type is a <type>struct ucred</type>
60 * <citerefentry><refentrytitle>unix</refentrytitle><manvolnum>7</manvolnum></citerefentry>
61 * man page for details. This corresponds to
62 * %G_CREDENTIALS_TYPE_LINUX_UCRED.
64 * On FreeBSD, the native credential type is a <type>struct cmsgcred</type>.
65 * This corresponds to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
71 * The #GCredentials structure contains only private data and
72 * should only be accessed using the provided API.
79 GObject parent_instance;
83 #elif defined(__FreeBSD__)
84 struct cmsgcred native;
87 #warning Please add GCredentials support for your OS
95 * Class structure for #GCredentials.
99 struct _GCredentialsClass
102 GObjectClass parent_class;
105 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
108 g_credentials_finalize (GObject *object)
110 G_GNUC_UNUSED GCredentials *credentials = G_CREDENTIALS (object);
112 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
113 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
118 g_credentials_class_init (GCredentialsClass *klass)
120 GObjectClass *gobject_class;
122 gobject_class = G_OBJECT_CLASS (klass);
123 gobject_class->finalize = g_credentials_finalize;
127 g_credentials_init (GCredentials *credentials)
130 credentials->native.pid = getpid ();
131 credentials->native.uid = geteuid ();
132 credentials->native.gid = getegid ();
133 #elif defined(__FreeBSD__)
134 memset (&credentials->native, 0, sizeof (struct cmsgcred));
135 credentials->native.cmcred_pid = getpid ();
136 credentials->native.cmcred_euid = geteuid ();
137 credentials->native.cmcred_gid = getegid ();
141 /* ---------------------------------------------------------------------------------------------------- */
146 * Creates a new #GCredentials object with credentials matching the
147 * the current process.
149 * Returns: A #GCredentials. Free with g_object_unref().
154 g_credentials_new (void)
156 return g_object_new (G_TYPE_CREDENTIALS, NULL);
159 /* ---------------------------------------------------------------------------------------------------- */
162 * g_credentials_to_string:
163 * @credentials: A #GCredentials object.
165 * Creates a human-readable textual representation of @credentials
166 * that can be used in logging and debug messages. The format of the
167 * returned string may change in future GLib release.
169 * Returns: A string that should be freed with g_free().
174 g_credentials_to_string (GCredentials *credentials)
178 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
180 ret = g_string_new ("GCredentials:");
182 g_string_append (ret, "linux-ucred:");
183 if (credentials->native.pid != -1)
184 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
185 if (credentials->native.uid != -1)
186 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
187 if (credentials->native.gid != -1)
188 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
189 if (ret->str[ret->len - 1] == ',')
190 ret->str[ret->len - 1] = '\0';
191 #elif defined(__FreeBSD__)
192 g_string_append (ret, "freebsd-cmsgcred:");
193 if (credentials->native.cmcred_pid != -1)
194 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_pid);
195 if (credentials->native.cmcred_euid != -1)
196 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_euid);
197 if (credentials->native.cmcred_gid != -1)
198 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_gid);
200 g_string_append (ret, "unknown");
203 return g_string_free (ret, FALSE);
206 /* ---------------------------------------------------------------------------------------------------- */
209 * g_credentials_is_same_user:
210 * @credentials: A #GCredentials.
211 * @other_credentials: A #GCredentials.
212 * @error: Return location for error or %NULL.
214 * Checks if @credentials and @other_credentials is the same user.
216 * This operation can fail if #GCredentials is not supported on the
219 * Returns: %TRUE if @credentials and @other_credentials has the same
220 * user, %FALSE otherwise or if @error is set.
225 g_credentials_is_same_user (GCredentials *credentials,
226 GCredentials *other_credentials,
231 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
232 g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
233 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
237 if (credentials->native.uid == other_credentials->native.uid)
239 #elif defined(__FreeBSD__)
240 if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
243 g_set_error_literal (error,
245 G_IO_ERROR_NOT_SUPPORTED,
246 _("GCredentials is not implemented on this OS"));
253 * g_credentials_get_native:
254 * @credentials: A #GCredentials.
255 * @native_type: The type of native credentials to get.
257 * Gets a pointer to native credentials of type @native_type from
260 * It is a programming error (which will cause an warning to be
261 * logged) to use this method if there is no #GCredentials support for
262 * the OS or if @native_type isn't supported by the OS.
264 * Returns: The pointer to native credentials or %NULL if the
265 * operation there is no #GCredentials support for the OS or if
266 * @native_type isn't supported by the OS. Do not free the returned
267 * data, it is owned by @credentials.
272 g_credentials_get_native (GCredentials *credentials,
273 GCredentialsType native_type)
277 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
282 if (native_type != G_CREDENTIALS_TYPE_LINUX_UCRED)
284 g_warning ("g_credentials_get_native: Trying to get credentials of type %d but only "
285 "G_CREDENTIALS_TYPE_LINUX_UCRED is supported.",
290 ret = &credentials->native;
292 #elif defined(__FreeBSD__)
293 if (native_type != G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED)
295 g_warning ("g_credentials_get_native: Trying to get credentials of type %d but only "
296 "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED is supported.",
301 ret = &credentials->native;
304 g_warning ("g_credentials_get_native: Trying to get credentials but GLib has no support "
305 "for the native credentials type. Please add support.");
312 * g_credentials_set_native:
313 * @credentials: A #GCredentials.
314 * @native_type: The type of native credentials to set.
315 * @native: A pointer to native credentials.
317 * Copies the native credentials of type @native_type from @native
320 * It is a programming error (which will cause an warning to be
321 * logged) to use this method if there is no #GCredentials support for
322 * the OS or if @native_type isn't supported by the OS.
327 g_credentials_set_native (GCredentials *credentials,
328 GCredentialsType native_type,
332 if (native_type != G_CREDENTIALS_TYPE_LINUX_UCRED)
334 g_warning ("g_credentials_set_native: Trying to set credentials of type %d "
335 "but only G_CREDENTIALS_TYPE_LINUX_UCRED is supported.",
340 memcpy (&credentials->native, native, sizeof (struct ucred));
342 #elif defined(__FreeBSD__)
343 if (native_type != G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED)
345 g_warning ("g_credentials_set_native: Trying to set credentials of type %d "
346 "but only G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED is supported.",
351 memcpy (&credentials->native, native, sizeof (struct cmsgcred));
354 g_warning ("g_credentials_set_native: Trying to set credentials but GLib has no support "
355 "for the native credentials type. Please add support.");
359 /* ---------------------------------------------------------------------------------------------------- */
363 * g_credentials_get_unix_user:
364 * @credentials: A #GCredentials
365 * @error: Return location for error or %NULL.
367 * Tries to get the UNIX user identifier from @credentials. This
368 * method is only available on UNIX platforms.
370 * This operation can fail if #GCredentials is not supported on the
371 * OS or if the native credentials type does not contain information
372 * about the UNIX user.
374 * Returns: The UNIX user identifier or -1 if @error is set.
379 g_credentials_get_unix_user (GCredentials *credentials,
384 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
385 g_return_val_if_fail (error == NULL || *error == NULL, -1);
388 ret = credentials->native.uid;
389 #elif defined(__FreeBSD__)
390 ret = credentials->native.cmcred_euid;
393 g_set_error_literal (error,
395 G_IO_ERROR_NOT_SUPPORTED,
396 _("There is no GCredentials support for your platform"));
403 * g_credentials_set_unix_user:
404 * @credentials: A #GCredentials.
405 * @uid: The UNIX user identifier to set.
406 * @error: Return location for error or %NULL.
408 * Tries to set the UNIX user identifier on @credentials. This method
409 * is only available on UNIX platforms.
411 * This operation can fail if #GCredentials is not supported on the
412 * OS or if the native credentials type does not contain information
413 * about the UNIX user.
415 * Returns: %TRUE if @uid was set, %FALSE if error is set.
420 g_credentials_set_unix_user (GCredentials *credentials,
426 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
427 g_return_val_if_fail (uid != -1, FALSE);
428 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
432 credentials->native.uid = uid;
434 #elif defined(__FreeBSD__)
435 credentials->native.cmcred_euid = uid;
438 g_set_error_literal (error,
440 G_IO_ERROR_NOT_SUPPORTED,
441 _("GCredentials is not implemented on this OS"));
446 #endif /* G_OS_UNIX */