/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-08-31 16:04:47 UTC
  • mfrom: (24.1.77 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080831160447-2xte5k90onspphki
Merge.

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, error_t */
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG,
 
60
                                   error_t */
60
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
61
62
                                   sigaddset(), sigaction(),
62
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
65
#include <errno.h>              /* errno, EBADF */
65
66
 
66
67
#define BUFFER_SIZE 256
67
 
#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"
68
71
 
69
72
const char *argp_program_version = "plugin-runner 1.0";
70
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
74
 
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;
 
75
struct plugin;
85
76
 
86
77
typedef struct plugin{
87
78
  char *name;                   /* can be NULL or any plugin name */
90
81
  char **environ;
91
82
  int envc;
92
83
  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;
93
94
  struct plugin *next;
94
95
} plugin;
95
96
 
96
 
static plugin *getplugin(char *name, plugin **plugin_list){
97
 
  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){
98
104
    if ((p->name == name)
99
105
        or (p->name and name and (strcmp(p->name, name) == 0))){
100
106
      return p;
105
111
  if (new_plugin == NULL){
106
112
    return NULL;
107
113
  }
108
 
  *new_plugin = (plugin) { .name = name,
 
114
  char *copy_name = NULL;
 
115
  if(name != NULL){
 
116
    copy_name = strdup(name);
 
117
    if(copy_name == NULL){
 
118
      return NULL;
 
119
    }
 
120
  }
 
121
 
 
122
  *new_plugin = (plugin) { .name = copy_name,
109
123
                           .argc = 1,
110
 
                           .envc = 0,
111
124
                           .disabled = false,
112
 
                           .next = *plugin_list };
 
125
                           .next = plugin_list };
113
126
  
114
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
115
128
  if (new_plugin->argv == NULL){
 
129
    free(copy_name);
116
130
    free(new_plugin);
117
131
    return NULL;
118
132
  }
119
 
  new_plugin->argv[0] = name;
 
133
  new_plugin->argv[0] = copy_name;
120
134
  new_plugin->argv[1] = NULL;
121
135
 
122
136
  new_plugin->environ = malloc(sizeof(char *));
123
137
  if(new_plugin->environ == NULL){
 
138
    free(copy_name);
124
139
    free(new_plugin->argv);
125
140
    free(new_plugin);
126
141
    return NULL;
127
142
  }
128
143
  new_plugin->environ[0] = NULL;
 
144
 
129
145
  /* Append the new plugin to the list */
130
 
  *plugin_list = new_plugin;
 
146
  plugin_list = new_plugin;
131
147
  return new_plugin;
132
148
}
133
149
 
170
186
  return add_to_char_array(def, &(p->environ), &(p->envc));
171
187
}
172
188
 
173
 
 
174
189
/*
175
190
 * Based on the example in the GNU LibC manual chapter 13.13 "File
176
191
 * Descriptor Flags".
187
202
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
188
203
}
189
204
 
190
 
process *process_list = NULL;
191
205
 
192
 
/* Mark a process as completed when it exits, and save its exit
 
206
/* Mark processes as completed when they exit, and save their exit
193
207
   status. */
194
208
void handle_sigchld(__attribute__((unused)) int sig){
195
 
  process *proc = process_list;
196
 
  int status;
197
 
  pid_t pid = wait(&status);
198
 
  if(pid == -1){
199
 
    perror("wait");
200
 
    return;
201
 
  }
202
 
  while(proc != NULL and proc->pid != pid){
203
 
    proc = proc->next;
204
 
  }
205
 
  if(proc == NULL){
206
 
    /* Process not found in process list */
207
 
    return;
208
 
  }
209
 
  proc->status = status;
210
 
  proc->completed = true;
 
209
  while(true){
 
210
    plugin *proc = plugin_list;
 
211
    int status;
 
212
    pid_t pid = waitpid(-1, &status, WNOHANG);
 
213
    if(pid == 0){
 
214
      /* Only still running child processes */
 
215
      break;
 
216
    }
 
217
    if(pid == -1){
 
218
      if (errno != ECHILD){
 
219
        perror("waitpid");
 
220
      }
 
221
      /* No child processes */
 
222
      break;
 
223
    }
 
224
 
 
225
    /* A child exited, find it in process_list */
 
226
    while(proc != NULL and proc->pid != pid){
 
227
      proc = proc->next;
 
228
    }
 
229
    if(proc == NULL){
 
230
      /* Process not found in process list */
 
231
      continue;
 
232
    }
 
233
    proc->status = status;
 
234
    proc->completed = true;
 
235
  }
211
236
}
212
237
 
 
238
/* Prints out a password to stdout */
213
239
bool print_out_password(const char *buffer, size_t length){
214
240
  ssize_t ret;
215
241
  if(length>0 and buffer[length-1] == '\n'){
225
251
  return true;
226
252
}
227
253
 
228
 
char **add_to_argv(char **argv, int *argc, char *arg){
229
 
  if (argv == NULL){
230
 
    *argc = 1;
231
 
    argv = malloc(sizeof(char*) * 2);
232
 
    if(argv == NULL){
233
 
      return NULL;
 
254
/* Removes and free a plugin from the plugin list */
 
255
static void free_plugin(plugin *plugin_node){
 
256
  
 
257
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
258
    free(*arg);
 
259
  }
 
260
  free(plugin_node->argv);
 
261
  for(char **env = plugin_node->environ; *env != NULL; env++){
 
262
    free(*env);
 
263
  }
 
264
  free(plugin_node->environ);
 
265
  free(plugin_node->buffer);
 
266
 
 
267
  /* Removes the plugin from the singly-linked list */
 
268
  if(plugin_node == plugin_list){
 
269
    /* First one - simple */
 
270
    plugin_list = plugin_list->next;
 
271
  } else {
 
272
    /* Second one or later */
 
273
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
274
      if(p->next == plugin_node){
 
275
        p->next = plugin_node->next;
 
276
        break;
 
277
      }
234
278
    }
235
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
236
 
    argv[1] = NULL;
237
 
  }
238
 
  *argc += 1;
239
 
  argv = realloc(argv, sizeof(char *)
240
 
                  * ((unsigned int) *argc + 1));
241
 
  if(argv == NULL){
242
 
    return NULL;
243
 
  }
244
 
  argv[*argc-1] = arg;
245
 
  argv[*argc] = NULL;   
246
 
  return argv;
 
279
  }
 
280
  
 
281
  free(plugin_node);
 
282
}
 
283
 
 
284
static void free_plugin_list(void){
 
285
  while(plugin_list != NULL){
 
286
    free_plugin(plugin_list);
 
287
  }
247
288
}
248
289
 
249
290
int main(int argc, char *argv[]){
250
 
  const char *plugindir = "/lib/mandos/plugins.d";
251
 
  const char *argfile = ARGFILE;
 
291
  char *plugindir = NULL;
 
292
  char *argfile = NULL;
252
293
  FILE *conffp;
253
294
  size_t d_name_len;
254
295
  DIR *dir = NULL;
269
310
  /* Establish a signal handler */
270
311
  sigemptyset(&sigchld_action.sa_mask);
271
312
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
272
 
  if(ret < 0){
 
313
  if(ret == -1){
273
314
    perror("sigaddset");
274
 
    exit(EXIT_FAILURE);
 
315
    exitstatus = EXIT_FAILURE;
 
316
    goto fallback;
275
317
  }
276
318
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
277
 
  if(ret < 0){
 
319
  if(ret == -1){
278
320
    perror("sigaction");
279
 
    exit(EXIT_FAILURE);
 
321
    exitstatus = EXIT_FAILURE;
 
322
    goto fallback;
280
323
  }
281
324
  
282
325
  /* The options we understand. */
299
342
    { .name = "plugin-dir", .key = 128,
300
343
      .arg = "DIRECTORY",
301
344
      .doc = "Specify a different plugin directory", .group = 2 },
302
 
    { .name = "userid", .key = 129,
303
 
      .arg = "ID", .flags = 0,
304
 
      .doc = "User ID the plugins will run as", .group = 2 },
305
 
    { .name = "groupid", .key = 130,
306
 
      .arg = "ID", .flags = 0,
307
 
      .doc = "Group ID the plugins will run as", .group = 2 },
308
 
    { .name = "debug", .key = 131,
309
 
      .doc = "Debug mode", .group = 3 },
 
345
    { .name = "config-file", .key = 129,
 
346
      .arg = "FILE",
 
347
      .doc = "Specify a different configuration file", .group = 2 },
 
348
    { .name = "userid", .key = 130,
 
349
      .arg = "ID", .flags = 0,
 
350
      .doc = "User ID the plugins will run as", .group = 3 },
 
351
    { .name = "groupid", .key = 131,
 
352
      .arg = "ID", .flags = 0,
 
353
      .doc = "Group ID the plugins will run as", .group = 3 },
 
354
    { .name = "debug", .key = 132,
 
355
      .doc = "Debug mode", .group = 4 },
310
356
    { .name = NULL }
311
357
  };
312
358
  
313
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
 
359
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
314
360
    /* Get the INPUT argument from `argp_parse', which we know is a
315
361
       pointer to our plugin list pointer. */
316
 
    plugin **plugins = state->input;
317
362
    switch (key) {
318
 
    case 'g':
 
363
    case 'g':                   /* --global-options */
319
364
      if (arg != NULL){
320
365
        char *p;
321
366
        while((p = strsep(&arg, ",")) != NULL){
322
367
          if(p[0] == '\0'){
323
368
            continue;
324
369
          }
325
 
          if(not add_argument(getplugin(NULL, plugins), p)){
 
370
          if(not add_argument(getplugin(NULL), p)){
326
371
            perror("add_argument");
327
372
            return ARGP_ERR_UNKNOWN;
328
373
          }
329
374
        }
330
375
      }
331
376
      break;
332
 
    case 'e':
 
377
    case 'e':                   /* --global-envs */
333
378
      if(arg == NULL){
334
379
        break;
335
380
      }
338
383
        if(envdef == NULL){
339
384
          break;
340
385
        }
341
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
 
386
        if(not add_environment(getplugin(NULL), envdef)){
342
387
          perror("add_environment");
343
388
        }
344
389
      }
345
390
      break;
346
 
    case 'o':
 
391
    case 'o':                   /* --options-for */
347
392
      if (arg != NULL){
348
393
        char *p_name = strsep(&arg, ":");
349
394
        if(p_name[0] == '\0'){
359
404
            if(p[0] == '\0'){
360
405
              continue;
361
406
            }
362
 
            if(not add_argument(getplugin(p_name, plugins), p)){
 
407
            if(not add_argument(getplugin(p_name), p)){
363
408
              perror("add_argument");
364
409
              return ARGP_ERR_UNKNOWN;
365
410
            }
367
412
        }
368
413
      }
369
414
      break;
370
 
    case 'f':
 
415
    case 'f':                   /* --envs-for */
371
416
      if(arg == NULL){
372
417
        break;
373
418
      }
381
426
          break;
382
427
        }
383
428
        envdef++;
384
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
429
        if(not add_environment(getplugin(p_name), envdef)){
385
430
          perror("add_environment");
386
431
        }
387
432
      }
388
433
      break;
389
 
    case 'd':
 
434
    case 'd':                   /* --disable */
390
435
      if (arg != NULL){
391
 
        plugin *p = getplugin(arg, plugins);
 
436
        plugin *p = getplugin(arg);
392
437
        if(p == NULL){
393
438
          return ARGP_ERR_UNKNOWN;
394
439
        }
395
440
        p->disabled = true;
396
441
      }
397
442
      break;
398
 
    case 128:
399
 
      plugindir = arg;
 
443
    case 128:                   /* --plugin-dir */
 
444
      plugindir = strdup(arg);
 
445
      if(plugindir == NULL){
 
446
        perror("strdup");
 
447
      }      
400
448
      break;
401
 
    case 129:
 
449
    case 129:                   /* --config-file */
 
450
      argfile = strdup(arg);
 
451
      if(argfile == NULL){
 
452
        perror("strdup");
 
453
      }
 
454
      break;      
 
455
    case 130:                   /* --userid */
402
456
      uid = (uid_t)strtol(arg, NULL, 10);
403
457
      break;
404
 
    case 130:
 
458
    case 131:                   /* --groupid */
405
459
      gid = (gid_t)strtol(arg, NULL, 10);
406
460
      break;
407
 
    case 131:
 
461
    case 132:                   /* --debug */
408
462
      debug = true;
409
463
      break;
410
464
    case ARGP_KEY_ARG:
418
472
    return 0;
419
473
  }
420
474
  
421
 
  plugin *plugin_list = NULL;
422
 
  
423
475
  struct argp argp = { .options = options, .parser = parse_opt,
424
476
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
425
477
                       .doc = "Mandos plugin runner -- Run plugins" };
426
478
  
427
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
479
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
428
480
  if (ret == ARGP_ERR_UNKNOWN){
429
481
    fprintf(stderr, "Unknown error while parsing arguments\n");
430
482
    exitstatus = EXIT_FAILURE;
431
 
    goto end;
 
483
    goto fallback;
432
484
  }
433
485
 
434
 
  conffp = fopen(argfile, "r");
 
486
  /* Opens the configfile if aviable */
 
487
  if (argfile == NULL){
 
488
    conffp = fopen(AFILE, "r");
 
489
  } else {
 
490
    conffp = fopen(argfile, "r");
 
491
  }  
435
492
  if(conffp != NULL){
436
493
    char *org_line = NULL;
 
494
    char *p, *arg, *new_arg, *line;
437
495
    size_t size = 0;
438
496
    ssize_t sret;
439
 
    char *p, *arg, *new_arg, *line;
440
497
    const char whitespace_delims[] = " \r\t\f\v\n";
441
498
    const char comment_delim[] = "#";
442
499
 
 
500
    custom_argc = 1;
 
501
    custom_argv = malloc(sizeof(char*) * 2);
 
502
    if(custom_argv == NULL){
 
503
      perror("malloc");
 
504
      exitstatus = EXIT_FAILURE;
 
505
      goto fallback;
 
506
    }
 
507
    custom_argv[0] = argv[0];
 
508
    custom_argv[1] = NULL;
 
509
 
 
510
    /* for each line in the config file, strip whitespace and ignore commented text */
443
511
    while(true){
444
512
      sret = getline(&org_line, &size, conffp);
445
513
      if(sret == -1){
453
521
          continue;
454
522
        }
455
523
        new_arg = strdup(p);
456
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
457
 
        if (custom_argv == NULL){
458
 
          perror("add_to_argv");
459
 
          exitstatus = EXIT_FAILURE;
460
 
          goto end;
461
 
        }
 
524
        if(new_arg == NULL){
 
525
          perror("strdup");
 
526
          exitstatus = EXIT_FAILURE;
 
527
          free(org_line);
 
528
          goto fallback;
 
529
        }
 
530
        
 
531
        custom_argc += 1;
 
532
        custom_argv = realloc(custom_argv, sizeof(char *)
 
533
                              * ((unsigned int) custom_argc + 1));
 
534
        if(custom_argv == NULL){
 
535
          perror("realloc");
 
536
          exitstatus = EXIT_FAILURE;
 
537
          free(org_line);
 
538
          goto fallback;
 
539
        }
 
540
        custom_argv[custom_argc-1] = new_arg;
 
541
        custom_argv[custom_argc] = NULL;        
462
542
      }
463
543
    }
464
544
    free(org_line);
468
548
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
469
549
      perror("fopen");
470
550
      exitstatus = EXIT_FAILURE;
471
 
      goto end;
 
551
      goto fallback;
472
552
    }
473
553
  }
474
 
 
 
554
  /* If there was any arguments from configuration file,
 
555
     pass them to parser as command arguments */
475
556
  if(custom_argv != NULL){
476
 
    custom_argv[0] = argv[0];
477
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
557
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
478
558
    if (ret == ARGP_ERR_UNKNOWN){
479
559
      fprintf(stderr, "Unknown error while parsing arguments\n");
480
560
      exitstatus = EXIT_FAILURE;
481
 
      goto end;
 
561
      goto fallback;
482
562
    }
483
563
  }
484
564
  
495
575
      }
