/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-07-25 22:44:20 UTC
  • mto: This revision was merged to the branch mainline in revision 724.
  • Revision ID: teddy@recompile.se-20140725224420-4a5ct2ptt0hsc92z
Require Python 2.7.

This is in preparation for the eventual move to Python 3, which will
happen as soon as all Python modules required by Mandos are available.
The mandos-ctl and mandos-monitor programs are already portable
between Python 2.6 and Python 3 without changes; this change will
bring the requirement up to Python 2.7.

* INSTALL (Prerequisites/Libraries/Mandos Server): Document
                                                   requirement of
                                                   Python 2.7; remove
                                                   Python-argparse
                                                   which is in the
                                                   Python 2.7 standard
                                                   library.
* debian/control (Source: mandos/Build-Depends-Indep): Depend on
                                                       exactly the
                                                       python2.7
                                                       package and all
                                                       the Python 2.7
                                                       versions of the
                                                       python modules.
  (Package: mandos/Depends): - '' - but still depend on python (<=2.7)
                            and the generic versions of the Python
                            modules; this is for mandos-ctl and
                            mandos-monitor, both of which are
                            compatible with Python 3, and use
                            #!/usr/bin/python.
* mandos: Use #!/usr/bin/python2.7 instead of #!/usr/bin/python.

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-2016 Teddy Hogeborn
6
 
 * Copyright © 2008-2016 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
765
    for(plugin *p = plugin_list; p != NULL; p=p->next){
796
766
      fprintf(stderr, "Plugin: %s has %d arguments\n",
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;
913
893
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
914
894
    if(ret == -1){
915
895
      error(0, errno, "stat");
916
 
      close(plugin_fd);
 
896
      TEMP_FAILURE_RETRY(close(plugin_fd));
917
897
      free(direntries[i]);
918
898
      continue;
919
899
    }
928
908
                plugindir != NULL ? plugindir : PDIR,
929
909
                direntries[i]->d_name);
930
910
      }
931
 
      close(plugin_fd);
 
911
      TEMP_FAILURE_RETRY(close(plugin_fd));
932
912
      free(direntries[i]);
933
913
      continue;
934
914
    }
936
916
    plugin *p = getplugin(direntries[i]->d_name);
937
917
    if(p == NULL){
938
918
      error(0, errno, "getplugin");
939
 
      close(plugin_fd);
 
919
      TEMP_FAILURE_RETRY(close(plugin_fd));
940
920
      free(direntries[i]);
941
921
      continue;
942
922
    }
945
925
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
946
926
                direntries[i]->d_name);
947
927
      }
948
 
      close(plugin_fd);
 
928
      TEMP_FAILURE_RETRY(close(plugin_fd));
949
929
      free(direntries[i]);
950
930
      continue;
951
931
    }
991
971
    if(pipefd[0] >= FD_SETSIZE){
992
972
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
993
973
              FD_SETSIZE);
994
 
      close(pipefd[0]);
995
 
      close(pipefd[1]);
 
974
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
975
      TEMP_FAILURE_RETRY(close(pipefd[1]));
996
976
      exitstatus = EX_OSERR;
997
977
      free(direntries[i]);
998
978
      goto fallback;
1002
982
    ret = set_cloexec_flag(pipefd[0]);
1003
983
    if(ret < 0){
1004
984
      error(0, errno, "set_cloexec_flag");
1005
 
      close(pipefd[0]);
1006
 
      close(pipefd[1]);
 
985
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
986
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1007
987
      exitstatus = EX_OSERR;
1008
988
      free(direntries[i]);
1009
989
      goto fallback;
1011
991
    ret = set_cloexec_flag(pipefd[1]);
1012
992
    if(ret < 0){
1013
993
      error(0, errno, "set_cloexec_flag");
1014
 
      close(pipefd[0]);
1015
 
      close(pipefd[1]);
 
994
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
995
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1016
996
      exitstatus = EX_OSERR;
1017
997
      free(direntries[i]);
1018
998
      goto fallback;
1037
1017
      error(0, errno, "fork");
1038
1018
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1039
1019
                                     &sigchld_action.sa_mask, NULL));
1040
 
      close(pipefd[0]);
1041
 
      close(pipefd[1]);
 
1020
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
1021
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1042
1022
      exitstatus = EX_OSERR;
