/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-29 05:53:59 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080829055359-wkdasnyxtylmnxus
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
                        "&COMMANDNAME;".

* plugins.d/password-prompt.c (main): Improved some documentation
                                      strings.  Do perror() of
                                      tcgetattr() fails.  Add debug
                                      output if interrupted by signal.
                                      Loop over write() instead of
                                      using fwrite() when outputting
                                      password.  Add debug output if
                                      getline() returns 0, unless it
                                      was caused by a signal.  Add
                                      exit status code to debug
                                      output.

* plugins.d/password-prompt.xml: Changed all single quotes to double
                                 quotes for consistency.  Removed
                                 <?xml-stylesheet>.
  (ENTITY TIMESTAMP): New.  Automatically updated by Emacs time-stamp
                      by using Emacs local variables.
  (/refentry/refentryinfo/title): Changed to "Mandos Manual".
  (/refentry/refentryinfo/productname): Changed to "Mandos".
  (/refentry/refentryinfo/date): New; set to "&TIMESTAMP;".
  (/refentry/refentryinfo/copyright): Split copyright holders.
  (/refentry/refnamediv/refpurpose): Improved wording.
  (SYNOPSIS): Fix to use correct markup.  Add short options.
  (DESCRIPTION, OPTIONS): Improved wording.
  (OPTIONS): Improved wording.  Use more correct markup.  Document
             short options.
  (EXIT STATUS): Add text.
  (ENVIRONMENT): Document use of "cryptsource" and "crypttarget".
  (FILES): REMOVED.
  (BUGS): Add text.
  (EXAMPLE): Added some examples.
  (SECURITY): Added text.
  (SEE ALSO): Remove reference to mandos(8).  Add reference to
              crypttab(5).

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 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
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;
118
115
      return NULL;
119
116
    }
120
117
  }
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));
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){
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
372
        if(p_name[0] == '\0'){
423
382
            if(p[0] == '\0'){
424
383
              continue;
425
384
            }
426
 
            if(not add_argument(getplugin(p_name), p)){
 
385
            if(not add_argument(getplugin(p_name, plugins), p)){
427
386
              perror("add_argument");
428
387
              return ARGP_ERR_UNKNOWN;
429
388
            }
431
390
        }
432
391
      }
433
392
      break;
434
 
    case 'E':                   /* --env-for */
 
393
    case 'f':
435
394
      if(arg == NULL){
436
395
        break;
437
396
      }
445
404
          break;
446
405
        }
447
406
        envdef++;
448
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
407
        if(not add_environment(getplugin(p_name, plugins), envdef)){
449
408
          perror("add_environment");
450
409
        }
451
410
      }
452
411
      break;
453
 
    case 'd':                   /* --disable */
 
412
    case 'd':
454
413
      if (arg != NULL){
455
 
        plugin *p = getplugin(arg);
 
414
        plugin *p = getplugin(arg, plugins);
456
415
        if(p == NULL){
457
416
          return ARGP_ERR_UNKNOWN;
458
417
        }
459
418
        p->disabled = true;
460
419
      }
461
420
      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 */
 
421
    case 128:
472
422
      plugindir = strdup(arg);
473
423
      if(plugindir == NULL){
474
424
        perror("strdup");
475
425
      }      
476
426
      break;
477
 
    case 129:                   /* --config-file */
478
 
      /* This is already done by parse_opt_config_file() */
479
 
      break;
480
 
    case 130:                   /* --userid */
 
427
    case 129:
 
428
      argfile = strdup(arg);
 
429
      if(argfile == NULL){
 
430
        perror("strdup");
 
431
      }
 
432
      break;      
 
433
    case 130:
481
434
      uid = (uid_t)strtol(arg, NULL, 10);
482
435
      break;
483
 
    case 131:                   /* --groupid */
 
436
    case 131:
484
437
      gid = (gid_t)strtol(arg, NULL, 10);
485
438
      break;
486
 
    case 132:                   /* --debug */
 
439
    case 132:
487
440
      debug = true;
488
441
      break;
489
442
    case ARGP_KEY_ARG:
497
450
    return 0;
498
451
  }
499
452
  
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
 
  }
 
453
  plugin *plugin_list = NULL;
531
454
  
532
 
  struct argp argp = { .options = options,
533
 
                       .parser = parse_opt_config_file,
534
 
                       .args_doc = "",
 
455
  struct argp argp = { .options = options, .parser = parse_opt,
 
456
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
535
457
                       .doc = "Mandos plugin runner -- Run plugins" };
536
458
  
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);
 
459
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
540
460
  if (ret == ARGP_ERR_UNKNOWN){
541
461
    fprintf(stderr, "Unknown error while parsing arguments\n");
542
462
    exitstatus = EXIT_FAILURE;
543
463
    goto fallback;
544
464
  }
545
 
  
546
 
  /* Reset to the normal argument parser */
547
 
  argp.parser = parse_opt;
548
 
  
549
 
  /* Open the configfile if available */
 
465
 
