/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-03-23 07:11:22 UTC
  • Revision ID: teddy@recompile.se-20160323071122-55srtjdizr21rr7f
Use HTTPS in home page links

Since we have a real certificate now, change all links to the home
page to use HTTPS.

* README: Use HTTPS in home page links
* debian/control (Source: mandos/Homepage): - '' -
* debian/copyright (Source): - '' -
* intro.xml (SEE ALSO): - '' -
* mandos.lsm (Primary-site): - '' -

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
353
  struct dirent **direntries = NULL;
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",
795
825
          }
796
826
        }
797
827
      }
798
 
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
828
      close(plugindir_fd);
799
829
    }
800
830
  }
801
831
  
860
890
    return 1;
861
891
  }
862
892
  
863
 
#ifdef __GLIBC__
864
 
#if __GLIBC_PREREQ(2, 15)
865
893
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
866
894
                             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__ */
875
895
  if(numplugins == -1){
876
896
    error(0, errno, "Could not scan plugin dir");
877
897
    direntries = NULL;
887
907
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
888
908
    if(plugin_fd == -1){
889
909
      error(0, errno, "Could not open plugin");
 
910
      free(direntries[i]);
890
911
      continue;
891
912
    }
892
913
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
893
914
    if(ret == -1){
894
915
      error(0, errno, "stat");
895
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
916
      close(plugin_fd);
 
917
      free(direntries[i]);
896
918
      continue;
897
919
    }
898
920
    
906
928
                plugindir != NULL ? plugindir : PDIR,
907
929
                direntries[i]->d_name);
908
930
      }
909
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
931
      close(plugin_fd);
 
932
      free(direntries[i]);
910
933
      continue;
911
934
    }
912
935
    
913
936
    plugin *p = getplugin(direntries[i]->d_name);
914
937
    if(p == NULL){
915
938
      error(0, errno, "getplugin");
916
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
939
      close(plugin_fd);
 
940
      free(direntries[i]);
917
941
      continue;
918
942
    }
919
943
    if(p->disabled){
921
945
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
922
946
                direntries[i]->d_name);
923
947
      }
924
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
948
      close(plugin_fd);
 
949
      free(direntries[i]);
925
950
      continue;
926
951
    }
927
952
    {
960
985
    if(ret == -1){
961
986
      error(0, errno, "pipe");
962
987
      exitstatus = EX_OSERR;
 
988
      free(direntries[i]);
963
989
      goto fallback;
964
990
    }
965
991
    if(pipefd[0] >= FD_SETSIZE){
966
992
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
967
993
              FD_SETSIZE);
968
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
969
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
994
      close(pipefd[0]);
 
995
      close(pipefd[1]);
970
996
      exitstatus = EX_OSERR;
 
997
      free(direntries[i]);
971
998
      goto fallback;
972
999
    }
973
1000
#ifndef O_CLOEXEC
975
1002
    ret = set_cloexec_flag(pipefd[0]);
976
1003
    if(ret < 0){
977
1004
      error(0, errno, "set_cloexec_flag");
978
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
979
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1005
      close(pipefd[0]);
 
1006
      close(pipefd[1]);
980
1007
      exitstatus = EX_OSERR;
 
1008
      free(direntries[i]);
981
1009
      goto fallback;
982
1010
    }
983
1011
    ret = set_cloexec_flag(pipefd[1]);
984
1012
    if(ret < 0){
985
1013
      error(0, errno, "set_cloexec_flag");
986
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
987
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1014
      close(pipefd[0]);
 
1015
      close(pipefd[1]);
988
1016
      exitstatus = EX_OSERR;
 
1017
      free(direntries[i]);
989
1018
      goto fallback;
990
1019
    }
991
1020
#endif  /* not O_CLOEXEC */
996
1025
    if(ret < 0){
997
1026
      error(0, errno, "sigprocmask");
998
1027
      exitstatus = EX_OSERR;
 
1028
      free(direntries[i]);
999
1029
      goto fallback;
1000
1030
    }
1001
1031
    /* Starting a new process to be watched */
1007
1037
      error(0, errno, "fork");
1008
1038
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1009
1039
                                     &sigchld_action.sa_mask, NULL));
1010
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1011
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1040
      close(pipefd[0]);
 
1041
      close(pipefd[1]);
1012
1042
      exitstatus = EX_OSERR;
 
1043
      free(direntries[i]);
1013
1044
      goto fallback;
1014
1045
    }
1015
1046
    if(pid == 0){
1041
1072
      /* no return */
1042
1073
    }
1043
1074
    /* Parent process */
1044
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1045
 
                                             pipe */
1046
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
 
1075
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1076
    close(plugin_fd);
1047
1077
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1048
1078
    if(new_plugin == NULL){
1049
1079
      error(0, errno, "getplugin");
1054
1084
        error(0, errno, "sigprocmask");
1055
1085
      }
1056
1086
      exitstatus = EX_OSERR;
 
1087
      free(direntries[i]);
1057
1088
      goto fallback;
1058
1089
    }
 
1090
    free(direntries[i]);
1059
1091
    
1060
1092
    new_plugin->pid = pid;
1061
1093
    new_plugin->fd = pipefd[0];
1071
1103
      goto fallback;
1072
1104
    }
1073
1105
    
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
 
1106
    FD_SET(new_plugin->fd, &rfds_all);
1088
1107
    
1089
1108
    if(maxfd < new_plugin->fd){
1090
1109
      maxfd = new_plugin->fd;
1093
1112
  
1094
1113
  free(direntries);
1095
1114
  direntries = NULL;
1096
 
  TEMP_FAILURE_RETRY(close(dir_fd));
 
1115
  close(dir_fd);
1097
1116
  dir_fd = -1;
1098
1117
  free_plugin(getplugin(NULL));
1099
1118
  
1139
1158
                      (intmax_t) (proc->pid),
1140
1159
                      WTERMSIG(proc->status),
1141
1160
                      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));
1145
1161
            }
1146
1162
          }
1147
1163
          
1148
1164
          /* Remove the plugin */
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
 
1165
          FD_CLR(proc->fd, &rfds_all);
1163
1166
          
1164
1167
          /* Block signal while modifying process_list */
1165
1168
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1205
1208
      }
1206
1209
      
1207
1210
      /* This process has not completed.  Does it have any output? */
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
 
1211
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1225
1212
        /* This process had nothing to say at this time */
1226
1213
        proc = proc->next;
1227
1214
        continue;
1297
1284
  free(direntries);
1298
1285
  
1299
1286
  if(dir_fd != -1){
1300
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1287
    close(dir_fd);
1301
1288
  }
1302
1289
  
1303
1290
  /* Kill the processes */
1323
1310
  free_plugin_list();
1324
1311
  
1325
1312
  free(plugindir);
 
1313
  free(pluginhelperdir);
1326
1314
  free(argfile);
1327
1315
  
1328
1316
  return exitstatus;