/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-09-05 17:39:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905173934-gs0r858omhudc71m
* plugin-runner.c (main): Move variables "tmpmax" and "tmp" into
                          the innermost scope possible.

Show diffs side-by-side

added added

removed removed

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