496
576
    }
497
577
  }
498
 
  
 
578
 
 
579
  /* Strip permissions down to nobody */
499
580
  ret = setuid(uid);
500
581
  if (ret == -1){
501
582
    perror("setuid");
502
 
  }
503
 
  
 
583
  }  
504
584
  setgid(gid);
505
585
  if (ret == -1){
506
586
    perror("setgid");
507
587
  }
 
588
 
 
589
  if (plugindir == NULL){
 
590
    dir = opendir(PDIR);
 
591
  } else {
 
592
    dir = opendir(plugindir);
 
593
  }
508
594
  
509
 
  dir = opendir(plugindir);
510
595
  if(dir == NULL){
511
596
    perror("Could not open plugin dir");
512
597
    exitstatus = EXIT_FAILURE;
513
 
    goto end;
 
598
    goto fallback;
514
599
  }
515
600
  
516
601
  /* Set the FD_CLOEXEC flag on the directory, if possible */
521
606
      if(ret < 0){
522
607
        perror("set_cloexec_flag");
523
608
        exitstatus = EXIT_FAILURE;
524
 
        goto end;
 
609
        goto fallback;
525
610
      }
526
611
    }
527
612
  }
528
613
  
529
614
  FD_ZERO(&rfds_all);
530
 
  
 
