Security fix: the mod_imagemagick importer used system to run convert, change it...
[synfig.git] / synfig-core / trunk / src / modules / mod_imagemagick / mptr_imagemagick.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mptr_imagemagick.cpp
3 **      \brief ppm Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <ETL/stringf>
35 #include "mptr_imagemagick.h"
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <algorithm>
41 #include <functional>
42 #include <ETL/stringf>
43 #include <synfig/general.h>
44
45 #endif
46
47 /* === M A C R O S ========================================================= */
48
49 using namespace synfig;
50 using namespace std;
51 using namespace etl;
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_IMPORTER_INIT(imagemagick_mptr);
56 SYNFIG_IMPORTER_SET_NAME(imagemagick_mptr,"imagemagick");
57 SYNFIG_IMPORTER_SET_EXT(imagemagick_mptr,"miff");
58 SYNFIG_IMPORTER_SET_VERSION(imagemagick_mptr,"0.1");
59 SYNFIG_IMPORTER_SET_CVS_ID(imagemagick_mptr,"$Id$");
60
61 /* === M E T H O D S ======================================================= */
62
63
64 imagemagick_mptr::imagemagick_mptr(const char *f)
65 {
66
67         filename=f;
68         file=NULL;
69 }
70
71 imagemagick_mptr::~imagemagick_mptr()
72 {
73         if(file)
74                 pclose(file);
75 }
76
77 bool
78 imagemagick_mptr::get_frame(synfig::Surface &surface,Time /*time*/, synfig::ProgressCallback *cb)
79 {
80 //#define HAS_LIBPNG 1
81
82 #if 1
83         if(filename.empty())
84         {
85                 if(cb)cb->error(_("No file to load"));
86                 else synfig::error(_("No file to load"));
87                 return false;
88         }
89         string temp_file="/tmp/deleteme.png";
90         string output="png32:"+temp_file;
91
92         pid_t pid = fork();
93   
94         if (pid == -1) {
95                 return false;
96         }
97   
98         if (pid == 0){
99                 // Child process
100                 if(filename.find("psd")!=String::npos)
101                         execlp("convert", "convert", filename.c_str(), "-flatten", output.c_str(), (const char *)NULL);
102                 else
103                         execlp("convert", "convert", filename.c_str(), output.c_str(), (const char *)NULL);
104                 // We should never reach here unless the exec failed
105                 return false;
106         }
107
108         int status;
109         waitpid(pid, &status, 0);
110         if( (WIFEXITED(status) && WEXITSTATUS(status) != 0) || !WIFEXITED(status) )
111                 return false;
112
113         Importer::Handle importer(Importer::open(temp_file));
114
115         DEBUGPOINT();
116
117         if(!importer)
118         {
119                 if(cb)cb->error(_("Unable to open ")+temp_file);
120                 else synfig::error(_("Unable to open ")+temp_file);
121                 return false;
122         }
123
124         DEBUGPOINT();
125
126         if(!importer->get_frame(surface,0,cb))
127         {
128                 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
129                 else synfig::error(_("Unable to get frame from ")+temp_file);
130                 return false;
131         }
132
133         if(!surface)
134         {
135                 if(cb)cb->error(_("Bad surface from ")+temp_file);
136                 else synfig::error(_("Bad surface from ")+temp_file);
137                 return false;
138         }
139
140         if(1)
141         {
142                 // remove odd premultiplication
143                 for(int i=0;i<surface.get_w()*surface.get_h();i++)
144                 {
145                         Color c(surface[0][i]);
146
147                         if(c.get_a())
148                         {
149                                 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
150                                 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
151                                 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
152                         }
153                         else
154                         {
155                                 surface[0][i].set_r(0);
156                                 surface[0][i].set_g(0);
157                                 surface[0][i].set_b(0);
158                         }
159                         surface[0][i].set_a(c.get_a());
160                 }
161         }
162
163         Surface bleh(surface);
164         surface=bleh;
165
166
167         //remove(temp_file.c_str());
168         DEBUGPOINT();
169         return true;
170
171 #else
172         
173 #error This code contains tempfile and arbitrary shell command execution vulnerabilities
174         
175         if(file)
176                 pclose(file);
177
178         string command;
179
180         if(filename.empty())
181         {
182                 if(cb)cb->error(_("No file to load"));
183                 else synfig::error(_("No file to load"));
184                 return false;
185         }
186
187         command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
188
189         file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
190
191         if(!file)
192         {
193                 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
194                 else synfig::error(_("Unable to open pipe to imagemagick"));
195                 return false;
196         }
197         int w,h;
198         float divisor;
199         char cookie[2];
200
201         while((cookie[0]=fgetc(file))!='P' && !feof(file));
202
203         if(feof(file))
204         {
205                 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
206                 else synfig::error(_("Reached end of stream without finding PPM header"));
207                 return false;
208         }
209
210         cookie[1]=fgetc(file);
211
212         if(cookie[0]!='P' || cookie[1]!='6')
213         {
214                 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
215                 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
216                 return false;
217         }
218
219         fgetc(file);
220         fscanf(file,"%d %d\n",&w,&h);
221         fscanf(file,"%f",&divisor);
222         fgetc(file);
223
224         if(feof(file))
225         {
226                 if(cb)cb->error(_("Premature end of file (after header)"));
227                 else synfig::error(_("Premature end of file (after header)"));
228                 return false;
229         }
230
231         int x;
232         int y;
233         frame.set_wh(w,h);
234         for(y=0;y<frame.get_h();y++)
235                 for(x=0;x<frame.get_w();x++)
236                 {
237                         if(feof(file))
238                         {
239                                 if(cb)cb->error(_("Premature end of file"));
240                                 else synfig::error(_("Premature end of file"));
241                                 return false;
242                         }
243                         float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
244                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
245                         float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
246 /*
247                         float b=(float)(unsigned char)fgetc(file)/divisor;
248                         float g=(float)(unsigned char)fgetc(file)/divisor;
249                         float r=(float)(unsigned char)fgetc(file)/divisor;
250 */
251                         frame[y][x]=Color(
252                                 b,
253                                 g,
254                                 r,
255                                 1.0
256                         );
257                 }
258
259         surface=frame;
260
261         return true;
262 #endif
263
264
265 }