/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-04 12:38:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080904123835-hervuvv4pmpxl8r1
* README: Improved wording.

* initramfs-tools-hook: Do not copy auto-save files from
                        "/etc/mandos/plugins.d".  Do not create key
                        ring files.  Do not reset permissions on
                        things like "$DESTDIR/lib/mandos/plugins.d",
                        which should not be publicly accessible.

* plugin-runner.xml (DESCRIPTION, PLUGINS): Improved grammar.

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,
70
69
#define PDIR "/lib/mandos/plugins.d"
71
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
72
71
 
73
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
74
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
75
74
 
 
75
struct plugin;
 
76
 
76
77
typedef struct plugin{
77
78
  char *name;                   /* can be NULL or any plugin name */
78
79
  char **argv;
95
96
 
96
97
static plugin *plugin_list = NULL;
97
98
 
98
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
99
100
   or if none is found, creates a new one */
100
101
static plugin *getplugin(char *name){
101
102
  /* Check for exiting plugin with that name */
102
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if((p->name == name)
104
 
       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))){
105
106
      return p;
106
107
    }
107
108
  }
108
109
  /* Create a new plugin */
109
110
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if(new_plugin == NULL){
 
111
  if (new_plugin == NULL){
111
112
    return NULL;
112
113
  }
113
114
  char *copy_name = NULL;
117
118
      return NULL;
118
119
    }
119
120
  }
120
 
  
 
121
 
121
122
  *new_plugin = (plugin) { .name = copy_name,
122
123
                           .argc = 1,
123
124
                           .disabled = false,
124
125
                           .next = plugin_list };
125
126
  
