/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

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