summaryrefslogtreecommitdiff
path: root/src/assuan-support.c
blob: 925aebaf0a5cf74d8b5a87482f0f29c177911695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* assuan-support.c - Assuan wrappers
 * Copyright (C) 2009 g10 Code GmbH
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <https://gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */


#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <stdlib.h>
#include <errno.h>

#include "assuan.h"

#include "gpgme.h"
#include "ath.h"
#include "priv-io.h"
#include "debug.h"


struct assuan_malloc_hooks _gpgme_assuan_malloc_hooks =
  {
    malloc,
    realloc,
    free
  };


int
_gpgme_assuan_log_cb (assuan_context_t ctx, void *hook,
		      unsigned int cat, const char *msg)
{
  (void)ctx;
  (void)hook;
  (void)cat;

  if (msg == NULL)
    return 1;

  _gpgme_debug (DEBUG_ASSUAN, -1, NULL, NULL, NULL, "%s", msg);
  return 0;
}


static void
my_usleep (assuan_context_t ctx, unsigned int usec)
{
  /* FIXME: Add to ath.  */
  __assuan_usleep (ctx, usec);
}


/* Create a pipe with an inheritable end.  */
static int
my_pipe (assuan_context_t ctx, assuan_fd_t fds[2], int inherit_idx)
{
  int res;
  int gfds[2];

  (void)ctx;

  res = _gpgme_io_pipe (gfds, inherit_idx);

  /* For now... */
  fds[0] = (assuan_fd_t) gfds[0];
  fds[1] = (assuan_fd_t) gfds[1];

  return res;
}


/* Close the given file descriptor, created with _assuan_pipe or one
   of the socket functions.  */
static int
my_close (assuan_context_t ctx, assuan_fd_t fd)
{
  (void)ctx;
  return _gpgme_io_close ((int) fd);
}


static gpgme_ssize_t
my_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size)
{
  (void)ctx;
  return _gpgme_io_read ((int) fd, buffer, size);
}


static gpgme_ssize_t
my_write (assuan_context_t ctx, assuan_fd_t fd, const void *buffer, size_t size)
{
  (void)ctx;
  return _gpgme_io_write ((int) fd, buffer, size);
}


static int
my_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg,
	    int flags)
{
  (void)ctx;
#ifdef HAVE_W32_SYSTEM
  (void)fd;
  (void)msg;
  (void)flags;
  gpg_err_set_errno (ENOSYS);
  return -1;
#else
  return _gpgme_io_recvmsg ((int) fd, msg, flags);
#endif
}



static int
my_sendmsg (assuan_context_t ctx, assuan_fd_t fd, const assuan_msghdr_t msg,
	    int flags)
{
  (void)ctx;
#ifdef HAVE_W32_SYSTEM
  (void)fd;
  (void)msg;
  (void)flags;
  gpg_err_set_errno (ENOSYS);
  return -1;
#else
  return _gpgme_io_sendmsg ((int) fd, msg, flags);
#endif
}


/* If NAME is NULL, don't exec, just fork.  FD_CHILD_LIST is modified
   to reflect the value of the FD in the peer process (on
   Windows).  */
