/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-01-14 14:20:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090114142017-84t4kfw56bsftjlo
Fixes for sscanf usage:

* plugin-runner.c (main): Parse numbers correctly and portably using
                          an intermediate "intmax_t".  Cast "pid_t" to
                          "intmax_t" before passing it to "printf()".
* plugins.d/mandos-client.c (main): Parse numbers correctly and
                                    portably using an intermediate
                                    "intmax_t".  Bug fix: cast
                                    "AvahiIfIndex" to "intmax_t" and
                                    use "PRIdMAX" instead of using
                                    "PRIu16", and use "PRIu16" to
                                    format port number.
* plugins.d/splashy.c (main): Parse numbers correctly and portably
                              using an intermediate "intmax_t".
* plugins.d/usplash.c (main): - '' -

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
27
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
29
                                   EXIT_SUCCESS, realloc() */
29
30
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO */
32
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
34
                                   stat, waitpid(), WIFEXITED(),
34
35
                                   WEXITSTATUS(), wait(), pid_t,
46
47
                                   fcntl(), setuid(), setgid(),
47
48
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
49
                                   access(), pipe(), fork(), close()
49
 
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   dup2(), STDOUT_FILENO, _exit(),
50
51
                                   execv(), write(), read(),
51
52
                                   close() */
52
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
63
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
65
                                   SIG_UNBLOCK, kill() */
65
66
#include <errno.h>              /* errno, EBADF */
 
67
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
66
68
 
67
69
#define BUFFER_SIZE 256
68
70
 
69
71
#define PDIR "/lib/mandos/plugins.d"
70
72
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
73
 
72
 
const char *argp_program_version = "plugin-runner 1.0";
 
74
const char *argp_program_version = "plugin-runner " VERSION;
73
75
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
76
 
