/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-13 04:29:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113042935-ztxe47n42q5z767b
* plugin-runner.c (main): Bug fix; do not accept a "d" character after
                          user ID or group ID numbers.  Bug fix: use
                          "%u" when printing PID of coredumped plugin,
                          not "%d".
* plugins.d/password-prompt.c (main): Remove comment which was copied
                                      from another program by mistake.
* plugins.d/splashy.c: Only comment changes.
* plugins.d/usplash.c: - '' -

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,
69
70
#define PDIR "/lib/mandos/plugins.d"
70
71
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
72
 
72
 
const char *argp_program_version = "plugin-runner 1.0";
 
73
const char *argp_program_version = "plugin-runner " VERSION;
73
74
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
75
 
75
 
struct plugin;
76
 
 
77
76
typedef struct plugin{
78
77
  char *name;                   /* can be NULL or any plugin name */
79
78
  char **argv;
96
95
 
97
96
static plugin *plugin_list = NULL;
98
97
 
99
 
/* Gets a existing plugin based on name,
 
98
/* Gets an existing plugin based on name,
100
99
   or if none is found, creates a new one */
101
100
static plugin *getplugin(char *name){
102
101
  /* Check for exiting plugin with that name */
118
117
      return NULL;
119
118
    }
120
119
  }
121
 
 
 
120
  
122
121
  *new_plugin = (plugin) { .name = copy_name,
123
122
                           .argc = 1,
124
123
                           .disabled = false,
132
131
  }
133
132
  new_plugin->argv[0] = copy_name;
134
133
  new_plugin->argv[1] = NULL;
135
 
 
 
134
  
136
135
  new_plugin->environ = malloc(sizeof(char *));
137
136
  if(new_plugin->environ == NULL){
138
137
    free(copy_name);
141
140
    return NULL;
142
141
  }
143
142
  new_plugin->environ[0] = NULL;
144
 
 
 
143
  
145
144
  /* Append the new plugin to the list */
146
145
  plugin_list = new_plugin;
147
146
  return new_plugin;
179
178
}
180
179
 
181
180
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def){
 
181
static bool add_environment(plugin *p, const char *def, bool replace){
183
182
  if(p == NULL){
184
183
    return false;
185
184
  }
 
185
  /* namelen = length of name of environment variable */
 
186
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
 
187
  /* Search for this environment variable */
 
188
  for(char **e = p->environ; *e != NULL; e++){
 
189
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
      /* It already exists */
 
191
      if(replace){
 
192
        char *new = realloc(*e, strlen(def) + 1);
 
193
        if(new == NULL){
 
194
          return false;
 
195
        }
 
196
        *e = new;
 
197
        strcpy(*e, def);
 
198
      }
 
199
      return true;
 
200
    }
 
201
  }
186
202
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
203
}
188
204
 
191
207
 * Descriptor Flags".
192
208
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
193
209
 */
194
 
static int set_cloexec_flag(int fd)
195
 
