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);
76 // Unlike SGPath::realpath, does not require that the file already exists,
77 // but does require that it be below the starting point
78 static std::string fgNormalizePath (const std::string& path)
80 string_list path_parts;
82 std::string normed_path = "", this_part = "";
84 for (int pos = 0; ; pos++) {
86 if (c == '\\') { c = '/'; }
87 if ((c == '/') || (c == 0)) {
88 if ((this_part == "/..") || (this_part == "..")) {
89 if (path_parts.empty()) { return ""; }
90 path_parts.pop_back();
91 } else if ((this_part != "/.") && (this_part != "/")) {
92 path_parts.push_back(this_part);
96 if (c == 0) { break; }
97 this_part = this_part + c;
99 for( string_list::const_iterator it = path_parts.begin();
100 it != path_parts.end();
103 normed_path.append(*it);
108 static string_list read_allowed_paths;
109 static string_list write_allowed_paths;
111 // Allowed paths here are absolute, and may contain _one_ *,
112 // which matches any string
113 // FG_SCENERY is deliberately not allowed, as it would make
114 // /sim/terrasync/scenery-dir a security hole
115 void fgInitAllowedPaths()
117 read_allowed_paths.clear();
118 write_allowed_paths.clear();
119 std::string fg_root = fgNormalizePath(globals->get_fg_root());
120 std::string fg_home = fgNormalizePath(globals->get_fg_home());
121 read_allowed_paths.push_back(fg_root + "/*");
122 read_allowed_paths.push_back(fg_home + "/*");
123 string_list const aircraft_paths = globals->get_aircraft_paths();
124 for( string_list::const_iterator it = aircraft_paths.begin();
125 it != aircraft_paths.end();
128 read_allowed_paths.push_back(fgNormalizePath(*it) + "/*");
131 for( string_list::const_iterator it = read_allowed_paths.begin();
132 it != read_allowed_paths.end();
134 { // if we get the initialization order wrong, better to have an
135 // obvious error than a can-read-everything security hole...
136 if (!(it->compare("/*"))){
137 flightgear::fatalMessageBox("Nasal initialization error",
138 "Empty string in FG_ROOT, FG_HOME or FG_AIRCRAFT",
139 "or fgInitAllowedPaths() called too early");
143 write_allowed_paths.push_back(fg_home + "/*.sav");
144 write_allowed_paths.push_back(fg_home + "/*.log");
145 write_allowed_paths.push_back(fg_home + "/cache/*");
146 write_allowed_paths.push_back(fg_home + "/Export/*");
147 write_allowed_paths.push_back(fg_home + "/state/*.xml");
148 write_allowed_paths.push_back(fg_home + "/aircraft-data/*.xml");
149 write_allowed_paths.push_back(fg_home + "/Wildfire/*.xml");
150 write_allowed_paths.push_back(fg_home + "/runtime-jetways/*.xml");
151 write_allowed_paths.push_back(fg_home + "/Input/Joysticks/*.xml");
153 // Check that it works
154 if(!fgValidatePath(globals->get_fg_home() + "/../no.log",true).empty() ||
155 !fgValidatePath(globals->get_fg_home() + "/no.logt",true).empty() ||
156 !fgValidatePath(globals->get_fg_home() + "/nolog",true).empty() ||
157 !fgValidatePath(globals->get_fg_home() + "no.log",true).empty() ||
158 !fgValidatePath(globals->get_fg_home() + "\\..\\no.log",false).empty() ||
159 fgValidatePath(globals->get_fg_home() + "/./TerraSync/../Export\\yes..gg",true).empty() ||
160 fgValidatePath(globals->get_fg_home() + "/aircraft-data/yes..xml",true).empty() ||
161 fgValidatePath(globals->get_fg_root() + "/.\\yes.bmp",false).empty()) {
162 flightgear::fatalMessageBox("Nasal initialization error",
163 "fgInitAllowedPaths() does not work",
169 // Check whether Nasal is allowed to access a path (assumed already normalized)
170 static std::string fgValidatePath_internal (const std::string& normed_path, bool write)
172 const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
175 // Check against each allowed pattern
176 for( string_list::const_iterator it = allowed_paths.begin();
177 it != allowed_paths.end();
180 star_pos = it->find('*');
181 if (star_pos == std::string::npos) {
182 if (!(it->compare(normed_path))) {
186 if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
187 && !(it->substr(0,star_pos)
188 .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
189 && !(it->substr(star_pos+1,it->size()-star_pos-1)
190 .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
191 it->size()-star_pos-1))) /* after-star parts match */) {
199 // Check whether Nasal is allowed to access a path
200 // Warning: because this always (not just on Windows) converts \ to /,
201 // if passing a std::string, use the returned path not the original one
202 // (This warning does not apply to the SGPath variant, as these are
203 // so converted on creation.)
204 std::string fgValidatePath (const std::string& path, bool write)
206 // Normalize the path (prevents ../../.. trickery)
207 // method 1 allows following symlinks to anywhere
208 // (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=780867);
209 // method 2 doesn't, and is intended to eventually replace it
210 std::string normed_path1 = fgNormalizePath(path);
211 SGPath path2 = SGPath(path);
212 std::string normed_path2;
213 if (path2.exists()) {
214 normed_path2 = path2.realpath();
215 } else { // realpath can't check non-existent files
216 normed_path2 = SGPath(path2.dir()).realpath()
217 + "/" + path2.file();
221 if (fgValidatePath_internal(normed_path1, write).empty() ||
222 fgValidatePath_internal(normed_path2, write).empty()) {
227 std::string fgValidatePath(const SGPath& path, bool write) { return fgValidatePath(path.str(),write); }