/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-10-30 16:23:43 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091030162343-1p2a8bf3gc084kc9
* plugins.d/password-prompt.c: Use environment variables and prompt
                               text from cryptsetup 1.1.
* plugins.d/password-prompt.xml (ENVIRONMENT): Document change in
                                               environment variables
                                               used.

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-2018 Teddy Hogeborn
6
 
 * Copyright © 2008-2018 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
27
 
                                   O_CLOEXEC, pipe2() */
 
26
                                   asprintf(), O_CLOEXEC */
28
27
#include <stddef.h>             /* size_t, NULL */
29
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
30
 
                                   realloc() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
 
29
                                   EXIT_SUCCESS, realloc() */
31
30
#include <stdbool.h>            /* bool, true, false */
32
 
#include <stdio.h>              /* fileno(), fprintf(),
33
 
                                   stderr, STDOUT_FILENO, fclose() */
34
 
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
35
 
                                   WIFEXITED(), WEXITSTATUS(), wait(),
36
 
                                   pid_t, uid_t, gid_t, getuid(),
37
 
                                   getgid() */
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
34
                                   stat, waitpid(), WIFEXITED(),
 
35
                                   WEXITSTATUS(), wait(), pid_t,
 
36
                                   uid_t, gid_t, getuid(), getgid(),
 
37
                                   dirfd() */
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG() */
42
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
41
                                   WEXITSTATUS(), WTERMSIG(),
 
42
                                   WCOREDUMP() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
 
#include <dirent.h>             /* struct dirent, scandirat() */
45
 
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
46
 
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
47
 
                                   struct stat, fstat(), close(),
48
 
                                   setgid(), setuid(), S_ISREG(),
49
 
                                   faccessat() pipe2(), fork(),
50
 
                                   _exit(), dup2(), fexecve(), read()
51
 
                                */
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
46
                                   readdir(), closedir(), dirfd() */
 
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() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
55
 
#include <string.h>             /* strsep, strlen(), strsignal(),
56
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal() */
57
58
#include <errno.h>              /* errno */
58
59
#include <argp.h>               /* struct argp_option, struct
59
60
                                   argp_state, struct argp,
67
68
                                */
68
69
#include <errno.h>              /* errno, EBADF */
69
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
70
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
71
 
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
72
 
#include <errno.h>              /* errno */
73
 
#include <error.h>              /* error() */
74
 
#include <fnmatch.h>            /* fnmatch() */
 
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
75
72
 
76
73
#define BUFFER_SIZE 256
77
74
 
78
75
#define PDIR "/lib/mandos/plugins.d"
79
 
#define PHDIR "/lib/mandos/plugin-helpers"
80
76
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
81
77
 
82
78
const char *argp_program_version = "plugin-runner " VERSION;
83
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
79
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
84
80
 
