Blame gio/gunixconnection.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2009 Codethink Limited
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
 * See the included COPYING file for more information.
Packit ae235b
 *
Packit ae235b
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gunixconnection.h"
Packit ae235b
#include "gnetworking.h"
Packit ae235b
#include "gsocket.h"
Packit ae235b
#include "gsocketcontrolmessage.h"
Packit ae235b
#include "gunixcredentialsmessage.h"
Packit ae235b
#include "gunixfdmessage.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gunixconnection
Packit ae235b
 * @title: GUnixConnection
Packit ae235b
 * @short_description: A UNIX domain GSocketConnection
Packit ae235b
 * @include: gio/gunixconnection.h
Packit ae235b
 * @see_also: #GSocketConnection.
Packit ae235b
 *
Packit ae235b
 * This is the subclass of #GSocketConnection that is created
Packit ae235b
 * for UNIX domain sockets.
Packit ae235b
 *
Packit ae235b
 * It contains functions to do some of the UNIX socket specific
Packit ae235b
 * functionality like passing file descriptors.
Packit ae235b
 *
Packit ae235b
 * Note that `<gio/gunixconnection.h>` belongs to the UNIX-specific
Packit ae235b
 * GIO interfaces, thus you have to use the `gio-unix-2.0.pc`
Packit ae235b
 * pkg-config file when using it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GUnixConnection:
Packit ae235b
 *
Packit ae235b
 * #GUnixConnection is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GUnixConnection, g_unix_connection,
Packit ae235b
			 G_TYPE_SOCKET_CONNECTION,
Packit ae235b
  g_socket_connection_factory_register_type (g_define_type_id,
Packit ae235b
					     G_SOCKET_FAMILY_UNIX,
Packit ae235b
					     G_SOCKET_TYPE_STREAM,
Packit ae235b
					     G_SOCKET_PROTOCOL_DEFAULT);
Packit ae235b
			 );
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_send_fd:
Packit ae235b
 * @connection: a #GUnixConnection
Packit ae235b
 * @fd: a file descriptor
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: (nullable): #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Passes a file descriptor to the receiving side of the
Packit ae235b
 * connection. The receiving end has to call g_unix_connection_receive_fd()
Packit ae235b
 * to accept the file descriptor.
Packit ae235b
 *
Packit ae235b
 * As well as sending the fd this also writes a single byte to the
Packit ae235b
 * stream, as this is required for fd passing to work on some
Packit ae235b
 * implementations.
Packit ae235b
 *
Packit ae235b
 * Returns: a %TRUE on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_unix_connection_send_fd (GUnixConnection  *connection,
Packit ae235b
                           gint              fd,
Packit ae235b
                           GCancellable     *cancellable,
Packit ae235b
                           GError          **error)
