/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: 2014-06-22 02:19:30 UTC
  • Revision ID: teddy@recompile.se-20140622021930-icl7h4cm97blhjml
mandos-keygen: Generate "checker" option to use SSH fingerprints.

To turn this off, use a new "--no-ssh" option to mandos-keygen.

* INSTALL (Mandos Server, Mandos Client): Document new suggested
                                          installation of SSH.
* Makefile (confdir/clients.conf): Use new "--no-ssh" option to
                                   "mandos-keygen".
* debian/control (mandos/Depends): Changed to "fping | ssh-client".
  (mandos-client/Recommends): New; set to "ssh".
* intro.xml (FREQUENTLY ASKED QUESTIONS): Rename and rewrite section
                                          called "Faking ping
                                          replies?" to address new
                                          default behavior.
* mandos-clients.conf.xml (OPTIONS/checker): Briefly discuss new
                                             behavior of
                                             mandos-keygen.
* mandos-keygen: Bug fix: Suppress failure output of "shred" to remove
                 "sec*", since no such files may exist.
 (password mode): Scan for SSH key fingerprints and output as new
                  "checker" and "ssh_fingerprint" options, unless new
                  "--no-ssh" option is given.
* mandos-keygen.xml (SYNOPSIS/--force): Bug fix: Document short form.
  (OPTIONS/--no-ssh): New.
  (SEE ALSO): Add reference "ssh-keyscan(1)".
* plugins.d/mandos-client.xml (SECURITY): Briefly mention the
                                          possibility of using SSH key
                                          fingerprints for checking.

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-2017 Teddy Hogeborn
6
 
 * Copyright © 2008-2017 Björn Påhlsson
 
5
 * Copyright © 2008-2014 Teddy Hogeborn
 
6
 * Copyright © 2008-2014 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
37
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
40
 
                                   WEXITSTATUS(), WTERMSIG() */
 
40
                                   WEXITSTATUS(), WTERMSIG(),
 
41
                                   WCOREDUMP() */
41
42
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
42
43
#include <iso646.h>             /* and, or, not */
43
44
#include <dirent.h>             /* struct dirent, scandirat() */
75
76
#define BUFFER_SIZE 256
76
77
 
77
78
#define PDIR "/lib/mandos/plugins.d"
78
 
#define PHDIR "/lib/mandos/plugin-helpers"
79
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
80
 
81
81
const char *argp_program_version = "plugin-runner " VERSION;
347
347
 
348
348
int main(int argc, char *argv[]){
349
349
  char *plugindir = NULL;
350
 
  char *pluginhelperdir = NULL;
351
350
  char *argfile = NULL;
352
351
  FILE *conffp;
353
352
  struct dirent **direntries = NULL;
415
414
      .doc = "Group ID the plugins will run as", .group = 3 },
416
415
    { .name = "debug", .key = 132,
417
416
      .doc = "Debug mode", .group = 4 },
418
 
    { .name = "plugin-helper-dir", .key = 133,
419
 
      .arg = "DIRECTORY",
420
 
      .doc = "Specify a different plugin helper directory",
421
 
      .group = 2 },
422
417
    /*
423
418
     * These reproduce what we would get without ARGP_NO_HELP
424
419
     */
550
545
    case 132:                   /* --debug */
551
546
      debug = true;
552
547
      break;
553
 
    case 133:                   /* --plugin-helper-dir */
554
 
      free(pluginhelperdir);
555
 
      pluginhelperdir = strdup(arg);
556
 
      if(pluginhelperdir != NULL){
557
 
        errno = 0;
558
 
      }
559
 
      break;
560
548
      /*
561
549
       * These reproduce what we would get without ARGP_NO_HELP
562
550
       */
613
601
    case 130:                   /* --userid */
614
602
    case 131:                   /* --groupid */
615
603
    case 132:                   /* --debug */
616
 
    case 133:                   /* --plugin-helper-dir */
617
604
    case '?':                   /* --help */
618
605
    case -3:                    /* --usage */
619
606
    case 'V':                   /* --version */
700
687
        custom_argc += 1;
701
688
        {
702
689
          char **new_argv = realloc(custom_argv, sizeof(char *)
703
 
                                    * ((size_t)custom_argc + 1));
 
690
                                    * ((unsigned int)
 
691
                                       custom_argc + 1));
704
692
          if(new_argv == NULL){
705
693
            error(0, errno, "realloc");
706
694
            exitstatus = EX_OSERR;
773
761
    goto fallback;
774
762
  }
775
763
  
776
 
  {
777
 
    char *pluginhelperenv;
778
 
    bool bret = true;
779
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
780
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
781
 
    if(ret != -1){
782
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
783
 
    }
784
 
    if(ret == -1 or not bret){
785
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
786
 
            " environment variable to \"%s\" for all plugins\n",
787
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
788
 
    }
789
 
    if(ret != -1){
790
 
      free(pluginhelperenv);
791
 
    }
792
 
  }