85
81
typedef struct plugin{
86
82
  char *name;                   /* can be NULL or any plugin name */
106
102
 
107
103
/* Gets an existing plugin based on name,
108
104
   or if none is found, creates a new one */
109
 
__attribute__((warn_unused_result))
110
105
static plugin *getplugin(char *name){
111
106
  /* Check for existing plugin with that name */
112
107
  for(plugin *p = plugin_list; p != NULL; p = p->next){
173
168
}
174
169
 
175
170
/* Helper function for add_argument and add_environment */
176
 
__attribute__((nonnull, warn_unused_result))
177
171
static bool add_to_char_array(const char *new, char ***array,
178
172
                              int *len){
179
173
  /* Resize the pointed-to array to hold one more pointer */
180
 
  char **new_array = NULL;
181
174
  do {
182
 
    new_array = realloc(*array, sizeof(char *)
183
 
                        * (size_t) ((*len) + 2));
184
 
  } while(new_array == NULL and errno == EINTR);
 
175
    *array = realloc(*array, sizeof(char *)
 
176
                     * (size_t) ((*len) + 2));
 
177
  } while(*array == NULL and errno == EINTR);
185
178
  /* Malloc check */
186
 
  if(new_array == NULL){
 
179
  if(*array == NULL){
187
180
    return false;
188
181
  }
189
 
  *array = new_array;
190
182
  /* Make a copy of the new string */
191
183
  char *copy;
192
184
  do {
204
196
}
205
197
 
206
198
/* Add to a plugin's argument vector */
207
 
__attribute__((nonnull(2), warn_unused_result))
208
199
static bool add_argument(plugin *p, const char *arg){
209
200
  if(p == NULL){
210
201
    return false;
213
204
}
214
205
 
215
206
/* Add to a plugin's environment */
216
 
__attribute__((nonnull(2), warn_unused_result))
217
207
static bool add_environment(plugin *p, const char *def, bool replace){
218
208
  if(p == NULL){
219
209
    return false;
221
211
  /* namelen = length of name of environment variable */
222
212
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
223
213
  /* Search for this environment variable */
224
 
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
225
 
    if(strncmp(*envdef, def, namelen + 1) == 0){
 
214
  for(char **e = p->environ; *e != NULL; e++){
 
215
    if(strncmp(*e, def, namelen + 1) == 0){
226
216
      /* It already exists */
227
217
      if(replace){
228
 
        char *new_envdef;
 
218
        char *new;
229
219
        do {
230
 
          new_envdef = realloc(*envdef, strlen(def) + 1);
231
 
        } while(new_envdef == NULL and errno == EINTR);
232
 
        if(new_envdef == NULL){
 
220
          new = realloc(*e, strlen(def) + 1);
 
221
        } while(new == NULL and errno == EINTR);
 
222
        if(new == NULL){
233
223
          return false;
234
224
        }
235
 
        *envdef = new_envdef;
236
 
        strcpy(*envdef, def);
 
225
        *e = new;
 
226
        strcpy(*e, def);
237
227
      }
238
228
      return true;
239
229
    }
241
231
  return add_to_char_array(def, &(p->environ), &(p->envc));
242
232
}
243
233
 
244
 
#ifndef O_CLOEXEC
245
234
/*
246
235
 * Based on the example in the GNU LibC manual chapter 13.13 "File
247
236
 * Descriptor Flags".
248
237
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
249
238
 */
250
 
__attribute__((warn_unused_result))
251
239
static int set_cloexec_flag(int fd){
252
240
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
253
241
  /* If reading the flags failed, return error indication now. */
258
246
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
259
247
                                       ret | FD_CLOEXEC));
260
248
}
261
 
#endif  /* not O_CLOEXEC */
262
249
 
263
250
 
264
251
/* Mark processes as completed when they exit, and save their exit
278
265
        /* No child processes */
279
266
        break;
280
267
      }
281
 
      error(0, errno, "waitpid");
 
268
      perror("waitpid");
282
269
    }
283
270
    
284
271
    /* A child exited, find it in process_list */
296
283
}
297
284
 
298
285
/* Prints out a password to stdout */
299
 
__attribute__((nonnull, warn_unused_result))
300
286
static bool print_out_password(const char *buffer, size_t length){
301
287
  ssize_t ret;
302
288
  for(size_t written = 0; written < length; written += (size_t)ret){
310
296
}
311
297
 
312
298
/* Removes and free a plugin from the plugin list */
313
 
__attribute__((nonnull))
314
299
static void free_plugin(plugin *plugin_node){
315
300
  
316
 
  for(char **arg = (plugin_node->argv)+1; *arg != NULL; arg++){
 
301
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
317
302
    free(*arg);
318
303
  }
319
 
  free(plugin_node->name);
320
304
  free(plugin_node->argv);
321
305
  for(char **env = plugin_node->environ; *env != NULL; env++){
322
306
    free(*env);
349
333
 
350
334
int main(int argc, char *argv[]){
351
335
  char *plugindir = NULL;
352
 
  char *pluginhelperdir = NULL;
353
336
  char *argfile = NULL;
354
337
  FILE *conffp;
355
 
  struct dirent **direntries = NULL;
 
338
  size_t d_name_len;
 
339
  DIR *dir = NULL;
 
340
  struct dirent *dirst;
356
341
  struct stat st;
357
342
  fd_set rfds_all;
358
343
  int ret, maxfd = 0;
366
351
                                      .sa_flags = SA_NOCLDSTOP };
367
352
  char **custom_argv = NULL;
368
353
  int custom_argc = 0;
369
 
  int dir_fd = -1;
370
354
  
371
355
  /* Establish a signal handler */
372
356
  sigemptyset(&sigchld_action.sa_mask);
373
357
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
374
358
  if(ret == -1){
375
 
    error(0, errno, "sigaddset");
376
 
    exitstatus = EX_OSERR;
 
359
    perror("sigaddset");
 
360
    exitstatus = EXIT_FAILURE;
377
361
    goto fallback;
378
362
  }
379
363
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
380
364
  if(ret == -1){
381
 
    error(0, errno, "sigaction");
382
 
    exitstatus = EX_OSERR;
 
365
    perror("sigaction");
 
366
    exitstatus = EXIT_FAILURE;
383
367
    goto fallback;
384
368
  }
385
369
  
417
401
      .doc = "Group ID the plugins will run as", .group = 3 },
418
402
    { .name = "debug", .key = 132,
419
403
      .doc = "Debug mode", .group = 4 },
420
 
    { .name = "plugin-helper-dir", .key = 133,
421
 
      .arg = "DIRECTORY",
422
 
      .doc = "Specify a different plugin helper directory",
423
 
      .group = 2 },
424
404
    /*
425
405
     * These reproduce what we would get without ARGP_NO_HELP
426
406
     */
433
413
    { .name = NULL }
434
414
  };
435
415
  
436
 
  __attribute__((nonnull(3)))
437
416
  error_t parse_opt(int key, char *arg, struct argp_state *state){
438
417
    errno = 0;
439
418
    switch(key){
440
419
      char *tmp;
441
 
      intmax_t tmp_id;
 
420
      intmax_t tmpmax;
442
421
    case 'g':                   /* --global-options */
443
422
      {
444
423
        char *plugin_option;
447
426
            break;
448
427
          }
449
428
        }
450
 
        errno = 0;
451
429
      }
452
430
      break;
453
431
    case 'G':                   /* --global-env */
454
 
      if(add_environment(getplugin(NULL), arg, true)){
455
 
        errno = 0;
456
 
      }
 
432
      add_environment(getplugin(NULL), arg, true);
457
433
      break;
458
434
    case 'o':                   /* --options-for */
459
435
      {
476
452
            break;
477
453
          }
478
454
        }
479
 
        errno = 0;
480
455
      }
481
456
      break;
482
457
    case 'E':                   /* --env-for */
494
469
          errno = EINVAL;
495
470
          break;
496
471
        }
