/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-09-01 08:29:23 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901082923-i2liq6t7warmu9xe
* mandos.xml: Enclose "RAM" with <acronym>.
* overview.xml: - '' -

* plugin-runner.xml (DESCRIPTION): Improved wording.
  (PURPOSE): New section.
  (OPTIONS): Improved wording.
  (OVERVIEW, PLUGINS): New section.
  (FALLBACK): New empty placeholder section.

* plugins.d/password-prompt.xml: Enclose "RAM" with <acronym>.

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))
 
360
                     struct argp_state *state) {
 
361
    /* Get the INPUT argument from `argp_parse', which we know is a
 
362
       pointer to our plugin list pointer. */
 
363
    switch (key) {
383
364
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
385
 
        char *plugin_option;
386
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
387
 
          if(plugin_option[0] == '\0'){
 
365
      if (arg != NULL){
 
366
        char *p;
 
367
        while((p = strsep(&arg, ",")) != NULL){
 
368
          if(p[0] == '\0'){
388
369
            continue;
389
370
          }
390
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
371
          if(not add_argument(getplugin(NULL), p)){
391
372
            perror("add_argument");
392
373
            return ARGP_ERR_UNKNOWN;
393
374
          }
394
375
        }
395
376
      }
396
377
      break;
397
 
    case 'G':                   /* --global-env */
 
378
    case 'e':                   /* --global-envs */
398
379
      if(arg == NULL){
399
380
        break;
400
381
      }
401
 
      if(not add_environment(getplugin(NULL), arg, true)){
402
 
        perror("add_environment");
 
382
      {
 
383
        char *envdef = strdup(arg);
 
384
        if(envdef == NULL){
 
385
          break;
 
386
        }
 
387
        if(not add_environment(getplugin(NULL), envdef)){
 
388
          perror("add_environment");
 
389
        }
403
390
      }
404
391
      break;
405
392
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
407
 
        char *plugin_name = strsep(&arg, ":");
408
 
        if(plugin_name[0] == '\0'){
409
 
          break;
410
 
        }
411
 
        char *plugin_option;
412
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
413
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
414
 
            perror("add_argument");
415
 
            return ARGP_ERR_UNKNOWN;
 
393
      if (arg != NULL){
 
394
        char *p_name = strsep(&arg, ":");
 
395
        if(p_name[0] == '\0'){
 
396
          break;
 
397
        }
 
398
        char *opt = strsep(&arg, ":");
 
399
        if(opt[0] == '\0'){
 
400
          break;
 
401
        }
 
402
        if(opt != NULL){
 
403
          char *p;
 
404
          while((p = strsep(&opt, ",")) != NULL){
 
405
            if(p[0] == '\0'){
 
406
              continue;
 
407
            }
 
408
            if(not add_argument(getplugin(p_name), p)){
 
409
              perror("add_argument");
 
410
              return ARGP_ERR_UNKNOWN;
 
411
            }
416
412
          }
417
413
        }
418
414
      }
419
415
      break;
420
 
    case 'E':                   /* --env-for */
 
416
    case 'f':                   /* --envs-for */
421
417
      if(arg == NULL){
422
418
        break;
423
419
      }
426
422
        if(envdef == NULL){
427
423
          break;
428
424
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
425
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
426
        if(p_name == NULL){
 
427
          break;
 
428
        }
 
429
        envdef++;
 
430
        if(not add_environment(getplugin(p_name), envdef)){
431
431
          perror("add_environment");
432
432
        }
433
433
      }
434
434
      break;
435
435
    case 'd':                   /* --disable */
436
 
      if(arg != NULL){
 
436
      if (arg != NULL){
437
437
        plugin *p = getplugin(arg);
438
438
        if(p == NULL){
439
439
          return ARGP_ERR_UNKNOWN;
441
441
        p->disabled = true;
442
442
      }
443
443
      break;
444
 
    case 'e':                   /* --enable */
445
 
      if(arg != NULL){
446
 
        plugin *p = getplugin(arg);
447
 
        if(p == NULL){
448
 
          return ARGP_ERR_UNKNOWN;
449
 
        }
450
 
        p->disabled = false;
451
 
      }
452
 
      break;
453
444
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
445
      plugindir = strdup(arg);
456
446
      if(plugindir == NULL){
457
447
        perror("strdup");
458
448
      }      
459
449
      break;
460
450
    case 129:                   /* --config-file */
461
 
      /* This is already done by parse_opt_config_file() */
462
 
      break;
463
 
    case 130:                   /* --userid */
464
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
465
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
466
 
         or arg[numchars] != '\0'){
467
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
468
 
                PRIdMAX "\n", arg, (intmax_t)uid);
469
 
      } else {
470
 
        uid = (uid_t)tmpmax;
471
 
      }
472
 
      break;
473
 
    case 131:                   /* --groupid */
474
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
475
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
476
 
         or arg[numchars] != '\0'){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
478
 
                PRIdMAX "\n", arg, (intmax_t)gid);
479
 
      } else {
480
 
        gid = (gid_t)tmpmax;
481
 
      }
482
 
      break;
483
 
    case 132:                   /* --debug */
484
 
      debug = true;
485
 
      break;
486
 
/*
487
 
 * When adding more options before this line, remember to also add a
488
 
 * "case" to the "parse_opt_config_file" function below.
489
 
 */
490
 
    case ARGP_KEY_ARG:
491
 
      /* Cryptsetup always passes an argument, which is an empty
492
 
         string if "none" was specified in /etc/crypttab.  So if
493
 
         argument was empty, we ignore it silently. */
494
 
      if(arg[0] != '\0'){
495
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
496
 
      }
497
 
      break;
498
 
    case ARGP_KEY_END:
499
 
      break;
500
 
    default:
501
 
      return ARGP_ERR_UNKNOWN;
502
 
    }
503
 
    return 0;
504
 
  }
