/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;
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
 
  
 
121
 
123
122
  *new_plugin = (plugin) { .name = copy_name,
124
123
                           .argc = 1,
125
124
                           .disabled = false,
126
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){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
226
226
  while(true){
227
227
    plugin *proc = plugin_list;
228
228
    int status;
232
232
      break;
233
233
    }
234
234
    if(pid == -1){
235
 
      if(errno != ECHILD){
 
235
      if (errno != ECHILD){
236
236
        perror("waitpid");
237
237
      }
238
238
      /* No child processes */
239
239
      break;
240
240
    }
241
 
    
 
241
 
242
242
    /* A child exited, find it in process_list */
243
243
    while(proc != NULL and proc->pid != pid){
244
244
      proc = proc->next;
253
253
}
254
254
 
255
255
/* Prints out a password to stdout */
256
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
257
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
258
261
  for(size_t written = 0; written < length; written += (size_t)ret){
259
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
260
263
                                   length - written));
310
313
  struct dirent *dirst;
311
314
  struct stat st;
312
315
  fd_set rfds_all;
313
 
  int ret, numchars, maxfd = 0;
314
 
  ssize_t sret;
315
 
  intmax_t tmpmax;
 
316
  int ret, maxfd = 0;
316
317
  uid_t uid = 65534;
317
318
  gid_t gid = 65534;
318
319
  bool debug = false;
343
344
    { .name = "global-options", .key = 'g',
344
345
      .arg = "OPTION[,OPTION[,...]]",
345
346
      .doc = "Options passed to all plugins" },
346
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
347
348
      .arg = "VAR=value",
348
349
      .doc = "Environment variable passed to all plugins" },
349
350
    { .name = "options-for", .key = 'o',
350
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
351
352
      .doc = "Options passed only to specified plugin" },
352
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
353
354
      .arg = "PLUGIN:ENV=value",
354
355
      .doc = "Environment variable passed to specified plugin" },
355
356
    { .name = "disable", .key = 'd',
356
357
      .arg = "PLUGIN",
357
358
      .doc = "Disable a specific plugin", .group = 1 },
358
 
    { .name = "enable", .key = 'e',
359
 
      .arg = "PLUGIN",
360
 
      .doc = "Enable a specific plugin", .group = 1 },
361
359
    { .name = "plugin-dir", .key = 128,
362
360
      .arg = "DIRECTORY",
363
361
      .doc = "Specify a different plugin directory", .group = 2 },
375
373
    { .name = NULL }
376
374
  };
377
375
  
378
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
379
 
                    struct argp_state *state) {
380
 
    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) {
381
381
    case 'g':                   /* --global-options */
382
 
      if(arg != NULL){
 
382
      if (arg != NULL){
383
383
        char *p;
384
384
        while((p = strsep(&arg, ",")) != NULL){
385
385
          if(p[0] == '\0'){
392
392
        }
393
393
      }
394
394
      break;
395
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
396
396
      if(arg == NULL){
397
397
        break;
398
398
      }
399
 
      if(not add_environment(getplugin(NULL), arg, true)){
400
 
        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
        }
401
407
      }
402
408
      break;
403
409
    case 'o':                   /* --options-for */
404
 
      if(arg != NULL){
 
410
      if (arg != NULL){
405
411
        char *p_name = strsep(&arg, ":");
406
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
407
413
          break;
408
414
        }
409
415
        char *opt = strsep(&arg, ":");
410
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
411
417
          break;
412
418
        }
413
 
        char *p;
414
 
        while((p = strsep(&opt, ",")) != NULL){
415
 
          if(p[0] == '\0'){
416
 
            continue;
417
 
          }
418
 
          if(not add_argument(getplugin(p_name), p)){
419
 
            perror("add_argument");
420
 
            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
            }
421
429
          }
422
430
        }
423
431
      }
424
432
      break;
425
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
426
434
      if(arg == NULL){
427
435
        break;
428
436
      }
431
439
        if(envdef == NULL){
432
440
          break;
433
441
        }
434
 
        *envdef = '\0';
