346f2b4
diff -up tecnoballz-0.92/include/handler_resources.h.sgid tecnoballz-0.92/include/handler_resources.h
346f2b4
--- tecnoballz-0.92/include/handler_resources.h.sgid	2007-10-09 07:46:24.000000000 +0200
346f2b4
+++ tecnoballz-0.92/include/handler_resources.h	2007-12-21 11:16:40.000000000 +0100
346f2b4
@@ -42,7 +42,6 @@ class handler_resources:public virtual t
346f2b4
     static const char *folderdata;
346f2b4
     static const char *folder_320;
346f2b4
     static const char *folder_640;
346f2b4
-    static char *fnamescore;
346f2b4
     static char tmp_filename[512];
346f2b4
     static char pathstring[512];
346f2b4
     static const Uint32 TEXTS_OFFSET = 2048;
346f2b4
@@ -93,6 +92,8 @@ class handler_resources:public virtual t
346f2b4
     static const Sint16 *zesinus360;
346f2b4
     static const Uint32 color_gradations[180];
346f2b4
 
346f2b4
+    static char *fnamescore;
346f2b4
+    static int score_fhand;
346f2b4
   private:
346f2b4
     /** Size last file loaded in memory */
346f2b4
     Uint32 last_filesize_loaded;
346f2b4
diff -up tecnoballz-0.92/src/handler_resources.cc.sgid tecnoballz-0.92/src/handler_resources.cc
346f2b4
--- tecnoballz-0.92/src/handler_resources.cc.sgid	2007-10-29 14:18:53.000000000 +0100
346f2b4
+++ tecnoballz-0.92/src/handler_resources.cc	2007-12-21 11:16:40.000000000 +0100
346f2b4
@@ -46,6 +46,7 @@
346f2b4
 #endif
346f2b4
 
346f2b4
 char * handler_resources::fnamescore = SCOREFILE;
346f2b4
+int handler_resources::score_fhand = -1;
346f2b4
 const char *
346f2b4
   handler_resources::folder_640 = "hires/";
346f2b4
 const char *
346f2b4
@@ -737,18 +738,54 @@ handler_resources::load_sinus ()
346f2b4
 char *
346f2b4
 handler_resources::load_high_score_file ()
346f2b4
 {
346f2b4
-  char* filedata = NULL;
346f2b4
+  if (score_fhand == -1)
346f2b4
+    return NULL;
346f2b4
+
346f2b4
+  /* read the size of the file */
346f2b4
+  struct stat sb;
346f2b4
+  if (fstat (score_fhand, &sb))
346f2b4
+    {
346f2b4
+      std::cerr << "(!)handler_resources::load_high_score_file() " <<
346f2b4
+        "can't stat file " << fnamescore
346f2b4
+        << "strerror:" << strerror (errno) << std::endl;
346f2b4
+      return NULL;
346f2b4
+    }
346f2b4
+  /* save filesize */
346f2b4
+  last_filesize_loaded = sb.st_size;
346f2b4
+
346f2b4
+  /* allocate memory require to load the filedata */
346f2b4
+  char *buffer = NULL;
346f2b4
   try
346f2b4
+  {
346f2b4
+    buffer = new char[sb.st_size];
346f2b4
+  }
346f2b4
+  catch (std::bad_alloc &)
346f2b4
+  {
346f2b4
+    std::cerr << "(!)handler_resources::load_high_score_file() " <<
346f2b4
+      "not enough memory to allocate " <<
346f2b4
+      sb.st_size << " bytes!" << std::endl;
346f2b4
+    throw;
346f2b4
+  }
346f2b4
+
346f2b4
+  /* read the file */
346f2b4
+  if(lseek(score_fhand, 0, SEEK_SET) != 0)
346f2b4
     {
346f2b4
-      filedata = load_file (fnamescore, &last_filesize_loaded);
346f2b4
+      std::cerr <<  "(!)handler_resources::load_high_score_file() can't rewind file " <<
346f2b4
+        fnamescore << "strerror:" << strerror (errno) << std::endl;
346f2b4
+      delete buffer;
346f2b4
+      return NULL;
346f2b4
     }
346f2b4
-  catch (...)
346f2b4
+
346f2b4
+  if (read (score_fhand, buffer, sb.st_size) != sb.st_size)
346f2b4
     {
346f2b4
-       std::cerr << "(!)handler_resources::load_high_score_file() " <<
346f2b4
-         "can't open the scores files!" << std::endl;
346f2b4
-       filedata = NULL;
346f2b4
+      std::cerr << "(!)handler_resources::load_high_score_file() " <<
346f2b4
+        "can't read file " << fnamescore
346f2b4
+        << "strerror:" << strerror (errno) << std::endl;
346f2b4
+      delete buffer;
346f2b4
+      return NULL;
346f2b4
     }
346f2b4
-  return filedata;
346f2b4
+
346f2b4
+  return buffer;
346f2b4
 }
346f2b4
 
