/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: 2009-01-18 06:41:57 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090118064157-8o4oia1y0t8di0xj
* debian/mandos-client.lintian-overrides: Remove override for
                                          unbreakable line in
                                          plugin-runner manual page.
* plugin-runner.xml (EXAMPLES): Make long command line more breakable.

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;
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
  
122
123
  *new_plugin = (plugin) { .name = copy_name,
123
124
                           .argc = 1,
124
125
                           .disabled = false,
125
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
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
 
bool print_out_password(const char *buffer, size_t length){
 
256
static 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
 
  }
261
258
  for(size_t written = 0; written < length; written += (size_t)ret){
262
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
263
260
                                   length - written));
313
310
  struct dirent *dirst;
314
311
  struct stat st;
315
312
  fd_set rfds_all;
316
 
  int ret, maxfd = 0;
 
313
  int ret, numchars, maxfd = 0;
 
314
  ssize_t sret;
 
315
  intmax_t tmpmax;
317
316
  uid_t uid = 65534;
318
317
  gid_t gid = 65534;
319
318
  bool debug = false;
376
375
    { .name = NULL }
377
376
  };
378
377
  
379
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
380
 
                     struct argp_state *state) {
381
 
    switch (key) {
 
378
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
379
                    struct argp_state *state) {
 
380
    switch(key) {
382
381
    case 'g':                   /* --global-options */
383
 
      if (arg != NULL){
 
382
      if(arg != NULL){
384
383
        char *p;
385
384
        while((p = strsep(&arg, ",")) != NULL){
386
385
          if(p[0] == '\0'){
397
396
      if(arg == NULL){
398
397
        break;
399
398
      }
400
 
      {
401
 
        char *envdef = strdup(arg);
402
 
        if(envdef == NULL){
403
 
          break;
404
 
        }
405
 
        if(not add_environment(getplugin(NULL), envdef, true)){
406
 
          perror("add_environment");
407
 
        }
 
399
      if(not add_environment(getplugin(NULL), arg, true)){
 
400
        perror("add_environment");
408
401
      }
409
402
      break;
410
403
    case 'o':                   /* --options-for */
411
 
      if (arg != NULL){
 
404
      if(arg != NULL){
412
405
        char *p_name = strsep(&arg, ":");
413
 
        if(p_name[0] == '\0'){
 
406
        if(p_name[0] == '\0' or arg == NULL){
414
407
          break;
415
408
        }
416
409
        char *opt = strsep(&arg, ":");
417
 
        if(opt[0] == '\0'){
 
410
        if(opt[0] == '\0' or opt == NULL){
418
411
          break;
419
412
        }
420
 
        if(opt != NULL){
421
 
          char *p;
422
 
          while((p = strsep(&opt, ",")) != NULL){
423
 
            if(p[0] == '\0'){
424
 
              continue;
425
 
            }
426
 
            if(not add_argument(getplugin(p_name), p)){
427
 
              perror("add_argument");
428
 
              return ARGP_ERR_UNKNOWN;
429
 
            }
 
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;
430
421
          }
431
422
        }
432
423
      }
440
431
        if(envdef == NULL){
441
432
          break;
442
433
        }
443
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
444
 
        if(p_name == NULL){
445
 
          break;
446
 
        }
447
 
        envdef++;
448
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
434
        *envdef = '\0';
 
435
        if(not add_environment(getplugin(arg), envdef+1, true)){
449
436
          perror("add_environment");
450
437
        }
451
438
      }
452
439
      break;
453
440
    case 'd':                   /* --disable */
454
 
      if (arg != NULL){
 
441
      if(arg != NULL){
455
442
        plugin *p = getplugin(arg);
456
443
        if(p == NULL){
457
444
          return ARGP_ERR_UNKNOWN;
460
447
      }
461
448
      break;
462
449
    case 'e':                   /* --enable */
463
 
      if (arg != NULL){
 
450
      if(arg != NULL){
464
451
        plugin *p = getplugin(arg);
465
452
        if(p == NULL){
466
453
          return ARGP_ERR_UNKNOWN;
469
456
      }
470
457
      break;
471
458
    case 128:                   /* --plugin-dir */
 
459
      free(plugindir);
472
460
      plugindir = strdup(arg);
473
461
      if(plugindir == NULL){
474
462
        perror("strdup");
478
466
      /* This is already done by parse_opt_config_file() */
479
467
      break;
480
468
    case 130:                   /* --userid */
481
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
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
      }
482
477
      break;
483
478
    case 131:                   /* --groupid */
484
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
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
      }
485
487
      break;
486
488
    case 132:                   /* --debug */
487
489
      debug = true;
488
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
 */
489
495
    case ARGP_KEY_ARG:
490
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", 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
      }
491
502
      break;
492
503
    case ARGP_KEY_END:
493
504
      break;
499
510
  
500
511
  /* This option parser is the same as parse_opt() above, except it
501
512
     ignores everything but the --config-file option. */
502
 
  error_t parse_opt_config_file (int key, char *arg,
503
 
                                 __attribute__((unused))
504
 
                                 struct argp_state *state) {
505
 
    switch (key) {
 
513
  error_t parse_opt_config_file(int key, char *arg,
 
514
                                __attribute__((unused))
 
515
                                struct argp_state *state) {
 
516
    switch(key) {
506
517
    case 'g':                   /* --global-options */
507
518
    case 'G':                   /* --global-env */
508
519
    case 'o':                   /* --options-for */
512
523
    case 128:                   /* --plugin-dir */
513
524
      break;
514
525
    case 129:                   /* --config-file */
 
526
      free(argfile);
515
527
      argfile = strdup(arg);
516
528
      if(argfile == NULL){
517
529
        perror("strdup");
534
546
                       .args_doc = "",
535
547
                       .doc = "Mandos plugin runner -- Run plugins" };
536
548
  
537
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
549
  /* Parse using parse_opt_config_file() in order to get the custom
538
550
     config file location, if any. */
539
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
540
 
  if (ret == ARGP_ERR_UNKNOWN){
 
551
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
552
  if(ret == ARGP_ERR_UNKNOWN){
541
553
    fprintf(stderr, "Unknown error while parsing arguments\n");
542
554
    exitstatus = EXIT_FAILURE;
543
555
    goto fallback;
547
559
  argp.parser = parse_opt;
548
560
  
549
561
  /* Open the configfile if available */
550
 
  if (argfile == NULL){
 
562
  if(argfile == NULL){
551
563
    conffp = fopen(AFILE, "r");
552
564
  } else {
553
565
    conffp = fopen(argfile, "r");
556
568
    char *org_line = NULL;
557
569
    char *p, *arg, *new_arg, *line;
558
570
    size_t size = 0;
559
 
    ssize_t sret;
560
571
    const char whitespace_delims[] = " \r\t\f\v\n";
561
572
    const char comment_delim[] = "#";
562
573
 
609
620
  } else {
610
621
    /* Check for harmful errors and go to fallback. Other errors might
611
622
       not affect opening plugins */
612
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
623
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
613
624
      perror("fopen");
614
625
      exitstatus = EXIT_FAILURE;
615
626
      goto fallback;
618
629
  /* If there was any arguments from configuration file,
619
630
     pass them to parser as command arguments */
620
631
  if(custom_argv != NULL){
621
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
622
 
                      0, NULL);
623
 
    if (ret == ARGP_ERR_UNKNOWN){
 
632
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
633
                     0, NULL);
 
634
    if(ret == ARGP_ERR_UNKNOWN){
624
635
      fprintf(stderr, "Unknown error while parsing arguments\n");
625
636
      exitstatus = EXIT_FAILURE;
626
637
      goto fallback;
629
640
  
630
641
  /* Parse actual command line arguments, to let them override the
631
642
     config file */
632
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
633
 
  if (ret == ARGP_ERR_UNKNOWN){
 
643
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
644
  if(ret == ARGP_ERR_UNKNOWN){
634
645
    fprintf(stderr, "Unknown error while parsing arguments\n");
635
646
    exitstatus = EXIT_FAILURE;
636
647
    goto fallback;
643
654
      for(char **a = p->argv; *a != NULL; a++){
644
655
        fprintf(stderr, "\tArg: %s\n", *a);
645
656
      }
646
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
657
      fprintf(stderr, "...and %d environment variables\n", p->envc);
647
658
      for(char **a = p->environ; *a != NULL; a++){
648
659
        fprintf(stderr, "\t%s\n", *a);
649
660
      }
652
663
  
653
664
  /* Strip permissions down to nobody */
654
665
  ret = setuid(uid);
655
 
  if (ret == -1){
 
666
  if(ret == -1){
656
667
    perror("setuid");
657
668
  }  
658
669
  setgid(gid);
659
 
  if (ret == -1){
 
670
  if(ret == -1){
660
671
    perror("setgid");
661
672
  }
662
673
  
663
 
  if (plugindir == NULL){
 
674
  if(plugindir == NULL){
664
675
    dir = opendir(PDIR);
665
676
  } else {
666
677
    dir = opendir(plugindir);
693
704
    
694
705
    /* All directory entries have been processed */
695
706
    if(dirst == NULL){
696
 
      if (errno == EBADF){
 
707
      if(errno == EBADF){
697
708
        perror("readdir");
698
709
        exitstatus = EXIT_FAILURE;
699
710
        goto fallback;
711
722
      
712
723
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
713
724
                                           ".dpkg-old",
 
725
                                           ".dpkg-bak",
714
726
                                           ".dpkg-divert", NULL };
715
727
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
716
728
        size_t pre_len = strlen(*pre);
747
759
    }
748
760
 
749
761
    char *filename;
750
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
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
    }
751
767
    if(ret < 0){
752
768
      perror("asprintf");
753
769
      continue;
754
770
    }
755
771
    
756
772
    ret = stat(filename, &st);
757
 
    if (ret == -1){
 
773
    if(ret == -1){
758
774
      perror("stat");
759
775
      free(filename);
760
776
      continue;
761
777
    }
762
778
 
763
779
    /* Ignore non-executable files */
764
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
780
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
765
781
      if(debug){
766
782
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
767
783
                " with bad type or mode\n", filename);
814
830
    
815
831
    int pipefd[2];
816
832
    ret = pipe(pipefd);
817
 
    if (ret == -1){
 
833
    if(ret == -1){
818
834
      perror("pipe");
819
835
      exitstatus = EXIT_FAILURE;
820
836
      goto fallback;
833
849
      goto fallback;
834
850
    }
835
851
    /* Block SIGCHLD until process is safely in process list */
836
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
852
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
837
853
    if(ret < 0){
838
854
      perror("sigprocmask");
839
855
      exitstatus = EXIT_FAILURE;
853
869
        perror("sigaction");
854
870
        _exit(EXIT_FAILURE);
855
871
      }
856
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
872
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
857
873
      if(ret < 0){
858
874
        perror("sigprocmask");
859
875
        _exit(EXIT_FAILURE);
860
876
      }
861
 
 
 
877
      
862
878
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
863
879
      if(ret == -1){
864
880
        perror("dup2");
887
903
    close(pipefd[1]);           /* Close unused write end of pipe */
888
904
    free(filename);
889
905
    plugin *new_plugin = getplugin(dirst->d_name);
890
 
    if (new_plugin == NULL){
 
906
    if(new_plugin == NULL){
891
907
      perror("getplugin");
892
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
908
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
893
909
      if(ret < 0){
894
910
        perror("sigprocmask");
895
911
      }
902
918
    
903
919
    /* Unblock SIGCHLD so signal handler can be run if this process
904
920
       has already completed */
905
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
921
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
906
922
    if(ret < 0){
907
923
      perror("sigprocmask");
908
924
      exitstatus = EXIT_FAILURE;
911
927
    
912
928
    FD_SET(new_plugin->fd, &rfds_all);
913
929
    
914
 
    if (maxfd < new_plugin->fd){
 
930
    if(maxfd < new_plugin->fd){
915
931
      maxfd = new_plugin->fd;
916
932
    }
917
 
    
918
933
  }
919
934
  
920
935
  closedir(dir);
921
936
  dir = NULL;
922
 
 
 
937
  
923
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
924
939
    if(p->pid != 0){
925
940
      break;
930
945
      free_plugin_list();
931
946
    }
932
947
  }
933
 
 
 
948
  
934
949
  /* Main loop while running plugins exist */
935
950
  while(plugin_list){
936
951
    fd_set rfds = rfds_all;
937
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
938
 
    if (select_ret == -1){
 
953
    if(select_ret == -1){
939
954
      perror("select");
940
955
      exitstatus = EXIT_FAILURE;
941
956
      goto fallback;
942
957
    }
943
958
    /* OK, now either a process completed, or something can be read
944
959
       from one of them */
945
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
960
    for(plugin *proc = plugin_list; proc != NULL;){
946
961
      /* Is this process completely done? */
947
962
      if(proc->eof and proc->completed){
948
963
        /* Only accept the plugin output if it exited cleanly */
952
967
 
953
968
          if(debug){
954
969
            if(WIFEXITED(proc->status)){
955
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
956
 
                      (unsigned int) (proc->pid),
 
970
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
971
                      " %d\n", (intmax_t) (proc->pid),
957
972
                      WEXITSTATUS(proc->status));
958
973
            } else if(WIFSIGNALED(proc->status)) {
959
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
960
 
                      (unsigned int) (proc->pid),
 
974
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
975
                      " %d\n", (intmax_t) (proc->pid),
961
976
                      WTERMSIG(proc->status));
962
977
            } else if(WCOREDUMP(proc->status)){
963
 
              fprintf(stderr, "Plugin %d dumped core\n",
964
 
                      (unsigned int) (proc->pid));
 
978
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
979
                      (intmax_t) (proc->pid));
965
980
            }
966
981
          }
967
982
          
975
990
            exitstatus = EXIT_FAILURE;
976
991
            goto fallback;
977
992
          }
 
993
          
 
994
          plugin *next_plugin = proc->next;
978
995
          free_plugin(proc);
 
996
          proc = next_plugin;
 
997
          
979
998
          /* We are done modifying process list, so unblock signal */
980
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
981
 
                             NULL);
 
999
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1000
                            NULL);
982
1001
          if(ret < 0){
983
1002
            perror("sigprocmask");
984
1003
            exitstatus = EXIT_FAILURE;
988
1007
          if(plugin_list == NULL){
989
1008
            break;
990
1009
          }
 
1010
          
991
1011
          continue;
992
1012
        }
993
1013
        
994
1014
        /* This process exited nicely, so print its buffer */
995
 
 
 
1015
        
996
1016
        bool bret = print_out_password(proc->buffer,
997
1017
                                       proc->buffer_length);
998
1018
        if(not bret){
1005
1025
      /* This process has not completed.  Does it have any output? */
1006
1026
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1007
1027
        /* This process had nothing to say at this time */
 
1028
        proc = proc->next;
1008
1029
        continue;
1009
1030
      }
1010
1031
      /* Before reading, make the process' data buffer large enough */
1011
1032
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1012
1033
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1013
1034
                               + (size_t) BUFFER_SIZE);
1014
 
        if (proc->buffer == NULL){
 
1035
        if(proc->buffer == NULL){
1015
1036
          perror("malloc");
1016
1037
          exitstatus = EXIT_FAILURE;
1017
1038
          goto fallback;
1019
1040
        proc->buffer_size += BUFFER_SIZE;
1020
1041
      }
1021
1042
      /* Read from the process */
1022
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1023
 
                 BUFFER_SIZE);
1024
 
      if(ret < 0){
 
1043
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1044
                  BUFFER_SIZE);
 
1045
      if(sret < 0){
1025
1046
        /* Read error from this process; ignore the error */
 
1047
        proc = proc->next;
1026
1048
        continue;
1027
1049
      }
1028
 
      if(ret == 0){
 
1050
      if(sret == 0){
1029
1051
        /* got EOF */
1030
1052
        proc->eof = true;
1031
1053
      } else {
1032
 
        proc->buffer_length += (size_t) ret;
 
1054
        proc->buffer_length += (size_t) sret;
1033
1055
      }
1034
1056
    }
1035
1057
  }
1043
1065
    bool bret;
1044
1066
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1045
1067
    char *passwordbuffer = getpass("Password: ");
1046
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
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);
1047
1075
    if(not bret){
1048
1076
      perror("print_out_password");
1049
1077
      exitstatus = EXIT_FAILURE;
1056
1084
    perror("sigaction");
1057
1085
    exitstatus = EXIT_FAILURE;
1058
1086
  }
1059
 
 
 
1087
  
1060
1088
  if(custom_argv != NULL){
1061
1089
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1062
1090
      free(*arg);
1068
1096
    closedir(dir);
1069
1097
  }
1070
1098
  
1071
 
  /* Free the process list and kill the processes */
 
1099
  /* Kill the processes */
1072
1100
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1073
1101
    if(p->pid != 0){
1074
1102
      close(p->fd);
1087
1115
  if(errno != ECHILD){
1088
1116
    perror("wait");
1089
1117
  }
1090
 
 
 
1118
  
1091
1119
  free_plugin_list();
1092
1120
  
1093
1121
  free(plugindir);