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 <cmath>
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
fgGetLowPass(double current,double target,double timeratio)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 /**
79 * Allowed paths here are absolute, and may contain _one_ *,
80 * which matches any string
81 */
fgInitAllowedPaths()82 void fgInitAllowedPaths()
83 {
84 if(SGPath("ygjmyfvhhnvdoesnotexist").realpath().utf8Str() == "ygjmyfvhhnvdoesnotexist"){
85 // Forbid using this version of fgValidatePath() with older
86 // (not normalizing non-existent files) versions of realpath(),
87 // as that would be a security hole
88 flightgear::fatalMessageBoxThenExit(
89 "Nasal initialization error",
90 "Version mismatch - please update simgear");
91 }
92 read_allowed_paths.clear();
93 write_allowed_paths.clear();
94
95 PathList read_paths = globals->get_extra_read_allowed_paths();
96 read_paths.push_back(globals->get_fg_root());
97 read_paths.push_back(globals->get_fg_home());
98
99 for( PathList::const_iterator it = read_paths.begin(); it != read_paths.end(); ++it )
100 {
101 // if we get the initialization order wrong, better to have an
102 // obvious error than a can-read-everything security hole...
103 if (it->isNull()) {
104 flightgear::fatalMessageBoxThenExit(
105 "Nasal initialization error",
106 "Empty string in FG_ROOT, FG_HOME, FG_AIRCRAFT, FG_SCENERY or "
107 "--allow-nasal-read, or fgInitAllowedPaths() called too early");
108 }
109 read_allowed_paths.push_back(it->realpath().utf8Str() + "/*");
110 read_allowed_paths.push_back(it->realpath().utf8Str());
111 }
112
113 std::string fg_home = globals->get_fg_home().realpath().utf8Str();
114 write_allowed_paths.push_back(fg_home + "/*.sav");
115 write_allowed_paths.push_back(fg_home + "/*.log");
116 write_allowed_paths.push_back(fg_home + "/cache/*");
117 write_allowed_paths.push_back(fg_home + "/Export/*");
118 write_allowed_paths.push_back(fg_home + "/state/*.xml");
119 write_allowed_paths.push_back(fg_home + "/aircraft-data/*.xml");
120 write_allowed_paths.push_back(fg_home + "/Wildfire/*.xml");
121 write_allowed_paths.push_back(fg_home + "/runtime-jetways/*.xml");
122 write_allowed_paths.push_back(fg_home + "/Input/Joysticks/*.xml");
123
124 // Check that it works
125 std::string homePath = globals->get_fg_home().utf8Str();
126 if(!fgValidatePath(homePath + "/../no.log",true).isNull() ||
127 !fgValidatePath(homePath + "/no.logt",true).isNull() ||
128 !fgValidatePath(homePath + "/nolog",true).isNull() ||
129 !fgValidatePath(homePath + "no.log",true).isNull() ||
130 !fgValidatePath(homePath + "\\..\\no.log",false).isNull() ||
131 fgValidatePath(homePath + "/aircraft-data/yes..xml",true).isNull() ||
132 fgValidatePath(homePath + "/.\\yes.bmp",false).isNull()) {
133 flightgear::fatalMessageBoxThenExit(
134 "Nasal initialization error",
135 "The FG_HOME directory must not be inside any of the FG_ROOT, "
136 "FG_AIRCRAFT, FG_SCENERY or --allow-nasal-read directories",
137 "(check that you have not accidentally included an extra ':', "
138 "as an empty part means the current directory)");
139 }
140 }
141
142 /**
143 * Check whether Nasal is allowed to access a path
144 * Warning: because this always (not just on Windows) treats both \ and /
145 * as path separators, and accepts relative paths (check-to-use race if
146 * the current directory changes),
147 * always use the returned path not the original one
148 */
fgValidatePath(const SGPath & path,bool write)149 SGPath fgValidatePath (const SGPath& path, bool write)
150 {
151 // Normalize the path (prevents ../../.. or symlink trickery)
152 std::string normed_path = path.realpath().utf8Str();
153
154 const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
155 size_t star_pos;
156
157 // Check against each allowed pattern
158 for( string_list::const_iterator it = allowed_paths.begin();
159 it != allowed_paths.end();
160 ++it )
161 {
162 star_pos = it->find('*');
163 if (star_pos == std::string::npos) {
164 if (!(it->compare(normed_path))) {
165 return SGPath::fromUtf8(normed_path);
166 }
167 } else {
168 if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
169 && !(it->substr(0,star_pos)
170 .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
171 && !(it->substr(star_pos+1,it->size()-star_pos-1)
172 .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
173 it->size()-star_pos-1))) /* after-star parts match */) {
174 return SGPath::fromUtf8(normed_path);
175 }
176 }
177 }
178 // no match found
179 return SGPath();
180 }
181
generateAuthorsText(SGPropertyNode * authors)182 std::string generateAuthorsText(SGPropertyNode* authors)
183 {
184 std::string result;
185 for (auto a : authors->getChildren("author")) {
186 const std::string name = a->getStringValue("name");
187 if (name.empty())
188 continue;
189
190 if (!result.empty())
191 result += ", ";
192 result += a->getStringValue("name");
193 }
194 return result;
195 }
196
getAircraftAuthorsText()197 std::string flightgear::getAircraftAuthorsText()
198 {
199 const auto authorsNode = fgGetNode("sim/authors");
200 if (authorsNode) {
201 // we have structured authors data
202 return generateAuthorsText(authorsNode);
203 }
204
205 // if we hit this point, there is no strucutred authors data
206 return fgGetString("/sim/author");
207 }
208
209 // end of util.cxx
210