static int
my_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name,
	  const char **argv,
	  assuan_fd_t fd_in, assuan_fd_t fd_out,
	  assuan_fd_t *fd_child_list,
	  void (*atfork) (void *opaque, int reserved),
	  void *atforkvalue, unsigned int flags)
{
  int err = 0;
  struct spawn_fd_item_s *fd_items;
  int i;

  (void)ctx;
  (void)flags;

  assert (name);

  if (! name)
    {
      gpg_err_set_errno (ENOSYS);
      return -1;
    }

  i = 0;
  if (fd_child_list)
    {
      while (fd_child_list[i] != ASSUAN_INVALID_FD)
	i++;
    }
  /* fd_in, fd_out, terminator */
  i += 3;
  fd_items = calloc (i, sizeof (struct spawn_fd_item_s));
  if (! fd_items)
    return -1;
  i = 0;
  if (fd_child_list)
    {
      while (fd_child_list[i] != ASSUAN_INVALID_FD)
	{
	  fd_items[i].fd = (int) fd_child_list[i];
	  fd_items[i].dup_to = -1;
	  i++;
	}
    }
  if (fd_in != ASSUAN_INVALID_FD)
    {
      fd_items[i].fd = (int) fd_in;
      fd_items[i].dup_to = 0;
      i++;
    }
  if (fd_out != ASSUAN_INVALID_FD)
    {
      fd_items[i].fd = (int) fd_out;
      fd_items[i].dup_to = 1;
      i++;
    }
  fd_items[i].fd = -1;
  fd_items[i].dup_to = -1;

#ifdef HAVE_W32_SYSTEM
  /* Fix up a potential logger fd so that on windows the fd
   * translation can work through gpgme-w32spawn.
   *
   * We do this here as a hack because we would
   * otherwise have to change assuan_api and the current
   * plan in 2019 is to change away from this to gpgrt
   * based IPC. */
  if (argv)
    {
      int loc = 0;
      while (argv[loc])
        {
          if (!strcmp ("--logger-fd", argv[loc]))
            {
              long logger_fd = -1;
              char *tail;
              int k = 0;
              loc++;
              if (!argv[loc])
                {
                  err = GPG_ERR_INV_ARG;
                  break;
                }
              logger_fd = strtol (argv[loc], &tail, 10);
              if (tail == argv[loc] || logger_fd <= 0)
                {
                  err = GPG_ERR_INV_ARG;
                  break;
                }
              while (fd_items[k++].fd != -1)
                {
                  if (fd_items[k].fd == logger_fd)
                    {
                      fd_items[k].arg_loc = loc;
                      break;
                    }
                }
              break;
            }
          loc++;
        }
    }
#endif

  if (!err)
    {
      err = _gpgme_io_spawn (name, (char*const*)argv,
                             (IOSPAWN_FLAG_NOCLOSE | IOSPAWN_FLAG_DETACHED),
                             fd_items, atfork, atforkvalue, r_pid);
    }
  if (!err)
    {
      i = 0;

      if (fd_child_list)
	{
	  while (fd_child_list[i] != ASSUAN_INVALID_FD)
	    {
	      fd_child_list[i] = (assuan_fd_t) fd_items[i].peer_name;
	      i++;
	    }
	}
    }
  free (fd_items);
  return err;
}


/* If action is 0, like waitpid.  If action is 1, just release the PID?  */
static pid_t
my_waitpid (assuan_context_t ctx, pid_t pid,
	    int nowait, int *status, int options)
{
  (void)ctx;
#ifdef HAVE_W32_SYSTEM
  (void)nowait;
  (void)status;
  (void)options;
  (void)pid;  /* Just a number without a kernel object.  */
#else
  /* We can't just release the PID, a waitpid is mandatory.  But
     NOWAIT in POSIX systems just means the caller already did the
     waitpid for this child.  */
  if (! nowait)
    return _gpgme_ath_waitpid (pid, status, options);
#endif
  return 0;
}




static int
my_socketpair (assuan_context_t ctx, int namespace, int style,
	       int protocol, assuan_fd_t filedes[2])
{
#ifdef HAVE_W32_SYSTEM
  (void)ctx;
  (void)namespace;
  (void)style;
  (void)protocol;
  (void)filedes;
  gpg_err_set_errno (ENOSYS);
  return -1;
#else
  /* FIXME: Debug output missing.  */
  return __assuan_socketpair (ctx, namespace, style, protocol, filedes);
#endif
}


static int
my_socket (assuan_context_t ctx, int namespace, int style, int protocol)
{
  (void)ctx;
  return _gpgme_io_socket (namespace, style, protocol);
}


static int
my_connect (assuan_context_t ctx, int sock, struct sockaddr *addr,
	    socklen_t length)
{
  (void)ctx;
  return _gpgme_io_connect (sock, addr, length);
}


/* Note for Windows: Ignore the incompatible pointer type warning for
   my_read and my_write.  Mingw has been changed to use int for
   ssize_t on 32 bit systems while we use long.  For 64 bit we use
   int64_t while mingw uses __int64_t.  It doe not matter at all
   because under Windows long and int are both 32 bit even on 64
   bit.  */
struct assuan_system_hooks _gpgme_assuan_system_hooks =
  {
    ASSUAN_SYSTEM_HOOKS_VERSION,
    my_usleep,
    my_pipe,
    my_close,
    my_read,
    my_write,
    my_recvmsg,
    my_sendmsg,
    my_spawn,
    my_waitpid,
    my_socketpair,
    my_socket,
    my_connect
  };