Blame gio/gcredentials.c

Packit ae235b
/* GDBus - GLib D-Bus Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <gobject/gvaluecollector.h>
Packit ae235b
Packit ae235b
#include "gcredentials.h"
Packit ae235b
#include "gcredentialsprivate.h"
Packit ae235b
#include "gnetworking.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gcredentials
Packit ae235b
 * @short_description: An object containing credentials
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * The #GCredentials type is a reference-counted wrapper for native
Packit ae235b
 * credentials. This information is typically used for identifying,
Packit ae235b
 * authenticating and authorizing other processes.
Packit ae235b
 *
Packit ae235b
 * Some operating systems supports looking up the credentials of the
Packit ae235b
 * remote peer of a communication endpoint - see e.g.
Packit ae235b
 * g_socket_get_credentials().
Packit ae235b
 *
Packit ae235b
 * Some operating systems supports securely sending and receiving
Packit ae235b
 * credentials over a Unix Domain Socket, see
Packit ae235b
 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
Packit ae235b
 * g_unix_connection_receive_credentials() for details.
Packit ae235b
 *
Packit ae235b
 * On Linux, the native credential type is a struct ucred - see the
Packit ae235b
 * unix(7) man page for details. This corresponds to
Packit ae235b
 * %G_CREDENTIALS_TYPE_LINUX_UCRED.
Packit ae235b
 *
Packit ae235b
 * On FreeBSD, Debian GNU/kFreeBSD, and GNU/Hurd, the native
Packit ae235b
 * credential type is a struct cmsgcred. This corresponds
Packit ae235b
 * to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
Packit ae235b
 *
Packit ae235b
 * On NetBSD, the native credential type is a struct unpcbid.
Packit ae235b
 * This corresponds to %G_CREDENTIALS_TYPE_NETBSD_UNPCBID.
Packit ae235b
 *
Packit ae235b
 * On OpenBSD, the native credential type is a struct sockpeercred.
Packit ae235b
 * This corresponds to %G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.
Packit ae235b
 *
Packit ae235b
 * On Solaris (including OpenSolaris and its derivatives), the native
Packit ae235b
 * credential type is a ucred_t. This corresponds to
Packit ae235b
 * %G_CREDENTIALS_TYPE_SOLARIS_UCRED.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCredentials:
Packit ae235b
 *
Packit ae235b
 * The #GCredentials structure contains only private data and
Packit ae235b
 * should only be accessed using the provided API.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
struct _GCredentials
Packit ae235b
{
Packit ae235b
  /*< private >*/
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  struct ucred native;
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  struct cmsgcred native;
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  struct unpcbid native;
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  struct sockpeercred native;
Packit ae235b
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  ucred_t *native;
Packit ae235b
#else
Packit ae235b
  #ifdef __GNUC__
Packit ae235b
  #warning Please add GCredentials support for your OS
Packit ae235b
  #endif
Packit ae235b
#endif
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCredentialsClass:
Packit ae235b
 *
Packit ae235b
 * Class structure for #GCredentials.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
