/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
68
 
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
68
 
 
69
#define PDIR "/lib/mandos/plugins.d"
 
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
69
71
 
70
72
const char *argp_program_version = "plugin-runner 1.0";
71
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
72
74
 
73
 
struct process;
74
 
 
75
 
typedef struct process{
 
75
struct plugin;
 
76
 
 
77
typedef struct plugin{
 
78
  char *name;                   /* can be NULL or any plugin name */
 
79
  char **argv;
 
80
  int argc;
 
81
  char **environ;
 
82
  int envc;
 
83
  bool disabled;
 
84
 
 
85
  /* Variables used for running processes*/
76
86
  pid_t pid;
77
87
  int fd;
78
88
  char *buffer;
81
91
  bool eof;
82
92
  volatile bool completed;
83
93
  volatile int status;
84
 
  struct process *next;
85
 
} process;
86
 
 
87
 
typedef struct plugin{
88
 
  char *name;                   /* can be NULL or any plugin name */
89
 
  char **argv;
90
 
  int argc;
91
 
  char **environ;
92
 
  int envc;
93
 
  bool disabled;
94
94
  struct plugin *next;
95
95
} plugin;
96
96
 
97
 
static plugin *getplugin(char *name, plugin **plugin_list){
98
 
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
 
97
static plugin *plugin_list = NULL;
 
98
 
 
99
/* Gets a existing plugin based on name,
 
100
   or if none is found, creates a new one */
 
101
static plugin *getplugin(char *name){
 
102
  /* Check for exiting plugin with that name */
 
103
  for (plugin *p = plugin_list; p != NULL; p = p->next){
99
104
    if ((p->name == name)
100
105
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
106
      return p;
113
118
      return NULL;
114
119
    }
115
120
  }
116
 
  
 
121
 
117
122
  *new_plugin = (plugin) { .name = copy_name,
118
123
                           .argc = 1,
119
 
                           .envc = 0,
120
124
                           .disabled = false,
121
 
                           .next = *plugin_list };
 
125
                           .next = plugin_list };
122
126
  
123
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
124
128
  if (new_plugin->argv == NULL){
128
132
  }
129
133
  new_plugin->argv[0] = copy_name;
130
134
  new_plugin->argv[1] = NULL;
131
 
 
 
135
  
132
136
  new_plugin->environ = malloc(sizeof(char *));
133
137
  if(new_plugin->environ == NULL){
134
138
    free(copy_name);
137
141
    return NULL;
138
142
  }
139
143
  new_plugin->environ[0] = NULL;
 
144
  
140
145
  /* Append the new plugin to the list */
141
 
  *plugin_list = new_plugin;
 
146
  plugin_list = new_plugin;
142
147
  return new_plugin;
143
148
}
144
149
 
174
179
}
175
180
 
176
181
/* Add to a plugin's environment */
177
 
static bool add_environment(plugin *p, const char *def){
 
182
static bool add_environment(plugin *p, const char *def, bool replace){
178
183
  if(p == NULL){
179
184
    return false;
180
185
  }
 
186
  /* namelen = length of name of environment variable */
 
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
 
188
  /* Search for this environment variable */
 
189
  for(char **e = p->environ; *e != NULL; e++){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
 
191
      /* It already exists */
 
192
      if(replace){
 
193
        char *new = realloc(*e, strlen(def));
 
194
        if(new == NULL){
 
195
          return false;
 
196
        }
 
197
        *e = new;
 
198
        strcpy(*e, def);
 
199
      }
 
200
      return true;
 
201
    }
 
202
  }
181
203
  return add_to_char_array(def, &(p->environ), &(p->envc));
182
204
}
183
205
 
184
 
 
185
206
/*
186
207
 * Based on the example in the GNU LibC manual chapter 13.13 "File
187
208
 * Descriptor Flags".
198
219
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
199
220
}
200
221
 
201
 
process *process_list = NULL;
202
222
 
203
223
/* Mark processes as completed when they exit, and save their exit
204
224
   status. */
