1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/SGLimits.hxx>
34 #include <simgear/math/SGMisc.hxx>
36 #include <GUI/MessageBox.hxx>
38 #include "fg_props.hxx"
39 #include "globals.hxx"
42 #ifdef OSG_LIBRARY_STATIC
43 #include "osgDB/Registry"
48 // Originally written by Alex Perry.
50 fgGetLowPass (double current, double target, double timeratio)
52 if ( timeratio < 0.0 ) {
53 if ( timeratio < -1.0 ) {
54 // time went backwards; kill the filter
57 // ignore mildly negative time
59 } else if ( timeratio < 0.2 ) {
60 // Normal mode of operation; fast
61 // approximation to exp(-timeratio)
62 current = current * (1.0 - timeratio) + target * timeratio;
63 } else if ( timeratio > 5.0 ) {
64 // Huge time step; assume filter has settled
67 // Moderate time step; non linear response
68 double keep = exp(-timeratio);
69 current = current * keep + target * (1.0 - keep);
75 static string_list read_allowed_paths;
76 static string_list write_allowed_paths;
79 * Allowed paths here are absolute, and may contain _one_ *,
80 * which matches any string
81 * FG_SCENERY is deliberately not allowed, as it would make
82 * /sim/terrasync/scenery-dir a security hole
84 void fgInitAllowedPaths()
86 if(SGPath("ygjmyfvhhnvdoesnotexist").realpath() == "ygjmyfvhhnvdoesnotexist"){
87 // Forbid using this version of fgValidatePath() with older
88 // (not normalizing non-existent files) versions of realpath(),
89 // as that would be a security hole
90 flightgear::fatalMessageBox("Nasal initialization error",
91 "Version mismatch - please update simgear",
95 read_allowed_paths.clear();
96 write_allowed_paths.clear();
97 std::string fg_root = SGPath(globals->get_fg_root()).realpath();
98 std::string fg_home = SGPath(globals->get_fg_home()).realpath();
99 #if defined(_MSC_VER) /*for MS compilers */ || defined(_WIN32) /*needed for non MS windows compilers like MingW*/
100 std::string sep = "\\";
102 std::string sep = "/";
104 read_allowed_paths.push_back(fg_root + sep + "*");
105 read_allowed_paths.push_back(fg_home + sep + "*");
106 string_list const aircraft_paths = globals->get_aircraft_paths();
107 for( string_list::const_iterator it = aircraft_paths.begin();
108 it != aircraft_paths.end();
111 // if we get the initialization order wrong, better to have an
112 // obvious error than a can-read-everything security hole...
113 if (it->empty() || fg_root.empty() || fg_home.empty()){
114 flightgear::fatalMessageBox("Nasal initialization error",
115 "Empty string in FG_ROOT, FG_HOME or FG_AIRCRAFT",
116 "or fgInitAllowedPaths() called too early");
119 read_allowed_paths.push_back(SGPath(*it).realpath() + sep + "*");
122 write_allowed_paths.push_back(fg_home + sep + "*.sav");
123 write_allowed_paths.push_back(fg_home + sep + "*.log");
124 write_allowed_paths.push_back(fg_home + sep + "cache" + sep + "*");
125 write_allowed_paths.push_back(fg_home + sep + "Export" + sep + "*");
126 write_allowed_paths.push_back(fg_home + sep + "state" + sep + "*.xml");
127 write_allowed_paths.push_back(fg_home + sep + "aircraft-data" + sep + "*.xml");
128 write_allowed_paths.push_back(fg_home + sep + "Wildfire" + sep + "*.xml");
129 write_allowed_paths.push_back(fg_home + sep + "runtime-jetways" + sep + "*.xml");
130 write_allowed_paths.push_back(fg_home + sep + "Input" + sep + "Joysticks" + sep + "*.xml");
132 // Check that it works
133 if(!fgValidatePath(globals->get_fg_home() + "/../no.log",true).empty() ||
134 !fgValidatePath(globals->get_fg_home() + "/no.logt",true).empty() ||
135 !fgValidatePath(globals->get_fg_home() + "/nolog",true).empty() ||
136 !fgValidatePath(globals->get_fg_home() + "no.log",true).empty() ||
137 !fgValidatePath(globals->get_fg_home() + "\\..\\no.log",false).empty() ||
138 fgValidatePath(globals->get_fg_home() + "/aircraft-data/yes..xml",true).empty() ||
139 fgValidatePath(globals->get_fg_root() + "/.\\yes.bmp",false).empty()) {
140 flightgear::fatalMessageBox("Nasal initialization error",
141 "fgInitAllowedPaths() does not work",
148 * Check whether Nasal is allowed to access a path
149 * Warning: because this always (not just on Windows) treats both \ and /
150 * as path separators, and accepts relative paths (check-to-use race if
151 * the current directory changes),
152 * always use the returned path not the original one
154 std::string fgValidatePath (const std::string& path, bool write)
156 // Normalize the path (prevents ../../.. or symlink trickery)
157 std::string normed_path = SGPath(path).realpath();
159 const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
162 // Check against each allowed pattern
163 for( string_list::const_iterator it = allowed_paths.begin();
164 it != allowed_paths.end();
167 star_pos = it->find('*');
168 if (star_pos == std::string::npos) {
169 if (!(it->compare(normed_path))) {
173 if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
174 && !(it->substr(0,star_pos)
175 .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
176 && !(it->substr(star_pos+1,it->size()-star_pos-1)
177 .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
178 it->size()-star_pos-1))) /* after-star parts match */) {
186 std::string fgValidatePath(const SGPath& path, bool write) { return fgValidatePath(path.str(),write); }