/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: teddy at bsnet
  • Date: 2010-09-02 18:36:36 UTC
  • mto: (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: teddy@fukt.bsnet.se-20100902183636-h0vb4iw3ut2g3mta
* mandos (ClientDBus.approvals_pending): Changed to be a property
                                         which emits D-Bus signals.
  (ClientDBus.approved_pending): Removed - all callers changed.
  (ClientDBus.GotSecret, ClientDBus.Rejected,
  ClientDBus.NeedApproval): Do not emit D-Bus signal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
6
7
 * 
7
8
 * This program is free software: you can redistribute it and/or
8
9
 * modify it under the terms of the GNU General Public License as
22
23
 */
23
24
 
24
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
25
 
                                   asprintf() */
 
26
                                   asprintf(), O_CLOEXEC */
26
27
#include <stddef.h>             /* size_t, NULL */
27
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
 
                                   EXIT_SUCCESS, realloc() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
 
29
                                   realloc() */
29
30
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
32
 
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
33
34
                                   stat, waitpid(), WIFEXITED(),
34
35
                                   WEXITSTATUS(), wait(), pid_t,
35
36
                                   uid_t, gid_t, getuid(), getgid(),
37
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
40
 
                                   WEXITSTATUS() */
 
41
                                   WEXITSTATUS(), WTERMSIG(),
 
42
                                   WCOREDUMP() */
41
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
42
44
#include <iso646.h>             /* and, or, not */
43
 
#include <dirent.h>             /* DIR, struct dirent, opendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
44
46
                                   readdir(), closedir(), dirfd() */
45
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
46
48
                                   fcntl(), setuid(), setgid(),
47
49
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
50
                                   access(), pipe(), fork(), close()
49
 
                                   dup2, STDOUT_FILENO, _exit(),
 
51
                                   dup2(), STDOUT_FILENO, _exit(),
50
52
                                   execv(), write(), read(),
51
53
                                   close() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
55
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strsep, strlen(), asprintf() */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal(), strcmp(), strncmp() */
55
58
#include <errno.h>              /* errno */
56
59
#include <argp.h>               /* struct argp_option, struct
57
60
                                   argp_state, struct argp,
61
64
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
65
                                   sigaddset(), sigaction(),
63
66
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
 
                                   SIG_UNBLOCK, kill() */
 
67
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
68
                                */
65
69
#include <errno.h>              /* errno, EBADF */
 
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
 
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
 
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
66
73
 
67
74
#define BUFFER_SIZE 256
68
75
 
69
76
#define PDIR "/lib/mandos/plugins.d"
70
77
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
78
 
72
 
const char *argp_program_version = "plugin-runner 1.0";
 
79
const char *argp_program_version = "plugin-runner " VERSION;
73
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
81
 
75
 
struct plugin;
76
 
 
77
82
typedef struct plugin{
78
83
  char *name;                   /* can be NULL or any plugin name */
79
84
  char **argv;
81
86
  char **environ;
82
87
  int envc;
83
88
  bool disabled;
84
 
 
 
89
  
85
90
  /* Variables used for running processes*/
86
91
  pid_t pid;
87
92
  int fd;
89
94
  size_t buffer_size;
90
95
  size_t buffer_length;
91
96
  bool eof;
92
 
  volatile bool completed;
93
 
  volatile int status;
 
97
  volatile sig_atomic_t completed;
 
98
  int status;
94
99
  struct plugin *next;
95
100
} plugin;
96
101
 
97
102
static plugin *plugin_list = NULL;
98
103
 
99
 
/* Gets a existing plugin based on name,
 
104
/* Gets an existing plugin based on name,
100
105
   or if none is found, creates a new one */
101
106
static plugin *getplugin(char *name){
102
 
  /* Check for exiting plugin with that name */
103
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
104
 
    if ((p->name == name)
105
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
 
107
  /* Check for existing plugin with that name */
 
108
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
109
    if((p->name == name)
 
110
       or (p->name and name and (strcmp(p->name, name) == 0))){
106
111
      return p;
107
112
    }
108
113
  }
109
114
  /* Create a new plugin */
110
 
  plugin *new_plugin = malloc(sizeof(plugin));
111
 
  if (new_plugin == NULL){
 
115
  plugin *new_plugin = NULL;
 
116
  do {
 
117
    new_plugin = malloc(sizeof(plugin));
 
118
  } while(new_plugin == NULL and errno == EINTR);
 
119
  if(new_plugin == NULL){
112
120
    return NULL;
113
121
  }
114
122
  char *copy_name = NULL;
115
123
  if(name != NULL){
116
 
    copy_name = strdup(name);
 
124
    do {
 
125
      copy_name = strdup(name);
 
126
    } while(copy_name == NULL and errno == EINTR);
117
127
    if(copy_name == NULL){
 
128
      int e = errno;
 
129
      free(new_plugin);
 
130
      errno = e;
118
131
      return NULL;
119
132
    }
120
133
  }
121
 
 
122
 
  *new_plugin = (plugin) { .name = copy_name,
123
 
                           .argc = 1,
124
 
                           .disabled = false,
125
 
                           .next = plugin_list };
126
 
  
127
 
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
 
  if (new_plugin->argv == NULL){
 
134
  
 
135
  *new_plugin = (plugin){ .name = copy_name,
 
136
                          .argc = 1,
 
137
                          .disabled = false,
 
138
                          .next = plugin_list };
 
139
  
 
140
  do {
 
141
    new_plugin->argv = malloc(sizeof(char *) * 2);
 
142
  } while(new_plugin->argv == NULL and errno == EINTR);
 
143
  if(new_plugin->argv == NULL){
 
144
    int e = errno;
129
145
    free(copy_name);
130
146
    free(new_plugin);
 
147
    errno = e;
131
148
    return NULL;
132
149
  }
133
150
  new_plugin->argv[0] = copy_name;
134
151
  new_plugin->argv[1] = NULL;
135
152
  
136
 
  new_plugin->environ = malloc(sizeof(char *));
 
153
  do {
 
154
    new_plugin->environ = malloc(sizeof(char *));
 
155
  } while(new_plugin->environ == NULL and errno == EINTR);
137
156
  if(new_plugin->environ == NULL){
 
157
    int e = errno;
138
158
    free(copy_name);
139
159
    free(new_plugin->argv);
140
160
    free(new_plugin);
 
161
    errno = e;
141
162
    return NULL;
142
163
  }
143
164
  new_plugin->environ[0] = NULL;
151
172
static bool add_to_char_array(const char *new, char ***array,
152
173
                              int *len){
153
174
  /* Resize the pointed-to array to hold one more pointer */
154
 
  *array = realloc(*array, sizeof(char *)
155
 
                   * (size_t) ((*len) + 2));
 
175
  do {
 
176
    *array = realloc(*array, sizeof(char *)
 
177
                     * (size_t) ((*len) + 2));
 
178
  } while(*array == NULL and errno == EINTR);
156
179
  /* Malloc check */
157
180
  if(*array == NULL){
158
181
    return false;
159
182
  }
160
183
  /* Make a copy of the new string */
161
 
  char *copy = strdup(new);
 
184
  char *copy;
 
185
  do {
 
186
    copy = strdup(new);
 
187
  } while(copy == NULL and errno == EINTR);
162
188
  if(copy == NULL){
163
189
    return false;
164
190
  }
187
213
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
188
214
  /* Search for this environment variable */
189
215
  for(char **e = p->environ; *e != NULL; e++){
190
 
    if(strncmp(*e, def, namelen+1) == 0){
 
216
    if(strncmp(*e, def, namelen + 1) == 0){
191
217
      /* It already exists */
192
218
      if(replace){
193
 
        char *new = realloc(*e, strlen(def));
 
219
        char *new;
 
220
        do {
 
221
          new = realloc(*e, strlen(def) + 1);
 
222
        } while(new == NULL and errno == EINTR);
194
223
        if(new == NULL){
195
224
          return false;
196
225
        }
206
235
/*
207
236
 * Based on the example in the GNU LibC manual chapter 13.13 "File
208
237
 * Descriptor Flags".
209
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
238
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
210
239
 */
211
 
static int set_cloexec_flag(int fd)
212
 
{
213
 
  int ret = fcntl(fd, F_GETFD, 0);
 
240
static int set_cloexec_flag(int fd){
 
241
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
214
242
  /* If reading the flags failed, return error indication now. */
215
243
  if(ret < 0){
216
244
    return ret;
217
245
  }
218
246
  /* Store modified flag word in the descriptor. */
219
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
247
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
248
                                       ret | FD_CLOEXEC));
220
249
}
221
250
 
222
251
 
223
252
/* Mark processes as completed when they exit, and save their exit
224
253
   status. */
225
 
void handle_sigchld(__attribute__((unused)) int sig){
 
254
static void handle_sigchld(__attribute__((unused)) int sig){
 
255
  int old_errno = errno;
226
256
  while(true){
227
257
    plugin *proc = plugin_list;
228
258
    int status;
232
262
      break;
233
263
    }
234
264
    if(pid == -1){
235
 
      if (errno != ECHILD){
236
 
        perror("waitpid");
 
265
      if(errno == ECHILD){
 
266
        /* No child processes */
 
267
        break;
237
268
      }
238
 
      /* No child processes */
239
 
      break;
 
269
      perror("waitpid");
240
270
    }
241
 
 
 
271
    
242
272
    /* A child exited, find it in process_list */
243
273
    while(proc != NULL and proc->pid != pid){
244
274
      proc = proc->next;
248
278
      continue;
249
279
    }
250
280
    proc->status = status;
251
 
    proc->completed = true;
 
281
    proc->completed = 1;
252
282
  }
 
283
  errno = old_errno;
253
284
}
254
285
 
255
286
/* Prints out a password to stdout */
256
 
bool print_out_password(const char *buffer, size_t length){
 
287
static bool print_out_password(const char *buffer, size_t length){
257
288
  ssize_t ret;
258
 
  if(length>0 and buffer[length-1] == '\n'){
259
 
    length--;
260
 
  }
261
289
  for(size_t written = 0; written < length; written += (size_t)ret){
262
290
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
263
291
                                   length - written));
280
308
  }
281
309
  free(plugin_node->environ);
282
310
  free(plugin_node->buffer);
283
 
 
 
311
  
284
312
  /* Removes the plugin from the singly-linked list */
285
313
  if(plugin_node == plugin_list){
286
314
    /* First one - simple */
314
342
  struct stat st;
315
343
  fd_set rfds_all;
316
344
  int ret, maxfd = 0;
 
345
  ssize_t sret;
317
346
  uid_t uid = 65534;
318
347
  gid_t gid = 65534;
319
348
  bool debug = false;
329
358
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
330
359
  if(ret == -1){
331
360
    perror("sigaddset");
332
 
    exitstatus = EXIT_FAILURE;
 
361
    exitstatus = EX_OSERR;
333
362
    goto fallback;
334
363
  }
335
364
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
336
365
  if(ret == -1){
337
366
    perror("sigaction");
338
 
    exitstatus = EXIT_FAILURE;
 
367
    exitstatus = EX_OSERR;
339
368
    goto fallback;
340
369
  }
341
370
  
344
373
    { .name = "global-options", .key = 'g',
345
374
      .arg = "OPTION[,OPTION[,...]]",
346
375
      .doc = "Options passed to all plugins" },
347
 
    { .name = "global-env", .key = 'e',
 
376
    { .name = "global-env", .key = 'G',
348
377
      .arg = "VAR=value",
349
378
      .doc = "Environment variable passed to all plugins" },
350
379
    { .name = "options-for", .key = 'o',
351
380
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
352
381
      .doc = "Options passed only to specified plugin" },
353
 
    { .name = "env-for", .key = 'f',
 
382
    { .name = "env-for", .key = 'E',
354
383
      .arg = "PLUGIN:ENV=value",
355
384
      .doc = "Environment variable passed to specified plugin" },
356
385
    { .name = "disable", .key = 'd',
357
386
      .arg = "PLUGIN",
358
387
      .doc = "Disable a specific plugin", .group = 1 },
 
388
    { .name = "enable", .key = 'e',
 
389
      .arg = "PLUGIN",
 
390
      .doc = "Enable a specific plugin", .group = 1 },
359
391
    { .name = "plugin-dir", .key = 128,
360
392
      .arg = "DIRECTORY",
361
393
      .doc = "Specify a different plugin directory", .group = 2 },
370
402
      .doc = "Group ID the plugins will run as", .group = 3 },
371
403
    { .name = "debug", .key = 132,
372
404
      .doc = "Debug mode", .group = 4 },
 
405
    /*
 
406
     * These reproduce what we would get without ARGP_NO_HELP
 
407
     */
 
408
    { .name = "help", .key = '?',
 
409
      .doc = "Give this help list", .group = -1 },
 
410
    { .name = "usage", .key = -3,
 
411
      .doc = "Give a short usage message", .group = -1 },
 
412
    { .name = "version", .key = 'V',
 
413
      .doc = "Print program version", .group = -1 },
373
414
    { .name = NULL }
374
415
  };
375
416
  
376
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
 
                     struct argp_state *state) {
378
 
    /* Get the INPUT argument from `argp_parse', which we know is a
379
 
       pointer to our plugin list pointer. */
380
 
    switch (key) {
 
417
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
418
    errno = 0;
 
419
    switch(key){
 
420
      char *tmp;
 
421
      intmax_t tmpmax;
381
422
    case 'g':                   /* --global-options */
382
 
      if (arg != NULL){
383
 
        char *p;
384
 
        while((p = strsep(&arg, ",")) != NULL){
385
 
          if(p[0] == '\0'){
386
 
            continue;
387
 
          }
388
 
          if(not add_argument(getplugin(NULL), p)){
389
 
            perror("add_argument");
390
 
            return ARGP_ERR_UNKNOWN;
391
 
          }
392
 
        }
393
 
      }
394
 
      break;
395
 
    case 'e':                   /* --global-env */
396
 
      if(arg == NULL){
397
 
        break;
398
 
      }
399
423
      {
400
 
        char *envdef = strdup(arg);
401
 
        if(envdef == NULL){
402
 
          break;
403
 
        }
404
 
        if(not add_environment(getplugin(NULL), envdef, true)){
405
 
          perror("add_environment");
 
424
        char *plugin_option;
 
425
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
426
          if(not add_argument(getplugin(NULL), plugin_option)){
 
427
            break;
 
428
          }
406
429
        }
407
430
      }
408
431
      break;
 
432
    case 'G':                   /* --global-env */
 
433
      add_environment(getplugin(NULL), arg, true);
 
434
      break;
409
435
    case 'o':                   /* --options-for */
410
 
      if (arg != NULL){
411
 
        char *p_name = strsep(&arg, ":");
412
 
        if(p_name[0] == '\0'){
413
 
          break;
414
 
        }
415
 
        char *opt = strsep(&arg, ":");
416
 
        if(opt[0] == '\0'){
417
 
          break;
418
 
        }
419
 
        if(opt != NULL){
420
 
          char *p;
421
 
          while((p = strsep(&opt, ",")) != NULL){
422
 
            if(p[0] == '\0'){
423
 
              continue;
424
 
            }
425
 
            if(not add_argument(getplugin(p_name), p)){
426
 
              perror("add_argument");
427
 
              return ARGP_ERR_UNKNOWN;
428
 
            }
 
436
      {
 
437
        char *option_list = strchr(arg, ':');
 
438
        if(option_list == NULL){
 
439
          argp_error(state, "No colon in \"%s\"", arg);
 
440
          errno = EINVAL;
 
441
          break;
 
442
        }
 
443
        *option_list = '\0';
 
444
        option_list++;
 
445
        if(arg[0] == '\0'){
 
446
          argp_error(state, "Empty plugin name");
 
447
          errno = EINVAL;
 
448
          break;
 
449
        }
 
450
        char *option;
 
451
        while((option = strsep(&option_list, ",")) != NULL){
 
452
          if(not add_argument(getplugin(arg), option)){
 
453
            break;
429
454
          }
430
455
        }
431
456
      }
432
457
      break;
433
 
    case 'f':                   /* --env-for */
434
 
      if(arg == NULL){
435
 
        break;
436
 
      }
 
458
    case 'E':                   /* --env-for */
437
459
      {
438
460
        char *envdef = strchr(arg, ':');
439
461
        if(envdef == NULL){
440
 
          break;
441
 
        }
442
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
443
 
        if(p_name == NULL){
444
 
          break;
445
 
        }
 
462
          argp_error(state, "No colon in \"%s\"", arg);
 
463
          errno = EINVAL;
 
464
          break;
 
465
        }
 
466
        *envdef = '\0';
446
467
        envdef++;
447
 
        if(not add_environment(getplugin(p_name), envdef, true)){
448
 
          perror("add_environment");
 
468
        if(arg[0] == '\0'){
 
469
          argp_error(state, "Empty plugin name");
 
470
          errno = EINVAL;
 
471
          break;
449
472
        }
 
473
        add_environment(getplugin(arg), envdef, true);
450
474
      }
451
475
      break;
452
476
    case 'd':                   /* --disable */
453
 
      if (arg != NULL){
454
 
        plugin *p = getplugin(arg);
455
 
        if(p == NULL){
456
 
          return ARGP_ERR_UNKNOWN;
457
 
        }
458
 
        p->disabled = true;
 
477
      {
 
478
        plugin *p = getplugin(arg);
 
479
        if(p != NULL){
 
480
          p->disabled = true;
 
481
        }
 
482
      }
 
483
      break;
 
484
    case 'e':                   /* --enable */
 
485
      {
 
486
        plugin *p = getplugin(arg);
 
487
        if(p != NULL){
 
488
          p->disabled = false;
 
489
        }
459
490
      }
460
491
      break;
461
492
    case 128:                   /* --plugin-dir */
 
493
      free(plugindir);
462
494
      plugindir = strdup(arg);
463
 
      if(plugindir == NULL){
464
 
        perror("strdup");
465
 
      }      
466
 
      break;
467
 
    case 129:                   /* --config-file */
 
495
      break;
 
496
    case 129:                   /* --config-file */
 
497
      /* This is already done by parse_opt_config_file() */
 
498
      break;
 
499
    case 130:                   /* --userid */
 
500
      tmpmax = strtoimax(arg, &tmp, 10);
 
501
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
502
         or tmpmax != (uid_t)tmpmax){
 
503
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
504
                   PRIdMAX, arg, (intmax_t)uid);
 
505
        break;
 
506
      }
 
507
      uid = (uid_t)tmpmax;
 
508
      break;
 
509
    case 131:                   /* --groupid */
 
510
      tmpmax = strtoimax(arg, &tmp, 10);
 
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
512
         or tmpmax != (gid_t)tmpmax){
 
513
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
514
                   PRIdMAX, arg, (intmax_t)gid);
 
515
        break;
 
516
      }
 
517
      gid = (gid_t)tmpmax;
 
518
      break;
 
519
    case 132:                   /* --debug */
 
520
      debug = true;
 
521
      break;
 
522
      /*
 
523
       * These reproduce what we would get without ARGP_NO_HELP
 
524
       */
 
525
    case '?':                   /* --help */
 
526
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
527
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
528
    case -3:                    /* --usage */
 
529
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
530
      argp_state_help(state, state->out_stream,
 
531
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
532
    case 'V':                   /* --version */
 
533
      fprintf(state->out_stream, "%s\n", argp_program_version);
 
534
      exit(EXIT_SUCCESS);
 
535
      break;
 
536
/*
 
537
 * When adding more options before this line, remember to also add a
 
538
 * "case" to the "parse_opt_config_file" function below.
 
539
 */
 
540
    case ARGP_KEY_ARG:
 
541
      /* Cryptsetup always passes an argument, which is an empty
 
542
         string if "none" was specified in /etc/crypttab.  So if
 
543
         argument was empty, we ignore it silently. */
 
544
      if(arg[0] == '\0'){
 
545
        break;
 
546
      }
 
547
    default:
 
548
      return ARGP_ERR_UNKNOWN;
 
549
    }
 
550
    return errno;               /* Set to 0 at start */
 
551
  }
 
552
  
 
553
  /* This option parser is the same as parse_opt() above, except it
 
554
     ignores everything but the --config-file option. */
 
555
  error_t parse_opt_config_file(int key, char *arg,
 
556
                                __attribute__((unused))
 
557
                                struct argp_state *state){
 
558
    errno = 0;
 
559
    switch(key){
 
560
    case 'g':                   /* --global-options */
 
561
    case 'G':                   /* --global-env */
 
562
    case 'o':                   /* --options-for */
 
563
    case 'E':                   /* --env-for */
 
564
    case 'd':                   /* --disable */
 
565
    case 'e':                   /* --enable */
 
566
    case 128:                   /* --plugin-dir */
 
567
      break;
 
568
    case 129:                   /* --config-file */
 
569
      free(argfile);
468
570
      argfile = strdup(arg);
469
 
      if(argfile == NULL){
470
 
        perror("strdup");
471
 
      }
472
 
      break;      
 
571
      break;
473
572
    case 130:                   /* --userid */
474
 
      uid = (uid_t)strtol(arg, NULL, 10);
475
 
      break;
476
573
    case 131:                   /* --groupid */
477
 
      gid = (gid_t)strtol(arg, NULL, 10);
478
 
      break;
479
574
    case 132:                   /* --debug */
480
 
      debug = true;
481
 
      break;
 
575
    case '?':                   /* --help */
 
576
    case -3:                    /* --usage */
 
577
    case 'V':                   /* --version */
482
578
    case ARGP_KEY_ARG:
483
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
484
 
      break;
485
 
    case ARGP_KEY_END:
486
579
      break;
487
580
    default:
488
581
      return ARGP_ERR_UNKNOWN;
489
582
    }
490
 
    return 0;
 
583
    return errno;
491
584
  }
492
585
  
493
 
  struct argp argp = { .options = options, .parser = parse_opt,
 
586
  struct argp argp = { .options = options,
 
587
                       .parser = parse_opt_config_file,
494
588
                       .args_doc = "",
495
589
                       .doc = "Mandos plugin runner -- Run plugins" };
496
590
  
 
591
  /* Parse using parse_opt_config_file() in order to get the custom
 
592
     config file location, if any. */
 
593
  ret = argp_parse(&argp, argc, argv,
 
594
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
595
                   NULL, NULL);
 
596
  switch(ret){
 
597
  case 0:
 
598
    break;
 
599
  case ENOMEM:
 
600
  default:
 
601
    errno = ret;
 
602
    perror("argp_parse");
 
603
    exitstatus = EX_OSERR;
 
604
    goto fallback;
 
605
  case EINVAL:
 
606
    exitstatus = EX_USAGE;
 
607
    goto fallback;
 
608
  }
 
609
  
 
610
  /* Reset to the normal argument parser */
 
611
  argp.parser = parse_opt;
 
612
  
497
613
  /* Open the configfile if available */
498
 
  if (argfile == NULL){
 
614
  if(argfile == NULL){
499
615
    conffp = fopen(AFILE, "r");
500
616
  } else {
501
617
    conffp = fopen(argfile, "r");
502
 
  }  
 
618
  }
503
619
  if(conffp != NULL){
504
620
    char *org_line = NULL;
505
621
    char *p, *arg, *new_arg, *line;
506
622
    size_t size = 0;
507
 
    ssize_t sret;
508
623
    const char whitespace_delims[] = " \r\t\f\v\n";
509
624
    const char comment_delim[] = "#";
510
 
 
 
625
    
511
626
    custom_argc = 1;
512
627
    custom_argv = malloc(sizeof(char*) * 2);
513
628
    if(custom_argv == NULL){
514
629
      perror("malloc");
515
 
      exitstatus = EXIT_FAILURE;
 
630
      exitstatus = EX_OSERR;
516
631
      goto fallback;
517
632
    }
518
633
    custom_argv[0] = argv[0];
519
634
    custom_argv[1] = NULL;
520
 
 
 
635
    
521
636
    /* for each line in the config file, strip whitespace and ignore
522
637
       commented text */
523
638
    while(true){
525
640
      if(sret == -1){
526
641
        break;
527
642
      }
528
 
 
 
643
      
529
644
      line = org_line;
530
645
      arg = strsep(&line, comment_delim);
531
646
      while((p = strsep(&arg, whitespace_delims)) != NULL){
535
650
        new_arg = strdup(p);
536
651
        if(new_arg == NULL){
537
652
          perror("strdup");
538
 
          exitstatus = EXIT_FAILURE;
 
653
          exitstatus = EX_OSERR;
539
654
          free(org_line);
540
655
          goto fallback;
541
656
        }
545
660
                              * ((unsigned int) custom_argc + 1));
546
661
        if(custom_argv == NULL){
547
662
          perror("realloc");
548
 
          exitstatus = EXIT_FAILURE;
 
663
          exitstatus = EX_OSERR;
549
664
          free(org_line);
550
665
          goto fallback;
551
666
        }
552
667
        custom_argv[custom_argc-1] = new_arg;
553
 
        custom_argv[custom_argc] = NULL;        
 
668
        custom_argv[custom_argc] = NULL;
554
669
      }
555
670
    }
 
671
    do {
 
672
      ret = fclose(conffp);
 
673
    } while(ret == EOF and errno == EINTR);
 
674
    if(ret == EOF){
 
675
      perror("fclose");
 
676
      exitstatus = EX_IOERR;
 
677
      goto fallback;
 
678
    }
556
679
    free(org_line);
557
680
  } else {
558
681
    /* Check for harmful errors and go to fallback. Other errors might
559
682
       not affect opening plugins */
560
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
683
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
561
684
      perror("fopen");
562
 
      exitstatus = EXIT_FAILURE;
 
685
      exitstatus = EX_OSERR;
563
686
      goto fallback;
564
687
    }
565
688
  }
566
 
  /* If there was any arguments from configuration file,
567
 
     pass them to parser as command arguments */
 
689
  /* If there were any arguments from the configuration file, pass
 
690
     them to parser as command line arguments */
568
691
  if(custom_argv != NULL){
569
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
570
 
                      0, NULL);
571
 
    if (ret == ARGP_ERR_UNKNOWN){
572
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
573
 
      exitstatus = EXIT_FAILURE;
 
692
    ret = argp_parse(&argp, custom_argc, custom_argv,
 
693
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
694
                     NULL, NULL);
 
695
    switch(ret){
 
696
    case 0:
 
697
      break;
 
698
    case ENOMEM:
 
699
    default:
 
700
      errno = ret;
 
701
      perror("argp_parse");
 
702
      exitstatus = EX_OSERR;
 
703
      goto fallback;
 
704
    case EINVAL:
 
705
      exitstatus = EX_CONFIG;
574
706
      goto fallback;
575
707
    }
576
708
  }
577
709
  
578
710
  /* Parse actual command line arguments, to let them override the
579
711
     config file */
580
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
581
 
  if (ret == ARGP_ERR_UNKNOWN){
582
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
583
 
    exitstatus = EXIT_FAILURE;
 
712
  ret = argp_parse(&argp, argc, argv,
 
713
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
714
                   NULL, NULL);
 
715
  switch(ret){
 
716
  case 0:
 
717
    break;
 
718
  case ENOMEM:
 
719
  default:
 
720
    errno = ret;
 
721
    perror("argp_parse");
 
722
    exitstatus = EX_OSERR;
 
723
    goto fallback;
 
724
  case EINVAL:
 
725
    exitstatus = EX_USAGE;
584
726
    goto fallback;
585
727
  }
586
728
  
591
733
      for(char **a = p->argv; *a != NULL; a++){
592
734
        fprintf(stderr, "\tArg: %s\n", *a);
593
735
      }
594
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
736
      fprintf(stderr, "...and %d environment variables\n", p->envc);
595
737
      for(char **a = p->environ; *a != NULL; a++){
596
738
        fprintf(stderr, "\t%s\n", *a);
597
739
      }
599
741
  }
600
742
  
601
743
  /* Strip permissions down to nobody */
 
744
  setgid(gid);
 
745
  if(ret == -1){
 
746
    perror("setgid");
 
747
  }
602
748
  ret = setuid(uid);
603
 
  if (ret == -1){
 
749
  if(ret == -1){
604
750
    perror("setuid");
605
 
  }  
606
 
  setgid(gid);
607
 
  if (ret == -1){
608
 
    perror("setgid");
609
 
  }
610
 
  
611
 
  if (plugindir == NULL){
612
 
    dir = opendir(PDIR);
613
 
  } else {
614
 
    dir = opendir(plugindir);
615
 
  }
616
 
  
617
 
  if(dir == NULL){
618
 
    perror("Could not open plugin dir");
619
 
    exitstatus = EXIT_FAILURE;
620
 
    goto fallback;
621
 
  }
622
 
  
623
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
751
  }
 
752
  
 
753
  /* Open plugin directory with close_on_exec flag */
624
754
  {
625
 
    int dir_fd = dirfd(dir);
626
 
    if(dir_fd >= 0){
627
 
      ret = set_cloexec_flag(dir_fd);
628
 
      if(ret < 0){
629
 
        perror("set_cloexec_flag");
630
 
        exitstatus = EXIT_FAILURE;
631
 
        goto fallback;
632
 
      }
 
755
    int dir_fd = -1;
 
756
    if(plugindir == NULL){
 
757
      dir_fd = open(PDIR, O_RDONLY |
 
758
#ifdef O_CLOEXEC
 
759
                    O_CLOEXEC
 
760
#else  /* not O_CLOEXEC */
 
761
                    0
 
762
#endif  /* not O_CLOEXEC */
 
763
                    );
 
764
    } else {
 
765
      dir_fd = open(plugindir, O_RDONLY |
 
766
#ifdef O_CLOEXEC
 
767
                    O_CLOEXEC
 
768
#else  /* not O_CLOEXEC */
 
769
                    0
 
770
#endif  /* not O_CLOEXEC */
 
771
                    );
 
772
    }
 
773
    if(dir_fd == -1){
 
774
      perror("Could not open plugin dir");
 
775
      exitstatus = EX_UNAVAILABLE;
 
776
      goto fallback;
 
777
    }
 
778
    
 
779
#ifndef O_CLOEXEC
 
780
  /* Set the FD_CLOEXEC flag on the directory */
 
781
    ret = set_cloexec_flag(dir_fd);
 
782
    if(ret < 0){
 
783
      perror("set_cloexec_flag");
 
784
      TEMP_FAILURE_RETRY(close(dir_fd));
 
785
      exitstatus = EX_OSERR;
 
786
      goto fallback;
 
787
    }
 
788
#endif  /* O_CLOEXEC */
 
789
    
 
790
    dir = fdopendir(dir_fd);
 
791
    if(dir == NULL){
 
792
      perror("Could not open plugin dir");
 
793
      TEMP_FAILURE_RETRY(close(dir_fd));
 
794
      exitstatus = EX_OSERR;
 
795
      goto fallback;
633
796
    }
634
797
  }
635
798
  
637
800
  
638
801
  /* Read and execute any executable in the plugin directory*/
639
802
  while(true){
640
 
    dirst = readdir(dir);
 
803
    do {
 
804
      dirst = readdir(dir);
 
805
    } while(dirst == NULL and errno == EINTR);
641
806
    
642
807
    /* All directory entries have been processed */
643
808
    if(dirst == NULL){
644
 
      if (errno == EBADF){
 
809
      if(errno == EBADF){
645
810
        perror("readdir");
646
 
        exitstatus = EXIT_FAILURE;
 
811
        exitstatus = EX_IOERR;
647
812
        goto fallback;
648
813
      }
649
814
      break;
659
824
      
660
825
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
661
826
                                           ".dpkg-old",
 
827
                                           ".dpkg-bak",
662
828
                                           ".dpkg-divert", NULL };
663
829
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
664
830
        size_t pre_len = strlen(*pre);
678
844
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
679
845
        size_t suf_len = strlen(*suf);
680
846
        if((d_name_len >= suf_len)
681
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
847
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
682
848
                == 0)){
683
849
          if(debug){
684
850
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
693
859
        continue;
694
860
      }
695
861
    }
696
 
 
 
862
    
697
863
    char *filename;
698
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
864
    if(plugindir == NULL){
 
865
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
866
                                             dirst->d_name));
 
867
    } else {
 
868
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
869
                                             plugindir,
 
870
                                             dirst->d_name));
 
871
    }
699
872
    if(ret < 0){
700
873
      perror("asprintf");
701
874
      continue;
702
875
    }
703
876
    
704
 
    ret = stat(filename, &st);
705
 
    if (ret == -1){
 
877
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
878
    if(ret == -1){
706
879
      perror("stat");
707
880
      free(filename);
708
881
      continue;
709
882
    }
710
 
 
 
883
    
711
884
    /* Ignore non-executable files */
712
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
885
    if(not S_ISREG(st.st_mode)
 
886
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
713
887
      if(debug){
714
888
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
715
889
                " with bad type or mode\n", filename);
761
935
    }
762
936
    
763
937
    int pipefd[2];
764
 
    ret = pipe(pipefd);
765
 
    if (ret == -1){
 
938
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
939
    if(ret == -1){
766
940
      perror("pipe");
767
 
      exitstatus = EXIT_FAILURE;
 
941
      exitstatus = EX_OSERR;
768
942
      goto fallback;
769
943
    }
770
944
    /* Ask OS to automatic close the pipe on exec */
771
945
    ret = set_cloexec_flag(pipefd[0]);
772
946
    if(ret < 0){
773
947
      perror("set_cloexec_flag");
774
 
      exitstatus = EXIT_FAILURE;
 
948
      exitstatus = EX_OSERR;
775
949
      goto fallback;
776
950
    }
777
951
    ret = set_cloexec_flag(pipefd[1]);
778
952
    if(ret < 0){
779
953
      perror("set_cloexec_flag");
780
 
      exitstatus = EXIT_FAILURE;
 
954
      exitstatus = EX_OSERR;
781
955
      goto fallback;
782
956
    }
783
957
    /* Block SIGCHLD until process is safely in process list */
784
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
958
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
959
                                              &sigchld_action.sa_mask,
 
960
                                              NULL));
785
961
    if(ret < 0){
786
962
      perror("sigprocmask");
787
 
      exitstatus = EXIT_FAILURE;
 
963
      exitstatus = EX_OSERR;
788
964
      goto fallback;
789
965
    }
790
966
    /* Starting a new process to be watched */
791
 
    pid_t pid = fork();
 
967
    pid_t pid;
 
968
    do {
 
969
      pid = fork();
 
970
    } while(pid == -1 and errno == EINTR);
792
971
    if(pid == -1){
793
972
      perror("fork");
794
 
      exitstatus = EXIT_FAILURE;
 
973
      exitstatus = EX_OSERR;
795
974
      goto fallback;
796
975
    }
797
976
    if(pid == 0){
799
978
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
800
979
      if(ret < 0){
801
980
        perror("sigaction");
802
 
        _exit(EXIT_FAILURE);
 
981
        _exit(EX_OSERR);
803
982
      }
804
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
983
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
805
984
      if(ret < 0){
806
985
        perror("sigprocmask");
807
 
        _exit(EXIT_FAILURE);
 
986
        _exit(EX_OSERR);
808
987
      }
809
 
 
 
988
      
810
989
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
811
990
      if(ret == -1){
812
991
        perror("dup2");
813
 
        _exit(EXIT_FAILURE);
 
992
        _exit(EX_OSERR);
814
993
      }
815
994
      
816
995
      if(dirfd(dir) < 0){
821
1000
      if(p->environ[0] == NULL){
822
1001
        if(execv(filename, p->argv) < 0){
823
1002
          perror("execv");
824
 
          _exit(EXIT_FAILURE);
 
1003
          _exit(EX_OSERR);
825
1004
        }
826
1005
      } else {
827
1006
        if(execve(filename, p->argv, p->environ) < 0){
828
1007
          perror("execve");
829
 
          _exit(EXIT_FAILURE);
 
1008
          _exit(EX_OSERR);
830
1009
        }
831
1010
      }
832
1011
      /* no return */
833
1012
    }
834
1013
    /* Parent process */
835
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1014
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1015
                                             pipe */
836
1016
    free(filename);
837
1017
    plugin *new_plugin = getplugin(dirst->d_name);
838
 
    if (new_plugin == NULL){
 
1018
    if(new_plugin == NULL){
839
1019
      perror("getplugin");
840
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
1020
      ret = (int)(TEMP_FAILURE_RETRY
 
1021
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1022
                               NULL)));
841
1023
      if(ret < 0){
842
1024
        perror("sigprocmask");
843
1025
      }
844
 
      exitstatus = EXIT_FAILURE;
 
1026
      exitstatus = EX_OSERR;
845
1027
      goto fallback;
846
1028
    }
847
1029
    
850
1032
    
851
1033
    /* Unblock SIGCHLD so signal handler can be run if this process
852
1034
       has already completed */
853
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
1035
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1036
                                              &sigchld_action.sa_mask,
 
1037
                                              NULL));
854
1038
    if(ret < 0){
855
1039
      perror("sigprocmask");
856
 
      exitstatus = EXIT_FAILURE;
 
1040
      exitstatus = EX_OSERR;
857
1041
      goto fallback;
858
1042
    }
859
1043
    
860
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1044
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1045
                                          -Wconversion */
861
1046
    
862
 
    if (maxfd < new_plugin->fd){
 
1047
    if(maxfd < new_plugin->fd){
863
1048
      maxfd = new_plugin->fd;
864
1049
    }
865
 
    
866
1050
  }
867
1051
  
868
 
  closedir(dir);
 
1052
  TEMP_FAILURE_RETRY(closedir(dir));
869
1053
  dir = NULL;
870
 
 
 
1054
  free_plugin(getplugin(NULL));
 
1055
  
871
1056
  for(plugin *p = plugin_list; p != NULL; p = p->next){
872
1057
    if(p->pid != 0){
873
1058
      break;
878
1063
      free_plugin_list();
879
1064
    }
880
1065
  }
881
 
 
 
1066
  
882
1067
  /* Main loop while running plugins exist */
883
1068
  while(plugin_list){
884
1069
    fd_set rfds = rfds_all;
885
1070
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
886
 
    if (select_ret == -1){
 
1071
    if(select_ret == -1 and errno != EINTR){
887
1072
      perror("select");
888
 
      exitstatus = EXIT_FAILURE;
 
1073
      exitstatus = EX_OSERR;
889
1074
      goto fallback;
890
1075
    }
891
1076
    /* OK, now either a process completed, or something can be read
892
1077
       from one of them */
893
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
1078
    for(plugin *proc = plugin_list; proc != NULL;){
894
1079
      /* Is this process completely done? */
895
 
      if(proc->eof and proc->completed){
 
1080
      if(proc->completed and proc->eof){
896
1081
        /* Only accept the plugin output if it exited cleanly */
897
1082
        if(not WIFEXITED(proc->status)
898
1083
           or WEXITSTATUS(proc->status) != 0){
899
1084
          /* Bad exit by plugin */
900
 
 
 
1085
          
901
1086
          if(debug){
902
1087
            if(WIFEXITED(proc->status)){
903
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
904
 
                      (unsigned int) (proc->pid),
 
1088
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
1089
                      " status %d\n", proc->name,
 
1090
                      (intmax_t) (proc->pid),
905
1091
                      WEXITSTATUS(proc->status));
906
 
            } else if(WIFSIGNALED(proc->status)) {
907
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
908
 
                      (unsigned int) (proc->pid),
909
 
                      WTERMSIG(proc->status));
 
1092
            } else if(WIFSIGNALED(proc->status)){
 
1093
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
1094
                      " signal %d: %s\n", proc->name,
 
1095
                      (intmax_t) (proc->pid),
 
1096
                      WTERMSIG(proc->status),
 
1097
                      strsignal(WTERMSIG(proc->status)));
910
1098
            } else if(WCOREDUMP(proc->status)){
911
 
              fprintf(stderr, "Plugin %d dumped core\n",
912
 
                      (unsigned int) (proc->pid));
 
1099
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1100
                      " core\n", proc->name, (intmax_t) (proc->pid));
913
1101
            }
914
1102
          }
915
1103
          
916
1104
          /* Remove the plugin */
917
 
          FD_CLR(proc->fd, &rfds_all);
918
 
 
 
1105
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1106
                                          -Wconversion */
 
1107
          
919
1108
          /* Block signal while modifying process_list */
920
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1109
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1110
                                        (SIG_BLOCK,
 
1111
                                         &sigchld_action.sa_mask,
 
1112
                                         NULL));
921
1113
          if(ret < 0){
922
1114
            perror("sigprocmask");
923
 
            exitstatus = EXIT_FAILURE;
 
1115
            exitstatus = EX_OSERR;
924
1116
            goto fallback;
925
1117
          }
 
1118
          
 
1119
          plugin *next_plugin = proc->next;
926
1120
          free_plugin(proc);
 
1121
          proc = next_plugin;
 
1122
          
927
1123
          /* We are done modifying process list, so unblock signal */
928
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
929
 
                             NULL);
 
1124
          ret = (int)(TEMP_FAILURE_RETRY
 
1125
                      (sigprocmask(SIG_UNBLOCK,
 
1126
                                   &sigchld_action.sa_mask, NULL)));
930
1127
          if(ret < 0){
931
1128
            perror("sigprocmask");
932
 
            exitstatus = EXIT_FAILURE;
 
1129
            exitstatus = EX_OSERR;
933
1130
            goto fallback;
934
1131
          }
935
1132
          
936
1133
          if(plugin_list == NULL){
937
1134
            break;
938
1135
          }
 
1136
          
939
1137
          continue;
940
1138
        }
941
1139
        
942
1140
        /* This process exited nicely, so print its buffer */
943
 
 
 
1141
        
944
1142
        bool bret = print_out_password(proc->buffer,
945
1143
                                       proc->buffer_length);
946
1144
        if(not bret){
947
1145
          perror("print_out_password");
948
 
          exitstatus = EXIT_FAILURE;
 
1146
          exitstatus = EX_IOERR;
949
1147
        }
950
1148
        goto fallback;
951
1149
      }
952
1150
      
953
1151
      /* This process has not completed.  Does it have any output? */
954
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1152
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1153
                                                         warning from
 
1154
                                                         -Wconversion */
955
1155
        /* This process had nothing to say at this time */
 
1156
        proc = proc->next;
956
1157
        continue;
957
1158
      }
958
1159
      /* Before reading, make the process' data buffer large enough */
959
1160
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
960
1161
        proc->buffer = realloc(proc->buffer, proc->buffer_size
961
1162
                               + (size_t) BUFFER_SIZE);
962
 
        if (proc->buffer == NULL){
 
1163
        if(proc->buffer == NULL){
963
1164
          perror("malloc");
964
 
          exitstatus = EXIT_FAILURE;
 
1165
          exitstatus = EX_OSERR;
965
1166
          goto fallback;
966
1167
        }
967
1168
        proc->buffer_size += BUFFER_SIZE;
968
1169
      }
969
1170
      /* Read from the process */
970
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
971
 
                 BUFFER_SIZE);
972
 
      if(ret < 0){
 
1171
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1172
                                     proc->buffer
 
1173
                                     + proc->buffer_length,
 
1174
                                     BUFFER_SIZE));
 
1175
      if(sret < 0){
973
1176
        /* Read error from this process; ignore the error */
 
1177
        proc = proc->next;
974
1178
        continue;
975
1179
      }
976
 
      if(ret == 0){
 
1180
      if(sret == 0){
977
1181
        /* got EOF */
978
1182
        proc->eof = true;
979
1183
      } else {
980
 
        proc->buffer_length += (size_t) ret;
 
1184
        proc->buffer_length += (size_t) sret;
981
1185
      }
982
1186
    }
983
1187
  }
