/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-12 19:12:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080912191240-edjlcll43eoijkx0
* Makefile (install): Use "install-client-nokey".
  (install-server): Create "/etc/default" and "/usr/sbin", too.
  (install-client): Do not depend on "$(INITRAMFSTOOLS)/hooks/.".
                    Renamed to "install-client-nokey".  Split out
                    post-installation-stuff to new "install-client"
                    target.

* mandos-clients.conf.xml: White space adjustments.
* mandos-keygen.xml: - '' -
* mandos.conf.xml: - '' -
* mandos.xml: - '' -
* plugin-runner.xml: - '' -
* plugins.d/mandos-client.xml: - '' -

* overview.xml: Improved grammar.

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;
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 */
102
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if((p->name == name)
104
 
       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))){
105
106
      return p;
106
107
    }
107
108
  }
108
109
  /* Create a new plugin */
109
110
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if(new_plugin == NULL){
 
111
  if (new_plugin == NULL){
111
112
    return NULL;
112
113
  }
113
114
  char *copy_name = NULL;
124
125
                           .next = plugin_list };
125
126
  
126
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if(new_plugin->argv == NULL){
 
128
  if (new_plugin->argv == NULL){
128
129
    free(copy_name);
129
130
    free(new_plugin);
130
131
    return NULL;
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;
230
232
      break;
231
233
    }
232
234
    if(pid == -1){
233
 
      if(errno != ECHILD){
 
235
      if (errno != ECHILD){
234
236
        perror("waitpid");
235
237
      }
236
238
      /* No child processes */
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;
256
258
  for(size_t written = 0; written < length; written += (size_t)ret){
257
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
309
311
  struct stat st;
310
312
  fd_set rfds_all;
311
313
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
314
  uid_t uid = 65534;
314
315
  gid_t gid = 65534;
315
316
  bool debug = false;
372
373
    { .name = NULL }
373
374
  };
374
375
  
375
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
376
 
                    struct argp_state *state) {
377
 
    switch(key) {
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
 
378
    switch (key) {
378
379
    case 'g':                   /* --global-options */
379
 
      if(arg != NULL){
 
380
      if (arg != NULL){
380
381
        char *p;
381
382
        while((p = strsep(&arg, ",")) != NULL){
382
383
          if(p[0] == '\0'){
398
399
      }
399
400
      break;
400
401
    case 'o':                   /* --options-for */
401
 
      if(arg != NULL){
 
402
      if (arg != NULL){
402
403
        char *p_name = strsep(&arg, ":");
403
404
        if(p_name[0] == '\0' or arg == NULL){
404
405
          break;
435
436
      }
436
437
      break;
437
438
    case 'd':                   /* --disable */
438
 
      if(arg != NULL){
 
439
      if (arg != NULL){
439
440
        plugin *p = getplugin(arg);
440
441
        if(p == NULL){
441
442
          return ARGP_ERR_UNKNOWN;
444
445
      }
445
446
      break;
446
447
    case 'e':                   /* --enable */
447
 
      if(arg != NULL){
 
448
      if (arg != NULL){
448
449
        plugin *p = getplugin(arg);
449
450
        if(p == NULL){
450
451
          return ARGP_ERR_UNKNOWN;
463
464
      /* This is already done by parse_opt_config_file() */
464
465
      break;
465
466
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%u", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
470
 
                uid);
471
 
      }
 
467
      uid = (uid_t)strtol(arg, NULL, 10);
472
468
      break;
473
469
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%u", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
478
 
                arg, gid);
479
 
      }
 
470
      gid = (gid_t)strtol(arg, NULL, 10);
480
471
      break;
481
472
    case 132:                   /* --debug */
482
473
      debug = true;
483
474
      break;
484
 
/*
485
 
 * When adding more options before this line, remember to also add a
486
 
 * "case" to the "parse_opt_config_file" function below.
487
 
 */
488
475
    case ARGP_KEY_ARG:
489
476
      /* Cryptsetup always passes an argument, which is an empty
490
477
         string if "none" was specified in /etc/crypttab.  So if
503
490
  
504
491
  /* This option parser is the same as parse_opt() above, except it
505
492
     ignores everything but the --config-file option. */
506
 
  error_t parse_opt_config_file(int key, char *arg,
507
 
                                __attribute__((unused))
508
 
                                struct argp_state *state) {
509
 
    switch(key) {
 
493
  error_t parse_opt_config_file (int key, char *arg,
 
494
                                 __attribute__((unused))
 
495
                                 struct argp_state *state) {
 
496
    switch (key) {
510
497
    case 'g':                   /* --global-options */
511
498
    case 'G':                   /* --global-env */
512
499
    case 'o':                   /* --options-for */
539
526
                       .args_doc = "",
540
527
                       .doc = "Mandos plugin runner -- Run plugins" };
541
528
  
542
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
529
  /* Parse using the parse_opt_config_file in order to get the custom
543
530
     config file location, if any. */
544
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if(ret == ARGP_ERR_UNKNOWN){
 
531
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
532
  if (ret == ARGP_ERR_UNKNOWN){
546
533
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
534
    exitstatus = EXIT_FAILURE;
548
535
    goto fallback;
552
539
  argp.parser = parse_opt;
553
540
  
554
541
  /* Open the configfile if available */
555
 
  if(argfile == NULL){
 
542
  if (argfile == NULL){
556
543
    conffp = fopen(AFILE, "r");
557
544
  } else {
558
545
    conffp = fopen(argfile, "r");
561
548
    char *org_line = NULL;
562
549
    char *p, *arg, *new_arg, *line;
563
550
    size_t size = 0;
 
551
    ssize_t sret;
564
552
    const char whitespace_delims[] = " \r\t\f\v\n";
565
553
    const char comment_delim[] = "#";
566
554
 
613
601
  } else {
614
602
    /* Check for harmful errors and go to fallback. Other errors might
615
603
       not affect opening plugins */
616
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
604
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
605
      perror("fopen");
618
606
      exitstatus = EXIT_FAILURE;
619
607
      goto fallback;
622
610
  /* If there was any arguments from configuration file,
623
611
     pass them to parser as command arguments */
624
612
  if(custom_argv != NULL){
625
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
626
 
                     0, NULL);
627
 
    if(ret == ARGP_ERR_UNKNOWN){
 
613
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
614
                      0, NULL);
 
615
    if (ret == ARGP_ERR_UNKNOWN){
628
616
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
617
      exitstatus = EXIT_FAILURE;
630
618
      goto fallback;
633
621
  
634
622
  /* Parse actual command line arguments, to let them override the
635
623
     config file */
636
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if(ret == ARGP_ERR_UNKNOWN){
 
624
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
625
  if (ret == ARGP_ERR_UNKNOWN){
638
626
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
627
    exitstatus = EXIT_FAILURE;
640
628
    goto fallback;
656
644
  
657
645
  /* Strip permissions down to nobody */
658
646
  ret = setuid(uid);
659
 
  if(ret == -1){
 
647
  if (ret == -1){
660
648
    perror("setuid");
661
649
  }  
662
650
  setgid(gid);
663
 
  if(ret == -1){
 
651
  if (ret == -1){
664
652
    perror("setgid");
665
653
  }
666
654
  
667
 
  if(plugindir == NULL){
 
655
  if (plugindir == NULL){
668
656
    dir = opendir(PDIR);
669
657
  } else {
670
658
    dir = opendir(plugindir);
697
685
    
698
686
    /* All directory entries have been processed */
699
687
    if(dirst == NULL){
700
 
      if(errno == EBADF){
 
688
      if (errno == EBADF){
701
689
        perror("readdir");
702
690
        exitstatus = EXIT_FAILURE;
703
691
        goto fallback;
715
703
      
716
704
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
717
705
                                           ".dpkg-old",
718
 
                                           ".dpkg-bak",
719
706
                                           ".dpkg-divert", NULL };
720
707
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
721
708
        size_t pre_len = strlen(*pre);
763
750
    }
764
751
    
765
752
    ret = stat(filename, &st);
766
 
    if(ret == -1){
 
753
    if (ret == -1){
767
754
      perror("stat");
768
755
      free(filename);
769
756
      continue;
770
757
    }
771
758
 
772
759
    /* Ignore non-executable files */
773
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
760
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
761
      if(debug){
775
762
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
763
                " with bad type or mode\n", filename);
823
810
    
824
811
    int pipefd[2];
825
812
    ret = pipe(pipefd);
826
 
    if(ret == -1){
 
813
    if (ret == -1){
827
814
      perror("pipe");
828
815
      exitstatus = EXIT_FAILURE;
829
816
      goto fallback;
842
829
      goto fallback;
843
830
    }
844
831
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
832
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
833
    if(ret < 0){
847
834
      perror("sigprocmask");
848
835
      exitstatus = EXIT_FAILURE;
862
849
        perror("sigaction");
863
850
        _exit(EXIT_FAILURE);
864
851
      }
865
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
852
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
866
853
      if(ret < 0){
867
854
        perror("sigprocmask");
868
855
        _exit(EXIT_FAILURE);
869
856
      }
870
 
      
 
857
 
871
858
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
872
859
      if(ret == -1){
873
860
        perror("dup2");
896
883
    close(pipefd[1]);           /* Close unused write end of pipe */
897
884
    free(filename);
898
885
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if(new_plugin == NULL){
 
886
    if (new_plugin == NULL){
900
887
      perror("getplugin");
901
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
888
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
889
      if(ret < 0){
903
890
        perror("sigprocmask");
904
891
      }
911
898
    
912
899
    /* Unblock SIGCHLD so signal handler can be run if this process
913
900
       has already completed */
914
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
901
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
902
    if(ret < 0){
916
903
      perror("sigprocmask");
917
904
      exitstatus = EXIT_FAILURE;
920
907
    
921
908
    FD_SET(new_plugin->fd, &rfds_all);
922
909
    
923
 
    if(maxfd < new_plugin->fd){
 
910
    if (maxfd < new_plugin->fd){
924
911
      maxfd = new_plugin->fd;
925
912
    }
 
913
    
926
914
  }
927
915
  
928
916
  closedir(dir);
929
917
  dir = NULL;
930
 
  
 
918
 
931
919
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
920
    if(p->pid != 0){
933
921
      break;
938
926
      free_plugin_list();
939
927
    }
940
928
  }
941
 
  
 
929
 
942
930
  /* Main loop while running plugins exist */
943
931
  while(plugin_list){
944
932
    fd_set rfds = rfds_all;
945
933
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if(select_ret == -1){
 
934
    if (select_ret == -1){
947
935
      perror("select");
948
936
      exitstatus = EXIT_FAILURE;
949
937
      goto fallback;
968
956
                      (unsigned int) (proc->pid),
969
957
                      WTERMSIG(proc->status));
970
958
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
 
959
              fprintf(stderr, "Plugin %d dumped core\n",
972
960
                      (unsigned int) (proc->pid));
973
961
            }
974
962
          }
984
972
            goto fallback;
985
973
          }
986
974
          
 
975
          /* We are done modifying process list, so unblock signal */
 
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
977
                             NULL);
 
978
          if(ret < 0){
 
979
            perror("sigprocmask");
 
980
            exitstatus = EXIT_FAILURE;
 
981
            goto fallback;
 
982
          }
 
983
          
 
984
          if(plugin_list == NULL){
 
985
            break;
 
986
          }
 
987
          
987
988
          plugin *next_plugin = proc->next;
988
989
          free_plugin(proc);
989
990
          proc = next_plugin;
990
 
          
991
 
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                            NULL);
994
 
          if(ret < 0){
995
 
            perror("sigprocmask");
996
 
            exitstatus = EXIT_FAILURE;
997
 
            goto fallback;
998
 
          }
999
 
          
1000
 
          if(plugin_list == NULL){
1001
 
            break;
1002
 
          }
1003
 
          
1004
991
          continue;
1005
992
        }
1006
993
        
1007
994
        /* This process exited nicely, so print its buffer */
1008
 
        
 
995
 
1009
996
        bool bret = print_out_password(proc->buffer,
1010
997
                                       proc->buffer_length);
1011
998
        if(not bret){
1025
1012
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
1013
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
1014
                               + (size_t) BUFFER_SIZE);
1028
 
        if(proc->buffer == NULL){
 
1015
        if (proc->buffer == NULL){
1029
1016
          perror("malloc");
1030
1017
          exitstatus = EXIT_FAILURE;
1031
1018
          goto fallback;
1033
1020
        proc->buffer_size += BUFFER_SIZE;
1034
1021
      }
1035
1022
      /* Read from the process */
1036
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1037
 
                  BUFFER_SIZE);
1038
 
      if(sret < 0){
 
1023
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1024
                 BUFFER_SIZE);
 
1025
      if(ret < 0){
1039
1026
        /* Read error from this process; ignore the error */
1040
1027
        proc = proc->next;
1041
1028
        continue;
1042
1029
      }
1043
 
      if(sret == 0){
 
1030
      if(ret == 0){
1044
1031
        /* got EOF */
1045
1032
        proc->eof = true;
1046
1033
      } else {
1047
 
        proc->buffer_length += (size_t) sret;
 
1034
        proc->buffer_length += (size_t) ret;
1048
1035
      }
1049
1036
    }
1050
1037
  }
1077
1064
    perror("sigaction");
1078
1065
    exitstatus = EXIT_FAILURE;
1079
1066
  }
1080
 
  
 
1067
 
1081
1068
  if(custom_argv != NULL){
1082
1069
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1083
1070
      free(*arg);
1089
1076
    closedir(dir);
1090
1077
  }
1091
1078
  
1092
 
  /* Kill the processes */
 
1079
  /* Free the process list and kill the processes */
1093
1080
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1094
1081
    if(p->pid != 0){
1095
1082
      close(p->fd);
1108
1095
  if(errno != ECHILD){
1109
1096
    perror("wait");
1110
1097
  }
1111
 
  
 
1098
 
1112
1099
  free_plugin_list();
1113
1100
  
1114
1101
  free(plugindir);