Packit ae235b
{
Packit ae235b
  GSocketControlMessage *scm;
Packit ae235b
  GSocket *socket;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
Packit ae235b
  g_return_val_if_fail (fd >= 0, FALSE);
Packit ae235b
Packit ae235b
  scm = g_unix_fd_message_new ();
Packit ae235b
Packit ae235b
  if (!g_unix_fd_message_append_fd (G_UNIX_FD_MESSAGE (scm), fd, error))
Packit ae235b
    {
Packit ae235b
      g_object_unref (scm);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_get (connection, "socket", &socket, NULL);
Packit ae235b
  if (g_socket_send_message (socket, NULL, NULL, 0, &scm, 1, 0, cancellable, error) != 1)
Packit ae235b
    /* XXX could it 'fail' with zero? */
Packit ae235b
    {
Packit ae235b
      g_object_unref (socket);
Packit ae235b
      g_object_unref (scm);
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (socket);
Packit ae235b
  g_object_unref (scm);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_receive_fd:
Packit ae235b
 * @connection: a #GUnixConnection
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
Packit ae235b
 * @error: (nullable): #GError for error reporting, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * Receives a file descriptor from the sending end of the connection.
Packit ae235b
 * The sending end has to call g_unix_connection_send_fd() for this
Packit ae235b
 * to work.
Packit ae235b
 *
Packit ae235b
 * As well as reading the fd this also reads a single byte from the
Packit ae235b
 * stream, as this is required for fd passing to work on some
Packit ae235b
 * implementations.
Packit ae235b
 *
Packit ae235b
 * Returns: a file descriptor on success, -1 on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_unix_connection_receive_fd (GUnixConnection  *connection,
Packit ae235b
                              GCancellable     *cancellable,
Packit ae235b
                              GError          **error)
Packit ae235b
{
Packit ae235b
  GSocketControlMessage **scms;
Packit ae235b
  gint *fds, nfd, fd, nscm;
Packit ae235b
  GUnixFDMessage *fdmsg;
Packit ae235b
  GSocket *socket;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), -1);
Packit ae235b
Packit ae235b
  g_object_get (connection, "socket", &socket, NULL);
Packit ae235b
  if (g_socket_receive_message (socket, NULL, NULL, 0,
Packit ae235b
                                &scms, &nscm, NULL, cancellable, error) != 1)
Packit ae235b
    /* XXX it _could_ 'fail' with zero. */
Packit ae235b
    {
Packit ae235b
      g_object_unref (socket);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (socket);
Packit ae235b
Packit ae235b
  if (nscm != 1)
Packit ae235b
    {
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
        ngettext("Expecting 1 control message, got %d",
Packit ae235b
                 "Expecting 1 control message, got %d",
Packit ae235b
                 nscm),
Packit ae235b
        nscm);
Packit ae235b
Packit ae235b
      for (i = 0; i < nscm; i++)
Packit ae235b
        g_object_unref (scms[i]);
Packit ae235b
Packit ae235b
      g_free (scms);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!G_IS_UNIX_FD_MESSAGE (scms[0]))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
			   _("Unexpected type of ancillary data"));
Packit ae235b
      g_object_unref (scms[0]);
Packit ae235b
      g_free (scms);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  fdmsg = G_UNIX_FD_MESSAGE (scms[0]);
Packit ae235b
  g_free (scms);
Packit ae235b
Packit ae235b
  fds = g_unix_fd_message_steal_fds (fdmsg, &nfd;;
Packit ae235b
  g_object_unref (fdmsg);
Packit ae235b
Packit ae235b
  if (nfd != 1)
Packit ae235b
    {
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                   ngettext("Expecting one fd, but got %d\n",
Packit ae235b
                            "Expecting one fd, but got %d\n",
Packit ae235b
                            nfd),
Packit ae235b
                   nfd);
Packit ae235b
Packit ae235b
      for (i = 0; i < nfd; i++)
Packit ae235b
        close (fds[i]);
Packit ae235b
Packit ae235b
      g_free (fds);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  fd = *fds;
Packit ae235b
  g_free (fds);
Packit ae235b
Packit ae235b
  if (fd < 0)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                           _("Received invalid fd"));
Packit ae235b
      fd = -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return fd;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_connection_init (GUnixConnection *connection)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_connection_class_init (GUnixConnectionClass *class)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/* TODO: Other stuff we might want to add are:
Packit ae235b
void                    g_unix_connection_send_fd_async                 (GUnixConnection      *connection,
Packit ae235b
                                                                         gint                  fd,
Packit ae235b
                                                                         gboolean              close,
Packit ae235b
                                                                         gint                  io_priority,
Packit ae235b
                                                                         GAsyncReadyCallback   callback,
Packit ae235b
                                                                         gpointer              user_data);
Packit ae235b
gboolean                g_unix_connection_send_fd_finish                (GUnixConnection      *connection,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
Packit ae235b
gboolean                g_unix_connection_send_fds                      (GUnixConnection      *connection,
Packit ae235b
                                                                         gint                 *fds,
Packit ae235b
                                                                         gint                  nfds,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
void                    g_unix_connection_send_fds_async                (GUnixConnection      *connection,
Packit ae235b
                                                                         gint                 *fds,
Packit ae235b
                                                                         gint                  nfds,
Packit ae235b
                                                                         gint                  io_priority,
Packit ae235b
                                                                         GAsyncReadyCallback   callback,
Packit ae235b
                                                                         gpointer              user_data);
Packit ae235b
gboolean                g_unix_connection_send_fds_finish               (GUnixConnection      *connection,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
Packit ae235b
void                    g_unix_connection_receive_fd_async              (GUnixConnection      *connection,
Packit ae235b
                                                                         gint                  io_priority,
Packit ae235b
                                                                         GAsyncReadyCallback   callback,
Packit ae235b
                                                                         gpointer              user_data);
Packit ae235b
gint                    g_unix_connection_receive_fd_finish             (GUnixConnection      *connection,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
Packit ae235b
Packit ae235b
gboolean                g_unix_connection_send_fake_credentials         (GUnixConnection      *connection,
Packit ae235b
                                                                         guint64               pid,
Packit ae235b
                                                                         guint64               uid,
Packit ae235b
                                                                         guint64               gid,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
void                    g_unix_connection_send_fake_credentials_async   (GUnixConnection      *connection,
Packit ae235b
                                                                         guint64               pid,
Packit ae235b
                                                                         guint64               uid,
Packit ae235b
                                                                         guint64               gid,
Packit ae235b
                                                                         gint                  io_priority,
Packit ae235b
                                                                         GAsyncReadyCallback   callback,
Packit ae235b
                                                                         gpointer              user_data);
Packit ae235b
gboolean                g_unix_connection_send_fake_credentials_finish  (GUnixConnection      *connection,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
Packit ae235b
gboolean                g_unix_connection_create_pair                   (GUnixConnection     **one,
Packit ae235b
                                                                         GUnixConnection     **two,
Packit ae235b
                                                                         GError              **error);
Packit ae235b
*/
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_send_credentials:
Packit ae235b
 * @connection: A #GUnixConnection.
Packit ae235b
 * @cancellable: (nullable): A #GCancellable or %NULL.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Passes the credentials of the current user the receiving side
Packit ae235b
 * of the connection. The receiving end has to call
Packit ae235b
 * g_unix_connection_receive_credentials() (or similar) to accept the
Packit ae235b
 * credentials.
Packit ae235b
 *
Packit ae235b
 * As well as sending the credentials this also writes a single NUL
Packit ae235b
 * byte to the stream, as this is required for credentials passing to
Packit ae235b
 * work on some implementations.
Packit ae235b
 *
Packit ae235b
 * Other ways to exchange credentials with a foreign peer includes the
Packit ae235b
 * #GUnixCredentialsMessage type and g_socket_get_credentials() function.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_unix_connection_send_credentials (GUnixConnection      *connection,
Packit ae235b
                                    GCancellable         *cancellable,
Packit ae235b
                                    GError              **error)
