/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: 2009-02-08 01:50:24 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: belorn@braxen-20090208015024-jewaulcqtxvbopq6
Fixed a bug in fallback handling

Show diffs side-by-side

added added

removed removed

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