/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-25 03:53:42 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080825035342-wheobopjfhf0hive
* Makefile (maintainer-clean): Also remove "confdir".
  (run-client): Also create a key.
  (run-server): Also create a local config including a client.
  (keydir/secring.gpg, keydir/pubring.gpg, keydir/seckey.txt
  keydir/pubkey.txt): New targets used by "run-client".
  (confdir/mandos.conf, confdir/clients.conf): New targets used by
                                               "run-server".

* mandos-keygen (KEYLENGTH): Changed default to "2048".

* mandos-keygen.xml (OPTIONS): Changed default value for "--length".

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
 
      }
 
250
char **add_to_argv(char **argv, int *argc, char *arg){
 
251
  if (argv == NULL){
 
252
    *argc = 1;
 
253
    argv = malloc(sizeof(char*) * 2);
 
254
    if(argv == NULL){
 
255
      return NULL;
295
256
    }
296
 
  }
297
 
  
298
 
  free(plugin_node);
 
257
    argv[0] = NULL;     /* Will be set to argv[0] in main before
 
258
                           parsing */
 
259
    argv[1] = NULL;
 
260
  }
 
261
  *argc += 1;
 
262
  argv = realloc(argv, sizeof(char *)
 
263
                  * ((unsigned int) *argc + 1));
 
264
  if(argv == NULL){
 
265
    return NULL;
 
266
  }
 
267
  argv[*argc-1] = arg;
 
268
  argv[*argc] = NULL;
 
269
  return argv;
299
270
}
300
271
 
301
 
static void free_plugin_list(void){
302
 
  while(plugin_list != NULL){
303
 
    free_plugin(plugin_list);
 
272
static void free_plugin_list(plugin *plugin_list){
 
273
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
274
    next = plugin_list->next;
 
275
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
276
      free(*arg);
 
277
    }
 
278
    free(plugin_list->argv);
 
279
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
280
      free(*env);
 
281
    }
 
282
    free(plugin_list->environ);
 
283
    free(plugin_list);
304
284
  }
305
285
}
306
286
 
