/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-14 22:55:11 UTC
  • Revision ID: teddy@recompile.se-20140614225511-0ge6tce2esulf65t
In plugin-runner, do scandirat() or scandir() instead of readdir().

* plugin-runner.c (set_cloexec_flag): Only define if O_CLOEXEC is not
                                      defined, it is not called
                                      otherwise.
  (main): Remove variables "dir" and "dirst"; new variable "dir_fd".
          Use scandirat() if available, scandir() otherwise, both
          using new filter function "good_name".  Removed checking of
          plugin file name patterns; this is now done by "good_name".
          Do not set FD_CLOEXEC flag on plugin_fd, since fexecve()
          will not work with it.  Use pipe2() to set O_CLOEXEC on pipe
          FD's (if possible).
  (main/good_name): New filter function for scandirat() or scandir().

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
#define BUFFER_SIZE 256
77
77
 
78
78
#define PDIR "/lib/mandos/plugins.d"
79
 
#define PHDIR "/lib/mandos/plugin-helpers"
80
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
81
80
 
82
81
const char *argp_program_version = "plugin-runner " VERSION;
348
347
 
349
348
int main(int argc, char *argv[]){
350
349
  char *plugindir = NULL;
351
 
  char *pluginhelperdir = NULL;
352
350
  char *argfile = NULL;
353
351
  FILE *conffp;
354
 
  struct dirent **direntries = NULL;
 
352
  struct dirent **direntries;
355
353
  struct stat st;
356
354
  fd_set rfds_all;
357
355
  int ret, maxfd = 0;
416
414
      .doc = "Group ID the plugins will run as", .group = 3 },
417
415
    { .name = "debug", .key = 132,
418
416
      .doc = "Debug mode", .group = 4 },
419
 
    { .name = "plugin-helper-dir", .key = 133,
420
 
      .arg = "DIRECTORY",
421
 
      .doc = "Specify a different plugin helper directory",
422
 
      .group = 2 },
423
417
    /*
424
418
     * These reproduce what we would get without ARGP_NO_HELP
425
419
     */
551
545
    case 132:                   /* --debug */
552
546
      debug = true;
553
547
      break;
554
 
    case 133:                   /* --plugin-helper-dir */
555
 
      free(pluginhelperdir);
556
 
      pluginhelperdir = strdup(arg);
557
 
      if(pluginhelperdir != NULL){
558
 
        errno = 0;
559
 
      }
560
 
      break;
561
548
      /*
562
549
       * These reproduce what we would get without ARGP_NO_HELP
563
550
       */
614
601
    case 130:                   /* --userid */
615
602
    case 131:                   /* --groupid */
616
603
    case 132:                   /* --debug */
617
 
    case 133:                   /* --plugin-helper-dir */
618
604
    case '?':                   /* --help */
619
605
    case -3:                    /* --usage */
620
606
    case 'V':                   /* --version */
775
761
    goto fallback;
776
762
  }
777
763
  
778
 
  {
779
 
    char *pluginhelperenv;
780
 
    bool bret = true;
781
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
782
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
783
 
    if(ret != -1){
784
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
785
 
    }
786
 
    if(ret == -1 or not bret){
787
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
788
 
            " environment variable to \"%s\" for all plugins\n",
789
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
790
 
    }
791
 
    if(ret != -1){
792
 
      free(pluginhelperenv);
793
 
    }
794
 
  }
795
 
  
796
764
  if(debug){
797
765
    for(plugin *p = plugin_list; p != NULL; p=p->next){
798
766
      fprintf(stderr, "Plugin: %s has %d arguments\n",
827
795
          }
828
796
        }
829
797
      }
830
 
      close(plugindir_fd);
 
798
      TEMP_FAILURE_RETRY(close(plugindir_fd));
831
799
    }
832
800
  }
833
801
  
861
829
    ret = set_cloexec_flag(dir_fd);
862
830
    if(ret < 0){
863
831
      error(0, errno, "set_cloexec_flag");
 
832
      TEMP_FAILURE_RETRY(close(dir_fd));
864
833
      exitstatus = EX_OSERR;
865
834
      goto fallback;
866
835
    }
906
875
#endif  /* not __GLIBC__ */
907
876
  if(numplugins == -1){
908
877
    error(0, errno, "Could not scan plugin dir");
909
 
    direntries = NULL;
 
878
    TEMP_FAILURE_RETRY(close(dir_fd));
910
879
    exitstatus = EX_OSERR;
911
880
    goto fallback;
912
881
  }
919
888
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
920
889
    if(plugin_fd == -1){
921
890
      error(0, errno, "Could not open plugin");
922
 
      free(direntries[i]);
923
891
      continue;
924
892
    }
925
893
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
926
894
    if(ret == -1){
927
895
      error(0, errno, "stat");
928
 
      close(plugin_fd);
929
 
      free(direntries[i]);
 
896
      TEMP_FAILURE_RETRY(close(plugin_fd));
930
897
      continue;
931
898
    }
932
899
    
940
907
                plugindir != NULL ? plugindir : PDIR,
