/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-09-04 14:04:09 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080904140409-anr3hckg8ew7q60e
* plugin-runner.xml (BUGS): Document the non-recursiveness of the
                            "--config-file" option.

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