/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

  • Committer: Teddy Hogeborn
  • Date: 2008-09-06 17:24:58 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080906172458-2x5wlfkn7oqckt1y
* legalnotice.xml: Copy DocBook 4.4-formatted text from
                   <http://www.gnu.org/licenses/gpl-3.0.dbk>.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
28
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
28
                                   EXIT_SUCCESS, realloc() */
30
29
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
33
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
33
                                   stat, waitpid(), WIFEXITED(),
35
34
                                   WEXITSTATUS(), wait(), pid_t,
47
46
                                   fcntl(), setuid(), setgid(),
48
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
49
48
                                   access(), pipe(), fork(), close()
50
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
51
50
                                   execv(), write(), read(),
52
51
                                   close() */
53
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
64
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
64
                                   SIG_UNBLOCK, kill() */
66
65
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
66
 
69
67
#define BUFFER_SIZE 256
70
68
 
71
69
#define PDIR "/lib/mandos/plugins.d"
72
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
73
71
 
74
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
75
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
76
74
 
 
75
struct plugin;
 
76
 
77
77
typedef struct plugin{
78
78
  char *name;                   /* can be NULL or any plugin name */
79
79
  char **argv;
89
89
  size_t buffer_size;
90
90
  size_t buffer_length;
91
91
  bool eof;
92
 
  volatile sig_atomic_t completed;
93
 
  int status;
 
92
  volatile bool completed;
 
93
  volatile int status;
94
94
  struct plugin *next;
95
95
} plugin;
96
96
 
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
 
  *new_plugin = (plugin){ .name = copy_name,
124
 
                          .argc = 1,
125
 
                          .disabled = false,
126
 
                          .next = plugin_list };
 
122
  *new_plugin = (plugin) { .name = copy_name,
 
123
                           .argc = 1,
 
124
                           .disabled = false,
 
125
                           .next = plugin_list };
127
126
  