307
287
int main(int argc, char *argv[]){
308
 
  char *plugindir = NULL;
309
 
  char *argfile = NULL;
 
288
  const char *plugindir = "/lib/mandos/plugins.d";
 
289
  const char *argfile = ARGFILE;
310
290
  FILE *conffp;
311
291
  size_t d_name_len;
312
292
  DIR *dir = NULL;
344
324
    { .name = "global-options", .key = 'g',
345
325
      .arg = "OPTION[,OPTION[,...]]",
346
326
      .doc = "Options passed to all plugins" },
347
 
    { .name = "global-env", .key = 'e',
 
327
    { .name = "global-envs", .key = 'e',
348
328
      .arg = "VAR=value",
349
329
      .doc = "Environment variable passed to all plugins" },
350
330
    { .name = "options-for", .key = 'o',
351
331
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
352
332
      .doc = "Options passed only to specified plugin" },
353
 
    { .name = "env-for", .key = 'f',
 
333
    { .name = "envs-for", .key = 'f',
354
334
      .arg = "PLUGIN:ENV=value",
355
335
      .doc = "Environment variable passed to specified plugin" },
356
336
    { .name = "disable", .key = 'd',
359
339
    { .name = "plugin-dir", .key = 128,
360
340
      .arg = "DIRECTORY",
361
341
      .doc = "Specify a different plugin directory", .group = 2 },
362
 
    { .name = "config-file", .key = 129,
363
 
      .arg = "FILE",
364
 
      .doc = "Specify a different configuration file", .group = 2 },
365
 
    { .name = "userid", .key = 130,
366
 
      .arg = "ID", .flags = 0,
367
 
      .doc = "User ID the plugins will run as", .group = 3 },
368
 
    { .name = "groupid", .key = 131,
369
 
      .arg = "ID", .flags = 0,
370
 
      .doc = "Group ID the plugins will run as", .group = 3 },
371
 
    { .name = "debug", .key = 132,
372
 
      .doc = "Debug mode", .group = 4 },
 
342
    { .name = "userid", .key = 129,
 
343
      .arg = "ID", .flags = 0,
 
344
      .doc = "User ID the plugins will run as", .group = 2 },
 
345
    { .name = "groupid", .key = 130,
 
346
      .arg = "ID", .flags = 0,
 
347
      .doc = "Group ID the plugins will run as", .group = 2 },
 
348
    { .name = "debug", .key = 131,
 
349
      .doc = "Debug mode", .group = 3 },
373
350
    { .name = NULL }
374
351
  };
375
352
  
376
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
 
                     struct argp_state *state) {
 
353
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
378
354
    /* Get the INPUT argument from `argp_parse', which we know is a
379
355
       pointer to our plugin list pointer. */
 
356
    plugin **plugins = state->input;
380
357
    switch (key) {
381
 
    case 'g':                   /* --global-options */
 
358
    case 'g':
382
359
      if (arg != NULL){
383
360
        char *p;
384
361
        while((p = strsep(&arg, ",")) != NULL){
385
362
          if(p[0] == '\0'){
386
363
            continue;
387
364
          }
388
 
          if(not add_argument(getplugin(NULL), p)){
 
365
          if(not add_argument(getplugin(NULL, plugins), p)){
389
366
            perror("add_argument");
390
367
            return ARGP_ERR_UNKNOWN;
391
368
          }
392
369
        }
393
370
      }
394
371
      break;
395
 
    case 'e':                   /* --global-env */
 
372
    case 'e':
396
373
      if(arg == NULL){
397
374
        break;
398
375
      }
401
378
        if(envdef == NULL){
402
379
          break;
403
380
        }
404
 
        if(not add_environment(getplugin(NULL), envdef, true)){
 
381
        if(not add_environment(getplugin(NULL, plugins), envdef)){
405
382
          perror("add_environment");
406
383
        }
407
384
      }
408
385
      break;
409
 
    case 'o':                   /* --options-for */
 
386
    case 'o':
410
387
      if (arg != NULL){
411
388
        char *p_name = strsep(&arg, ":");
412
389
        if(p_name[0] == '\0'){
422
399
            if(p[0] == '\0'){
423
400
              continue;
424
401
            }
425
 
            if(not add_argument(getplugin(p_name), p)){
 
402
            if(not add_argument(getplugin(p_name, plugins), p)){
426
403
              perror("add_argument");
427
404
              return ARGP_ERR_UNKNOWN;
428
405
            }
430
407
        }
431
408
      }
432
409
      break;
433
 
    case 'f':                   /* --env-for */
 
410
    case 'f':
434
411
      if(arg == NULL){
435
412
        break;
436
413
      }
444
421
          break;
445
422
        }
446
423
        envdef++;
447
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
424
        if(not add_environment(getplugin(p_name, plugins), envdef)){
448
425
          perror("add_environment");
449
426
        }
450
427
      }
451
428
      break;
452
 
    case 'd':                   /* --disable */
 
429
    case 'd':
453
430
      if (arg != NULL){
454
 
        plugin *p = getplugin(arg);
 
431
        plugin *p = getplugin(arg, plugins);
455
432
        if(p == NULL){
456
433
          return ARGP_ERR_UNKNOWN;
457
434
        }
458
435
        p->disabled = true;
459
436
      }
460
437
      break;
461
 
    case 128:                   /* --plugin-dir */
462
 
      plugindir = strdup(arg);
463
 
      if(plugindir == NULL){
464
 
        perror("strdup");
465
 
      }      
 
438
    case 128:
 
439
      plugindir = arg;
466
440
      break;
467
 
    case 129:                   /* --config-file */
468
 
      argfile = strdup(arg);
469
 
      if(argfile == NULL){
470
 
        perror("strdup");
471
 
      }
472
 
      break;      
473
 
    case 130:                   /* --userid */
 
441
    case 129:
474
442
      uid = (uid_t)strtol(arg, NULL, 10);
475
443
      break;
476
 
    case 131:                   /* --groupid */
 
444
    case 130:
477
445
      gid = (gid_t)strtol(arg, NULL, 10);
478
446
      break;
479
 
    case 132:                   /* --debug */
 
447
    case 131:
480
448
      debug = true;
481
449
      break;
482
450
    case ARGP_KEY_ARG:
490
458
    return 0;
491
459
  }
492
460
  
 
461
  plugin *plugin_list = NULL;
 
462
  
493
463
  struct argp argp = { .options = options, .parser = parse_opt,
494
 
                       .args_doc = "",
 
464
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
495
465
                       .doc = "Mandos plugin runner -- Run plugins" };
496
466
  
497
 
  /* Open the configfile if available */
498
 
  if (argfile == NULL){
499
 
    conffp = fopen(AFILE, "r");
500
 
  } else {
501
 
    conffp = fopen(argfile, "r");
502
 
  }  
 
467
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
468
  if (ret == ARGP_ERR_UNKNOWN){
 
469
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
470
    exitstatus = EXIT_FAILURE;
 
471
    goto fallback;
 
472
  }
 
473
 
 
474
  conffp = fopen(argfile, "r");
503
475
  if(conffp != NULL){
504
476
    char *org_line = NULL;
505
477
    char *p, *arg, *new_arg, *line;
508
480
    const char whitespace_delims[] = " \r\t\f\v\n";
509
481
    const char comment_delim[] = "#";
510
482
 
511
 
    custom_argc = 1;
512
 
    custom_argv = malloc(sizeof(char*) * 2);
513
 
    if(custom_argv == NULL){
514
 
      perror("malloc");
515
 
      exitstatus = EXIT_FAILURE;
516
 
      goto fallback;
517
 
    }
518
 
    custom_argv[0] = argv[0];
519
 
    custom_argv[1] = NULL;
520
 
 
521
 
    /* for each line in the config file, strip whitespace and ignore
522
 
       commented text */
523
483
    while(true){
524
484
      sret = getline(&org_line, &size, conffp);
525
485
      if(sret == -1){
533
493
          continue;
534
494
        }
535
495
        new_arg = strdup(p);
536
 
        if(new_arg == NULL){
537
 
          perror("strdup");
538
 
          exitstatus = EXIT_FAILURE;
539
 
          free(org_line);
540
 
          goto fallback;
541
 
        }
542
 
        
543
 
        custom_argc += 1;
544
 
        custom_argv = realloc(custom_argv, sizeof(char *)
545
 
                              * ((unsigned int) custom_argc + 1));
546
 
        if(custom_argv == NULL){
547
 
          perror("realloc");
548
 
          exitstatus = EXIT_FAILURE;
549
 
          free(org_line);
550
 
          goto fallback;
551
 
        }
552
 
        custom_argv[custom_argc-1] = new_arg;
553
 
        custom_argv[custom_argc] = NULL;        
 
496
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
497
        if (custom_argv == NULL){
 
498
          perror("add_to_argv");
 
499
          exitstatus = EXIT_FAILURE;
 
500
          goto fallback;
 
501
        }
554
502
      }
555
503
    }
556
504
    free(org_line);
557
 
  } else {
 
505
  } else{
558
506
    /* Check for harmful errors and go to fallback. Other errors might
559
507
       not affect opening plugins */
560
508
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
563
511
      goto fallback;
564
512
    }
565
513
  }
566
 
  /* If there was any arguments from configuration file,
567
 
     pass them to parser as command arguments */
 
514
 
568
515
  if(custom_argv != NULL){
569
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
570
 
                      0, NULL);
 
516
    custom_argv[0] = argv[0];
 
517
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0,
 
518
                      &plugin_list);
