/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-07 07:48:59 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090907074859-nv2nni1wwib5hi10
* plugin-runner.c: Minor stylistic changes.

* plugins.d/askpass-fifo.c: Changed contact information and did minor
                            stylistic changes.

* plugins.d/mandos-client.c: Minor stylistic changes.

* plugins.d/password-prompt.c: Changed contact information.

* plugins.d/splashy.c: Changed contact information.
  (main): Changed error handling design.  Bug fix: Will now be much
          more tolerant against signals.  Bug fix: Will now re-raise
          signal received.

* plugins.d/usplash.c: Changed contact information and did minor
                       stylistic changes.

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