Clean up GSettingsSchema logic
[glib.git] / gio / gcredentials.c
blob8e9f28a02c05d01a98d82414d7ab4b8341e5e98f
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>
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
28 #include <gobject/gvaluecollector.h>
30 #include "gcredentials.h"
31 #include "gcredentialsprivate.h"
32 #include "gnetworking.h"
33 #include "gioerror.h"
34 #include "gioenumtypes.h"
36 #include "glibintl.h"
38 /**
39 * SECTION:gcredentials
40 * @short_description: An object containing credentials
41 * @include: gio/gio.h
43 * The #GCredentials type is a reference-counted wrapper for native
44 * credentials. This information is typically used for identifying,
45 * authenticating and authorizing other processes.
47 * Some operating systems supports looking up the credentials of the
48 * remote peer of a communication endpoint - see e.g.
49 * g_socket_get_credentials().
51 * Some operating systems supports securely sending and receiving
52 * credentials over a Unix Domain Socket, see
53 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
54 * g_unix_connection_receive_credentials() for details.
56 * On Linux, the native credential type is a <type>struct ucred</type>
57 * - see the
58 * <citerefentry><refentrytitle>unix</refentrytitle><manvolnum>7</manvolnum></citerefentry>
59 * man page for details. This corresponds to
60 * %G_CREDENTIALS_TYPE_LINUX_UCRED.
62 * On FreeBSD, Debian GNU/kFreeBSD, and GNU/Hurd, the native
63 * credential type is a <type>struct cmsgcred</type>. This corresponds
64 * to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
66 * On OpenBSD, the native credential type is a <type>struct sockpeercred</type>.
67 * This corresponds to %G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.
69 * On Solaris (including OpenSolaris and its derivatives), the native
70 * credential type is a <type>ucred_t</type>. This corresponds to
71 * %G_CREDENTIALS_TYPE_SOLARIS_UCRED.
74 /**
75 * GCredentials:
77 * The #GCredentials structure contains only private data and
78 * should only be accessed using the provided API.
80 * Since: 2.26
82 struct _GCredentials
84 /*< private >*/
85 GObject parent_instance;
87 #if G_CREDENTIALS_USE_LINUX_UCRED
88 struct ucred native;
89 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
90 struct cmsgcred native;
91 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
92 struct sockpeercred native;
93 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
94 ucred_t *native;
95 #else
96 #ifdef __GNUC__
97 #warning Please add GCredentials support for your OS
98 #endif
99 #endif
103 * GCredentialsClass:
105 * Class structure for #GCredentials.
107 * Since: 2.26
109 struct _GCredentialsClass
111 /*< private >*/
112 GObjectClass parent_class;
115 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
117 static void
118 g_credentials_finalize (GObject *object)
120 #if G_CREDENTIALS_USE_SOLARIS_UCRED
121 GCredentials *credentials = G_CREDENTIALS (object);
123 ucred_free (credentials->native);
124 #endif
126 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
127 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
131 static void
132 g_credentials_class_init (GCredentialsClass *klass)
134 GObjectClass *gobject_class;
136 gobject_class = G_OBJECT_CLASS (klass);
137 gobject_class->finalize = g_credentials_finalize;
140 static void
141 g_credentials_init (GCredentials *credentials)
143 #if G_CREDENTIALS_USE_LINUX_UCRED
144 credentials->native.pid = getpid ();
145 credentials->native.uid = geteuid ();
146 credentials->native.gid = getegid ();
147 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
148 memset (&credentials->native, 0, sizeof (struct cmsgcred));
149 credentials->native.cmcred_pid = getpid ();
150 credentials->native.cmcred_euid = geteuid ();
151 credentials->native.cmcred_gid = getegid ();
152 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
153 credentials->native.pid = getpid ();
154 credentials->native.uid = geteuid ();
155 credentials->native.gid = getegid ();
156 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
157 credentials->native = ucred_get (P_MYID);
158 #endif
161 /* ---------------------------------------------------------------------------------------------------- */
164 * g_credentials_new:
166 * Creates a new #GCredentials object with credentials matching the
167 * the current process.
169 * Returns: A #GCredentials. Free with g_object_unref().
171 * Since: 2.26
173 GCredentials *
174 g_credentials_new (void)
176 return g_object_new (G_TYPE_CREDENTIALS, NULL);
179 /* ---------------------------------------------------------------------------------------------------- */
182 * g_credentials_to_string:
183 * @credentials: A #GCredentials object.
185 * Creates a human-readable textual representation of @credentials
186 * that can be used in logging and debug messages. The format of the
187 * returned string may change in future GLib release.
189 * Returns: A string that should be freed with g_free().
191 * Since: 2.26
193 gchar *
194 g_credentials_to_string (GCredentials *credentials)
196 GString *ret;
198 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
200 ret = g_string_new ("GCredentials:");
201 #if G_CREDENTIALS_USE_LINUX_UCRED
202 g_string_append (ret, "linux-ucred:");
203 if (credentials->native.pid != -1)
204 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
205 if (credentials->native.uid != -1)
206 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
207 if (credentials->native.gid != -1)
208 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
209 if (ret->str[ret->len - 1] == ',')
210 ret->str[ret->len - 1] = '\0';
211 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
212 g_string_append (ret, "freebsd-cmsgcred:");
213 if (credentials->native.cmcred_pid != -1)
214 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_pid);
215 if (credentials->native.cmcred_euid != -1)
216 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_euid);
217 if (credentials->native.cmcred_gid != -1)
218 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_gid);
219 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
220 g_string_append (ret, "openbsd-sockpeercred:");
221 if (credentials->native.pid != -1)
222 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
223 if (credentials->native.uid != -1)
224 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
225 if (credentials->native.gid != -1)
226 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
227 if (ret->str[ret->len - 1] == ',')
228 ret->str[ret->len - 1] = '\0';
229 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
230 g_string_append (ret, "solaris-ucred:");
232 id_t id;
233 if ((id = ucred_getpid (credentials->native)) != -1)
234 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) id);
235 if ((id = ucred_geteuid (credentials->native)) != -1)
236 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) id);
237 if ((id = ucred_getegid (credentials->native)) != -1)
238 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) id);
239 if (ret->str[ret->len - 1] == ',')
240 ret->str[ret->len - 1] = '\0';
242 #else
243 g_string_append (ret, "unknown");
244 #endif
246 return g_string_free (ret, FALSE);
249 /* ---------------------------------------------------------------------------------------------------- */
252 * g_credentials_is_same_user:
253 * @credentials: A #GCredentials.
254 * @other_credentials: A #GCredentials.
255 * @error: Return location for error or %NULL.
257 * Checks if @credentials and @other_credentials is the same user.
259 * This operation can fail if #GCredentials is not supported on the
260 * the OS.
262 * Returns: %TRUE if @credentials and @other_credentials has the same
263 * user, %FALSE otherwise or if @error is set.
265 * Since: 2.26
267 gboolean
268 g_credentials_is_same_user (GCredentials *credentials,
269 GCredentials *other_credentials,
270 GError **error)
272 gboolean ret;
274 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
275 g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
276 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
278 ret = FALSE;
279 #if G_CREDENTIALS_USE_LINUX_UCRED
280 if (credentials->native.uid == other_credentials->native.uid)
281 ret = TRUE;
282 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
283 if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
284 ret = TRUE;
285 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
286 if (credentials->native.uid == other_credentials->native.uid)
287 ret = TRUE;
288 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
289 if (ucred_geteuid (credentials->native) == ucred_geteuid (other_credentials->native))
290 ret = TRUE;
291 #else
292 g_set_error_literal (error,
293 G_IO_ERROR,
294 G_IO_ERROR_NOT_SUPPORTED,
295 _("GCredentials is not implemented on this OS"));
296 #endif
298 return ret;
301 static gboolean
302 credentials_native_type_check (GCredentialsType requested_type,
303 const char *op)
305 GEnumClass *enum_class;
306 GEnumValue *requested;
307 #if G_CREDENTIALS_SUPPORTED
308 GEnumValue *supported;
309 #endif
311 #if G_CREDENTIALS_SUPPORTED
312 if (requested_type == G_CREDENTIALS_NATIVE_TYPE)
313 return TRUE;
314 #endif
316 enum_class = g_type_class_ref (g_credentials_type_get_type ());
317 requested = g_enum_get_value (enum_class, requested_type);
319 #if G_CREDENTIALS_SUPPORTED
320 supported = g_enum_get_value (enum_class, G_CREDENTIALS_NATIVE_TYPE);
321 g_warning ("g_credentials_%s_native: Trying to %s credentials of type %s "
322 "but only %s is supported on this platform.",
323 op, op,
324 requested ? requested->value_name : "(unknown)",
325 supported->value_name);
326 #else
327 g_warning ("g_credentials_%s_native: Trying to %s credentials of type %s "
328 "but there is no support for GCredentials on this platform.",
329 op, op,
330 requested ? requested->value_name : "(unknown)");
331 #endif
333 g_type_class_unref (enum_class);
334 return FALSE;
338 * g_credentials_get_native: (skip)
339 * @credentials: A #GCredentials.
340 * @native_type: The type of native credentials to get.
342 * Gets a pointer to native credentials of type @native_type from
343 * @credentials.
345 * It is a programming error (which will cause an warning to be
346 * logged) to use this method if there is no #GCredentials support for
347 * the OS or if @native_type isn't supported by the OS.
349 * Returns: The pointer to native credentials or %NULL if the
350 * operation there is no #GCredentials support for the OS or if
351 * @native_type isn't supported by the OS. Do not free the returned
352 * data, it is owned by @credentials.
354 * Since: 2.26
356 gpointer
357 g_credentials_get_native (GCredentials *credentials,
358 GCredentialsType native_type)
360 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
362 if (!credentials_native_type_check (native_type, "get"))
363 return NULL;
365 #if G_CREDENTIALS_USE_SOLARIS_UCRED
366 return credentials->native;
367 #elif G_CREDENTIALS_SUPPORTED
368 return &credentials->native;
369 #else
370 g_assert_not_reached ();
371 #endif
375 * g_credentials_set_native:
376 * @credentials: A #GCredentials.
377 * @native_type: The type of native credentials to set.
378 * @native: A pointer to native credentials.
380 * Copies the native credentials of type @native_type from @native
381 * into @credentials.
383 * It is a programming error (which will cause an warning to be
384 * logged) to use this method if there is no #GCredentials support for
385 * the OS or if @native_type isn't supported by the OS.
387 * Since: 2.26
389 void
390 g_credentials_set_native (GCredentials *credentials,
391 GCredentialsType native_type,
392 gpointer native)
394 if (!credentials_native_type_check (native_type, "set"))
395 return;
397 #if G_CREDENTIALS_USE_SOLARIS_UCRED
398 memcpy (credentials->native, native, ucred_size ());
399 #elif G_CREDENTIALS_SUPPORTED
400 memcpy (&credentials->native, native, sizeof (credentials->native));
401 #else
402 g_assert_not_reached ();
403 #endif
406 /* ---------------------------------------------------------------------------------------------------- */
408 #ifdef G_OS_UNIX
410 * g_credentials_get_unix_user:
411 * @credentials: A #GCredentials
412 * @error: Return location for error or %NULL.
414 * Tries to get the UNIX user identifier from @credentials. This
415 * method is only available on UNIX platforms.
417 * This operation can fail if #GCredentials is not supported on the
418 * OS or if the native credentials type does not contain information
419 * about the UNIX user.
421 * Returns: The UNIX user identifier or -1 if @error is set.
423 * Since: 2.26
425 uid_t
426 g_credentials_get_unix_user (GCredentials *credentials,
427 GError **error)
429 uid_t ret;
431 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
432 g_return_val_if_fail (error == NULL || *error == NULL, -1);
434 #if G_CREDENTIALS_USE_LINUX_UCRED
435 ret = credentials->native.uid;
436 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
437 ret = credentials->native.cmcred_euid;
438 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
439 ret = credentials->native.uid;
440 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
441 ret = ucred_geteuid (credentials->native);
442 #else
443 ret = -1;
444 g_set_error_literal (error,
445 G_IO_ERROR,
446 G_IO_ERROR_NOT_SUPPORTED,
447 _("There is no GCredentials support for your platform"));
448 #endif
450 return ret;
454 * g_credentials_get_unix_pid:
455 * @credentials: A #GCredentials
456 * @error: Return location for error or %NULL.
458 * Tries to get the UNIX process identifier from @credentials. This
459 * method is only available on UNIX platforms.
461 * This operation can fail if #GCredentials is not supported on the
462 * OS or if the native credentials type does not contain information
463 * about the UNIX process ID.
465 * Returns: The UNIX process ID, or -1 if @error is set.
467 * Since: 2.36
469 pid_t
470 g_credentials_get_unix_pid (GCredentials *credentials,
471 GError **error)
473 pid_t ret;
475 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
476 g_return_val_if_fail (error == NULL || *error == NULL, -1);
478 #if G_CREDENTIALS_USE_LINUX_UCRED
479 ret = credentials->native.pid;
480 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
481 ret = credentials->native.cmcred_pid;
482 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
483 ret = credentials->native.pid;
484 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
485 ret = ucred_getpid (credentials->native);
486 #else
487 ret = -1;
488 g_set_error_literal (error,
489 G_IO_ERROR,
490 G_IO_ERROR_NOT_SUPPORTED,
491 _("GCredentials does not contain a process ID on this OS"));
492 #endif
494 return ret;
498 * g_credentials_set_unix_user:
499 * @credentials: A #GCredentials.
500 * @uid: The UNIX user identifier to set.
501 * @error: Return location for error or %NULL.
503 * Tries to set the UNIX user identifier on @credentials. This method
504 * is only available on UNIX platforms.
506 * This operation can fail if #GCredentials is not supported on the
507 * OS or if the native credentials type does not contain information
508 * about the UNIX user. It can also fail if the OS does not allow the
509 * use of "spoofed" credentials.
511 * Returns: %TRUE if @uid was set, %FALSE if error is set.
513 * Since: 2.26
515 gboolean
516 g_credentials_set_unix_user (GCredentials *credentials,
517 uid_t uid,
518 GError **error)
520 gboolean ret;
522 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
523 g_return_val_if_fail (uid != -1, FALSE);
524 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
526 ret = FALSE;
527 #if G_CREDENTIALS_USE_LINUX_UCRED
528 credentials->native.uid = uid;
529 ret = TRUE;
530 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
531 credentials->native.cmcred_euid = uid;
532 ret = TRUE;
533 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
534 credentials->native.uid = uid;
535 ret = TRUE;
536 #elif !G_CREDENTIALS_SPOOFING_SUPPORTED
537 g_set_error_literal (error,
538 G_IO_ERROR,
539 G_IO_ERROR_PERMISSION_DENIED,
540 _("Credentials spoofing is not possible on this OS"));
541 ret = FALSE;
542 #else
543 g_set_error_literal (error,
544 G_IO_ERROR,
545 G_IO_ERROR_NOT_SUPPORTED,
546 _("GCredentials is not implemented on this OS"));
547 ret = FALSE;
548 #endif
550 return ret;
553 #endif /* G_OS_UNIX */