/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: Teddy Hogeborn
  • Date: 2008-09-05 18:19:52 UTC
  • mfrom: (24.1.91 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080905181952-qfuwtyvefanh9v3v
* mandos: Open the PID file before daemonizing, but write to it
          afterwards.

* plugin-runner.xml (SECURITY): Improved grammar.

Also merge.

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 an existing plugin based on name,
 
100
   or if none is found, creates a new one */
 
101
static plugin *getplugin(char *name){
 
102
  /* Check for exiting plugin with that name */
 
103
  for (plugin *p = plugin_list; p != NULL; p = p->next){
99
104
    if ((p->name == name)
100
105
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
106
      return p;
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) + 1);
 
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){
218
238
      /* No child processes */
219
239
      break;
220
240
    }
221
 
 
 
241
    
222
242
    /* A child exited, find it in process_list */
223
243
    while(proc != NULL and proc->pid != pid){
224
244
      proc = proc->next;
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
 
        if(p_name[0] == '\0'){
 
413
        if(p_name[0] == '\0' or arg == NULL){
390
414
          break;
391
415
        }
392
416
        char *opt = strsep(&arg, ":");
393
 
        if(opt[0] == '\0'){
 
417
        if(opt[0] == '\0' or opt == NULL){
394
418
          break;
395
419
        }
396
 
        if(opt != NULL){
397
 
          char *p;
398
 
          while((p = strsep(&opt, ",")) != NULL){
399
 
            if(p[0] == '\0'){
400
 
              continue;
401
 
            }
402
 
            if(not add_argument(getplugin(p_name, plugins), p)){
403
 
              perror("add_argument");
404
 
              return ARGP_ERR_UNKNOWN;
405
 
            }
 
420
        char *p;
 
421
        while((p = strsep(&opt, ",")) != NULL){
 
422
          if(p[0] == '\0'){
 
423
            continue;
 
424
          }
 
425
          if(not add_argument(getplugin(p_name), p)){
 
426
            perror("add_argument");
 
427
            return ARGP_ERR_UNKNOWN;
406
428
          }
407
429
        }
408
430
      }
409
431
      break;
410
 
    case 'f':
 
432
    case 'E':                   /* --env-for */
411
433
      if(arg == NULL){
412
434
        break;
413
435
      }
421
443
          break;
422
444
        }
423
445
        envdef++;
424
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
446
        if(not add_environment(getplugin(p_name), envdef, true)){
425
447
          perror("add_environment");
426
448
        }
427
449
      }
428
450
      break;
429
 
    case 'd':
 
451
    case 'd':                   /* --disable */
430
452
      if (arg != NULL){
431
 
        plugin *p = getplugin(arg, plugins);
 
453
        plugin *p = getplugin(arg);
432
454
        if(p == NULL){
433
455
          return ARGP_ERR_UNKNOWN;
434
456
        }
435
457
        p->disabled = true;
436
458
      }
437
459
      break;
438
 
    case 128:
439
 
      plugindir = arg;
440
 
      break;
441
 
    case 129:
 
460
    case 'e':                   /* --enable */
 
461
      if (arg != NULL){
 
462
        plugin *p = getplugin(arg);
 
463
        if(p == NULL){
 
464
          return ARGP_ERR_UNKNOWN;
 
465
        }
 
466
        p->disabled = false;
 
467
      }
 
468
      break;
 
469
    case 128:                   /* --plugin-dir */
 
470
      plugindir = strdup(arg);
 
471
      if(plugindir == NULL){
 
472
        perror("strdup");
 
473
      }      
 
474
      break;
 
475
    case 129:                   /* --config-file */
 
476
      /* This is already done by parse_opt_config_file() */
 
477
      break;
 
478
    case 130:                   /* --userid */
442
479
      uid = (uid_t)strtol(arg, NULL, 10);
443
480
      break;
444
 
    case 130:
 
481
    case 131:                   /* --groupid */
445
482
      gid = (gid_t)strtol(arg, NULL, 10);
446
483
      break;
447
 
    case 131:
 
484
    case 132:                   /* --debug */
448
485
      debug = true;
449
486
      break;
450
487
    case ARGP_KEY_ARG:
458
495
    return 0;
459
496
  }
460
497
  
461
 
  plugin *plugin_list = NULL;
 
498
  /* This option parser is the same as parse_opt() above, except it
 
499
     ignores everything but the --config-file option. */
 
500
  error_t parse_opt_config_file (int key, char *arg,
 
501
                                 __attribute__((unused))
 
502
                                 struct argp_state *state) {
 
503
    switch (key) {
 
504
    case 'g':                   /* --global-options */
 
505
    case 'G':                   /* --global-env */
 
506
    case 'o':                   /* --options-for */
 
507
    case 'E':                   /* --env-for */
 
508
    case 'd':                   /* --disable */
 
509
    case 'e':                   /* --enable */
 
510
    case 128:                   /* --plugin-dir */
 
511
      break;
 
512
    case 129:                   /* --config-file */
 
513
      argfile = strdup(arg);
 
514
      if(argfile == NULL){
 
515
        perror("strdup");
 
516
      }
 
517
      break;      
 
518
    case 130:                   /* --userid */
 
519
    case 131:                   /* --groupid */
 
520
    case 132:                   /* --debug */
 
521
    case ARGP_KEY_ARG:
 
522
    case ARGP_KEY_END:
 
523
      break;
 
524
    default:
 
525
      return ARGP_ERR_UNKNOWN;
 
526
    }
 
527
    return 0;
 
528
  }
462
529
  
463
 
  struct argp argp = { .options = options, .parser = parse_opt,
464
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
530
  struct argp argp = { .options = options,
 
531
                       .parser = parse_opt_config_file,
 
532
                       .args_doc = "",
465
533
                       .doc = "Mandos plugin runner -- Run plugins" };
466
534
  
467
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
535
  /* Parse using the parse_opt_config_file in order to get the custom
 
536
     config file location, if any. */
 
537
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
468
538
  if (ret == ARGP_ERR_UNKNOWN){
469
539
    fprintf(stderr, "Unknown error while parsing arguments\n");
470
540
    exitstatus = EXIT_FAILURE;
471
541
    goto fallback;
472
542
  }
473
 
 
474
 
  conffp = fopen(argfile, "r");
 
543
  
 
544
  /* Reset to the normal argument parser */
 
545
  argp.parser = parse_opt;
 
546
  
 
547
  /* Open the configfile if available */
 
548
  if (argfile == NULL){
 
549
    conffp = fopen(AFILE, "r");
 
550
  } else {
 
551
    conffp = fopen(argfile, "r");
 
552
  }  
475
553
  if(conffp != NULL){
476
554
    char *org_line = NULL;
477
555
    char *p, *arg, *new_arg, *line;
480
558
    const char whitespace_delims[] = " \r\t\f\v\n";
481
559
    const char comment_delim[] = "#";
482
560
 
 
561
    custom_argc = 1;
 
562
    custom_argv = malloc(sizeof(char*) * 2);
 
563
    if(custom_argv == NULL){
 
564
      perror("malloc");
 
565
      exitstatus = EXIT_FAILURE;
 
566
      goto fallback;
 
567
    }
 
568
    custom_argv[0] = argv[0];
 
569
    custom_argv[1] = NULL;
 
570
 
 
571
    /* for each line in the config file, strip whitespace and ignore
 
572
       commented text */
483
573
    while(true){
484
574
      sret = getline(&org_line, &size, conffp);
485
575
      if(sret == -1){
493
583
          continue;
494
584
        }
495
585
        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
 
        }
 
586
        if(new_arg == NULL){
 
587
          perror("strdup");
 
588
          exitstatus = EXIT_FAILURE;
 
589
          free(org_line);
 
590
          goto fallback;
 
591
        }
 
592
        
 
593
        custom_argc += 1;
 
594
        custom_argv = realloc(custom_argv, sizeof(char *)
 
595
                              * ((unsigned int) custom_argc + 1));
 
596
        if(custom_argv == NULL){
 
597
          perror("realloc");
 
598
          exitstatus = EXIT_FAILURE;
 
599
          free(org_line);
 
600
          goto fallback;
 
601
        }
 
602
        custom_argv[custom_argc-1] = new_arg;
 
603
        custom_argv[custom_argc] = NULL;        
502
604
      }
503
605
    }
504
606
    free(org_line);
505
 
  } else{
 
607
  } else {
506
608
    /* Check for harmful errors and go to fallback. Other errors might
507
609
       not affect opening plugins */
508
610
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
511
613
      goto fallback;
512
614
    }
513
615
  }
