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