/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-21 04:22:50 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080921042250-r0qqjsbz2ulfo5le
* Makefile: Bug fix: fix syntax error.

* debian/control: Depend on "po-debconf".
  (mandos, mandos-client): Add "${misc:Depends}", used by
                           dh_installdebconf.

* debian/mandos-client.config: New; show note.
* debian/mandos-client.template: New.

* debian/mandos.README.Debian: New.

* debian/mandos.config: New; show note.
* debian/mandos.prerm: New; stop daemon.
* debian/mandos.template: New.

* debian/po/POTFILES.in: New.
* debian/po/sv.po: New.

* debian/rules (clean): Added "debconf-updatepo" as suggested by
                        po-debconf(7).
  (binary-common): Added "dh_installdebconf" to use "debian/*.config"
                   and "debian/*.template" files.

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