/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: 2008-09-01 16:53:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901165317-77dccp81zyqimt2j
* plugin-runner.c (add_environment): Override existing environment
                                     variables when asked to do so.
                                     All callers changed.
  (main): Removed old argp.args_doc string.  Parse argument file
          before normal command line arguments.  Use ARGP_IN_ORDER
          flag in both calls to argp_parse.  Do not strdup() strings
          to be sent to add_environment().

* plugin-runner.xml (OPTIONS): Document "--global-env" and "--envs-for".
  (FILES): Note that the config file is parsed before the command line
           options.

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
28
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
28
                                   EXIT_SUCCESS, realloc() */
30
29
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
33
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
33
                                   stat, waitpid(), WIFEXITED(),
35
34
                                   WEXITSTATUS(), wait(), pid_t,
47
46
                                   fcntl(), setuid(), setgid(),
48
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
49
48
                                   access(), pipe(), fork(), close()
50
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
51
50
                                   execv(), write(), read(),
52
51
                                   close() */
53
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
64
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
64
                                   SIG_UNBLOCK, kill() */
66
65
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
66
 
69
67
#define BUFFER_SIZE 256
70
68
 
71
69
#define PDIR "/lib/mandos/plugins.d"
72
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
73
71
 
74
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
75
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
76
74
 
 
75
struct plugin;
 
76
 
77
77
typedef struct plugin{
78
78
  char *name;                   /* can be NULL or any plugin name */
79
79
  char **argv;
89
89
  size_t buffer_size;
90
90
  size_t buffer_length;
91
91
  bool eof;
92
 
  volatile sig_atomic_t completed;
93
 
  int status;
 
92
  volatile bool completed;
 
93
  volatile int status;
94
94
  struct plugin *next;
95
95
} plugin;
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
100
100
   or if none is found, creates a new one */
