]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Make fgValidatePath always return std::string, not char *
[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     // Check that it works
120     if(!fgValidatePath(globals->get_fg_home() + "/../no.log",true).empty() ||
121         !fgValidatePath(globals->get_fg_home() + "/no.lot",true).empty() ||
122         !fgValidatePath(globals->get_fg_home() + "/nolog",true).empty() ||
123         !fgValidatePath(globals->get_fg_home() + "no.log",true).empty() ||
124         !fgValidatePath("..\\" + globals->get_fg_home() + "/no.log",false).empty() ||
125         !fgValidatePath(std::string("/tmp/no.xml"),false).empty() ||
126         fgValidatePath(globals->get_fg_home() + "/./ff/../Export\\yes..gg",true).empty() ||
127         fgValidatePath(globals->get_fg_home() + "/aircraft-data/yes..xml",true).empty() ||
128         fgValidatePath(globals->get_fg_root() + "/./\\yes.bmp",false).empty()) {
129             flightgear::fatalMessageBox("Nasal initialization error",
130                                     "fgInitAllowedPaths() does not work",
131                                     "");
132             exit(-1);
133     }
134 }
135
136 // Normalize a path
137 // Unlike SGPath::realpath, does not require that the file already exists,
138 // but does require that it be below the starting point
139 static std::string fgNormalizePath (const std::string& path)
140 {
141     string_list path_parts;
142     char c;
143     std::string normed_path = "", this_part = "";
144     
145     for (int pos = 0; ; pos++) {
146         c = path[pos];
147         if (c == '\\') { c = '/'; }
148         if ((c == '/') || (c == 0)) {
149             if ((this_part == "/..") || (this_part == "..")) {
150                 if (path_parts.empty()) { return ""; }
151                 path_parts.pop_back();
152             } else if ((this_part != "/.") && (this_part != "/")) {
153                 path_parts.push_back(this_part);
154             }
155             this_part = "";
156         }
157         if (c == 0) { break; }
158         this_part = this_part + c;
159     }
160     for( string_list::const_iterator it = path_parts.begin();
161                                      it != path_parts.end();
162                                    ++it )
163     {
164         normed_path.append(*it);
165     }
166     return normed_path;
167  }
168
169
170 // Check whether Nasal is allowed to access a path
171 std::string fgValidatePath (const std::string& path, bool write)
172 {
173     const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
174     int star_pos;
175     
176     // Normalize the path (prevents ../../.. trickery)
177     std::string normed_path = fgNormalizePath(path);
178
179     // Check against each allowed pattern
180     for( string_list::const_iterator it = allowed_paths.begin();
181                                      it != allowed_paths.end();
182                                    ++it )
183     {
184         star_pos = it->find('*');
185         if (star_pos == std::string::npos) {
186             if (!(it->compare(normed_path))) {
187                 return normed_path;
188             }
189         } else {
190             if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
191                 && !(it->substr(0,star_pos)
192                     .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
193                 && !(it->substr(star_pos+1,it->size()-star_pos-1)
194                     .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
195                       it->size()-star_pos-1))) /* after-star parts match */) {
196                 return normed_path;
197             }
198         }
199     }
200     // no match found
201     return "";
202 }
203 std::string fgValidatePath(const SGPath& path, bool write) { return fgValidatePath(path.str(),write); }
204 // end of util.cxx
205