615
 
 
616
  /* Read and execute any executable in the plugin directory*/
531
617
  while(true){
532
618
    dirst = readdir(dir);
533
619
    
536
622
      if (errno == EBADF){
537
623
        perror("readdir");
538
624
        exitstatus = EXIT_FAILURE;
539
 
        goto end;
 
625
        goto fallback;
540
626
      }
541
627
      break;
542
628
    }
564
650
          break;
565
651
        }
566
652
      }
567
 
      
568
653
      if(bad_name){
569
654
        continue;
570
655
      }
571
 
      
572
656
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
573
657
        size_t suf_len = strlen(*suf);
574
658
        if((d_name_len >= suf_len)
601
685
      free(filename);
602
686
      continue;
603
687
    }
604
 
    
 
688
 
 
689
    /* Ignore non-executable files */
605
690
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
606
691
      if(debug){
607
692
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
610
695
      free(filename);
611
696
      continue;
612
697
    }
613
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
698
    
 
699
    plugin *p = getplugin(dirst->d_name);
614
700
    if(p == NULL){
615
701
      perror("getplugin");
616
702
      free(filename);
626
712
    }
627
713
    {
628
714
      /* Add global arguments to argument list for this plugin */
629
 
      plugin *g = getplugin(NULL, &plugin_list);
 
715
      plugin *g = getplugin(NULL);
630
716
      if(g != NULL){
631
717
        for(char **a = g->argv + 1; *a != NULL; a++){
632
718
          if(not add_argument(p, *a)){
657
743
      }
658
744
    }
659
745
    
660
 
    int pipefd[2]; 
 
746
    int pipefd[2];
661
747
    ret = pipe(pipefd);
662
748
    if (ret == -1){
663
749
      perror("pipe");
664
750
      exitstatus = EXIT_FAILURE;
665
 
      goto end;
 
751
      goto fallback;
666
752
    }
 
753
    /* Ask OS to automatic close the pipe on exec */
667
754
    ret = set_cloexec_flag(pipefd[0]);
668
755
    if(ret < 0){
669
756
      perror("set_cloexec_flag");
670
757
      exitstatus = EXIT_FAILURE;
671
 
      goto end;
 
758
      goto fallback;
672
759
    }
673
760
    ret = set_cloexec_flag(pipefd[1]);
674
761
    if(ret < 0){
675
762
      perror("set_cloexec_flag");
676
763
      exitstatus = EXIT_FAILURE;
677
 
      goto end;
 
764
      goto fallback;
678
765
    }
679
766
    /* Block SIGCHLD until process is safely in process list */
680
767
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
681
768
    if(ret < 0){
682
769
      perror("sigprocmask");
683
770
      exitstatus = EXIT_FAILURE;
684
 
      goto end;
 
771
      goto fallback;
685
772
    }
686
773
    // Starting a new process to be watched
687
774
    pid_t pid = fork();
688
775
    if(pid == -1){
689
776
      perror("fork");
690
777
      exitstatus = EXIT_FAILURE;
691
 
      goto end;
 
778
      goto fallback;
692
779
    }
693
780
    if(pid == 0){
694
781
      /* this is the child process */
727
814
      }
728
815
      /* no return */
729
816
    }
730
 
    /* parent process */
 
817
    /* Parent process */
 
818
    close(pipefd[1]);           /* Close unused write end of pipe */
731
819
    free(filename);
732
 
    close(pipefd[1]);           /* close unused write end of pipe */
733
 
    process *new_process = malloc(sizeof(process));
734
 
    if (new_process == NULL){
735
 
      perror("malloc");
 
820
    plugin *new_plugin = getplugin(dirst->d_name);
 
821
    if (new_plugin == NULL){
 
822
      perror("getplugin");
736
823
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
737
824
      if(ret < 0){
738
 
        perror("sigprocmask");
 
825
        perror("sigprocmask");
739
826
      }
740
827
      exitstatus = EXIT_FAILURE;
741
 
      goto end;
 
828
      goto fallback;
742
829
    }
743
830
    
744
 
    *new_process = (struct process){ .pid = pid,
745
 
                                     .fd = pipefd[0],
746
 
                                     .next = process_list };
747
 
    // List handling
748
 
    process_list = new_process;
 
831
    new_plugin->pid = pid;
 
832
    new_plugin->fd = pipefd[0];
 
833
    
749
834
    /* Unblock SIGCHLD so signal handler can be run if this process
750
835
       has already completed */
751
836
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
752
837
    if(ret < 0){
753
838
      perror("sigprocmask");
754
839
      exitstatus = EXIT_FAILURE;
755
 
      goto end;
756
 
    }
757
 
    
758
 
    FD_SET(new_process->fd, &rfds_all);
759
 
    
760
 
    if (maxfd < new_process->fd){
761
 
      maxfd = new_process->fd;
762
 
    }
763
 
    
764
 
  }
765
 
  
766
 
  /* Free the plugin list */
767
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
768
 
    next = plugin_list->next;
769
 
    free(plugin_list->argv);
770
 
    if(plugin_list->environ[0] != NULL){
771
 
      for(char **e = plugin_list->environ; *e != NULL; e++){
772
 
        free(*e);
773
 
      }
774
 
    }
775
 
    free(plugin_list);
 
840
      goto fallback;
 
841
    }
 