struct _GCredentialsClass
Packit ae235b
{
Packit ae235b
  /*< private >*/
Packit ae235b
  GObjectClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_credentials_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
#if G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  GCredentials *credentials = G_CREDENTIALS (object);
Packit ae235b
Packit ae235b
  ucred_free (credentials->native);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
Packit ae235b
    G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_credentials_class_init (GCredentialsClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class;
Packit ae235b
Packit ae235b
  gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  gobject_class->finalize = g_credentials_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_credentials_init (GCredentials *credentials)
Packit ae235b
{
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  credentials->native.pid = getpid ();
Packit ae235b
  credentials->native.uid = geteuid ();
Packit ae235b
  credentials->native.gid = getegid ();
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  memset (&credentials->native, 0, sizeof (struct cmsgcred));
Packit ae235b
  credentials->native.cmcred_pid  = getpid ();
Packit ae235b
  credentials->native.cmcred_euid = geteuid ();
Packit ae235b
  credentials->native.cmcred_gid  = getegid ();
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  credentials->native.unp_pid = getpid ();
Packit ae235b
  credentials->native.unp_euid = geteuid ();
Packit ae235b
  credentials->native.unp_egid = getegid ();
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  credentials->native.pid = getpid ();
Packit ae235b
  credentials->native.uid = geteuid ();
Packit ae235b
  credentials->native.gid = getegid ();
Packit ae235b
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  credentials->native = ucred_get (P_MYID);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_new:
Packit ae235b
 *
Packit ae235b
 * Creates a new #GCredentials object with credentials matching the
Packit ae235b
 * the current process.
Packit ae235b
 *
Packit ae235b
 * Returns: A #GCredentials. Free with g_object_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GCredentials *
Packit ae235b
g_credentials_new (void)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_CREDENTIALS, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_to_string:
Packit ae235b
 * @credentials: A #GCredentials object.
Packit ae235b
 *
Packit ae235b
 * Creates a human-readable textual representation of @credentials
Packit ae235b
 * that can be used in logging and debug messages. The format of the
Packit ae235b
 * returned string may change in future GLib release.
Packit ae235b
 *
Packit ae235b
 * Returns: A string that should be freed with g_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_credentials_to_string (GCredentials *credentials)
Packit ae235b
{
Packit ae235b
  GString *ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
Packit ae235b
Packit ae235b
  ret = g_string_new ("GCredentials:");
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  g_string_append (ret, "linux-ucred:");
Packit ae235b
  if (credentials->native.pid != -1)
Packit ae235b
    g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
Packit ae235b
  if (credentials->native.uid != -1)
Packit ae235b
    g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
Packit ae235b
  if (credentials->native.gid != -1)
Packit ae235b
    g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
Packit ae235b
  if (ret->str[ret->len - 1] == ',')
Packit ae235b
    ret->str[ret->len - 1] = '\0';
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  g_string_append (ret, "freebsd-cmsgcred:");
Packit ae235b
  if (credentials->native.cmcred_pid != -1)
Packit ae235b
    g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_pid);
Packit ae235b
  if (credentials->native.cmcred_euid != -1)
Packit ae235b
    g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_euid);
Packit ae235b
  if (credentials->native.cmcred_gid != -1)
Packit ae235b
    g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_gid);
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  g_string_append (ret, "netbsd-unpcbid:");
Packit ae235b
  if (credentials->native.unp_pid != -1)
Packit ae235b
    g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_pid);
Packit ae235b
  if (credentials->native.unp_euid != -1)
Packit ae235b
    g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_euid);
Packit ae235b
  if (credentials->native.unp_egid != -1)
Packit ae235b
    g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_egid);
Packit ae235b
  ret->str[ret->len - 1] = '\0';
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  g_string_append (ret, "openbsd-sockpeercred:");
Packit ae235b
  if (credentials->native.pid != -1)
Packit ae235b
    g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
Packit ae235b
  if (credentials->native.uid != -1)
Packit ae235b
    g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
Packit ae235b
  if (credentials->native.gid != -1)
Packit ae235b
    g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
Packit ae235b
  if (ret->str[ret->len - 1] == ',')
Packit ae235b
    ret->str[ret->len - 1] = '\0';
Packit ae235b
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  g_string_append (ret, "solaris-ucred:");
Packit ae235b
  {
Packit ae235b
    id_t id;
Packit ae235b
    if ((id = ucred_getpid (credentials->native)) != -1)
Packit ae235b
      g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) id);
Packit ae235b
    if ((id = ucred_geteuid (credentials->native)) != -1)
Packit ae235b
      g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) id);
Packit ae235b
    if ((id = ucred_getegid (credentials->native)) != -1)
Packit ae235b
      g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) id);
Packit ae235b
    if (ret->str[ret->len - 1] == ',')
Packit ae235b
      ret->str[ret->len - 1] = '\0';
Packit ae235b
  }
