/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

merge + small bugfix

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
#include <argp.h>               /* struct argp_option, struct
57
57
                                   argp_state, struct argp,
58
58
                                   argp_parse(), ARGP_ERR_UNKNOWN,
59
 
                                   ARGP_KEY_END, ARGP_KEY_ARG,
60
 
                                   error_t */
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
60
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
61
                                   sigaddset(), sigaction(),
63
62
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
64
#include <errno.h>              /* errno, EBADF */
66
65
 
67
66
#define BUFFER_SIZE 256
68
 
 
69
 
#define PDIR "/lib/mandos/plugins.d"
70
 
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
67
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
68
 
72
69
const char *argp_program_version = "plugin-runner 1.0";
73
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
71
 
75
 
struct plugin;
 
72
struct process;
 
73
 
 
74
typedef struct process{
 
75
  pid_t pid;
 
76
  int fd;
 
77
  char *buffer;
 
78
  size_t buffer_size;
 
79
  size_t buffer_length;
 
80
  bool eof;
 
81
  bool completed;
 
82
  int status;
 
83
  struct process *next;
 
84
} process;
76
85
 
77
86
typedef struct plugin{
78
87
  char *name;                   /* can be NULL or any plugin name */
81
90
  char **environ;
82
91
  int envc;
83
92
  bool disabled;
84
 
 
85
 
  /* Variables used for running processes*/
86
 
  pid_t pid;
87
 
  int fd;
88
 
  char *buffer;
89
 
  size_t buffer_size;
90
 
  size_t buffer_length;
91
 
  bool eof;
92
 
  volatile bool completed;
93
 
  volatile int status;
94
93
  struct plugin *next;
95
94
} plugin;
96
95
 
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){
 
96
static plugin *getplugin(char *name, plugin **plugin_list){
 
97
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
104
98
    if ((p->name == name)
105
99
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
100
      return p;
114
108
  char *copy_name = NULL;
115
109
  if(name != NULL){
116
110
    copy_name = strdup(name);
117
 
    if(copy_name == NULL){
118
 
      return NULL;
119
 
    }
120
 
  }
121
 
 
 
111
  }
 
112
  if(copy_name == NULL){
 
113
    return NULL;
 
114
  }
 
115
  
122
116
  *new_plugin = (plugin) { .name = copy_name,
123
117
                           .argc = 1,
 
118
                           .envc = 0,
124
119
                           .disabled = false,
125
 
                           .next = plugin_list };
 
120
                           .next = *plugin_list };
126
121
  
127
122
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
123
  if (new_plugin->argv == NULL){
132
127
  }
133
128
  new_plugin->argv[0] = copy_name;
134
129
  new_plugin->argv[1] = NULL;
135
 
  
 
130
 
136
131
  new_plugin->environ = malloc(sizeof(char *));
137
132
  if(new_plugin->environ == NULL){
138
133
    free(copy_name);
141
136
    return NULL;
142
137
  }
143
138
  new_plugin->environ[0] = NULL;
144
 
  
145
139
  /* Append the new plugin to the list */
146
 
  plugin_list = new_plugin;
 
140
  *plugin_list = new_plugin;
147
141
  return new_plugin;
148
142
}
149
143
 
179
173
}
180
174
 