842
    
 
843
    FD_SET(new_plugin->fd, &rfds_all);
 
844
    
 
845
    if (maxfd < new_plugin->fd){
 
846
      maxfd = new_plugin->fd;
 
847
    }
 
848
    
776
849
  }
777
850
  
778
851
  closedir(dir);
779
852
  dir = NULL;
780
 
    
781
 
  if (process_list == NULL){
782
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
783
 
            " directory?\n");
784
 
    process_list = NULL;
 
853
 
 
854
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
855
    if(p->pid != 0){
 
856
      break;
 
857
    }
 
858
    if(p->next == NULL){
 
859
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
860
              " directory?\n");
 
861
      free_plugin_list();
 
862
    }
785
863
  }
786
 
  while(process_list){
 
864
 
 
865
  /* Main loop while running plugins exist */
 
866
  while(plugin_list){
787
867
    fd_set rfds = rfds_all;
788
868
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
789
869
    if (select_ret == -1){
790
870
      perror("select");
791
871
      exitstatus = EXIT_FAILURE;
792
 
      goto end;
 
872
      goto fallback;
793
873
    }
794
874
    /* OK, now either a process completed, or something can be read
795
875
       from one of them */
796
 
    for(process *proc = process_list; proc ; proc = proc->next){
 
876
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
797
877
      /* Is this process completely done? */
798
878
      if(proc->eof and proc->completed){
799
879
        /* Only accept the plugin output if it exited cleanly */
800
880
        if(not WIFEXITED(proc->status)
801
881
           or WEXITSTATUS(proc->status) != 0){
802
882
          /* Bad exit by plugin */
 
883
 
803
884
          if(debug){
804
885
            if(WIFEXITED(proc->status)){
805
886
              fprintf(stderr, "Plugin %u exited with status %d\n",
814
895
                      (unsigned int) (proc->pid));
815
896
            }
816
897
          }
 
898
          
817
899
          /* Remove the plugin */
818
900
          FD_CLR(proc->fd, &rfds_all);
 
901
 
819
902
          /* Block signal while modifying process_list */
820
 
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
903
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
821
904
          if(ret < 0){
822
905
            perror("sigprocmask");
823
906
            exitstatus = EXIT_FAILURE;
824
 
            goto end;
825
 
          }
826
 
          /* Delete this process entry from the list */
827
 
          if(process_list == proc){
828
 
            /* First one - simple */
829
 
            process_list = proc->next;
830
 
          } else {
831
 
            /* Second one or later */
832
 
            for(process *p = process_list; p != NULL; p = p->next){
833
 
              if(p->next == proc){
834
 
                p->next = proc->next;
835
 
                break;
836
 
              }
837
 
            }
838
 
          }
 
907
            goto fallback;
 
908
          }
 
909
          free_plugin(proc);
839
910
          /* We are done modifying process list, so unblock signal */
840
911
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
841
912
                             NULL);
842
913
          if(ret < 0){
843
914
            perror("sigprocmask");
844
 
          }
845
 
          free(proc->buffer);
846
 
          free(proc);
847
 
          /* We deleted this process from the list, so we can't go
848
 
             proc->next.  Therefore, start over from the beginning of
849
 
             the process list */
850
 
          break;
 
915
            exitstatus = EXIT_FAILURE;
 
916
            goto fallback;
 
917
          }
 
918
          
 
919
          if(plugin_list == NULL){
 
920
            break;
 
921
          }
 
922
          continue;
851
923
        }
 
924
        
852
925
        /* This process exited nicely, so print its buffer */
853
926
 
854
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
927
        bool bret = print_out_password(proc->buffer,
 
928
                                       proc->buffer_length);
855
929
        if(not bret){
856
930
          perror("print_out_password");
857
931
          exitstatus = EXIT_FAILURE;
858
932
        }
859
 
        goto end;
 
933
        goto fallback;
860
934
      }
 
