Blame src/assuan-socket-server.c

Packit 3c2767
/* assuan-socket-server.c - Assuan socket based server
Packit 3c2767
 * Copyright (C) 2002, 2007, 2009 Free Software Foundation, Inc.
Packit 3c2767
 *
Packit 3c2767
 * This file is part of Assuan.
Packit 3c2767
 *
Packit 3c2767
 * Assuan is free software; you can redistribute it and/or modify it
Packit 3c2767
 * under the terms of the GNU Lesser General Public License as
Packit 3c2767
 * published by the Free Software Foundation; either version 2.1 of
Packit 3c2767
 * the License, or (at your option) any later version.
Packit 3c2767
 *
Packit 3c2767
 * Assuan is distributed in the hope that it will be useful, but
Packit 3c2767
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3c2767
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3c2767
 * Lesser General Public License for more details.
Packit 3c2767
 *
Packit 3c2767
 * You should have received a copy of the GNU Lesser General Public
Packit 3c2767
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 3c2767
 * SPDX-License-Identifier: LGPL-2.1+
Packit 3c2767
 */
Packit 3c2767
Packit 3c2767
#ifdef HAVE_CONFIG_H
Packit 3c2767
#include <config.h>
Packit 3c2767
#endif
Packit 3c2767
Packit 3c2767
#include <stdlib.h>
Packit 3c2767
#include <stdio.h>
Packit 3c2767
#include <errno.h>
Packit 3c2767
#ifdef HAVE_UNISTD_H
Packit 3c2767
# include <unistd.h>
Packit 3c2767
#endif
Packit 3c2767
#ifdef HAVE_SYS_TYPES_H
Packit 3c2767
# include <sys/types.h>
Packit 3c2767
#endif
Packit 3c2767
#ifdef HAVE_UCRED_H
Packit 3c2767
#include <ucred.h>
Packit 3c2767
#endif
Packit 3c2767
#ifdef HAVE_W32_SYSTEM
Packit 3c2767
# ifdef HAVE_WINSOCK2_H
Packit 3c2767
#  include <winsock2.h>
Packit 3c2767
# endif
Packit 3c2767
# include <windows.h>
Packit 3c2767
# if HAVE_SYS_SOCKET_H
Packit 3c2767
#  include <sys/socket.h>
Packit 3c2767
# elif HAVE_WS2TCPIP_H
Packit 3c2767
#  include <ws2tcpip.h>
Packit 3c2767
# endif
Packit 3c2767
#else
Packit 3c2767
# include <sys/socket.h>
Packit 3c2767
# include <sys/un.h>
Packit 3c2767
#endif
Packit 3c2767
Packit 3c2767
#include "debug.h"
Packit 3c2767
#include "assuan-defs.h"
Packit 3c2767
Packit 3c2767
static gpg_error_t
Packit 3c2767
accept_connection_bottom (assuan_context_t ctx)
Packit 3c2767
{
Packit 3c2767
  assuan_fd_t fd = ctx->connected_fd;
Packit 3c2767
Packit 3c2767
  TRACE (ctx, ASSUAN_LOG_SYSIO, "accept_connection_bottom", ctx);
Packit 3c2767
Packit 3c2767
  ctx->peercred_valid = 0;
Packit 3c2767
#ifdef HAVE_SO_PEERCRED
Packit 3c2767
  {
Packit 3c2767
    struct ucred cr;
Packit 3c2767
    socklen_t cl = sizeof cr;
Packit 3c2767
Packit 3c2767
    if ( !getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl))
Packit 3c2767
      {
Packit 3c2767
         ctx->peercred.pid = cr.pid;
Packit 3c2767
         ctx->peercred.uid = cr.uid;
Packit 3c2767
         ctx->peercred.gid = cr.gid;
Packit 3c2767
         ctx->peercred_valid = 1;
Packit 3c2767
Packit 3c2767
         /* This overrides any already set PID if the function returns
Packit 3c2767
            a valid one. */
Packit 3c2767
         if (cr.pid != ASSUAN_INVALID_PID && cr.pid)
Packit 3c2767
           ctx->pid = cr.pid;
Packit 3c2767
      }
Packit 3c2767
  }