205
225
void handle_sigchld(__attribute__((unused)) int sig){
206
226
  while(true){
207
 
    process *proc = process_list;
 
227
    plugin *proc = plugin_list;
208
228
    int status;
209
229
    pid_t pid = waitpid(-1, &status, WNOHANG);
210
230
    if(pid == 0){
232
252
  }
233
253
}
234
254
 
 
255
/* Prints out a password to stdout */
235
256
bool print_out_password(const char *buffer, size_t length){
236
257
  ssize_t ret;
237
258
  if(length>0 and buffer[length-1] == '\n'){
247
268
  return true;
248
269
}
249
270
 
250
 
static void free_plugin_list(plugin *plugin_list){
251
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
252
 
    next = plugin_list->next;
253
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
254
 
      free(*arg);
255
 
    }
256
 
    free(plugin_list->argv);
257
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
258
 
      free(*env);
259
 
    }
260
 
    free(plugin_list->environ);
261
 
    free(plugin_list);
 
271
/* Removes and free a plugin from the plugin list */
 
272
static void free_plugin(plugin *plugin_node){
 
273
  
 
274
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
275
    free(*arg);
 
276
  }
 
277
  free(plugin_node->argv);
 
278
  for(char **env = plugin_node->environ; *env != NULL; env++){
 
279
    free(*env);
 
280
  }
 
281
  free(plugin_node->environ);
 
282
  free(plugin_node->buffer);
 
283
 
 
284
  /* Removes the plugin from the singly-linked list */
 
285
  if(plugin_node == plugin_list){
 
286
    /* First one - simple */
 
287
    plugin_list = plugin_list->next;
 
288
  } else {
 
289
    /* Second one or later */
 
290
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
291
      if(p->next == plugin_node){
 
292
        p->next = plugin_node->next;
 
293
        break;
 
294
      }
 
295
    }
 
296
  }
 
297
  
 
298
  free(plugin_node);
 
299
}
 
300
 
 
301
static void free_plugin_list(void){
 
302
  while(plugin_list != NULL){
 
303
    free_plugin(plugin_list);
262
304
  }
263
305
}
264
306
 