497
 
        if(add_environment(getplugin(arg), envdef, true)){
498
 
          errno = 0;
499
 
        }
 
472
        add_environment(getplugin(arg), envdef, true);
500
473
      }
501
474
      break;
502
475
    case 'd':                   /* --disable */
504
477
        plugin *p = getplugin(arg);
505
478
        if(p != NULL){
506
479
          p->disabled = true;
507
 
          errno = 0;
508
480
        }
509
481
      }
510
482
      break;
513
485
        plugin *p = getplugin(arg);
514
486
        if(p != NULL){
515
487
          p->disabled = false;
516
 
          errno = 0;
517
488
        }
518
489
      }
519
490
      break;
520
491
    case 128:                   /* --plugin-dir */
521
492
      free(plugindir);
522
493
      plugindir = strdup(arg);
523
 
      if(plugindir != NULL){
524
 
        errno = 0;
525
 
      }
526
494
      break;
527
495
    case 129:                   /* --config-file */
528
496
      /* This is already done by parse_opt_config_file() */
529
497
      break;
530
498
    case 130:                   /* --userid */
531
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
499
      tmpmax = strtoimax(arg, &tmp, 10);
532
500
      if(errno != 0 or tmp == arg or *tmp != '\0'
533
 
         or tmp_id != (uid_t)tmp_id){
 
501
         or tmpmax != (uid_t)tmpmax){
534
502
        argp_error(state, "Bad user ID number: \"%s\", using %"
535
503
                   PRIdMAX, arg, (intmax_t)uid);
536
504
        break;
537
505
      }
538
 
      uid = (uid_t)tmp_id;
539
 
      errno = 0;
 
506
      uid = (uid_t)tmpmax;
540
507
      break;
541
508
    case 131:                   /* --groupid */
542
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
509
      tmpmax = strtoimax(arg, &tmp, 10);
543
510
      if(errno != 0 or tmp == arg or *tmp != '\0'
544
 
         or tmp_id != (gid_t)tmp_id){
 
511
         or tmpmax != (gid_t)tmpmax){
545
512
        argp_error(state, "Bad group ID number: \"%s\", using %"
546
513
                   PRIdMAX, arg, (intmax_t)gid);
547
514
        break;
548
515
      }
549
 
      gid = (gid_t)tmp_id;
550
 
      errno = 0;
 
516
      gid = (gid_t)tmpmax;
551
517
      break;
552
518
    case 132:                   /* --debug */
553
519
      debug = true;
554
520
      break;
555
 
    case 133:                   /* --plugin-helper-dir */
556
 
      free(pluginhelperdir);
557
 
      pluginhelperdir = strdup(arg);
558
 
      if(pluginhelperdir != NULL){
559
 
        errno = 0;
560
 
      }
561
 
      break;
562
521
      /*
563
522
       * These reproduce what we would get without ARGP_NO_HELP
564
523
       */
565
524
    case '?':                   /* --help */
566
525
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
567
526
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
568
 
      __builtin_unreachable();
569
527
    case -3:                    /* --usage */
570
528
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
571
529
      argp_state_help(state, state->out_stream,
572
530
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
573
 
      __builtin_unreachable();
574
531
    case 'V':                   /* --version */
575
532
      fprintf(state->out_stream, "%s\n", argp_program_version);
576
533
      exit(EXIT_SUCCESS);
586
543
      if(arg[0] == '\0'){
587
544
        break;
588
545
      }
589
 
#if __GNUC__ >= 7
590
 
      __attribute__((fallthrough));
591
 
#else
592
 
          /* FALLTHROUGH */
593
 
#endif
594
546
    default:
595
547
      return ARGP_ERR_UNKNOWN;
596
548
    }
615
567
    case 129:                   /* --config-file */
616
568
      free(argfile);
617
569
      argfile = strdup(arg);
618
 
      if(argfile != NULL){
619
 
        errno = 0;
620
 
      }
621
570
      break;
622
571
    case 130:                   /* --userid */
623
572
    case 131:                   /* --groupid */
624
573
    case 132:                   /* --debug */
625
 
    case 133:                   /* --plugin-helper-dir */
626
574
    case '?':                   /* --help */
627
575
    case -3:                    /* --usage */
628
576
    case 'V':                   /* --version */
650
598
  case ENOMEM:
651
599
  default:
652
600
    errno = ret;
653
 
    error(0, errno, "argp_parse");
 
601
    perror("argp_parse");
654
602
    exitstatus = EX_OSERR;
655
603
    goto fallback;
656
604
  case EINVAL:
677
625
    custom_argc = 1;
678
626
    custom_argv = malloc(sizeof(char*) * 2);
679
627
    if(custom_argv == NULL){
680
 
      error(0, errno, "malloc");
681
 
      exitstatus = EX_OSERR;
 
628
      perror("malloc");
 
629
      exitstatus = EXIT_FAILURE;
682
630
      goto fallback;
683
631
    }
684
632
    custom_argv[0] = argv[0];
700
648
        }
701
649
        new_arg = strdup(p);
702
650
        if(new_arg == NULL){
703
 
          error(0, errno, "strdup");
704
 
          exitstatus = EX_OSERR;
 
651
          perror("strdup");
 
652
          exitstatus = EXIT_FAILURE;
705
653
          free(org_line);
706
654
          goto fallback;
707
655
        }