550
466
  if (argfile == NULL){
551
467
    conffp = fopen(AFILE, "r");
552
468
  } else {
553
469
    conffp = fopen(argfile, "r");
554
 
  }  
 
470
  }
 
471
  
555
472
  if(conffp != NULL){
556
473
    char *org_line = NULL;
557
474
    char *p, *arg, *new_arg, *line;
569
486
    }
570
487
    custom_argv[0] = argv[0];
571
488
    custom_argv[1] = NULL;
572
 
 
573
 
    /* for each line in the config file, strip whitespace and ignore
574
 
       commented text */
 
489
    
575
490
    while(true){
576
491
      sret = getline(&org_line, &size, conffp);
577
492
      if(sret == -1){
606
521
      }
607
522
    }
608
523
    free(org_line);
609
 
  } else {
 
524
  } else{
610
525
    /* Check for harmful errors and go to fallback. Other errors might
611
526
       not affect opening plugins */
612
527
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
615
530
      goto fallback;
616
531
    }
617
532
  }
618
 
  /* If there was any arguments from configuration file,
619
 
     pass them to parser as command arguments */
 
533
 
620
534
  if(custom_argv != NULL){
621
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
622
 
                      0, NULL);
 
535
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
623
536
    if (ret == ARGP_ERR_UNKNOWN){
624
537
      fprintf(stderr, "Unknown error while parsing arguments\n");
625
538
      exitstatus = EXIT_FAILURE;
627
540
    }
628
541
  }
629
542
  
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
543
  if(debug){
640
544
    for(plugin *p = plugin_list; p != NULL; p=p->next){
641
545
      fprintf(stderr, "Plugin: %s has %d arguments\n",
650
554
    }
651
555
  }
652
556
  
653
 
  /* Strip permissions down to nobody */
654
557
  ret = setuid(uid);
655
558
  if (ret == -1){
656
559
    perror("setuid");
657
 
  }  
 
560
  }
 
561
  
658
562
  setgid(gid);
659
563
  if (ret == -1){
660
564
    perror("setgid");
661
565
  }
662
 
  
 
566
 