265
307
int main(int argc, char *argv[]){
266
 
  const char *plugindir = "/lib/mandos/plugins.d";
267
 
  const char *argfile = ARGFILE;
 
308
  char *plugindir = NULL;
 
309
  char *argfile = NULL;
268
310
  FILE *conffp;
269
311
  size_t d_name_len;
270
312
  DIR *dir = NULL;
302
344
    { .name = "global-options", .key = 'g',
303
345
      .arg = "OPTION[,OPTION[,...]]",
304
346
      .doc = "Options passed to all plugins" },
305
 
    { .name = "global-envs", .key = 'e',
 
347
    { .name = "global-env", .key = 'G',
306
348
      .arg = "VAR=value",
307
349
      .doc = "Environment variable passed to all plugins" },
308
350
    { .name = "options-for", .key = 'o',
309
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
310
352
      .doc = "Options passed only to specified plugin" },
311
 
    { .name = "envs-for", .key = 'f',
 
353
    { .name = "env-for", .key = 'E',
312
354
      .arg = "PLUGIN:ENV=value",
313
355
      .doc = "Environment variable passed to specified plugin" },
314
356
    { .name = "disable", .key = 'd',
315
357
      .arg = "PLUGIN",
316
358
      .doc = "Disable a specific plugin", .group = 1 },
 
359
    { .name = "enable", .key = 'e',
 
360
      .arg = "PLUGIN",
 
361
      .doc = "Enable a specific plugin", .group = 1 },
317
362
    { .name = "plugin-dir", .key = 128,
318
363
      .arg = "DIRECTORY",
319
364
      .doc = "Specify a different plugin directory", .group = 2 },
320
 
    { .name = "userid", .key = 129,
321
 
      .arg = "ID", .flags = 0,
322
 
      .doc = "User ID the plugins will run as", .group = 2 },
323
 
    { .name = "groupid", .key = 130,
324
 
      .arg = "ID", .flags = 0,
325
 
      .doc = "Group ID the plugins will run as", .group = 2 },
326
 
    { .name = "debug", .key = 131,
327
 
      .doc = "Debug mode", .group = 3 },
 
365
    { .name = "config-file", .key = 129,
 
366
      .arg = "FILE",
 
367
      .doc = "Specify a different configuration file", .group = 2 },
 
368
    { .name = "userid", .key = 130,
 
369
      .arg = "ID", .flags = 0,
 
370
      .doc = "User ID the plugins will run as", .group = 3 },
 
371
    { .name = "groupid", .key = 131,
 
372
      .arg = "ID", .flags = 0,
 
373
      .doc = "Group ID the plugins will run as", .group = 3 },
 
374
    { .name = "debug", .key = 132,
 
375
      .doc = "Debug mode", .group = 4 },
328
376
    { .name = NULL }
329
377
  };
330
378
  
331
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
332
 
    /* Get the INPUT argument from `argp_parse', which we know is a
333
 
       pointer to our plugin list pointer. */
334
 
    plugin **plugins = state->input;
 
379
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
380
                     struct argp_state *state) {
335
381
    switch (key) {
336
 
    case 'g':
 
382
    case 'g':                   /* --global-options */
337
383
      if (arg != NULL){
338
384
        char *p;
339
385
        while((p = strsep(&arg, ",")) != NULL){
340
386
          if(p[0] == '\0'){
341
387
            continue;
342
388
          }
343
 
          if(not add_argument(getplugin(NULL, plugins), p)){
 
389
          if(not add_argument(getplugin(NULL), p)){
344
390
            perror("add_argument");
345
391
            return ARGP_ERR_UNKNOWN;
346
392
          }
347
393
        }
348
394
      }
349
395
      break;
350
 
    case 'e':
 
396
    case 'G':                   /* --global-env */
351
397
      if(arg == NULL){
352
398
        break;
353
399
      }
356
402
        if(envdef == NULL){
357
403
          break;
358
404
        }
359
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
 
405
        if(not add_environment(getplugin(NULL), envdef, true)){
360
406
          perror("add_environment");
361
407
        }
362
408
      }
363
409
      break;
364
 
    case 'o':
 
410
    case 'o':                   /* --options-for */
365
411
      if (arg != NULL){
366
412
        char *p_name = strsep(&arg, ":");
367
413
        if(p_name[0] == '\0'){
377
423
            if(p[0] == '\0'){
378
424
              continue;
379
425
            }
380
 
            if(not add_argument(getplugin(p_name, plugins), p)){
 
426
            if(not add_argument(getplugin(p_name), p)){
381
427
              perror("add_argument");
382
428
              return ARGP_ERR_UNKNOWN;
383
429
            }
385
431
        }
386
432
      }
387
433
      break;
388
 
    case 'f':
 
434
    case 'E':                   /* --env-for */
389
435
      if(arg == NULL){
390
436
        break;
391
437
      }
399
445
          break;
400
446
        }
401
447
        envdef++;
402
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
448
        if(not add_environment(getplugin(p_name), envdef, true)){
403
449
          perror("add_environment");
404
450
        }
405
451
      }
406
452
      break;
407
 
    case 'd':
 
453
    case 'd':                   /* --disable */
408
454
      if (arg != NULL){
409
 
        plugin *p = getplugin(arg, plugins);
 
455
        plugin *p = getplugin(arg);
410
456
        if(p == NULL){
411
457
          return ARGP_ERR_UNKNOWN;
412
458
        }
413
459
        p->disabled = true;
414
460
      }
415
461
      break;
416
 
    case 128:
417
 
      plugindir = arg;
418
 
      break;
419
 
    case 129:
 
462
    case 'e':                   /* --enable */
 
463
      if (arg != NULL){
 
464
        plugin *p = getplugin(arg);
 
465
        if(p == NULL){
 
466
          return ARGP_ERR_UNKNOWN;
 
467
        }
 
468
        p->disabled = false;
 
469
      }
 
470
      break;
 
471
    case 128:                   /* --plugin-dir */
 
472
      plugindir = strdup(arg);
 
473
      if(plugindir == NULL){
 
474
        perror("strdup");
 
475
      }      
 
476
      break;
 
477
    case 129:                   /* --config-file */
 
478
      /* This is already done by parse_opt_config_file() */
 
479
      break;
 
480
    case 130:                   /* --userid */
420
481
      uid = (uid_t)strtol(arg, NULL, 10);
421
482
      break;
422
 
    case 130:
 
483
    case 131:                   /* --groupid */
423
484
      gid = (gid_t)strtol(arg, NULL, 10);
424
485
      break;
425
 
    case 131:
 
486
    case 132:                   /* --debug */
426
487
      debug = true;
427
488
      break;
428
489
    case ARGP_KEY_ARG:
436
497
    return 0;
437
498
  }
438
499
  
439
 
  plugin *plugin_list = NULL;
 
500
  /* This option parser is the same as parse_opt() above, except it
 
501
     ignores everything but the --config-file option. */
 
502
  error_t parse_opt_config_file (int key, char *arg,
 
503
                                 __attribute__((unused))
 
504
                                 struct argp_state *state) {
 
505
    switch (key) {
 
506
    case 'g':                   /* --global-options */
 
507
    case 'G':                   /* --global-env */
 
508
    case 'o':                   /* --options-for */
 
509
    case 'E':                   /* --env-for */
 
510
    case 'd':                   /* --disable */
 
511
    case 'e':                   /* --enable */
 
512
    case 128:                   /* --plugin-dir */
 
513
      break;
 
514
    case 129:                   /* --config-file */
 
515
      argfile = strdup(arg);
 
516
      if(argfile == NULL){
 
517
        perror("strdup");
 
518
      }
 
519
      break;      
 
520
    case 130:                   /* --userid */
 
521
    case 131:                   /* --groupid */
 
522
    case 132:                   /* --debug */
 
523
    case ARGP_KEY_ARG:
 
524
    case ARGP_KEY_END:
 
525
      break;
 
526
    default:
 
527
      return ARGP_ERR_UNKNOWN;
 
528
    }
 
529
    return 0;
 
530
  }
440
531
  
441
 
  struct argp argp = { .options = options, .parser = parse_opt,
442
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
532
  struct argp argp = { .options = options,
 
533
                       .parser = parse_opt_config_file,
 
534
                       .args_doc = "",
443
535
                       .doc = "Mandos plugin runner -- Run plugins" };
444
536
  
445
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
537
  /* Parse using the parse_opt_config_file in order to get the custom
 
538
     config file location, if any. */
 
539
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
446
540
  if (ret == ARGP_ERR_UNKNOWN){
447
541
    fprintf(stderr, "Unknown error while parsing arguments\n");
448
542
    exitstatus = EXIT_FAILURE;
449
543
    goto fallback;
450
544
  }
451
 
 
452
 
  conffp = fopen(argfile, "r");
 
545
  
 
546
  /* Reset to the normal argument parser */
 
547
  argp.parser = parse_opt;
 
548
  
 
549
  /* Open the configfile if available */
 
550
  if (argfile == NULL){
 
551
    conffp = fopen(AFILE, "r");
 
552
  } else {
 
553
    conffp = fopen(argfile, "r");
 
554
  }  
453
555
  if(conffp != NULL){
454
556
    char *org_line = NULL;
455
557
    char *p, *arg, *new_arg, *line;
467
569
    }
468
570
    custom_argv[0] = argv[0];
469
571
    custom_argv[1] = NULL;
470
 
    
 
572
 
 
573
    /* for each line in the config file, strip whitespace and ignore
 
574
       commented text */
471
575
    while(true){
472
576
      sret = getline(&org_line, &size, conffp);
473
577
      if(sret == -1){
481
585
          continue;
482
586
        }
483
587
        new_arg = strdup(p);
484
 
 
 
588
        if(new_arg == NULL){
 
589
          perror("strdup");
 
590
          exitstatus = EXIT_FAILURE;
 
591
          free(org_line);
 
592
          goto fallback;
 
593
        }
 
594
        
485
595
        custom_argc += 1;
486
596
        custom_argv = realloc(custom_argv, sizeof(char *)
487
597
                              * ((unsigned int) custom_argc + 1));
496
606
      }
497
607
    }
498
608
    free(org_line);
499
 
  } else{
 
609
  } else {
500
610
    /* Check for harmful errors and go to fallback. Other errors might
501
611
       not affect opening plugins */
502
612
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
505
615
      goto fallback;
506
616
    }