708
656
        
709
657
        custom_argc += 1;
710
 
        {
711
 
          char **new_argv = realloc(custom_argv, sizeof(char *)
712
 
                                    * ((size_t)custom_argc + 1));
713
 
          if(new_argv == NULL){
714
 
            error(0, errno, "realloc");
715
 
            exitstatus = EX_OSERR;
716
 
            free(new_arg);
717
 
            free(org_line);
718
 
            goto fallback;
719
 
          } else {
720
 
            custom_argv = new_argv;
721
 
          }
 
658
        custom_argv = realloc(custom_argv, sizeof(char *)
 
659
                              * ((unsigned int) custom_argc + 1));
 
660
        if(custom_argv == NULL){
 
661
          perror("realloc");
 
662
          exitstatus = EXIT_FAILURE;
 
663
          free(org_line);
 
664
          goto fallback;
722
665
        }
723
666
        custom_argv[custom_argc-1] = new_arg;
724
667
        custom_argv[custom_argc] = NULL;
728
671
      ret = fclose(conffp);
729
672
    } while(ret == EOF and errno == EINTR);
730
673
    if(ret == EOF){
731
 
      error(0, errno, "fclose");
732
 
      exitstatus = EX_IOERR;
 
674
      perror("fclose");
 
675
      exitstatus = EXIT_FAILURE;
733
676
      goto fallback;
734
677
    }
735
678
    free(org_line);
737
680
    /* Check for harmful errors and go to fallback. Other errors might
738
681
       not affect opening plugins */
739
682
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
740
 
      error(0, errno, "fopen");
741
 
      exitstatus = EX_OSERR;
 
683
      perror("fopen");
 
684
      exitstatus = EXIT_FAILURE;
742
685
      goto fallback;
743
686
    }
744
687
  }
754
697
    case ENOMEM:
755
698
    default:
756
699
      errno = ret;
757
 
      error(0, errno, "argp_parse");
 
700
      perror("argp_parse");
758
701
      exitstatus = EX_OSERR;
759
702
      goto fallback;
760
703
    case EINVAL:
774
717
  case ENOMEM:
775
718
  default:
776
719
    errno = ret;
777
 
    error(0, errno, "argp_parse");
 
720
    perror("argp_parse");
778
721
    exitstatus = EX_OSERR;
779
722
    goto fallback;
780
723
  case EINVAL:
782
725
    goto fallback;
783
726
  }
784
727
  
785
 
  {
786
 
    char *pluginhelperenv;
787
 
    bool bret = true;
788
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
789
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
790
 
    if(ret != -1){
791
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
792
 
    }
793
 
    if(ret == -1 or not bret){
794
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
795
 
            " environment variable to \"%s\" for all plugins\n",
796
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
797
 
    }
798
 
    if(ret != -1){
799
 
      free(pluginhelperenv);
800
 
    }
801
 
  }
802
 
  
803
728
  if(debug){
804
 
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
729
    for(plugin *p = plugin_list; p != NULL; p=p->next){
805
730
      fprintf(stderr, "Plugin: %s has %d arguments\n",
806
731
              p->name ? p->name : "Global", p->argc - 1);
807
732
      for(char **a = p->argv; *a != NULL; a++){
814
739
    }
815
740
  }
816
741
  
817
 
  if(getuid() == 0){
818
 
    /* Work around Debian bug #633582:
819
 
       <https://bugs.debian.org/633582> */
820
 
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
821
 
    if(plugindir_fd == -1){
822
 
      if(errno != ENOENT){
823
 
        error(0, errno, "open(\"" PDIR "\")");
824
 
      }
825
 
    } else {
826
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
827
 
      if(ret == -1){
828
 
        error(0, errno, "fstat");
829
 
      } else {
830
 
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
831
 
          ret = fchown(plugindir_fd, uid, gid);
832
 
          if(ret == -1){
833
 
            error(0, errno, "fchown");
834
 
          }
835
 
        }
836
 
      }
837
 
      close(plugindir_fd);
838
 
    }
839
 
  }
840
 
  
841
 
  /* Lower permissions */
842
 
  ret = setgid(gid);
 
742
  /* Strip permissions down to nobody */
 
743
  setgid(gid);
843
744
  if(ret == -1){
844
 
    error(0, errno, "setgid");
 
745
    perror("setgid");
845
746
  }
846
747
  ret = setuid(uid);
847
748
  if(ret == -1){
848
 
    error(0, errno, "setuid");
 
749
    perror("setuid");
849
750
  }
850
751
  
851
752
  /* Open plugin directory with close_on_exec flag */
852
753
  {
853
 
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
854
 
#ifdef O_CLOEXEC
855
 
                  O_CLOEXEC
856
 
#else  /* not O_CLOEXEC */
857
 
                  0
858
 
#endif  /* not O_CLOEXEC */
859
 
                  );
 
754
    int dir_fd = -1;
 
755
    if(plugindir == NULL){
 
756
      dir_fd = open(PDIR, O_RDONLY |
 
757
#ifdef O_CLOEXEC
 
758
                    O_CLOEXEC
 
759
#else  /* not O_CLOEXEC */
 
760
                    0
 
761
#endif  /* not O_CLOEXEC */
 
762
                    );
 
763
    } else {
 
764
      dir_fd = open(plugindir, O_RDONLY |
 
765
#ifdef O_CLOEXEC
 
766
                    O_CLOEXEC
 
767
#else  /* not O_CLOEXEC */
 
768
                    0
 
769
#endif  /* not O_CLOEXEC */
 
770
                    );
 
771
    }