663
567
  if (plugindir == NULL){
664
568
    dir = opendir(PDIR);
665
569
  } else {
687
591
  
688
592
  FD_ZERO(&rfds_all);
689
593
  
690
 
  /* Read and execute any executable in the plugin directory*/
691
594
  while(true){
692
595
    dirst = readdir(dir);
693
596
    
694
 
    /* All directory entries have been processed */
 
597
    // All directory entries have been processed
695
598
    if(dirst == NULL){
696
599
      if (errno == EBADF){
697
600
        perror("readdir");
703
606
    
704
607
    d_name_len = strlen(dirst->d_name);
705
608
    
706
 
    /* Ignore dotfiles, backup files and other junk */
 
609
    // Ignore dotfiles, backup files and other junk
707
610
    {
708
611
      bool bad_name = false;
709
612
      
724
627
          break;
725
628
        }
726
629
      }
 
630
      
727
631
      if(bad_name){
728
632
        continue;
729
633
      }
 
634
      
730
635
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
731
636
        size_t suf_len = strlen(*suf);
732
637
        if((d_name_len >= suf_len)
759
664
      free(filename);
760
665
      continue;
761
666
    }
762
 
 
763
 
    /* Ignore non-executable files */
 
667
    
764
668
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
765
669
      if(debug){
766
670
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
769
673
      free(filename);
770
674
      continue;
771
675
    }
772
 
    
773
 
    plugin *p = getplugin(dirst->d_name);
 
676
    plugin *p = getplugin(dirst->d_name, &plugin_list);
774
677
    if(p == NULL){
775
678
      perror("getplugin");
776
679
      free(filename);
786
689
    }
787
690
    {
788
691
      /* Add global arguments to argument list for this plugin */
789
 
      plugin *g = getplugin(NULL);
 
692
      plugin *g = getplugin(NULL, &plugin_list);
790
693
      if(g != NULL){
791
694
        for(char **a = g->argv + 1; *a != NULL; a++){
792
695
          if(not add_argument(p, *a)){
795
698
        }
796
699
        /* Add global environment variables */
797
700
        for(char **e = g->environ; *e != NULL; e++){
798
 
          if(not add_environment(p, *e, false)){
 
701
          if(not add_environment(p, *e)){
799
702
            perror("add_environment");
800
703
          }
801
704
        }
806
709
       process, too. */
807
710
    if(p->environ[0] != NULL){
808
711
      for(char **e = environ; *e != NULL; e++){
809
 
        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)){
810
718
          perror("add_environment");
811
719
        }
812
720
      }
819
727
      exitstatus = EXIT_FAILURE;
820
728
      goto fallback;
821
729
    }
822
 
    /* Ask OS to automatic close the pipe on exec */
823
730
    ret = set_cloexec_flag(pipefd[0]);
824
731
    if(ret < 0){
825
732
      perror("set_cloexec_flag");
839
746
      exitstatus = EXIT_FAILURE;
840
747
      goto fallback;
841
748
    }
842
 
    /* Starting a new process to be watched */
 
749
    // Starting a new process to be watched
843
750
    pid_t pid = fork();
844
751
    if(pid == -1){
845
752
      perror("fork");
883
790
      }
884
791
      /* no return */
885
792
    }
886
 
    /* Parent process */
887
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
793
    /* parent process */
888
794
    free(filename);
889
 
    plugin *new_plugin = getplugin(dirst->d_name);
890
 
    if (new_plugin == NULL){
891
 
      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");
892
799
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
893
800
      if(ret < 0){
894
 
        perror("sigprocmask");
 
801
        perror("sigprocmask");
895
802
      }
896
803
      exitstatus = EXIT_FAILURE;
897
804
      goto fallback;
898
805
    }
899
806
    
900
 
    new_plugin->pid = pid;
901
 
    new_plugin->fd = pipefd[0];
902
 
    
 
807
    *new_process = (struct process){ .pid = pid,
 
808
                                     .fd = pipefd[0],
 
809
                                     .next = process_list };
 
810
    // List handling
 
811
    process_list = new_process;
903
812
    /* Unblock SIGCHLD so signal handler can be run if this process
904
813
       has already completed */
905
814
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
909
818
      goto fallback;
910
819
    }
911
820
    
912
 
    FD_SET(new_plugin->fd, &rfds_all);
 
821
    FD_SET(new_process->fd, &rfds_all);
913
822
    
914
 
    if (maxfd < new_plugin->fd){
915
 
      maxfd = new_plugin->fd;
 
823
    if (maxfd < new_process->fd){
 
824
      maxfd = new_process->fd;
916
825
    }
917
826
    
918
827
  }
 
828
 
 
829
  free_plugin_list(plugin_list);
 
830
  plugin_list = NULL;
919
831
  
920
832
  closedir(dir);
921
833
  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
 
    }
 
834
    
 
835
  if (process_list == NULL){
 
836
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
837
            " directory?\n");
 
838
    process_list = NULL;
932
839
  }
933
 
 
934
 
  /* Main loop while running plugins exist */
935
 
  while(plugin_list){
 
840
  while(process_list){
936
841
    fd_set rfds = rfds_all;
937
842
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
938
843
    if (select_ret == -1){
942
847
    }
943
848
    /* OK, now either a process completed, or something can be read
944
849
       from one of them */
945
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
850
    for(process *proc = process_list; proc ; proc = proc->next){
946
851
      /* Is this process completely done? */
947
852
      if(proc->eof and proc->completed){
948
853
        /* Only accept the plugin output if it exited cleanly */
949
854
        if(not WIFEXITED(proc->status)
950
855
           or WEXITSTATUS(proc->status) != 0){
951
856
          /* Bad exit by plugin */
952
 
 
953
857
          if(debug){
954
858
            if(WIFEXITED(proc->status)){
955
859
              fprintf(stderr, "Plugin %u exited with status %d\n",
964
868
                      (unsigned int) (proc->pid));
965
869
            }
966
870
          }
967
 
          
968
871
          /* Remove the plugin */
969
872
          FD_CLR(proc->fd, &rfds_all);
970
 
 
971
873
          /* Block signal while modifying process_list */
972
874
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
973
875
          if(ret < 0){
975
877
            exitstatus = EXIT_FAILURE;
976
878
            goto fallback;
977
879
          }
978
 
          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
          }
979
893
          /* We are done modifying process list, so unblock signal */
980
894
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
981
895
                             NULL);
982
896
          if(ret < 0){
983
897
            perror("sigprocmask");
984
 
            exitstatus = EXIT_FAILURE;
985
 
            goto fallback;
986
 
          }
987
 
          
988
 
          if(plugin_list == NULL){
989
 
            break;
990
 
          }
991
 
          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;
992
905
        }
993
 
        
994
906
        /* This process exited nicely, so print its buffer */
995
907
 
996
908
        bool bret = print_out_password(proc->buffer,
1001
913
        }
1002
914
        goto fallback;
1003
915
      }
1004
 
      
1005
916
      /* This process has not completed.  Does it have any output? */
1006
917
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1007
918
        /* This process had nothing to say at this time */
1037
948
 
1038
949
 fallback:
1039
950
  
1040
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
951
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
1041
952
    /* Fallback if all plugins failed, none are found or an error
1042
953
       occured */
1043
954
    bool bret;
1063
974
    }
1064
975
    free(custom_argv);
1065
976
  }
 
977
  free_plugin_list(plugin_list);
1066
978
  
1067
979
  if(dir != NULL){
1068
980
    closedir(dir);
1069
981
  }
1070
982
  
1071
983
  /* 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
 
      }
 
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");
1080
991
    }
 
992
    free(process_list->buffer);
 
993
    free(process_list);
1081
994
  }
1082
995
  
1083
996
  /* Wait for any remaining child processes to terminate */
1088
1001
    perror("wait");
1089
1002
  }
1090
1003
 
1091
 
  free_plugin_list();
1092
 
  
1093
1004
  free(plugindir);
1094
1005
  free(argfile);
1095
1006