Packit 3c2767
#elif defined (HAVE_GETPEERUCRED)
Packit 3c2767
  {
Packit 3c2767
    ucred_t *ucred = NULL;
Packit 3c2767
Packit 3c2767
    if (getpeerucred (fd, &ucred) != -1)
Packit 3c2767
      {
Packit 3c2767
        ctx->peercred.uid = ucred_geteuid (ucred);
Packit 3c2767
        ctx->peercred.gid = ucred_getegid (ucred);
Packit 3c2767
	ctx->peercred.pid = ucred_getpid (ucred);
Packit 3c2767
	ctx->peercred_valid = 1;
Packit 3c2767
	ucred_free (ucred);
Packit 3c2767
      }
Packit 3c2767
  }
Packit 3c2767
#elif defined (HAVE_LOCAL_PEEREID)
Packit 3c2767
  {
Packit 3c2767
    struct unpcbid unp;
Packit 3c2767
    socklen_t unpl = sizeof unp;
Packit 3c2767
Packit 3c2767
    if (getsockopt (fd, 0, LOCAL_PEEREID, &unp, &unpl) != -1)
Packit 3c2767
      {
Packit 3c2767
	ctx->peercred.pid = unp.unp_pid;
Packit 3c2767
	ctx->peercred.uid = unp.unp_euid;
Packit 3c2767
	ctx->peercred.gid = unp.unp_egid;
Packit 3c2767
	ctx->peercred_valid = 1;
Packit 3c2767
      }
Packit 3c2767
  }
Packit 3c2767
#elif defined(HAVE_GETPEEREID)
Packit 3c2767
  {
Packit 3c2767
    if (getpeereid (fd, &ctx->peercred.uid, &ctx->peercred.gid) != -1)
Packit 3c2767
      {
Packit 3c2767
	ctx->peercred.pid = ASSUAN_INVALID_PID;
Packit 3c2767
	ctx->peercred_valid = 1;
Packit 3c2767
      }
Packit 3c2767
  }
Packit 3c2767
#endif
Packit 3c2767
Packit 3c2767
  ctx->inbound.fd = fd;
Packit 3c2767
  ctx->inbound.eof = 0;
Packit 3c2767
  ctx->inbound.linelen = 0;
Packit 3c2767
  ctx->inbound.attic.linelen = 0;
Packit 3c2767
  ctx->inbound.attic.pending = 0;
Packit 3c2767
Packit 3c2767
  ctx->outbound.fd = fd;
Packit 3c2767
  ctx->outbound.data.linelen = 0;
Packit 3c2767
  ctx->outbound.data.error = 0;
Packit 3c2767
Packit 3c2767
  ctx->flags.confidential = 0;
Packit 3c2767
Packit 3c2767
  return 0;
Packit 3c2767
}
Packit 3c2767
Packit 3c2767
Packit 3c2767
static gpg_error_t
Packit 3c2767
accept_connection (assuan_context_t ctx)
Packit 3c2767
{
Packit 3c2767
  assuan_fd_t fd;
Packit 3c2767
  struct sockaddr_un clnt_addr;
Packit 3c2767
  socklen_t len = sizeof clnt_addr;
Packit 3c2767
Packit 3c2767
  TRACE1 (ctx, ASSUAN_LOG_SYSIO, "accept_connection", ctx,
Packit 3c2767
         "listen_fd=0x%x", ctx->listen_fd);
Packit 3c2767
Packit 3c2767
  fd = SOCKET2HANDLE(accept (HANDLE2SOCKET(ctx->listen_fd),
Packit 3c2767
                             (struct sockaddr*)&clnt_addr, &len ));
Packit 3c2767
  if (fd == ASSUAN_INVALID_FD)
Packit 3c2767
    {
Packit 3c2767
      return _assuan_error (ctx, gpg_err_code_from_syserror ());
Packit 3c2767
    }
Packit 3c2767
  TRACE1 (ctx, ASSUAN_LOG_SYSIO, "accept_connection", ctx,
Packit 3c2767
          "fd->0x%x", fd);
Packit 3c2767
  if (_assuan_sock_check_nonce (ctx, fd, &ctx->listen_nonce))
Packit 3c2767
    {
Packit 3c2767
      _assuan_close (ctx, fd);
Packit 3c2767
      return _assuan_error (ctx, GPG_ERR_ASS_ACCEPT_FAILED);
Packit 3c2767
    }
Packit 3c2767
Packit 3c2767
  ctx->connected_fd = fd;
Packit 3c2767
  return accept_connection_bottom (ctx);
Packit 3c2767
}
Packit 3c2767
Packit 3c2767
Packit 3c2767
/*
Packit 3c2767
   Flag bits: 0 - use sendmsg/recvmsg to allow descriptor passing
Packit 3c2767
              1 - FD has already been accepted.
Packit 3c2767
*/
Packit 3c2767
gpg_error_t
Packit 3c2767
assuan_init_socket_server (assuan_context_t ctx, assuan_fd_t fd,
Packit 3c2767
			   unsigned int flags)