346f2b4
 /* 
346f2b4
@@ -759,25 +796,18 @@ handler_resources::load_high_score_file 
346f2b4
 void
346f2b4
 handler_resources::save_high_score_file (char *buffer, Uint32 size)
346f2b4
 {
346f2b4
-#ifdef WIN32
346f2b4
-  /* set umask so that files are group-writable */
346f2b4
-  _umask (0002);
346f2b4
-#else
346f2b4
-  umask (0002);
346f2b4
-#endif
346f2b4
-  Sint32 fhand = open (fnamescore, O_WRONLY | O_CREAT, 00666);
346f2b4
-  if (fhand == -1)
346f2b4
+  if (score_fhand == -1)
346f2b4
+    return;
346f2b4
+
346f2b4
+  if(lseek(score_fhand, 0, SEEK_SET) != 0)
346f2b4
     {
346f2b4
-      fprintf (stderr,
346f2b4
-               "handler_resources::saveScores(): file:%s / error:%s\n",
346f2b4
-               fnamescore, strerror (errno));
346f2b4
+      fprintf(stderr,
346f2b4
+              "handler_resources::saveScores() can't rewind file %s (%s)\n\n",
346f2b4
+              fnamescore, strerror(errno));
346f2b4
+      return;
346f2b4
     }
346f2b4
-#ifdef WIN32
346f2b4
-  _write (fhand, buffer, size);
346f2b4
-#else
346f2b4
-  write (fhand, buffer, size);
346f2b4
-#endif
346f2b4
-  if (close (fhand) == -1)
346f2b4
+
346f2b4
+  if(write(score_fhand, buffer, size) != size)
346f2b4
     {
346f2b4
       fprintf (stderr,
346f2b4
                "handler_resources::saveScores(): file:%s / error:%s\n",
346f2b4
diff -up tecnoballz-0.92/src/main.cc.sgid tecnoballz-0.92/src/main.cc
346f2b4
--- tecnoballz-0.92/src/main.cc.sgid	2007-11-19 13:44:15.000000000 +0100
346f2b4
+++ tecnoballz-0.92/src/main.cc	2007-12-21 11:17:38.000000000 +0100
346f2b4
@@ -26,6 +26,8 @@
346f2b4
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
346f2b4
  * MA  02110-1301, USA.
346f2b4
  */
346f2b4
+#include <unistd.h>
346f2b4
+#include "../include/handler_resources.h"
346f2b4
 #include "../include/tecnoballz.h"
346f2b4
 #include "../include/handler_display.h"
346f2b4
 #include "../include/handler_audio.h"
346f2b4
@@ -61,6 +63,19 @@ returnToMenu (void)
346f2b4
 Sint32
346f2b4
 main (Sint32 arg_count, char **arg_values)
346f2b4
 {
346f2b4
+  gid_t realgid;
346f2b4
+
346f2b4
+  handler_resources::score_fhand = open(handler_resources::fnamescore, O_RDWR);
346f2b4
+
346f2b4
+  /* Figure out who we really are. */
346f2b4
+  realgid = getgid();
346f2b4
+
346f2b4
+  /* This is where we drop our setuid/setgid privileges. */
346f2b4
+  if (setresgid(-1, realgid, realgid) != 0) {
346f2b4
+      perror("Could not drop setgid privileges.  Aborting.");
346f2b4
+      exit(1);
346f2b4
+  }
346f2b4
+
346f2b4
   /* GP2X or PSP port */
346f2b4
 #ifdef TECNOBALLZ_HANDHELD_CONSOLE
346f2b4
   /* Use atexit() to call the return-to-menu function,
346f2b4
@@ -71,7 +86,7 @@ main (Sint32 arg_count, char **arg_value
346f2b4
   configuration.load ();
346f2b4
   if (!configuration.scan_arguments (arg_count, arg_values))
346f2b4
     {
346f2b4
-      return 0;
346f2b4
+      goto main_exit;
346f2b4
     }
346f2b4
   if (tecnoballz::is_verbose)
346f2b4
     {
346f2b4
@@ -79,7 +94,6 @@ main (Sint32 arg_count, char **arg_value
346f2b4
         << "TecnoballZ starts! " << std::endl 
346f2b4
         << "================================" << std::endl;
346f2b4
     }
346f2b4
-  Sint32 error = 0;
346f2b4
   try
346f2b4
     {
346f2b4
       tecnoballz::first_init (&configuration);
346f2b4
@@ -94,6 +108,8 @@ main (Sint32 arg_count, char **arg_value
346f2b4
     {
346f2b4
       std::cerr << "fatal error" << std::endl;
346f2b4
       tecnoballz::release_all_objects (&configuration);
346f2b4
+      if (handler_resources::score_fhand != -1)
346f2b4
+        close(handler_resources::score_fhand);
346f2b4
       throw;
346f2b4
     }
346f2b4
   if (tecnoballz::is_verbose)
346f2b4
@@ -106,7 +122,12 @@ main (Sint32 arg_count, char **arg_value
346f2b4
       std::cout << "TecnoballZ is finished! ========" << std::endl;
346f2b4
     }
346f2b4
   configuration.save ();
346f2b4
-  return error;
346f2b4
+
346f2b4
+main_exit:
346f2b4
+  if (handler_resources::score_fhand != -1)
346f2b4
+    close(handler_resources::score_fhand);
346f2b4
+
346f2b4
+  return 0;
346f2b4
 }
346f2b4
 
346f2b4