860
772
    if(dir_fd == -1){
861
 
      error(0, errno, "Could not open plugin dir");
862
 
      exitstatus = EX_UNAVAILABLE;
 
773
      perror("Could not open plugin dir");
 
774
      exitstatus = EXIT_FAILURE;
863
775
      goto fallback;
864
776
    }
865
777
    
867
779
  /* Set the FD_CLOEXEC flag on the directory */
868
780
    ret = set_cloexec_flag(dir_fd);
869
781
    if(ret < 0){
870
 
      error(0, errno, "set_cloexec_flag");
871
 
      exitstatus = EX_OSERR;
 
782
      perror("set_cloexec_flag");
 
783
      TEMP_FAILURE_RETRY(close(dir_fd));
 
784
      exitstatus = EXIT_FAILURE;
872
785
      goto fallback;
873
786
    }
874
787
#endif  /* O_CLOEXEC */
875
 
  }
876
 
  
877
 
  int good_name(const struct dirent * const dirent){
878
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
879
 
                                      "*.dpkg-old", "*.dpkg-bak",
880
 
                                      "*.dpkg-divert", NULL };
881
 
#ifdef __GNUC__
882
 
#pragma GCC diagnostic push
883
 
#pragma GCC diagnostic ignored "-Wcast-qual"
884
 
#endif
885
 
    for(const char **pat = (const char **)patterns;
886
 
        *pat != NULL; pat++){
887
 
#ifdef __GNUC__
888
 
#pragma GCC diagnostic pop
889
 
#endif
890
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
891
 
         != FNM_NOMATCH){
892
 
        if(debug){
893
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
894
 
                    " matching pattern %s\n", dirent->d_name, *pat);
895
 
        }
896
 
        return 0;
897
 
      }
 
788
    
 
789
    dir = fdopendir(dir_fd);
 
790
    if(dir == NULL){
 
791
      perror("Could not open plugin dir");
 
792
      TEMP_FAILURE_RETRY(close(dir_fd));
 
793
      exitstatus = EXIT_FAILURE;
 
794
      goto fallback;
898
795
    }
899
 
    return 1;
900
 
  }
901
 
  
902
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
903
 
                             alphasort);
904
 
  if(numplugins == -1){
905
 
    error(0, errno, "Could not scan plugin dir");
906
 
    direntries = NULL;
907
 
    exitstatus = EX_OSERR;
908
 
    goto fallback;
909
796
  }
910
797
  
911
798
  FD_ZERO(&rfds_all);
912
799
  
913
800
  /* Read and execute any executable in the plugin directory*/
914
 
  for(int i = 0; i < numplugins; i++){
915
 
    
916
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
917
 
    if(plugin_fd == -1){
918
 
      error(0, errno, "Could not open plugin");
919
 
      free(direntries[i]);
 
801
  while(true){
 
802
    do {
 
803
      dirst = readdir(dir);
 
804
    } while(dirst == NULL and errno == EINTR);
 
805
    
 
806
    /* All directory entries have been processed */
 
807
    if(dirst == NULL){
 
808
      if(errno == EBADF){
 
809
        perror("readdir");
 
810
        exitstatus = EXIT_FAILURE;
 
811
        goto fallback;
 
812
      }
 
813
      break;
 
814
    }
 
815
    
 
816
    d_name_len = strlen(dirst->d_name);
 
817
    
 
818
    /* Ignore dotfiles, backup files and other junk */
 
819
    {
 
820
      bool bad_name = false;
 
821
      
 
822
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
823
      
 
824
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
825
                                           ".dpkg-old",
 
826
                                           ".dpkg-bak",
 
827
                                           ".dpkg-divert", NULL };
 
828
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
829
        size_t pre_len = strlen(*pre);
 
830
        if((d_name_len >= pre_len)
 
831
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
 
832
          if(debug){
 
833
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
834
                    " with bad prefix %s\n", dirst->d_name, *pre);
 
835
          }
 
836
          bad_name = true;
 
837
          break;
 
838
        }
 
839
      }
 
840
      if(bad_name){
 
841
        continue;
 
842
      }
 
843
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
844
        size_t suf_len = strlen(*suf);
 
845
        if((d_name_len >= suf_len)
 
846
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
847
                == 0)){
 
848
          if(debug){
 
849
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
850
                    " with bad suffix %s\n", dirst->d_name, *suf);
 
851
          }
 
852
          bad_name = true;
 
853
          break;
 
854
        }
 
855
      }
 
856
      
 
857
      if(bad_name){
 
858
        continue;
 
859
      }
 
860
    }
 
861
    
 
862
    char *filename;
 
863
    if(plugindir == NULL){
 
864
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
865
                                             dirst->d_name));
 
866
    } else {
 
867
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
868
                                             plugindir,
 
869
                                             dirst->d_name));
 
870
    }
 
871
    if(ret < 0){
 
872
      perror("asprintf");
920
873
      continue;
921
874
    }
922
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
875
    
 
876
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
923
877
    if(ret == -1){
924
 
      error(0, errno, "stat");
925
 
      close(plugin_fd);
926
 
      free(direntries[i]);
 
878
      perror("stat");
 
879
      free(filename);
927
880
      continue;
928
881
    }
