summaryrefslogtreecommitdiff
path: root/src/engine-spawn.c
blob: eb4e0385ddc0e1a5a264475988199f2542ccb832 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* engine-spawn.c - Run an arbitrary program
   Copyright (C) 2014 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 <http://www.gnu.org/licenses/>.
*/

#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

#include "gpgme.h"
#include "util.h"
#include "ops.h"
#include "wait.h"
#include "context.h"  /*temp hack until we have GpmeData methods to do I/O */
#include "priv-io.h"
#include "sema.h"
#include "debug.h"

#include "engine-backend.h"


/* This type is used to build a list of data sources/sinks.  */
struct datalist_s
{
  struct datalist_s *next;
  gpgme_data_t data;  /* The data object. */
  int inbound;        /* True if this is used for reading from the peer.  */
  int dup_to;         /* The fd used by the peer.  */
};


struct fd_data_map_s
{
  gpgme_data_t data;
  int inbound;  /* True if this is used for reading from the peer. */
  int dup_to;   /* Dup the fd to that one.  */
  int fd;       /* The fd to use.  */
  int peer_fd;  /* The other side of the pipe. */
  void *tag;    /* Tag used by the I/O callback.  */
};


struct engine_spawn
{
  struct datalist_s *arglist;
  struct datalist_s **argtail;

  struct fd_data_map_s *fd_data_map;

  struct gpgme_io_cbs io_cbs;
};
typedef struct engine_spawn *engine_spawn_t;


static void engspawn_io_event (void *engine,
                               gpgme_event_io_t type, void *type_data);
static gpgme_error_t engspawn_cancel (void *engine);



static void
close_notify_handler (int fd, void *opaque)
{
  engine_spawn_t esp = opaque;
  int i;

  assert (fd != -1);

  if (esp->fd_data_map)
    {
      for (i = 0; esp->fd_data_map[i].data; i++)
	{
	  if (esp->fd_data_map[i].fd == fd)
	    {
	      if (esp->fd_data_map[i].tag)
		(*esp->io_cbs.remove) (esp->fd_data_map[i].tag);
	      esp->fd_data_map[i].fd = -1;
	      break;
            }
	  if (esp->fd_data_map[i].peer_fd == fd)
	    {
	      esp->fd_data_map[i].peer_fd = -1;
	      break;
            }
        }
    }
}


static gpgme_error_t
add_data (engine_spawn_t esp, gpgme_data_t data, int dup_to, int inbound)
{
  struct datalist_s *a;

  assert (esp);
  assert (data);

  a = malloc (sizeof *a);
  if (!a)
    return gpg_error_from_syserror ();
  a->next = NULL;
  a->data = data;
  a->inbound = inbound;
  a->dup_to = dup_to;
  *esp->argtail = a;
  esp->argtail = &a->next;
  return 0;
}


static void
free_fd_data_map (struct fd_data_map_s *fd_data_map)
{
  int i;

  if (!fd_data_map)
    return;

  for (i = 0; fd_data_map[i].data; i++)
    {
      if (fd_data_map[i].fd != -1)
	_gpgme_io_close (fd_data_map[i].fd);
      if (fd_data_map[i].peer_fd != -1)
	_gpgme_io_close (fd_data_map[i].peer_fd);
      /* Don't release data because this is only a reference.  */
    }
  free (fd_data_map);
}