935
      
861
936
      /* This process has not completed.  Does it have any output? */
862
937
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
863
938
        /* This process had nothing to say at this time */
870
945
        if (proc->buffer == NULL){
871
946
          perror("malloc");
872
947
          exitstatus = EXIT_FAILURE;
873
 
          goto end;
 
948
          goto fallback;
874
949
        }
875
950
        proc->buffer_size += BUFFER_SIZE;
876
951
      }
891
966
  }
892
967
 
893
968
 
894
 
 end:
 
969
 fallback:
895
970
  
896
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
897
 
    /* Fallback if all plugins failed, none are found or an error occured */
 
971
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
972
    /* Fallback if all plugins failed, none are found or an error
 
973
       occured */
898
974
    bool bret;
899
975
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
900
976
    char *passwordbuffer = getpass("Password: ");
902
978
    if(not bret){
903
979
      perror("print_out_password");
904
980
      exitstatus = EXIT_FAILURE;
905
 
      goto end;
906
981
    }
907
982
  }
908
983
  
909
984
  /* Restore old signal handler */
910
 
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
911
 
  
912
 
  free(custom_argv);
913
 
  
914
 
  /* Free the plugin list */
915
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
916
 
    next = plugin_list->next;
917
 
    free(plugin_list->argv);
918
 
    if(plugin_list->environ[0] != NULL){
919
 
      for(char **e = plugin_list->environ; *e != NULL; e++){
920
 
        free(*e);
921
 
      }
 
985
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
986
  if(ret == -1){
 
987
    perror("sigaction");
 
988
    exitstatus = EXIT_FAILURE;
 
989
  }
 
990
 
 
991
  if(custom_argv != NULL){
 
992
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
993
      free(*arg);
922
994
    }
923
 
    free(plugin_list->environ);
924
 
    free(plugin_list);
 
995
    free(custom_argv);
925
996
  }
