Fix issue in unused mod_ffmpeg importer security fixes; read from the ffmpeg output...
[synfig.git] / synfig-core / trunk / src / modules / mod_ffmpeg / mptr_ffmpeg.cpp
index 3a1646f..f1f7e0b 100644 (file)
@@ -1,8 +1,10 @@
-/*! ========================================================================
-** Synfig
-** ppm Target Module
-** $Id: mptr_ffmpeg.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
+/* === S Y N F I G ========================================================= */
+/*!    \file mptr_ffmpeg.cpp
+**     \brief ppm Target Module
 **
+**     $Id$
+**
+**     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **
 **     This package is free software; you can redistribute it and/or
@@ -14,6 +16,7 @@
 **     but WITHOUT ANY WARRANTY; without even the implied warranty of
 **     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 **     General Public License for more details.
+**     \endlegal
 **
 ** === N O T E S ===========================================================
 **
@@ -31,6 +34,9 @@
 #include <ETL/stringf>
 #include "mptr_ffmpeg.h"
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
 #include <iostream>
 #include <algorithm>
 #include <functional>
@@ -49,7 +55,7 @@ SYNFIG_IMPORTER_INIT(ffmpeg_mptr);
 SYNFIG_IMPORTER_SET_NAME(ffmpeg_mptr,"ffmpeg");
 SYNFIG_IMPORTER_SET_EXT(ffmpeg_mptr,"avi");
 SYNFIG_IMPORTER_SET_VERSION(ffmpeg_mptr,"0.1");
-SYNFIG_IMPORTER_SET_CVS_ID(ffmpeg_mptr,"$Id: mptr_ffmpeg.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
+SYNFIG_IMPORTER_SET_CVS_ID(ffmpeg_mptr,"$Id$");
 
 /* === M E T H O D S ======================================================= */
 
@@ -60,14 +66,47 @@ ffmpeg_mptr::seek_to(int frame)
        {
                if(file)
                {
-                       pclose(file);
+                       fclose(file);
+                       int status;
+                       waitpid(pid,&status,0);
                }
 
-               string command;
-
-               command=strprintf("ffmpeg -i \"%s\" -an -f image2pipe -vcodec ppm -\n",filename.c_str());
-
-               file=popen(command.c_str(),"r");
+               int p[2];
+         
+               if (pipe(p)) {
+                       cerr<<"Unable to open pipe to ffmpeg"<<endl;
+                       return false;
+               };
+         
+               pid = fork();
+         
+               if (pid == -1) {
+                       cerr<<"Unable to open pipe to ffmpeg"<<endl;
+                       return false;
+               }
+         
+               if (pid == 0){
+                       // Child process
+                       // Close pipein, not needed
+                       close(p[0]);
+                       // Dup pipein to stdout
+                       if( dup2( p[1], STDOUT_FILENO ) == -1 ){
+                               cerr<<"Unable to open pipe to ffmpeg"<<endl;
+                               return false;
+                       }
+                       // Close the unneeded pipein
+                       close(p[1]);
+                       execlp("ffmpeg", "ffmpeg", "-i", filename.c_str(), "-an", "-f", "image2pipe", "-vcodec", "ppm", "-", (const char *)NULL);
+                       // We should never reach here unless the exec failed
+                       cerr<<"Unable to open pipe to ffmpeg"<<endl;
+                       return false;
+               } else {
+                       // Parent process
+                       // Close pipeout, not needed
+                       close(p[1]);
+                       // Save pipein to file handle, will read from it later
+                       file = fdopen(p[0], "rb");
+               }
 
                if(!file)
                {
@@ -145,6 +184,7 @@ ffmpeg_mptr::grab_frame(void)
 
 ffmpeg_mptr::ffmpeg_mptr(const char *f)
 {
+       pid=-1;
 #ifdef HAVE_TERMIOS_H
        tcgetattr (0, &oldtty);
 #endif
@@ -157,7 +197,11 @@ ffmpeg_mptr::ffmpeg_mptr(const char *f)
 ffmpeg_mptr::~ffmpeg_mptr()
 {
        if(file)
-               pclose(file);
+       {
+               fclose(file);
+               int status;
+               waitpid(pid,&status,0);
+       }
 #ifdef HAVE_TERMIOS_H
        tcsetattr(0,TCSANOW,&oldtty);
 #endif