/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,
70
69
#define PDIR "/lib/mandos/plugins.d"
71
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
72
71
 
73
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
74
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
75
74
 
 
75
struct plugin;
 
76
 
76
77
typedef struct plugin{
77
78
  char *name;                   /* can be NULL or any plugin name */
78
79
  char **argv;
95
96
 
96
97
static plugin *plugin_list = NULL;
97
98
 
98
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
99
100
   or if none is found, creates a new one */
100
101
static plugin *getplugin(char *name){
101
102
  /* Check for exiting plugin with that name */
102
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if((p->name == name)
104
 
       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))){
105
106
      return p;
106
107
    }
107
108
  }
108
109
  /* Create a new plugin */
109
110
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if(new_plugin == NULL){
 
111
  if (new_plugin == NULL){
111
112
    return NULL;
112
113
  }
113
114
  char *copy_name = NULL;
117
118
      return NULL;
118
119
    }
119
120
  }
120
 
  
 
121
 
121
122
  *new_plugin = (plugin) { .name = copy_name,
122
123
                           .argc = 1,
123
124
                           .disabled = false,
124
125
                           .next = plugin_list };
125
126
  
126
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if(new_plugin->argv == NULL){
 
128
  if (new_plugin->argv == NULL){
128
129
    free(copy_name);
129
130
    free(new_plugin);
130
131
    return NULL;
186
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
187
188
  /* Search for this environment variable */
188
189
  for(char **e = p->environ; *e != NULL; e++){
189
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
190
191
      /* It already exists */
191
192
      if(replace){
192
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
193
194
        if(new == NULL){
194
195
          return false;
195
196
        }
207
208
 * Descriptor Flags".
208
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
209
210
 */
210
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
211
213
  int ret = fcntl(fd, F_GETFD, 0);
212
214
  /* If reading the flags failed, return error indication now. */
213
215
  if(ret < 0){
220
222
 
221
223
/* Mark processes as completed when they exit, and save their exit
222
224
   status. */
223
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
224
226
  while(true){
225
227
    plugin *proc = plugin_list;
226
228
    int status;
230
232
      break;
231
233
    }
232
234
    if(pid == -1){
233
 
      if(errno != ECHILD){
 
235
      if (errno != ECHILD){
234
236
        perror("waitpid");
235
237
      }
236
238
      /* No child processes */
237
239
      break;
238
240
    }
239
 
    
 
241
 
240
242
    /* A child exited, find it in process_list */
241
243
    while(proc != NULL and proc->pid != pid){
242
244
      proc = proc->next;
251
253
}
252
254
 
253
255
/* Prints out a password to stdout */
254
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
255
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
256
261
  for(size_t written = 0; written < length; written += (size_t)ret){
257
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
258
263
                                   length - written));
309
314
  struct stat st;
310
315
  fd_set rfds_all;
311
316
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
317
  uid_t uid = 65534;
314
318
  gid_t gid = 65534;
315
319
  bool debug = false;
340
344
    { .name = "global-options", .key = 'g',
341
345
      .arg = "OPTION[,OPTION[,...]]",
342
346
      .doc = "Options passed to all plugins" },
343
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
344
348
      .arg = "VAR=value",
345
349
      .doc = "Environment variable passed to all plugins" },
346
350
    { .name = "options-for", .key = 'o',
347
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
348
352
      .doc = "Options passed only to specified plugin" },
349
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
350
354
      .arg = "PLUGIN:ENV=value",
351
355
      .doc = "Environment variable passed to specified plugin" },
352
356
    { .name = "disable", .key = 'd',
353
357
      .arg = "PLUGIN",
354
358
      .doc = "Disable a specific plugin", .group = 1 },
355
 
    { .name = "enable", .key = 'e',
356
 
      .arg = "PLUGIN",
357
 
      .doc = "Enable a specific plugin", .group = 1 },
358
359
    { .name = "plugin-dir", .key = 128,
359
360
      .arg = "DIRECTORY",
360
361
      .doc = "Specify a different plugin directory", .group = 2 },
372
373
    { .name = NULL }
373
374
  };
374
375
  
375
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
376
 
                    struct argp_state *state) {
377
 
    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) {
378
381
    case 'g':                   /* --global-options */
379
 
      if(arg != NULL){
 
382
      if (arg != NULL){
380
383
        char *p;
381
384
        while((p = strsep(&arg, ",")) != NULL){
382
385
          if(p[0] == '\0'){
389
392
        }
390
393
      }
391
394
      break;
392
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
393
396
      if(arg == NULL){
394
397
        break;
395
398
      }
396
 
      if(not add_environment(getplugin(NULL), arg, true)){
397
 
        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
        }
398
407
      }
399
408
      break;
400
409
    case 'o':                   /* --options-for */
401
 
      if(arg != NULL){
 
410
      if (arg != NULL){
402
411
        char *p_name = strsep(&arg, ":");
403
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
404
413
          break;
405
414
        }
406
415
        char *opt = strsep(&arg, ":");
407
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
408
417
          break;
409
418
        }
410
 
        char *p;
411
 
        while((p = strsep(&opt, ",")) != NULL){
412
 
          if(p[0] == '\0'){
413
 
            continue;
414
 
          }
415
 
          if(not add_argument(getplugin(p_name), p)){
416
 
            perror("add_argument");
417
 
            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
            }
418
429
          }
419
430
        }
420
431
      }
421
432
      break;
422
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
423
434
      if(arg == NULL){
424
435
        break;
425
436
      }
428
439
        if(envdef == NULL){
429
440
          break;
430
441
        }
431
 
        *envdef = '\0';
432
 
        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)){
433
448
          perror("add_environment");
434
449
        }
