]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/util.cxx
Interim windows build fix
[flightgear.git] / src / Main / util.cxx
index ca3231206db146073d6ebc257c9bfeafaf3ae714..6bc4ddb50f36093591778d1ee750c91bb7a56167 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include <simgear/compiler.h>
 
-#include <math.h>
+#include <cmath>
+
+#include <cstdlib>
 
 #include <vector>
-SG_USING_STD(vector);
 
 #include <simgear/debug/logstream.hxx>
+#include <simgear/math/SGLimits.hxx>
+#include <simgear/math/SGMisc.hxx>
 
+#include <GUI/MessageBox.hxx>
 #include "fg_io.hxx"
 #include "fg_props.hxx"
 #include "globals.hxx"
 #include "util.hxx"
 
+#ifdef OSG_LIBRARY_STATIC
+#include "osgDB/Registry"
+#endif
 
-void
-fgDefaultWeatherValue (const char * propname, double value)
-{
-    unsigned int i;
-
-    SGPropertyNode * branch = fgGetNode("/environment/config/boundary", true);
-    vector<SGPropertyNode_ptr> entries = branch->getChildren("entry");
-    for (i = 0; i < entries.size(); i++) {
-        entries[i]->setDoubleValue(propname, value);
-    }
-
-    branch = fgGetNode("/environment/config/aloft", true);
-    entries = branch->getChildren("entry");
-    for (i = 0; i < entries.size(); i++) {
-        entries[i]->setDoubleValue(propname, value);
-    }
-}
-
-
-void
-fgSetupWind (double min_hdg, double max_hdg, double speed, double gust)
-{
-                                // Initialize to a reasonable state
-  fgDefaultWeatherValue("wind-from-heading-deg", min_hdg);
-  fgDefaultWeatherValue("wind-speed-kt", speed);
-
-  SG_LOG(SG_GENERAL, SG_INFO, "WIND: " << min_hdg << '@' <<
-         speed << " knots" << endl);
-
-                                // Now, add some variety to the layers
-  min_hdg += 10;
-  if (min_hdg > 360)
-      min_hdg -= 360;
-  speed *= 1.1;
-  fgSetDouble("/environment/config/boundary/entry[1]/wind-from-heading-deg",
-              min_hdg);
-  fgSetDouble("/environment/config/boundary/entry[1]/wind-speed-kt",
-              speed);
-
-  min_hdg += 20;
-  if (min_hdg > 360)
-      min_hdg -= 360;
-  speed *= 1.1;
-  fgSetDouble("/environment/config/aloft/entry[0]/wind-from-heading-deg",
-              min_hdg);
-  fgSetDouble("/environment/config/aloft/entry[0]/wind-speed-kt",
-              speed);
-
-  min_hdg += 10;
-  if (min_hdg > 360)
-      min_hdg -= 360;
-  speed *= 1.1;
-  fgSetDouble("/environment/config/aloft/entry[1]/wind-from-heading-deg",
-              min_hdg);
-  fgSetDouble("/environment/config/aloft/entry[1]/wind-speed-kt",
-              speed);
-
-  min_hdg += 10;
-  if (min_hdg > 360)
-      min_hdg -= 360;
-  speed *= 1.1;
-  fgSetDouble("/environment/config/aloft/entry[2]/wind-from-heading-deg",
-              min_hdg);
-  fgSetDouble("/environment/config/aloft/entry[2]/wind-speed-kt",
-              speed);
-}
-
-
-void
-fgExit (int status)
-{
-    SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
-
-    globals->get_io()->shutdown_all();
-    exit(status);
-}
-
+using std::vector;
 
 // Originally written by Alex Perry.
 double
 fgGetLowPass (double current, double target, double timeratio)
 {
     if ( timeratio < 0.0 ) {
-       if ( timeratio < -1.0 ) {
+        if ( timeratio < -1.0 ) {
                                 // time went backwards; kill the filter
                 current = target;
         } else {
@@ -138,5 +72,124 @@ fgGetLowPass (double current, double target, double timeratio)
     return current;
 }
 
+static string_list read_allowed_paths;
+static string_list write_allowed_paths;
+
+/**
+ * Allowed paths here are absolute, and may contain _one_ *,
+ * which matches any string
+ */
+void fgInitAllowedPaths()
+{
+    if(SGPath("ygjmyfvhhnvdoesnotexist").realpath() == "ygjmyfvhhnvdoesnotexist"){
+        // Forbid using this version of fgValidatePath() with older
+        // (not normalizing non-existent files) versions of realpath(),
+        // as that would be a security hole
+        flightgear::fatalMessageBox("Nasal initialization error",
+                                    "Version mismatch - please update simgear",
+                                    "");
+        exit(-1);
+    }
+    read_allowed_paths.clear();
+    write_allowed_paths.clear();
+    std::string fg_root = SGPath(globals->get_fg_root()).realpath();
+    std::string fg_home = SGPath(globals->get_fg_home()).realpath();
+#if defined(_MSC_VER) /*for MS compilers */ || defined(_WIN32) /*needed for non MS windows compilers like MingW*/
+     std::string sep = "\\";
+#else
+     std::string sep = "/";
+#endif
+    read_allowed_paths.push_back(fg_root + sep + "*");
+    read_allowed_paths.push_back(fg_home + sep + "*");
+    string_list const aircraft_paths = globals->get_aircraft_paths();
+    string_list const scenery_paths = globals->get_secure_fg_scenery();
+    // not plain fg_scenery, to avoid making
+    // /sim/terrasync/scenery-dir a security hole
+
+    for( string_list::const_iterator it = aircraft_paths.begin();;++it )
+    {
+      if (it == aircraft_paths.end()) {
+        it = scenery_paths.begin();
+      }
+      if (it == scenery_paths.end()) {
+        break; // here rather than in the loop condition because
+               // scenery_paths may be empty
+      }
+      // if we get the initialization order wrong, better to have an
+      // obvious error than a can-read-everything security hole...
+        if (it->empty() || fg_root.empty() || fg_home.empty()){
+            flightgear::fatalMessageBox("Nasal initialization error",
+               "Empty string in FG_ROOT, FG_HOME, FG_AIRCRAFT or FG_SCENERY",
+                                 "or fgInitAllowedPaths() called too early");
+            exit(-1);
+        }
+        read_allowed_paths.push_back(SGPath(*it).realpath() + sep + "*");
+    }
+
+    write_allowed_paths.push_back(fg_home + sep + "*.sav");
+    write_allowed_paths.push_back(fg_home + sep + "*.log");
+    write_allowed_paths.push_back(fg_home + sep + "cache" + sep + "*");
+    write_allowed_paths.push_back(fg_home + sep + "Export" + sep + "*");
+    write_allowed_paths.push_back(fg_home + sep + "state" + sep + "*.xml");
+    write_allowed_paths.push_back(fg_home + sep + "aircraft-data" + sep + "*.xml");
+    write_allowed_paths.push_back(fg_home + sep + "Wildfire" + sep + "*.xml");
+    write_allowed_paths.push_back(fg_home + sep + "runtime-jetways" + sep + "*.xml");
+    write_allowed_paths.push_back(fg_home + sep + "Input" + sep + "Joysticks" + sep + "*.xml");
+    
+    // Check that it works
+    if(!fgValidatePath(globals->get_fg_home() + "/../no.log",true).empty() ||
+        !fgValidatePath(globals->get_fg_home() + "/no.logt",true).empty() ||
+        !fgValidatePath(globals->get_fg_home() + "/nolog",true).empty() ||
+        !fgValidatePath(globals->get_fg_home() + "no.log",true).empty() ||
+        !fgValidatePath(globals->get_fg_home() + "\\..\\no.log",false).empty() ||
+        fgValidatePath(globals->get_fg_home() + "/aircraft-data/yes..xml",true).empty() ||
+        fgValidatePath(globals->get_fg_root() + "/.\\yes.bmp",false).empty()) {
+            flightgear::fatalMessageBox("Nasal initialization error",
+                                    "fgInitAllowedPaths() does not work",
+                                    "");
+            exit(-1);
+    }
+}
+
+/**
+ * Check whether Nasal is allowed to access a path
+ * Warning: because this always (not just on Windows) treats both \ and /
+ * as path separators, and accepts relative paths (check-to-use race if
+ * the current directory changes),
+ * always use the returned path not the original one
+ */
+std::string fgValidatePath (const std::string& path, bool write)
+{
+    // Normalize the path (prevents ../../.. or symlink trickery)
+    std::string normed_path = SGPath(path).realpath();
+    
+    const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
+    size_t star_pos;
+    
+    // Check against each allowed pattern
+    for( string_list::const_iterator it = allowed_paths.begin();
+                                     it != allowed_paths.end();
+                                   ++it )
+    {
+        star_pos = it->find('*');
+        if (star_pos == std::string::npos) {
+            if (!(it->compare(normed_path))) {
+                return normed_path;
+            }
+        } else {
+            if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
+                && !(it->substr(0,star_pos)
+                    .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
+                && !(it->substr(star_pos+1,it->size()-star_pos-1)
+                    .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
+                      it->size()-star_pos-1))) /* after-star parts match */) {
+                return normed_path;
+            }
+        }
+    }
+    // no match found
+    return "";
+}
+std::string fgValidatePath(const SGPath& path, bool write) { return fgValidatePath(path.str(),write); }
 // end of util.cxx