/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-09-13 19:20:50 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080913192050-gxuo0jv4o25jn848
* TODO: Minor changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets a existing plugin based on name,
 
99
/* Gets an existing plugin based on name,
100
100
   or if none is found, creates a new one */
101
101
static plugin *getplugin(char *name){
102
102
  /* Check for exiting plugin with that name */
118
118
      return NULL;
119
119
    }
120
120
  }
121
 
 
 
121
  
122
122
  *new_plugin = (plugin) { .name = copy_name,
123
123
                           .argc = 1,
124
124
                           .disabled = false,
132
132
  }
133
133
  new_plugin->argv[0] = copy_name;
134
134
  new_plugin->argv[1] = NULL;
135
 
 
 
135
  
136
136
  new_plugin->environ = malloc(sizeof(char *));
137
137
  if(new_plugin->environ == NULL){
138
138
    free(copy_name);
141
141
    return NULL;
142
142
  }
143
143
  new_plugin->environ[0] = NULL;
144
 
 
 
144
  
145
145
  /* Append the new plugin to the list */
146
146
  plugin_list = new_plugin;
147
147
  return new_plugin;
179
179
}
180
180
 
181
181
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def){
 
182
static bool add_environment(plugin *p, const char *def, bool replace){
183
183
  if(p == NULL){
184
184
    return false;
185
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
  }
186
203
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
204
}
188
205
 
221
238
      /* No child processes */
222
239
      break;
223
240
    }
224
 
 
 
241
    
225
242
    /* A child exited, find it in process_list */