514
 
 
 
616
  /* If there was any arguments from configuration file,
 
617
     pass them to parser as command arguments */
515
618
  if(custom_argv != NULL){
516
 
    custom_argv[0] = argv[0];
517
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0,
518
 
                      &plugin_list);
 
619
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
620
                      0, NULL);
519
621
    if (ret == ARGP_ERR_UNKNOWN){
520
622
      fprintf(stderr, "Unknown error while parsing arguments\n");
521
623
      exitstatus = EXIT_FAILURE;
523
625
    }
524
626
  }
525
627
  
 
628
  /* Parse actual command line arguments, to let them override the
 
629
     config file */
 
630
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
631
  if (ret == ARGP_ERR_UNKNOWN){
 
632
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
633
    exitstatus = EXIT_FAILURE;
 
634
    goto fallback;
 
635
  }
 
636
  
526
637
  if(debug){
527
638
    for(plugin *p = plugin_list; p != NULL; p=p->next){
528
639
      fprintf(stderr, "Plugin: %s has %d arguments\n",
537
648
    }
538
649
  }
539
650
  
 
651
  /* Strip permissions down to nobody */
540
652
  ret = setuid(uid);
541
653
  if (ret == -1){
542
654
    perror("setuid");
543
 
  }
544
 
  
 
655
  }  