984
 
 
985
 
 
 
1188
  
 
1189
  
986
1190
 fallback:
987
1191
  
988
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1192
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1193
                             and exitstatus != EX_OK)){
989
1194
    /* Fallback if all plugins failed, none are found or an error
990
1195
       occured */
991
1196
    bool bret;
992
1197
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
993
1198
    char *passwordbuffer = getpass("Password: ");
994
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1199
    size_t len = strlen(passwordbuffer);
 
1200
    /* Strip trailing newline */
 
1201
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1202
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1203
      len--;
 
1204
    }
 
1205
    bret = print_out_password(passwordbuffer, len);
995
1206
    if(not bret){
996
1207
      perror("print_out_password");
997
 
      exitstatus = EXIT_FAILURE;
 
1208
      exitstatus = EX_IOERR;
998
1209
    }
999
1210
  }
1000
1211
  
1002
1213
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1003
1214
  if(ret == -1){
1004
1215
    perror("sigaction");
1005
 
    exitstatus = EXIT_FAILURE;
 
1216
    exitstatus = EX_OSERR;
1006
1217
  }
1007
 
 
 
1218
  
1008
1219
  if(custom_argv != NULL){
1009
1220
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1010
1221
      free(*arg);
1016
1227
    closedir(dir);
1017
1228
  }
1018
1229
  
1019
 
  /* Free the process list and kill the processes */
 
1230
  /* Kill the processes */
1020
1231
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1021
1232
    if(p->pid != 0){
1022
1233
      close(p->fd);
1029
1240
  }
1030
1241
  
1031
1242
  /* Wait for any remaining child processes to terminate */
1032
 
  do{
 
1243
  do {
1033
1244
    ret = wait(NULL);
1034
1245
  } while(ret >= 0);
1035
1246
  if(errno != ECHILD){
1036
1247
    perror("wait");
1037
1248
  }
1038
 
 
 
1249
  
1039
1250
  free_plugin_list();
1040
1251
  
1041
1252
  free(plugindir);