435
450
      }
436
451
      break;
437
452
    case 'd':                   /* --disable */
438
 
      if(arg != NULL){
 
453
      if (arg != NULL){
439
454
        plugin *p = getplugin(arg);
440
455
        if(p == NULL){
441
456
          return ARGP_ERR_UNKNOWN;
443
458
        p->disabled = true;
444
459
      }
445
460
      break;
446
 
    case 'e':                   /* --enable */
447
 
      if(arg != NULL){
448
 
        plugin *p = getplugin(arg);
449
 
        if(p == NULL){
450
 
          return ARGP_ERR_UNKNOWN;
451
 
        }
452
 
        p->disabled = false;
453
 
      }
454
 
      break;
455
461
    case 128:                   /* --plugin-dir */
456
 
      free(plugindir);
457
462
      plugindir = strdup(arg);
458
463
      if(plugindir == NULL){
459
464
        perror("strdup");
460
465
      }      
461
466
      break;
462
467
    case 129:                   /* --config-file */
463
 
      /* This is already done by parse_opt_config_file() */
464
 
      break;
465
 
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%u", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
470
 
                uid);
471
 
      }
472
 
      break;
473
 
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%u", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
478
 
                arg, gid);
479
 
      }
480
 
      break;
481
 
    case 132:                   /* --debug */
482
 
      debug = true;
483
 
      break;
484
 
/*
485
 
 * When adding more options before this line, remember to also add a
486
 
 * "case" to the "parse_opt_config_file" function below.
487
 
 */
488
 
    case ARGP_KEY_ARG:
489
 
      /* Cryptsetup always passes an argument, which is an empty
490
 
         string if "none" was specified in /etc/crypttab.  So if
491
 
         argument was empty, we ignore it silently. */
492
 
      if(arg[0] != '\0'){
493
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
494
 
      }
495
 
      break;
496
 
    case ARGP_KEY_END:
497
 
      break;
498
 
    default:
499
 
      return ARGP_ERR_UNKNOWN;
500
 
    }
501
 
    return 0;
502
 
  }
503
 
  
504
 
  /* This option parser is the same as parse_opt() above, except it
505
 
     ignores everything but the --config-file option. */