545
656
  setgid(gid);
546
657
  if (ret == -1){
547
658
    perror("setgid");
548
659
  }
549
660
  
550
 
  dir = opendir(plugindir);
 
661
  if (plugindir == NULL){
 
662
    dir = opendir(PDIR);
 
663
  } else {
 
664
    dir = opendir(plugindir);
 
665
  }
 
666
  
551
667
  if(dir == NULL){
552
668
    perror("Could not open plugin dir");
553
669
    exitstatus = EXIT_FAILURE;
569
685
  
570
686
  FD_ZERO(&rfds_all);
571
687
  
 
688
  /* Read and execute any executable in the plugin directory*/
572
689
  while(true){
573
690
    dirst = readdir(dir);
574
691
    
575
 
    // All directory entries have been processed
 
692
    /* All directory entries have been processed */
576
693
    if(dirst == NULL){
577
694
      if (errno == EBADF){
578
695
        perror("readdir");
584
701
    
585
702
    d_name_len = strlen(dirst->d_name);
586
703
    
587
 
    // Ignore dotfiles, backup files and other junk
 
704
    /* Ignore dotfiles, backup files and other junk */
588
705
    {
589
706
      bool bad_name = false;
590
707
      
605
722
          break;
606
723
        }
607
724
      }
608
 
      
609
725
      if(bad_name){
610
726
        continue;
611
727
      }
612
 
      
613
728
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
614
729
        size_t suf_len = strlen(*suf);
615
730
        if((d_name_len >= suf_len)
642
757
      free(filename);
643
758
      continue;
644
759
    }
645
 
    
 
760
 
 
761
    /* Ignore non-executable files */
646
762
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
647
763
      if(debug){
648
764
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
651
767
      free(filename);
652
768
      continue;
653
769
    }
654
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
770
    
 
771
    plugin *p = getplugin(dirst->d_name);
655
772
    if(p == NULL){
656
773
      perror("getplugin");
657
774
      free(filename);
667
784
    }
668
785
    {
669
786
      /* Add global arguments to argument list for this plugin */
670
 
      plugin *g = getplugin(NULL, &plugin_list);
 
787
      plugin *g = getplugin(NULL);
671
788
      if(g != NULL){
672
789
        for(char **a = g->argv + 1; *a != NULL; a++){
673
790
          if(not add_argument(p, *a)){
676
793
        }
677
794
        /* Add global environment variables */
678
795
        for(char **e = g->environ; *e != NULL; e++){
679
 
          if(not add_environment(p, *e)){
 
796
          if(not add_environment(p, *e, false)){
680
797
            perror("add_environment");
681
798
          }
682
799
        }
687
804
       process, too. */
688
805
    if(p->environ[0] != NULL){
689
806
      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)){
 
807
        if(not add_environment(p, *e, false)){
696
808
          perror("add_environment");
697
809
        }
698
810
      }
705
817
      exitstatus = EXIT_FAILURE;
706
818
      goto fallback;
707
819
    }
 
820
    /* Ask OS to automatic close the pipe on exec */
708
821
    ret = set_cloexec_flag(pipefd[0]);
709
822
    if(ret < 0){
710
823
      perror("set_cloexec_flag");
724
837
      exitstatus = EXIT_FAILURE;
725
838
      goto fallback;
726
839
    }
727
 
    // Starting a new process to be watched
 
840
    /* Starting a new process to be watched */
728
841
    pid_t pid = fork();
729
842
    if(pid == -1){
730
843
      perror("fork");
768
881
      }
769
882
      /* no return */
770
883
    }
771
 
    /* parent process */
 
884
    /* Parent process */
 
885
    close(pipefd[1]);           /* Close unused write end of pipe */
772
886
    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");
 
887
    plugin *new_plugin = getplugin(dirst->d_name);
 
888
    if (new_plugin == NULL){
 
889
      perror("getplugin");
777
890
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
778
891
      if(ret < 0){
779
 
        perror("sigprocmask");
 
892
        perror("sigprocmask");
780
893
      }
781
894
      exitstatus = EXIT_FAILURE;
782
895
      goto fallback;
783
896
    }
784
897
    
785
 
    *new_process = (struct process){ .pid = pid,
786
 
                                     .fd = pipefd[0],
787
 
                                     .next = process_list };
788
 
    // List handling
789
 
    process_list = new_process;
 
898
    new_plugin->pid = pid;
 
899
    new_plugin->fd = pipefd[0];
 
900
    
790
901
    /* Unblock SIGCHLD so signal handler can be run if this process
791
902
       has already completed */
792
903
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
796
907
      goto fallback;
797
908
    }
798
909
    
799
 
    FD_SET(new_process->fd, &rfds_all);
 
910
    FD_SET(new_plugin->fd, &rfds_all);
800
911
    
801
 
    if (maxfd < new_process->fd){
802
 
      maxfd = new_process->fd;
 
912
    if (maxfd < new_plugin->fd){
 
913
      maxfd = new_plugin->fd;
803
914
    }
804
915
    
805
916
  }
806
917
  
807
 
  free_plugin_list(plugin_list);
808
 
  plugin_list = NULL;
809
 
  
810
918
  closedir(dir);
811
919
  dir = NULL;
812
 
    
813
 
  if (process_list == NULL){
814
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
815
 
            " directory?\n");
816
 
    process_list = NULL;
 
920
 
 
921
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
922
    if(p->pid != 0){
 
923
      break;
 
924
    }
 
925
    if(p->next == NULL){
 
926
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
927
              " directory?\n");
 
928
      free_plugin_list();
 
929
    }
817
930
  }
818
 
  while(process_list){
 
931
 
 
932
  /* Main loop while running plugins exist */
 
933
  while(plugin_list){
819
934
    fd_set rfds = rfds_all;
820
935
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
821
936
    if (select_ret == -1){
825
940
    }
826
941
    /* OK, now either a process completed, or something can be read
827
942
       from one of them */
828
 
    for(process *proc = process_list; proc ; proc = proc->next){
 
943
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
829
944
      /* Is this process completely done? */
830
945
      if(proc->eof and proc->completed){
831
946
        /* Only accept the plugin output if it exited cleanly */
832
947
        if(not WIFEXITED(proc->status)
833
948
           or WEXITSTATUS(proc->status) != 0){
834
949
          /* Bad exit by plugin */
 
950
 
835
951
          if(debug){
836
952
            if(WIFEXITED(proc->status)){
837
953
              fprintf(stderr, "Plugin %u exited with status %d\n",
846
962
                      (unsigned int) (proc->pid));
847
963
            }
848
964
          }
 
965
          
849
966
          /* Remove the plugin */
850
967
          FD_CLR(proc->fd, &rfds_all);
 
968
 
851
969
          /* Block signal while modifying process_list */
852
970
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
853
971
          if(ret < 0){
855
973
            exitstatus = EXIT_FAILURE;
856
974
            goto fallback;
857
975
          }
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
 
          }
 
976
          free_plugin(proc);
871
977
          /* We are done modifying process list, so unblock signal */
872
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
873
979
                             NULL);
874
980
          if(ret < 0){
875
981
            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;
 
982
            exitstatus = EXIT_FAILURE;
 
983
            goto fallback;
 
984
          }
 
985
          
 
986
          if(plugin_list == NULL){
 
987
            break;
 
988
          }
 
989
          continue;
883
990
        }
 
991
        
884
992
        /* This process exited nicely, so print its buffer */
885
993
 
886
994
        bool bret = print_out_password(proc->buffer,
891
999
        }
892
1000
        goto fallback;
893
1001
      }
 
1002
      
894
1003
      /* This process has not completed.  Does it have any output? */
895
1004
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
896
1005
        /* This process had nothing to say at this time */
926
1035
 
927
1036
 fallback:
928
1037
  
929
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
1038
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
930
1039
    /* Fallback if all plugins failed, none are found or an error
931
1040
       occured */
932
1041
    bool bret;
947
1056
  }
948
1057
 
949
1058
  if(custom_argv != NULL){
950
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
 
1059
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
951
1060
      free(*arg);
952
1061
    }
953
1062
    free(custom_argv);
954
1063
  }
955
 
  free_plugin_list(plugin_list);
956
1064
  
957
1065
  if(dir != NULL){
958
1066
    closedir(dir);
959
1067
  }
960
1068
  
961
1069
  /* 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");
 
1070
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
1071
    if(p->pid != 0){
 
1072
      close(p->fd);
 
1073
      ret = kill(p->pid, SIGTERM);
 
1074
      if(ret == -1 and errno != ESRCH){
 
1075
        /* Set-uid proccesses might not get closed */
 
1076
        perror("kill");
 
1077
      }
969
1078
    }
970
 
    free(process_list->buffer);
971
 
    free(process_list);
972
1079
  }
973
1080
  
974
1081
  /* Wait for any remaining child processes to terminate */
978
1085
  if(errno != ECHILD){
979
1086
    perror("wait");
980
1087
  }
 
1088
 
 
1089
  free_plugin_list();
 
1090
  
 
1091
  free(plugindir);
 
1092
  free(argfile);
981
1093
  
982
1094
  return exitstatus;
983
1095
}