128
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
 
  if(new_plugin->argv == NULL){
 
128
  if (new_plugin->argv == NULL){
130
129
    free(copy_name);
131
130
    free(new_plugin);
132
131
    return NULL;
209
208
 * Descriptor Flags".
210
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
211
210
 */
212
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
213
213
  int ret = fcntl(fd, F_GETFD, 0);
214
214
  /* If reading the flags failed, return error indication now. */
215
215
  if(ret < 0){
222
222
 
223
223
/* Mark processes as completed when they exit, and save their exit
224
224
   status. */
225
 
static void handle_sigchld(__attribute__((unused)) int sig){
226
 
  int old_errno = errno;
 
225
void handle_sigchld(__attribute__((unused)) int sig){
227
226
  while(true){
228
227
    plugin *proc = plugin_list;
229
228
    int status;
233
232
      break;
234
233
    }
235
234
    if(pid == -1){
236
 
      if(errno == ECHILD){
237
 
        /* No child processes */
238
 
        break;
 
235
      if (errno != ECHILD){
 
236
        perror("waitpid");
239
237
      }
240
 
      perror("waitpid");
 
238
      /* No child processes */
 
239
      break;
241
240
    }
242
241
    
243
242
    /* A child exited, find it in process_list */
249
248
      continue;
250
249
    }
251
250
    proc->status = status;
252
 
    proc->completed = 1;
 
251
    proc->completed = true;
253
252
  }
254
 
  errno = old_errno;
255
253
}
256
254
 
257
255
/* Prints out a password to stdout */
258
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
259
257
  ssize_t ret;
260
258
  for(size_t written = 0; written < length; written += (size_t)ret){
261
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
312
310
  struct dirent *dirst;
313
311
  struct stat st;
314
312
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
316
 
  ssize_t sret;
317
 
  intmax_t tmpmax;
 
313
  int ret, maxfd = 0;
318
314
  uid_t uid = 65534;
319
315
  gid_t gid = 65534;
320
316
  bool debug = false;
377
373
    { .name = NULL }
378
374
  };
379
375
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
 
378
    switch (key) {
383
379
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
385
 
        char *plugin_option;
386
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
387
 
          if(plugin_option[0] == '\0'){
 
380
      if (arg != NULL){
 
381
        char *p;
 
382
        while((p = strsep(&arg, ",")) != NULL){
 
383
          if(p[0] == '\0'){
388
384
            continue;
389
385
          }
390
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
386
          if(not add_argument(getplugin(NULL), p)){
391
387
            perror("add_argument");
392
388
            return ARGP_ERR_UNKNOWN;
393
389
          }
398
394
      if(arg == NULL){
399
395
        break;
400
396
      }
401
 
      if(not add_environment(getplugin(NULL), arg, true)){
402
 
        perror("add_environment");
 
397
      {
 
398
        char *envdef = strdup(arg);
 
399
        if(envdef == NULL){
 
400
          break;
 
401
        }
 
402
        if(not add_environment(getplugin(NULL), envdef, true)){
 
403
          perror("add_environment");
 
404
        }
403
405
      }
404
406
      break;
405
407
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
407
 
        char *plugin_name = strsep(&arg, ":");
408
 
        if(plugin_name[0] == '\0'){
409
 
          break;
410
 
        }
411
 
        char *plugin_option;
412
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
413
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
408
      if (arg != NULL){
 
409
        char *p_name = strsep(&arg, ":");
 
410
        if(p_name[0] == '\0' or arg == NULL){
 
411
          break;
 
412
        }
 
413
        char *opt = strsep(&arg, ":");
 
414
        if(opt[0] == '\0' or opt == NULL){
 
415
          break;
 
416
        }
 
417
        char *p;
 
418
        while((p = strsep(&opt, ",")) != NULL){
 
419
          if(p[0] == '\0'){
 
420
            continue;
 
421
          }
 
422
          if(not add_argument(getplugin(p_name), p)){
414
423
            perror("add_argument");
415
424
            return ARGP_ERR_UNKNOWN;
416
425
          }
426
435
        if(envdef == NULL){
427
436
          break;
428
437
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
438
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
439
        if(p_name == NULL){
 
440
          break;
 
441
        }
 
442
        envdef++;
 
443
        if(not add_environment(getplugin(p_name), envdef, true)){
431
444
          perror("add_environment");
432
445
        }
433
446
      }
434
447
      break;
435
448
    case 'd':                   /* --disable */
436
 
      if(arg != NULL){
 
449
      if (arg != NULL){
437
450
        plugin *p = getplugin(arg);
438
451
        if(p == NULL){
439
452
          return ARGP_ERR_UNKNOWN;
442
455
      }
443
456
      break;
444
457
    case 'e':                   /* --enable */
445
 
      if(arg != NULL){
 
458
      if (arg != NULL){
446
459
        plugin *p = getplugin(arg);
447
460
        if(p == NULL){
448
461
          return ARGP_ERR_UNKNOWN;
451
464
      }
452
465
      break;
453
466
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
467
      plugindir = strdup(arg);
456
468
      if(plugindir == NULL){
457
469
        perror("strdup");
461
473
      /* This is already done by parse_opt_config_file() */
462
474
      break;
463
475
    case 130:                   /* --userid */
464
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
465
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
466
 
         or arg[numchars] != '\0'){
467
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
468
 
                PRIdMAX "\n", arg, (intmax_t)uid);
469
 
      } else {
470
 
        uid = (uid_t)tmpmax;
471
 
      }
 
476
      uid = (uid_t)strtol(arg, NULL, 10);
472
477
      break;
473
478
    case 131:                   /* --groupid */
474
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
475
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
476
 
         or arg[numchars] != '\0'){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
478
 
                PRIdMAX "\n", arg, (intmax_t)gid);
479
 
      } else {
480
 
        gid = (gid_t)tmpmax;
481
 
      }
 
479
      gid = (gid_t)strtol(arg, NULL, 10);
482
480
      break;
483
481
    case 132:                   /* --debug */
484
482
      debug = true;
485
483
      break;
486
 
/*
487
 
 * When adding more options before this line, remember to also add a
488
 
 * "case" to the "parse_opt_config_file" function below.
489
 
 */
490
484
    case ARGP_KEY_ARG:
491
 
      /* Cryptsetup always passes an argument, which is an empty
492
 
         string if "none" was specified in /etc/crypttab.  So if
493
 
         argument was empty, we ignore it silently. */
494
 
      if(arg[0] != '\0'){
495
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
496
 
      }
 
485
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
497
486
      break;
498
487
    case ARGP_KEY_END:
499
488
      break;
505
494
  
506
495
  /* This option parser is the same as parse_opt() above, except it
507
496
     ignores everything but the --config-file option. */
508
 
  error_t parse_opt_config_file(int key, char *arg,
509
 
                                __attribute__((unused))
510
 
                                struct argp_state *state){
511
 
    switch(key){
 
497
  error_t parse_opt_config_file (int key, char *arg,
 
498
                                 __attribute__((unused))
 
499
                                 struct argp_state *state) {
 
500
    switch (key) {
512
501
    case 'g':                   /* --global-options */
513
502
    case 'G':                   /* --global-env */
514
503
    case 'o':                   /* --options-for */
518
507
    case 128:                   /* --plugin-dir */
519
508
      break;
520
509
    case 129:                   /* --config-file */
521
 
      free(argfile);
522
510
      argfile = strdup(arg);
523
511
      if(argfile == NULL){
524
512
        perror("strdup");
541
529
                       .args_doc = "",
542
530
                       .doc = "Mandos plugin runner -- Run plugins" };
543
531
  
544
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
532
  /* Parse using the parse_opt_config_file in order to get the custom
545
533
     config file location, if any. */
546
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
547
 
  if(ret == ARGP_ERR_UNKNOWN){
 
534
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
535
  if (ret == ARGP_ERR_UNKNOWN){
548
536
    fprintf(stderr, "Unknown error while parsing arguments\n");
549
537
    exitstatus = EXIT_FAILURE;
550
538
    goto fallback;
554
542
  argp.parser = parse_opt;
555
543
  
556
544
  /* Open the configfile if available */
557
 
  if(argfile == NULL){
 
545
  if (argfile == NULL){
558
546
    conffp = fopen(AFILE, "r");
559
547
  } else {
560
548
    conffp = fopen(argfile, "r");
563
551
    char *org_line = NULL;
564
552
    char *p, *arg, *new_arg, *line;
565
553
    size_t size = 0;
 
554
    ssize_t sret;
566
555
    const char whitespace_delims[] = " \r\t\f\v\n";
567
556
    const char comment_delim[] = "#";
568
557
 
615
604
  } else {
616
605
    /* Check for harmful errors and go to fallback. Other errors might
617
606
       not affect opening plugins */
618
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
607
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
619
608
      perror("fopen");
620
609
      exitstatus = EXIT_FAILURE;
621
610
      goto fallback;
624
613
  /* If there was any arguments from configuration file,
625
614
     pass them to parser as command arguments */
626
615
  if(custom_argv != NULL){
627
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
628
 
                     0, NULL);
629
 
    if(ret == ARGP_ERR_UNKNOWN){
 
616
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
617
                      0, NULL);
 
618
    if (ret == ARGP_ERR_UNKNOWN){
630
619
      fprintf(stderr, "Unknown error while parsing arguments\n");
631
620
      exitstatus = EXIT_FAILURE;
632
621
      goto fallback;
635
624
  
636
625
  /* Parse actual command line arguments, to let them override the
637
626
     config file */
638
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
639
 
  if(ret == ARGP_ERR_UNKNOWN){
 
627
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
628
  if (ret == ARGP_ERR_UNKNOWN){
640
629
    fprintf(stderr, "Unknown error while parsing arguments\n");
641
630
    exitstatus = EXIT_FAILURE;
642
631
    goto fallback;
649
638
      for(char **a = p->argv; *a != NULL; a++){
650
639
        fprintf(stderr, "\tArg: %s\n", *a);
651
640
      }
652
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
641
      fprintf(stderr, "...and %u environment variables\n", p->envc);
653
642
      for(char **a = p->environ; *a != NULL; a++){
654
643
        fprintf(stderr, "\t%s\n", *a);
655
644
      }
657
646
  }
658
647
  
659
648
  /* Strip permissions down to nobody */
 
649
  ret = setuid(uid);
 
650
  if (ret == -1){
 
651
    perror("setuid");
 
652
  }  
660
653
  setgid(gid);
661
 
  if(ret == -1){
 
654
  if (ret == -1){
662
655
    perror("setgid");
663
656
  }
664
 
  ret = setuid(uid);
665
 
  if(ret == -1){
666
 
    perror("setuid");
667
 
  }
668
657
  
669
 
  if(plugindir == NULL){
 
658
  if (plugindir == NULL){
670
659
    dir = opendir(PDIR);
671
660
  } else {
672
661
    dir = opendir(plugindir);
699
688
    
700
689
    /* All directory entries have been processed */
701
690
    if(dirst == NULL){
702
 
      if(errno == EBADF){
 
691
      if (errno == EBADF){
703
692
        perror("readdir");
704
693
        exitstatus = EXIT_FAILURE;
705
694
        goto fallback;
717
706
      
718
707
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
719
708
                                           ".dpkg-old",
720
 
                                           ".dpkg-bak",
721
709
                                           ".dpkg-divert", NULL };
722
710
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
723
711
        size_t pre_len = strlen(*pre);
765
753
    }
766
754
    
767
755
    ret = stat(filename, &st);
768
 
    if(ret == -1){
 
756
    if (ret == -1){
769
757
      perror("stat");
770
758
      free(filename);
771
759
      continue;
772
760
    }
773
761
 
774
762
    /* Ignore non-executable files */
775
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
763
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
776
764
      if(debug){
777
765
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
778
766
                " with bad type or mode\n", filename);
825
813
    
826
814
    int pipefd[2];
827
815
    ret = pipe(pipefd);
828
 
    if(ret == -1){
 
816
    if (ret == -1){
829
817
      perror("pipe");
830
818
      exitstatus = EXIT_FAILURE;
831
819
      goto fallback;
844
832
      goto fallback;
845
833
    }
846
834
    /* Block SIGCHLD until process is safely in process list */
847
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
835
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
848
836
    if(ret < 0){
849
837
      perror("sigprocmask");
850
838
      exitstatus = EXIT_FAILURE;
864
852
        perror("sigaction");
865
853
        _exit(EXIT_FAILURE);
866
854
      }
867
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
855
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
868
856
      if(ret < 0){
869
857
        perror("sigprocmask");
870
858
        _exit(EXIT_FAILURE);
871
859
      }
872
 
      
 
860
 
873
861
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
874
862
      if(ret == -1){
875
863
        perror("dup2");
898
886
    close(pipefd[1]);           /* Close unused write end of pipe */
899
887
    free(filename);
900
888
    plugin *new_plugin = getplugin(dirst->d_name);
901
 
    if(new_plugin == NULL){
 
889
    if (new_plugin == NULL){
902
890
      perror("getplugin");
903
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
891
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
904
892
      if(ret < 0){
905
893
        perror("sigprocmask");
906
894
      }
913
901
    
914
902
    /* Unblock SIGCHLD so signal handler can be run if this process
915
903
       has already completed */
916
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
904
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
917
905
    if(ret < 0){
918
906
      perror("sigprocmask");
919
907
      exitstatus = EXIT_FAILURE;
922
910
    
923
911
    FD_SET(new_plugin->fd, &rfds_all);
924
912
    
925
 
    if(maxfd < new_plugin->fd){
 
913
    if (maxfd < new_plugin->fd){
926
914
      maxfd = new_plugin->fd;
927
915
    }
 
916
    
928
917
  }
929
918
  
930
919
  closedir(dir);
931
920
  dir = NULL;
932
 
  free_plugin(getplugin(NULL));
933
 
  
 
921
 
934
922
  for(plugin *p = plugin_list; p != NULL; p = p->next){
935
923
    if(p->pid != 0){
936
924
      break;
941
929
      free_plugin_list();
942
930
    }
943
931
  }
944
 
  
 
932
 
945
933
  /* Main loop while running plugins exist */
946
934
  while(plugin_list){
947
935
    fd_set rfds = rfds_all;
948
936
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
949
 
    if(select_ret == -1){
 
937
    if (select_ret == -1){
950
938
      perror("select");
951
939
      exitstatus = EXIT_FAILURE;
952
940
      goto fallback;
953
941
    }
954
942
    /* OK, now either a process completed, or something can be read
955
943
       from one of them */
956
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
944
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
957
945
      /* Is this process completely done? */
958
 
      if(proc->completed and proc->eof){
 
946
      if(proc->eof and proc->completed){
959
947
        /* Only accept the plugin output if it exited cleanly */
960
948
        if(not WIFEXITED(proc->status)
961
949
           or WEXITSTATUS(proc->status) != 0){
963
951
 
964
952
          if(debug){
965
953
            if(WIFEXITED(proc->status)){
966
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
967
 
                      " status %d\n", proc->name,
968
 
                      (intmax_t) (proc->pid),
 
954
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
955
                      (unsigned int) (proc->pid),
969
956
                      WEXITSTATUS(proc->status));
970
 
            } else if(WIFSIGNALED(proc->status)){
971
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
973
 
                      (intmax_t) (proc->pid),
 
957
            } else if(WIFSIGNALED(proc->status)) {
 
958
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
959
                      (unsigned int) (proc->pid),
974
960
                      WTERMSIG(proc->status));
975
961
            } else if(WCOREDUMP(proc->status)){
976
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
962
              fprintf(stderr, "Plugin %d dumped core\n",
 
963
                      (unsigned int) (proc->pid));
978
964
            }
979
965
          }
980
966
          
988
974
            exitstatus = EXIT_FAILURE;
989
975
            goto fallback;
990
976
          }
991
 
          
992
 
          plugin *next_plugin = proc->next;
993
977
          free_plugin(proc);
994
 
          proc = next_plugin;
995
 
          
996
978
          /* We are done modifying process list, so unblock signal */
997
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
998
 
                            NULL);
 
979
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
980
                             NULL);
999
981
          if(ret < 0){
1000
982
            perror("sigprocmask");
1001
983
            exitstatus = EXIT_FAILURE;
1005
987
          if(plugin_list == NULL){
1006
988
            break;
1007
989
          }
1008
 
          
1009
990
          continue;
1010
991
        }
1011
992
        
1012
993
        /* This process exited nicely, so print its buffer */
1013
 
        
 
994
 
1014
995
        bool bret = print_out_password(proc->buffer,
1015
996
                                       proc->buffer_length);
1016
997
        if(not bret){
1023
1004
      /* This process has not completed.  Does it have any output? */
1024
1005
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1025
1006
        /* This process had nothing to say at this time */
1026
 
        proc = proc->next;
1027
1007
        continue;
1028
1008
      }
1029
1009
      /* Before reading, make the process' data buffer large enough */
1030
1010
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1031
1011
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1032
1012
                               + (size_t) BUFFER_SIZE);
1033
 
        if(proc->buffer == NULL){
 
1013
        if (proc->buffer == NULL){
1034
1014
          perror("malloc");
1035
1015
          exitstatus = EXIT_FAILURE;
1036
1016
          goto fallback;
1038
1018
        proc->buffer_size += BUFFER_SIZE;
1039
1019
      }
1040
1020
      /* Read from the process */
1041
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1042
 
                  BUFFER_SIZE);
1043
 
      if(sret < 0){
 
1021
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1022
                 BUFFER_SIZE);
 
1023
      if(ret < 0){
1044
1024
        /* Read error from this process; ignore the error */
1045
 
        proc = proc->next;
1046
1025
        continue;
1047
1026
      }
1048
 
      if(sret == 0){
 
1027
      if(ret == 0){
1049
1028
        /* got EOF */
1050
1029
        proc->eof = true;
1051
1030
      } else {
1052
 
        proc->buffer_length += (size_t) sret;
 
1031
        proc->buffer_length += (size_t) ret;
1053
1032
      }
1054
1033
    }
1055
1034
  }
1082
1061
    perror("sigaction");
1083
1062
    exitstatus = EXIT_FAILURE;
1084
1063
  }
1085
 
  
 
1064
 
1086
1065
  if(custom_argv != NULL){
1087
1066
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1088
1067
      free(*arg);
1094
1073
    closedir(dir);
1095
1074
  }
1096
1075
  
1097
 
  /* Kill the processes */
 
1076
  /* Free the process list and kill the processes */
1098
1077
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1099
1078
    if(p->pid != 0){
1100
1079
      close(p->fd);
1113
1092
  if(errno != ECHILD){
1114
1093
    perror("wait");
1115
1094
  }
1116
 
  
 
1095
 
1117
1096
  free_plugin_list();
1118
1097
  
1119
1098
  free(plugindir);