793
 
  
794
764
  if(debug){
795
 
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
765
    for(plugin *p = plugin_list; p != NULL; p=p->next){
796
766
      fprintf(stderr, "Plugin: %s has %d arguments\n",
797
767
              p->name ? p->name : "Global", p->argc - 1);
798
768
      for(char **a = p->argv; *a != NULL; a++){
807
777
  
808
778
  if(getuid() == 0){
809
779
    /* Work around Debian bug #633582:
810
 
       <https://bugs.debian.org/633582> */
 
780
       <http://bugs.debian.org/633582> */
811
781
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
812
782
    if(plugindir_fd == -1){
813
783
      if(errno != ENOENT){
825
795
          }
826
796
        }
827
797
      }
828
 
      close(plugindir_fd);
 
798
      TEMP_FAILURE_RETRY(close(plugindir_fd));
829
799
    }
830
800
  }
831
801
  
890
860
    return 1;
891
861
  }
892
862
  
 
863
#ifdef __GLIBC__
 
864
#if __GLIBC_PREREQ(2, 15)
893
865
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
894
866
                             alphasort);
 
867
#else  /* not __GLIBC_PREREQ(2, 15) */
 
868
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
 
869
                           &direntries, good_name, alphasort);
 
870
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
871
#else   /* not __GLIBC__ */
 
872
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
 
873
                           &direntries, good_name, alphasort);
 
874
#endif  /* not __GLIBC__ */
895
875
  if(numplugins == -1){
896
876
    error(0, errno, "Could not scan plugin dir");
897
877
    direntries = NULL;
907
887
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
908
888
    if(plugin_fd == -1){
909
889
      error(0, errno, "Could not open plugin");
910
 
      free(direntries[i]);
911
890
      continue;
912
891
    }
913
892
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
914
893
    if(ret == -1){
915
894
      error(0, errno, "stat");
916
 
      close(plugin_fd);
917
 
      free(direntries[i]);
 
895
      TEMP_FAILURE_RETRY(close(plugin_fd));
918
896
      continue;
919
897
    }
920
898
    
928
906
                plugindir != NULL ? plugindir : PDIR,
929
907
                direntries[i]->d_name);
930
908
      }
931
 
      close(plugin_fd);
932
 
      free(direntries[i]);
 
909
      TEMP_FAILURE_RETRY(close(plugin_fd));
933
910
      continue;
934
911
    }
935
912
    
936
913
    plugin *p = getplugin(direntries[i]->d_name);
937
914
    if(p == NULL){
938
915
      error(0, errno, "getplugin");
939
 
      close(plugin_fd);
940
 
      free(direntries[i]);
 
916
      TEMP_FAILURE_RETRY(close(plugin_fd));
941
917
      continue;
942
918
    }
943
919
    if(p->disabled){
945
921
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
946
922
                direntries[i]->d_name);
947
923
      }
948
 
      close(plugin_fd);
949
 
      free(direntries[i]);
 
924
      TEMP_FAILURE_RETRY(close(plugin_fd));
950
925
      continue;
951
926
    }
952
927
    {
985
960
    if(ret == -1){
986
961
      error(0, errno, "pipe");
987
962
      exitstatus = EX_OSERR;
988
 
      free(direntries[i]);
989
963
      goto fallback;
990
964
    }
991
965
    if(pipefd[0] >= FD_SETSIZE){
992
966
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
993
967
              FD_SETSIZE);
994
 
      close(pipefd[0]);
995
 
      close(pipefd[1]);
 
968
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
969
      TEMP_FAILURE_RETRY(close(pipefd[1]));
996
970
      exitstatus = EX_OSERR;
997
 
      free(direntries[i]);
998
971
      goto fallback;
999
972
    }
1000
973
#ifndef O_CLOEXEC
1002
975
    ret = set_cloexec_flag(pipefd[0]);
1003
976
    if(ret < 0){
1004
977
      error(0, errno, "set_cloexec_flag");
1005
 
      close(pipefd[0]);
1006
 
      close(pipefd[1]);
 
978
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
979
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1007
980
      exitstatus = EX_OSERR;
1008
 
      free(direntries[i]);
1009
981
      goto fallback;
1010
982
    }
1011
983
    ret = set_cloexec_flag(pipefd[1]);
1012
984
    if(ret < 0){
1013
985
      error(0, errno, "set_cloexec_flag");
1014
 
      close(pipefd[0]);
1015
 
      close(pipefd[1]);
 
986
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
987
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1016
988
      exitstatus = EX_OSERR;
1017
 
      free(direntries[i]);
1018
989
      goto fallback;
1019
990
    }
1020
991
#endif  /* not O_CLOEXEC */
1025
996
    if(ret < 0){
1026
997
      error(0, errno, "sigprocmask");
1027
998
      exitstatus = EX_OSERR;
1028
 
      free(direntries[i]);
1029
999
      goto fallback;
1030
1000
    }
1031
1001
    /* Starting a new process to be watched */
1037
1007
      error(0, errno, "fork");