75
77
typedef struct plugin{
98
100
   or if none is found, creates a new one */
99
101
static plugin *getplugin(char *name){
100
102
  /* 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))){
 
103
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
104
    if((p->name == name)
 
105
       or (p->name and name and (strcmp(p->name, name) == 0))){
104
106
      return p;
105
107
    }
106
108
  }
107
109
  /* Create a new plugin */
108
110
  plugin *new_plugin = malloc(sizeof(plugin));
109
 
  if (new_plugin == NULL){
 
111
  if(new_plugin == NULL){
110
112
    return NULL;
111
113
  }
112
114
  char *copy_name = NULL;
123
125
                           .next = plugin_list };
124
126
  
125
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
126
 
  if (new_plugin->argv == NULL){
 
128
  if(new_plugin->argv == NULL){
127
129
    free(copy_name);
128
130
    free(new_plugin);
129
131
    return NULL;
229
231
      break;
230
232
    }
231
233
    if(pid == -1){
232
 
      if (errno != ECHILD){
 
234
      if(errno != ECHILD){
233
235
        perror("waitpid");
234
236
      }
235
237
      /* No child processes */
307
309
  struct dirent *dirst;
308
310
  struct stat st;
309
311
  fd_set rfds_all;
310
 
  int ret, maxfd = 0;
 
312
  int ret, numchars, maxfd = 0;
 
313
  ssize_t sret;
 
314
  intmax_t tmpmax;
311
315
  uid_t uid = 65534;
312
316
  gid_t gid = 65534;
313
317
  bool debug = false;
370
374
    { .name = NULL }
371
375
  };
372
376
  
373
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
374
 
                     struct argp_state *state) {
375
 
    switch (key) {
 
377
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
378
                    struct argp_state *state) {
 
379
    switch(key) {
376
380
    case 'g':                   /* --global-options */
377
 
      if (arg != NULL){
 
381
      if(arg != NULL){
378
382
        char *p;
379
383
        while((p = strsep(&arg, ",")) != NULL){
380
384
          if(p[0] == '\0'){
396
400
      }
397
401
      break;
398
402
    case 'o':                   /* --options-for */
399
 
      if (arg != NULL){
 
403
      if(arg != NULL){
400
404
        char *p_name = strsep(&arg, ":");
401
405
        if(p_name[0] == '\0' or arg == NULL){
402
406
          break;
433
437
      }
434
438
      break;
435
439
    case 'd':                   /* --disable */
436
 
      if (arg != NULL){
 
440
      if(arg != NULL){
437
441
        plugin *p = getplugin(arg);
438
442
        if(p == NULL){
439
443
          return ARGP_ERR_UNKNOWN;
442
446
      }
443
447
      break;
444
448
    case 'e':                   /* --enable */
445
 
      if (arg != NULL){
 
449
      if(arg != NULL){
446
450
        plugin *p = getplugin(arg);
447
451
        if(p == NULL){
448
452
          return ARGP_ERR_UNKNOWN;
461
465
      /* This is already done by parse_opt_config_file() */
462
466
      break;
463
467
    case 130:                   /* --userid */
464
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
468
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
469
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
470
         or arg[numchars] != '\0'){
 
471
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
472
                PRIdMAX "\n", arg, (intmax_t)uid);
 
473
      } else {
 
474
        uid = (uid_t)tmpmax;
 
475
      }
465
476
      break;
466
477
    case 131:                   /* --groupid */
467
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
478
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
479
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
480
         or arg[numchars] != '\0'){
 
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
 
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
      }
641
662
  
642
663
  /* Strip permissions down to nobody */
643
664
  ret = setuid(uid);
644
 
  if (ret == -1){
 
665
  if(ret == -1){
645
666
    perror("setuid");
646
667
  }  
647
668
  setgid(gid);
648
 
  if (ret == -1){
 
669
  if(ret == -1){
649
670
    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);
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;
846
868
        perror("sigaction");
847
869
        _exit(EXIT_FAILURE);
848
870
      }
849
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
871
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
850
872
      if(ret < 0){
851
873
        perror("sigprocmask");
852
874
        _exit(EXIT_FAILURE);
853
875
      }
854
 
 
 
876
      
855
877
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
856
878
      if(ret == -1){
857
879
        perror("dup2");
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
 
    
911
932
  }
912
933
  
913
934
  closedir(dir);
914
935
  dir = NULL;
915
 
 
 
936
  
916
937
  for(plugin *p = plugin_list; p != NULL; p = p->next){
917
938
    if(p->pid != 0){
918
939
      break;
923
944
      free_plugin_list();
924
945
    }
925
946
  }
926
 
 
 
947
  
927
948
  /* Main loop while running plugins exist */
928
949
  while(plugin_list){
929
950
    fd_set rfds = rfds_all;
930
951
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
931
 
    if (select_ret == -1){
 
952
    if(select_ret == -1){
932
953
      perror("select");
933
954
      exitstatus = EXIT_FAILURE;
934
955
      goto fallback;
945
966
 
946
967
          if(debug){
947
968
            if(WIFEXITED(proc->status)){
948
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
949
 
                      (unsigned int) (proc->pid),
 
969
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
970
                      " %d\n", (intmax_t) (proc->pid),
950
971
                      WEXITSTATUS(proc->status));
951
972
            } else if(WIFSIGNALED(proc->status)) {
952
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
953
 
                      (unsigned int) (proc->pid),
 
973
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
974
                      " %d\n", (intmax_t) (proc->pid),
954
975
                      WTERMSIG(proc->status));
955
976
            } else if(WCOREDUMP(proc->status)){
956
 
              fprintf(stderr, "Plugin %d dumped core\n",
957
 
                      (unsigned int) (proc->pid));
 
977
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
978
                      (intmax_t) (proc->pid));
958
979
            }
959
980
          }
960
981
          
969
990
            goto fallback;
970
991
          }
971
992
          
 
993
          plugin *next_plugin = proc->next;
 
994
          free_plugin(proc);
 
995
          proc = next_plugin;
 
996
          
972
997
          /* We are done modifying process list, so unblock signal */
973
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
974
 
                             NULL);
 
998
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
999
                            NULL);
975
1000
          if(ret < 0){
976
1001
            perror("sigprocmask");
977
1002
            exitstatus = EXIT_FAILURE;
982
1007
            break;
983
1008
          }
984
1009
          
985
 
          plugin *next_plugin = proc->next;
986
 
          free_plugin(proc);
987
 
          proc = next_plugin;
988
1010
          continue;
989
1011
        }
990
1012
        
991
1013
        /* This process exited nicely, so print its buffer */
992
 
 
 
1014
        
993
1015
        bool bret = print_out_password(proc->buffer,
994
1016
                                       proc->buffer_length);
995
1017
        if(not bret){
1009
1031
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1010
1032
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1011
1033
                               + (size_t) BUFFER_SIZE);
1012
 
        if (proc->buffer == NULL){
 
1034
        if(proc->buffer == NULL){
1013
1035
          perror("malloc");
1014
1036
          exitstatus = EXIT_FAILURE;
1015
1037
          goto fallback;
1017
1039
        proc->buffer_size += BUFFER_SIZE;
1018
1040
      }
1019
1041
      /* Read from the process */
1020
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1021
 
                 BUFFER_SIZE);
1022
 
      if(ret < 0){
 
1042
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1043
                  BUFFER_SIZE);
 
1044
      if(sret < 0){
1023
1045
        /* Read error from this process; ignore the error */
1024
1046
        proc = proc->next;
1025
1047
        continue;
1026
1048
      }
1027
 
      if(ret == 0){
 
1049
      if(sret == 0){
1028
1050
        /* got EOF */
1029
1051
        proc->eof = true;
1030
1052
      } else {
1031
 
        proc->buffer_length += (size_t) ret;
 
1053
        proc->buffer_length += (size_t) sret;
1032
1054
      }
1033
1055
    }
1034
1056
  }
1061
1083
    perror("sigaction");
1062
1084
    exitstatus = EXIT_FAILURE;
1063
1085
  }
1064
 
 
 
1086
  
1065
1087
  if(custom_argv != NULL){
1066
1088
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1067
1089
      free(*arg);
1073
1095
    closedir(dir);
1074
1096
  }
1075
1097
  
1076
 
  /* Free the process list and kill the processes */
 
1098
  /* Kill the processes */
1077
1099
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1078
1100
    if(p->pid != 0){
1079
1101
      close(p->fd);
1092
1114
  if(errno != ECHILD){
1093
1115
    perror("wait");
1094
1116
  }
1095
 
 
 
1117
  
1096
1118
  free_plugin_list();
1097
1119
  
1098
1120
  free(plugindir);