/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: Björn Påhlsson
  • Date: 2008-09-05 19:30:21 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 176.
  • Revision ID: belorn@braxen-20080905193021-vie7b22ootg6r2t8
Several memory leaks detected by valgrind fixed
A bugg fixed that could cause seg fault.

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