507
617
  }
508
 
 
 
618
  /* If there was any arguments from configuration file,
 
619
     pass them to parser as command arguments */
509
620
  if(custom_argv != NULL){
510
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
621
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
622
                      0, NULL);
511
623
    if (ret == ARGP_ERR_UNKNOWN){
512
624
      fprintf(stderr, "Unknown error while parsing arguments\n");
513
625
      exitstatus = EXIT_FAILURE;
515
627
    }
516
628
  }
517
629
  
 
630
  /* Parse actual command line arguments, to let them override the
 
631
     config file */
 
632
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
633
  if (ret == ARGP_ERR_UNKNOWN){
 
634
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
635
    exitstatus = EXIT_FAILURE;
 
636
    goto fallback;
 
637
  }
 
638
  
518
639
  if(debug){
519
640
    for(plugin *p = plugin_list; p != NULL; p=p->next){
520
641
      fprintf(stderr, "Plugin: %s has %d arguments\n",
529
650
    }
530
651
  }
531
652
  
 
653
  /* Strip permissions down to nobody */
532
654
  ret = setuid(uid);
533
655
  if (ret == -1){
534
656
    perror("setuid");
535
 
  }
536
 
  
 
657
  }  
537
658
  setgid(gid);
538
659
  if (ret == -1){
539
660
    perror("setgid");
540
661
  }
