/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-01 16:53:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901165317-77dccp81zyqimt2j
* plugin-runner.c (add_environment): Override existing environment
                                     variables when asked to do so.
                                     All callers changed.
  (main): Removed old argp.args_doc string.  Parse argument file
          before normal command line arguments.  Use ARGP_IN_ORDER
          flag in both calls to argp_parse.  Do not strdup() strings
          to be sent to add_environment().

* plugin-runner.xml (OPTIONS): Document "--global-env" and "--envs-for".
  (FILES): Note that the config file is parsed before the command line
           options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
28
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
28
                                   EXIT_SUCCESS, realloc() */
30
29
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
33
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
33
                                   stat, waitpid(), WIFEXITED(),
35
34
                                   WEXITSTATUS(), wait(), pid_t,
47
46
                                   fcntl(), setuid(), setgid(),
48
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
49
48
                                   access(), pipe(), fork(), close()
50
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
51
50
                                   execv(), write(), read(),
52
51
                                   close() */
53
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
70
69
#define PDIR "/lib/mandos/plugins.d"
71
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
72
71
 
73
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
74
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
75
74
 
 
75
struct plugin;
 
76
 
76
77
typedef struct plugin{
77
78
  char *name;                   /* can be NULL or any plugin name */
78
79
  char **argv;
95
96
 
96
97
static plugin *plugin_list = NULL;
97
98
 
98
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
99
100
   or if none is found, creates a new one */
100
101
static plugin *getplugin(char *name){
101
102
  /* Check for exiting plugin with that name */
117
118
      return NULL;
118
119
    }
119
120
  }
120
 
  
 
121
 
121
122
  *new_plugin = (plugin) { .name = copy_name,
122
123
                           .argc = 1,
123
124
                           .disabled = false,
186
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
187
188
  /* Search for this environment variable */
188
189
  for(char **e = p->environ; *e != NULL; e++){
189
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
190
191
      /* It already exists */
191
192
      if(replace){
192
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
193
194
        if(new == NULL){
194
195
          return false;
195
196
        }
207
208
 * Descriptor Flags".
208
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
209
210
 */
210
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
211
213
  int ret = fcntl(fd, F_GETFD, 0);
212
214
  /* If reading the flags failed, return error indication now. */
213
215
  if(ret < 0){
220
222
 
221
223
/* Mark processes as completed when they exit, and save their exit
222
224
   status. */
223
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
224
226
  while(true){
225
227
    plugin *proc = plugin_list;
226
228
    int status;
236
238
      /* No child processes */
237
239
      break;
238
240
    }
239
 
    
 
241
 
240
242
    /* A child exited, find it in process_list */
241
243
    while(proc != NULL and proc->pid != pid){
242
244
      proc = proc->next;
251
253
}
252
254
 
253
255
/* Prints out a password to stdout */
254
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
255
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
256
261
  for(size_t written = 0; written < length; written += (size_t)ret){
257
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
258
263
                                   length - written));
339
344
    { .name = "global-options", .key = 'g',
340
345
      .arg = "OPTION[,OPTION[,...]]",
341
346
      .doc = "Options passed to all plugins" },
342
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
343
348
      .arg = "VAR=value",
344
349
      .doc = "Environment variable passed to all plugins" },
345
350
    { .name = "options-for", .key = 'o',
346
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
347
352
      .doc = "Options passed only to specified plugin" },
348
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
349
354
      .arg = "PLUGIN:ENV=value",
350
355
      .doc = "Environment variable passed to specified plugin" },
351
356
    { .name = "disable", .key = 'd',
352
357
      .arg = "PLUGIN",
353
358
      .doc = "Disable a specific plugin", .group = 1 },
354
 
    { .name = "enable", .key = 'e',
355
 
      .arg = "PLUGIN",
356
 
      .doc = "Enable a specific plugin", .group = 1 },
357
359
    { .name = "plugin-dir", .key = 128,
358
360
      .arg = "DIRECTORY",
359
361
      .doc = "Specify a different plugin directory", .group = 2 },
373
375
  
374
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
375
377
                     struct argp_state *state) {
 
378
    /* Get the INPUT argument from `argp_parse', which we know is a
 
379
       pointer to our plugin list pointer. */
376
380
    switch (key) {
377
381
    case 'g':                   /* --global-options */
378
382
      if (arg != NULL){
388
392
        }
389
393
      }
390
394
      break;
391
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
392
396
      if(arg == NULL){
393
397
        break;
394
398
      }
395
 
      if(not add_environment(getplugin(NULL), arg, true)){
396
 
        perror("add_environment");
 
399
      {
 
400
        char *envdef = strdup(arg);
 
401
        if(envdef == NULL){
 
402
          break;
 
403
        }
 
404
        if(not add_environment(getplugin(NULL), envdef, true)){
 
405
          perror("add_environment");
 
406
        }
397
407
      }
398
408
      break;
399
409
    case 'o':                   /* --options-for */
400
410
      if (arg != NULL){
401
411
        char *p_name = strsep(&arg, ":");
402
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
403
413
          break;
404
414
        }
405
415
        char *opt = strsep(&arg, ":");
406
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
407
417
          break;
408
418
        }
409
 
        char *p;
410
 
        while((p = strsep(&opt, ",")) != NULL){
411
 
          if(p[0] == '\0'){
412
 
            continue;
413
 
          }
414
 
          if(not add_argument(getplugin(p_name), p)){
415
 
            perror("add_argument");
416
 
            return ARGP_ERR_UNKNOWN;
 
419
        if(opt != NULL){
 
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;
 
428
            }
417
429
          }
418
430
        }
419
431
      }