1038
1008
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1039
1009
                                     &sigchld_action.sa_mask, NULL));
1040
 
      close(pipefd[0]);
1041
 
      close(pipefd[1]);
 
1010
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
1011
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1042
1012
      exitstatus = EX_OSERR;
1043
 
      free(direntries[i]);
1044
1013
      goto fallback;
1045
1014
    }
1046
1015
    if(pid == 0){
1072
1041
      /* no return */
1073
1042
    }
1074
1043
    /* Parent process */
1075
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1076
 
    close(plugin_fd);
 
1044
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1045
                                             pipe */
 
1046
    TEMP_FAILURE_RETRY(close(plugin_fd));
1077
1047
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1078
1048
    if(new_plugin == NULL){
1079
1049
      error(0, errno, "getplugin");
1084
1054
        error(0, errno, "sigprocmask");
1085
1055
      }
1086
1056
      exitstatus = EX_OSERR;
1087
 
      free(direntries[i]);
1088
1057
      goto fallback;
1089
1058
    }
1090
 
    free(direntries[i]);
1091
1059
    
1092
1060
    new_plugin->pid = pid;
1093
1061
    new_plugin->fd = pipefd[0];
1103
1071
      goto fallback;
1104
1072
    }
1105
1073
    
1106
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1074
#if defined (__GNUC__) and defined (__GLIBC__)
 
1075
#if not __GLIBC_PREREQ(2, 16)
 
1076
#pragma GCC diagnostic push
 
1077
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1078
#endif
 
1079
#endif
 
1080
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1081
                                          -Wconversion in GNU libc
 
1082
                                          before 2.16 */
 
1083
#if defined (__GNUC__) and defined (__GLIBC__)
 
1084
#if not __GLIBC_PREREQ(2, 16)
 
1085
#pragma GCC diagnostic pop
 
1086
#endif
 
1087
#endif
1107
1088
    
1108
1089
    if(maxfd < new_plugin->fd){
1109
1090
      maxfd = new_plugin->fd;
1112
1093
  
1113
1094
  free(direntries);
1114
1095
  direntries = NULL;
1115
 
  close(dir_fd);
 
1096
  TEMP_FAILURE_RETRY(close(dir_fd));
1116
1097
  dir_fd = -1;
1117
1098
  free_plugin(getplugin(NULL));
1118
1099
  
1158
1139
                      (intmax_t) (proc->pid),
1159
1140
                      WTERMSIG(proc->status),
1160
1141
                      strsignal(WTERMSIG(proc->status)));
 
1142
            } else if(WCOREDUMP(proc->status)){
 
1143
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1144
                      " core\n", proc->name, (intmax_t) (proc->pid));
1161
1145
            }
1162
1146
          }
1163
1147
          
1164
1148
          /* Remove the plugin */
1165
 
          FD_CLR(proc->fd, &rfds_all);
 
1149
#if defined (__GNUC__) and defined (__GLIBC__)
 
1150
#if not __GLIBC_PREREQ(2, 16)
 
1151
#pragma GCC diagnostic push
 
1152
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1153
#endif
 
1154
#endif
 
1155
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1156
                                          -Wconversion in GNU libc
 
1157
                                          before 2.16 */
 
1158
#if defined (__GNUC__) and defined (__GLIBC__)
 
1159
#if not __GLIBC_PREREQ(2, 16)
 
1160
#pragma GCC diagnostic pop
 
1161
#endif
 
1162
#endif
1166
1163
          
1167
1164
          /* Block signal while modifying process_list */
1168
1165
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1208
1205
      }
1209
1206
      
1210
1207
      /* This process has not completed.  Does it have any output? */
1211
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1208
#if defined (__GNUC__) and defined (__GLIBC__)
 
1209
#if not __GLIBC_PREREQ(2, 16)
 
1210
#pragma GCC diagnostic push
 
1211
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1212
#endif
 
1213
#endif
 
1214
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1215
                                                         warning from
 
1216
                                                         -Wconversion
 
1217
                                                         in GNU libc
 
1218
                                                         before
 
1219
                                                         2.16 */
 
1220
#if defined (__GNUC__) and defined (__GLIBC__)
 
1221
#if not __GLIBC_PREREQ(2, 16)
 
1222
#pragma GCC diagnostic pop
 
1223
#endif
 
1224
#endif
1212
1225
        /* This process had nothing to say at this time */
1213
1226
        proc = proc->next;
1214
1227
        continue;
1284
1297
  free(direntries);
1285
1298
  
1286
1299
  if(dir_fd != -1){
1287
 
    close(dir_fd);
 
1300
    TEMP_FAILURE_RETRY(close(dir_fd));
1288
1301
  }
1289
1302
  
1290
1303
  /* Kill the processes */
1310
1323
  free_plugin_list();
1311
1324
  
1312
1325
  free(plugindir);
1313
 
  free(pluginhelperdir);
1314
1326
  free(argfile);
1315
1327
  
1316
1328
  return exitstatus;