926
997
  
927
998
  if(dir != NULL){
929
1000
  }
930
1001
  
931
1002
  /* Free the process list and kill the processes */
932
 
  for(process *next; process_list != NULL; process_list = next){
933
 
    next = process_list->next;
934
 
    close(process_list->fd);
935
 
    ret = kill(process_list->pid, SIGTERM);
936
 
    if(ret == -1 and errno != ESRCH){
937
 
      /* set-uid proccesses migth not get closed */
938
 
      perror("kill");
 
1003
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
1004
    if(p->pid != 0){
 
1005
      close(p->fd);
 
1006
      ret = kill(p->pid, SIGTERM);
 
1007
      if(ret == -1 and errno != ESRCH){
 
1008
        /* Set-uid proccesses might not get closed */
 
1009
        perror("kill");
 
1010
      }
939
1011
    }
940
 
    free(process_list->buffer);
941
 
    free(process_list);
942
1012
  }
943
1013
  
944
1014
  /* Wait for any remaining child processes to terminate */
948
1018
  if(errno != ECHILD){
949
1019
    perror("wait");
950
1020
  }
 
1021
 
 
1022
  free_plugin_list();
 
1023
  
 
1024
  free(plugindir);
 
1025
  free(argfile);
951
1026
  
952
1027
  return exitstatus;
953
1028
}