/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-21 12:04:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080921120402-mgd2jl8xo634jw18
* Makefile: Put the init script before avahi-daemon.

* debian/mandos.prerm: Bug fix: stop mandos, not ssh.

* debian/rules (install-indep): Put the init script before
                                avahi-daemon.

* init.d/mandos: Require "avahi-daemon".

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,
62
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
62
                                   sigaddset(), sigaction(),
64
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill(), sig_atomic_t
66
 
                                */
 
64
                                   SIG_UNBLOCK, kill() */
67
65
#include <errno.h>              /* errno, EBADF */
68
 
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
69
66
 
70
67
#define BUFFER_SIZE 256
71
68
 
72
69
#define PDIR "/lib/mandos/plugins.d"
73
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
74
71
 
75
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
76
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
77
74
 
78
75
typedef struct plugin{
82
79
  char **environ;
83
80
  int envc;
84
81
  bool disabled;
85
 
  
 
82
 
86
83
  /* Variables used for running processes*/
87
84
  pid_t pid;
88
85
  int fd;
90
87
  size_t buffer_size;
91
88
  size_t buffer_length;
92
89
  bool eof;
93
 
  volatile sig_atomic_t completed;
94
 
  int status;
 
90
  volatile bool completed;
 
91
  volatile int status;
95
92
  struct plugin *next;
96
93
} plugin;
97
94
 
101
98
   or if none is found, creates a new one */
