/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

* Makefile (install-client-nokey): Move "initramfs-tools-script" from
                                   "/scripts/local-top/mandos" to
                                   "/scripts/init-premount/mandos".
  (uninstall-client): - '' -
* debian/mandos-client.dirs: - '' -
* initramfs-tools-script (PREREQ): Added "udev".

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