929
882
    
930
883
    /* Ignore non-executable files */
931
884
    if(not S_ISREG(st.st_mode)
932
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
933
 
                                        X_OK, 0)) != 0)){
 
885
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
934
886
      if(debug){
935
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
936
 
                " with bad type or mode\n",
937
 
                plugindir != NULL ? plugindir : PDIR,
938
 
                direntries[i]->d_name);
 
887
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
888
                " with bad type or mode\n", filename);
939
889
      }
940
 
      close(plugin_fd);
941
 
      free(direntries[i]);
 
890
      free(filename);
942
891
      continue;
943
892
    }
944
893
    
945
 
    plugin *p = getplugin(direntries[i]->d_name);
 
894
    plugin *p = getplugin(dirst->d_name);
946
895
    if(p == NULL){
947
 
      error(0, errno, "getplugin");
948
 
      close(plugin_fd);
949
 
      free(direntries[i]);
 
896
      perror("getplugin");
 
897
      free(filename);
950
898
      continue;
951
899
    }
952
900
    if(p->disabled){
953
901
      if(debug){
954
902
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
955
 
                direntries[i]->d_name);
 
903
                dirst->d_name);
956
904
      }
957
 
      close(plugin_fd);
958
 
      free(direntries[i]);
 
905
      free(filename);
959
906
      continue;
960
907
    }
961
908
    {
964
911
      if(g != NULL){
965
912
        for(char **a = g->argv + 1; *a != NULL; a++){
966
913
          if(not add_argument(p, *a)){
967
 
            error(0, errno, "add_argument");
 
914
            perror("add_argument");
968
915
          }
969
916
        }
970
917
        /* Add global environment variables */
971
918
        for(char **e = g->environ; *e != NULL; e++){
972
919
          if(not add_environment(p, *e, false)){
973
 
            error(0, errno, "add_environment");
 
920
            perror("add_environment");
974
921
          }
975
922
        }
976
923
      }
977
924
    }
978
 
    /* If this plugin has any environment variables, we need to
979
 
       duplicate the environment from this process, too. */
 
925
    /* If this plugin has any environment variables, we will call
 
926
       using execve and need to duplicate the environment from this
 
927
       process, too. */
980
928
    if(p->environ[0] != NULL){
981
929
      for(char **e = environ; *e != NULL; e++){
982
930
        if(not add_environment(p, *e, false)){
983
 
          error(0, errno, "add_environment");
 
931
          perror("add_environment");
984
932
        }
985
933
      }
986
934
    }
987
935
    
988
936
    int pipefd[2];
989
 
#ifndef O_CLOEXEC
990
937
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
991
 
#else  /* O_CLOEXEC */
992
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
993
 
#endif  /* O_CLOEXEC */
994
938
    if(ret == -1){
995
 
      error(0, errno, "pipe");
996
 
      exitstatus = EX_OSERR;
997
 
      free(direntries[i]);
998
 
      goto fallback;
999
 
    }
1000
 
    if(pipefd[0] >= FD_SETSIZE){
1001
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
1002
 
              FD_SETSIZE);
1003
 
      close(pipefd[0]);
1004
 
      close(pipefd[1]);
1005
 
      exitstatus = EX_OSERR;
1006
 
      free(direntries[i]);
1007
 
      goto fallback;
1008
 
    }
1009
 
#ifndef O_CLOEXEC
 
939
      perror("pipe");
 
940
      exitstatus = EXIT_FAILURE;
 
941
      goto fallback;
 
942
    }
1010
943
    /* Ask OS to automatic close the pipe on exec */
1011
944
    ret = set_cloexec_flag(pipefd[0]);
1012
945
    if(ret < 0){
1013
 
      error(0, errno, "set_cloexec_flag");
1014
 
      close(pipefd[0]);
1015
 
      close(pipefd[1]);
1016
 
      exitstatus = EX_OSERR;
1017
 
      free(direntries[i]);
 
946
      perror("set_cloexec_flag");
 
947
      exitstatus = EXIT_FAILURE;
1018
948
      goto fallback;
1019
949
    }
1020
950
    ret = set_cloexec_flag(pipefd[1]);
1021
951
    if(ret < 0){
1022
 
      error(0, errno, "set_cloexec_flag");
1023
 
      close(pipefd[0]);
1024
 
      close(pipefd[1]);
1025
 
      exitstatus = EX_OSERR;
1026
 
      free(direntries[i]);
 
952
      perror("set_cloexec_flag");
 
953
      exitstatus = EXIT_FAILURE;
1027
954
      goto fallback;
1028
955
    }
1029
 
#endif  /* not O_CLOEXEC */
1030
956
    /* Block SIGCHLD until process is safely in process list */
1031
957
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1032
958
                                              &sigchld_action.sa_mask,
1033
959
                                              NULL));
1034
960
    if(ret < 0){
1035
 
      error(0, errno, "sigprocmask");
1036
 
      exitstatus = EX_OSERR;
1037
 
      free(direntries[i]);
 
961
      perror("sigprocmask");
 
962
      exitstatus = EXIT_FAILURE;
1038
963
      goto fallback;
1039
964
    }
1040
965
    /* Starting a new process to be watched */
1043
968
      pid = fork();
