Coverage Report

Created: 2022-12-08 06:09

/src/libassuan/src/assuan-pipe-server.c
Line
Count
Source (jump to first uncovered line)
1
/* assuan-pipe-server.c - Assuan server working over a pipe
2
 * Copyright (C) 2001, 2002, 2009 Free Software Foundation, Inc.
3
 *
4
 * This file is part of Assuan.
5
 *
6
 * Assuan is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * Assuan is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18
 * SPDX-License-Identifier: LGPL-2.1+
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include <config.h>
23
#endif
24
25
#include <stdlib.h>
26
#include <stdio.h>
27
#ifdef HAVE_SYS_TYPES_H
28
# include <sys/types.h>
29
#endif
30
#ifdef HAVE_SYS_STAT_H
31
# include <sys/stat.h>
32
#endif
33
#ifdef HAVE_UNISTD_H
34
# include <unistd.h>
35
#endif
36
#ifdef HAVE_W32_SYSTEM
37
# ifdef HAVE_WINSOCK2_H
38
#  include <winsock2.h>
39
# endif
40
# include <windows.h>
41
#ifdef HAVE_FCNTL_H
42
# include <fcntl.h>
43
#endif
44
#endif
45
46
#include "assuan-defs.h"
47
#include "debug.h"
48
49
/* Returns true if atoi(S) denotes a valid socket. */
50
#ifndef HAVE_W32_SYSTEM
51
static int
52
is_valid_socket (const char *s)
53
0
{
54
0
  struct stat buf;
55
56
0
  if ( fstat (atoi (s), &buf ) )
57
0
    return 0;
58
0
  return S_ISSOCK (buf.st_mode);
59
0
}
60
#endif /*!HAVE_W32_SYSTEM*/
61
62
63
/* This actually is a int file descriptor (and not assuan_fd_t) as
64
   _get_osfhandle is called on W32 systems.  */
65
gpg_error_t
66
assuan_init_pipe_server (assuan_context_t ctx, assuan_fd_t filedes[2])
67
0
{
68
0
  const char *s;
69
0
  unsigned long ul;
70
0
  gpg_error_t rc;
71
0
  assuan_fd_t infd = ASSUAN_INVALID_FD;
72
0
  assuan_fd_t outfd = ASSUAN_INVALID_FD;
73
0
  int is_usd = 0;
74
0
  TRACE_BEG (ctx, ASSUAN_LOG_CTX, "assuan_init_pipe_server", ctx);
75
0
  if (filedes)
76
0
    {
77
0
      TRACE_LOG2 ("fd[0]=0x%x, fd[1]=0x%x", filedes[0], filedes[1]);
78
0
    }
79
80
0
  rc = _assuan_register_std_commands (ctx);
81
0
  if (rc)
82
0
    return TRACE_ERR (rc);
83
84
#ifdef HAVE_W32_SYSTEM
85
  if (filedes)
86
    {
87
      infd  = filedes[0];
88
      outfd = filedes[1];
89
    }
90
  else
91
    {
92
      infd = assuan_fd_from_posix_fd (0);
93
      outfd = assuan_fd_from_posix_fd (1);
94
    }
95
#else
96
0
  s = getenv ("_assuan_connection_fd");
97
0
  if (s && *s && is_valid_socket (s))
98
0
    {
99
      /* Well, we are called with an bi-directional file descriptor.
100
   Prepare for using sendmsg/recvmsg.  In this case we ignore
101
   the passed file descriptors. */
102
0
      infd = atoi (s);
103
0
      outfd = atoi (s);
104
0
      is_usd = 1;
105
106
0
    }
107
0
  else if (filedes && filedes[0] != ASSUAN_INVALID_FD
108
0
     && filedes[1] != ASSUAN_INVALID_FD )
109
0
    {
110
      /* Standard pipe server. */
111
0
      infd = filedes[0];
112
0
      outfd = filedes[1];
113
0
    }
114
0
  else
115
0
    {
116
0
      rc = _assuan_error (ctx, GPG_ERR_ASS_SERVER_START);
117
0
      return TRACE_ERR (rc);
118
0
    }
119
0
#endif
120
121
0
  ctx->flags.is_server = 1;
122
0
  ctx->engine.release = _assuan_server_release;
123
0
  ctx->engine.readfnc = _assuan_simple_read;
124
0
  ctx->engine.writefnc = _assuan_simple_write;
125
0
  ctx->engine.sendfd = NULL;
126
#ifdef HAVE_W32_SYSTEM
127
  ctx->engine.receivefd = w32_fdpass_recv;
128
#else
129
0
  ctx->engine.receivefd = NULL;
130
0
#endif
131
0
  ctx->max_accepts = 1;
132
133
0
  s = getenv ("_assuan_pipe_connect_pid");
134
0
  if (s && (ul=strtoul (s, NULL, 10)) && ul)
135
0
    ctx->pid = (pid_t)ul;
136
0
  else
137
0
    ctx->pid = (pid_t)-1;
138
0
  ctx->accept_handler = NULL;
139
0
  ctx->finish_handler = _assuan_server_finish;
140
0
  ctx->inbound.fd = infd;
141
0
  ctx->outbound.fd = outfd;
142
143
0
  if (is_usd)
144
0
    _assuan_init_uds_io (ctx);
145
146
0
  return TRACE_SUC();
147
0
}