/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 Hogeborn
  • Date: 2009-04-17 08:26:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090417082617-eltetak5lzjyz55o
* initramfs-tools-hook: Bug fix: Add "--userid" and "--groupid" to
                        start of "plugin-runner.conf" file instead of
                        appending, to allow any preexisting options to
                        override.
* plugin-runner.conf: Improved wording.

Show diffs side-by-side

added added

removed removed

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