]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Stop using property listener for fgValidatePath
[flightgear.git] / src / Main / util.cxx
1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
3 //
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.
8 //
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.
13 //
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.
17 //
18 // $Id$
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <simgear/compiler.h>
25
26 #include <math.h>
27
28 #include <cstdlib>
29
30 #include <vector>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/SGLimits.hxx>
34 #include <simgear/math/SGMisc.hxx>
35
36 #include <GUI/MessageBox.hxx>
37 #include "fg_io.hxx"
38 #include "fg_props.hxx"
39 #include "globals.hxx"
40 #include "util.hxx"
41
42 #ifdef OSG_LIBRARY_STATIC
43 #include "osgDB/Registry"
44 #endif
45
46 using std::vector;
47
48 // Originally written by Alex Perry.
49 double
50 fgGetLowPass (double current, double target, double timeratio)
51 {
52     if ( timeratio < 0.0 ) {
53         if ( timeratio < -1.0 ) {
54                                 // time went backwards; kill the filter
55                 current = target;
56         } else {
57                                 // ignore mildly negative time
58         }
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
65         current = target;
66     } else {
67                                 // Moderate time step; non linear response
68         double keep = exp(-timeratio);
69         current = current * keep + target * (1.0 - keep);
70     }
71
72     return current;
73 }
74
75 static string_list read_allowed_paths;
76 static string_list write_allowed_paths;
77
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()
83 {
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();
91                                    ++it )
92     {
93         read_allowed_paths.push_back(*it + "/*");
94     }
95
96     for( string_list::const_iterator it = read_allowed_paths.begin();
97                                      it != read_allowed_paths.end();
98                                    ++it )
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");
105             exit(-1);
106         }
107     }
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");
118     
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",
130                                     "");
131             exit(-1);
132     }
133 }
134
135 // Normalize a path
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)
139 {
140     string_list path_parts;
141     char c;
142     std::string normed_path = "", this_part = "";
143     
144     for (int pos = 0; ; pos++) {
145         c = path[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);
153             }
154             this_part = "";
155         }
156         if (c == 0) { break; }
157         this_part = this_part + c;
158     }
159     for( string_list::const_iterator it = path_parts.begin();
160                                      it != path_parts.end();
161                                    ++it )
162     {
163         normed_path.append(*it);
164     }
165     return normed_path;
166  }
167
168
169 // Check whether Nasal is allowed to access a path
170 std::string fgValidatePath (const std::string& path, bool write)
171 {
172     const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
173     int star_pos;
174     
175     // Normalize the path (prevents ../../.. trickery)
176     std::string normed_path = fgNormalizePath(path);
177
178     // Check against each allowed pattern
179     for( string_list::const_iterator it = allowed_paths.begin();
180                                      it != allowed_paths.end();
181                                    ++it )
182     {
183         star_pos = it->find('*');
184         if (star_pos == std::string::npos) {
185             if (!(it->compare(normed_path))) {
186                 return normed_path;
187             }
188         } else {
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 */) {
195                 return normed_path;
196             }
197         }
198     }
199     // no match found
200     return "";
201 }
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)
205 {
206   validate_path_temp = fgValidatePath(std::string(path), write);
207   if(validate_path_temp.empty()){
208       return 0;
209   }
210   return validate_path_temp.c_str();
211 }
212 // end of util.cxx
213