541
662
  
542
 
  dir = opendir(plugindir);
 
663
  if (plugindir == NULL){
 
664
    dir = opendir(PDIR);
 
665
  } else {
 
666
    dir = opendir(plugindir);
 
667
  }
 
668
  
543
669
  if(dir == NULL){
544
670
    perror("Could not open plugin dir");
545
671
    exitstatus = EXIT_FAILURE;
561
687
  
562
688
  FD_ZERO(&rfds_all);
563
689
  
 
690
  /* Read and execute any executable in the plugin directory*/
564
691
  while(true){
565
692
    dirst = readdir(dir);
566
693
    
567
 
    // All directory entries have been processed
 
694
    /* All directory entries have been processed */
568
695
    if(dirst == NULL){
569
696
      if (errno == EBADF){
570
697
        perror("readdir");
576
703
    
577
704
    d_name_len = strlen(dirst->d_name);
578
705
    
579
 
    // Ignore dotfiles, backup files and other junk
 
706
    /* Ignore dotfiles, backup files and other junk */
580
707
    {
581
708
      bool bad_name = false;
582
709
      
597
724
          break;
598
725
        }
599
726
      }
600
 
      
601
727
      if(bad_name){
602
728
        continue;
603
729
      }
604
 
      
605
730
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
606
731
        size_t suf_len = strlen(*suf);
607
732
        if((d_name_len >= suf_len)
634
759
      free(filename);
635
760
      continue;
636
761
    }
637
 
    
 
762
 
 
763
    /* Ignore non-executable files */
638
764
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
639
765
      if(debug){
640
766
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
643
769
      free(filename);
644
770
      continue;
645
771
    }
646
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
772
    
 
773
    plugin *p = getplugin(dirst->d_name);
647
774
    if(p == NULL){
648
775
      perror("getplugin");
649
776
      free(filename);
659
786
    }
660
787
    {
661
788
      /* Add global arguments to argument list for this plugin */
662
 
      plugin *g = getplugin(NULL, &plugin_list);
 
789
      plugin *g = getplugin(NULL);
663
790
      if(g != NULL){
664
791
        for(char **a = g->argv + 1; *a != NULL; a++){
665
792
          if(not add_argument(p, *a)){
668
795
        }
669
796
        /* Add global environment variables */
670
797
        for(char **e = g->environ; *e != NULL; e++){
671
 
          if(not add_environment(p, *e)){
 
798
          if(not add_environment(p, *e, false)){
672
799
            perror("add_environment");
673
800
          }
674
801
        }
679
806
       process, too. */
680
807
    if(p->environ[0] != NULL){
681
808
      for(char **e = environ; *e != NULL; e++){
682
 
        char *copy = strdup(*e);
683
 
        if(copy == NULL){
684
 
          perror("strdup");
685
 
          continue;
686
 
        }
687
 
        if(not add_environment(p, copy)){
 
809
        if(not add_environment(p, *e, false)){
688
810
          perror("add_environment");
689
811
        }
690
812
      }
697
819
      exitstatus = EXIT_FAILURE;
698
820
      goto fallback;
699
821
    }
 
822
    /* Ask OS to automatic close the pipe on exec */
700
823
    ret = set_cloexec_flag(pipefd[0]);
701
824
    if(ret < 0){
702
825
      perror("set_cloexec_flag");
716
839
      exitstatus = EXIT_FAILURE;
717
840
      goto fallback;
718
841
    }
719
 
    // Starting a new process to be watched
 
842
    /* Starting a new process to be watched */
720
843
    pid_t pid = fork();
721
844
    if(pid == -1){
722
845
      perror("fork");
760
883
      }
761
884
      /* no return */
762
885
    }
763
 
    /* parent process */
 
886
    /* Parent process */
 
887
    close(pipefd[1]);           /* Close unused write end of pipe */
764
888
    free(filename);
765
 
    close(pipefd[1]);           /* close unused write end of pipe */
766
 
    process *new_process = malloc(sizeof(process));
767
 
    if (new_process == NULL){
768
 
      perror("malloc");
 
889
    plugin *new_plugin = getplugin(dirst->d_name);
 
890
    if (new_plugin == NULL){
 
891
      perror("getplugin");
769
892
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
770
893
      if(ret < 0){
771
 
        perror("sigprocmask");
 
894
        perror("sigprocmask");
772
895
      }
773
896
      exitstatus = EXIT_FAILURE;
774
897
      goto fallback;
775
898
    }
776
899
    
777
 
    *new_process = (struct process){ .pid = pid,
778
 
                                     .fd = pipefd[0],
779
 
                                     .next = process_list };
780
 
    // List handling
781
 
    process_list = new_process;
 
900
    new_plugin->pid = pid;
 
901
    new_plugin->fd = pipefd[0];
 
902
    
782
903
    /* Unblock SIGCHLD so signal handler can be run if this process
783
904
       has already completed */
784
905
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
788
909
      goto fallback;
789
910
    }
790
911
    
791
 
    FD_SET(new_process->fd, &rfds_all);
 
912
    FD_SET(new_plugin->fd, &rfds_all);
792
913
    
793
 
    if (maxfd < new_process->fd){
794
 
      maxfd = new_process->fd;
 
914
    if (maxfd < new_plugin->fd){
 
915
      maxfd = new_plugin->fd;
795
916
    }
796
917
    
797
918
  }
798
919
  
799
 
  free_plugin_list(plugin_list);
800
 
  plugin_list = NULL;
801
 
  
802
920
  closedir(dir);
803
921
  dir = NULL;
804
 
    
805
 
  if (process_list == NULL){
806
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
807
 
            " directory?\n");
808
 
    process_list = NULL;
 
922
 
 
923
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
924
    if(p->pid != 0){
 
925
      break;
 
926
    }
 
927
    if(p->next == NULL){
 
928
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
929
              " directory?\n");
 
930
      free_plugin_list();
 
931
    }
809
932
  }
810
 
  while(process_list){
 
933
 
 
934
  /* Main loop while running plugins exist */
 
935
  while(plugin_list){
811
936
    fd_set rfds = rfds_all;
812
937
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
813
938
    if (select_ret == -1){
817
942
    }
818
943
    /* OK, now either a process completed, or something can be read
819
944
       from one of them */
820
 
    for(process *proc = process_list; proc ; proc = proc->next){
 
945
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
821
946
      /* Is this process completely done? */
822
947
      if(proc->eof and proc->completed){
823
948
        /* Only accept the plugin output if it exited cleanly */
824
949
        if(not WIFEXITED(proc->status)
825
950
           or WEXITSTATUS(proc->status) != 0){
826
951
          /* Bad exit by plugin */
 
952
 
827
953
          if(debug){
828
954
            if(WIFEXITED(proc->status)){
829
955
              fprintf(stderr, "Plugin %u exited with status %d\n",
838
964
                      (unsigned int) (proc->pid));
839
965
            }
840
966
          }
 
967
          
841
968
          /* Remove the plugin */
842
969
          FD_CLR(proc->fd, &rfds_all);
 
970
 
843
971
          /* Block signal while modifying process_list */
844
972
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
845
973
          if(ret < 0){
847
975
            exitstatus = EXIT_FAILURE;
848
976
            goto fallback;
849
977
          }
850
 
          /* Delete this process entry from the list */
851
 
          if(process_list == proc){
852
 
            /* First one - simple */
853
 
            process_list = proc->next;
854
 
          } else {
855
 
            /* Second one or later */
856
 
            for(process *p = process_list; p != NULL; p = p->next){
857
 
              if(p->next == proc){
858
 
                p->next = proc->next;
859
 
                break;
860
 
              }
861
 
            }
862
 
          }
 
978
          free_plugin(proc);
863
979
          /* We are done modifying process list, so unblock signal */
864
980
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
865
981
                             NULL);
866
982
          if(ret < 0){
867
983
            perror("sigprocmask");
868
 
          }
869
 
          free(proc->buffer);
870
 
          free(proc);
871
 
          /* We deleted this process from the list, so we can't go
872
 
             proc->next.  Therefore, start over from the beginning of
873
 
             the process list */
874
 
          break;
 
984
            exitstatus = EXIT_FAILURE;
 
985
            goto fallback;
 
986
          }
 
987
          
 
988
          if(plugin_list == NULL){
 
989
            break;
 
990
          }
 
991
          continue;
875
992
        }
 
993
        
876
994
        /* This process exited nicely, so print its buffer */
877
995
 
878
996
        bool bret = print_out_password(proc->buffer,
883
1001
        }
884
1002
        goto fallback;
885
1003
      }
 
1004
      
886
1005
      /* This process has not completed.  Does it have any output? */
887
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
888
1007
        /* This process had nothing to say at this time */
918
1037
 
919
1038
 fallback:
920
1039
  
921
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
1040
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
922
1041
    /* Fallback if all plugins failed, none are found or an error
923
1042
       occured */
924
1043
    bool bret;
944
1063
    }
945
1064
    free(custom_argv);
946
1065
  }
947
 
  free_plugin_list(plugin_list);
948
1066
  
949
1067
  if(dir != NULL){
950
1068
    closedir(dir);
951
1069
  }
952
1070
  
953
1071
  /* Free the process list and kill the processes */
954
 
  for(process *next; process_list != NULL; process_list = next){
955
 
    next = process_list->next;
956
 
    close(process_list->fd);
957
 
    ret = kill(process_list->pid, SIGTERM);
958
 
    if(ret == -1 and errno != ESRCH){
959
 
      /* set-uid proccesses migth not get closed */
960
 
      perror("kill");
 
1072
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
1073
    if(p->pid != 0){
 
1074
      close(p->fd);
 
1075
      ret = kill(p->pid, SIGTERM);
 
1076
      if(ret == -1 and errno != ESRCH){
 
1077
        /* Set-uid proccesses might not get closed */
 
1078
        perror("kill");
 
1079
      }
961
1080
    }
962
 
    free(process_list->buffer);
963
 
    free(process_list);
964
1081
  }
965
1082
  
966
1083
  /* Wait for any remaining child processes to terminate */
970
1087
  if(errno != ECHILD){
971
1088
    perror("wait");
972
1089
  }
 
1090
 
 
1091
  free_plugin_list();
 
1092
  
 
1093
  free(plugindir);
 
1094
  free(argfile);
973
1095
  
974
1096
  return exitstatus;
975
1097
}