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;
78 // Allowed paths here are absolute, and may contain _one_ *,
79 // which matches any string
80 // FG_SCENERY is deliberately not allowed, as it would make
81 // /sim/terrasync/scenery-dir a security hole
82 void fgInitAllowedPaths()
84 read_allowed_paths.clear();
85 write_allowed_paths.clear();
86 read_allowed_paths.push_back(globals->get_fg_root() + "/*");
87 read_allowed_paths.push_back(globals->get_fg_home() + "/*");
88 string_list const aircraft_paths = globals->get_aircraft_paths();
89 for( string_list::const_iterator it = aircraft_paths.begin();
90 it != aircraft_paths.end();
93 read_allowed_paths.push_back(*it + "/*");
96 for( string_list::const_iterator it = read_allowed_paths.begin();
97 it != read_allowed_paths.end();
99 { // if we get the initialization order wrong, better to have an
100 // obvious error than a can-read-everything security hole...
101 if (!(it->compare("/*"))){
102 flightgear::fatalMessageBox("Nasal initialization error",
103 "Empty string in FG_ROOT, FG_HOME or FG_AIRCRAFT",
104 "or fgInitAllowedPaths() called too early");
108 write_allowed_paths.push_back("/tmp/*.xml");
109 write_allowed_paths.push_back(globals->get_fg_home() + "/*.sav");
110 write_allowed_paths.push_back(globals->get_fg_home() + "/*.log");
111 write_allowed_paths.push_back(globals->get_fg_home() + "/cache/*");
112 write_allowed_paths.push_back(globals->get_fg_home() + "/Export/*");
113 write_allowed_paths.push_back(globals->get_fg_home() + "/state/*.xml");
114 write_allowed_paths.push_back(globals->get_fg_home() + "/aircraft-data/*.xml");
115 write_allowed_paths.push_back(globals->get_fg_home() + "/Wildfire/*.xml");
116 write_allowed_paths.push_back(globals->get_fg_home() + "/runtime-jetways/*.xml");
117 write_allowed_paths.push_back(globals->get_fg_home() + "/Input/Joysticks/*.xml");
119 if(!fgValidatePath(globals->get_fg_home() + "/../no.log",true).empty() ||
120 !fgValidatePath(globals->get_fg_home() + "/no.lot",true).empty() ||
121 fgValidatePath((globals->get_fg_home() + "/nolog").c_str(),true) ||
122 !fgValidatePath(globals->get_fg_home() + "no.log",true).empty() ||
123 !fgValidatePath("..\\" + globals->get_fg_home() + "/no.log",false).empty() ||
124 fgValidatePath("/tmp/no.xml",false) ||
125 fgValidatePath(globals->get_fg_home() + "/./ff/../Export\\yes..gg",true).empty() ||
126 !fgValidatePath((globals->get_fg_home() + "/aircraft-data/yes..xml").c_str(),true) ||
127 fgValidatePath(globals->get_fg_root() + "/./\\yes.bmp",false).empty()) {
128 flightgear::fatalMessageBox("Nasal initialization error",
129 "fgInitAllowedPaths() does not work",
136 // Unlike SGPath::realpath, does not require that the file already exists,
137 // but does require that it be below the starting point
138 static std::string fgNormalizePath (const std::string& path)
140 string_list path_parts;
142 std::string normed_path = "", this_part = "";
144 for (int pos = 0; ; pos++) {
146 if (c == '\\') { c = '/'; }
147 if ((c == '/') || (c == 0)) {
148 if ((this_part == "/..") || (this_part == "..")) {
149 if (path_parts.empty()) { return ""; }
150 path_parts.pop_back();
151 } else if ((this_part != "/.") && (this_part != "/")) {
152 path_parts.push_back(this_part);
156 if (c == 0) { break; }
157 this_part = this_part + c;
159 for( string_list::const_iterator it = path_parts.begin();
160 it != path_parts.end();
163 normed_path.append(*it);
169 // Check whether Nasal is allowed to access a path
170 std::string fgValidatePath (const std::string& path, bool write)
172 const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
175 // Normalize the path (prevents ../../.. trickery)
176 std::string normed_path = fgNormalizePath(path);
178 // Check against each allowed pattern
179 for( string_list::const_iterator it = allowed_paths.begin();
180 it != allowed_paths.end();
183 star_pos = it->find('*');
184 if (star_pos == std::string::npos) {
185 if (!(it->compare(normed_path))) {
189 if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
190 && !(it->substr(0,star_pos)
191 .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
192 && !(it->substr(star_pos+1,it->size()-star_pos-1)
193 .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
194 it->size()-star_pos-1))) /* after-star parts match */) {
202 // s.c_str() becomes invalid when s is destroyed, so need a static s
203 std::string validate_path_temp;
204 const char* fgValidatePath(const char* path, bool write)
206 validate_path_temp = fgValidatePath(std::string(path), write);
207 if(validate_path_temp.empty()){
210 return validate_path_temp.c_str();