226
243
    while(proc != NULL and proc->pid != pid){
227
244
      proc = proc->next;
238
255
/* Prints out a password to stdout */
239
256
bool print_out_password(const char *buffer, size_t length){
240
257
  ssize_t ret;
241
 
  if(length>0 and buffer[length-1] == '\n'){
242
 
    length--;
243
 
  }
244
258
  for(size_t written = 0; written < length; written += (size_t)ret){
245
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
246
260
                                   length - written));
327
341
    { .name = "global-options", .key = 'g',
328
342
      .arg = "OPTION[,OPTION[,...]]",
329
343
      .doc = "Options passed to all plugins" },
330
 
    { .name = "global-envs", .key = 'e',
 
344
    { .name = "global-env", .key = 'G',
331
345
      .arg = "VAR=value",
332
346
      .doc = "Environment variable passed to all plugins" },
333
347
    { .name = "options-for", .key = 'o',
334
348
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
349
      .doc = "Options passed only to specified plugin" },
336
 
    { .name = "envs-for", .key = 'f',
 
350
    { .name = "env-for", .key = 'E',
337
351
      .arg = "PLUGIN:ENV=value",
338
352
      .doc = "Environment variable passed to specified plugin" },
339
353
    { .name = "disable", .key = 'd',
340
354
      .arg = "PLUGIN",
341
355
      .doc = "Disable a specific plugin", .group = 1 },
 
356
    { .name = "enable", .key = 'e',
 
357
      .arg = "PLUGIN",
 
358
      .doc = "Enable a specific plugin", .group = 1 },
342
359
    { .name = "plugin-dir", .key = 128,
343
360
      .arg = "DIRECTORY",
344
361
      .doc = "Specify a different plugin directory", .group = 2 },
356
373
    { .name = NULL }
357
374
  };
358
375
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
360
 
    /* Get the INPUT argument from `argp_parse', which we know is a
361
 
       pointer to our plugin list pointer. */
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
362
378
    switch (key) {
363
379
    case 'g':                   /* --global-options */
364
380
      if (arg != NULL){
374
390
        }
375
391
      }
376
392
      break;
377
 
    case 'e':                   /* --global-envs */
 
393
    case 'G':                   /* --global-env */
378
394
      if(arg == NULL){
379
395
        break;
380
396
      }
381
 
      {
382
 
        char *envdef = strdup(arg);
383
 
        if(envdef == NULL){
384
 
          break;
385
 
        }
386
 
        if(not add_environment(getplugin(NULL), envdef)){
387
 
          perror("add_environment");
388
 
        }
 
397
      if(not add_environment(getplugin(NULL), arg, true)){
 
398
        perror("add_environment");
389
399
      }
390
400
      break;
391
401
    case 'o':                   /* --options-for */
392
402
      if (arg != NULL){
393
403
        char *p_name = strsep(&arg, ":");
394
 
        if(p_name[0] == '\0'){
 
404
        if(p_name[0] == '\0' or arg == NULL){
395
405
          break;
396
406
        }
397
407
        char *opt = strsep(&arg, ":");
398
 
        if(opt[0] == '\0'){
 
408
        if(opt[0] == '\0' or opt == NULL){
399
409
          break;
400
410
        }
401
 
        if(opt != NULL){
402
 
          char *p;
403
 
          while((p = strsep(&opt, ",")) != NULL){
404
 
            if(p[0] == '\0'){
405
 
              continue;
406
 
            }
407
 
            if(not add_argument(getplugin(p_name), p)){
408
 
              perror("add_argument");
409
 
              return ARGP_ERR_UNKNOWN;
410
 
            }
 
411
        char *p;
 
412
        while((p = strsep(&opt, ",")) != NULL){
 
413
          if(p[0] == '\0'){
 
414
            continue;
 
415
          }
 
416
          if(not add_argument(getplugin(p_name), p)){
 
417
            perror("add_argument");
 
418
            return ARGP_ERR_UNKNOWN;
411
419
          }
412
420
        }
413
421
      }
414
422
      break;
415
 
    case 'f':                   /* --envs-for */
 
423
    case 'E':                   /* --env-for */
416
424
      if(arg == NULL){
417
425
        break;
418
426
      }
421
429
        if(envdef == NULL){
422
430
          break;
423
431
        }
424
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
425
 
        if(p_name == NULL){
426
 
          break;
427
 
        }
428
 
        envdef++;
429
 
        if(not add_environment(getplugin(p_name), envdef)){
 
432
        *envdef = '\0';
 
433
        if(not add_environment(getplugin(arg), envdef+1, true)){
430
434
          perror("add_environment");
431
435
        }
432
436
      }
440
444
        p->disabled = true;
441
445
      }
442
446
      break;
 
447
    case 'e':                   /* --enable */
 
448
      if (arg != NULL){
 
449
        plugin *p = getplugin(arg);
 
450
        if(p == NULL){
 
451
          return ARGP_ERR_UNKNOWN;
 
452
        }
 
453
        p->disabled = false;
 
454
      }
 
455
      break;
443
456
    case 128:                   /* --plugin-dir */
 
457
      free(plugindir);
444
458
      plugindir = strdup(arg);
445
459
      if(plugindir == NULL){
446
460
        perror("strdup");
447
461
      }      
448
462
      break;
449
463
    case 129:                   /* --config-file */
 
464
      /* This is already done by parse_opt_config_file() */
 
465
      break;
 
466
    case 130:                   /* --userid */
 
467
      uid = (uid_t)strtol(arg, NULL, 10);
 
468
      break;
 
469
    case 131:                   /* --groupid */
 
470
      gid = (gid_t)strtol(arg, NULL, 10);
 
471
      break;
 
472
    case 132:                   /* --debug */
 
473
      debug = true;
 
474
      break;
 
475
    case ARGP_KEY_ARG:
 
476
      /* Cryptsetup always passes an argument, which is an empty
 
477
         string if "none" was specified in /etc/crypttab.  So if
 
478
         argument was empty, we ignore it silently. */
 
479
      if(arg[0] != '\0'){
 
480
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
481
      }
 
482
      break;
 
483
    case ARGP_KEY_END:
 
484
      break;
 
485
    default:
 
486
      return ARGP_ERR_UNKNOWN;
 
487
    }
 
488
    return 0;
 
489
  }
 
490
  
 
491
  /* This option parser is the same as parse_opt() above, except it
 
492
     ignores everything but the --config-file option. */
 
493
  error_t parse_opt_config_file (int key, char *arg,
 
494
                                 __attribute__((unused))
 
495
                                 struct argp_state *state) {
 
496
    switch (key) {
 
497
    case 'g':                   /* --global-options */
 
498
    case 'G':                   /* --global-env */
 
499
    case 'o':                   /* --options-for */
 
500
    case 'E':                   /* --env-for */
 
501
    case 'd':                   /* --disable */
 
502
    case 'e':                   /* --enable */
 
503
    case 128:                   /* --plugin-dir */
 
504
      break;
 
505
    case 129:                   /* --config-file */
 
506
      free(argfile);
450
507
      argfile = strdup(arg);
451
508
      if(argfile == NULL){
452
509
        perror("strdup");
453
510
      }
454
511
      break;      
455
512
    case 130:                   /* --userid */
456
 
      uid = (uid_t)strtol(arg, NULL, 10);
457
 
      break;
458
513
    case 131:                   /* --groupid */
459
 
      gid = (gid_t)strtol(arg, NULL, 10);
460
 
      break;
461
514
    case 132:                   /* --debug */
462
 
      debug = true;
463
 
      break;
464
515
    case ARGP_KEY_ARG:
465
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
466
 
      break;
467
516
    case ARGP_KEY_END:
468
517
      break;
469
518
    default:
472
521
    return 0;
473
522
  }
474
523
  
475
 
  struct argp argp = { .options = options, .parser = parse_opt,
476
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
524
  struct argp argp = { .options = options,
 
525
                       .parser = parse_opt_config_file,
 
526
                       .args_doc = "",
477
527
                       .doc = "Mandos plugin runner -- Run plugins" };
478
528
  
479
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
529
  /* Parse using the parse_opt_config_file in order to get the custom
 
530
     config file location, if any. */
 
531
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
480
532
  if (ret == ARGP_ERR_UNKNOWN){
481
533
    fprintf(stderr, "Unknown error while parsing arguments\n");
482
534
    exitstatus = EXIT_FAILURE;
483
535
    goto fallback;
484
536
  }
485
 
 
486
 
  /* Opens the configfile if aviable */
 
537
  
 
538
  /* Reset to the normal argument parser */
 
539
  argp.parser = parse_opt;
 
540
  
 
541
  /* Open the configfile if available */
487
542
  if (argfile == NULL){
488
543
    conffp = fopen(AFILE, "r");
489
544
  } else {
507
562
    custom_argv[0] = argv[0];
508
563
    custom_argv[1] = NULL;
509
564
 
510
 
    /* for each line in the config file, strip whitespace and ignore commented text */
 
565
    /* for each line in the config file, strip whitespace and ignore
 
566
       commented text */
511
567
    while(true){
512
568
      sret = getline(&org_line, &size, conffp);
513
569
      if(sret == -1){
542
598
      }
543
599
    }
544
600
    free(org_line);
545
 
  } else{
 
601
  } else {
546
602
    /* Check for harmful errors and go to fallback. Other errors might
547
603
       not affect opening plugins */
548
604
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
554
610
  /* If there was any arguments from configuration file,
555
611
     pass them to parser as command arguments */
556
612
  if(custom_argv != NULL){
557
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
613
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
614
                      0, NULL);
558
615
    if (ret == ARGP_ERR_UNKNOWN){
559
616
      fprintf(stderr, "Unknown error while parsing arguments\n");
560
617
      exitstatus = EXIT_FAILURE;
562
619
    }
563
620
  }
564
621
  
 
622
  /* Parse actual command line arguments, to let them override the
 
623
     config file */
 
624
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
625
  if (ret == ARGP_ERR_UNKNOWN){
 
626
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
627
    exitstatus = EXIT_FAILURE;
 
628
    goto fallback;
 
629
  }
 
630
  
565
631
  if(debug){
566
632
    for(plugin *p = plugin_list; p != NULL; p=p->next){
567
633
      fprintf(stderr, "Plugin: %s has %d arguments\n",
575
641
      }
576
642
    }
577
643
  }
578
 
 
 
644
  
579
645
  /* Strip permissions down to nobody */
580
646
  ret = setuid(uid);
581
647
  if (ret == -1){
585
651
  if (ret == -1){
586
652
    perror("setgid");
587
653
  }
588
 
 
 
654
  
589
655
  if (plugindir == NULL){
590
656
    dir = opendir(PDIR);
591
657
  } else {
612
678
  }
613
679
  
614
680
  FD_ZERO(&rfds_all);
615
 
 
 
681
  
616
682
  /* Read and execute any executable in the plugin directory*/
617
683
  while(true){
618
684
    dirst = readdir(dir);
619
685
    
620
 
    // All directory entries have been processed
 
686
    /* All directory entries have been processed */
621
687
    if(dirst == NULL){
622
688
      if (errno == EBADF){
623
689
        perror("readdir");
629
695
    
630
696
    d_name_len = strlen(dirst->d_name);
631
697
    
632
 
    // Ignore dotfiles, backup files and other junk
 
698
    /* Ignore dotfiles, backup files and other junk */
633
699
    {
634
700
      bool bad_name = false;
635
701
      
673
739
    }
674
740
 
675
741
    char *filename;
676
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
742
    if(plugindir == NULL){
 
743
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
744
    } else {
 
745
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
746
    }
677
747
    if(ret < 0){
678
748
      perror("asprintf");
679
749
      continue;
721
791
        }
722
792
        /* Add global environment variables */
723
793
        for(char **e = g->environ; *e != NULL; e++){
724
 
          if(not add_environment(p, *e)){
 
794
          if(not add_environment(p, *e, false)){
725
795
            perror("add_environment");
726
796
          }
727
797
        }
732
802
       process, too. */
733
803
    if(p->environ[0] != NULL){
734
804
      for(char **e = environ; *e != NULL; e++){
735
 
        char *copy = strdup(*e);
736
 
        if(copy == NULL){
737
 
          perror("strdup");
738
 
          continue;
739
 
        }
740
 
        if(not add_environment(p, copy)){
 
805
        if(not add_environment(p, *e, false)){
741
806
          perror("add_environment");
742
807
        }
743
808
      }
770
835
      exitstatus = EXIT_FAILURE;
771
836
      goto fallback;
772
837
    }
773
 
    // Starting a new process to be watched
 
838
    /* Starting a new process to be watched */
774
839
    pid_t pid = fork();
775
840
    if(pid == -1){
776
841
      perror("fork");
873
938
    }
874
939
    /* OK, now either a process completed, or something can be read
875
940
       from one of them */
876
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
941
    for(plugin *proc = plugin_list; proc != NULL;){
877
942
      /* Is this process completely done? */
878
943
      if(proc->eof and proc->completed){
879
944
        /* Only accept the plugin output if it exited cleanly */
906
971
            exitstatus = EXIT_FAILURE;
907
972
            goto fallback;
908
973
          }
909
 
          free_plugin(proc);
 
974
          
910
975
          /* We are done modifying process list, so unblock signal */
911
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
912
977
                             NULL);
919
984
          if(plugin_list == NULL){
920
985
            break;
921
986
          }
 
987
          
 
988
          plugin *next_plugin = proc->next;
 
989
          free_plugin(proc);
 
990
          proc = next_plugin;
922
991
          continue;
923
992
        }
924
993
        
936
1005
      /* This process has not completed.  Does it have any output? */
937
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
938
1007
        /* This process had nothing to say at this time */
 
1008
        proc = proc->next;
939
1009
        continue;
940
1010
      }
941
1011
      /* Before reading, make the process' data buffer large enough */
954
1024
                 BUFFER_SIZE);
955
1025
      if(ret < 0){
956
1026
        /* Read error from this process; ignore the error */
 
1027
        proc = proc->next;
957
1028
        continue;
958
1029
      }
959
1030
      if(ret == 0){
974
1045
    bool bret;
975
1046
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
976
1047
    char *passwordbuffer = getpass("Password: ");
977
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1048
    size_t len = strlen(passwordbuffer);
 
1049
    /* Strip trailing newline */
 
1050
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1051
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1052
      len--;
 
1053
    }
 
1054
    bret = print_out_password(passwordbuffer, len);
978
1055
    if(not bret){
979
1056
      perror("print_out_password");
980
1057
      exitstatus = EXIT_FAILURE;