/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: Björn Påhlsson
  • Date: 2008-08-31 16:03:21 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 132.
  • Revision ID: belorn@braxen-20080831160321-fgvlw43uu6qz7k06
Rearranged so plugins and processes is the same thing

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