101
101
static plugin *getplugin(char *name){
102
102
  /* Check for exiting plugin with that name */
103
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
104
 
    if((p->name == name)
105
 
       or (p->name and name and (strcmp(p->name, name) == 0))){
 
103
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
104
    if ((p->name == name)
 
105
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
106
      return p;
107
107
    }
108
108
  }
109
109
  /* Create a new plugin */
110
110
  plugin *new_plugin = malloc(sizeof(plugin));
111
 
  if(new_plugin == NULL){
 
111
  if (new_plugin == NULL){
112
112
    return NULL;
113
113
  }
114
114
  char *copy_name = NULL;
115
115
  if(name != NULL){
116
116
    copy_name = strdup(name);
117
117
    if(copy_name == NULL){
118
 
      free(new_plugin);
119
118
      return NULL;
120
119
    }
121
120
  }
122
 
  
123
 
  *new_plugin = (plugin){ .name = copy_name,
124
 
                          .argc = 1,
125
 
                          .disabled = false,
126
 
                          .next = plugin_list };
 
121
 
 
122
  *new_plugin = (plugin) { .name = copy_name,
 
123
                           .argc = 1,
 
124
                           .disabled = false,
 
125
                           .next = plugin_list };
127
126
  
128
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
 
  if(new_plugin->argv == NULL){
 
128
  if (new_plugin->argv == NULL){
130
129
    free(copy_name);
131
130
    free(new_plugin);
132
131
    return NULL;
188
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
189
188
  /* Search for this environment variable */
190
189
  for(char **e = p->environ; *e != NULL; e++){
191
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
192
191
      /* It already exists */
193
192
      if(replace){
194
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
195
194
        if(new == NULL){
196
195
          return false;
197
196
        }
209
208
 * Descriptor Flags".
210
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
211
210
 */
212
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
213
213
  int ret = fcntl(fd, F_GETFD, 0);
214
214
  /* If reading the flags failed, return error indication now. */
215
215
  if(ret < 0){
222
222
 
223
223
/* Mark processes as completed when they exit, and save their exit
224
224
   status. */
225
 
static void handle_sigchld(__attribute__((unused)) int sig){
226
 
  int old_errno = errno;
 
225
void handle_sigchld(__attribute__((unused)) int sig){
227
226
  while(true){
228
227
    plugin *proc = plugin_list;
229
228
    int status;
233
232
      break;
234
233
    }
235
234
    if(pid == -1){
236
 
      if(errno == ECHILD){
237
 
        /* No child processes */
238
 
        break;
 
235
      if (errno != ECHILD){
 
236
        perror("waitpid");
239
237
      }
240
 
      perror("waitpid");
 
238
      /* No child processes */
 
239
      break;
241
240
    }
242
 
    
 
241
 
243
242
    /* A child exited, find it in process_list */
244
243
    while(proc != NULL and proc->pid != pid){
245
244
      proc = proc->next;
249
248
      continue;
250
249
    }
251
250
    proc->status = status;
252
 
    proc->completed = 1;
 
251
    proc->completed = true;
253
252
  }
254
 
  errno = old_errno;
255
253
}
256
254
 
257
255
/* Prints out a password to stdout */
258
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
259
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
260
261
  for(size_t written = 0; written < length; written += (size_t)ret){
261
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
262
263
                                   length - written));
312
313
  struct dirent *dirst;
313
314
  struct stat st;
314
315
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
316
 
  ssize_t sret;
317
 
  intmax_t tmpmax;
 
316
  int ret, maxfd = 0;
318
317
  uid_t uid = 65534;
319
318
  gid_t gid = 65534;
320
319
  bool debug = false;
345
344
    { .name = "global-options", .key = 'g',
346
345
      .arg = "OPTION[,OPTION[,...]]",
347
346
      .doc = "Options passed to all plugins" },
348
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
349
348
      .arg = "VAR=value",
350
349
      .doc = "Environment variable passed to all plugins" },
351
350
    { .name = "options-for", .key = 'o',
352
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
353
352
      .doc = "Options passed only to specified plugin" },
354
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
355
354
      .arg = "PLUGIN:ENV=value",
356
355
      .doc = "Environment variable passed to specified plugin" },
357
356
    { .name = "disable", .key = 'd',
358
357
      .arg = "PLUGIN",
359
358
      .doc = "Disable a specific plugin", .group = 1 },
360
 
    { .name = "enable", .key = 'e',
361
 
      .arg = "PLUGIN",
362
 
      .doc = "Enable a specific plugin", .group = 1 },
363
359
    { .name = "plugin-dir", .key = 128,
364
360
      .arg = "DIRECTORY",
365
361
      .doc = "Specify a different plugin directory", .group = 2 },
377
373
    { .name = NULL }
378
374
  };
379
375
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
 
378
    /* Get the INPUT argument from `argp_parse', which we know is a
 
379
       pointer to our plugin list pointer. */
 
380
    switch (key) {
383
381
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
 
382
      if (arg != NULL){
385
383
        char *p;
386
384
        while((p = strsep(&arg, ",")) != NULL){
387
385
          if(p[0] == '\0'){
394
392
        }
395
393
      }
396
394
      break;
397
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
398
396
      if(arg == NULL){
399
397
        break;
400
398
      }
401
 
      if(not add_environment(getplugin(NULL), arg, true)){
402
 
        perror("add_environment");
 
399
      {
 
400
        char *envdef = strdup(arg);
 
401
        if(envdef == NULL){
 
402
          break;
 
403
        }
 
404
        if(not add_environment(getplugin(NULL), envdef, true)){
 
405
          perror("add_environment");
 
406
        }
403
407
      }
404
408
      break;
405
409
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
 
410
      if (arg != NULL){
407
411
        char *p_name = strsep(&arg, ":");
408
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
409
413
          break;
410
414
        }
411
415
        char *opt = strsep(&arg, ":");
412
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
413
417
          break;
414
418
        }
415
 
        char *p;
416
 
        while((p = strsep(&opt, ",")) != NULL){
417
 
          if(p[0] == '\0'){
418
 
            continue;
419
 
          }
420
 
          if(not add_argument(getplugin(p_name), p)){
421
 
            perror("add_argument");
422
 
            return ARGP_ERR_UNKNOWN;
 
419
        if(opt != NULL){
 
420
          char *p;
 
421
          while((p = strsep(&opt, ",")) != NULL){
 
422
            if(p[0] == '\0'){
 
423
              continue;
 
424
            }
 
425
            if(not add_argument(getplugin(p_name), p)){
 
426
              perror("add_argument");
 
427
              return ARGP_ERR_UNKNOWN;
 
428
            }
423
429
          }
424
430
        }
425
431
      }
426
432
      break;
427
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
428
434
      if(arg == NULL){
429
435
        break;
430
436
      }
433
439
        if(envdef == NULL){
434
440
          break;
435
441
        }
436
 
        *envdef = '\0';
437
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
442
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
443
        if(p_name == NULL){
 
444
          break;
 
445
        }
 
446
        envdef++;
 
447
        if(not add_environment(getplugin(p_name), envdef, true)){
438
448
          perror("add_environment");
439
449
        }
440
450
      }
441
451
      break;
442
452
    case 'd':                   /* --disable */
443
 
      if(arg != NULL){
 
453
      if (arg != NULL){
444
454
        plugin *p = getplugin(arg);
445
455
        if(p == NULL){
446
456
          return ARGP_ERR_UNKNOWN;
448
458
        p->disabled = true;
449
459
      }
450
460
      break;
451
 
    case 'e':                   /* --enable */
452
 
      if(arg != NULL){
453
 
        plugin *p = getplugin(arg);
454
 
        if(p == NULL){
455
 
          return ARGP_ERR_UNKNOWN;
456
 
        }
457
 
        p->disabled = false;
458
 
      }
459
 
      break;
460
461
    case 128:                   /* --plugin-dir */
461
 
      free(plugindir);
462
462
      plugindir = strdup(arg);
463
463
      if(plugindir == NULL){
464
464
        perror("strdup");
465
465
      }      
466
466
      break;
467
467
    case 129:                   /* --config-file */
468
 
      /* This is already done by parse_opt_config_file() */
469
 
      break;
470
 
    case 130:                   /* --userid */
471
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
472
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
473
 
         or arg[numchars] != '\0'){
474
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
 
                PRIdMAX "\n", arg, (intmax_t)uid);
476
 
      } else {
477
 
        uid = (uid_t)tmpmax;
478
 
      }
479
 
      break;
480
 
    case 131:                   /* --groupid */
481
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
482
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
483
 
         or arg[numchars] != '\0'){
484
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
 
                PRIdMAX "\n", arg, (intmax_t)gid);
486
 
      } else {
487
 
        gid = (gid_t)tmpmax;
488
 
      }
489
 
      break;
490
 
    case 132:                   /* --debug */
491
 
      debug = true;
492
 
      break;
493
 
/*
494
 
 * When adding more options before this line, remember to also add a
495
 
 * "case" to the "parse_opt_config_file" function below.
496
 
 */
497
 
    case ARGP_KEY_ARG:
498
 
      /* Cryptsetup always passes an argument, which is an empty
499
 
         string if "none" was specified in /etc/crypttab.  So if
500
 
         argument was empty, we ignore it silently. */
501
 
      if(arg[0] != '\0'){
502
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
503
 
      }
504
 
      break;
505
 
    case ARGP_KEY_END:
506
 
      break;
507
 
    default:
508
 
      return ARGP_ERR_UNKNOWN;
509
 
    }
510
 
    return 0;
511
 
  }
512
 
  
513
 
  /* This option parser is the same as parse_opt() above, except it
514
 
     ignores everything but the --config-file option. */
515
 
  error_t parse_opt_config_file(int key, char *arg,
516
 
                                __attribute__((unused))
517
 
                                struct argp_state *state){
518
 
    switch(key){
519
 
    case 'g':                   /* --global-options */
520
 
    case 'G':                   /* --global-env */
521
 
    case 'o':                   /* --options-for */
522
 
    case 'E':                   /* --env-for */
523
 
    case 'd':                   /* --disable */
524
 
    case 'e':                   /* --enable */
525
 
    case 128:                   /* --plugin-dir */
526
 
      break;
527
 
    case 129:                   /* --config-file */
528
 
      free(argfile);
529
468
      argfile = strdup(arg);
530
469
      if(argfile == NULL){
531
470
        perror("strdup");
532
471
      }
533
472
      break;      
534
473
    case 130:                   /* --userid */
 
474
      uid = (uid_t)strtol(arg, NULL, 10);
 
475
      break;
535
476
    case 131:                   /* --groupid */
 
477
      gid = (gid_t)strtol(arg, NULL, 10);
 
478
      break;
536
479
    case 132:                   /* --debug */
 
480
      debug = true;
 
481
      break;
537
482
    case ARGP_KEY_ARG:
 
483
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
484
      break;
538
485
    case ARGP_KEY_END:
539
486
      break;
540
487
    default:
543
490
    return 0;
544
491
  }
545
492
  
546
 
  struct argp argp = { .options = options,
547
 
                       .parser = parse_opt_config_file,
 
493
  struct argp argp = { .options = options, .parser = parse_opt,
548
494
                       .args_doc = "",
549
495
                       .doc = "Mandos plugin runner -- Run plugins" };
550
496
  
551
 
  /* Parse using parse_opt_config_file() in order to get the custom
552
 
     config file location, if any. */
553
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
554
 
  if(ret == ARGP_ERR_UNKNOWN){
555
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
556
 
    exitstatus = EXIT_FAILURE;
557
 
    goto fallback;
558
 
  }
559
 
  
560
 
  /* Reset to the normal argument parser */
561
 
  argp.parser = parse_opt;
562
 
  
563
497
  /* Open the configfile if available */
564
 
  if(argfile == NULL){
 
498
  if (argfile == NULL){
565
499
    conffp = fopen(AFILE, "r");
566
500
  } else {
567
501
    conffp = fopen(argfile, "r");
570
504
    char *org_line = NULL;
571
505
    char *p, *arg, *new_arg, *line;
572
506
    size_t size = 0;
 
507
    ssize_t sret;
573
508
    const char whitespace_delims[] = " \r\t\f\v\n";
574
509
    const char comment_delim[] = "#";
575
510
 
622
557
  } else {
623
558
    /* Check for harmful errors and go to fallback. Other errors might
624
559
       not affect opening plugins */
625
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
560
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
626
561
      perror("fopen");
627
562
      exitstatus = EXIT_FAILURE;
628
563
      goto fallback;
631
566
  /* If there was any arguments from configuration file,
632
567
     pass them to parser as command arguments */
633
568
  if(custom_argv != NULL){
634
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
635
 
                     0, NULL);
636
 
    if(ret == ARGP_ERR_UNKNOWN){
 
569
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
570
                      0, NULL);
 
571
    if (ret == ARGP_ERR_UNKNOWN){
637
572
      fprintf(stderr, "Unknown error while parsing arguments\n");
638
573
      exitstatus = EXIT_FAILURE;
639
574
      goto fallback;
642
577
  
643
578
  /* Parse actual command line arguments, to let them override the
644
579
     config file */
645
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
646
 
  if(ret == ARGP_ERR_UNKNOWN){
 
580
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
581
  if (ret == ARGP_ERR_UNKNOWN){
647
582
    fprintf(stderr, "Unknown error while parsing arguments\n");
648
583
    exitstatus = EXIT_FAILURE;
649
584
    goto fallback;
656
591
      for(char **a = p->argv; *a != NULL; a++){
657
592
        fprintf(stderr, "\tArg: %s\n", *a);
658
593
      }
659
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
594
      fprintf(stderr, "...and %u environment variables\n", p->envc);
660
595
      for(char **a = p->environ; *a != NULL; a++){
661
596
        fprintf(stderr, "\t%s\n", *a);
662
597
      }
664
599
  }
665
600
  
666
601
  /* Strip permissions down to nobody */
 
602
  ret = setuid(uid);
 
603
  if (ret == -1){
 
604
    perror("setuid");
 
605
  }  
667
606
  setgid(gid);
668
 
  if(ret == -1){
 
607
  if (ret == -1){
669
608
    perror("setgid");
670
609
  }
671
 
  ret = setuid(uid);
672
 
  if(ret == -1){
673
 
    perror("setuid");
674
 
  }
675
610
  
676
 
  if(plugindir == NULL){
 
611
  if (plugindir == NULL){
677
612
    dir = opendir(PDIR);
678
613
  } else {
679
614
    dir = opendir(plugindir);
706
641
    
707
642
    /* All directory entries have been processed */
708
643
    if(dirst == NULL){
709
 
      if(errno == EBADF){
 
644
      if (errno == EBADF){
710
645
        perror("readdir");
711
646
        exitstatus = EXIT_FAILURE;
712
647
        goto fallback;
724
659
      
725
660
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
726
661
                                           ".dpkg-old",
727
 
                                           ".dpkg-bak",
728
662
                                           ".dpkg-divert", NULL };
729
663
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
730
664
        size_t pre_len = strlen(*pre);
761
695
    }
762
696
 
763
697
    char *filename;
764
 
    if(plugindir == NULL){
765
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
766
 
    } else {
767
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
768
 
    }
 
698
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
769
699
    if(ret < 0){
770
700
      perror("asprintf");
771
701
      continue;
772
702
    }
773
703
    
774
704
    ret = stat(filename, &st);
775
 
    if(ret == -1){
 
705
    if (ret == -1){
776
706
      perror("stat");
777
707
      free(filename);
778
708
      continue;
779
709
    }
780
710
 
781
711
    /* Ignore non-executable files */
782
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
712
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
783
713
      if(debug){
784
714
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
785
715
                " with bad type or mode\n", filename);
832
762
    
833
763
    int pipefd[2];
834
764
    ret = pipe(pipefd);
835
 
    if(ret == -1){
 
765
    if (ret == -1){
836
766
      perror("pipe");
837
767
      exitstatus = EXIT_FAILURE;
838
768
      goto fallback;
851
781
      goto fallback;
852
782
    }
853
783
    /* Block SIGCHLD until process is safely in process list */
854
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
784
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
855
785
    if(ret < 0){
856
786
      perror("sigprocmask");
857
787
      exitstatus = EXIT_FAILURE;
871
801
        perror("sigaction");
872
802
        _exit(EXIT_FAILURE);
873
803
      }
874
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
804
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
875
805
      if(ret < 0){
876
806
        perror("sigprocmask");
877
807
        _exit(EXIT_FAILURE);
878
808
      }
879
 
      
 
809
 
880
810
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
881
811
      if(ret == -1){
882
812
        perror("dup2");
905
835
    close(pipefd[1]);           /* Close unused write end of pipe */
906
836
    free(filename);
907
837
    plugin *new_plugin = getplugin(dirst->d_name);
908
 
    if(new_plugin == NULL){
 
838
    if (new_plugin == NULL){
909
839
      perror("getplugin");
910
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
840
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
841
      if(ret < 0){
912
842
        perror("sigprocmask");
913
843
      }
920
850
    
921
851
    /* Unblock SIGCHLD so signal handler can be run if this process
922
852
       has already completed */
923
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
853
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
924
854
    if(ret < 0){
925
855
      perror("sigprocmask");
926
856
      exitstatus = EXIT_FAILURE;
929
859
    
930
860
    FD_SET(new_plugin->fd, &rfds_all);
931
861
    
932
 
    if(maxfd < new_plugin->fd){
 
862
    if (maxfd < new_plugin->fd){
933
863
      maxfd = new_plugin->fd;
934
864
    }
 
865
    
935
866
  }
936
867
  
937
868
  closedir(dir);
938
869
  dir = NULL;
939
 
  
 
870
 
940
871
  for(plugin *p = plugin_list; p != NULL; p = p->next){
941
872
    if(p->pid != 0){
942
873
      break;
947
878
      free_plugin_list();
948
879
    }
949
880
  }
950
 
  
 
881
 
951
882
  /* Main loop while running plugins exist */
952
883
  while(plugin_list){
953
884
    fd_set rfds = rfds_all;
954
885
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
955
 
    if(select_ret == -1){
 
886
    if (select_ret == -1){
956
887
      perror("select");
957
888
      exitstatus = EXIT_FAILURE;
958
889
      goto fallback;
959
890
    }
960
891
    /* OK, now either a process completed, or something can be read
961
892
       from one of them */
962
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
893
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
963
894
      /* Is this process completely done? */
964
 
      if(proc->completed and proc->eof){
 
895
      if(proc->eof and proc->completed){
965
896
        /* Only accept the plugin output if it exited cleanly */
966
897
        if(not WIFEXITED(proc->status)
967
898
           or WEXITSTATUS(proc->status) != 0){
969
900
 
970
901
          if(debug){
971
902
            if(WIFEXITED(proc->status)){
972
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
973
 
                      " status %d\n", proc->name,
974
 
                      (intmax_t) (proc->pid),
 
903
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
904
                      (unsigned int) (proc->pid),
975
905
                      WEXITSTATUS(proc->status));
976
 
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
978
 
                      " signal %d\n", proc->name,
979
 
                      (intmax_t) (proc->pid),
 
906
            } else if(WIFSIGNALED(proc->status)) {
 
907
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
908
                      (unsigned int) (proc->pid),
980
909
                      WTERMSIG(proc->status));
981
910
            } else if(WCOREDUMP(proc->status)){
982
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
983
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
911
              fprintf(stderr, "Plugin %d dumped core\n",
 
912
                      (unsigned int) (proc->pid));
984
913
            }
985
914
          }
986
915
          
994
923
            exitstatus = EXIT_FAILURE;
995
924
            goto fallback;
996
925
          }
997
 
          
998
 
          plugin *next_plugin = proc->next;
999
926
          free_plugin(proc);
1000
 
          proc = next_plugin;
1001
 
          
1002
927
          /* We are done modifying process list, so unblock signal */
1003
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1004
 
                            NULL);
 
928
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
929
                             NULL);
1005
930
          if(ret < 0){
1006
931
            perror("sigprocmask");
1007
932
            exitstatus = EXIT_FAILURE;
1011
936
          if(plugin_list == NULL){
1012
937
            break;
1013
938
          }
1014
 
          
1015
939
          continue;
1016
940
        }
1017
941
        
1018
942
        /* This process exited nicely, so print its buffer */
1019
 
        
 
943
 
1020
944
        bool bret = print_out_password(proc->buffer,
1021
945
                                       proc->buffer_length);
1022
946
        if(not bret){
1029
953
      /* This process has not completed.  Does it have any output? */
1030
954
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1031
955
        /* This process had nothing to say at this time */
1032
 
        proc = proc->next;
1033
956
        continue;
1034
957
      }
1035
958
      /* Before reading, make the process' data buffer large enough */
1036
959
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1037
960
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1038
961
                               + (size_t) BUFFER_SIZE);
1039
 
        if(proc->buffer == NULL){
 
962
        if (proc->buffer == NULL){
1040
963
          perror("malloc");
1041
964
          exitstatus = EXIT_FAILURE;
1042
965
          goto fallback;
1044
967
        proc->buffer_size += BUFFER_SIZE;
1045
968
      }
1046
969
      /* Read from the process */
1047
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1048
 
                  BUFFER_SIZE);
1049
 
      if(sret < 0){
 
970
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
971
                 BUFFER_SIZE);
 
972
      if(ret < 0){
1050
973
        /* Read error from this process; ignore the error */
1051
 
        proc = proc->next;
1052
974
        continue;
1053
975
      }
1054
 
      if(sret == 0){
 
976
      if(ret == 0){
1055
977
        /* got EOF */
1056
978
        proc->eof = true;
1057
979
      } else {
1058
 
        proc->buffer_length += (size_t) sret;
 
980
        proc->buffer_length += (size_t) ret;
1059
981
      }
1060
982
    }
1061
983
  }
1069
991
    bool bret;
1070
992
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1071
993
    char *passwordbuffer = getpass("Password: ");
1072
 
    size_t len = strlen(passwordbuffer);
1073
 
    /* Strip trailing newline */
1074
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1075
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1076
 
      len--;
1077
 
    }
1078
 
    bret = print_out_password(passwordbuffer, len);
 
994
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1079
995
    if(not bret){
1080
996
      perror("print_out_password");
1081
997
      exitstatus = EXIT_FAILURE;
1088
1004
    perror("sigaction");
1089
1005
    exitstatus = EXIT_FAILURE;
1090
1006
  }
1091
 
  
 
1007
 
1092
1008
  if(custom_argv != NULL){
1093
1009
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1094
1010
      free(*arg);
1100
1016
    closedir(dir);
1101
1017
  }
1102
1018
  
1103
 
  /* Kill the processes */
 
1019
  /* Free the process list and kill the processes */
1104
1020
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1105
1021
    if(p->pid != 0){
1106
1022
      close(p->fd);
1119
1035
  if(errno != ECHILD){
1120
1036
    perror("wait");
1121
1037
  }
1122
 
  
 
1038
 
1123
1039
  free_plugin_list();
1124
1040
  
1125
1041
  free(plugindir);