571
519
    if (ret == ARGP_ERR_UNKNOWN){
572
520
      fprintf(stderr, "Unknown error while parsing arguments\n");
573
521
      exitstatus = EXIT_FAILURE;
575
523
    }
576
524
  }
577
525
  
578
 
  /* Parse actual command line arguments, to let them override the
579
 
     config file */
580
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
581
 
  if (ret == ARGP_ERR_UNKNOWN){
582
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
583
 
    exitstatus = EXIT_FAILURE;
584
 
    goto fallback;
585
 
  }
586
 
  
587
526
  if(debug){
588
527
    for(plugin *p = plugin_list; p != NULL; p=p->next){
589
528
      fprintf(stderr, "Plugin: %s has %d arguments\n",
598
537
    }
599
538
  }
600
539
  
601
 
  /* Strip permissions down to nobody */
602
540
  ret = setuid(uid);
603
541
  if (ret == -1){
604
542
    perror("setuid");
605
 
  }  
 
543
  }
 
544
  
606
545
  setgid(gid);
607
546
  if (ret == -1){
608
547
    perror("setgid");
609
548
  }
610
549
  
611
 
  if (plugindir == NULL){
612
 
    dir = opendir(PDIR);
613
 
  } else {
614
 
    dir = opendir(plugindir);
615
 
  }