435
 
        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)){
436
448
          perror("add_environment");
437
449
        }
438
450
      }
439
451
      break;
440
452
    case 'd':                   /* --disable */
441
 
      if(arg != NULL){
 
453
      if (arg != NULL){
442
454
        plugin *p = getplugin(arg);
443
455
        if(p == NULL){
444
456
          return ARGP_ERR_UNKNOWN;
446
458
        p->disabled = true;
447
459
      }
448
460
      break;
449
 
    case 'e':                   /* --enable */
450
 
      if(arg != NULL){
451
 
        plugin *p = getplugin(arg);
452
 
        if(p == NULL){
453
 
          return ARGP_ERR_UNKNOWN;
454
 
        }
455
 
        p->disabled = false;
456
 
      }
457
 
      break;
458
461
    case 128:                   /* --plugin-dir */
459
 
      free(plugindir);
460
462
      plugindir = strdup(arg);
461
463
      if(plugindir == NULL){
462
464
        perror("strdup");
463
465
      }      
464
466
      break;
465
467
    case 129:                   /* --config-file */
466
 
      /* This is already done by parse_opt_config_file() */
467
 
      break;
468
 
    case 130:                   /* --userid */
469
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
470
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
471
 
         or arg[numchars] != '\0'){
472
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
473
 
                PRIdMAX "\n", arg, (intmax_t)uid);
474
 
      } else {
475
 
        uid = (uid_t)tmpmax;
476
 
      }
477
 
      break;
478
 
    case 131:                   /* --groupid */
479
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
480
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
481
 
         or arg[numchars] != '\0'){
482
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
483
 
                PRIdMAX "\n", arg, (intmax_t)gid);
484
 
      } else {
485
 
        gid = (gid_t)tmpmax;
486
 
      }
487
 
      break;
488
 
    case 132:                   /* --debug */
489
 
      debug = true;
490
 
      break;
491
 
/*
492
 
 * When adding more options before this line, remember to also add a
493
 
 * "case" to the "parse_opt_config_file" function below.
494
 
 */
495
 
    case ARGP_KEY_ARG:
496
 
      /* Cryptsetup always passes an argument, which is an empty
497
 
         string if "none" was specified in /etc/crypttab.  So if
498
 
         argument was empty, we ignore it silently. */
499
 
      if(arg[0] != '\0'){
500
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
501
 
      }
502
 
      break;
503
 
    case ARGP_KEY_END:
504
 
      break;
505
 
    default:
506
 
      return ARGP_ERR_UNKNOWN;
507
 
    }
508
 
    return 0;
509
 
  }
510
 
  
511
 
  /* This option parser is the same as parse_opt() above, except it
512
 
     ignores everything but the --config-file option. */