static gpgme_error_t
build_fd_data_map (engine_spawn_t esp)
{
  struct datalist_s *a;
  size_t datac;
  int fds[2];

  for (datac = 0, a = esp->arglist; a; a = a->next)
    if (a->data)
      datac++;

  free_fd_data_map (esp->fd_data_map);
  esp->fd_data_map = calloc (datac + 1, sizeof *esp->fd_data_map);
  if (!esp->fd_data_map)
    return gpg_error_from_syserror ();

  for (datac = 0, a = esp->arglist; a; a = a->next)
    {
      assert (a->data);

      if (_gpgme_io_pipe (fds, a->inbound ? 1 : 0) == -1)
        {
          free (esp->fd_data_map);
          esp->fd_data_map = NULL;
          return gpg_error_from_syserror ();
        }
      if (_gpgme_io_set_close_notify (fds[0], close_notify_handler, esp)
          || _gpgme_io_set_close_notify (fds[1], close_notify_handler, esp))
        {
          /* FIXME: Need error cleanup.  */
          return gpg_error (GPG_ERR_GENERAL);
        }

      esp->fd_data_map[datac].inbound = a->inbound;
      if (a->inbound)
        {
          esp->fd_data_map[datac].fd       = fds[0];
          esp->fd_data_map[datac].peer_fd  = fds[1];
        }
      else
        {
          esp->fd_data_map[datac].fd       = fds[1];
          esp->fd_data_map[datac].peer_fd  = fds[0];
        }
      esp->fd_data_map[datac].data    = a->data;
      esp->fd_data_map[datac].dup_to  = a->dup_to;
      datac++;
    }

  return 0;
}


static gpgme_error_t
add_io_cb (engine_spawn_t esp, int fd, int dir, gpgme_io_cb_t handler,
           void *data, void **tag)
{
  gpgme_error_t err;

  err = (*esp->io_cbs.add) (esp->io_cbs.add_priv, fd, dir, handler, data, tag);
  if (err)
    return err;
  if (!dir) /* Fixme: Kludge around poll() problem.  */
    err = _gpgme_io_set_nonblocking (fd);
  return err;
}


static gpgme_error_t
engspawn_start (engine_spawn_t esp, const char *file, const char *argv[],
                unsigned int flags)
{
  gpgme_error_t err;
  int i, n;
  int status;
  struct spawn_fd_item_s *fd_list;
  pid_t pid;
  unsigned int spflags;
  const char *save_argv0 = NULL;

  if (!esp || !file || !argv || !argv[0])
    return gpg_error (GPG_ERR_INV_VALUE);

  spflags = 0;
  if ((flags & GPGME_SPAWN_DETACHED))
    spflags |= IOSPAWN_FLAG_DETACHED;
  if ((flags & GPGME_SPAWN_ALLOW_SET_FG))
    spflags |= IOSPAWN_FLAG_ALLOW_SET_FG;


  err = build_fd_data_map (esp);
  if (err)
    return err;

  n = 0;
  for (i = 0; esp->fd_data_map[i].data; i++)
    n++;
  fd_list = calloc (n+1, sizeof *fd_list);
  if (!fd_list)
    return gpg_error_from_syserror ();

  /* Build the fd list for the child.  */
  n = 0;
  for (i = 0; esp->fd_data_map[i].data; i++)
    {
      fd_list[n].fd = esp->fd_data_map[i].peer_fd;
      fd_list[n].dup_to = esp->fd_data_map[i].dup_to;
      n++;
    }
  fd_list[n].fd = -1;
  fd_list[n].dup_to = -1;

  if (argv[0] && !*argv[0])
    {
      save_argv0 = argv[0];
      argv[0] = _gpgme_get_basename (file);
    }
  status = _gpgme_io_spawn (file, (char * const *)argv, spflags,
                            fd_list, NULL, NULL, &pid);
  if (save_argv0)
    argv[0] = save_argv0;
  free (fd_list);
  if (status == -1)
    return gpg_error_from_syserror ();

  for (i = 0; esp->fd_data_map[i].data; i++)
    {
      err = add_io_cb (esp, esp->fd_data_map[i].fd,
                       esp->fd_data_map[i].inbound,
                       esp->fd_data_map[i].inbound
                       ? _gpgme_data_inbound_handler
                       : _gpgme_data_outbound_handler,
                       esp->fd_data_map[i].data, &esp->fd_data_map[i].tag);
      if (err)
        return err;  /* FIXME: kill the child */
    }

  engspawn_io_event (esp, GPGME_EVENT_START, NULL);

  return 0;
}



/*
    Public functions
 */

static const char *
engspawn_get_file_name (void)
{
  return "/nonexistent";
}