102
99
static plugin *getplugin(char *name){
103
100
  /* Check for exiting plugin with that name */
104
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
105
 
    if((p->name == name)
106
 
       or (p->name and name and (strcmp(p->name, name) == 0))){
 
101
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
102
    if ((p->name == name)
 
103
        or (p->name and name and (strcmp(p->name, name) == 0))){
107
104
      return p;
108
105
    }
109
106
  }
110
107
  /* Create a new plugin */
111
108
  plugin *new_plugin = malloc(sizeof(plugin));
112
 
  if(new_plugin == NULL){
 
109
  if (new_plugin == NULL){
113
110
    return NULL;
114
111
  }
115
112
  char *copy_name = NULL;
116
113
  if(name != NULL){
117
114
    copy_name = strdup(name);
118
115
    if(copy_name == NULL){
119
 
      free(new_plugin);
120
116
      return NULL;
121
117
    }
122
118
  }
123
119
  
124
 
  *new_plugin = (plugin){ .name = copy_name,
125
 
                          .argc = 1,
126
 
                          .disabled = false,
127
 
                          .next = plugin_list };
 
120
  *new_plugin = (plugin) { .name = copy_name,
 
121
                           .argc = 1,
 
122
                           .disabled = false,
 
123
                           .next = plugin_list };
128
124
  
129
125
  new_plugin->argv = malloc(sizeof(char *) * 2);
130
 
  if(new_plugin->argv == NULL){
 
126
  if (new_plugin->argv == NULL){
131
127
    free(copy_name);
132
128
    free(new_plugin);
133
129
    return NULL;
224
220
/* Mark processes as completed when they exit, and save their exit
225
221
   status. */
226
222
static void handle_sigchld(__attribute__((unused)) int sig){
227
 
  int old_errno = errno;
228
223
  while(true){
229
224
    plugin *proc = plugin_list;
230
225
    int status;
234
229
      break;
235
230
    }
236
231
    if(pid == -1){
237
 
      if(errno == ECHILD){
238
 
        /* No child processes */
239
 
        break;
 
232
      if (errno != ECHILD){
 
233
        perror("waitpid");
240
234
      }
241
 
      perror("waitpid");
 
235
      /* No child processes */
 
236
      break;
242
237
    }
243
238
    
244
239
    /* A child exited, find it in process_list */
250
245
      continue;
251
246
    }
252
247
    proc->status = status;
253
 
    proc->completed = 1;
 
248
    proc->completed = true;
254
249
  }
255
 
  errno = old_errno;
256
250
}
257
251
 
258
252
/* Prints out a password to stdout */
280
274
  }
281
275
  free(plugin_node->environ);
282
276
  free(plugin_node->buffer);
283
 
  
 
277
 
284
278
  /* Removes the plugin from the singly-linked list */
285
279
  if(plugin_node == plugin_list){
286
280
    /* First one - simple */
314
308
  struct stat st;
315
309
  fd_set rfds_all;
316
310
  int ret, maxfd = 0;
317
 
  ssize_t sret;
318
 
  intmax_t tmpmax;
319
311
  uid_t uid = 65534;
320
312
  gid_t gid = 65534;
321
313
  bool debug = false;
378
370
    { .name = NULL }
379
371
  };
380
372
  
381
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
382
 
                    struct argp_state *state){
383
 
    char *tmp;
384
 
    switch(key){
 
373
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
374
                     struct argp_state *state) {
 
375
    switch (key) {
385
376
    case 'g':                   /* --global-options */
386
 
      if(arg != NULL){
387
 
        char *plugin_option;
388
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
389
 
          if(plugin_option[0] == '\0'){
 
377
      if (arg != NULL){
 
378
        char *p;
 
379
        while((p = strsep(&arg, ",")) != NULL){
 
380
          if(p[0] == '\0'){
390
381
            continue;
391
382
          }
392
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
383
          if(not add_argument(getplugin(NULL), p)){
393
384
            perror("add_argument");
394
385
            return ARGP_ERR_UNKNOWN;
395
386
          }
405
396
      }
406
397
      break;
407
398
    case 'o':                   /* --options-for */
408
 
      if(arg != NULL){
409
 
        char *plugin_name = strsep(&arg, ":");
410
 
        if(plugin_name[0] == '\0'){
411
 
          break;
412
 
        }
413
 
        char *plugin_option;
414
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
415
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
399
      if (arg != NULL){
 
400
        char *p_name = strsep(&arg, ":");
 
401
        if(p_name[0] == '\0' or arg == NULL){
 
402
          break;
 
403
        }
 
404
        char *opt = strsep(&arg, ":");
 
405
        if(opt[0] == '\0' or opt == NULL){
 
406
          break;
 
407
        }
 
408
        char *p;
 
409
        while((p = strsep(&opt, ",")) != NULL){
 
410
          if(p[0] == '\0'){
 
411
            continue;
 
412
          }
 
413
          if(not add_argument(getplugin(p_name), p)){
416
414
            perror("add_argument");
417
415
            return ARGP_ERR_UNKNOWN;
418
416
          }
435
433
      }
436
434
      break;
437
435
    case 'd':                   /* --disable */
438
 
      if(arg != NULL){
 
436
      if (arg != NULL){
439
437
        plugin *p = getplugin(arg);
440
438
        if(p == NULL){
441
439
          return ARGP_ERR_UNKNOWN;
444
442
      }
445
443
      break;
446
444
    case 'e':                   /* --enable */
447
 
      if(arg != NULL){
 
445
      if (arg != NULL){
448
446
        plugin *p = getplugin(arg);
449
447
        if(p == NULL){
450
448
          return ARGP_ERR_UNKNOWN;
463
461
      /* This is already done by parse_opt_config_file() */
464
462
      break;
465
463
    case 130:                   /* --userid */
466
 
      errno = 0;
467
 
      tmpmax = strtoimax(arg, &tmp, 10);
468
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
469
 
         or tmpmax != (uid_t)tmpmax){
470
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
471
 
                PRIdMAX "\n", arg, (intmax_t)uid);
472
 
      } else {
473
 
        uid = (uid_t)tmpmax;
474
 
      }
 
464
      uid = (uid_t)strtol(arg, NULL, 10);
475
465
      break;
476
466
    case 131:                   /* --groupid */
477
 
      errno = 0;
478
 
      tmpmax = strtoimax(arg, &tmp, 10);
479
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
480
 
         or tmpmax != (gid_t)tmpmax){
481
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
482
 
                PRIdMAX "\n", arg, (intmax_t)gid);
483
 
      } else {
484
 
        gid = (gid_t)tmpmax;
485
 
      }
 
467
      gid = (gid_t)strtol(arg, NULL, 10);
486
468
      break;
487
469
    case 132:                   /* --debug */
488
470
      debug = true;
489
471
      break;
490
 
/*
491
 
 * When adding more options before this line, remember to also add a
492
 
 * "case" to the "parse_opt_config_file" function below.
493
 
 */
494
472
    case ARGP_KEY_ARG:
495
473
      /* Cryptsetup always passes an argument, which is an empty
496
474
         string if "none" was specified in /etc/crypttab.  So if
509
487
  
510
488
  /* This option parser is the same as parse_opt() above, except it
511
489
     ignores everything but the --config-file option. */
512
 
  error_t parse_opt_config_file(int key, char *arg,
513
 
                                __attribute__((unused))
514
 
                                struct argp_state *state){
515
 
    switch(key){
 
490
  error_t parse_opt_config_file (int key, char *arg,
 
491
                                 __attribute__((unused))
 
492
                                 struct argp_state *state) {
 
493
    switch (key) {
516
494
    case 'g':                   /* --global-options */
517
495
    case 'G':                   /* --global-env */
518
496
    case 'o':                   /* --options-for */
545
523
                       .args_doc = "",
546
524
                       .doc = "Mandos plugin runner -- Run plugins" };
547
525
  
548
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
526
  /* Parse using the parse_opt_config_file in order to get the custom
549
527
     config file location, if any. */
550
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
551
 
  if(ret == ARGP_ERR_UNKNOWN){
 
528
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
529
  if (ret == ARGP_ERR_UNKNOWN){
552
530
    fprintf(stderr, "Unknown error while parsing arguments\n");
553
531
    exitstatus = EXIT_FAILURE;
554
532
    goto fallback;
558
536
  argp.parser = parse_opt;
559
537
  
560
538
  /* Open the configfile if available */
561
 
  if(argfile == NULL){
 
539
  if (argfile == NULL){
562
540
    conffp = fopen(AFILE, "r");
563
541
  } else {
564
542
    conffp = fopen(argfile, "r");
567
545
    char *org_line = NULL;
568
546
    char *p, *arg, *new_arg, *line;
569
547
    size_t size = 0;
 
548
    ssize_t sret;
570
549
    const char whitespace_delims[] = " \r\t\f\v\n";
571
550
    const char comment_delim[] = "#";
572
 
    
 
551
 
573
552
    custom_argc = 1;
574
553
    custom_argv = malloc(sizeof(char*) * 2);
575
554
    if(custom_argv == NULL){
579
558
    }
580
559
    custom_argv[0] = argv[0];
581
560
    custom_argv[1] = NULL;
582
 
    
 
561
 
583
562
    /* for each line in the config file, strip whitespace and ignore
584
563
       commented text */
585
564
    while(true){
587
566
      if(sret == -1){
588
567
        break;
589
568
      }
590
 
      
 
569
 
591
570
      line = org_line;
592
571
      arg = strsep(&line, comment_delim);
593
572
      while((p = strsep(&arg, whitespace_delims)) != NULL){
619
598
  } else {
620
599
    /* Check for harmful errors and go to fallback. Other errors might
621
600
       not affect opening plugins */
622
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
601
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
623
602
      perror("fopen");
624
603
      exitstatus = EXIT_FAILURE;
625
604
      goto fallback;
628
607
  /* If there was any arguments from configuration file,
629
608
     pass them to parser as command arguments */
630
609
  if(custom_argv != NULL){
631
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
632
 
                     0, NULL);
633
 
    if(ret == ARGP_ERR_UNKNOWN){
 
610
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
611
                      0, NULL);
 
612
    if (ret == ARGP_ERR_UNKNOWN){
634
613
      fprintf(stderr, "Unknown error while parsing arguments\n");
635
614
      exitstatus = EXIT_FAILURE;
636
615
      goto fallback;
639
618
  
640
619
  /* Parse actual command line arguments, to let them override the
641
620
     config file */
642
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
643
 
  if(ret == ARGP_ERR_UNKNOWN){
 
621
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
622
  if (ret == ARGP_ERR_UNKNOWN){
644
623
    fprintf(stderr, "Unknown error while parsing arguments\n");
645
624
    exitstatus = EXIT_FAILURE;
646
625
    goto fallback;
653
632
      for(char **a = p->argv; *a != NULL; a++){
654
633
        fprintf(stderr, "\tArg: %s\n", *a);
655
634
      }
656
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
635
      fprintf(stderr, "...and %u environment variables\n", p->envc);
657
636
      for(char **a = p->environ; *a != NULL; a++){
658
637
        fprintf(stderr, "\t%s\n", *a);
659
638
      }
661
640
  }
662
641
  
663
642
  /* Strip permissions down to nobody */
 
643
  ret = setuid(uid);
 
644
  if (ret == -1){
 
645
    perror("setuid");
 
646
  }  
664
647
  setgid(gid);
665
 
  if(ret == -1){
 
648
  if (ret == -1){
666
649
    perror("setgid");
667
650
  }
668
 
  ret = setuid(uid);
669
 
  if(ret == -1){
670
 
    perror("setuid");
671
 
  }
672
651
  
673
 
  if(plugindir == NULL){
 
652
  if (plugindir == NULL){
674
653
    dir = opendir(PDIR);
675
654
  } else {
676
655
    dir = opendir(plugindir);
703
682
    
704
683
    /* All directory entries have been processed */
705
684
    if(dirst == NULL){
706
 
      if(errno == EBADF){
 
685
      if (errno == EBADF){
707
686
        perror("readdir");
708
687
        exitstatus = EXIT_FAILURE;
709
688
        goto fallback;
721
700
      
722
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
723
702
                                           ".dpkg-old",
724
 
                                           ".dpkg-bak",
725
703
                                           ".dpkg-divert", NULL };
726
704
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
727
705
        size_t pre_len = strlen(*pre);
756
734
        continue;
757
735
      }
758
736
    }
759
 
    
 
737
 
760
738
    char *filename;
761
739
    if(plugindir == NULL){
762
740
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
769
747
    }
770
748
    
771
749
    ret = stat(filename, &st);
772
 
    if(ret == -1){
 
750
    if (ret == -1){
773
751
      perror("stat");
774
752
      free(filename);
775
753
      continue;
776
754
    }
777
 
    
 
755
 
778
756
    /* Ignore non-executable files */
779
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
757
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
780
758
      if(debug){
781
759
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
782
760
                " with bad type or mode\n", filename);
829
807
    
830
808
    int pipefd[2];
831
809
    ret = pipe(pipefd);
832
 
    if(ret == -1){
 
810
    if (ret == -1){
833
811
      perror("pipe");
834
812
      exitstatus = EXIT_FAILURE;
835
813
      goto fallback;
848
826
      goto fallback;
849
827
    }
850
828
    /* Block SIGCHLD until process is safely in process list */
851
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
829
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
852
830
    if(ret < 0){
853
831
      perror("sigprocmask");
854
832
      exitstatus = EXIT_FAILURE;
868
846
        perror("sigaction");
869
847
        _exit(EXIT_FAILURE);
870
848
      }
871
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
849
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
872
850
      if(ret < 0){
873
851
        perror("sigprocmask");
874
852
        _exit(EXIT_FAILURE);
875
853
      }
876
 
      
 
854
 
877
855
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
878
856
      if(ret == -1){
879
857
        perror("dup2");
902
880
    close(pipefd[1]);           /* Close unused write end of pipe */
903
881
    free(filename);
904
882
    plugin *new_plugin = getplugin(dirst->d_name);
905
 
    if(new_plugin == NULL){
 
883
    if (new_plugin == NULL){
906
884
      perror("getplugin");
907
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
885
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
908
886
      if(ret < 0){
909
887
        perror("sigprocmask");
910
888
      }
917
895
    
918
896
    /* Unblock SIGCHLD so signal handler can be run if this process
919
897
       has already completed */
920
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
898
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
921
899
    if(ret < 0){
922
900
      perror("sigprocmask");
923
901
      exitstatus = EXIT_FAILURE;
926
904
    
927
905
    FD_SET(new_plugin->fd, &rfds_all);
928
906
    
929
 
    if(maxfd < new_plugin->fd){
 
907
    if (maxfd < new_plugin->fd){
930
908
      maxfd = new_plugin->fd;
931
909
    }
 
910
    
932
911
  }
933
912
  
934
913
  closedir(dir);
935
914
  dir = NULL;
936
 
  free_plugin(getplugin(NULL));
937
 
  
 
915
 
938
916
  for(plugin *p = plugin_list; p != NULL; p = p->next){
939
917
    if(p->pid != 0){
940
918
      break;
945
923
      free_plugin_list();
946
924
    }
947
925
  }
948
 
  
 
926
 
949
927
  /* Main loop while running plugins exist */
950
928
  while(plugin_list){
951
929
    fd_set rfds = rfds_all;
952
930
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
953
 
    if(select_ret == -1){
 
931
    if (select_ret == -1){
954
932
      perror("select");
955
933
      exitstatus = EXIT_FAILURE;
956
934
      goto fallback;
959
937
       from one of them */
960
938
    for(plugin *proc = plugin_list; proc != NULL;){
961
939
      /* Is this process completely done? */
962
 
      if(proc->completed and proc->eof){
 
940
      if(proc->eof and proc->completed){
963
941
        /* Only accept the plugin output if it exited cleanly */
964
942
        if(not WIFEXITED(proc->status)
965
943
           or WEXITSTATUS(proc->status) != 0){
966
944
          /* Bad exit by plugin */
967
 
          
 
945
 
968
946
          if(debug){
969
947
            if(WIFEXITED(proc->status)){
970
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
971
 
                      " status %d\n", proc->name,
972
 
                      (intmax_t) (proc->pid),
 
948
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
949
                      (unsigned int) (proc->pid),
973
950
                      WEXITSTATUS(proc->status));
974
 
            } else if(WIFSIGNALED(proc->status)){
975
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
976
 
                      " signal %d\n", proc->name,
977
 
                      (intmax_t) (proc->pid),
 
951
            } else if(WIFSIGNALED(proc->status)) {
 
952
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
953
                      (unsigned int) (proc->pid),
978
954
                      WTERMSIG(proc->status));
979
955
            } else if(WCOREDUMP(proc->status)){
980
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
981
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
956
              fprintf(stderr, "Plugin %d dumped core\n",
 
957
                      (unsigned int) (proc->pid));
982
958
            }
983
959
          }
984
960
          
985
961
          /* Remove the plugin */
986
962
          FD_CLR(proc->fd, &rfds_all);
987
 
          
 
963
 
988
964
          /* Block signal while modifying process_list */
989
965
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
990
966
          if(ret < 0){
993
969
            goto fallback;
994
970
          }
995
971
          
 
972
          /* We are done modifying process list, so unblock signal */
 
973
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
974
                             NULL);
 
975
          if(ret < 0){
 
976
            perror("sigprocmask");
 
977
            exitstatus = EXIT_FAILURE;
 
978
            goto fallback;
 
979
          }
 
980
          
 
981
          if(plugin_list == NULL){
 
982
            break;
 
983
          }
 
984
          
996
985
          plugin *next_plugin = proc->next;
997
986
          free_plugin(proc);
998
987
          proc = next_plugin;
999
 
          
1000
 
          /* We are done modifying process list, so unblock signal */
1001
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1002
 
                            NULL);
1003
 
          if(ret < 0){
1004
 
            perror("sigprocmask");
1005
 
            exitstatus = EXIT_FAILURE;
1006
 
            goto fallback;
1007
 
          }
1008
 
          
1009
 
          if(plugin_list == NULL){
1010
 
            break;
1011
 
          }
1012
 
          
1013
988
          continue;
1014
989
        }
1015
990
        
1016
991
        /* This process exited nicely, so print its buffer */
1017
 
        
 
992
 
1018
993
        bool bret = print_out_password(proc->buffer,
1019
994
                                       proc->buffer_length);
1020
995
        if(not bret){
1034
1009
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1035
1010
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1036
1011
                               + (size_t) BUFFER_SIZE);
1037
 
        if(proc->buffer == NULL){
 
1012
        if (proc->buffer == NULL){
1038
1013
          perror("malloc");
1039
1014
          exitstatus = EXIT_FAILURE;
1040
1015
          goto fallback;
1042
1017
        proc->buffer_size += BUFFER_SIZE;
1043
1018
      }
1044
1019
      /* Read from the process */
1045
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1046
 
                  BUFFER_SIZE);
1047
 
      if(sret < 0){
 
1020
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1021
                 BUFFER_SIZE);
 
1022
      if(ret < 0){
1048
1023
        /* Read error from this process; ignore the error */
1049
1024
        proc = proc->next;
1050
1025
        continue;
1051
1026
      }
1052
 
      if(sret == 0){
 
1027
      if(ret == 0){
1053
1028
        /* got EOF */
1054
1029
        proc->eof = true;
1055
1030
      } else {
1056
 
        proc->buffer_length += (size_t) sret;
 
1031
        proc->buffer_length += (size_t) ret;
1057
1032
      }
1058
1033
    }
1059
1034
  }
1060
 
  
1061
 
  
 
1035
 
 
1036
 
1062
1037
 fallback:
1063
1038
  
1064
1039
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1086
1061
    perror("sigaction");
1087
1062
    exitstatus = EXIT_FAILURE;
1088
1063
  }
1089
 
  
 
1064
 
1090
1065
  if(custom_argv != NULL){
1091
1066
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1092
1067
      free(*arg);
1098
1073
    closedir(dir);
1099
1074
  }
1100
1075
  
1101
 
  /* Kill the processes */
 
1076
  /* Free the process list and kill the processes */
1102
1077
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1103
1078
    if(p->pid != 0){
1104
1079
      close(p->fd);
1117
1092
  if(errno != ECHILD){
1118
1093
    perror("wait");
1119
1094
  }
1120
 
  
 
1095
 
1121
1096
  free_plugin_list();
1122
1097
  
1123
1098
  free(plugindir);