{
 
210
static int set_cloexec_flag(int fd){
196
211
  int ret = fcntl(fd, F_GETFD, 0);
197
212
  /* If reading the flags failed, return error indication now. */
198
213
  if(ret < 0){
205
220
 
206
221
/* Mark processes as completed when they exit, and save their exit
207
222
   status. */
208
 
void handle_sigchld(__attribute__((unused)) int sig){
 
223
static void handle_sigchld(__attribute__((unused)) int sig){
209
224
  while(true){
210
225
    plugin *proc = plugin_list;
211
226
    int status;
221
236
      /* No child processes */
222
237
      break;
223
238
    }
224
 
 
 
239
    
225
240
    /* A child exited, find it in process_list */
226
241
    while(proc != NULL and proc->pid != pid){
227
242
      proc = proc->next;
236
251
}
237
252
 
238
253
/* Prints out a password to stdout */
239
 
bool print_out_password(const char *buffer, size_t length){
 
254
static bool print_out_password(const char *buffer, size_t length){
240
255
  ssize_t ret;
241
 
  if(length>0 and buffer[length-1] == '\n'){
242
 
    length--;
243
 
  }
244
256
  for(size_t written = 0; written < length; written += (size_t)ret){
245
257
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
246
258
                                   length - written));
297
309
  struct stat st;
298
310
  fd_set rfds_all;
299
311
  int ret, maxfd = 0;
 
312
  ssize_t sret;
300
313
  uid_t uid = 65534;
301
314
  gid_t gid = 65534;
302
315
  bool debug = false;
327
340
    { .name = "global-options", .key = 'g',
328
341
      .arg = "OPTION[,OPTION[,...]]",
329
342
      .doc = "Options passed to all plugins" },
330
 
    { .name = "global-envs", .key = 'e',
 
343
    { .name = "global-env", .key = 'G',
331
344
      .arg = "VAR=value",
332
345
      .doc = "Environment variable passed to all plugins" },
333
346
    { .name = "options-for", .key = 'o',
334
347
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
348
      .doc = "Options passed only to specified plugin" },
336
 
    { .name = "envs-for", .key = 'f',
 
349
    { .name = "env-for", .key = 'E',
337
350
      .arg = "PLUGIN:ENV=value",
338
351
      .doc = "Environment variable passed to specified plugin" },
339
352
    { .name = "disable", .key = 'd',
340
353
      .arg = "PLUGIN",
341
354
      .doc = "Disable a specific plugin", .group = 1 },
 
355
    { .name = "enable", .key = 'e',
 
356
      .arg = "PLUGIN",
 
357
      .doc = "Enable a specific plugin", .group = 1 },
342
358
    { .name = "plugin-dir", .key = 128,
343
359
      .arg = "DIRECTORY",
344
360
      .doc = "Specify a different plugin directory", .group = 2 },
358
374
  
359
375
  error_t parse_opt (int key, char *arg, __attribute__((unused))
360
376
                     struct argp_state *state) {
361
 
    /* Get the INPUT argument from `argp_parse', which we know is a
362
 
       pointer to our plugin list pointer. */
363
377
    switch (key) {
364
378
    case 'g':                   /* --global-options */
365
379
      if (arg != NULL){
375
389
        }
376
390
      }
377
391
      break;
378
 
    case 'e':                   /* --global-envs */
 
392
    case 'G':                   /* --global-env */
379
393
      if(arg == NULL){
380
394
        break;
381
395
      }
382
 
      {
383
 
        char *envdef = strdup(arg);
384
 
        if(envdef == NULL){
385
 
          break;
386
 
        }
387
 
        if(not add_environment(getplugin(NULL), envdef)){
388
 
          perror("add_environment");
389
 
        }
 
396
      if(not add_environment(getplugin(NULL), arg, true)){
 
397
        perror("add_environment");
390
398
      }
391
399
      break;
392
400
    case 'o':                   /* --options-for */
393
401
      if (arg != NULL){
394
402
        char *p_name = strsep(&arg, ":");
395
 
        if(p_name[0] == '\0'){
 
403
        if(p_name[0] == '\0' or arg == NULL){
396
404
          break;
397
405
        }
398
406
        char *opt = strsep(&arg, ":");
399
 
        if(opt[0] == '\0'){
 
407
        if(opt[0] == '\0' or opt == NULL){
400
408
          break;
401
409
        }
402
 
        if(opt != NULL){
403
 
          char *p;
404
 
          while((p = strsep(&opt, ",")) != NULL){
405
 
            if(p[0] == '\0'){
406
 
              continue;
407
 
            }
408
 
            if(not add_argument(getplugin(p_name), p)){
409
 
              perror("add_argument");
410
 
              return ARGP_ERR_UNKNOWN;
411
 
            }
 
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;
412
418
          }
413
419
        }
414
420
      }
415
421
      break;
416
 
    case 'f':                   /* --envs-for */
 
422
    case 'E':                   /* --env-for */
417
423
      if(arg == NULL){
418
424
        break;
419
425
      }
422
428
        if(envdef == NULL){
423
429
          break;
424
430
        }
425
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
426
 
        if(p_name == NULL){
427
 
          break;
428
 
        }
429
 
        envdef++;
430
 
        if(not add_environment(getplugin(p_name), envdef)){
 
431
        *envdef = '\0';
 
432
        if(not add_environment(getplugin(arg), envdef+1, true)){
431
433
          perror("add_environment");
432
434
        }
433
435
      }
441
443
        p->disabled = true;
442
444
      }
443
445
      break;
 
446
    case 'e':                   /* --enable */
 
447
      if (arg != NULL){
 
448
        plugin *p = getplugin(arg);
 
449
        if(p == NULL){
 
450
          return ARGP_ERR_UNKNOWN;
 
451
        }
 
452
        p->disabled = false;
 
453
      }
 
454
      break;
444
455
    case 128:                   /* --plugin-dir */
 
456
      free(plugindir);
445
457
      plugindir = strdup(arg);
446
458
      if(plugindir == NULL){
447
459
        perror("strdup");
448
460
      }      
449
461
      break;
450
462
    case 129:                   /* --config-file */
 
463
      /* This is already done by parse_opt_config_file() */
 
464
      break;
 
465
    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
      }
 
472
      break;
 
473
    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
      }
 
480
      break;
 
481
    case 132:                   /* --debug */
 
482
      debug = true;
 
483
      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
    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
      }
 
495
      break;
 
496
    case ARGP_KEY_END:
 
497
      break;
 
498
    default:
 
499
      return ARGP_ERR_UNKNOWN;
 
500
    }
 
501
    return 0;
 
502
  }
 
503
  
 
504
  /* This option parser is the same as parse_opt() above, except it
 
505
     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) {
 
510
    case 'g':                   /* --global-options */
 
511
    case 'G':                   /* --global-env */
 
512
    case 'o':                   /* --options-for */
 
513
    case 'E':                   /* --env-for */
 
514
    case 'd':                   /* --disable */
 
515
    case 'e':                   /* --enable */
 
516
    case 128:                   /* --plugin-dir */
 
517
      break;
 
518
    case 129:                   /* --config-file */
 
519
      free(argfile);
451
520
      argfile = strdup(arg);
452
521
      if(argfile == NULL){
453
522
        perror("strdup");
454
523
      }
455
524
      break;      
456
525
    case 130:                   /* --userid */
457
 
      uid = (uid_t)strtol(arg, NULL, 10);
458
 
      break;
459
526
    case 131:                   /* --groupid */
460
 
      gid = (gid_t)strtol(arg, NULL, 10);
461
 
      break;
462
527
    case 132:                   /* --debug */
463
 
      debug = true;
464
 
      break;
465
528
    case ARGP_KEY_ARG:
466
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
467
 
      break;
468
529
    case ARGP_KEY_END:
469
530
      break;
470
531
    default:
473
534
    return 0;
474
535
  }
475
536
  
476
 
  struct argp argp = { .options = options, .parser = parse_opt,
477
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
537
  struct argp argp = { .options = options,
 
538
                       .parser = parse_opt_config_file,
 
539
                       .args_doc = "",
478
540
                       .doc = "Mandos plugin runner -- Run plugins" };
479
541
  
480
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
542
  /* Parse using parse_opt_config_file() in order to get the custom
 
543
     config file location, if any. */
 
544
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
481
545
  if (ret == ARGP_ERR_UNKNOWN){
482
546
    fprintf(stderr, "Unknown error while parsing arguments\n");
483
547
    exitstatus = EXIT_FAILURE;
484
548
    goto fallback;
485
549
  }
486
 
 
487
 
  /* Opens the configfile if aviable */
 
550
  
 
551
  /* Reset to the normal argument parser */
 
552
  argp.parser = parse_opt;
 
553
  
 
554
  /* Open the configfile if available */
488
555
  if (argfile == NULL){
489
556
    conffp = fopen(AFILE, "r");
490
557
  } else {
494
561
    char *org_line = NULL;
495
562
    char *p, *arg, *new_arg, *line;
496
563
    size_t size = 0;
497
 
    ssize_t sret;
498
564
    const char whitespace_delims[] = " \r\t\f\v\n";
499
565
    const char comment_delim[] = "#";
500
566
 
544
610
      }
545
611
    }
546
612
    free(org_line);
547
 
  } else{
 
613
  } else {
548
614
    /* Check for harmful errors and go to fallback. Other errors might
549
615
       not affect opening plugins */
550
616
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
556
622
  /* If there was any arguments from configuration file,
557
623
     pass them to parser as command arguments */
558
624
  if(custom_argv != NULL){
559
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
625
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
626
                      0, NULL);
560
627
    if (ret == ARGP_ERR_UNKNOWN){
561
628
      fprintf(stderr, "Unknown error while parsing arguments\n");
562
629
      exitstatus = EXIT_FAILURE;
564
631
    }
565
632
  }
566
633
  
 
634
  /* Parse actual command line arguments, to let them override the
 
635
     config file */
 
636
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
637
  if (ret == ARGP_ERR_UNKNOWN){
 
638
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
639
    exitstatus = EXIT_FAILURE;
 
640
    goto fallback;
 
641
  }
 
642
  
567
643
  if(debug){
568
644
    for(plugin *p = plugin_list; p != NULL; p=p->next){
569
645
      fprintf(stderr, "Plugin: %s has %d arguments\n",
577
653
      }
578
654
    }
579
655
  }
580
 
 
 
656
  
581
657
  /* Strip permissions down to nobody */
582
658
  ret = setuid(uid);
583
659
  if (ret == -1){
587
663
  if (ret == -1){
588
664
    perror("setgid");
589
665
  }
590
 
 
 
666
  
591
667
  if (plugindir == NULL){
592
668
    dir = opendir(PDIR);
593
669
  } else {
614
690
  }
615
691
  
616
692
  FD_ZERO(&rfds_all);
617
 
 
 
693
  
618
694
  /* Read and execute any executable in the plugin directory*/
619
695
  while(true){
620
696
    dirst = readdir(dir);
621
697
    
622
 
    // All directory entries have been processed
 
698
    /* All directory entries have been processed */
623
699
    if(dirst == NULL){
624
700
      if (errno == EBADF){
625
701
        perror("readdir");
631
707
    
632
708
    d_name_len = strlen(dirst->d_name);
633
709
    
634
 
    // Ignore dotfiles, backup files and other junk
 
710
    /* Ignore dotfiles, backup files and other junk */
635
711
    {
636
712
      bool bad_name = false;
637
713
      
639
715
      
640
716
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
641
717
                                           ".dpkg-old",
 
718
                                           ".dpkg-bak",
642
719
                                           ".dpkg-divert", NULL };
643
720
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
644
721
        size_t pre_len = strlen(*pre);
675
752
    }
676
753
 
677
754
    char *filename;
678
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
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
    }
679
760
    if(ret < 0){
680
761
      perror("asprintf");
681
762
      continue;
723
804
        }
724
805
        /* Add global environment variables */
725
806
        for(char **e = g->environ; *e != NULL; e++){
726
 
          if(not add_environment(p, *e)){
 
807
          if(not add_environment(p, *e, false)){
727
808
            perror("add_environment");
728
809
          }
729
810
        }
734
815
       process, too. */
735
816
    if(p->environ[0] != NULL){
736
817
      for(char **e = environ; *e != NULL; e++){
737
 
        char *copy = strdup(*e);
738
 
        if(copy == NULL){
739
 
          perror("strdup");
740
 
          continue;
741
 
        }
742
 
        if(not add_environment(p, copy)){
 
818
        if(not add_environment(p, *e, false)){
743
819
          perror("add_environment");
744
820
        }
745
821
      }
772
848
      exitstatus = EXIT_FAILURE;
773
849
      goto fallback;
774
850
    }
775
 
    // Starting a new process to be watched
 
851
    /* Starting a new process to be watched */
776
852
    pid_t pid = fork();
777
853
    if(pid == -1){
778
854
      perror("fork");
786
862
        perror("sigaction");
787
863
        _exit(EXIT_FAILURE);
788
864
      }
789
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
865
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
790
866
      if(ret < 0){
791
867
        perror("sigprocmask");
792
868
        _exit(EXIT_FAILURE);
793
869
      }
794
 
 
 
870
      
795
871
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
796
872
      if(ret == -1){
797
873
        perror("dup2");
847
923
    if (maxfd < new_plugin->fd){
848
924
      maxfd = new_plugin->fd;
849
925
    }
850
 
    
851
926
  }
852
927
  
853
928
  closedir(dir);
854
929
  dir = NULL;
855
 
 
 
930
  
856
931
  for(plugin *p = plugin_list; p != NULL; p = p->next){
857
932
    if(p->pid != 0){
858
933
      break;
863
938
      free_plugin_list();
864
939
    }
865
940
  }
866
 
 
 
941
  
867
942
  /* Main loop while running plugins exist */
868
943
  while(plugin_list){
869
944
    fd_set rfds = rfds_all;
875
950
    }
876
951
    /* OK, now either a process completed, or something can be read
877
952
       from one of them */
878
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
953
    for(plugin *proc = plugin_list; proc != NULL;){
879
954
      /* Is this process completely done? */
880
955
      if(proc->eof and proc->completed){
881
956
        /* Only accept the plugin output if it exited cleanly */
893
968
                      (unsigned int) (proc->pid),
894
969
                      WTERMSIG(proc->status));
895
970
            } else if(WCOREDUMP(proc->status)){
896
 
              fprintf(stderr, "Plugin %d dumped core\n",
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
897
972
                      (unsigned int) (proc->pid));
898
973
            }
899
974
          }
908
983
            exitstatus = EXIT_FAILURE;
909
984
            goto fallback;
910
985
          }
 
986
          
 
987
          plugin *next_plugin = proc->next;
911
988
          free_plugin(proc);
 
989
          proc = next_plugin;
 
990
          
912
991
          /* We are done modifying process list, so unblock signal */
913
992
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
914
993
                             NULL);
921
1000
          if(plugin_list == NULL){
922
1001
            break;
923
1002
          }
 
1003
          
924
1004
          continue;
925
1005
        }
926
1006
        
927
1007
        /* This process exited nicely, so print its buffer */
928
 
 
 
1008
        
929
1009
        bool bret = print_out_password(proc->buffer,
930
1010
                                       proc->buffer_length);
931
1011
        if(not bret){
938
1018
      /* This process has not completed.  Does it have any output? */
939
1019
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
940
1020
        /* This process had nothing to say at this time */
 
1021
        proc = proc->next;
941
1022
        continue;
942
1023
      }
943
1024
      /* Before reading, make the process' data buffer large enough */
952
1033
        proc->buffer_size += BUFFER_SIZE;
953
1034
      }
954
1035
      /* Read from the process */
955
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
956
 
                 BUFFER_SIZE);
957
 
      if(ret < 0){
 
1036
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1037
                  BUFFER_SIZE);
 
1038
      if(sret < 0){
958
1039
        /* Read error from this process; ignore the error */
 
1040
        proc = proc->next;
959
1041
        continue;
960
1042
      }
961
 
      if(ret == 0){
 
1043
      if(sret == 0){
962
1044
        /* got EOF */
963
1045
        proc->eof = true;
964
1046
      } else {
965
 
        proc->buffer_length += (size_t) ret;
 
1047
        proc->buffer_length += (size_t) sret;
966
1048
      }
967
1049
    }
968
1050
  }
976
1058
    bool bret;
977
1059
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
978
1060
    char *passwordbuffer = getpass("Password: ");
979
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
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);
980
1068
    if(not bret){
981
1069
      perror("print_out_password");
982
1070
      exitstatus = EXIT_FAILURE;
989
1077
    perror("sigaction");
990
1078
    exitstatus = EXIT_FAILURE;
991
1079
  }
992
 
 
 
1080
  
993
1081
  if(custom_argv != NULL){
994
1082
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
995
1083
      free(*arg);
1001
1089
    closedir(dir);
1002
1090
  }
1003
1091
  
1004
 
  /* Free the process list and kill the processes */
 
1092
  /* Kill the processes */
1005
1093
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1006
1094
    if(p->pid != 0){
1007
1095
      close(p->fd);
1020
1108
  if(errno != ECHILD){
1021
1109
    perror("wait");
1022
1110
  }
1023
 
 
 
1111
  
1024
1112
  free_plugin_list();
1025
1113
  
1026
1114
  free(plugindir);