Packit ae235b
#else
Packit ae235b
  g_string_append (ret, "unknown");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return g_string_free (ret, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_is_same_user:
Packit ae235b
 * @credentials: A #GCredentials.
Packit ae235b
 * @other_credentials: A #GCredentials.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Checks if @credentials and @other_credentials is the same user.
Packit ae235b
 *
Packit ae235b
 * This operation can fail if #GCredentials is not supported on the
Packit ae235b
 * the OS.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @credentials and @other_credentials has the same
Packit ae235b
 * user, %FALSE otherwise or if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_credentials_is_same_user (GCredentials  *credentials,
Packit ae235b
                            GCredentials  *other_credentials,
Packit ae235b
                            GError       **error)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  if (credentials->native.uid == other_credentials->native.uid)
Packit ae235b
    ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
Packit ae235b
    ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  if (credentials->native.unp_euid == other_credentials->native.unp_euid)
Packit ae235b
    ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  if (credentials->native.uid == other_credentials->native.uid)
Packit ae235b
    ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  if (ucred_geteuid (credentials->native) == ucred_geteuid (other_credentials->native))
Packit ae235b
    ret = TRUE;
Packit ae235b
#else
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                       _("GCredentials is not implemented on this OS"));
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
credentials_native_type_check (GCredentialsType  requested_type,
Packit ae235b
                               const char       *op)
Packit ae235b
{
Packit ae235b
  GEnumClass *enum_class;
Packit ae235b
  GEnumValue *requested;
Packit ae235b
#if G_CREDENTIALS_SUPPORTED
Packit ae235b
  GEnumValue *supported;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_SUPPORTED
Packit ae235b
  if (requested_type == G_CREDENTIALS_NATIVE_TYPE)
Packit ae235b
    return TRUE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  enum_class = g_type_class_ref (g_credentials_type_get_type ());
Packit ae235b
  requested = g_enum_get_value (enum_class, requested_type);
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_SUPPORTED
Packit ae235b
  supported = g_enum_get_value (enum_class, G_CREDENTIALS_NATIVE_TYPE);
Packit ae235b
  g_assert (supported);
Packit ae235b
  g_warning ("g_credentials_%s_native: Trying to %s credentials of type %s "
Packit ae235b
             "but only %s is supported on this platform.",
Packit ae235b
             op, op,
Packit ae235b
             requested ? requested->value_name : "(unknown)",
Packit ae235b
             supported->value_name);
Packit ae235b
#else
Packit ae235b
  g_warning ("g_credentials_%s_native: Trying to %s credentials of type %s "
Packit ae235b
             "but there is no support for GCredentials on this platform.",
Packit ae235b
             op, op,
Packit ae235b
             requested ? requested->value_name : "(unknown)");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_type_class_unref (enum_class);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_get_native: (skip)
Packit ae235b
 * @credentials: A #GCredentials.
Packit ae235b
 * @native_type: The type of native credentials to get.
Packit ae235b
 *
Packit ae235b
 * Gets a pointer to native credentials of type @native_type from
Packit ae235b
 * @credentials.
Packit ae235b
 *
Packit ae235b
 * It is a programming error (which will cause an warning to be
Packit ae235b
 * logged) to use this method if there is no #GCredentials support for
Packit ae235b
 * the OS or if @native_type isn't supported by the OS.
Packit ae235b
 *
Packit ae235b
 * Returns: The pointer to native credentials or %NULL if the
Packit ae235b
 * operation there is no #GCredentials support for the OS or if
Packit ae235b
 * @native_type isn't supported by the OS. Do not free the returned
Packit ae235b
 * data, it is owned by @credentials.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_credentials_get_native (GCredentials     *credentials,
Packit ae235b
                          GCredentialsType  native_type)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
Packit ae235b
Packit ae235b
  if (!credentials_native_type_check (native_type, "get"))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  return credentials->native;
Packit ae235b
#elif G_CREDENTIALS_SUPPORTED
Packit ae235b
  return &credentials->native;
Packit ae235b
#else
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_set_native:
Packit ae235b
 * @credentials: A #GCredentials.
Packit ae235b
 * @native_type: The type of native credentials to set.
Packit ae235b
 * @native: (not nullable): A pointer to native credentials.
Packit ae235b
 *
Packit ae235b
 * Copies the native credentials of type @native_type from @native
Packit ae235b
 * into @credentials.
Packit ae235b
 *
Packit ae235b
 * It is a programming error (which will cause an warning to be
Packit ae235b
 * logged) to use this method if there is no #GCredentials support for
Packit ae235b
 * the OS or if @native_type isn't supported by the OS.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_credentials_set_native (GCredentials     *credentials,
Packit ae235b
                          GCredentialsType  native_type,
Packit ae235b
                          gpointer          native)