181
175
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def, bool replace){
 
176
static bool add_environment(plugin *p, const char *def){
183
177
  if(p == NULL){
184
178
    return false;
185
179
  }
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
180
  return add_to_char_array(def, &(p->environ), &(p->envc));
204
181
}
205
182
 
 
183
 
206
184
/*
207
185
 * Based on the example in the GNU LibC manual chapter 13.13 "File
208
186
 * Descriptor Flags".
219
197
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
220
198
}
221
199
 
 
200
process *process_list = NULL;
222
201
 
223
 
/* Mark processes as completed when they exit, and save their exit
 
202
/* Mark a process as completed when it exits, and save its exit
224
203
   status. */
225
204
void handle_sigchld(__attribute__((unused)) int sig){
226
 
  while(true){
227
 
    plugin *proc = plugin_list;
228
 
    int status;
229
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
230
 
    if(pid == 0){
231
 
      /* Only still running child processes */
232
 
      break;
233
 
    }
234
 
    if(pid == -1){
235
 
      if (errno != ECHILD){
236
 
        perror("waitpid");
237
 
      }
238
 
      /* No child processes */
239
 
      break;
240
 
    }
241
 
 
242
 
    /* A child exited, find it in process_list */
243
 
    while(proc != NULL and proc->pid != pid){
244
 
      proc = proc->next;
245
 
    }
246
 
    if(proc == NULL){
247
 
      /* Process not found in process list */
248
 
      continue;
249
 
    }
250
 
    proc->status = status;
251
 
    proc->completed = true;
252
 
  }
 
205
  process *proc = process_list;
 
206
  int status;
 
207
  pid_t pid = wait(&status);
 
208
  if(pid == -1){
 
209
    perror("wait");
 
210
    return;
 
211
  }
 
212
  while(proc != NULL and proc->pid != pid){
 
213
    proc = proc->next;
 
214
  }
 
215
  if(proc == NULL){
 
216
    /* Process not found in process list */
 
217
    return;
 
218
  }
 
219
  proc->status = status;
 
220
  proc->completed = true;
253
221
}
254
222
 
255
 
/* Prints out a password to stdout */
256
223
bool print_out_password(const char *buffer, size_t length){
257
224
  ssize_t ret;
258
225
  if(length>0 and buffer[length-1] == '\n'){
268
235
  return true;
269
236
}
270
237
 
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
 
      }
 
238
char **add_to_argv(char **argv, int *argc, char *arg){
 
239
  if (argv == NULL){
 
240
    *argc = 1;
 
241
    argv = malloc(sizeof(char*) * 2);
 
242
    if(argv == NULL){
 
243
      return NULL;
295
244
    }
296
 
  }
297
 
  
298
 
  free(plugin_node);
 
245
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
246
    argv[1] = NULL;
 
247
  }
 
248
  *argc += 1;
 
249
  argv = realloc(argv, sizeof(char *)
 
250
                  * ((unsigned int) *argc + 1));
 
251
  if(argv == NULL){
 
252
    return NULL;
 
253
  }
 
254
  argv[*argc-1] = arg;
 
255
  argv[*argc] = NULL;   
 
256
  return argv;
299
257
}
300
258
 
301
 
static void free_plugin_list(void){
302
 
  while(plugin_list != NULL){
303
 
    free_plugin(plugin_list);
304
 
  }
 
259
static void free_plugin_list(plugin *plugin_list){
 
260
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
 
261
    next = plugin_list->next;
 
262
    free(plugin_list->name);
 
263
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
264
      free(*arg);
 
265
    }    
 
266
    free(plugin_list->argv);
 
267
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
268
      free(*env);
 
269
    }
 
270
    free(plugin_list->environ);
 
271
    free(plugin_list);
 
272
  }  
305
273
}
306
274
 