420
432
      break;
421
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
422
434
      if(arg == NULL){
423
435
        break;
424
436
      }
427
439
        if(envdef == NULL){
428
440
          break;
429
441
        }
430
 
        *envdef = '\0';
431
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
442
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
443
        if(p_name == NULL){
 
444
          break;
 
445
        }
 
446
        envdef++;
 
447
        if(not add_environment(getplugin(p_name), envdef, true)){
432
448
          perror("add_environment");
433
449
        }
434
450
      }
442
458
        p->disabled = true;
443
459
      }
444
460
      break;
445
 
    case 'e':                   /* --enable */
446
 
      if (arg != NULL){
447
 
        plugin *p = getplugin(arg);
448
 
        if(p == NULL){
449
 
          return ARGP_ERR_UNKNOWN;
450
 
        }
451
 
        p->disabled = false;
452
 
      }
453
 
      break;
454
461
    case 128:                   /* --plugin-dir */
455
 
      free(plugindir);
456
462
      plugindir = strdup(arg);
457
463
      if(plugindir == NULL){
458
464
        perror("strdup");
459
465
      }      
460
466
      break;
461
467
    case 129:                   /* --config-file */
462
 
      /* This is already done by parse_opt_config_file() */
463
 
      break;
 
468
      argfile = strdup(arg);
 
469
      if(argfile == NULL){
 
470
        perror("strdup");
 
471
      }
 
472
      break;      
464
473
    case 130:                   /* --userid */
465
474
      uid = (uid_t)strtol(arg, NULL, 10);
466
475
      break;
471
480
      debug = true;
472
481
      break;
473
482
    case ARGP_KEY_ARG:
474
 
      /* Cryptsetup always passes an argument, which is an empty
475
 
         string if "none" was specified in /etc/crypttab.  So if
476
 
         argument was empty, we ignore it silently. */
477
 
      if(arg[0] != '\0'){
478
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
 
      }
480
 
      break;
481
 
    case ARGP_KEY_END:
482
 
      break;
483
 
    default:
484
 
      return ARGP_ERR_UNKNOWN;
485
 
    }
486
 
    return 0;
487
 
  }
488
 
  
489
 
  /* This option parser is the same as parse_opt() above, except it
490
 
     ignores everything but the --config-file option. */
491
 
  error_t parse_opt_config_file (int key, char *arg,
492
 
                                 __attribute__((unused))
493
 
                                 struct argp_state *state) {
494
 
    switch (key) {
495
 
    case 'g':                   /* --global-options */
496
 
    case 'G':                   /* --global-env */
497
 
    case 'o':                   /* --options-for */
498
 
    case 'E':                   /* --env-for */
499
 
    case 'd':                   /* --disable */
500
 
    case 'e':                   /* --enable */
501
 
    case 128:                   /* --plugin-dir */
502
 
      break;
503
 
    case 129:                   /* --config-file */
504
 
      free(argfile);
505
 
      argfile = strdup(arg);
506
 
      if(argfile == NULL){
507
 
        perror("strdup");
508
 
      }
509
 
      break;      
510
 
    case 130:                   /* --userid */
511
 
    case 131:                   /* --groupid */
512
 
    case 132:                   /* --debug */
513
 
    case ARGP_KEY_ARG:
514
 
    case ARGP_KEY_END:
515
 
      break;
516
 
    default:
517
 
      return ARGP_ERR_UNKNOWN;
518
 
    }
519
 
    return 0;
520
 
  }
521
 
  
522
 
  struct argp argp = { .options = options,
523
 
                       .parser = parse_opt_config_file,
 
483
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
484
      break;
 
485
    case ARGP_KEY_END:
 
486
      break;
 
487
    default:
 
488
      return ARGP_ERR_UNKNOWN;
 
489
    }
 
490
    return 0;
 
491
  }
 
492
  
 
493
  struct argp argp = { .options = options, .parser = parse_opt,
524
494
                       .args_doc = "",
525
495
                       .doc = "Mandos plugin runner -- Run plugins" };
526
496
  
527
 
  /* Parse using the parse_opt_config_file in order to get the custom
528
 
     config file location, if any. */
