/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: 2016-11-26 23:21:16 UTC
  • Revision ID: teddy@recompile.se-20161126232116-nhqk9x1qij09b7cx
Add comment in documentation source with clarifying text.

* intro.xml (INTRODUCTION): Add comment with clarification.

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