513
 
  error_t parse_opt_config_file(int key, char *arg,
514
 
                                __attribute__((unused))
515
 
                                struct argp_state *state) {
516
 
    switch(key) {
517
 
    case 'g':                   /* --global-options */
518
 
    case 'G':                   /* --global-env */
519
 
    case 'o':                   /* --options-for */
520
 
    case 'E':                   /* --env-for */
521
 
    case 'd':                   /* --disable */
522
 
    case 'e':                   /* --enable */
523
 
    case 128:                   /* --plugin-dir */
524
 
      break;
525
 
    case 129:                   /* --config-file */
526
 
      free(argfile);
527
468
      argfile = strdup(arg);
528
469
      if(argfile == NULL){
529
470
        perror("strdup");
530
471
      }
531
472
      break;      
532
473
    case 130:                   /* --userid */
 
474
      uid = (uid_t)strtol(arg, NULL, 10);
 
475
      break;
533
476
    case 131:                   /* --groupid */
 
477
      gid = (gid_t)strtol(arg, NULL, 10);
 
478
      break;
534
479
    case 132:                   /* --debug */
 
480
      debug = true;
 
481
      break;
535
482
    case ARGP_KEY_ARG:
 
483
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
484
      break;
536
485
    case ARGP_KEY_END:
537
486
      break;
538
487
    default:
541
490
    return 0;
542
491
  }
543
492
  
544
 
  struct argp argp = { .options = options,
545
 
                       .parser = parse_opt_config_file,
 
493
  struct argp argp = { .options = options, .parser = parse_opt,
546
494
                       .args_doc = "",
547
495
                       .doc = "Mandos plugin runner -- Run plugins" };
548
496
  
549
 
  /* Parse using parse_opt_config_file() in order to get the custom
550
 
     config file location, if any. */
551
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
552
 
  if(ret == ARGP_ERR_UNKNOWN){
553
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
554
 
    exitstatus = EXIT_FAILURE;
555
 
    goto fallback;
556
 
  }
557
 
  
558
 
  /* Reset to the normal argument parser */
559
 
  argp.parser = parse_opt;
560
 
  
561
497
  /* Open the configfile if available */
562
 
  if(argfile == NULL){
 
498
  if (argfile == NULL){
563
499
    conffp = fopen(AFILE, "r");
564
500
  } else {
565
501
    conffp = fopen(argfile, "r");
568
504
    char *org_line = NULL;
569
505
    char *p, *arg, *new_arg, *line;
570
506
    size_t size = 0;
 
507
    ssize_t sret;
571
508
    const char whitespace_delims[] = " \r\t\f\v\n";
572
509
    const char comment_delim[] = "#";
573
510
 
620
557
  } else {
621
558
    /* Check for harmful errors and go to fallback. Other errors might
622
559
       not affect opening plugins */
623
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
560
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
624
561
      perror("fopen");
625
562
      exitstatus = EXIT_FAILURE;
626
563
      goto fallback;
629
566
  /* If there was any arguments from configuration file,
630
567
     pass them to parser as command arguments */
631
568
  if(custom_argv != NULL){
632
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
633
 
                     0, NULL);
634
 
    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){
635
572
      fprintf(stderr, "Unknown error while parsing arguments\n");
636
573
      exitstatus = EXIT_FAILURE;
637
574
      goto fallback;
640
577
  
641
578
  /* Parse actual command line arguments, to let them override the
642
579
     config file */
643
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
644
 
  if(ret == ARGP_ERR_UNKNOWN){
 
580
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
581
  if (ret == ARGP_ERR_UNKNOWN){
645
582
    fprintf(stderr, "Unknown error while parsing arguments\n");
646
583
    exitstatus = EXIT_FAILURE;
647
584
    goto fallback;
654
591
      for(char **a = p->argv; *a != NULL; a++){
655
592
        fprintf(stderr, "\tArg: %s\n", *a);
656
593
      }
657
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
594
      fprintf(stderr, "...and %u environment variables\n", p->envc);
658
595
      for(char **a = p->environ; *a != NULL; a++){
659
596
        fprintf(stderr, "\t%s\n", *a);
660
597
      }
663
600
  
664
601
  /* Strip permissions down to nobody */
665
602
  ret = setuid(uid);
666
 
  if(ret == -1){
 
603
  if (ret == -1){
667
604
    perror("setuid");
668
605
  }  
669
606
  setgid(gid);
670
 
  if(ret == -1){
 
607
  if (ret == -1){
671
608
    perror("setgid");
672
609
  }
673
610
  
674
 
  if(plugindir == NULL){
 
611
  if (plugindir == NULL){
675
612
    dir = opendir(PDIR);
676
613
  } else {
677
614
    dir = opendir(plugindir);
704
641
    
705
642
    /* All directory entries have been processed */
706
643
    if(dirst == NULL){
707
 
      if(errno == EBADF){
 
644
      if (errno == EBADF){
708
645
        perror("readdir");
709
646
        exitstatus = EXIT_FAILURE;
710
647
        goto fallback;
722
659
      
723
660
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
724
661
                                           ".dpkg-old",
725
 
                                           ".dpkg-bak",
726
662
                                           ".dpkg-divert", NULL };
727
663
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
728
664
        size_t pre_len = strlen(*pre);
759
695
    }
760
696
 
761
697
    char *filename;
762
 
    if(plugindir == NULL){
763
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
764
 
    } else {
765
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
766
 
    }
 
698
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
767
699
    if(ret < 0){
768
700
      perror("asprintf");
769
701
      continue;
770
702
    }
771
703
    
772
704
    ret = stat(filename, &st);
773
 
    if(ret == -1){
 
705
    if (ret == -1){
774
706
      perror("stat");
775
707
      free(filename);
776
708
      continue;
777
709
    }
778
710
 
779
711
    /* Ignore non-executable files */
780
 
    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)){
781
713
      if(debug){
782
714
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
783
715
                " with bad type or mode\n", filename);
830
762
    
831
763
    int pipefd[2];
832
764
    ret = pipe(pipefd);
833
 
    if(ret == -1){
 
765
    if (ret == -1){
834
766
      perror("pipe");
835
767
      exitstatus = EXIT_FAILURE;
836
768
      goto fallback;
849
781
      goto fallback;
850
782
    }
851
783
    /* Block SIGCHLD until process is safely in process list */
852
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
784
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
853
785
    if(ret < 0){
854
786
      perror("sigprocmask");
855
787
      exitstatus = EXIT_FAILURE;
869
801
        perror("sigaction");
870
802
        _exit(EXIT_FAILURE);
871
803
      }
872
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
804
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
873
805
      if(ret < 0){
874
806
        perror("sigprocmask");
875
807
        _exit(EXIT_FAILURE);
876
808
      }
877
 
      
 
809
 
878
810
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
879
811
      if(ret == -1){
880
812
        perror("dup2");
903
835
    close(pipefd[1]);           /* Close unused write end of pipe */
904
836
    free(filename);
905
837
    plugin *new_plugin = getplugin(dirst->d_name);
906
 
    if(new_plugin == NULL){
 
838
    if (new_plugin == NULL){
907
839
      perror("getplugin");
908
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
840
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
909
841
      if(ret < 0){
910
842
        perror("sigprocmask");
911
843
      }
918
850
    
919
851
    /* Unblock SIGCHLD so signal handler can be run if this process
920
852
       has already completed */
921
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
853
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
922
854
    if(ret < 0){
923
855
      perror("sigprocmask");
924
856
      exitstatus = EXIT_FAILURE;
927
859
    
928
860
    FD_SET(new_plugin->fd, &rfds_all);
929
861
    
930
 
    if(maxfd < new_plugin->fd){
 
862
    if (maxfd < new_plugin->fd){
931
863
      maxfd = new_plugin->fd;
932
864
    }
 
865
    
933
866
  }
934
867
  
935
868
  closedir(dir);
936
869
  dir = NULL;
937
 
  
 
870
 
938
871
  for(plugin *p = plugin_list; p != NULL; p = p->next){
939
872
    if(p->pid != 0){
940
873
      break;
945
878
      free_plugin_list();
946
879
    }
947
880
  }
948
 
  
 
881
 
949
882
  /* Main loop while running plugins exist */
950
883
  while(plugin_list){
951
884
    fd_set rfds = rfds_all;
952
885
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
953
 
    if(select_ret == -1){
 
886
    if (select_ret == -1){
954
887
      perror("select");
955
888
      exitstatus = EXIT_FAILURE;
956
889
      goto fallback;
957
890
    }
958
891
    /* OK, now either a process completed, or something can be read
959
892
       from one of them */
960
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
893
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
961
894
      /* Is this process completely done? */
962
895
      if(proc->eof and proc->completed){
963
896
        /* Only accept the plugin output if it exited cleanly */
967
900
 
968
901
          if(debug){
969
902
            if(WIFEXITED(proc->status)){
970
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
971
 
                      " %d\n", (intmax_t) (proc->pid),
 
903
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
904
                      (unsigned int) (proc->pid),
972
905
                      WEXITSTATUS(proc->status));
973
906
            } else if(WIFSIGNALED(proc->status)) {
974
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
975
 
                      " %d\n", (intmax_t) (proc->pid),
 
907
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
908
                      (unsigned int) (proc->pid),
976
909
                      WTERMSIG(proc->status));
977
910
            } else if(WCOREDUMP(proc->status)){
978
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
979
 
                      (intmax_t) (proc->pid));
 
911
              fprintf(stderr, "Plugin %d dumped core\n",
 
912
                      (unsigned int) (proc->pid));
980
913
            }
981
914
          }
982
915
          
990
923
            exitstatus = EXIT_FAILURE;
991
924
            goto fallback;
992
925
          }
993
 
          
994
 
          plugin *next_plugin = proc->next;
995
926
          free_plugin(proc);
996
 
          proc = next_plugin;
997
 
          
998
927
          /* We are done modifying process list, so unblock signal */
999
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1000
 
                            NULL);
 
928
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
929
                             NULL);
1001
930
          if(ret < 0){
1002
931
            perror("sigprocmask");
1003
932
            exitstatus = EXIT_FAILURE;
1007
936
          if(plugin_list == NULL){
1008
937
            break;
1009
938
          }
1010
 
          
1011
939
          continue;
1012
940
        }
1013
941
        
1014
942
        /* This process exited nicely, so print its buffer */
1015
 
        
 
943
 
1016
944
        bool bret = print_out_password(proc->buffer,
1017
945
                                       proc->buffer_length);
1018
946
        if(not bret){
1025
953
      /* This process has not completed.  Does it have any output? */
1026
954
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1027
955
        /* This process had nothing to say at this time */
1028
 
        proc = proc->next;
1029
956
        continue;
1030
957
      }
1031
958
      /* Before reading, make the process' data buffer large enough */
1032
959
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1033
960
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1034
961
                               + (size_t) BUFFER_SIZE);
1035
 
        if(proc->buffer == NULL){
 
962
        if (proc->buffer == NULL){
1036
963
          perror("malloc");
1037
964
          exitstatus = EXIT_FAILURE;
1038
965
          goto fallback;
1040
967
        proc->buffer_size += BUFFER_SIZE;
1041
968
      }
1042
969
      /* Read from the process */
1043
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1044
 
                  BUFFER_SIZE);
1045
 
      if(sret < 0){
 
970
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
971
                 BUFFER_SIZE);
 
972
      if(ret < 0){
1046
973
        /* Read error from this process; ignore the error */
1047
 
        proc = proc->next;
1048
974
        continue;
1049
975
      }
1050
 
      if(sret == 0){
 
976
      if(ret == 0){
1051
977
        /* got EOF */
1052
978
        proc->eof = true;
1053
979
      } else {
1054
 
        proc->buffer_length += (size_t) sret;
 
980
        proc->buffer_length += (size_t) ret;
1055
981
      }
1056
982
    }
1057
983
  }
1065
991
    bool bret;
1066
992
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1067
993
    char *passwordbuffer = getpass("Password: ");
1068
 
    size_t len = strlen(passwordbuffer);
1069
 
    /* Strip trailing newline */
1070
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1071
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1072
 
      len--;
1073
 
    }
1074
 
    bret = print_out_password(passwordbuffer, len);
 
994
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1075
995
    if(not bret){
1076
996
      perror("print_out_password");
1077
997
      exitstatus = EXIT_FAILURE;
1084
1004
    perror("sigaction");
1085
1005
    exitstatus = EXIT_FAILURE;
1086
1006
  }
1087
 
  
 
1007
 
1088
1008
  if(custom_argv != NULL){
1089
1009
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1090
1010
      free(*arg);
1096
1016
    closedir(dir);
1097
1017
  }
1098
1018
  
1099
 
  /* Kill the processes */
 
1019
  /* Free the process list and kill the processes */
1100
1020
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1101
1021
    if(p->pid != 0){
1102
1022
      close(p->fd);
1115
1035
  if(errno != ECHILD){
1116
1036
    perror("wait");
1117
1037
  }
1118
 
  
 
1038
 
1119
1039
  free_plugin_list();
1120
1040
  
1121
1041
  free(plugindir);