941
908
                direntries[i]->d_name);
942
909
      }
943
 
      close(plugin_fd);
944
 
      free(direntries[i]);
 
910
      TEMP_FAILURE_RETRY(close(plugin_fd));
945
911
      continue;
946
912
    }
947
913
    
948
914
    plugin *p = getplugin(direntries[i]->d_name);
949
915
    if(p == NULL){
950
916
      error(0, errno, "getplugin");
951
 
      close(plugin_fd);
952
 
      free(direntries[i]);
 
917
      TEMP_FAILURE_RETRY(close(plugin_fd));
953
918
      continue;
954
919
    }
955
920
    if(p->disabled){
957
922
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
958
923
                direntries[i]->d_name);
959
924
      }
960
 
      close(plugin_fd);
961
 
      free(direntries[i]);
 
925
      TEMP_FAILURE_RETRY(close(plugin_fd));
962
926
      continue;
963
927
    }
964
928
    {
997
961
    if(ret == -1){
998
962
      error(0, errno, "pipe");
999
963
      exitstatus = EX_OSERR;
1000
 
      free(direntries[i]);
1001
964
      goto fallback;
1002
965
    }
1003
966
    if(pipefd[0] >= FD_SETSIZE){
1004
967
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
1005
968
              FD_SETSIZE);
1006
 
      close(pipefd[0]);
1007
 
      close(pipefd[1]);
 
969
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
970
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1008
971
      exitstatus = EX_OSERR;
1009
 
      free(direntries[i]);
1010
972
      goto fallback;
1011
973
    }
1012
974
#ifndef O_CLOEXEC
1014
976
    ret = set_cloexec_flag(pipefd[0]);
1015
977
    if(ret < 0){
1016
978
      error(0, errno, "set_cloexec_flag");
1017
 
      close(pipefd[0]);
1018
 
      close(pipefd[1]);
 
979
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
980
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1019
981
      exitstatus = EX_OSERR;
1020
 
      free(direntries[i]);
1021
982
      goto fallback;
1022
983
    }
1023
984
    ret = set_cloexec_flag(pipefd[1]);
1024
985
    if(ret < 0){
1025
986
      error(0, errno, "set_cloexec_flag");
1026
 
      close(pipefd[0]);
1027
 
      close(pipefd[1]);
 
987
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
988
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1028
989
      exitstatus = EX_OSERR;
1029
 
      free(direntries[i]);
1030
990
      goto fallback;
1031
991
    }
1032
992
#endif  /* not O_CLOEXEC */
1037
997
    if(ret < 0){
1038
998
      error(0, errno, "sigprocmask");
1039
999
      exitstatus = EX_OSERR;
1040
 
      free(direntries[i]);
1041
1000
      goto fallback;
1042
1001
    }
1043
1002
    /* Starting a new process to be watched */
1049
1008
      error(0, errno, "fork");
1050
1009
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1051
1010
                                     &sigchld_action.sa_mask, NULL));
1052
 
      close(pipefd[0]);
1053
 
      close(pipefd[1]);
 
1011
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
1012
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1054
1013
      exitstatus = EX_OSERR;
1055
 
      free(direntries[i]);
1056
1014
      goto fallback;
1057
1015
    }
1058
1016
    if(pid == 0){
1084
1042
      /* no return */
1085
1043
    }
1086
1044
    /* Parent process */
1087
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1088
 
    close(plugin_fd);
 
1045
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1046
                                             pipe */
 
1047
    TEMP_FAILURE_RETRY(close(plugin_fd));
1089
1048
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1090
1049
    if(new_plugin == NULL){
1091
1050
      error(0, errno, "getplugin");
1096
1055
        error(0, errno, "sigprocmask");
1097
1056
      }
1098
1057
      exitstatus = EX_OSERR;
1099
 
      free(direntries[i]);
1100
1058
      goto fallback;
1101
1059
    }
1102
 
    free(direntries[i]);
1103
1060
    
1104
1061
    new_plugin->pid = pid;
1105
1062
    new_plugin->fd = pipefd[0];
1135
1092
    }
1136
1093
  }
1137
1094
  
1138
 
  free(direntries);
1139
 
  direntries = NULL;
1140
 
  close(dir_fd);
1141
 
  dir_fd = -1;
 
1095
  TEMP_FAILURE_RETRY(close(dir_fd));
1142
1096
  free_plugin(getplugin(NULL));
1143
1097
  
1144
1098
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1338
1292
    free(custom_argv);
1339
1293
  }
1340
1294
  
1341
 
  free(direntries);
1342
 
  
1343
1295
  if(dir_fd != -1){
1344
 
    close(dir_fd);
 
1296
    TEMP_FAILURE_RETRY(close(dir_fd));
1345
1297
  }
1346
1298
  
1347
1299
  /* Kill the processes */
1367
1319
  free_plugin_list();
1368
1320
  
1369
1321
  free(plugindir);
1370
 
  free(pluginhelperdir);
1371
1322
  free(argfile);
1372
1323
  
1373
1324
  return exitstatus;