/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

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