506
 
  error_t parse_opt_config_file(int key, char *arg,
507
 
                                __attribute__((unused))
508
 
                                struct argp_state *state) {
509
 
    switch(key) {
510
 
    case 'g':                   /* --global-options */
511
 
    case 'G':                   /* --global-env */
512
 
    case 'o':                   /* --options-for */
513
 
    case 'E':                   /* --env-for */
514
 
    case 'd':                   /* --disable */
515
 
    case 'e':                   /* --enable */
516
 
    case 128:                   /* --plugin-dir */
517
 
      break;
518
 
    case 129:                   /* --config-file */
519
 
      free(argfile);
520
468
      argfile = strdup(arg);
521
469
      if(argfile == NULL){
522
470
        perror("strdup");
523
471
      }
524
472
      break;      
525
473
    case 130:                   /* --userid */
 
474
      uid = (uid_t)strtol(arg, NULL, 10);
 
475
      break;
526
476
    case 131:                   /* --groupid */
 
477
      gid = (gid_t)strtol(arg, NULL, 10);
 
478
      break;
527
479
    case 132:                   /* --debug */
 
480
      debug = true;
 
481
      break;
528
482
    case ARGP_KEY_ARG:
 
483
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
484
      break;
529
485
    case ARGP_KEY_END:
530
486
      break;
531
487
    default:
534
490
    return 0;
535
491
  }
536
492
  
537
 
  struct argp argp = { .options = options,
538
 
                       .parser = parse_opt_config_file,
 
493
  struct argp argp = { .options = options, .parser = parse_opt,
539
494
                       .args_doc = "",
540
495
                       .doc = "Mandos plugin runner -- Run plugins" };
541
496
  
542
 
  /* Parse using parse_opt_config_file() in order to get the custom
543
 
     config file location, if any. */
544
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if(ret == ARGP_ERR_UNKNOWN){
546
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
 
    exitstatus = EXIT_FAILURE;
548
 
    goto fallback;
549
 
  }
550
 
  
551
 
  /* Reset to the normal argument parser */
552
 
  argp.parser = parse_opt;
553
 
  
554
497
  /* Open the configfile if available */
555
 
  if(argfile == NULL){
 
498
  if (argfile == NULL){
556
499
    conffp = fopen(AFILE, "r");
557
500
  } else {
558
501
    conffp = fopen(argfile, "r");
561
504
    char *org_line = NULL;
562
505
    char *p, *arg, *new_arg, *line;
563
506
    size_t size = 0;
 
507
    ssize_t sret;
564
508
    const char whitespace_delims[] = " \r\t\f\v\n";
565
509
    const char comment_delim[] = "#";
566
510
 
613
557
  } else {
614
558
    /* Check for harmful errors and go to fallback. Other errors might
615
559
       not affect opening plugins */
616
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
560
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
561
      perror("fopen");
618
562
      exitstatus = EXIT_FAILURE;
619
563
      goto fallback;
622
566
  /* If there was any arguments from configuration file,
623
567
     pass them to parser as command arguments */
624
568
  if(custom_argv != NULL){
625
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
626
 
                     0, NULL);
627
 
    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){
628
572
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
573
      exitstatus = EXIT_FAILURE;
630
574
      goto fallback;
633
577
  
634
578
  /* Parse actual command line arguments, to let them override the
635
579
     config file */
636
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if(ret == ARGP_ERR_UNKNOWN){
 
580
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
581
  if (ret == ARGP_ERR_UNKNOWN){
638
582
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
583
    exitstatus = EXIT_FAILURE;
640
584
    goto fallback;
656
600
  
657
601
  /* Strip permissions down to nobody */
658
602
  ret = setuid(uid);
659
 
  if(ret == -1){
 
603
  if (ret == -1){
660
604
    perror("setuid");
661
605
  }  
662
606
  setgid(gid);
663
 
  if(ret == -1){
 
607
  if (ret == -1){
664
608
    perror("setgid");
665
609
  }
666
610
  
667
 
  if(plugindir == NULL){
 
611
  if (plugindir == NULL){
668
612
    dir = opendir(PDIR);
669
613
  } else {
670
614
    dir = opendir(plugindir);
697
641
    
698
642
    /* All directory entries have been processed */
699
643
    if(dirst == NULL){
700
 
      if(errno == EBADF){
 
644
      if (errno == EBADF){
701
645
        perror("readdir");
702
646
        exitstatus = EXIT_FAILURE;
703
647
        goto fallback;
715
659
      
716
660
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
717
661
                                           ".dpkg-old",
718
 
                                           ".dpkg-bak",
719
662
                                           ".dpkg-divert", NULL };
720
663
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
721
664
        size_t pre_len = strlen(*pre);
752
695
    }
753
696
 
754
697
    char *filename;
755
 
    if(plugindir == NULL){
756
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
757
 
    } else {
758
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
759
 
    }
 
698
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
760
699
    if(ret < 0){
761
700
      perror("asprintf");
762
701
      continue;
763
702
    }
764
703
    
765
704
    ret = stat(filename, &st);
766
 
    if(ret == -1){
 
705
    if (ret == -1){
767
706
      perror("stat");
768
707
      free(filename);
769
708
      continue;
770
709
    }
771
710
 
772
711
    /* Ignore non-executable files */
773
 
    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)){
774
713
      if(debug){
775
714
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
715
                " with bad type or mode\n", filename);
823
762
    
824
763
    int pipefd[2];
825
764
    ret = pipe(pipefd);
826
 
    if(ret == -1){
 
765
    if (ret == -1){
827
766
      perror("pipe");
828
767
      exitstatus = EXIT_FAILURE;
829
768
      goto fallback;
842
781
      goto fallback;
843
782
    }
844
783
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
784
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
785
    if(ret < 0){
847
786
      perror("sigprocmask");
848
787
      exitstatus = EXIT_FAILURE;
862
801
        perror("sigaction");
863
802
        _exit(EXIT_FAILURE);
864
803
      }
865
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
804
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
866
805
      if(ret < 0){
867
806
        perror("sigprocmask");
868
807
        _exit(EXIT_FAILURE);
869
808
      }
870
 
      
 
809
 
871
810
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
872
811
      if(ret == -1){
873
812
        perror("dup2");
896
835
    close(pipefd[1]);           /* Close unused write end of pipe */
897
836
    free(filename);
898
837
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if(new_plugin == NULL){
 
838
    if (new_plugin == NULL){
900
839
      perror("getplugin");
901
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
840
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
841
      if(ret < 0){
903
842
        perror("sigprocmask");
904
843
      }
911
850
    
912
851
    /* Unblock SIGCHLD so signal handler can be run if this process
913
852
       has already completed */
914
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
853
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
854
    if(ret < 0){
916
855
      perror("sigprocmask");
917
856
      exitstatus = EXIT_FAILURE;
920
859
    
921
860
    FD_SET(new_plugin->fd, &rfds_all);
922
861
    
923
 
    if(maxfd < new_plugin->fd){
 
862
    if (maxfd < new_plugin->fd){
924
863
      maxfd = new_plugin->fd;
925
864
    }
 
865
    
926
866
  }
927
867
  
928
868
  closedir(dir);
929
869
  dir = NULL;
930
 
  
 
870
 
931
871
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
872
    if(p->pid != 0){
933
873
      break;
938
878
      free_plugin_list();
939
879
    }
940
880
  }
941
 
  
 
881
 
942
882
  /* Main loop while running plugins exist */
943
883
  while(plugin_list){
944
884
    fd_set rfds = rfds_all;
945
885
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if(select_ret == -1){
 
886
    if (select_ret == -1){
947
887
      perror("select");
948
888
      exitstatus = EXIT_FAILURE;
949
889
      goto fallback;
950
890
    }
951
891
    /* OK, now either a process completed, or something can be read
952
892
       from one of them */
953
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
893
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
954
894
      /* Is this process completely done? */
955
895
      if(proc->eof and proc->completed){
956
896
        /* Only accept the plugin output if it exited cleanly */
968
908
                      (unsigned int) (proc->pid),
969
909
                      WTERMSIG(proc->status));
970
910
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
 
911
              fprintf(stderr, "Plugin %d dumped core\n",
972
912
                      (unsigned int) (proc->pid));
973
913
            }
974
914
          }
983
923
            exitstatus = EXIT_FAILURE;
984
924
            goto fallback;
985
925
          }
986
 
          
987
 
          plugin *next_plugin = proc->next;
988
926
          free_plugin(proc);
989
 
          proc = next_plugin;
990
 
          
991
927
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                            NULL);
 
928
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
929
                             NULL);
994
930
          if(ret < 0){
995
931
            perror("sigprocmask");
996
932
            exitstatus = EXIT_FAILURE;
1000
936
          if(plugin_list == NULL){
1001
937
            break;
1002
938
          }
1003
 
          
1004
939
          continue;
1005
940
        }
1006
941
        
1007
942
        /* This process exited nicely, so print its buffer */
1008
 
        
 
943
 
1009
944
        bool bret = print_out_password(proc->buffer,
1010
945
                                       proc->buffer_length);
1011
946
        if(not bret){
1018
953
      /* This process has not completed.  Does it have any output? */
1019
954
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1020
955
        /* This process had nothing to say at this time */
1021
 
        proc = proc->next;
1022
956
        continue;
1023
957
      }
1024
958
      /* Before reading, make the process' data buffer large enough */
1025
959
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
960
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
961
                               + (size_t) BUFFER_SIZE);
1028
 
        if(proc->buffer == NULL){
 
962
        if (proc->buffer == NULL){
1029
963
          perror("malloc");
1030
964
          exitstatus = EXIT_FAILURE;
1031
965
          goto fallback;
1033
967
        proc->buffer_size += BUFFER_SIZE;
1034
968
      }
1035
969
      /* Read from the process */
1036
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1037
 
                  BUFFER_SIZE);
1038
 
      if(sret < 0){
 
970
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
971
                 BUFFER_SIZE);
 
972
      if(ret < 0){
1039
973
        /* Read error from this process; ignore the error */
1040
 
        proc = proc->next;
1041
974
        continue;
1042
975
      }
1043
 
      if(sret == 0){
 
976
      if(ret == 0){
1044
977
        /* got EOF */
1045
978
        proc->eof = true;
1046
979
      } else {
1047
 
        proc->buffer_length += (size_t) sret;
 
980
        proc->buffer_length += (size_t) ret;
1048
981
      }
1049
982
    }
1050
983
  }
1058
991
    bool bret;
1059
992
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1060
993
    char *passwordbuffer = getpass("Password: ");
1061
 
    size_t len = strlen(passwordbuffer);
1062
 
    /* Strip trailing newline */
1063
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1064
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1065
 
      len--;
1066
 
    }
1067
 
    bret = print_out_password(passwordbuffer, len);
 
994
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1068
995
    if(not bret){
1069
996
      perror("print_out_password");
1070
997
      exitstatus = EXIT_FAILURE;
1077
1004
    perror("sigaction");
1078
1005
    exitstatus = EXIT_FAILURE;
1079
1006
  }
1080
 
  
 
1007
 
1081
1008
  if(custom_argv != NULL){
1082
1009
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1083
1010
      free(*arg);
1089
1016
    closedir(dir);
1090
1017
  }
1091
1018
  
1092
 
  /* Kill the processes */
 
1019
  /* Free the process list and kill the processes */
1093
1020
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1094
1021
    if(p->pid != 0){
1095
1022
      close(p->fd);
1108
1035
  if(errno != ECHILD){
1109
1036
    perror("wait");
1110
1037
  }
1111
 
  
 
1038
 
1112
1039
  free_plugin_list();
1113
1040
  
1114
1041
  free(plugindir);