/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-01 16:19:32 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901161932-ostp7tulh9aijulh
* plugin-runner.c (add_environment): Never insert existing environment
                                     variables.
  (main): Rename "--global-envs" to "--global-env" and "--envs-for" to
          "--env-for".

* plugin-runner.xml (SYNOPSIS): Rename "--global-envs" to
                                "--global-env" and "--envs-for" to
                                "--env-for".
  (OPTIONS): Added "--global-env" and "--env-for".
  (FALLBACK): Add id attribute.
  (EXIT STATUS): Add text.
  (ENVIRONMENT): New section.
  (FILES): Document configuration file.

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
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets an existing plugin based on name,
 
99
/* Gets a 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);
119
118
      return NULL;
120
119
    }
121
120
  }
122
 
  
123
 
  *new_plugin = (plugin){ .name = copy_name,
124
 
                          .argc = 1,
125
 
                          .disabled = false,
126
 
                          .next = plugin_list };
 
121
 
 
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;
180
179
}
181
180
 
182
181
/* Add to a plugin's environment */
183
 
static bool add_environment(plugin *p, const char *def, bool replace){
 
182
static bool add_environment(plugin *p, const char *def){
184
183
  if(p == NULL){
185
184
    return false;
186
185
  }
188
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
189
188
  /* Search for this environment variable */
190
189
  for(char **e = p->environ; *e != NULL; e++){
191
 
    if(strncmp(*e, def, namelen + 1) == 0){
192
 
      /* It already exists */
193
 
      if(replace){
194
 
        char *new = realloc(*e, strlen(def) + 1);
195
 
        if(new == NULL){
196
 
          return false;
197
 
        }
198
 
        *e = new;
199
 
        strcpy(*e, def);
200
 
      }
 
190
    if(strncmp(*e, def, namelen+1) == 0){
 
191
      /* Refuse to add an existing variable */
201
192
      return true;
202
193
    }
203
194
  }
209
200
 * Descriptor Flags".
210
201
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
211
202
 */
212
 
static int set_cloexec_flag(int fd){
 
203
static int set_cloexec_flag(int fd)
 
204
{
213
205
  int ret = fcntl(fd, F_GETFD, 0);
214
206
  /* If reading the flags failed, return error indication now. */
215
207
  if(ret < 0){
222
214
 
223
215
/* Mark processes as completed when they exit, and save their exit
224
216
   status. */
225
 
static void handle_sigchld(__attribute__((unused)) int sig){
226
 
  int old_errno = errno;
 
217
void handle_sigchld(__attribute__((unused)) int sig){
227
218
  while(true){
228
219
    plugin *proc = plugin_list;
229
220
    int status;
233
224
      break;
234
225
    }
235
226
    if(pid == -1){
236
 
      if(errno == ECHILD){
237
 
        /* No child processes */
238
 
        break;
 
227
      if (errno != ECHILD){
 
228
        perror("waitpid");
239
229
      }
240
 
      perror("waitpid");
 
230
      /* No child processes */
 
231
      break;
241
232
    }
242
 
    
 
233
 
243
234
    /* A child exited, find it in process_list */
244
235
    while(proc != NULL and proc->pid != pid){
245
236
      proc = proc->next;
249
240
      continue;
250
241
    }
251
242
    proc->status = status;
252
 
    proc->completed = 1;
 
243
    proc->completed = true;
253
244
  }
254
 
  errno = old_errno;
255
245
}
256
246
 
257
247
/* Prints out a password to stdout */
258
 
static bool print_out_password(const char *buffer, size_t length){
 
248
bool print_out_password(const char *buffer, size_t length){
259
249
  ssize_t ret;
 
250
  if(length>0 and buffer[length-1] == '\n'){
 
251
    length--;
 
252
  }
260
253
  for(size_t written = 0; written < length; written += (size_t)ret){
261
254
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
262
255
                                   length - written));
312
305
  struct dirent *dirst;
313
306
  struct stat st;
314
307
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
316
 
  ssize_t sret;
317
 
  intmax_t tmpmax;
 
308
  int ret, maxfd = 0;
318
309
  uid_t uid = 65534;
319
310
  gid_t gid = 65534;
320
311
  bool debug = false;
345
336
    { .name = "global-options", .key = 'g',
346
337
      .arg = "OPTION[,OPTION[,...]]",
347
338
      .doc = "Options passed to all plugins" },
348
 
    { .name = "global-env", .key = 'G',
 
339
    { .name = "global-env", .key = 'e',
349
340
      .arg = "VAR=value",
350
341
      .doc = "Environment variable passed to all plugins" },
351
342
    { .name = "options-for", .key = 'o',
352
343
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
353
344
      .doc = "Options passed only to specified plugin" },
354
 
    { .name = "env-for", .key = 'E',
 
345
    { .name = "env-for", .key = 'f',
355
346
      .arg = "PLUGIN:ENV=value",
356
347
      .doc = "Environment variable passed to specified plugin" },
357
348
    { .name = "disable", .key = 'd',
358
349
      .arg = "PLUGIN",
359
350
      .doc = "Disable a specific plugin", .group = 1 },
360
 
    { .name = "enable", .key = 'e',
361
 
      .arg = "PLUGIN",
362
 
      .doc = "Enable a specific plugin", .group = 1 },
363
351
    { .name = "plugin-dir", .key = 128,
364
352
      .arg = "DIRECTORY",
365
353
      .doc = "Specify a different plugin directory", .group = 2 },
377
365
    { .name = NULL }
378
366
  };
379
367
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
368
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
369
                     struct argp_state *state) {
 
370
    /* Get the INPUT argument from `argp_parse', which we know is a
 
371
       pointer to our plugin list pointer. */
 
372
    switch (key) {
383
373
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
 
374
      if (arg != NULL){
385
375
        char *p;
386
376
        while((p = strsep(&arg, ",")) != NULL){
387
377
          if(p[0] == '\0'){
394
384
        }
395
385
      }
396
386
      break;
397
 
    case 'G':                   /* --global-env */
 
387
    case 'e':                   /* --global-env */
398
388
      if(arg == NULL){
399
389
        break;
400
390
      }
401
 
      if(not add_environment(getplugin(NULL), arg, true)){
402
 
        perror("add_environment");
 
391
      {
 
392
        char *envdef = strdup(arg);
 
393
        if(envdef == NULL){
 
394
          break;
 
395
        }
 
396
        if(not add_environment(getplugin(NULL), envdef)){
 
397
          perror("add_environment");
 
398
        }
403
399
      }
404
400
      break;
405
401
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
 
402
      if (arg != NULL){
407
403
        char *p_name = strsep(&arg, ":");
408
 
        if(p_name[0] == '\0' or arg == NULL){
 
404
        if(p_name[0] == '\0'){
409
405
          break;
410
406
        }
411
407
        char *opt = strsep(&arg, ":");
412
 
        if(opt[0] == '\0' or opt == NULL){
 
408
        if(opt[0] == '\0'){
413
409
          break;
414
410
        }
415
 
        char *p;
416
 
        while((p = strsep(&opt, ",")) != NULL){
417
 
          if(p[0] == '\0'){
418
 
            continue;
419
 
          }
420
 
          if(not add_argument(getplugin(p_name), p)){
421
 
            perror("add_argument");
422
 
            return ARGP_ERR_UNKNOWN;
 
411
        if(opt != NULL){
 
412
          char *p;
 
413
          while((p = strsep(&opt, ",")) != NULL){
 
414
            if(p[0] == '\0'){
 
415
              continue;
 
416
            }
 
417
            if(not add_argument(getplugin(p_name), p)){
 
418
              perror("add_argument");
 
419
              return ARGP_ERR_UNKNOWN;
 
420
            }
423
421
          }
424
422
        }
425
423
      }
426
424
      break;
427
 
    case 'E':                   /* --env-for */
 
425
    case 'f':                   /* --env-for */
428
426
      if(arg == NULL){
429
427
        break;
430
428
      }
433
431
        if(envdef == NULL){
434
432
          break;
435
433
        }
436
 
        *envdef = '\0';
437
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
434
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
435
        if(p_name == NULL){
 
436
          break;
 
437
        }
 
438
        envdef++;
 
439
        if(not add_environment(getplugin(p_name), envdef)){
438
440
          perror("add_environment");
439
441
        }
440
442
      }
441
443
      break;
442
444
    case 'd':                   /* --disable */
443
 
      if(arg != NULL){
 
445
      if (arg != NULL){
444
446
        plugin *p = getplugin(arg);
445
447
        if(p == NULL){
446
448
          return ARGP_ERR_UNKNOWN;
448
450
        p->disabled = true;
449
451
      }
450
452
      break;
451
 
    case 'e':                   /* --enable */
452
 
      if(arg != NULL){
453
 
        plugin *p = getplugin(arg);
454
 
        if(p == NULL){
455
 
          return ARGP_ERR_UNKNOWN;
456
 
        }
457
 
        p->disabled = false;
458
 
      }
459
 
      break;
460
453
    case 128:                   /* --plugin-dir */
461
 
      free(plugindir);
462
454
      plugindir = strdup(arg);
463
455
      if(plugindir == NULL){
464
456
        perror("strdup");
465
457
      }      
466
458
      break;
467
459
    case 129:                   /* --config-file */
468
 
      /* This is already done by parse_opt_config_file() */
469
 
      break;
470
 
    case 130:                   /* --userid */
471
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
472
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
473
 
         or arg[numchars] != '\0'){
474
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
 
                PRIdMAX "\n", arg, (intmax_t)uid);
476
 
      } else {
477
 
        uid = (uid_t)tmpmax;
478
 
      }
479
 
      break;
480
 
    case 131:                   /* --groupid */
481
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
482
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
483
 
         or arg[numchars] != '\0'){
484
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
 
                PRIdMAX "\n", arg, (intmax_t)gid);
486
 
      } else {
487
 
        gid = (gid_t)tmpmax;
488
 
      }
489
 
      break;
490
 
    case 132:                   /* --debug */
491
 
      debug = true;
492
 
      break;
493
 
/*
494
 
 * When adding more options before this line, remember to also add a
495
 
 * "case" to the "parse_opt_config_file" function below.
496
 
 */
497
 
    case ARGP_KEY_ARG:
498
 
      /* Cryptsetup always passes an argument, which is an empty
499
 
         string if "none" was specified in /etc/crypttab.  So if
500
 
         argument was empty, we ignore it silently. */
501
 
      if(arg[0] != '\0'){
502
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
503
 
      }
504
 
      break;
505
 
    case ARGP_KEY_END:
506
 
      break;
507
 
    default:
508
 
      return ARGP_ERR_UNKNOWN;
509
 
    }
510
 
    return 0;
511
 
  }
512
 
  
513
 
  /* This option parser is the same as parse_opt() above, except it
514
 
     ignores everything but the --config-file option. */
515
 
  error_t parse_opt_config_file(int key, char *arg,
516
 
                                __attribute__((unused))
517
 
                                struct argp_state *state){
518
 
    switch(key){
519
 
    case 'g':                   /* --global-options */
520
 
    case 'G':                   /* --global-env */
521
 
    case 'o':                   /* --options-for */
522
 
    case 'E':                   /* --env-for */
523
 
    case 'd':                   /* --disable */
524
 
    case 'e':                   /* --enable */
525
 
    case 128:                   /* --plugin-dir */
526
 
      break;
527
 
    case 129:                   /* --config-file */
528
 
      free(argfile);
529
460
      argfile = strdup(arg);
530
461
      if(argfile == NULL){
531
462
        perror("strdup");
532
463
      }
533
464
      break;      
534
465
    case 130:                   /* --userid */
 
466
      uid = (uid_t)strtol(arg, NULL, 10);
 
467
      break;
535
468
    case 131:                   /* --groupid */
 
469
      gid = (gid_t)strtol(arg, NULL, 10);
 
470
      break;
536
471
    case 132:                   /* --debug */
 
472
      debug = true;
 
473
      break;
537
474
    case ARGP_KEY_ARG:
 
475
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
476
      break;
538
477
    case ARGP_KEY_END:
539
478
      break;
540
479
    default:
543
482
    return 0;
544
483
  }
545
484
  
546
 
  struct argp argp = { .options = options,
547
 
                       .parser = parse_opt_config_file,
548
 
                       .args_doc = "",
 
485
  struct argp argp = { .options = options, .parser = parse_opt,
 
486
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
549
487
                       .doc = "Mandos plugin runner -- Run plugins" };
550
488
  
551
 
  /* Parse using parse_opt_config_file() in order to get the custom
552
 
     config file location, if any. */
553
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
554
 
  if(ret == ARGP_ERR_UNKNOWN){
 
489
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
490
  if (ret == ARGP_ERR_UNKNOWN){
555
491
    fprintf(stderr, "Unknown error while parsing arguments\n");
556
492
    exitstatus = EXIT_FAILURE;
557
493
    goto fallback;
558
494
  }
559
 
  
560
 
  /* Reset to the normal argument parser */
561
 
  argp.parser = parse_opt;
562
 
  
563
 
  /* Open the configfile if available */
564
 
  if(argfile == NULL){
 
495
 
 
496
  /* Opens the configfile if aviable */
 
497
  if (argfile == NULL){
565
498
    conffp = fopen(AFILE, "r");
566
499
  } else {
567
500
    conffp = fopen(argfile, "r");
570
503
    char *org_line = NULL;
571
504
    char *p, *arg, *new_arg, *line;
572
505
    size_t size = 0;
 
506
    ssize_t sret;
573
507
    const char whitespace_delims[] = " \r\t\f\v\n";
574
508
    const char comment_delim[] = "#";
575
509
 
619
553
      }
620
554
    }
621
555
    free(org_line);
622
 
  } else {
 
556
  } else{
623
557
    /* Check for harmful errors and go to fallback. Other errors might
624
558
       not affect opening plugins */
625
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
559
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
626
560
      perror("fopen");
627
561
      exitstatus = EXIT_FAILURE;
628
562
      goto fallback;
631
565
  /* If there was any arguments from configuration file,
632
566
     pass them to parser as command arguments */
633
567
  if(custom_argv != NULL){
634
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
635
 
                     0, NULL);
636
 
    if(ret == ARGP_ERR_UNKNOWN){
 
568
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
569
    if (ret == ARGP_ERR_UNKNOWN){
637
570
      fprintf(stderr, "Unknown error while parsing arguments\n");
638
571
      exitstatus = EXIT_FAILURE;
639
572
      goto fallback;
640
573
    }
641
574
  }
642
575
  
643
 
  /* Parse actual command line arguments, to let them override the
644
 
     config file */
645
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
646
 
  if(ret == ARGP_ERR_UNKNOWN){
647
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
648
 
    exitstatus = EXIT_FAILURE;
649
 
    goto fallback;
650
 
  }
651
 
  
652
576
  if(debug){
653
577
    for(plugin *p = plugin_list; p != NULL; p=p->next){
654
578
      fprintf(stderr, "Plugin: %s has %d arguments\n",
656
580
      for(char **a = p->argv; *a != NULL; a++){
657
581
        fprintf(stderr, "\tArg: %s\n", *a);
658
582
      }
659
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
583
      fprintf(stderr, "...and %u environment variables\n", p->envc);
660
584
      for(char **a = p->environ; *a != NULL; a++){
661
585
        fprintf(stderr, "\t%s\n", *a);
662
586
      }
663
587
    }
664
588
  }
665
 
  
 
589
 
666
590
  /* Strip permissions down to nobody */
 
591
  ret = setuid(uid);
 
592
  if (ret == -1){
 
593
    perror("setuid");
 
594
  }  
667
595
  setgid(gid);
668
 
  if(ret == -1){
 
596
  if (ret == -1){
669
597
    perror("setgid");
670
598
  }
671
 
  ret = setuid(uid);
672
 
  if(ret == -1){
673
 
    perror("setuid");
674
 
  }
675
 
  
676
 
  if(plugindir == NULL){
 
599
 
 
600
  if (plugindir == NULL){
677
601
    dir = opendir(PDIR);
678
602
  } else {
679
603
    dir = opendir(plugindir);
699
623
  }
700
624
  
701
625
  FD_ZERO(&rfds_all);
702
 
  
 
626
 
703
627
  /* Read and execute any executable in the plugin directory*/
704
628
  while(true){
705
629
    dirst = readdir(dir);
706
630
    
707
 
    /* All directory entries have been processed */
 
631
    // All directory entries have been processed
708
632
    if(dirst == NULL){
709
 
      if(errno == EBADF){
 
633
      if (errno == EBADF){
710
634
        perror("readdir");
711
635
        exitstatus = EXIT_FAILURE;
712
636
        goto fallback;
716
640
    
717
641
    d_name_len = strlen(dirst->d_name);
718
642
    
719
 
    /* Ignore dotfiles, backup files and other junk */
 
643
    // Ignore dotfiles, backup files and other junk
720
644
    {
721
645
      bool bad_name = false;
722
646
      
724
648
      
725
649
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
726
650
                                           ".dpkg-old",
727
 
                                           ".dpkg-bak",
728
651
                                           ".dpkg-divert", NULL };
729
652
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
730
653
        size_t pre_len = strlen(*pre);
761
684
    }
762
685
 
763
686
    char *filename;
764
 
    if(plugindir == NULL){
765
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
766
 
    } else {
767
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
768
 
    }
 
687
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
769
688
    if(ret < 0){
770
689
      perror("asprintf");
771
690
      continue;
772
691
    }
773
692
    
774
693
    ret = stat(filename, &st);
775
 
    if(ret == -1){
 
694
    if (ret == -1){
776
695
      perror("stat");
777
696
      free(filename);
778
697
      continue;
779
698
    }
780
699
 
781
700
    /* Ignore non-executable files */
782
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
701
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
783
702
      if(debug){
784
703
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
785
704
                " with bad type or mode\n", filename);
813
732
        }
814
733
        /* Add global environment variables */
815
734
        for(char **e = g->environ; *e != NULL; e++){
816
 
          if(not add_environment(p, *e, false)){
 
735
          if(not add_environment(p, *e)){
817
736
            perror("add_environment");
818
737
          }
819
738
        }
824
743
       process, too. */
825
744
    if(p->environ[0] != NULL){
826
745
      for(char **e = environ; *e != NULL; e++){
827
 
        if(not add_environment(p, *e, false)){
 
746
        char *copy = strdup(*e);
 
747
        if(copy == NULL){
 
748
          perror("strdup");
 
749
          continue;
 
750
        }
 
751
        if(not add_environment(p, copy)){
828
752
          perror("add_environment");
829
753
        }
830
754
      }
832
756
    
833
757
    int pipefd[2];
834
758
    ret = pipe(pipefd);
835
 
    if(ret == -1){
 
759
    if (ret == -1){
836
760
      perror("pipe");
837
761
      exitstatus = EXIT_FAILURE;
838
762
      goto fallback;
851
775
      goto fallback;
852
776
    }
853
777
    /* Block SIGCHLD until process is safely in process list */
854
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
778
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
855
779
    if(ret < 0){
856
780
      perror("sigprocmask");
857
781
      exitstatus = EXIT_FAILURE;
858
782
      goto fallback;
859
783
    }
860
 
    /* Starting a new process to be watched */
 
784
    // Starting a new process to be watched
861
785
    pid_t pid = fork();
862
786
    if(pid == -1){
863
787
      perror("fork");
871
795
        perror("sigaction");
872
796
        _exit(EXIT_FAILURE);
873
797
      }
874
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
798
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
875
799
      if(ret < 0){
876
800
        perror("sigprocmask");
877
801
        _exit(EXIT_FAILURE);
878
802
      }
879
 
      
 
803
 
880
804
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
881
805
      if(ret == -1){
882
806
        perror("dup2");
905
829
    close(pipefd[1]);           /* Close unused write end of pipe */
906
830
    free(filename);
907
831
    plugin *new_plugin = getplugin(dirst->d_name);
908
 
    if(new_plugin == NULL){
 
832
    if (new_plugin == NULL){
909
833
      perror("getplugin");
910
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
834
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
835
      if(ret < 0){
912
836
        perror("sigprocmask");
913
837
      }
920
844
    
921
845
    /* Unblock SIGCHLD so signal handler can be run if this process
922
846
       has already completed */
923
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
847
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
924
848
    if(ret < 0){
925
849
      perror("sigprocmask");
926
850
      exitstatus = EXIT_FAILURE;
929
853
    
930
854
    FD_SET(new_plugin->fd, &rfds_all);
931
855
    
932
 
    if(maxfd < new_plugin->fd){
 
856
    if (maxfd < new_plugin->fd){
933
857
      maxfd = new_plugin->fd;
934
858
    }
 
859
    
935
860
  }
936
861
  
937
862
  closedir(dir);
938
863
  dir = NULL;
939
 
  free_plugin(getplugin(NULL));
940
 
  
 
864
 
941
865
  for(plugin *p = plugin_list; p != NULL; p = p->next){
942
866
    if(p->pid != 0){
943
867
      break;
948
872
      free_plugin_list();
949
873
    }
950
874
  }
951
 
  
 
875
 
952
876
  /* Main loop while running plugins exist */
953
877
  while(plugin_list){
954
878
    fd_set rfds = rfds_all;
955
879
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
956
 
    if(select_ret == -1){
 
880
    if (select_ret == -1){
957
881
      perror("select");
958
882
      exitstatus = EXIT_FAILURE;
959
883
      goto fallback;
960
884
    }
961
885
    /* OK, now either a process completed, or something can be read
962
886
       from one of them */
963
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
887
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
964
888
      /* Is this process completely done? */
965
 
      if(proc->completed and proc->eof){
 
889
      if(proc->eof and proc->completed){
966
890
        /* Only accept the plugin output if it exited cleanly */
967
891
        if(not WIFEXITED(proc->status)
968
892
           or WEXITSTATUS(proc->status) != 0){
970
894
 
971
895
          if(debug){
972
896
            if(WIFEXITED(proc->status)){
973
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
974
 
                      " %d\n", (intmax_t) (proc->pid),
 
897
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
898
                      (unsigned int) (proc->pid),
975
899
                      WEXITSTATUS(proc->status));
976
 
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
978
 
                      " %d\n", (intmax_t) (proc->pid),
 
900
            } else if(WIFSIGNALED(proc->status)) {
 
901
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
902
                      (unsigned int) (proc->pid),
979
903
                      WTERMSIG(proc->status));
980
904
            } else if(WCOREDUMP(proc->status)){
981
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
982
 
                      (intmax_t) (proc->pid));
 
905
              fprintf(stderr, "Plugin %d dumped core\n",
 
906
                      (unsigned int) (proc->pid));
983
907
            }
984
908
          }
985
909
          
993
917
            exitstatus = EXIT_FAILURE;
994
918
            goto fallback;
995
919
          }
996
 
          
997
 
          plugin *next_plugin = proc->next;
998
920
          free_plugin(proc);
999
 
          proc = next_plugin;
1000
 
          
1001
921
          /* We are done modifying process list, so unblock signal */
1002
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1003
 
                            NULL);
 
922
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
923
                             NULL);
1004
924
          if(ret < 0){
1005
925
            perror("sigprocmask");
1006
926
            exitstatus = EXIT_FAILURE;
1010
930
          if(plugin_list == NULL){
1011
931
            break;
1012
932
          }
1013
 
          
1014
933
          continue;
1015
934
        }
1016
935
        
1017
936
        /* This process exited nicely, so print its buffer */
1018
 
        
 
937
 
1019
938
        bool bret = print_out_password(proc->buffer,
1020
939
                                       proc->buffer_length);
1021
940
        if(not bret){
1028
947
      /* This process has not completed.  Does it have any output? */
1029
948
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1030
949
        /* This process had nothing to say at this time */
1031
 
        proc = proc->next;
1032
950
        continue;
1033
951
      }
1034
952
      /* Before reading, make the process' data buffer large enough */
1035
953
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1036
954
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1037
955
                               + (size_t) BUFFER_SIZE);
1038
 
        if(proc->buffer == NULL){
 
956
        if (proc->buffer == NULL){
1039
957
          perror("malloc");
1040
958
          exitstatus = EXIT_FAILURE;
1041
959
          goto fallback;
1043
961
        proc->buffer_size += BUFFER_SIZE;
1044
962
      }
1045
963
      /* Read from the process */
1046
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1047
 
                  BUFFER_SIZE);
1048
 
      if(sret < 0){
 
964
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
965
                 BUFFER_SIZE);
 
966
      if(ret < 0){
1049
967
        /* Read error from this process; ignore the error */
1050
 
        proc = proc->next;
1051
968
        continue;
1052
969
      }
1053
 
      if(sret == 0){
 
970
      if(ret == 0){
1054
971
        /* got EOF */
1055
972
        proc->eof = true;
1056
973
      } else {
1057
 
        proc->buffer_length += (size_t) sret;
 
974
        proc->buffer_length += (size_t) ret;
1058
975
      }
1059
976
    }
1060
977
  }
1068
985
    bool bret;
1069
986
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1070
987
    char *passwordbuffer = getpass("Password: ");
1071
 
    size_t len = strlen(passwordbuffer);
1072
 
    /* Strip trailing newline */
1073
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1074
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1075
 
      len--;
1076
 
    }
1077
 
    bret = print_out_password(passwordbuffer, len);
 
988
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1078
989
    if(not bret){
1079
990
      perror("print_out_password");
1080
991
      exitstatus = EXIT_FAILURE;
1087
998
    perror("sigaction");
1088
999
    exitstatus = EXIT_FAILURE;
1089
1000
  }
1090
 
  
 
1001
 
1091
1002
  if(custom_argv != NULL){
1092
1003
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1093
1004
      free(*arg);
1099
1010
    closedir(dir);
1100
1011
  }
1101
1012
  
1102
 
  /* Kill the processes */
 
1013
  /* Free the process list and kill the processes */
1103
1014
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1104
1015
    if(p->pid != 0){
1105
1016
      close(p->fd);
1118
1029
  if(errno != ECHILD){
1119
1030
    perror("wait");
1120
1031
  }
1121
 
  
 
1032
 
1122
1033
  free_plugin_list();
1123
1034
  
1124
1035
  free(plugindir);