307
275
int main(int argc, char *argv[]){
308
 
  char *plugindir = NULL;
309
 
  char *argfile = NULL;
 
276
  const char *plugindir = "/lib/mandos/plugins.d";
 
277
  const char *argfile = ARGFILE;
310
278
  FILE *conffp;
311
279
  size_t d_name_len;
312
280
  DIR *dir = NULL;
335
303
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
336
304
  if(ret == -1){
337
305
    perror("sigaction");
338
 
    exitstatus = EXIT_FAILURE;
 
306
    exitstatus = EXIT_FAILURE;    
339
307
    goto fallback;
340
308
  }
341
309
  
344
312
    { .name = "global-options", .key = 'g',
345
313
      .arg = "OPTION[,OPTION[,...]]",
346
314
      .doc = "Options passed to all plugins" },
347
 
    { .name = "global-env", .key = 'e',
 
315
    { .name = "global-envs", .key = 'e',
348
316
      .arg = "VAR=value",
349
317
      .doc = "Environment variable passed to all plugins" },
350
318
    { .name = "options-for", .key = 'o',
351
319
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
352
320
      .doc = "Options passed only to specified plugin" },
353
 
    { .name = "env-for", .key = 'f',
 
321
    { .name = "envs-for", .key = 'f',
354
322
      .arg = "PLUGIN:ENV=value",
355
323
      .doc = "Environment variable passed to specified plugin" },
356
324
    { .name = "disable", .key = 'd',
359
327
    { .name = "plugin-dir", .key = 128,
360
328
      .arg = "DIRECTORY",
361
329
      .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 },
 
330
    { .name = "userid", .key = 129,
 
331
      .arg = "ID", .flags = 0,
 
332
      .doc = "User ID the plugins will run as", .group = 2 },
 
333
    { .name = "groupid", .key = 130,
 
334
      .arg = "ID", .flags = 0,
 
335
      .doc = "Group ID the plugins will run as", .group = 2 },
 
336
    { .name = "debug", .key = 131,
 
337
      .doc = "Debug mode", .group = 3 },
373
338
    { .name = NULL }
374
339
  };
375
340
  
376
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
 
                     struct argp_state *state) {
 
341
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
378
342
    /* Get the INPUT argument from `argp_parse', which we know is a
379
343
       pointer to our plugin list pointer. */
 
344
    plugin **plugins = state->input;
380
345
    switch (key) {
381
 
    case 'g':                   /* --global-options */
 
346
    case 'g':
382
347
      if (arg != NULL){
383
348
        char *p;
384
349
        while((p = strsep(&arg, ",")) != NULL){
385
350
          if(p[0] == '\0'){
386
351
            continue;
387
352
          }
388
 
          if(not add_argument(getplugin(NULL), p)){
 
353
          if(not add_argument(getplugin(NULL, plugins), p)){
389
354
            perror("add_argument");
390
355
            return ARGP_ERR_UNKNOWN;
391
356
          }
392
357
        }
393
358
      }
394
359
      break;
395
 
    case 'e':                   /* --global-env */
 
360
    case 'e':
396
361
      if(arg == NULL){
397
362
        break;
398
363
      }
401
366
        if(envdef == NULL){
402
367
          break;
403
368
        }
404
 
        if(not add_environment(getplugin(NULL), envdef, true)){
 
369
        if(not add_environment(getplugin(NULL, plugins), envdef)){
405
370
          perror("add_environment");
406
371
        }
407
372
      }
408
373
      break;
409
 
    case 'o':                   /* --options-for */
 
374
    case 'o':
410
375
      if (arg != NULL){
411
376
        char *p_name = strsep(&arg, ":");
412
377
        if(p_name[0] == '\0'){
422
387
            if(p[0] == '\0'){
423
388
              continue;
424
389
            }
425
 
            if(not add_argument(getplugin(p_name), p)){
 
390
            if(not add_argument(getplugin(p_name, plugins), p)){
426
391
              perror("add_argument");
427
392
              return ARGP_ERR_UNKNOWN;
428
393
            }
430
395
        }
431
396
      }
432
397
      break;
433
 
    case 'f':                   /* --env-for */
 
398
    case 'f':
434
399
      if(arg == NULL){
435
400
        break;
436
401
      }
444
409
          break;
445
410
        }
446
411
        envdef++;
447
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
412
        if(not add_environment(getplugin(p_name, plugins), envdef)){
448
413
          perror("add_environment");
449
414
        }
450
415
      }
451
416
      break;
452
 
    case 'd':                   /* --disable */
 
417
    case 'd':
453
418
      if (arg != NULL){
454
 
        plugin *p = getplugin(arg);
 
419
        plugin *p = getplugin(arg, plugins);
455
420
        if(p == NULL){
456
421
          return ARGP_ERR_UNKNOWN;
457
422
        }
458
423
        p->disabled = true;
459
424
      }
460
425
      break;
461
 
    case 128:                   /* --plugin-dir */
462
 
      plugindir = strdup(arg);
463
 
      if(plugindir == NULL){
464
 
        perror("strdup");
465
 
      }      
 
426
    case 128:
 
427
      plugindir = arg;
466
428
      break;
467
 
    case 129:                   /* --config-file */
468
 
      argfile = strdup(arg);
469
 
      if(argfile == NULL){
470
 
        perror("strdup");
471
 
      }
472
 
      break;      
473
 
    case 130:                   /* --userid */
 
429
    case 129:
474
430
      uid = (uid_t)strtol(arg, NULL, 10);
475
431
      break;
476
 
    case 131:                   /* --groupid */
 
432
    case 130:
477
433
      gid = (gid_t)strtol(arg, NULL, 10);
478
434
      break;
479
 
    case 132:                   /* --debug */
 
435
    case 131:
480
436
      debug = true;
481
437
      break;
482
438
    case ARGP_KEY_ARG:
490
446
    return 0;
491
447
  }
492
448
  
 
449
  plugin *plugin_list = NULL;
 
450
  
493
451
  struct argp argp = { .options = options, .parser = parse_opt,
494
 
                       .args_doc = "",
 
452
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
495
453
                       .doc = "Mandos plugin runner -- Run plugins" };
496
454
  
497
 
  /* Open the configfile if available */
498
 
  if (argfile == NULL){
499
 
    conffp = fopen(AFILE, "r");
500
 
  } else {
501
 
    conffp = fopen(argfile, "r");
502
 
  }  
 
455
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
456
  if (ret == ARGP_ERR_UNKNOWN){
 
457
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
458
    exitstatus = EXIT_FAILURE;
 
459
    goto fallback;
 
460
  }
 
461
 
 
462
  conffp = fopen(argfile, "r");
503
463
  if(conffp != NULL){
504
464
    char *org_line = NULL;
505
465
    char *p, *arg, *new_arg, *line;
508
468
    const char whitespace_delims[] = " \r\t\f\v\n";
509
469
    const char comment_delim[] = "#";
510
470
 
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
471
    while(true){
524
472
      sret = getline(&org_line, &size, conffp);
525
473
      if(sret == -1){
533
481
          continue;
534
482
        }
535
483
        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;        
 
484
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
485
        if (custom_argv == NULL){
 
486
          perror("add_to_argv");
 
487
          exitstatus = EXIT_FAILURE;
 
488
          goto fallback;
 
489
        }
554
490
      }
555
491
    }
556
492
    free(org_line);
557
 
  } else {
 
493
  } else{
558
494
    /* Check for harmful errors and go to fallback. Other errors might
559
495
       not affect opening plugins */
560
496
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
563
499
      goto fallback;
564
500
    }
565
501
  }
566
 
  /* If there was any arguments from configuration file,
567
 
     pass them to parser as command arguments */
 
502
 
568
503
  if(custom_argv != NULL){
569
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
570
 
                      0, NULL);
 
504
    custom_argv[0] = argv[0];
 
505
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
571
506
    if (ret == ARGP_ERR_UNKNOWN){
572
507
      fprintf(stderr, "Unknown error while parsing arguments\n");
573
508
      exitstatus = EXIT_FAILURE;
575
510
    }
576
511
  }