static char *
engspawn_get_version (const char *file_name)
{
  (void)file_name;
  return strdup ("1.0");
}


static const char *
engspawn_get_req_version (void)
{
  return "1.0";
}


static gpgme_error_t
engspawn_new (void **engine, const char *file_name, const char *home_dir)
{
  engine_spawn_t esp;

  (void)file_name;
  (void)home_dir;

  esp = calloc (1, sizeof *esp);
  if (!esp)
    return gpg_error_from_syserror ();

  esp->argtail = &esp->arglist;
  *engine = esp;
  return 0;
}


static void
engspawn_release (void *engine)
{
  engine_spawn_t esp = engine;

  if (!esp)
    return;

  engspawn_cancel (engine);

  while (esp->arglist)
    {
      struct datalist_s *next = esp->arglist->next;

      free (esp->arglist);
      esp->arglist = next;
    }

  free (esp);
}


static void
engspawn_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs)
{
  engine_spawn_t esp = engine;

  esp->io_cbs = *io_cbs;
}


static void
engspawn_io_event (void *engine, gpgme_event_io_t type, void *type_data)
{
  engine_spawn_t esp = engine;

  TRACE3 (DEBUG_ENGINE, "gpgme:engspawn_io_event", esp,
          "event %p, type %d, type_data %p",
          esp->io_cbs.event, type, type_data);
  if (esp->io_cbs.event)
    (*esp->io_cbs.event) (esp->io_cbs.event_priv, type, type_data);
}


static gpgme_error_t
engspawn_cancel (void *engine)
{
  engine_spawn_t esp = engine;

  if (!esp)
    return gpg_error (GPG_ERR_INV_VALUE);

  if (esp->fd_data_map)
    {
      free_fd_data_map (esp->fd_data_map);
      esp->fd_data_map = NULL;
    }

  return 0;
}


static gpgme_error_t
engspawn_op_spawn (void *engine,
                   const char *file, const char *argv[],
                   gpgme_data_t datain,
                   gpgme_data_t dataout, gpgme_data_t dataerr,
                   unsigned int flags)
{
  engine_spawn_t esp = engine;
  gpgme_error_t err = 0;

  if (datain)
    err = add_data (esp, datain, 0, 0);
  if (!err && dataout)
    err = add_data (esp, dataout, 1, 1);
  if (!err && dataerr)
    err = add_data (esp, dataerr, 2, 1);

  if (!err)
    err = engspawn_start (esp, file, argv, flags);

  return err;
}



struct engine_ops _gpgme_engine_ops_spawn =
  {
    /* Static functions.  */
    engspawn_get_file_name,
    NULL,               /* get_home_dir */
    engspawn_get_version,
    engspawn_get_req_version,
    engspawn_new,

    /* Member functions.  */
    engspawn_release,
    NULL,		/* reset */
    NULL,		/* set_status_handler */
    NULL,		/* set_command_handler */
    NULL,		/* set_colon_line_handler */
    NULL,		/* set_locale */
    NULL,		/* set_protocol */
    NULL,		/* decrypt */
    NULL,		/* decrypt_verify */
    NULL,		/* delete */
    NULL,		/* edit */
    NULL,		/* encrypt */
    NULL,		/* encrypt_sign */
    NULL,		/* export */
    NULL,		/* export_ext */
    NULL,		/* genkey */
    NULL,		/* import */
    NULL,		/* keylist */
    NULL,		/* keylist_ext */
    NULL,		/* sign */
    NULL,		/* trustlist */
    NULL,		/* verify */
    NULL,		/* getauditlog */
    NULL,               /* opassuan_transact */
    NULL,		/* conf_load */
    NULL,		/* conf_save */
    engspawn_set_io_cbs,
    engspawn_io_event,	/* io_event */
    engspawn_cancel,	/* cancel */
    NULL,               /* cancel_op */
    NULL,               /* passwd */
    NULL,               /* set_pinentry_mode */
    engspawn_op_spawn   /* opspawn */
  };