126
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if(new_plugin->argv == NULL){
 
128
  if (new_plugin->argv == NULL){
128
129
    free(copy_name);
129
130
    free(new_plugin);
130
131
    return NULL;
186
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
187
188
  /* Search for this environment variable */
188
189
  for(char **e = p->environ; *e != NULL; e++){
189
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
190
191
      /* It already exists */
191
192
      if(replace){
192
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
193
194
        if(new == NULL){
194
195
          return false;
195
196
        }
207
208
 * Descriptor Flags".
208
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
209
210
 */
210
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
211
213
  int ret = fcntl(fd, F_GETFD, 0);
212
214
  /* If reading the flags failed, return error indication now. */
213
215
  if(ret < 0){
220
222
 
221
223
/* Mark processes as completed when they exit, and save their exit
222
224
   status. */
223
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
224
226
  while(true){
225
227
    plugin *proc = plugin_list;
226
228
    int status;
230
232
      break;
231
233
    }
232
234
    if(pid == -1){
233
 
      if(errno != ECHILD){
 
235
      if (errno != ECHILD){
234
236
        perror("waitpid");
235
237
      }
236
238
      /* No child processes */
237
239
      break;
238
240
    }
239
 
    
 
241
 
240
242
    /* A child exited, find it in process_list */
241
243
    while(proc != NULL and proc->pid != pid){
242
244
      proc = proc->next;
251
253
}
252
254
 
253
255
/* Prints out a password to stdout */
254
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
255
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
256
261
  for(size_t written = 0; written < length; written += (size_t)ret){
257
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
258
263
                                   length - written));
309
314
  struct stat st;
310
315
  fd_set rfds_all;
311
316
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
317
  uid_t uid = 65534;
314
318
  gid_t gid = 65534;
315
319
  bool debug = false;
372
376
    { .name = NULL }
373
377
  };
374
378
  
375
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
376
 
                    struct argp_state *state) {
377
 
    switch(key) {
 
379
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
380
                     struct argp_state *state) {
 
381
    switch (key) {
378
382
    case 'g':                   /* --global-options */
379
 
      if(arg != NULL){
 
383
      if (arg != NULL){
380
384
        char *p;
381
385
        while((p = strsep(&arg, ",")) != NULL){
382
386
          if(p[0] == '\0'){
393
397
      if(arg == NULL){
394
398
        break;
395
399
      }
396
 
      if(not add_environment(getplugin(NULL), arg, true)){
397
 
        perror("add_environment");
 
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
        }
398
408
      }
399
409
      break;
400
410
    case 'o':                   /* --options-for */
401
 
      if(arg != NULL){
 
411
      if (arg != NULL){
402
412
        char *p_name = strsep(&arg, ":");
403
 
        if(p_name[0] == '\0' or arg == NULL){
 
413
        if(p_name[0] == '\0'){
404
414
          break;
405
415
        }
406
416
        char *opt = strsep(&arg, ":");
407
 
        if(opt[0] == '\0' or opt == NULL){
 
417
        if(opt[0] == '\0'){
408
418
          break;
409
419
        }
410
 
        char *p;
411
 
        while((p = strsep(&opt, ",")) != NULL){
412
 
          if(p[0] == '\0'){
413
 
            continue;
414
 
          }
415
 
          if(not add_argument(getplugin(p_name), p)){
416
 
            perror("add_argument");
417
 
            return ARGP_ERR_UNKNOWN;
 
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
            }
418
430
          }
419
431
        }
420
432
      }
428
440
        if(envdef == NULL){
429
441
          break;
430
442
        }
431
 
        *envdef = '\0';
432
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
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)){
433
449
          perror("add_environment");
434
450
        }
435
451
      }
436
452
      break;
437
453
    case 'd':                   /* --disable */
438
 
      if(arg != NULL){
 
454
      if (arg != NULL){
439
455
        plugin *p = getplugin(arg);
440
456
        if(p == NULL){
441
457
          return ARGP_ERR_UNKNOWN;
444
460
      }
445
461
      break;
446
462
    case 'e':                   /* --enable */
447
 
      if(arg != NULL){
 
463
      if (arg != NULL){
448
464
        plugin *p = getplugin(arg);
449
465
        if(p == NULL){
450
466
          return ARGP_ERR_UNKNOWN;
453
469
      }
454
470
      break;
455
471
    case 128:                   /* --plugin-dir */
456
 
      free(plugindir);
457
472
      plugindir = strdup(arg);
458
473
      if(plugindir == NULL){
459
474
        perror("strdup");
463
478
      /* This is already done by parse_opt_config_file() */
464
479
      break;
465
480
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%u", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
470
 
                uid);
471
 
      }
 
481
      uid = (uid_t)strtol(arg, NULL, 10);
472
482
      break;
473
483
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%u", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
478
 
                arg, gid);
479
 
      }
 
484
      gid = (gid_t)strtol(arg, NULL, 10);
480
485
      break;
481
486
    case 132:                   /* --debug */
482
487
      debug = true;
483
488
      break;
484
 
/*
485
 
 * When adding more options before this line, remember to also add a
486
 
 * "case" to the "parse_opt_config_file" function below.
487
 
 */
488
489
    case ARGP_KEY_ARG:
489
 
      /* Cryptsetup always passes an argument, which is an empty
490
 
         string if "none" was specified in /etc/crypttab.  So if
491
 
         argument was empty, we ignore it silently. */
492
 
      if(arg[0] != '\0'){
493
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
494
 
      }
 
490
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
495
491
      break;
496
492
    case ARGP_KEY_END:
497
493
      break;
503
499
  
504
500
  /* This option parser is the same as parse_opt() above, except it
505
501
     ignores everything but the --config-file option. */
506
 
  error_t parse_opt_config_file(int key, char *arg,
507
 
                                __attribute__((unused))
508
 
                                struct argp_state *state) {
509
 
    switch(key) {
 
502
  error_t parse_opt_config_file (int key, char *arg,
 
503
                                 __attribute__((unused))
 
504
                                 struct argp_state *state) {
 
505
    switch (key) {
510
506
    case 'g':                   /* --global-options */
511
507
    case 'G':                   /* --global-env */
512
508
    case 'o':                   /* --options-for */
516
512
    case 128:                   /* --plugin-dir */
517
513
      break;
518
514
    case 129:                   /* --config-file */
519
 
      free(argfile);
520
515
      argfile = strdup(arg);
521
516
      if(argfile == NULL){
522
517
        perror("strdup");
539
534
                       .args_doc = "",
540
535
                       .doc = "Mandos plugin runner -- Run plugins" };
541
536
  
542
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
537
  /* Parse using the parse_opt_config_file in order to get the custom
543
538
     config file location, if any. */
544
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if(ret == ARGP_ERR_UNKNOWN){
 
539
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
540
  if (ret == ARGP_ERR_UNKNOWN){
546
541
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
542
    exitstatus = EXIT_FAILURE;
548
543
    goto fallback;
552
547
  argp.parser = parse_opt;
553
548
  
554
549
  /* Open the configfile if available */
555
 
  if(argfile == NULL){
 
550
  if (argfile == NULL){
556
551
    conffp = fopen(AFILE, "r");
557
552
  } else {
558
553
    conffp = fopen(argfile, "r");
561
556
    char *org_line = NULL;
562
557
    char *p, *arg, *new_arg, *line;
563
558
    size_t size = 0;
 
559
    ssize_t sret;
564
560
    const char whitespace_delims[] = " \r\t\f\v\n";
565
561
    const char comment_delim[] = "#";
566
562
 
613
609
  } else {
614
610
    /* Check for harmful errors and go to fallback. Other errors might
615
611
       not affect opening plugins */
616
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
612
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
613
      perror("fopen");
618
614
      exitstatus = EXIT_FAILURE;
619
615
      goto fallback;
622
618
  /* If there was any arguments from configuration file,
623
619
     pass them to parser as command arguments */
624
620
  if(custom_argv != NULL){
625
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
626
 
                     0, NULL);
627
 
    if(ret == ARGP_ERR_UNKNOWN){
 
621
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
622
                      0, NULL);
 
623
    if (ret == ARGP_ERR_UNKNOWN){
628
624
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
625
      exitstatus = EXIT_FAILURE;
630
626
      goto fallback;
633
629
  
634
630
  /* Parse actual command line arguments, to let them override the
635
631
     config file */
636
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if(ret == ARGP_ERR_UNKNOWN){
 
632
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
633
  if (ret == ARGP_ERR_UNKNOWN){
638
634
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
635
    exitstatus = EXIT_FAILURE;
640
636
    goto fallback;
656
652
  
657
653
  /* Strip permissions down to nobody */
658
654
  ret = setuid(uid);
659
 
  if(ret == -1){
 
655
  if (ret == -1){
660
656
    perror("setuid");
661
657
  }  
662
658
  setgid(gid);
663
 
  if(ret == -1){
 
659
  if (ret == -1){
664
660
    perror("setgid");
665
661
  }
666
662
  
667
 
  if(plugindir == NULL){
 
663
  if (plugindir == NULL){
668
664
    dir = opendir(PDIR);
669
665
  } else {
670
666
    dir = opendir(plugindir);
697
693
    
698
694
    /* All directory entries have been processed */
699
695
    if(dirst == NULL){
700
 
      if(errno == EBADF){
 
696
      if (errno == EBADF){
701
697
        perror("readdir");
702
698
        exitstatus = EXIT_FAILURE;
703
699
        goto fallback;
715
711
      
716
712
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
717
713
                                           ".dpkg-old",
718
 
                                           ".dpkg-bak",
719
714
                                           ".dpkg-divert", NULL };
720
715
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
721
716
        size_t pre_len = strlen(*pre);
752
747
    }
753
748
 
754
749
    char *filename;
755
 
    if(plugindir == NULL){
756
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
757
 
    } else {
758
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
759
 
    }
 
750
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
760
751
    if(ret < 0){
761
752
      perror("asprintf");
762
753
      continue;
763
754
    }
764
755
    
765
756
    ret = stat(filename, &st);
766
 
    if(ret == -1){
 
757
    if (ret == -1){
767
758
      perror("stat");
768
759
      free(filename);
769
760
      continue;
770
761
    }
771
762
 
772
763
    /* Ignore non-executable files */
773
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
764
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
765
      if(debug){
775
766
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
767
                " with bad type or mode\n", filename);
823
814
    
824
815
    int pipefd[2];
825
816
    ret = pipe(pipefd);
826
 
    if(ret == -1){
 
817
    if (ret == -1){
827
818
      perror("pipe");
828
819
      exitstatus = EXIT_FAILURE;
829
820
      goto fallback;
842
833
      goto fallback;
843
834
    }
844
835
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
836
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
837
    if(ret < 0){
847
838
      perror("sigprocmask");
848
839
      exitstatus = EXIT_FAILURE;
862
853
        perror("sigaction");
863
854
        _exit(EXIT_FAILURE);
864
855
      }
865
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
856
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
866
857
      if(ret < 0){
867
858
        perror("sigprocmask");
868
859
        _exit(EXIT_FAILURE);
869
860
      }
870
 
      
 
861
 
871
862
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
872
863
      if(ret == -1){
873
864
        perror("dup2");
896
887
    close(pipefd[1]);           /* Close unused write end of pipe */
897
888
    free(filename);
898
889
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if(new_plugin == NULL){
 
890
    if (new_plugin == NULL){
900
891
      perror("getplugin");
901
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
892
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
893
      if(ret < 0){
903
894
        perror("sigprocmask");
904
895
      }
911
902
    
912
903
    /* Unblock SIGCHLD so signal handler can be run if this process
913
904
       has already completed */
914
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
905
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
906
    if(ret < 0){
916
907
      perror("sigprocmask");
917
908
      exitstatus = EXIT_FAILURE;
920
911
    
921
912
    FD_SET(new_plugin->fd, &rfds_all);
922
913
    
923
 
    if(maxfd < new_plugin->fd){
 
914
    if (maxfd < new_plugin->fd){
924
915
      maxfd = new_plugin->fd;
925
916
    }
 
917
    
926
918
  }
927
919
  
928
920
  closedir(dir);
929
921
  dir = NULL;
930
 
  
 
922
 
931
923
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
924
    if(p->pid != 0){
933
925
      break;
938
930
      free_plugin_list();
939
931
    }
940
932
  }
941
 
  
 
933
 
942
934
  /* Main loop while running plugins exist */
943
935
  while(plugin_list){
944
936
    fd_set rfds = rfds_all;
945
937
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if(select_ret == -1){
 
938
    if (select_ret == -1){
947
939
      perror("select");
948
940
      exitstatus = EXIT_FAILURE;
949
941
      goto fallback;
950
942
    }
951
943
    /* OK, now either a process completed, or something can be read
952
944
       from one of them */
953
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
945
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
954
946
      /* Is this process completely done? */
955
947
      if(proc->eof and proc->completed){
956
948
        /* Only accept the plugin output if it exited cleanly */
968
960
                      (unsigned int) (proc->pid),
969
961
                      WTERMSIG(proc->status));
970
962
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
 
963
              fprintf(stderr, "Plugin %d dumped core\n",
972
964
                      (unsigned int) (proc->pid));
973
965
            }
974
966
          }
983
975
            exitstatus = EXIT_FAILURE;
984
976
            goto fallback;
985
977
          }
986
 
          
987
 
          plugin *next_plugin = proc->next;
988
978
          free_plugin(proc);
989
 
          proc = next_plugin;
990
 
          
991
979
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                            NULL);
 
980
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
981
                             NULL);
994
982
          if(ret < 0){
995
983
            perror("sigprocmask");
996
984
            exitstatus = EXIT_FAILURE;
1000
988
          if(plugin_list == NULL){
1001
989
            break;
1002
990
          }
1003
 
          
1004
991
          continue;
1005
992
        }
1006
993
        
1007
994
        /* This process exited nicely, so print its buffer */
1008
 
        
 
995
 
1009
996
        bool bret = print_out_password(proc->buffer,
1010
997
                                       proc->buffer_length);
1011
998
        if(not bret){
1018
1005
      /* This process has not completed.  Does it have any output? */
1019
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1020
1007
        /* This process had nothing to say at this time */
1021
 
        proc = proc->next;
1022
1008
        continue;
1023
1009
      }
1024
1010
      /* Before reading, make the process' data buffer large enough */
1025
1011
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
1012
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
1013
                               + (size_t) BUFFER_SIZE);
1028
 
        if(proc->buffer == NULL){
 
1014
        if (proc->buffer == NULL){
1029
1015
          perror("malloc");
1030
1016
          exitstatus = EXIT_FAILURE;
1031
1017
          goto fallback;
1033
1019
        proc->buffer_size += BUFFER_SIZE;
1034
1020
      }
1035
1021
      /* Read from the process */
1036
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1037
 
                  BUFFER_SIZE);
1038
 
      if(sret < 0){
 
1022
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1023
                 BUFFER_SIZE);
 
1024
      if(ret < 0){
1039
1025
        /* Read error from this process; ignore the error */
1040
 
        proc = proc->next;
1041
1026
        continue;
1042
1027
      }
1043
 
      if(sret == 0){
 
1028
      if(ret == 0){
1044
1029
        /* got EOF */
1045
1030
        proc->eof = true;
1046
1031
      } else {
1047
 
        proc->buffer_length += (size_t) sret;
 
1032
        proc->buffer_length += (size_t) ret;
1048
1033
      }
1049
1034
    }
1050
1035
  }
1058
1043
    bool bret;
1059
1044
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1060
1045
    char *passwordbuffer = getpass("Password: ");
1061
 
    size_t len = strlen(passwordbuffer);
1062
 
    /* Strip trailing newline */
1063
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1064
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1065
 
      len--;
1066
 
    }
1067
 
    bret = print_out_password(passwordbuffer, len);
 
1046
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1068
1047
    if(not bret){
1069
1048
      perror("print_out_password");
1070
1049
      exitstatus = EXIT_FAILURE;
1077
1056
    perror("sigaction");
1078
1057
    exitstatus = EXIT_FAILURE;
1079
1058
  }
1080
 
  
 
1059
 
1081
1060
  if(custom_argv != NULL){
1082
1061
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1083
1062
      free(*arg);
1089
1068
    closedir(dir);
1090
1069
  }
1091
1070
  
1092
 
  /* Kill the processes */
 
1071
  /* Free the process list and kill the processes */
1093
1072
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1094
1073
    if(p->pid != 0){
1095
1074
      close(p->fd);
1108
1087
  if(errno != ECHILD){
1109
1088
    perror("wait");
1110
1089
  }
1111
 
  
 
1090
 
1112
1091
  free_plugin_list();
1113
1092
  
1114
1093
  free(plugindir);