Packit ae235b
{
Packit ae235b
  if (!credentials_native_type_check (native_type, "set"))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  memcpy (credentials->native, native, ucred_size ());
Packit ae235b
#elif G_CREDENTIALS_SUPPORTED
Packit ae235b
  memcpy (&credentials->native, native, sizeof (credentials->native));
Packit ae235b
#else
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
/**
Packit ae235b
 * g_credentials_get_unix_user:
Packit ae235b
 * @credentials: A #GCredentials
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Tries to get the UNIX user identifier from @credentials. This
Packit ae235b
 * method is only available on UNIX platforms.
Packit ae235b
 *
Packit ae235b
 * This operation can fail if #GCredentials is not supported on the
Packit ae235b
 * OS or if the native credentials type does not contain information
Packit ae235b
 * about the UNIX user.
Packit ae235b
 *
Packit ae235b
 * Returns: The UNIX user identifier or -1 if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
uid_t
Packit ae235b
g_credentials_get_unix_user (GCredentials    *credentials,
Packit ae235b
                             GError         **error)
Packit ae235b
{
Packit ae235b
  uid_t ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  ret = credentials->native.uid;
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  ret = credentials->native.cmcred_euid;
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  ret = credentials->native.unp_euid;
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  ret = credentials->native.uid;
Packit ae235b
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  ret = ucred_geteuid (credentials->native);
Packit ae235b
#else
Packit ae235b
  ret = -1;
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                       _("There is no GCredentials support for your platform"));
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_get_unix_pid:
Packit ae235b
 * @credentials: A #GCredentials
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Tries to get the UNIX process identifier from @credentials. This
Packit ae235b
 * method is only available on UNIX platforms.
Packit ae235b
 *
Packit ae235b
 * This operation can fail if #GCredentials is not supported on the
Packit ae235b
 * OS or if the native credentials type does not contain information
Packit ae235b
 * about the UNIX process ID.
Packit ae235b
 *
Packit ae235b
 * Returns: The UNIX process ID, or -1 if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
pid_t
Packit ae235b
g_credentials_get_unix_pid (GCredentials    *credentials,
Packit ae235b
                            GError         **error)
Packit ae235b
{
Packit ae235b
  pid_t ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
Packit ae235b
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  ret = credentials->native.pid;
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  ret = credentials->native.cmcred_pid;
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  ret = credentials->native.unp_pid;
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  ret = credentials->native.pid;
Packit ae235b
#elif G_CREDENTIALS_USE_SOLARIS_UCRED
Packit ae235b
  ret = ucred_getpid (credentials->native);
Packit ae235b
#else
Packit ae235b
  ret = -1;
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                       _("GCredentials does not contain a process ID on this OS"));
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_credentials_set_unix_user:
Packit ae235b
 * @credentials: A #GCredentials.
Packit ae235b
 * @uid: The UNIX user identifier to set.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Tries to set the UNIX user identifier on @credentials. This method
Packit ae235b
 * is only available on UNIX platforms.
Packit ae235b
 *
Packit ae235b
 * This operation can fail if #GCredentials is not supported on the
Packit ae235b
 * OS or if the native credentials type does not contain information
Packit ae235b
 * about the UNIX user. It can also fail if the OS does not allow the
Packit ae235b
 * use of "spoofed" credentials.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @uid was set, %FALSE if error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_credentials_set_unix_user (GCredentials    *credentials,
Packit ae235b
                             uid_t            uid,
Packit ae235b
                             GError         **error)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
Packit ae235b
  g_return_val_if_fail (uid != -1, FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
#if G_CREDENTIALS_USE_LINUX_UCRED
Packit ae235b
  credentials->native.uid = uid;
Packit ae235b
  ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
Packit ae235b
  credentials->native.cmcred_euid = uid;
Packit ae235b
  ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
Packit ae235b
  credentials->native.unp_euid = uid;
Packit ae235b
  ret = TRUE;
Packit ae235b
#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
Packit ae235b
  credentials->native.uid = uid;
Packit ae235b
  ret = TRUE;
Packit ae235b
#elif !G_CREDENTIALS_SPOOFING_SUPPORTED
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_PERMISSION_DENIED,
Packit ae235b
                       _("Credentials spoofing is not possible on this OS"));
Packit ae235b
  ret = FALSE;
Packit ae235b
#else
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                       _("GCredentials is not implemented on this OS"));
Packit ae235b
  ret = FALSE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* G_OS_UNIX */