Packit 3c2767
{
Packit 3c2767
  gpg_error_t rc;
Packit 3c2767
  TRACE_BEG2 (ctx, ASSUAN_LOG_CTX, "assuan_init_socket_server", ctx,
Packit 3c2767
	      "fd=0x%x, flags=0x%x", fd, flags);
Packit 3c2767
Packit 3c2767
  rc = _assuan_register_std_commands (ctx);
Packit 3c2767
  if (rc)
Packit 3c2767
    return TRACE_ERR (rc);
Packit 3c2767
Packit 3c2767
  ctx->engine.release = _assuan_server_release;
Packit 3c2767
  ctx->engine.readfnc = _assuan_simple_read;
Packit 3c2767
  ctx->engine.writefnc = _assuan_simple_write;
Packit 3c2767
  ctx->engine.sendfd = NULL;
Packit 3c2767
  ctx->engine.receivefd = NULL;
Packit 3c2767
  ctx->is_server = 1;
Packit 3c2767
  if (flags & ASSUAN_SOCKET_SERVER_ACCEPTED)
Packit 3c2767
    /* We want a second accept to indicate EOF. */
Packit 3c2767
    ctx->max_accepts = 1;
Packit 3c2767
  else
Packit 3c2767
    ctx->max_accepts = -1;
Packit 3c2767
  ctx->input_fd = ASSUAN_INVALID_FD;
Packit 3c2767
  ctx->output_fd = ASSUAN_INVALID_FD;
Packit 3c2767
Packit 3c2767
  ctx->inbound.fd = ASSUAN_INVALID_FD;
Packit 3c2767
  ctx->outbound.fd = ASSUAN_INVALID_FD;
Packit 3c2767
Packit 3c2767
  if (flags & ASSUAN_SOCKET_SERVER_ACCEPTED)
Packit 3c2767
    {
Packit 3c2767
      ctx->listen_fd = ASSUAN_INVALID_FD;
Packit 3c2767
      ctx->connected_fd = fd;
Packit 3c2767
    }
Packit 3c2767
  else
Packit 3c2767
    {
Packit 3c2767
      ctx->listen_fd = fd;
Packit 3c2767
      ctx->connected_fd = ASSUAN_INVALID_FD;
Packit 3c2767
    }
Packit 3c2767
  ctx->accept_handler = ((flags & ASSUAN_SOCKET_SERVER_ACCEPTED)
Packit 3c2767
                         ? accept_connection_bottom
Packit 3c2767
                         : accept_connection);
Packit 3c2767
  ctx->finish_handler = _assuan_server_finish;
Packit 3c2767
Packit 3c2767
  if (flags & ASSUAN_SOCKET_SERVER_FDPASSING)
Packit 3c2767
    _assuan_init_uds_io (ctx);
Packit 3c2767
Packit 3c2767
  rc = _assuan_register_std_commands (ctx);
Packit 3c2767
  if (rc)
Packit 3c2767
    _assuan_reset (ctx);
Packit 3c2767
  return TRACE_ERR (rc);
Packit 3c2767
}
Packit 3c2767
Packit 3c2767
Packit 3c2767
/* Save a copy of NONCE in context CTX.  This should be used to
Packit 3c2767
   register the server's nonce with an context established by
Packit 3c2767
   assuan_init_socket_server.  */
Packit 3c2767
void
Packit 3c2767
assuan_set_sock_nonce (assuan_context_t ctx, assuan_sock_nonce_t *nonce)
Packit 3c2767
{
Packit 3c2767
  if (ctx && nonce)
Packit 3c2767
    ctx->listen_nonce = *nonce;
Packit 3c2767
}