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,
 
 
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 */
 
67
66
#define BUFFER_SIZE 256
 
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"
 
72
69
const char *argp_program_version = "plugin-runner 1.0";
 
73
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
 
125
114
  new_plugin->argv = malloc(sizeof(char *) * 2);
 
126
115
  if (new_plugin->argv == NULL){
 
128
116
    free(new_plugin);
 
131
 
  new_plugin->argv[0] = copy_name;
 
 
119
  new_plugin->argv[0] = name;
 
132
120
  new_plugin->argv[1] = NULL;
 
134
122
  new_plugin->environ = malloc(sizeof(char *));
 
135
123
  if(new_plugin->environ == NULL){
 
137
124
    free(new_plugin->argv);
 
138
125
    free(new_plugin);
 
 
203
190
process *process_list = NULL;
 
205
 
/* Mark processes as completed when they exit, and save their exit
 
 
192
/* Mark a process as completed when it exits, and save its exit
 
207
194
void handle_sigchld(__attribute__((unused)) int sig){
 
209
 
    process *proc = process_list;
 
211
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
 
213
 
      /* Only still running child processes */
 
217
 
      if (errno != ECHILD){
 
220
 
      /* No child processes */
 
224
 
    /* A child exited, find it in process_list */
 
225
 
    while(proc != NULL and proc->pid != pid){
 
229
 
      /* Process not found in process list */
 
232
 
    proc->status = status;
 
233
 
    proc->completed = true;
 
 
195
  process *proc = process_list;
 
 
197
  pid_t pid = wait(&status);
 
 
202
  while(proc != NULL and proc->pid != pid){
 
 
206
    /* Process not found in process list */
 
 
209
  proc->status = status;
 
 
210
  proc->completed = true;
 
237
213
bool print_out_password(const char *buffer, size_t length){
 
 
252
 
static void free_plugin_list(plugin *plugin_list){
 
253
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
254
 
    next = plugin_list->next;
 
255
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
258
 
    free(plugin_list->argv);
 
259
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
262
 
    free(plugin_list->environ);
 
 
228
char **add_to_argv(char **argv, int *argc, char *arg){
 
 
231
    argv = malloc(sizeof(char*) * 2);
 
 
235
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
 
239
  argv = realloc(argv, sizeof(char *)
 
 
240
                  * ((unsigned int) *argc + 1));
 
267
249
int main(int argc, char *argv[]){
 
268
 
  char *plugindir = NULL;
 
269
 
  char *argfile = NULL;
 
 
250
  const char *plugindir = "/lib/mandos/plugins.d";
 
 
251
  const char *argfile = ARGFILE;
 
271
253
  size_t d_name_len;
 
 
287
269
  /* Establish a signal handler */
 
288
270
  sigemptyset(&sigchld_action.sa_mask);
 
289
271
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
 
291
273
    perror("sigaddset");
 
292
 
    exitstatus = EXIT_FAILURE;
 
295
276
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
 
297
278
    perror("sigaction");
 
298
 
    exitstatus = EXIT_FAILURE;
 
302
282
  /* The options we understand. */
 
 
319
299
    { .name = "plugin-dir", .key = 128,
 
320
300
      .arg = "DIRECTORY",
 
321
301
      .doc = "Specify a different plugin directory", .group = 2 },
 
322
 
    { .name = "config-file", .key = 129,
 
324
 
      .doc = "Specify a different configuration file", .group = 2 },
 
325
 
    { .name = "userid", .key = 130,
 
326
 
      .arg = "ID", .flags = 0,
 
327
 
      .doc = "User ID the plugins will run as", .group = 3 },
 
328
 
    { .name = "groupid", .key = 131,
 
329
 
      .arg = "ID", .flags = 0,
 
330
 
      .doc = "Group ID the plugins will run as", .group = 3 },
 
331
 
    { .name = "debug", .key = 132,
 
332
 
      .doc = "Debug mode", .group = 4 },
 
 
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 },
 
 
422
 
      plugindir = strdup(arg);
 
423
 
      if(plugindir == NULL){
 
428
 
      argfile = strdup(arg);
 
 
402
      uid = (uid_t)strtol(arg, NULL, 10);
 
434
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
 
405
      gid = (gid_t)strtol(arg, NULL, 10);
 
437
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
442
410
    case ARGP_KEY_ARG:
 
 
460
428
  if (ret == ARGP_ERR_UNKNOWN){
 
461
429
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
462
430
    exitstatus = EXIT_FAILURE;
 
466
 
  if (argfile == NULL){
 
467
 
    conffp = fopen(AFILE, "r");
 
469
 
    conffp = fopen(argfile, "r");
 
 
434
  conffp = fopen(argfile, "r");
 
472
435
  if(conffp != NULL){
 
473
436
    char *org_line = NULL;
 
474
 
    char *p, *arg, *new_arg, *line;
 
 
439
    char *p, *arg, *new_arg, *line;
 
477
440
    const char whitespace_delims[] = " \r\t\f\v\n";
 
478
441
    const char comment_delim[] = "#";
 
481
 
    custom_argv = malloc(sizeof(char*) * 2);
 
482
 
    if(custom_argv == NULL){
 
484
 
      exitstatus = EXIT_FAILURE;
 
487
 
    custom_argv[0] = argv[0];
 
488
 
    custom_argv[1] = NULL;
 
491
444
      sret = getline(&org_line, &size, conffp);
 
 
502
455
        new_arg = strdup(p);
 
505
 
          exitstatus = EXIT_FAILURE;
 
511
 
        custom_argv = realloc(custom_argv, sizeof(char *)
 
512
 
                              * ((unsigned int) custom_argc + 1));
 
513
 
        if(custom_argv == NULL){
 
515
 
          exitstatus = EXIT_FAILURE;
 
519
 
        custom_argv[custom_argc-1] = new_arg;
 
520
 
        custom_argv[custom_argc] = NULL;        
 
 
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;
 
 
527
468
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
529
470
      exitstatus = EXIT_FAILURE;
 
534
475
  if(custom_argv != NULL){
 
 
476
    custom_argv[0] = argv[0];
 
535
477
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
536
478
    if (ret == ARGP_ERR_UNKNOWN){
 
537
479
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
538
480
      exitstatus = EXIT_FAILURE;
 
 
564
506
    perror("setgid");
 
567
 
  if (plugindir == NULL){
 
570
 
    dir = opendir(plugindir);
 
 
509
  dir = opendir(plugindir);
 
574
511
    perror("Could not open plugin dir");
 
575
512
    exitstatus = EXIT_FAILURE;
 
579
516
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
 
724
661
    ret = pipe(pipefd);
 
727
664
      exitstatus = EXIT_FAILURE;
 
730
667
    ret = set_cloexec_flag(pipefd[0]);
 
732
669
      perror("set_cloexec_flag");
 
733
670
      exitstatus = EXIT_FAILURE;
 
736
673
    ret = set_cloexec_flag(pipefd[1]);
 
738
675
      perror("set_cloexec_flag");
 
739
676
      exitstatus = EXIT_FAILURE;
 
742
679
    /* Block SIGCHLD until process is safely in process list */
 
743
680
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
745
682
      perror("sigprocmask");
 
746
683
      exitstatus = EXIT_FAILURE;
 
749
686
    // Starting a new process to be watched
 
750
687
    pid_t pid = fork();
 
753
690
      exitstatus = EXIT_FAILURE;
 
757
694
      /* this is the child process */
 
 
829
 
  free_plugin_list(plugin_list);
 
 
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++){
 
 
871
817
          /* Remove the plugin */
 
872
818
          FD_CLR(proc->fd, &rfds_all);
 
873
819
          /* Block signal while modifying process_list */
 
874
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
 
820
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
876
822
            perror("sigprocmask");
 
877
823
            exitstatus = EXIT_FAILURE;
 
880
826
          /* Delete this process entry from the list */
 
881
827
          if(process_list == proc){
 
 
906
852
        /* This process exited nicely, so print its buffer */
 
908
 
        bool bret = print_out_password(proc->buffer,
 
909
 
                                       proc->buffer_length);
 
 
854
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
911
856
          perror("print_out_password");
 
912
857
          exitstatus = EXIT_FAILURE;
 
916
861
      /* This process has not completed.  Does it have any output? */
 
917
862
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
 
951
896
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
952
 
    /* Fallback if all plugins failed, none are found or an error
 
 
897
    /* Fallback if all plugins failed, none are found or an error occured */
 
955
899
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
956
900
    char *passwordbuffer = getpass("Password: ");
 
 
959
903
      perror("print_out_password");
 
960
904
      exitstatus = EXIT_FAILURE;
 
964
909
  /* Restore old signal handler */
 
965
 
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
968
 
    exitstatus = EXIT_FAILURE;
 
971
 
  if(custom_argv != NULL){
 
972
 
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
 
910
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
 
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++){
 
 
923
    free(plugin_list->environ);
 
977
 
  free_plugin_list(plugin_list);