Packit ae235b
{
Packit ae235b
  GCredentials *credentials;
Packit ae235b
  GSocketControlMessage *scm;
Packit ae235b
  GSocket *socket;
Packit ae235b
  gboolean ret;
Packit ae235b
  GOutputVector vector;
Packit ae235b
  guchar nul_byte[1] = {'\0'};
Packit ae235b
  gint num_messages;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
Packit ae235b
  credentials = g_credentials_new ();
Packit ae235b
Packit ae235b
  vector.buffer = &nul_byte;
Packit ae235b
  vector.size = 1;
Packit ae235b
Packit ae235b
  if (g_unix_credentials_message_is_supported ())
Packit ae235b
    {
Packit ae235b
      scm = g_unix_credentials_message_new_with_credentials (credentials);
Packit ae235b
      num_messages = 1;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      scm = NULL;
Packit ae235b
      num_messages = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_get (connection, "socket", &socket, NULL);
Packit ae235b
  if (g_socket_send_message (socket,
Packit ae235b
                             NULL, /* address */
Packit ae235b
                             &vector,
Packit ae235b
                             1,
Packit ae235b
                             &scm,
Packit ae235b
                             num_messages,
Packit ae235b
                             G_SOCKET_MSG_NONE,
Packit ae235b
                             cancellable,
Packit ae235b
                             error) != 1)
Packit ae235b
    {
Packit ae235b
      g_prefix_error (error, _("Error sending credentials: "));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_object_unref (socket);
Packit ae235b
  if (scm != NULL)
Packit ae235b
    g_object_unref (scm);
Packit ae235b
  g_object_unref (credentials);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
send_credentials_async_thread (GTask         *task,
Packit ae235b
			       gpointer       source_object,
Packit ae235b
			       gpointer       task_data,
Packit ae235b
			       GCancellable  *cancellable)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  if (g_unix_connection_send_credentials (G_UNIX_CONNECTION (source_object),
Packit ae235b
					  cancellable,
Packit ae235b
					  &error))
Packit ae235b
    g_task_return_boolean (task, TRUE);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_send_credentials_async:
Packit ae235b
 * @connection: A #GUnixConnection.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Asynchronously send credentials.
Packit ae235b
 *
Packit ae235b
 * For more details, see g_unix_connection_send_credentials() which is
Packit ae235b
 * the synchronous version of this call.
Packit ae235b
 *
Packit ae235b
 * When the operation is finished, @callback will be called. You can then call
Packit ae235b
 * g_unix_connection_send_credentials_finish() to get the result of the operation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_unix_connection_send_credentials_async (GUnixConnection      *connection,
Packit ae235b
                                          GCancellable         *cancellable,
Packit ae235b
                                          GAsyncReadyCallback   callback,
Packit ae235b
                                          gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (connection, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_unix_connection_send_credentials_async);
Packit ae235b
  g_task_run_in_thread (task, send_credentials_async_thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_send_credentials_finish:
Packit ae235b
 * @connection: A #GUnixConnection.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Finishes an asynchronous send credentials operation started with
Packit ae235b
 * g_unix_connection_send_credentials_async().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the operation was successful, otherwise %FALSE.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_unix_connection_send_credentials_finish (GUnixConnection *connection,
Packit ae235b
                                           GAsyncResult    *result,
Packit ae235b
                                           GError         **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, connection), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_receive_credentials:
Packit ae235b
 * @connection: A #GUnixConnection.
Packit ae235b
 * @cancellable: (nullable): A #GCancellable or %NULL.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Receives credentials from the sending end of the connection.  The
Packit ae235b
 * sending end has to call g_unix_connection_send_credentials() (or
Packit ae235b
 * similar) for this to work.
Packit ae235b
 *
Packit ae235b
 * As well as reading the credentials this also reads (and discards) a
Packit ae235b
 * single byte from the stream, as this is required for credentials
Packit ae235b
 * passing to work on some implementations.
Packit ae235b
 *
Packit ae235b
 * Other ways to exchange credentials with a foreign peer includes the
Packit ae235b
 * #GUnixCredentialsMessage type and g_socket_get_credentials() function.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): Received credentials on success (free with
Packit ae235b
 * g_object_unref()), %NULL if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GCredentials *
Packit ae235b
g_unix_connection_receive_credentials (GUnixConnection      *connection,
Packit ae235b
                                       GCancellable         *cancellable,
Packit ae235b
                                       GError              **error)
Packit ae235b
{
Packit ae235b
  GCredentials *ret;
Packit ae235b
  GSocketControlMessage **scms;
Packit ae235b
  gint nscm;
Packit ae235b
  GSocket *socket;
Packit ae235b
  gint n;
Packit ae235b
  gssize num_bytes_read;
Packit ae235b
#ifdef __linux__
Packit ae235b
  gboolean turn_off_so_passcreds;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  ret = NULL;
Packit ae235b
  scms = NULL;
Packit ae235b
Packit ae235b
  g_object_get (connection, "socket", &socket, NULL);
Packit ae235b
Packit ae235b
  /* On Linux, we need to turn on SO_PASSCRED if it isn't enabled
Packit ae235b
   * already. We also need to turn it off when we're done.  See
Packit ae235b
   * #617483 for more discussion.
Packit ae235b
   */
Packit ae235b
#ifdef __linux__
Packit ae235b
  {
Packit ae235b
    gint opt_val;
Packit ae235b
Packit ae235b
    turn_off_so_passcreds = FALSE;
Packit ae235b
    opt_val = 0;
Packit ae235b
    if (!g_socket_get_option (socket,
Packit ae235b
			      SOL_SOCKET,
Packit ae235b
			      SO_PASSCRED,
Packit ae235b
			      &opt_val,
Packit ae235b
			      NULL))
Packit ae235b
      {
Packit ae235b
        int errsv = errno;
Packit ae235b
        g_set_error (error,
Packit ae235b
                     G_IO_ERROR,
Packit ae235b
                     g_io_error_from_errno (errsv),
Packit ae235b
                     _("Error checking if SO_PASSCRED is enabled for socket: %s"),
Packit ae235b
                     g_strerror (errsv));
Packit ae235b
        goto out;
Packit ae235b
      }
Packit ae235b
    if (opt_val == 0)
Packit ae235b
      {
Packit ae235b
        if (!g_socket_set_option (socket,
Packit ae235b
				  SOL_SOCKET,
Packit ae235b
				  SO_PASSCRED,
Packit ae235b
				  TRUE,
Packit ae235b
				  NULL))
Packit ae235b
          {
Packit ae235b
            int errsv = errno;
Packit ae235b
            g_set_error (error,
Packit ae235b
                         G_IO_ERROR,
Packit ae235b
                         g_io_error_from_errno (errsv),
Packit ae235b
                         _("Error enabling SO_PASSCRED: %s"),
Packit ae235b
                         g_strerror (errsv));
Packit ae235b
            goto out;
Packit ae235b
          }
Packit ae235b
        turn_off_so_passcreds = TRUE;
Packit ae235b
      }
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_type_ensure (G_TYPE_UNIX_CREDENTIALS_MESSAGE);
Packit ae235b
  num_bytes_read = g_socket_receive_message (socket,
Packit ae235b
                                             NULL, /* GSocketAddress **address */
Packit ae235b
                                             NULL,
Packit ae235b
                                             0,
Packit ae235b
                                             &scms,
Packit ae235b
                                             &nscm,
Packit ae235b
                                             NULL,
Packit ae235b
                                             cancellable,
Packit ae235b
                                             error);
Packit ae235b
  if (num_bytes_read != 1)
Packit ae235b
    {
Packit ae235b
      /* Handle situation where g_socket_receive_message() returns
Packit ae235b
       * 0 bytes and not setting @error
Packit ae235b
       */
Packit ae235b
      if (num_bytes_read == 0 && error != NULL && *error == NULL)
Packit ae235b
        {
Packit ae235b
          g_set_error_literal (error,
Packit ae235b
                               G_IO_ERROR,
Packit ae235b
                               G_IO_ERROR_FAILED,
Packit ae235b
                               _("Expecting to read a single byte for receiving credentials but read zero bytes"));
Packit ae235b
        }
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_unix_credentials_message_is_supported () &&
Packit ae235b
      /* Fall back on get_credentials if the other side didn't send the credentials */
Packit ae235b
      nscm > 0)
Packit ae235b
    {
Packit ae235b
      if (nscm != 1)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_FAILED,
Packit ae235b
                       ngettext("Expecting 1 control message, got %d",
Packit ae235b
                                "Expecting 1 control message, got %d",
Packit ae235b
                                nscm),
Packit ae235b
                       nscm);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
Packit ae235b
        {
Packit ae235b
          g_set_error_literal (error,
Packit ae235b
                               G_IO_ERROR,
Packit ae235b
                               G_IO_ERROR_FAILED,
Packit ae235b
                               _("Unexpected type of ancillary data"));
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
Packit ae235b
      g_object_ref (ret);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (nscm != 0)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_FAILED,
Packit ae235b
                       _("Not expecting control message, but got %d"),
Packit ae235b
                       nscm);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          ret = g_socket_get_credentials (socket, error);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
Packit ae235b
#ifdef __linux__
Packit ae235b
  if (turn_off_so_passcreds)
Packit ae235b
    {
Packit ae235b
      if (!g_socket_set_option (socket,
Packit ae235b
				SOL_SOCKET,
Packit ae235b
				SO_PASSCRED,
Packit ae235b
				FALSE,
Packit ae235b
				NULL))
Packit ae235b
        {
Packit ae235b
          int errsv = errno;
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       g_io_error_from_errno (errsv),
Packit ae235b
                       _("Error while disabling SO_PASSCRED: %s"),
Packit ae235b
                       g_strerror (errsv));
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (scms != NULL)
Packit ae235b
    {
Packit ae235b
      for (n = 0; n < nscm; n++)
Packit ae235b
        g_object_unref (scms[n]);
Packit ae235b
      g_free (scms);
Packit ae235b
    }
Packit ae235b
  g_object_unref (socket);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
receive_credentials_async_thread (GTask         *task,
Packit ae235b
				  gpointer       source_object,
Packit ae235b
				  gpointer       task_data,
Packit ae235b
				  GCancellable  *cancellable)
Packit ae235b
{
Packit ae235b
  GCredentials *creds;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  creds = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (source_object),
Packit ae235b
                                                 cancellable,
Packit ae235b
                                                 &error);
Packit ae235b
  if (creds)
Packit ae235b
    g_task_return_pointer (task, creds, g_object_unref);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_receive_credentials_async:
Packit ae235b
 * @connection: A #GUnixConnection.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Asynchronously receive credentials.
Packit ae235b
 *
Packit ae235b
 * For more details, see g_unix_connection_receive_credentials() which is
Packit ae235b
 * the synchronous version of this call.
Packit ae235b
 *
Packit ae235b
 * When the operation is finished, @callback will be called. You can then call
Packit ae235b
 * g_unix_connection_receive_credentials_finish() to get the result of the operation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_unix_connection_receive_credentials_async (GUnixConnection      *connection,
Packit ae235b
                                              GCancellable         *cancellable,
Packit ae235b
                                              GAsyncReadyCallback   callback,
Packit ae235b
                                              gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (connection, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_unix_connection_receive_credentials_async);
Packit ae235b
  g_task_run_in_thread (task, receive_credentials_async_thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_connection_receive_credentials_finish:
Packit ae235b
 * @connection: A #GUnixConnection.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Finishes an asynchronous receive credentials operation started with
Packit ae235b
 * g_unix_connection_receive_credentials_async().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GCredentials, or %NULL on error.
Packit ae235b
 *     Free the returned object with g_object_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GCredentials *
Packit ae235b
g_unix_connection_receive_credentials_finish (GUnixConnection *connection,
Packit ae235b
                                              GAsyncResult    *result,
Packit ae235b
                                              GError         **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, connection), NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
}