/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: 2009-04-17 08:26:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090417082617-eltetak5lzjyz55o
* initramfs-tools-hook: Bug fix: Add "--userid" and "--groupid" to
                        start of "plugin-runner.conf" file instead of
                        appending, to allow any preexisting options to
                        override.
* plugin-runner.conf: Improved wording.

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 Teddy Hogeborn & Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
6
7
 * 
7
8
 * This program is free software: you can redistribute it and/or
8
9
 * modify it under the terms of the GNU General Public License as
61
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
63
                                   sigaddset(), sigaction(),
63
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
 
                                   SIG_UNBLOCK, kill() */
 
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
66
                                */
65
67
#include <errno.h>              /* errno, EBADF */
 
68
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
66
69
 
67
70
#define BUFFER_SIZE 256
68
71
 
79
82
  char **environ;
80
83
  int envc;
81
84
  bool disabled;
82
 
 
 
85
  
83
86
  /* Variables used for running processes*/
84
87
  pid_t pid;
85
88
  int fd;
87
90
  size_t buffer_size;
88
91
  size_t buffer_length;
89
92
  bool eof;
90
 
  volatile bool completed;
91
 
  volatile int status;
 
93
  volatile sig_atomic_t completed;
 
94
  int status;
92
95
  struct plugin *next;
93
96
} plugin;
94
97
 
98
101
   or if none is found, creates a new one */