1043
1023
      free(direntries[i]);
1044
1024
      goto fallback;
1072
1052
      /* no return */
1073
1053
    }
1074
1054
    /* Parent process */
1075
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1076
 
    close(plugin_fd);
 
1055
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1056
                                             pipe */
 
1057
    TEMP_FAILURE_RETRY(close(plugin_fd));
1077
1058
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1078
1059
    if(new_plugin == NULL){
1079
1060
      error(0, errno, "getplugin");
1103
1084
      goto fallback;
1104
1085
    }
1105
1086
    
1106
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1087
#if defined (__GNUC__) and defined (__GLIBC__)
 
1088
#if not __GLIBC_PREREQ(2, 16)
 
1089
#pragma GCC diagnostic push
 
1090
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1091
#endif
 
1092
#endif
 
1093
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1094
                                          -Wconversion in GNU libc
 
1095
                                          before 2.16 */
 
1096
#if defined (__GNUC__) and defined (__GLIBC__)
 
1097
#if not __GLIBC_PREREQ(2, 16)
 
1098
#pragma GCC diagnostic pop
 
1099
#endif
 
1100
#endif
1107
1101
    
1108
1102
    if(maxfd < new_plugin->fd){
1109
1103
      maxfd = new_plugin->fd;
1112
1106
  
1113
1107
  free(direntries);
1114
1108
  direntries = NULL;
1115
 
  close(dir_fd);
 
1109
  TEMP_FAILURE_RETRY(close(dir_fd));
1116
1110
  dir_fd = -1;
1117
1111
  free_plugin(getplugin(NULL));
1118
1112
  
1158
1152
                      (intmax_t) (proc->pid),
1159
1153
                      WTERMSIG(proc->status),
1160
1154
                      strsignal(WTERMSIG(proc->status)));
 
1155
            } else if(WCOREDUMP(proc->status)){
 
1156
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1157
                      " core\n", proc->name, (intmax_t) (proc->pid));
1161
1158
            }
1162
1159
          }
1163
1160
          
1164
1161
          /* Remove the plugin */
1165
 
          FD_CLR(proc->fd, &rfds_all);
 
1162
#if defined (__GNUC__) and defined (__GLIBC__)
 
1163
#if not __GLIBC_PREREQ(2, 16)
 
1164
#pragma GCC diagnostic push
 
1165
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1166
#endif
 
1167
#endif
 
1168
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1169
                                          -Wconversion in GNU libc
 
1170
                                          before 2.16 */
 
1171
#if defined (__GNUC__) and defined (__GLIBC__)
 
1172
#if not __GLIBC_PREREQ(2, 16)
 
1173
#pragma GCC diagnostic pop
 
1174
#endif
 
1175
#endif
1166
1176
          
1167
1177
          /* Block signal while modifying process_list */
1168
1178
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1208
1218
      }
1209
1219
      
1210
1220
      /* This process has not completed.  Does it have any output? */
1211
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1221
#if defined (__GNUC__) and defined (__GLIBC__)
 
1222
#if not __GLIBC_PREREQ(2, 16)
 
1223
#pragma GCC diagnostic push
 
1224
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1225
#endif
 
1226
#endif
 
1227
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1228
                                                         warning from
 
1229
                                                         -Wconversion
 
1230
                                                         in GNU libc
 
1231
                                                         before
 
1232
                                                         2.16 */
 
1233
#if defined (__GNUC__) and defined (__GLIBC__)
 
1234
#if not __GLIBC_PREREQ(2, 16)
 
1235
#pragma GCC diagnostic pop
 
1236
#endif
 
1237
#endif
1212
1238
        /* This process had nothing to say at this time */
1213
1239
        proc = proc->next;
1214
1240
        continue;
1284
1310
  free(direntries);
1285
1311
  
1286
1312
  if(dir_fd != -1){
1287
 
    close(dir_fd);
 
1313
    TEMP_FAILURE_RETRY(close(dir_fd));
1288
1314
  }
1289
1315
  
1290
1316
  /* Kill the processes */
1310
1336
  free_plugin_list();
1311
1337
  
1312
1338
  free(plugindir);
1313
 
  free(pluginhelperdir);
1314
1339
  free(argfile);
1315
1340
  
1316
1341
  return exitstatus;