616
 
  
 
550
  dir = opendir(plugindir);
617
551
  if(dir == NULL){
618
552
    perror("Could not open plugin dir");
619
553
    exitstatus = EXIT_FAILURE;
635
569
  
636
570
  FD_ZERO(&rfds_all);
637
571
  
638
 
  /* Read and execute any executable in the plugin directory*/
639
572
  while(true){
640
573
    dirst = readdir(dir);
641
574
    
642
 
    /* All directory entries have been processed */
 
575
    // All directory entries have been processed
643
576
    if(dirst == NULL){
644
577
      if (errno == EBADF){
645
578
        perror("readdir");
651
584
    
652
585
    d_name_len = strlen(dirst->d_name);
653
586
    
654
 
    /* Ignore dotfiles, backup files and other junk */
 
587
    // Ignore dotfiles, backup files and other junk
655
588
    {
656
589
      bool bad_name = false;
657
590
      
672
605
          break;
673
606
        }
674
607
      }
 
608
      
675
609
      if(bad_name){
676
610
        continue;
677
611
      }
 
612
      
678
613
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
679
614
        size_t suf_len = strlen(*suf);
680
615
        if((d_name_len >= suf_len)
707
642
      free(filename);
708
643
      continue;
709
644
    }
710
 
 
711
 
    /* Ignore non-executable files */
 
645
    
712
646
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
713
647
      if(debug){
714
648
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
717
651
      free(filename);
718
652
      continue;
719
653
    }
720
 
    
721
 
    plugin *p = getplugin(dirst->d_name);
 
654
    plugin *p = getplugin(dirst->d_name, &plugin_list);
722
655
    if(p == NULL){
723
656
      perror("getplugin");
724
657
      free(filename);
734
667
    }
735
668
    {
736
669
      /* Add global arguments to argument list for this plugin */
737
 
      plugin *g = getplugin(NULL);
 
670
      plugin *g = getplugin(NULL, &plugin_list);
738
671
      if(g != NULL){
739
672
        for(char **a = g->argv + 1; *a != NULL; a++){
740
673
          if(not add_argument(p, *a)){
743
676
        }
744
677
        /* Add global environment variables */
745
678
        for(char **e = g->environ; *e != NULL; e++){
746
 
          if(not add_environment(p, *e, false)){
 
679
          if(not add_environment(p, *e)){
747
680
            perror("add_environment");
748
681
          }
749
682
        }
754
687
       process, too. */
755
688
    if(p->environ[0] != NULL){
756
689
      for(char **e = environ; *e != NULL; e++){
757
 
        if(not add_environment(p, *e, false)){
 
690
        char *copy = strdup(*e);
 
691
        if(copy == NULL){
 
692
          perror("strdup");
 
693
          continue;
 
694
        }
 
695
        if(not add_environment(p, copy)){
758
696
          perror("add_environment");
759
697
        }
760
698
      }
767
705
      exitstatus = EXIT_FAILURE;
768
706
      goto fallback;
769
707
    }
770
 
    /* Ask OS to automatic close the pipe on exec */
771
708
    ret = set_cloexec_flag(pipefd[0]);
772
709
    if(ret < 0){
773
710
      perror("set_cloexec_flag");
787
724
      exitstatus = EXIT_FAILURE;
788
725
      goto fallback;
789
726
    }
790
 
    /* Starting a new process to be watched */
 
727
    // Starting a new process to be watched
791
728
    pid_t pid = fork();
792
729
    if(pid == -1){
793
730
      perror("fork");
831
768
      }
832
769
      /* no return */
833
770
    }
834
 
    /* Parent process */
835
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
771
    /* parent process */
836
772
    free(filename);
837
 
    plugin *new_plugin = getplugin(dirst->d_name);
838
 
    if (new_plugin == NULL){
839
 
      perror("getplugin");
 
773
    close(pipefd[1]);           /* close unused write end of pipe */
 
774
    process *new_process = malloc(sizeof(process));
 
775
    if (new_process == NULL){
 
776
      perror("malloc");
840
777
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
841
778
      if(ret < 0){
842
 
        perror("sigprocmask");
 
779
        perror("sigprocmask");
843
780
      }
844
781
      exitstatus = EXIT_FAILURE;
845
782
      goto fallback;
846
783
    }
847
784
    
848
 
    new_plugin->pid = pid;
849
 
    new_plugin->fd = pipefd[0];
850
 
    
 
785
    *new_process = (struct process){ .pid = pid,
 
786
                                     .fd = pipefd[0],
 
787
                                     .next = process_list };
 
788
    // List handling
 
789
    process_list = new_process;
851
790
    /* Unblock SIGCHLD so signal handler can be run if this process
852
791
       has already completed */
853
792
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
857
796
      goto fallback;
858
797
    }
859
798
    
860
 
    FD_SET(new_plugin->fd, &rfds_all);
 
799
    FD_SET(new_process->fd, &rfds_all);
861
800
    
862
 
    if (maxfd < new_plugin->fd){
863
 
      maxfd = new_plugin->fd;
 
801
    if (maxfd < new_process->fd){
 
802
      maxfd = new_process->fd;
864
803
    }
865
804
    
866
805
  }
867
806
  
 
807
  free_plugin_list(plugin_list);
 
808
  plugin_list = NULL;
 
809
  
868
810
  closedir(dir);
869
811
  dir = NULL;
870
 
 
871
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
872
 
    if(p->pid != 0){
873
 
      break;
874
 
    }
875
 
    if(p->next == NULL){
876
 
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
877
 
              " directory?\n");
878
 
      free_plugin_list();
879
 
    }
 
812
    
 
813
  if (process_list == NULL){
 
814
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
815
            " directory?\n");
 
816
    process_list = NULL;
880
817
  }
881
 
 
882
 
  /* Main loop while running plugins exist */
883
 
  while(plugin_list){
 
818
  while(process_list){
884
819
    fd_set rfds = rfds_all;
885
820
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
886
821
    if (select_ret == -1){
890
825
    }
891
826
    /* OK, now either a process completed, or something can be read
892
827
       from one of them */
893
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
828
    for(process *proc = process_list; proc ; proc = proc->next){
894
829
      /* Is this process completely done? */
895
830
      if(proc->eof and proc->completed){
896
831
        /* Only accept the plugin output if it exited cleanly */
897
832
        if(not WIFEXITED(proc->status)
898
833
           or WEXITSTATUS(proc->status) != 0){
899
834
          /* Bad exit by plugin */
900
 
 
901
835
          if(debug){
902
836
            if(WIFEXITED(proc->status)){
903
837
              fprintf(stderr, "Plugin %u exited with status %d\n",
912
846
                      (unsigned int) (proc->pid));
913
847
            }
914
848
          }
915
 
          
916
849
          /* Remove the plugin */
917
850
          FD_CLR(proc->fd, &rfds_all);
918
 
 
919
851
          /* Block signal while modifying process_list */
920
852
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
921
853
          if(ret < 0){
923
855
            exitstatus = EXIT_FAILURE;
924
856
            goto fallback;
925
857
          }
926
 
          free_plugin(proc);
 
858
          /* Delete this process entry from the list */
 
859
          if(process_list == proc){
 
860
            /* First one - simple */
 
861
            process_list = proc->next;
 
862
          } else {
 
863
            /* Second one or later */
 
864
            for(process *p = process_list; p != NULL; p = p->next){
 
865
              if(p->next == proc){
 
866
                p->next = proc->next;
 
867
                break;
 
868
              }
 
869
            }
 
870
          }
927
871
          /* We are done modifying process list, so unblock signal */
928
872
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
929
873
                             NULL);
930
874
          if(ret < 0){
931
875
            perror("sigprocmask");
932
 
            exitstatus = EXIT_FAILURE;
933
 
            goto fallback;
934
 
          }
935
 
          
936
 
          if(plugin_list == NULL){
937
 
            break;
938
 
          }
939
 
          continue;
 
876
          }
 
877
          free(proc->buffer);
 
878
          free(proc);
 
879
          /* We deleted this process from the list, so we can't go
 
880
             proc->next.  Therefore, start over from the beginning of
 
881
             the process list */
 
882
          break;
940
883
        }
941
 
        
942
884
        /* This process exited nicely, so print its buffer */
943
885
 
944
886
        bool bret = print_out_password(proc->buffer,
949
891
        }
950
892
        goto fallback;
951
893
      }
952
 
      
953
894
      /* This process has not completed.  Does it have any output? */
954
895
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
955
896
        /* This process had nothing to say at this time */
985
926
 
986
927
 fallback:
987
928
  
988
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
929
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
989
930
    /* Fallback if all plugins failed, none are found or an error
990
931
       occured */
991
932
    bool bret;
1006
947
  }
1007
948
 
1008
949
  if(custom_argv != NULL){
1009
 
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
950
    for(char **arg = custom_argv; *arg != NULL; arg++){
1010
951
      free(*arg);
1011
952
    }
1012
953
    free(custom_argv);
1013
954
  }
 
955
  free_plugin_list(plugin_list);
1014
956
  
1015
957
  if(dir != NULL){
1016
958
    closedir(dir);
1017
959
  }
1018
960
  
1019
961
  /* Free the process list and kill the processes */
1020
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1021
 
    if(p->pid != 0){
1022
 
      close(p->fd);
1023
 
      ret = kill(p->pid, SIGTERM);
1024
 
      if(ret == -1 and errno != ESRCH){
1025
 
        /* Set-uid proccesses might not get closed */
1026
 
        perror("kill");
1027
 
      }
 
962
  for(process *next; process_list != NULL; process_list = next){
 
963
    next = process_list->next;
 
964
    close(process_list->fd);
 
965
    ret = kill(process_list->pid, SIGTERM);
 
966
    if(ret == -1 and errno != ESRCH){
 
967
      /* set-uid proccesses migth not get closed */
 
968
      perror("kill");
1028
969
    }
 
970
    free(process_list->buffer);
 
971
    free(process_list);
1029
972
  }
1030
973
  
1031
974
  /* Wait for any remaining child processes to terminate */
1035
978
  if(errno != ECHILD){
1036
979
    perror("wait");
1037
980
  }
1038
 
 
1039
 
  free_plugin_list();
1040
 
  
1041
 
  free(plugindir);
1042
 
  free(argfile);
1043
981
  
1044
982
  return exitstatus;
1045
983
}