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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include <gobject/gvaluecollector.h>
28 #include "gcredentials.h"
29 #include "gcredentialsprivate.h"
30 #include "gnetworking.h"
32 #include "gioenumtypes.h"
37 * SECTION:gcredentials
38 * @short_description: An object containing credentials
41 * The #GCredentials type is a reference-counted wrapper for native
42 * credentials. This information is typically used for identifying,
43 * authenticating and authorizing other processes.
45 * Some operating systems supports looking up the credentials of the
46 * remote peer of a communication endpoint - see e.g.
47 * g_socket_get_credentials().
49 * Some operating systems supports securely sending and receiving
50 * credentials over a Unix Domain Socket, see
51 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
52 * g_unix_connection_receive_credentials() for details.
54 * On Linux, the native credential type is a `struct ucred` - see the
55 * unix(7) man page for details. This corresponds to
56 * %G_CREDENTIALS_TYPE_LINUX_UCRED.
58 * On Apple operating systems (including iOS, tvOS, and macOS),
59 * the native credential type is a `struct xucred`.
60 * This corresponds to %G_CREDENTIALS_TYPE_APPLE_XUCRED.
62 * On FreeBSD, Debian GNU/kFreeBSD, and GNU/Hurd, the native
63 * credential type is a `struct cmsgcred`. This corresponds
64 * to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
66 * On NetBSD, the native credential type is a `struct unpcbid`.
67 * This corresponds to %G_CREDENTIALS_TYPE_NETBSD_UNPCBID.
69 * On OpenBSD, the native credential type is a `struct sockpeercred`.
70 * This corresponds to %G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.
72 * On Solaris (including OpenSolaris and its derivatives), the native
73 * credential type is a `ucred_t`. This corresponds to
74 * %G_CREDENTIALS_TYPE_SOLARIS_UCRED.
80 * The #GCredentials structure contains only private data and
81 * should only be accessed using the provided API.
88 GObject parent_instance;
90 #if G_CREDENTIALS_USE_LINUX_UCRED
92 #elif G_CREDENTIALS_USE_APPLE_XUCRED
94 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
95 struct cmsgcred native;
96 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
97 struct unpcbid native;
98 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
99 struct sockpeercred native;
100 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
104 #pragma GCC diagnostic push
105 #pragma GCC diagnostic warning "-Wcpp"
106 #warning Please add GCredentials support for your OS
107 #pragma GCC diagnostic pop
115 * Class structure for #GCredentials.
119 struct _GCredentialsClass
122 GObjectClass parent_class;
125 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT)
128 g_credentials_finalize (GObject *object)
130 #if G_CREDENTIALS_USE_SOLARIS_UCRED
131 GCredentials *credentials = G_CREDENTIALS (object);
133 ucred_free (credentials->native);
136 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
137 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
142 g_credentials_class_init (GCredentialsClass *klass)
144 GObjectClass *gobject_class;
146 gobject_class = G_OBJECT_CLASS (klass);
147 gobject_class->finalize = g_credentials_finalize;
151 g_credentials_init (GCredentials *credentials)
153 #if G_CREDENTIALS_USE_LINUX_UCRED
154 credentials->native.pid = getpid ();
155 credentials->native.uid = geteuid ();
156 credentials->native.gid = getegid ();
157 #elif G_CREDENTIALS_USE_APPLE_XUCRED
160 credentials->native.cr_version = XUCRED_VERSION;
161 credentials->native.cr_uid = geteuid ();
162 credentials->native.cr_ngroups = 1;
163 credentials->native.cr_groups[0] = getegid ();
165 /* FIXME: In principle this could use getgroups() to fill in the rest
166 * of cr_groups, but then we'd have to handle the case where a process
167 * can have more than NGROUPS groups, if that's even possible. A macOS
168 * user would have to develop and test this.
170 * For now we fill it with -1 (meaning "no data"). */
171 for (i = 1; i < NGROUPS; i++)
172 credentials->native.cr_groups[i] = -1;
173 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
174 memset (&credentials->native, 0, sizeof (struct cmsgcred));
175 credentials->native.cmcred_pid = getpid ();
176 credentials->native.cmcred_euid = geteuid ();
177 credentials->native.cmcred_gid = getegid ();
178 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
179 credentials->native.unp_pid = getpid ();
180 credentials->native.unp_euid = geteuid ();
181 credentials->native.unp_egid = getegid ();
182 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
183 credentials->native.pid = getpid ();
184 credentials->native.uid = geteuid ();
185 credentials->native.gid = getegid ();
186 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
187 credentials->native = ucred_get (P_MYID);
191 /* ---------------------------------------------------------------------------------------------------- */
196 * Creates a new #GCredentials object with credentials matching the
197 * the current process.
199 * Returns: (transfer full): A #GCredentials. Free with g_object_unref().
204 g_credentials_new (void)
206 return g_object_new (G_TYPE_CREDENTIALS, NULL);
209 /* ---------------------------------------------------------------------------------------------------- */
212 * g_credentials_to_string:
213 * @credentials: A #GCredentials object.
215 * Creates a human-readable textual representation of @credentials
216 * that can be used in logging and debug messages. The format of the
217 * returned string may change in future GLib release.
219 * Returns: (transfer full): A string that should be freed with g_free().
224 g_credentials_to_string (GCredentials *credentials)
227 #if G_CREDENTIALS_USE_APPLE_XUCRED
228 glib_typeof (credentials->native.cr_ngroups) i;
231 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
233 ret = g_string_new ("GCredentials:");
234 #if G_CREDENTIALS_USE_LINUX_UCRED
235 g_string_append (ret, "linux-ucred:");
236 if (credentials->native.pid != (pid_t) -1)
237 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
238 if (credentials->native.uid != (uid_t) -1)
239 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
240 if (credentials->native.gid != (gid_t) -1)
241 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
242 if (ret->str[ret->len - 1] == ',')
243 ret->str[ret->len - 1] = '\0';
244 #elif G_CREDENTIALS_USE_APPLE_XUCRED
245 g_string_append (ret, "apple-xucred:");
246 g_string_append_printf (ret, "version=%u,", credentials->native.cr_version);
247 if (credentials->native.cr_uid != (uid_t) -1)
248 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cr_uid);
249 for (i = 0; i < credentials->native.cr_ngroups; i++)
250 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cr_groups[i]);
251 if (ret->str[ret->len - 1] == ',')
252 ret->str[ret->len - 1] = '\0';
253 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
254 g_string_append (ret, "freebsd-cmsgcred:");
255 if (credentials->native.cmcred_pid != (pid_t) -1)
256 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_pid);
257 if (credentials->native.cmcred_euid != (uid_t) -1)
258 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_euid);
259 if (credentials->native.cmcred_gid != (gid_t) -1)
260 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_gid);
261 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
262 g_string_append (ret, "netbsd-unpcbid:");
263 if (credentials->native.unp_pid != (pid_t) -1)
264 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_pid);
265 if (credentials->native.unp_euid != (uid_t) -1)
266 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_euid);
267 if (credentials->native.unp_egid != (gid_t) -1)
268 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_egid);
269 ret->str[ret->len - 1] = '\0';
270 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
271 g_string_append (ret, "openbsd-sockpeercred:");
272 if (credentials->native.pid != (pid_t) -1)
273 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
274 if (credentials->native.uid != (uid_t) -1)
275 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
276 if (credentials->native.gid != (gid_t) -1)
277 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
278 if (ret->str[ret->len - 1] == ',')
279 ret->str[ret->len - 1] = '\0';
280 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
281 g_string_append (ret, "solaris-ucred:");
284 if ((id = ucred_getpid (credentials->native)) != (id_t) -1)
285 g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) id);
286 if ((id = ucred_geteuid (credentials->native)) != (id_t) -1)
287 g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) id);
288 if ((id = ucred_getegid (credentials->native)) != (id_t) -1)
289 g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) id);
290 if (ret->str[ret->len - 1] == ',')
291 ret->str[ret->len - 1] = '\0';
294 g_string_append (ret, "unknown");
297 return g_string_free (ret, FALSE);
300 /* ---------------------------------------------------------------------------------------------------- */
302 #if G_CREDENTIALS_USE_LINUX_UCRED
304 * Check whether @native contains invalid data. If getsockopt SO_PEERCRED
305 * is used on a TCP socket, it succeeds but yields a credentials structure
306 * with pid 0, uid -1 and gid -1. Similarly, if SO_PASSCRED is used on a
307 * receiving Unix socket when the sending socket did not also enable
308 * SO_PASSCRED, it can succeed but yield a credentials structure with
309 * pid 0, uid /proc/sys/kernel/overflowuid and gid
310 * /proc/sys/kernel/overflowgid.
313 linux_ucred_check_valid (struct ucred *native,
318 || native->gid == -1)
320 g_set_error_literal (error,
322 G_IO_ERROR_INVALID_DATA,
323 _("GCredentials contains invalid data"));
332 * g_credentials_is_same_user:
333 * @credentials: A #GCredentials.
334 * @other_credentials: A #GCredentials.
335 * @error: Return location for error or %NULL.
337 * Checks if @credentials and @other_credentials is the same user.
339 * This operation can fail if #GCredentials is not supported on the
342 * Returns: %TRUE if @credentials and @other_credentials has the same
343 * user, %FALSE otherwise or if @error is set.
348 g_credentials_is_same_user (GCredentials *credentials,
349 GCredentials *other_credentials,
354 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
355 g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
356 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
359 #if G_CREDENTIALS_USE_LINUX_UCRED
360 if (linux_ucred_check_valid (&credentials->native, NULL)
361 && credentials->native.uid == other_credentials->native.uid)
363 #elif G_CREDENTIALS_USE_APPLE_XUCRED
364 if (credentials->native.cr_version == other_credentials->native.cr_version &&
365 credentials->native.cr_uid == other_credentials->native.cr_uid)
367 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
368 if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
370 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
371 if (credentials->native.unp_euid == other_credentials->native.unp_euid)
373 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
374 if (credentials->native.uid == other_credentials->native.uid)
376 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
377 if (ucred_geteuid (credentials->native) == ucred_geteuid (other_credentials->native))
380 g_set_error_literal (error,
382 G_IO_ERROR_NOT_SUPPORTED,
383 _("GCredentials is not implemented on this OS"));
390 credentials_native_type_check (GCredentialsType requested_type,
393 GEnumClass *enum_class;
394 GEnumValue *requested;
395 #if G_CREDENTIALS_SUPPORTED
396 GEnumValue *supported;
399 #if G_CREDENTIALS_SUPPORTED
400 if (requested_type == G_CREDENTIALS_NATIVE_TYPE)
404 enum_class = g_type_class_ref (g_credentials_type_get_type ());
405 requested = g_enum_get_value (enum_class, requested_type);
407 #if G_CREDENTIALS_SUPPORTED
408 supported = g_enum_get_value (enum_class, G_CREDENTIALS_NATIVE_TYPE);
409 g_assert (supported);
410 g_warning ("g_credentials_%s_native: Trying to %s credentials of type %s "
411 "but only %s is supported on this platform.",
413 requested ? requested->value_name : "(unknown)",
414 supported->value_name);
416 g_warning ("g_credentials_%s_native: Trying to %s credentials of type %s "
417 "but there is no support for GCredentials on this platform.",
419 requested ? requested->value_name : "(unknown)");
422 g_type_class_unref (enum_class);
427 * g_credentials_get_native: (skip)
428 * @credentials: A #GCredentials.
429 * @native_type: The type of native credentials to get.
431 * Gets a pointer to native credentials of type @native_type from
434 * It is a programming error (which will cause a warning to be
435 * logged) to use this method if there is no #GCredentials support for
436 * the OS or if @native_type isn't supported by the OS.
438 * Returns: (transfer none) (nullable): The pointer to native credentials or
439 * %NULL if there is no #GCredentials support for the OS or if @native_type
440 * isn't supported by the OS. Do not free the returned data, it is owned
446 g_credentials_get_native (GCredentials *credentials,
447 GCredentialsType native_type)
449 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
451 if (!credentials_native_type_check (native_type, "get"))
454 #if G_CREDENTIALS_USE_SOLARIS_UCRED
455 return credentials->native;
456 #elif G_CREDENTIALS_SUPPORTED
457 return &credentials->native;
459 g_assert_not_reached ();
464 * g_credentials_set_native:
465 * @credentials: A #GCredentials.
466 * @native_type: The type of native credentials to set.
467 * @native: (not nullable): A pointer to native credentials.
469 * Copies the native credentials of type @native_type from @native
472 * It is a programming error (which will cause a warning to be
473 * logged) to use this method if there is no #GCredentials support for
474 * the OS or if @native_type isn't supported by the OS.
479 g_credentials_set_native (GCredentials *credentials,
480 GCredentialsType native_type,
483 if (!credentials_native_type_check (native_type, "set"))
486 #if G_CREDENTIALS_USE_SOLARIS_UCRED
487 memcpy (credentials->native, native, ucred_size ());
488 #elif G_CREDENTIALS_SUPPORTED
489 memcpy (&credentials->native, native, sizeof (credentials->native));
491 g_assert_not_reached ();
495 /* ---------------------------------------------------------------------------------------------------- */
499 * g_credentials_get_unix_user:
500 * @credentials: A #GCredentials
501 * @error: Return location for error or %NULL.
503 * Tries to get the UNIX user identifier from @credentials. This
504 * method 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.
510 * Returns: The UNIX user identifier or `-1` if @error is set.
515 g_credentials_get_unix_user (GCredentials *credentials,
520 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
521 g_return_val_if_fail (error == NULL || *error == NULL, -1);
523 #if G_CREDENTIALS_USE_LINUX_UCRED
524 if (linux_ucred_check_valid (&credentials->native, error))
525 ret = credentials->native.uid;
528 #elif G_CREDENTIALS_USE_APPLE_XUCRED
529 if (credentials->native.cr_version == XUCRED_VERSION)
531 ret = credentials->native.cr_uid;
535 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
536 /* No point in translating the part in parentheses... */
537 "%s (struct xucred cr_version %u != %u)",
538 _("There is no GCredentials support for your platform"),
539 credentials->native.cr_version,
543 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
544 ret = credentials->native.cmcred_euid;
545 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
546 ret = credentials->native.unp_euid;
547 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
548 ret = credentials->native.uid;
549 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
550 ret = ucred_geteuid (credentials->native);
553 g_set_error_literal (error,
555 G_IO_ERROR_NOT_SUPPORTED,
556 _("There is no GCredentials support for your platform"));
563 * g_credentials_get_unix_pid:
564 * @credentials: A #GCredentials
565 * @error: Return location for error or %NULL.
567 * Tries to get the UNIX process identifier from @credentials. This
568 * method is only available on UNIX platforms.
570 * This operation can fail if #GCredentials is not supported on the
571 * OS or if the native credentials type does not contain information
572 * about the UNIX process ID (for example this is the case for
573 * %G_CREDENTIALS_TYPE_APPLE_XUCRED).
575 * Returns: The UNIX process ID, or `-1` if @error is set.
580 g_credentials_get_unix_pid (GCredentials *credentials,
585 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
586 g_return_val_if_fail (error == NULL || *error == NULL, -1);
588 #if G_CREDENTIALS_USE_LINUX_UCRED
589 if (linux_ucred_check_valid (&credentials->native, error))
590 ret = credentials->native.pid;
593 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
594 ret = credentials->native.cmcred_pid;
595 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
596 ret = credentials->native.unp_pid;
597 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
598 ret = credentials->native.pid;
599 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
600 ret = ucred_getpid (credentials->native);
602 /* this case includes G_CREDENTIALS_USE_APPLE_XUCRED */
604 g_set_error_literal (error,
606 G_IO_ERROR_NOT_SUPPORTED,
607 _("GCredentials does not contain a process ID on this OS"));
614 * g_credentials_set_unix_user:
615 * @credentials: A #GCredentials.
616 * @uid: The UNIX user identifier to set.
617 * @error: Return location for error or %NULL.
619 * Tries to set the UNIX user identifier on @credentials. This method
620 * is only available on UNIX platforms.
622 * This operation can fail if #GCredentials is not supported on the
623 * OS or if the native credentials type does not contain information
624 * about the UNIX user. It can also fail if the OS does not allow the
625 * use of "spoofed" credentials.
627 * Returns: %TRUE if @uid was set, %FALSE if error is set.
632 g_credentials_set_unix_user (GCredentials *credentials,
636 gboolean ret = FALSE;
638 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
639 g_return_val_if_fail (uid != -1, FALSE);
640 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
642 #if G_CREDENTIALS_USE_LINUX_UCRED
643 credentials->native.uid = uid;
645 #elif G_CREDENTIALS_USE_APPLE_XUCRED
646 credentials->native.cr_uid = uid;
648 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
649 credentials->native.cmcred_euid = uid;
651 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
652 credentials->native.unp_euid = uid;
654 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
655 credentials->native.uid = uid;
657 #elif !G_CREDENTIALS_SPOOFING_SUPPORTED
658 g_set_error_literal (error,
660 G_IO_ERROR_PERMISSION_DENIED,
661 _("Credentials spoofing is not possible on this OS"));
664 g_set_error_literal (error,
666 G_IO_ERROR_NOT_SUPPORTED,
667 _("GCredentials is not implemented on this OS"));
674 #endif /* G_OS_UNIX */