505
 
  
506
 
  /* This option parser is the same as parse_opt() above, except it
507
 
     ignores everything but the --config-file option. */
508
 
  error_t parse_opt_config_file(int key, char *arg,
509
 
                                __attribute__((unused))
510
 
                                struct argp_state *state){
511
 
    switch(key){
512
 
    case 'g':                   /* --global-options */
513
 
    case 'G':                   /* --global-env */
514
 
    case 'o':                   /* --options-for */
515
 
    case 'E':                   /* --env-for */
516
 
    case 'd':                   /* --disable */
517
 
    case 'e':                   /* --enable */
518
 
    case 128:                   /* --plugin-dir */
519
 
      break;
520
 
    case 129:                   /* --config-file */
521
 
      free(argfile);
522
451
      argfile = strdup(arg);
523
452
      if(argfile == NULL){
524
453
        perror("strdup");
525
454
      }
526
455
      break;      
527
456
    case 130:                   /* --userid */
 
457
      uid = (uid_t)strtol(arg, NULL, 10);
 
458
      break;
528
459
    case 131:                   /* --groupid */
 
460
      gid = (gid_t)strtol(arg, NULL, 10);
 
461
      break;
529
462
    case 132:                   /* --debug */
 
463
      debug = true;
 
464
      break;
530
465
    case ARGP_KEY_ARG:
 
466
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
467
      break;
531
468
    case ARGP_KEY_END:
532
469
      break;
533
470
    default:
536
473
    return 0;
537
474
  }
538
475
  
539
 
  struct argp argp = { .options = options,
540
 
                       .parser = parse_opt_config_file,
541
 
                       .args_doc = "",
 
476
  struct argp argp = { .options = options, .parser = parse_opt,
 
477
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
542
478
                       .doc = "Mandos plugin runner -- Run plugins" };
543
479
  
544
 
  /* Parse using parse_opt_config_file() in order to get the custom
545
 
     config file location, if any. */
546
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
547
 
  if(ret == ARGP_ERR_UNKNOWN){
 
480
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
481
  if (ret == ARGP_ERR_UNKNOWN){
548
482
    fprintf(stderr, "Unknown error while parsing arguments\n");
549
483
    exitstatus = EXIT_FAILURE;
550
484
    goto fallback;
551
485
  }
552
 
  
553
 
  /* Reset to the normal argument parser */
554
 
  argp.parser = parse_opt;
555
 
  
556
 
  /* Open the configfile if available */
557
 
  if(argfile == NULL){
 
486
 
 
487
  /* Opens the configfile if aviable */
 
488
  if (argfile == NULL){
558
489
    conffp = fopen(AFILE, "r");
559
490
  } else {
560
491
    conffp = fopen(argfile, "r");
563
494
    char *org_line = NULL;
564
495
    char *p, *arg, *new_arg, *line;
565
496
    size_t size = 0;
 
497
    ssize_t sret;
566
498
    const char whitespace_delims[] = " \r\t\f\v\n";
567
499
    const char comment_delim[] = "#";
568
500
 
612
544
      }
613
545
    }
614
546
    free(org_line);
615
 
  } else {
 
547
  } else{
616
548
    /* Check for harmful errors and go to fallback. Other errors might
617
549
       not affect opening plugins */
618
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
550
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
619
551
      perror("fopen");
620
552
      exitstatus = EXIT_FAILURE;
621
553
      goto fallback;
624
556
  /* If there was any arguments from configuration file,
625
557
     pass them to parser as command arguments */
626
558
  if(custom_argv != NULL){
627
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
628
 
                     0, NULL);
629
 
    if(ret == ARGP_ERR_UNKNOWN){
 
559
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
560
    if (ret == ARGP_ERR_UNKNOWN){
630
561
      fprintf(stderr, "Unknown error while parsing arguments\n");
631
562
      exitstatus = EXIT_FAILURE;
632
563
      goto fallback;
633
564
    }
634
565
  }
635
566
  
636
 
  /* Parse actual command line arguments, to let them override the
637
 
     config file */
638
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
639
 
  if(ret == ARGP_ERR_UNKNOWN){
640
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
641
 
    exitstatus = EXIT_FAILURE;
642
 
    goto fallback;
643
 
  }
644
 
  
645
567
  if(debug){
646
568
    for(plugin *p = plugin_list; p != NULL; p=p->next){
647
569
      fprintf(stderr, "Plugin: %s has %d arguments\n",
649
571
      for(char **a = p->argv; *a != NULL; a++){
650
572
        fprintf(stderr, "\tArg: %s\n", *a);
651
573
      }
652
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
574
      fprintf(stderr, "...and %u environment variables\n", p->envc);
653
575
      for(char **a = p->environ; *a != NULL; a++){
654
576
        fprintf(stderr, "\t%s\n", *a);
655
577
      }
656
578
    }
657
579
  }
658
 
  
 
580
 
659
581
  /* Strip permissions down to nobody */
 
582
  ret = setuid(uid);
 
583
  if (ret == -1){
 
584
    perror("setuid");
 
585
  }  
660
586
  setgid(gid);
661
 
  if(ret == -1){
 
587
  if (ret == -1){
662
588
    perror("setgid");
663
589
  }
664
 
  ret = setuid(uid);
665
 
  if(ret == -1){
666
 
    perror("setuid");
667
 
  }
668
 
  
669
 
  if(plugindir == NULL){
 
590
 
 
591
  if (plugindir == NULL){
670
592
    dir = opendir(PDIR);
671
593
  } else {
672
594
    dir = opendir(plugindir);
692
614
  }
693
615
  
694
616
  FD_ZERO(&rfds_all);
695
 
  
 
617
 
696
618
  /* Read and execute any executable in the plugin directory*/
697
619
  while(true){
698
620
    dirst = readdir(dir);
699
621
    
700
 
    /* All directory entries have been processed */
 
622
    // All directory entries have been processed
701
623
    if(dirst == NULL){
702
 
      if(errno == EBADF){
 
624
      if (errno == EBADF){
703
625
        perror("readdir");
704
626
        exitstatus = EXIT_FAILURE;
705
627
        goto fallback;
709
631
    
710
632
    d_name_len = strlen(dirst->d_name);
711
633
    
712
 
    /* Ignore dotfiles, backup files and other junk */
 
634
    // Ignore dotfiles, backup files and other junk
713
635
    {
714
636
      bool bad_name = false;
715
637
      
717
639
      
718
640
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
719
641
                                           ".dpkg-old",
720
 
                                           ".dpkg-bak",
721
642
                                           ".dpkg-divert", NULL };
722
643
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
723
644
        size_t pre_len = strlen(*pre);
754
675
    }
755
676
 
756
677
    char *filename;
757
 
    if(plugindir == NULL){
758
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
759
 
    } else {
760
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
761
 
    }
 
678
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
762
679
    if(ret < 0){
763
680
      perror("asprintf");
764
681
      continue;
765
682
    }
766
683
    
767
684
    ret = stat(filename, &st);
768
 
    if(ret == -1){
 
685
    if (ret == -1){
769
686
      perror("stat");
770
687
      free(filename);
771
688
      continue;
772
689
    }
773
690
 
774
691
    /* Ignore non-executable files */
775
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
692
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
776
693
      if(debug){
777
694
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
778
695
                " with bad type or mode\n", filename);
806
723
        }
807
724
        /* Add global environment variables */
808
725
        for(char **e = g->environ; *e != NULL; e++){
809
 
          if(not add_environment(p, *e, false)){
 
726
          if(not add_environment(p, *e)){
810
727
            perror("add_environment");
811
728
          }
812
729
        }
817
734
       process, too. */
818
735
    if(p->environ[0] != NULL){
819
736
      for(char **e = environ; *e != NULL; e++){
820
 
        if(not add_environment(p, *e, false)){
 
737
        char *copy = strdup(*e);
 
738
        if(copy == NULL){
 
739
          perror("strdup");
 
740
          continue;
 
741
        }
 
742
        if(not add_environment(p, copy)){
821
743
          perror("add_environment");
822
744
        }
823
745
      }
825
747
    
826
748
    int pipefd[2];
827
749
    ret = pipe(pipefd);
828
 
    if(ret == -1){
 
750
    if (ret == -1){
829
751
      perror("pipe");
830
752
      exitstatus = EXIT_FAILURE;
831
753
      goto fallback;
844
766
      goto fallback;
845
767
    }
846
768
    /* Block SIGCHLD until process is safely in process list */
847
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
769
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
848
770
    if(ret < 0){
849
771
      perror("sigprocmask");
850
772
      exitstatus = EXIT_FAILURE;
851
773
      goto fallback;
852
774
    }
853
 
    /* Starting a new process to be watched */
 
775
    // Starting a new process to be watched
854
776
    pid_t pid = fork();
855
777
    if(pid == -1){
856
778
      perror("fork");
864
786
        perror("sigaction");
865
787
        _exit(EXIT_FAILURE);
866
788
      }
867
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
789
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
868
790
      if(ret < 0){
869
791
        perror("sigprocmask");
870
792
        _exit(EXIT_FAILURE);
871
793
      }
872
 
      
 
794
 
873
795
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
874
796
      if(ret == -1){
875
797
        perror("dup2");
898
820
    close(pipefd[1]);           /* Close unused write end of pipe */
899
821
    free(filename);
900
822
    plugin *new_plugin = getplugin(dirst->d_name);
901
 
    if(new_plugin == NULL){
 
823
    if (new_plugin == NULL){
902
824
      perror("getplugin");
903
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
825
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
904
826
      if(ret < 0){
905
827
        perror("sigprocmask");
906
828
      }
913
835
    
914
836
    /* Unblock SIGCHLD so signal handler can be run if this process
915
837
       has already completed */
916
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
838
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
917
839
    if(ret < 0){
918
840
      perror("sigprocmask");
919
841
      exitstatus = EXIT_FAILURE;
922
844
    
923
845
    FD_SET(new_plugin->fd, &rfds_all);
924
846
    
925
 
    if(maxfd < new_plugin->fd){
 
847
    if (maxfd < new_plugin->fd){
926
848
      maxfd = new_plugin->fd;
927
849
    }
 
850
    
928
851
  }
929
852
  
930
853
  closedir(dir);
931
854
  dir = NULL;
932
 
  free_plugin(getplugin(NULL));
933
 
  
 
855
 
934
856
  for(plugin *p = plugin_list; p != NULL; p = p->next){
935
857
    if(p->pid != 0){
936
858
      break;
941
863
      free_plugin_list();
942
864
    }
943
865
  }
944
 
  
 
866
 
945
867
  /* Main loop while running plugins exist */
946
868
  while(plugin_list){
947
869
    fd_set rfds = rfds_all;
948
870
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
949
 
    if(select_ret == -1){
 
871
    if (select_ret == -1){
950
872
      perror("select");
951
873
      exitstatus = EXIT_FAILURE;
952
874
      goto fallback;
953
875
    }
954
876
    /* OK, now either a process completed, or something can be read
955
877
       from one of them */
956
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
878
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
957
879
      /* Is this process completely done? */
958
 
      if(proc->completed and proc->eof){
 
880
      if(proc->eof and proc->completed){
959
881
        /* Only accept the plugin output if it exited cleanly */
960
882
        if(not WIFEXITED(proc->status)
961
883
           or WEXITSTATUS(proc->status) != 0){
963
885
 
964
886
          if(debug){
965
887
            if(WIFEXITED(proc->status)){
966
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
967
 
                      " status %d\n", proc->name,
968
 
                      (intmax_t) (proc->pid),
 
888
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
889
                      (unsigned int) (proc->pid),
969
890
                      WEXITSTATUS(proc->status));
970
 
            } else if(WIFSIGNALED(proc->status)){
971
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
973
 
                      (intmax_t) (proc->pid),
 
891
            } else if(WIFSIGNALED(proc->status)) {
 
892
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
893
                      (unsigned int) (proc->pid),
974
894
                      WTERMSIG(proc->status));
975
895
            } else if(WCOREDUMP(proc->status)){
976
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
896
              fprintf(stderr, "Plugin %d dumped core\n",
 
897
                      (unsigned int) (proc->pid));
978
898
            }
979
899
          }
980
900
          
988
908
            exitstatus = EXIT_FAILURE;
989
909
            goto fallback;
990
910
          }
991
 
          
992
 
          plugin *next_plugin = proc->next;
993
911
          free_plugin(proc);
994
 
          proc = next_plugin;
995
 
          
996
912
          /* We are done modifying process list, so unblock signal */
997
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
998
 
                            NULL);
 
913
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
914
                             NULL);
999
915
          if(ret < 0){
1000
916
            perror("sigprocmask");
1001
917
            exitstatus = EXIT_FAILURE;
1005
921
          if(plugin_list == NULL){
1006
922
            break;
1007
923
          }
1008
 
          
1009
924
          continue;
1010
925
        }
1011
926
        
1012
927
        /* This process exited nicely, so print its buffer */
1013
 
        
 
928
 
1014
929
        bool bret = print_out_password(proc->buffer,
1015
930
                                       proc->buffer_length);
1016
931
        if(not bret){
1023
938
      /* This process has not completed.  Does it have any output? */
1024
939
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1025
940
        /* This process had nothing to say at this time */
1026
 
        proc = proc->next;
1027
941
        continue;
1028
942
      }
1029
943
      /* Before reading, make the process' data buffer large enough */
1030
944
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1031
945
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1032
946
                               + (size_t) BUFFER_SIZE);
1033
 
        if(proc->buffer == NULL){
 
947
        if (proc->buffer == NULL){
1034
948
          perror("malloc");
1035
949
          exitstatus = EXIT_FAILURE;
1036
950
          goto fallback;
1038
952
        proc->buffer_size += BUFFER_SIZE;
1039
953
      }
1040
954
      /* Read from the process */
1041
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1042
 
                  BUFFER_SIZE);
1043
 
      if(sret < 0){
 
955
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
956
                 BUFFER_SIZE);
 
957
      if(ret < 0){
1044
958
        /* Read error from this process; ignore the error */
1045
 
        proc = proc->next;
1046
959
        continue;
1047
960
      }
1048
 
      if(sret == 0){
 
961
      if(ret == 0){
1049
962
        /* got EOF */
1050
963
        proc->eof = true;
1051
964
      } else {
1052
 
        proc->buffer_length += (size_t) sret;
 
965
        proc->buffer_length += (size_t) ret;
1053
966
      }
1054
967
    }
1055
968
  }
1063
976
    bool bret;
1064
977
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1065
978
    char *passwordbuffer = getpass("Password: ");
1066
 
    size_t len = strlen(passwordbuffer);
1067
 
    /* Strip trailing newline */
1068
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1069
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1070
 
      len--;
1071
 
    }
1072
 
    bret = print_out_password(passwordbuffer, len);
 
979
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1073
980
    if(not bret){
1074
981
      perror("print_out_password");
1075
982
      exitstatus = EXIT_FAILURE;
1082
989
    perror("sigaction");
1083
990
    exitstatus = EXIT_FAILURE;
1084
991
  }
1085
 
  
 
992
 
1086
993
  if(custom_argv != NULL){
1087
994
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1088
995
      free(*arg);
1094
1001
    closedir(dir);
1095
1002
  }
1096
1003
  
1097
 
  /* Kill the processes */
 
1004
  /* Free the process list and kill the processes */
1098
1005
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1099
1006
    if(p->pid != 0){
1100
1007
      close(p->fd);
1113
1020
  if(errno != ECHILD){
1114
1021
    perror("wait");
1115
1022
  }
1116
 
  
 
1023
 
1117
1024
  free_plugin_list();
1118
1025
  
1119
1026
  free(plugindir);