1044
969
    } while(pid == -1 and errno == EINTR);
1045
970
    if(pid == -1){
1046
 
      error(0, errno, "fork");
1047
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1048
 
                                     &sigchld_action.sa_mask, NULL));
1049
 
      close(pipefd[0]);
1050
 
      close(pipefd[1]);
1051
 
      exitstatus = EX_OSERR;
1052
 
      free(direntries[i]);
 
971
      perror("fork");
 
972
      exitstatus = EXIT_FAILURE;
1053
973
      goto fallback;
1054
974
    }
1055
975
    if(pid == 0){
1056
976
      /* this is the child process */
1057
977
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1058
978
      if(ret < 0){
1059
 
        error(0, errno, "sigaction");
1060
 
        _exit(EX_OSERR);
 
979
        perror("sigaction");
 
980
        _exit(EXIT_FAILURE);
1061
981
      }
1062
982
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1063
983
      if(ret < 0){
1064
 
        error(0, errno, "sigprocmask");
1065
 
        _exit(EX_OSERR);
 
984
        perror("sigprocmask");
 
985
        _exit(EXIT_FAILURE);
1066
986
      }
1067
987
      
1068
988
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
1069
989
      if(ret == -1){
1070
 
        error(0, errno, "dup2");
1071
 
        _exit(EX_OSERR);
 
990
        perror("dup2");
 
991
        _exit(EXIT_FAILURE);
1072
992
      }
1073
993
      
1074
 
      if(fexecve(plugin_fd, p->argv,
1075
 
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1076
 
        error(0, errno, "fexecve for %s/%s",
1077
 
              plugindir != NULL ? plugindir : PDIR,
1078
 
              direntries[i]->d_name);
1079
 
        _exit(EX_OSERR);
 
994
      if(dirfd(dir) < 0){
 
995
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
996
           above and must now close it manually here. */
 
997
        closedir(dir);
 
998
      }
 
999
      if(p->environ[0] == NULL){
 
1000
        if(execv(filename, p->argv) < 0){
 
1001
          perror("execv");
 
1002
          _exit(EXIT_FAILURE);
 
1003
        }
 
1004
      } else {
 
1005
        if(execve(filename, p->argv, p->environ) < 0){
 
1006
          perror("execve");
 
1007
          _exit(EXIT_FAILURE);
 
1008
        }
1080
1009
      }
1081
1010
      /* no return */
1082
1011
    }
1083
1012
    /* Parent process */
1084
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1085
 
    close(plugin_fd);
1086
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1013
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1014
                                             pipe */
 
1015
    free(filename);
 
1016
    plugin *new_plugin = getplugin(dirst->d_name);
1087
1017
    if(new_plugin == NULL){
1088
 
      error(0, errno, "getplugin");
 
1018
      perror("getplugin");
1089
1019
      ret = (int)(TEMP_FAILURE_RETRY
1090
1020
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1091
1021
                               NULL)));
1092
1022
      if(ret < 0){
1093
 
        error(0, errno, "sigprocmask");
 
1023
        perror("sigprocmask");
1094
1024
      }
1095
 
      exitstatus = EX_OSERR;
1096
 
      free(direntries[i]);
 
1025
      exitstatus = EXIT_FAILURE;
1097
1026
      goto fallback;
1098
1027
    }
1099
 
    free(direntries[i]);
1100
1028
    
1101
1029
    new_plugin->pid = pid;
1102
1030
    new_plugin->fd = pipefd[0];
1103
 
 
1104
 
    if(debug){
1105
 
      fprintf(stderr, "Plugin %s started (PID %" PRIdMAX ")\n",
1106
 
              new_plugin->name, (intmax_t) (new_plugin->pid));
1107
 
    }
1108
 
 
 
1031
    
1109
1032
    /* Unblock SIGCHLD so signal handler can be run if this process
1110
1033
       has already completed */
1111
1034
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1112
1035
                                              &sigchld_action.sa_mask,
1113
1036
                                              NULL));
1114
1037
    if(ret < 0){
1115
 
      error(0, errno, "sigprocmask");
1116
 
      exitstatus = EX_OSERR;
 
1038
      perror("sigprocmask");
 
1039
      exitstatus = EXIT_FAILURE;
1117
1040
      goto fallback;
1118
1041
    }
1119
1042
    
1120
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1043
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1044
                                          -Wconversion */
1121
1045
    
1122
1046
    if(maxfd < new_plugin->fd){
1123
1047
      maxfd = new_plugin->fd;
1124
1048
    }
1125
1049
  }
1126
1050
  
1127
 
  free(direntries);
1128
 
  direntries = NULL;
1129
 
  close(dir_fd);
1130
 
  dir_fd = -1;
 
1051
  TEMP_FAILURE_RETRY(closedir(dir));
 
1052
  dir = NULL;
1131
1053
  free_plugin(getplugin(NULL));
1132
1054
  
1133
1055
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1146
1068
    fd_set rfds = rfds_all;
1147
1069
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1148
1070
    if(select_ret == -1 and errno != EINTR){
1149
 
      error(0, errno, "select");
1150
 
      exitstatus = EX_OSERR;
 
1071
      perror("select");
 
1072
      exitstatus = EXIT_FAILURE;
1151
1073
      goto fallback;
1152
1074
    }