529
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
530
 
  if (ret == ARGP_ERR_UNKNOWN){
531
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
532
 
    exitstatus = EXIT_FAILURE;
533
 
    goto fallback;
534
 
  }
535
 
  
536
 
  /* Reset to the normal argument parser */
537
 
  argp.parser = parse_opt;
538
 
  
539
497
  /* Open the configfile if available */
540
498
  if (argfile == NULL){
541
499
    conffp = fopen(AFILE, "r");
701
659
      
702
660
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
703
661
                                           ".dpkg-old",
704
 
                                           ".dpkg-bak",
705
662
                                           ".dpkg-divert", NULL };
706
663
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
707
664
        size_t pre_len = strlen(*pre);
738
695
    }
739
696
 
740
697
    char *filename;
741
 
    if(plugindir == NULL){
742
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
743
 
    } else {
744
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
745
 
    }
 
698
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
746
699
    if(ret < 0){
747
700
      perror("asprintf");
748
701
      continue;
848
801
        perror("sigaction");
849
802
        _exit(EXIT_FAILURE);
850
803
      }
851
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
804
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
805
      if(ret < 0){
853
806
        perror("sigprocmask");
854
807
        _exit(EXIT_FAILURE);
855
808
      }
856
 
      
 
809
 
857
810
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
811
      if(ret == -1){
859
812
        perror("dup2");
909
862
    if (maxfd < new_plugin->fd){
910
863
      maxfd = new_plugin->fd;
911
864
    }
 
865
    
912
866
  }
913
867
  
914
868
  closedir(dir);
915
869
  dir = NULL;
916
 
  
 
870
 
917
871
  for(plugin *p = plugin_list; p != NULL; p = p->next){
918
872
    if(p->pid != 0){
919
873
      break;
924
878
      free_plugin_list();
925
879
    }
926
880
  }
927
 
  
 
881
 
928
882
  /* Main loop while running plugins exist */
929
883
  while(plugin_list){
930
884
    fd_set rfds = rfds_all;
936
890
    }
937
891
    /* OK, now either a process completed, or something can be read
938
892
       from one of them */
939
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
893
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
940
894
      /* Is this process completely done? */
941
895
      if(proc->eof and proc->completed){
942
896
        /* Only accept the plugin output if it exited cleanly */
969
923
            exitstatus = EXIT_FAILURE;
970
924
            goto fallback;
971
925
          }
972
 
          
973
 
          plugin *next_plugin = proc->next;
974
926
          free_plugin(proc);
975
 
          proc = next_plugin;
976
 
          
977
927
          /* We are done modifying process list, so unblock signal */
978
928
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
929
                             NULL);
986
936
          if(plugin_list == NULL){
987
937
            break;
988
938
          }
989
 
          
990
939
          continue;
991
940
        }
992
941
        
993
942
        /* This process exited nicely, so print its buffer */
994
 
        
 
943
 
995
944
        bool bret = print_out_password(proc->buffer,
996
945
                                       proc->buffer_length);
997
946
        if(not bret){
1004
953
      /* This process has not completed.  Does it have any output? */
1005
954
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1006
955
        /* This process had nothing to say at this time */
1007
 
        proc = proc->next;
1008
956
        continue;
1009
957
      }
1010
958
      /* Before reading, make the process' data buffer large enough */
1023
971
                 BUFFER_SIZE);
1024
972
      if(ret < 0){
1025
973
        /* Read error from this process; ignore the error */
1026
 
        proc = proc->next;
1027
974
        continue;
1028
975
      }
1029
976
      if(ret == 0){
1044
991
    bool bret;
1045
992
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1046
993
    char *passwordbuffer = getpass("Password: ");
1047
 
    size_t len = strlen(passwordbuffer);
1048
 
    /* Strip trailing newline */
1049
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1050
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1051
 
      len--;
1052
 
    }
1053
 
    bret = print_out_password(passwordbuffer, len);
 
994
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1054
995
    if(not bret){
1055
996
      perror("print_out_password");
1056
997
      exitstatus = EXIT_FAILURE;
1063
1004
    perror("sigaction");
1064
1005
    exitstatus = EXIT_FAILURE;
1065
1006
  }
1066
 
  
 
1007
 
1067
1008
  if(custom_argv != NULL){
1068
1009
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1069
1010
      free(*arg);
1075
1016
    closedir(dir);
1076
1017
  }
1077
1018
  
1078
 
  /* Kill the processes */
 
1019
  /* Free the process list and kill the processes */
1079
1020
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1080
1021
    if(p->pid != 0){
1081
1022
      close(p->fd);
1094
1035
  if(errno != ECHILD){
1095
1036
    perror("wait");
1096
1037
  }
1097
 
  
 
1038
 
1098
1039
  free_plugin_list();
1099
1040
  
1100
1041
  free(plugindir);