99
102
static plugin *getplugin(char *name){
100
103
  /* Check for exiting plugin with that name */
101
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
102
 
    if ((p->name == name)
103
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
 
104
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
105
    if((p->name == name)
 
106
       or (p->name and name and (strcmp(p->name, name) == 0))){
104
107
      return p;
105
108
    }
106
109
  }
107
110
  /* Create a new plugin */
108
111
  plugin *new_plugin = malloc(sizeof(plugin));
109
 
  if (new_plugin == NULL){
 
112
  if(new_plugin == NULL){
110
113
    return NULL;
111
114
  }
112
115
  char *copy_name = NULL;
113
116
  if(name != NULL){
114
117
    copy_name = strdup(name);
115
118
    if(copy_name == NULL){
 
119
      free(new_plugin);
116
120
      return NULL;
117
121
    }
118
122
  }
119
123
  
120
 
  *new_plugin = (plugin) { .name = copy_name,
121
 
                           .argc = 1,
122
 
                           .disabled = false,
123
 
                           .next = plugin_list };
 
124
  *new_plugin = (plugin){ .name = copy_name,
 
125
                          .argc = 1,
 
126
                          .disabled = false,
 
127
                          .next = plugin_list };
124
128
  
125
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
126
 
  if (new_plugin->argv == NULL){
 
130
  if(new_plugin->argv == NULL){
127
131
    free(copy_name);
128
132
    free(new_plugin);
129
133
    return NULL;
220
224
/* Mark processes as completed when they exit, and save their exit
221
225
   status. */
222
226
static void handle_sigchld(__attribute__((unused)) int sig){
 
227
  int old_errno = errno;
223
228
  while(true){
224
229
    plugin *proc = plugin_list;
225
230
    int status;
229
234
      break;
230
235
    }
231
236
    if(pid == -1){
232
 
      if (errno != ECHILD){
233
 
        perror("waitpid");
 
237
      if(errno == ECHILD){
 
238
        /* No child processes */
 
239
        break;
234
240
      }
235
 
      /* No child processes */
236
 
      break;
 
241
      perror("waitpid");
237
242
    }
238
243
    
239
244
    /* A child exited, find it in process_list */
245
250
      continue;
246
251
    }
247
252
    proc->status = status;
248
 
    proc->completed = true;
 
253
    proc->completed = 1;
249
254
  }
 
255
  errno = old_errno;
250
256
}
251
257
 
252
258
/* Prints out a password to stdout */
274
280
  }
275
281
  free(plugin_node->environ);
276
282
  free(plugin_node->buffer);
277
 
 
 
283
  
278
284
  /* Removes the plugin from the singly-linked list */
279
285
  if(plugin_node == plugin_list){
280
286
    /* First one - simple */
308
314
  struct stat st;
309
315
  fd_set rfds_all;
310
316
  int ret, maxfd = 0;
 
317
  ssize_t sret;
 
318
  intmax_t tmpmax;
311
319
  uid_t uid = 65534;
312
320
  gid_t gid = 65534;
313
321
  bool debug = false;
370
378
    { .name = NULL }
371
379
  };
372
380
  
373
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
374
 
                     struct argp_state *state) {
375
 
    switch (key) {
 
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
382
                    struct argp_state *state){
 
383
    char *tmp;
 
384
    switch(key){
376
385
    case 'g':                   /* --global-options */
377
 
      if (arg != NULL){
378
 
        char *p;
379
 
        while((p = strsep(&arg, ",")) != NULL){
380
 
          if(p[0] == '\0'){
 
386
      if(arg != NULL){
 
387
        char *plugin_option;
 
388
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
389
          if(plugin_option[0] == '\0'){
381
390
            continue;
382
391
          }
383
 
          if(not add_argument(getplugin(NULL), p)){
 
392
          if(not add_argument(getplugin(NULL), plugin_option)){
384
393
            perror("add_argument");
385
394
            return ARGP_ERR_UNKNOWN;
386
395
          }
396
405
      }
397
406
      break;
398
407
    case 'o':                   /* --options-for */
399
 
      if (arg != NULL){
400
 
        char *p_name = strsep(&arg, ":");
401
 
        if(p_name[0] == '\0' or arg == NULL){
402
 
          break;
403
 
        }
404
 
        char *opt = strsep(&arg, ":");
405
 
        if(opt[0] == '\0' or opt == NULL){
406
 
          break;
407
 
        }
408
 
        char *p;
409
 
        while((p = strsep(&opt, ",")) != NULL){
410
 
          if(p[0] == '\0'){
411
 
            continue;
412
 
          }
413
 
          if(not add_argument(getplugin(p_name), p)){
 
408
      if(arg != NULL){
 
409
        char *plugin_name = strsep(&arg, ":");
 
410
        if(plugin_name[0] == '\0'){
 
411
          break;
 
412
        }
 
413
        char *plugin_option;
 
414
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
415
          if(not add_argument(getplugin(plugin_name), plugin_option)){
414
416
            perror("add_argument");
415
417
            return ARGP_ERR_UNKNOWN;
416
418
          }
433
435
      }
434
436
      break;
435
437
    case 'd':                   /* --disable */
436
 
      if (arg != NULL){
 
438
      if(arg != NULL){
437
439
        plugin *p = getplugin(arg);
438
440
        if(p == NULL){
439
441
          return ARGP_ERR_UNKNOWN;
442
444
      }
443
445
      break;
444
446
    case 'e':                   /* --enable */
445
 
      if (arg != NULL){
 
447
      if(arg != NULL){
446
448
        plugin *p = getplugin(arg);
447
449
        if(p == NULL){
448
450
          return ARGP_ERR_UNKNOWN;
461
463
      /* This is already done by parse_opt_config_file() */
462
464
      break;
463
465
    case 130:                   /* --userid */
464
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
466
      errno = 0;
 
467
      tmpmax = strtoimax(arg, &tmp, 10);
 
468
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
469
         or tmpmax != (uid_t)tmpmax){
 
470
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
471
                PRIdMAX "\n", arg, (intmax_t)uid);
 
472
      } else {
 
473
        uid = (uid_t)tmpmax;
 
474
      }
465
475
      break;
466
476
    case 131:                   /* --groupid */
467
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
477
      errno = 0;
 
478
      tmpmax = strtoimax(arg, &tmp, 10);
 
479
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
480
         or tmpmax != (gid_t)tmpmax){
 
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
482
                PRIdMAX "\n", arg, (intmax_t)gid);
 
483
      } else {
 
484
        gid = (gid_t)tmpmax;
 
485
      }
468
486
      break;
469
487
    case 132:                   /* --debug */
470
488
      debug = true;
471
489
      break;
 
490
/*
 
491
 * When adding more options before this line, remember to also add a
 
492
 * "case" to the "parse_opt_config_file" function below.
 
493
 */
472
494
    case ARGP_KEY_ARG:
473
495
      /* Cryptsetup always passes an argument, which is an empty
474
496
         string if "none" was specified in /etc/crypttab.  So if
487
509
  
488
510
  /* This option parser is the same as parse_opt() above, except it
489
511
     ignores everything but the --config-file option. */
490
 
  error_t parse_opt_config_file (int key, char *arg,
491
 
                                 __attribute__((unused))
492
 
                                 struct argp_state *state) {
493
 
    switch (key) {
 
512
  error_t parse_opt_config_file(int key, char *arg,
 
513
                                __attribute__((unused))
 
514
                                struct argp_state *state){
 
515
    switch(key){
494
516
    case 'g':                   /* --global-options */
495
517
    case 'G':                   /* --global-env */
496
518
    case 'o':                   /* --options-for */
523
545
                       .args_doc = "",
524
546
                       .doc = "Mandos plugin runner -- Run plugins" };
525
547
  
526
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
548
  /* Parse using parse_opt_config_file() in order to get the custom
527
549
     config file location, if any. */
528
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
529
 
  if (ret == ARGP_ERR_UNKNOWN){
 
550
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
551
  if(ret == ARGP_ERR_UNKNOWN){
530
552
    fprintf(stderr, "Unknown error while parsing arguments\n");
531
553
    exitstatus = EXIT_FAILURE;
532
554
    goto fallback;
536
558
  argp.parser = parse_opt;
537
559
  
538
560
  /* Open the configfile if available */
539
 
  if (argfile == NULL){
 
561
  if(argfile == NULL){
540
562
    conffp = fopen(AFILE, "r");
541
563
  } else {
542
564
    conffp = fopen(argfile, "r");
545
567
    char *org_line = NULL;
546
568
    char *p, *arg, *new_arg, *line;
547
569
    size_t size = 0;
548
 
    ssize_t sret;
549
570
    const char whitespace_delims[] = " \r\t\f\v\n";
550
571
    const char comment_delim[] = "#";
551
 
 
 
572
    
552
573
    custom_argc = 1;
553
574
    custom_argv = malloc(sizeof(char*) * 2);
554
575
    if(custom_argv == NULL){
558
579
    }
559
580
    custom_argv[0] = argv[0];
560
581
    custom_argv[1] = NULL;
561
 
 
 
582
    
562
583
    /* for each line in the config file, strip whitespace and ignore
563
584
       commented text */
564
585
    while(true){
566
587
      if(sret == -1){
567
588
        break;
568
589
      }
569
 
 
 
590
      
570
591
      line = org_line;
571
592
      arg = strsep(&line, comment_delim);
572
593
      while((p = strsep(&arg, whitespace_delims)) != NULL){
598
619
  } else {
599
620
    /* Check for harmful errors and go to fallback. Other errors might
600
621
       not affect opening plugins */
601
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
622
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
602
623
      perror("fopen");
603
624
      exitstatus = EXIT_FAILURE;
604
625
      goto fallback;
607
628
  /* If there was any arguments from configuration file,
608
629
     pass them to parser as command arguments */
609
630
  if(custom_argv != NULL){
610
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
611
 
                      0, NULL);
612
 
    if (ret == ARGP_ERR_UNKNOWN){
 
631
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
632
                     0, NULL);
 
633
    if(ret == ARGP_ERR_UNKNOWN){
613
634
      fprintf(stderr, "Unknown error while parsing arguments\n");
614
635
      exitstatus = EXIT_FAILURE;
615
636
      goto fallback;
618
639
  
619
640
  /* Parse actual command line arguments, to let them override the
620
641
     config file */
621
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
622
 
  if (ret == ARGP_ERR_UNKNOWN){
 
642
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
643
  if(ret == ARGP_ERR_UNKNOWN){
623
644
    fprintf(stderr, "Unknown error while parsing arguments\n");
624
645
    exitstatus = EXIT_FAILURE;
625
646
    goto fallback;
632
653
      for(char **a = p->argv; *a != NULL; a++){
633
654
        fprintf(stderr, "\tArg: %s\n", *a);
634
655
      }
635
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
656
      fprintf(stderr, "...and %d environment variables\n", p->envc);
636
657
      for(char **a = p->environ; *a != NULL; a++){
637
658
        fprintf(stderr, "\t%s\n", *a);
638
659
      }
640
661
  }
641
662
  
642
663
  /* Strip permissions down to nobody */
 
664
  setgid(gid);
 
665
  if(ret == -1){
 
666
    perror("setgid");
 
667
  }
643
668
  ret = setuid(uid);
644
 
  if (ret == -1){
 
669
  if(ret == -1){
645
670
    perror("setuid");
646
 
  }  
647
 
  setgid(gid);
648
 
  if (ret == -1){
649
 
    perror("setgid");
650
671
  }
651
672
  
652
 
  if (plugindir == NULL){
 
673
  if(plugindir == NULL){
653
674
    dir = opendir(PDIR);
654
675
  } else {
655
676
    dir = opendir(plugindir);
682
703
    
683
704
    /* All directory entries have been processed */
684
705
    if(dirst == NULL){
685
 
      if (errno == EBADF){
 
706
      if(errno == EBADF){
686
707
        perror("readdir");
687
708
        exitstatus = EXIT_FAILURE;
688
709
        goto fallback;
700
721
      
701
722
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
702
723
                                           ".dpkg-old",
 
724
                                           ".dpkg-bak",
703
725
                                           ".dpkg-divert", NULL };
704
726
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
705
727
        size_t pre_len = strlen(*pre);
734
756
        continue;
735
757
      }
736
758
    }
737
 
 
 
759
    
738
760
    char *filename;
739
761
    if(plugindir == NULL){
740
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
747
769
    }
748
770
    
749
771
    ret = stat(filename, &st);
750
 
    if (ret == -1){
 
772
    if(ret == -1){
751
773
      perror("stat");
752
774
      free(filename);
753
775
      continue;
754
776
    }
755
 
 
 
777
    
756
778
    /* Ignore non-executable files */
757
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
758
780
      if(debug){
759
781
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
760
782
                " with bad type or mode\n", filename);
807
829
    
808
830
    int pipefd[2];
809
831
    ret = pipe(pipefd);
810
 
    if (ret == -1){
 
832
    if(ret == -1){
811
833
      perror("pipe");
812
834
      exitstatus = EXIT_FAILURE;
813
835
      goto fallback;
826
848
      goto fallback;
827
849
    }
828
850
    /* Block SIGCHLD until process is safely in process list */
829
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
851
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
830
852
    if(ret < 0){
831
853
      perror("sigprocmask");
832
854
      exitstatus = EXIT_FAILURE;
880
902
    close(pipefd[1]);           /* Close unused write end of pipe */
881
903
    free(filename);
882
904
    plugin *new_plugin = getplugin(dirst->d_name);
883
 
    if (new_plugin == NULL){
 
905
    if(new_plugin == NULL){
884
906
      perror("getplugin");
885
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
907
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
886
908
      if(ret < 0){
887
909
        perror("sigprocmask");
888
910
      }
895
917
    
896
918
    /* Unblock SIGCHLD so signal handler can be run if this process
897
919
       has already completed */
898
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
920
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
899
921
    if(ret < 0){
900
922
      perror("sigprocmask");
901
923
      exitstatus = EXIT_FAILURE;
904
926
    
905
927
    FD_SET(new_plugin->fd, &rfds_all);
906
928
    
907
 
    if (maxfd < new_plugin->fd){
 
929
    if(maxfd < new_plugin->fd){
908
930
      maxfd = new_plugin->fd;
909
931
    }
910
932
  }
911
933
  
912
934
  closedir(dir);
913
935
  dir = NULL;
 
936
  free_plugin(getplugin(NULL));
914
937
  
915
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
916
939
    if(p->pid != 0){
927
950
  while(plugin_list){
928
951
    fd_set rfds = rfds_all;
929
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
930
 
    if (select_ret == -1){
 
953
    if(select_ret == -1){
931
954
      perror("select");
932
955
      exitstatus = EXIT_FAILURE;
933
956
      goto fallback;
936
959
       from one of them */
937
960
    for(plugin *proc = plugin_list; proc != NULL;){
938
961
      /* Is this process completely done? */
939
 
      if(proc->eof and proc->completed){
 
962
      if(proc->completed and proc->eof){
940
963
        /* Only accept the plugin output if it exited cleanly */
941
964
        if(not WIFEXITED(proc->status)
942
965
           or WEXITSTATUS(proc->status) != 0){
943
966
          /* Bad exit by plugin */
944
 
 
 
967
          
945
968
          if(debug){
946
969
            if(WIFEXITED(proc->status)){
947
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
948
 
                      (unsigned int) (proc->pid),
 
970
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
971
                      " status %d\n", proc->name,
 
972
                      (intmax_t) (proc->pid),
949
973
                      WEXITSTATUS(proc->status));
950
 
            } else if(WIFSIGNALED(proc->status)) {
951
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
952
 
                      (unsigned int) (proc->pid),
 
974
            } else if(WIFSIGNALED(proc->status)){
 
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
976
                      " signal %d\n", proc->name,
 
977
                      (intmax_t) (proc->pid),
953
978
                      WTERMSIG(proc->status));
954
979
            } else if(WCOREDUMP(proc->status)){
955
 
              fprintf(stderr, "Plugin %d dumped core\n",
956
 
                      (unsigned int) (proc->pid));
 
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
957
982
            }
958
983
          }
959
984
          
960
985
          /* Remove the plugin */
961
986
          FD_CLR(proc->fd, &rfds_all);
962
 
 
 
987
          
963
988
          /* Block signal while modifying process_list */
964
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
965
990
          if(ret < 0){
973
998
          proc = next_plugin;
974
999
          
975
1000
          /* We are done modifying process list, so unblock signal */
976
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
 
                             NULL);
 
1001
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1002
                            NULL);
978
1003
          if(ret < 0){
979
1004
            perror("sigprocmask");
980
1005
            exitstatus = EXIT_FAILURE;
1009
1034
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1010
1035
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1011
1036
                               + (size_t) BUFFER_SIZE);
1012
 
        if (proc->buffer == NULL){
 
1037
        if(proc->buffer == NULL){
1013
1038
          perror("malloc");
1014
1039
          exitstatus = EXIT_FAILURE;
1015
1040
          goto fallback;
1017
1042
        proc->buffer_size += BUFFER_SIZE;
1018
1043
      }
1019
1044
      /* Read from the process */
1020
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1021
 
                 BUFFER_SIZE);
1022
 
      if(ret < 0){
 
1045
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1046
                  BUFFER_SIZE);
 
1047
      if(sret < 0){
1023
1048
        /* Read error from this process; ignore the error */
1024
1049
        proc = proc->next;
1025
1050
        continue;
1026
1051
      }
1027
 
      if(ret == 0){
 
1052
      if(sret == 0){
1028
1053
        /* got EOF */
1029
1054
        proc->eof = true;
1030
1055
      } else {
1031
 
        proc->buffer_length += (size_t) ret;
 
1056
        proc->buffer_length += (size_t) sret;
1032
1057
      }
1033
1058
    }
1034
1059
  }
1035
 
 
1036
 
 
 
1060
  
 
1061
  
1037
1062
 fallback:
1038
1063
  
1039
1064
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){