1153
1075
    /* OK, now either a process completed, or something can be read
1172
1094
                      (intmax_t) (proc->pid),
1173
1095
                      WTERMSIG(proc->status),
1174
1096
                      strsignal(WTERMSIG(proc->status)));
 
1097
            } else if(WCOREDUMP(proc->status)){
 
1098
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1099
                      " core\n", proc->name, (intmax_t) (proc->pid));
1175
1100
            }
1176
1101
          }
1177
1102
          
1178
1103
          /* Remove the plugin */
1179
 
          FD_CLR(proc->fd, &rfds_all);
 
1104
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1105
                                          -Wconversion */
1180
1106
          
1181
1107
          /* Block signal while modifying process_list */
1182
1108
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1184
1110
                                         &sigchld_action.sa_mask,
1185
1111
                                         NULL));
1186
1112
          if(ret < 0){
1187
 
            error(0, errno, "sigprocmask");
1188
 
            exitstatus = EX_OSERR;
 
1113
            perror("sigprocmask");
 
1114
            exitstatus = EXIT_FAILURE;
1189
1115
            goto fallback;
1190
1116
          }
1191
1117
          
1198
1124
                      (sigprocmask(SIG_UNBLOCK,
1199
1125
                                   &sigchld_action.sa_mask, NULL)));
1200
1126
          if(ret < 0){
1201
 
            error(0, errno, "sigprocmask");
1202
 
            exitstatus = EX_OSERR;
 
1127
            perror("sigprocmask");
 
1128
            exitstatus = EXIT_FAILURE;
1203
1129
            goto fallback;
1204
1130
          }
1205
1131
          
1215
1141
        bool bret = print_out_password(proc->buffer,
1216
1142
                                       proc->buffer_length);
1217
1143
        if(not bret){
1218
 
          error(0, errno, "print_out_password");
1219
 
          exitstatus = EX_IOERR;
 
1144
          perror("print_out_password");
 
1145
          exitstatus = EXIT_FAILURE;
1220
1146
        }
1221
1147
        goto fallback;
1222
1148
      }
1223
1149
      
1224
1150
      /* This process has not completed.  Does it have any output? */
1225
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1151
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1152
                                                         warning from
 
1153
                                                         -Wconversion */
1226
1154
        /* This process had nothing to say at this time */
1227
1155
        proc = proc->next;
1228
1156
        continue;
1229
1157
      }
1230
1158
      /* Before reading, make the process' data buffer large enough */
1231
1159
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1232
 
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
1233
 
                                   + (size_t) BUFFER_SIZE);
1234
 
        if(new_buffer == NULL){
1235
 
          error(0, errno, "malloc");
1236
 
          exitstatus = EX_OSERR;
 
1160
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
1161
                               + (size_t) BUFFER_SIZE);
 
1162
        if(proc->buffer == NULL){
 
1163
          perror("malloc");
 
1164
          exitstatus = EXIT_FAILURE;
1237
1165
          goto fallback;
1238
1166
        }
1239
 
        proc->buffer = new_buffer;
1240
1167
        proc->buffer_size += BUFFER_SIZE;
1241
1168
      }
1242
1169
      /* Read from the process */
1261
1188
  
1262
1189
 fallback:
1263
1190
  
1264
 
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
1265
 
                             and exitstatus != EX_OK)){
 
1191
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1266
1192
    /* Fallback if all plugins failed, none are found or an error
1267
1193
       occured */
1268
1194
    bool bret;
1276
1202
    }
1277
1203
    bret = print_out_password(passwordbuffer, len);
1278
1204
    if(not bret){
1279
 
      error(0, errno, "print_out_password");
1280
 
      exitstatus = EX_IOERR;
 
1205
      perror("print_out_password");
 
1206
      exitstatus = EXIT_FAILURE;
1281
1207
    }
1282
1208
  }
1283
1209
  
1284
1210
  /* Restore old signal handler */
1285
1211
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1286
1212
  if(ret == -1){
1287
 
    error(0, errno, "sigaction");
1288
 
    exitstatus = EX_OSERR;
 
1213
    perror("sigaction");
 
1214
    exitstatus = EXIT_FAILURE;
1289
1215
  }
1290
1216
  
1291
1217
  if(custom_argv != NULL){
1295
1221
    free(custom_argv);
1296
1222
  }
1297
1223
  
1298
 
  free(direntries);
1299
 
  
1300
 
  if(dir_fd != -1){
1301
 
    close(dir_fd);
 
1224
  if(dir != NULL){
 
1225
    closedir(dir);
1302
1226
  }
1303
1227
  
1304
1228
  /* Kill the processes */
1308
1232
      ret = kill(p->pid, SIGTERM);
1309
1233
      if(ret == -1 and errno != ESRCH){
1310
1234
        /* Set-uid proccesses might not get closed */
1311
 
        error(0, errno, "kill");
 
1235
        perror("kill");
1312
1236
      }
1313
1237
    }
1314
1238
  }
1318
1242
    ret = wait(NULL);
1319
1243
  } while(ret >= 0);
1320
1244
  if(errno != ECHILD){
1321
 
    error(0, errno, "wait");
 
1245
    perror("wait");
1322
1246
  }
1323
1247
  
1324
1248
  free_plugin_list();
1325
1249
  
1326
1250
  free(plugindir);
1327
 
  free(pluginhelperdir);
1328
1251
  free(argfile);
1329
1252
  
1330
1253
  return exitstatus;