577
512
  
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
513
  if(debug){
588
514
    for(plugin *p = plugin_list; p != NULL; p=p->next){
589
515
      fprintf(stderr, "Plugin: %s has %d arguments\n",
598
524
    }
599
525
  }
600
526
  
601
 
  /* Strip permissions down to nobody */
602
527
  ret = setuid(uid);
603
528
  if (ret == -1){
604
529
    perror("setuid");
605
 
  }  
 
530
  }
 
531
  
606
532
  setgid(gid);
607
533
  if (ret == -1){
608
534
    perror("setgid");
609
535
  }
610
536
  
611
 
  if (plugindir == NULL){
612
 
    dir = opendir(PDIR);
613
 
  } else {
614
 
    dir = opendir(plugindir);
615
 
  }
616
 
  
 
537
  dir = opendir(plugindir);
617
538
  if(dir == NULL){
618
539
    perror("Could not open plugin dir");
619
540
    exitstatus = EXIT_FAILURE;
635
556
  
636
557
  FD_ZERO(&rfds_all);
637
558
  
638
 
  /* Read and execute any executable in the plugin directory*/
639
559
  while(true){
640
560
    dirst = readdir(dir);
641
561
    
642
 
    /* All directory entries have been processed */
 
562
    // All directory entries have been processed
643
563
    if(dirst == NULL){
644
564
      if (errno == EBADF){
645
565
        perror("readdir");
651
571
    
652
572
    d_name_len = strlen(dirst->d_name);
653
573
    
654
 
    /* Ignore dotfiles, backup files and other junk */
 
574
    // Ignore dotfiles, backup files and other junk
655
575
    {
656
576
      bool bad_name = false;
657
577
      
672
592
          break;
673
593
        }
674
594
      }
 
595
      
675
596
      if(bad_name){
676
597
        continue;
677
598
      }
 
599
      
678
600
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
679
601
        size_t suf_len = strlen(*suf);
680
602
        if((d_name_len >= suf_len)
707
629
      free(filename);
708
630
      continue;
709
631
    }
710
 
 
711
 
    /* Ignore non-executable files */
 
632
    
712
633
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
713
634
      if(debug){
714
635
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
717
638
      free(filename);
718
639
      continue;
719
640
    }
720
 
    
721
 
    plugin *p = getplugin(dirst->d_name);
 
641
    plugin *p = getplugin(dirst->d_name, &plugin_list);
722
642
    if(p == NULL){
723
643
      perror("getplugin");
724
644
      free(filename);
734
654
    }
735
655
    {
736
656
      /* Add global arguments to argument list for this plugin */
737
 
      plugin *g = getplugin(NULL);
 
657
      plugin *g = getplugin(NULL, &plugin_list);
738
658
      if(g != NULL){
739
659
        for(char **a = g->argv + 1; *a != NULL; a++){
740
660
          if(not add_argument(p, *a)){
743
663
        }
744
664
        /* Add global environment variables */
745
665
        for(char **e = g->environ; *e != NULL; e++){
746
 
          if(not add_environment(p, *e, false)){
 
666
          if(not add_environment(p, *e)){
747
667
            perror("add_environment");
748
668
          }
749
669
        }
754
674
       process, too. */
755
675
    if(p->environ[0] != NULL){
756
676
      for(char **e = environ; *e != NULL; e++){
757
 
        if(not add_environment(p, *e, false)){
 
677
        char *copy = strdup(*e);
 
678
        if(copy == NULL){
 
679
          perror("strdup");
 
680
          continue;
 
681
        }
 
682
        if(not add_environment(p, copy)){
758
683
          perror("add_environment");
759
684
        }
760
685
      }
761
686
    }
762
687
    
763
 
    int pipefd[2];
 
688
    int pipefd[2]; 
764
689
    ret = pipe(pipefd);
765
690
    if (ret == -1){
766
691
      perror("pipe");
767
692
      exitstatus = EXIT_FAILURE;
768
693
      goto fallback;
769
694
    }
770
 
    /* Ask OS to automatic close the pipe on exec */
771
695
    ret = set_cloexec_flag(pipefd[0]);
772
696
    if(ret < 0){
773
697
      perror("set_cloexec_flag");
787
711
      exitstatus = EXIT_FAILURE;
788
712
      goto fallback;
789
713
    }
790
 
    /* Starting a new process to be watched */
 
714
    // Starting a new process to be watched
791
715
    pid_t pid = fork();
792
716
    if(pid == -1){
793
717
      perror("fork");
831
755
      }
832
756
      /* no return */
833
757
    }
834
 
    /* Parent process */
835
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
758
    /* parent process */
836
759
    free(filename);
837
 
    plugin *new_plugin = getplugin(dirst->d_name);
838
 
    if (new_plugin == NULL){
839
 
      perror("getplugin");
 
760
    close(pipefd[1]);           /* close unused write end of pipe */
 
761
    process *new_process = malloc(sizeof(process));
 
762
    if (new_process == NULL){
 
763
      perror("malloc");
840
764
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
841
765
      if(ret < 0){
842
 
        perror("sigprocmask");
 
766
        perror("sigprocmask");
843
767
      }
844
768
      exitstatus = EXIT_FAILURE;
845
769
      goto fallback;
846
770
    }
847
771
    
848
 
    new_plugin->pid = pid;
849
 
    new_plugin->fd = pipefd[0];
850
 
    
 
772
    *new_process = (struct process){ .pid = pid,
 
773
                                     .fd = pipefd[0],
 
774
                                     .next = process_list };
 
775
    // List handling
 
776
    process_list = new_process;
851
777
    /* Unblock SIGCHLD so signal handler can be run if this process
852
778
       has already completed */
853
779
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
857
783
      goto fallback;
858
784
    }
859
785
    
860
 
    FD_SET(new_plugin->fd, &rfds_all);
 
786
    FD_SET(new_process->fd, &rfds_all);
861
787
    
862
 
    if (maxfd < new_plugin->fd){
863
 
      maxfd = new_plugin->fd;
 
788
    if (maxfd < new_process->fd){
 
789
      maxfd = new_process->fd;
864
790
    }
865
791
    
866
792
  }
867
793
  
 
794
  free_plugin_list(plugin_list);
 
795
  
868
796
  closedir(dir);
869
797
  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
 
    }
 
798
    
 
799
  if (process_list == NULL){
 
800
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
801
            " directory?\n");
 
802
    process_list = NULL;
880
803
  }
881
 
 
882
 
  /* Main loop while running plugins exist */
883
 
  while(plugin_list){
 
804
  while(process_list){
884
805
    fd_set rfds = rfds_all;
885
806
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
886
807
    if (select_ret == -1){
890
811
    }
891
812
    /* OK, now either a process completed, or something can be read
892
813
       from one of them */
893
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
814
    for(process *proc = process_list; proc ; proc = proc->next){
894
815
      /* Is this process completely done? */
895
816
      if(proc->eof and proc->completed){
896
817
        /* Only accept the plugin output if it exited cleanly */
897
818
        if(not WIFEXITED(proc->status)
898
819
           or WEXITSTATUS(proc->status) != 0){
899
820
          /* Bad exit by plugin */
900
 
 
901
821
          if(debug){
902
822
            if(WIFEXITED(proc->status)){
903
823
              fprintf(stderr, "Plugin %u exited with status %d\n",
912
832
                      (unsigned int) (proc->pid));
913
833
            }
914
834
          }
915
 
          
916
835
          /* Remove the plugin */
917
836
          FD_CLR(proc->fd, &rfds_all);
918
 
 
919
837
          /* Block signal while modifying process_list */
920
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
838
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
921
839
          if(ret < 0){
922
840
            perror("sigprocmask");
923
841
            exitstatus = EXIT_FAILURE;
924
842
            goto fallback;
925
843
          }
926
 
          free_plugin(proc);
 
844
          /* Delete this process entry from the list */
 
845
          if(process_list == proc){
 
846
            /* First one - simple */
 
847
            process_list = proc->next;
 
848
          } else {
 
849
            /* Second one or later */
 
850
            for(process *p = process_list; p != NULL; p = p->next){
 
851
              if(p->next == proc){
 
852
                p->next = proc->next;
 
853
                break;
 
854
              }
 
855
            }
 
856
          }
927
857
          /* We are done modifying process list, so unblock signal */
928
858
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
929
859
                             NULL);
930
860
          if(ret < 0){
931
861
            perror("sigprocmask");
932
 
            exitstatus = EXIT_FAILURE;
933
 
            goto fallback;
934
 
          }
935
 
          
936
 
          if(plugin_list == NULL){
937
 
            break;
938
 
          }
939
 
          continue;
 
862
          }
 
863
          free(proc->buffer);
 
864
          free(proc);
 
865
          /* We deleted this process from the list, so we can't go
 
866
             proc->next.  Therefore, start over from the beginning of
 
867
             the process list */
 
868
          break;
940
869
        }
941
 
        
942
870
        /* This process exited nicely, so print its buffer */
943
871
 
944
 
        bool bret = print_out_password(proc->buffer,
945
 
                                       proc->buffer_length);
 
872
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
946
873
        if(not bret){
947
874
          perror("print_out_password");
948
875
          exitstatus = EXIT_FAILURE;
949
876
        }
950
877
        goto fallback;
951
878
      }
952
 
      
953
879
      /* This process has not completed.  Does it have any output? */
954
880
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
955
881
        /* This process had nothing to say at this time */
985
911
 
986
912
 fallback:
987
913
  
988
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
989
 
    /* Fallback if all plugins failed, none are found or an error
990
 
       occured */
 
914
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
915
    /* Fallback if all plugins failed, none are found or an error occured */
991
916
    bool bret;
992
917
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
993
918
    char *passwordbuffer = getpass("Password: ");
1006
931
  }
1007
932
 
1008
933
  if(custom_argv != NULL){
1009
 
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
934
    for(char **arg = custom_argv; *arg != NULL; arg++){
1010
935
      free(*arg);
1011
936
    }
1012
937
    free(custom_argv);
1013
938
  }
 
939
  free_plugin_list(plugin_list);
1014
940
  
1015
941
  if(dir != NULL){
1016
942
    closedir(dir);
1017
943
  }
1018
944
  
1019
945
  /* 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
 
      }
 
946
  for(process *next; process_list != NULL; process_list = next){
 
947
    next = process_list->next;
 
948
    close(process_list->fd);
 
949
    ret = kill(process_list->pid, SIGTERM);
 
950
    if(ret == -1 and errno != ESRCH){
 
951
      /* set-uid proccesses migth not get closed */
 
952
      perror("kill");
1028
953
    }
 
954
    free(process_list->buffer);
 
955
    free(process_list);
1029
956
  }
1030
957
  
1031
958
  /* Wait for any remaining child processes to terminate */
1035
962
  if(errno != ECHILD){
1036
963
    perror("wait");
1037
964
  }
1038
 
 
1039
 
  free_plugin_list();
1040
 
  
1041
 
  free(plugindir);
1042
 
  free(argfile);
1043
965
  
1044
966
  return exitstatus;
1045
967
}