Only build in gettext support when gettext is available.
[synfig.git] / synfig-core / trunk / src / modules / mod_imagemagick / mptr_imagemagick.cpp
index bd577ac..4d7ca20 100644 (file)
@@ -34,6 +34,9 @@
 #include <ETL/stringf>
 #include "mptr_imagemagick.h"
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
 #include <algorithm>
 #include <functional>
 #include <ETL/stringf>
@@ -72,16 +75,11 @@ imagemagick_mptr::~imagemagick_mptr()
 }
 
 bool
-imagemagick_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *cb)
+imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
 {
 //#define HAS_LIBPNG 1
 
 #if 1
-       if(file)
-               pclose(file);
-
-       string command;
-
        if(filename.empty())
        {
                if(cb)cb->error(_("No file to load"));
@@ -89,15 +87,27 @@ imagemagick_mptr::get_frame(synfig::Surface &surface,Time time, synfig::Progress
                return false;
        }
        string temp_file="/tmp/deleteme.png";
+       string output="png32:"+temp_file;
 
-       if(filename.find("psd")!=String::npos)
-               command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
-       else
-               command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
-
-       synfig::info("command=%s",command.c_str());
+       pid_t pid = fork();
+  
+       if (pid == -1) {
+               return false;
+       }
+  
+       if (pid == 0){
+               // Child process
+               if(filename.find("psd")!=String::npos)
+                       execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
+               else
+                       execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
+               // We should never reach here unless the exec failed
+               return false;
+       }
 
-       if(system(command.c_str())!=0)
+       int status;
+       waitpid(pid, &status, 0);
+       if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
                return false;
 
        Importer::Handle importer(Importer::open(temp_file));
@@ -159,6 +169,9 @@ imagemagick_mptr::get_frame(synfig::Surface &surface,Time time, synfig::Progress
        return true;
 
 #else
+       
+#error This code contains tempfile and arbitrary shell command execution vulnerabilities
+       
        if(file)
                pclose(file);
 
@@ -173,7 +186,7 @@ imagemagick_mptr::get_frame(synfig::Surface &surface,Time time, synfig::Progress
 
        command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
 
-       file=popen(command.c_str(),"r");
+       file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
 
        if(!file)
        {