/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

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));
309
314
  struct stat st;
310
315
  fd_set rfds_all;
311
316
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
317
  uid_t uid = 65534;
314
318
  gid_t gid = 65534;
315
319
  bool debug = false;
340
344
    { .name = "global-options", .key = 'g',
341
345
      .arg = "OPTION[,OPTION[,...]]",
342
346
      .doc = "Options passed to all plugins" },
343
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
344
348
      .arg = "VAR=value",
345
349
      .doc = "Environment variable passed to all plugins" },
346
350
    { .name = "options-for", .key = 'o',
347
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
348
352
      .doc = "Options passed only to specified plugin" },
349
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
350
354
      .arg = "PLUGIN:ENV=value",
351
355
      .doc = "Environment variable passed to specified plugin" },
352
356
    { .name = "disable", .key = 'd',
353
357
      .arg = "PLUGIN",
354
358
      .doc = "Disable a specific plugin", .group = 1 },
355
 
    { .name = "enable", .key = 'e',
356
 
      .arg = "PLUGIN",
357
 
      .doc = "Enable a specific plugin", .group = 1 },
358
359
    { .name = "plugin-dir", .key = 128,
359
360
      .arg = "DIRECTORY",
360
361
      .doc = "Specify a different plugin directory", .group = 2 },
374
375
  
375
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
376
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. */
377
380
    switch (key) {
378
381
    case 'g':                   /* --global-options */
379
382
      if (arg != NULL){
389
392
        }
390
393
      }
391
394
      break;
392
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
393
396
      if(arg == NULL){
394
397
        break;
395
398
      }
396
 
      if(not add_environment(getplugin(NULL), arg, true)){
397
 
        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
        }
398
407
      }
399
408
      break;
400
409
    case 'o':                   /* --options-for */
401
410
      if (arg != NULL){
402
411
        char *p_name = strsep(&arg, ":");
403
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
404
413
          break;
405
414
        }
406
415
        char *opt = strsep(&arg, ":");
407
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
408
417
          break;
409
418
        }
410
 
        char *p;
411
 
        while((p = strsep(&opt, ",")) != NULL){
412
 
          if(p[0] == '\0'){
413
 
            continue;
414
 
          }
415
 
          if(not add_argument(getplugin(p_name), p)){
416
 
            perror("add_argument");
417
 
            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
            }
418
429
          }
419
430
        }
420
431
      }
421
432
      break;
422
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
423
434
      if(arg == NULL){
424
435
        break;
425
436
      }
428
439
        if(envdef == NULL){
429
440
          break;
430
441
        }
431
 
        *envdef = '\0';
432
 
        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)){
433
448
          perror("add_environment");
434
449
        }
435
450
      }
443
458
        p->disabled = true;
444
459
      }
445
460
      break;
446
 
    case 'e':                   /* --enable */
447
 
      if (arg != NULL){
448
 
        plugin *p = getplugin(arg);
449
 
        if(p == NULL){
450
 
          return ARGP_ERR_UNKNOWN;
451
 
        }
452
 
        p->disabled = false;
453
 
      }
454
 
      break;
455
461
    case 128:                   /* --plugin-dir */
456
 
      free(plugindir);
457
462
      plugindir = strdup(arg);
458
463
      if(plugindir == NULL){
459
464
        perror("strdup");
460
465
      }      
461
466
      break;
462
467
    case 129:                   /* --config-file */
463
 
      /* This is already done by parse_opt_config_file() */
464
 
      break;
 
468
      argfile = strdup(arg);
 
469
      if(argfile == NULL){
 
470
        perror("strdup");
 
471
      }
 
472
      break;      
465
473
    case 130:                   /* --userid */
466
474
      uid = (uid_t)strtol(arg, NULL, 10);
467
475
      break;
472
480
      debug = true;
473
481
      break;
474
482
    case ARGP_KEY_ARG:
475
 
      /* Cryptsetup always passes an argument, which is an empty
476
 
         string if "none" was specified in /etc/crypttab.  So if
477
 
         argument was empty, we ignore it silently. */
478
 
      if(arg[0] != '\0'){
479
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
480
 
      }
481
 
      break;
482
 
    case ARGP_KEY_END:
483
 
      break;
484
 
    default:
485
 
      return ARGP_ERR_UNKNOWN;
486
 
    }
487
 
    return 0;
488
 
  }
489
 
  
490
 
  /* This option parser is the same as parse_opt() above, except it
491
 
     ignores everything but the --config-file option. */
492
 
  error_t parse_opt_config_file (int key, char *arg,
493
 
                                 __attribute__((unused))
494
 
                                 struct argp_state *state) {
495
 
    switch (key) {
496
 
    case 'g':                   /* --global-options */
497
 
    case 'G':                   /* --global-env */
498
 
    case 'o':                   /* --options-for */
499
 
    case 'E':                   /* --env-for */
500
 
    case 'd':                   /* --disable */
501
 
    case 'e':                   /* --enable */
502
 
    case 128:                   /* --plugin-dir */
503
 
      break;
504
 
    case 129:                   /* --config-file */
505
 
      free(argfile);
506
 
      argfile = strdup(arg);
507
 
      if(argfile == NULL){
508
 
        perror("strdup");
509
 
      }
510
 
      break;      
511
 
    case 130:                   /* --userid */
512
 
    case 131:                   /* --groupid */
513
 
    case 132:                   /* --debug */
514
 
    case ARGP_KEY_ARG:
515
 
    case ARGP_KEY_END:
516
 
      break;
517
 
    default:
518
 
      return ARGP_ERR_UNKNOWN;
519
 
    }
520
 
    return 0;
521
 
  }
522
 
  
523
 
  struct argp argp = { .options = options,
524
 
                       .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,
525
494
                       .args_doc = "",
526
495
                       .doc = "Mandos plugin runner -- Run plugins" };
527
496
  
528
 
  /* Parse using the parse_opt_config_file in order to get the custom
529
 
     config file location, if any. */
530
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
531
 
  if (ret == ARGP_ERR_UNKNOWN){
532
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
533
 
    exitstatus = EXIT_FAILURE;
534
 
    goto fallback;
535
 
  }
536
 
  
537
 
  /* Reset to the normal argument parser */
538
 
  argp.parser = parse_opt;
539
 
  
540
497
  /* Open the configfile if available */
541
498
  if (argfile == NULL){
542
499
    conffp = fopen(AFILE, "r");
547
504
    char *org_line = NULL;
548
505
    char *p, *arg, *new_arg, *line;
549
506
    size_t size = 0;
 
507
    ssize_t sret;
550
508
    const char whitespace_delims[] = " \r\t\f\v\n";
551
509
    const char comment_delim[] = "#";
552
510
 
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 */
1019
967
        proc->buffer_size += BUFFER_SIZE;
1020
968
      }
1021
969
      /* Read from the process */
1022
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1023
 
                  BUFFER_SIZE);
1024
 
      if(sret < 0){
 
970
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
971
                 BUFFER_SIZE);
 
972
      if(ret < 0){
1025
973
        /* Read error from this process; ignore the error */
1026
 
        proc = proc->next;
1027
974
        continue;
1028
975
      }
1029
 
      if(sret == 0){
 
976
      if(ret == 0){
1030
977
        /* got EOF */
1031
978
        proc->eof = true;
1032
979
      } else {
1033
 
        proc->buffer_length += (size_t) sret;
 
980
        proc->buffer_length += (size_t) ret;
1034
981
      }
1035
982
    }
1036
983
  }
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);