Fix for bug 797.
[gnupg.git] / g10 / photoid.c
blobcca32bc822de37db678fa5f46c13ff45751f16b7
1 /* photoid.c - photo ID handling code
2 * Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef _WIN32
27 # include <windows.h>
28 # ifndef VER_PLATFORM_WIN32_WINDOWS
29 # define VER_PLATFORM_WIN32_WINDOWS 1
30 # endif
31 #endif
33 #include "gpg.h"
34 #include "packet.h"
35 #include "status.h"
36 #include "exec.h"
37 #include "keydb.h"
38 #include "util.h"
39 #include "i18n.h"
40 #include "iobuf.h"
41 #include "options.h"
42 #include "main.h"
43 #include "photoid.h"
44 #include "ttyio.h"
46 /* Generate a new photo id packet, or return NULL if canceled */
47 PKT_user_id *
48 generate_photo_id(PKT_public_key *pk,const char *photo_name)
50 PKT_user_id *uid;
51 int error=1,i;
52 unsigned int len;
53 char *filename;
54 byte *photo=NULL;
55 byte header[16];
56 IOBUF file;
57 int overflow;
59 header[0]=0x10; /* little side of photo header length */
60 header[1]=0; /* big side of photo header length */
61 header[2]=1; /* 1 == version of photo header */
62 header[3]=1; /* 1 == JPEG */
64 for(i=4;i<16;i++) /* The reserved bytes */
65 header[i]=0;
67 #define EXTRA_UID_NAME_SPACE 71
68 uid=xmalloc_clear(sizeof(*uid)+71);
70 if(photo_name && *photo_name)
71 filename=make_filename(photo_name,(void *)NULL);
72 else
74 tty_printf(_("\nPick an image to use for your photo ID."
75 " The image must be a JPEG file.\n"
76 "Remember that the image is stored within your public key."
77 " If you use a\n"
78 "very large picture, your key will become very large"
79 " as well!\n"
80 "Keeping the image close to 240x288 is a good size"
81 " to use.\n"));
82 filename=NULL;
85 while(photo==NULL)
87 if(filename==NULL)
89 char *tempname;
91 tty_printf("\n");
93 tty_enable_completion(NULL);
95 tempname=cpr_get("photoid.jpeg.add",
96 _("Enter JPEG filename for photo ID: "));
98 tty_disable_completion();
100 filename=make_filename(tempname,(void *)NULL);
102 xfree(tempname);
104 if(strlen(filename)==0)
105 goto scram;
108 file=iobuf_open(filename);
109 if (file && is_secured_file (iobuf_get_fd (file)))
111 iobuf_close (file);
112 file = NULL;
113 errno = EPERM;
115 if(!file)
117 log_error(_("unable to open JPEG file `%s': %s\n"),
118 filename,strerror(errno));
119 xfree(filename);
120 filename=NULL;
121 continue;
125 len=iobuf_get_filelength(file, &overflow);
126 if(len>6144 || overflow)
128 tty_printf( _("This JPEG is really large (%d bytes) !\n"),len);
129 if(!cpr_get_answer_is_yes("photoid.jpeg.size",
130 _("Are you sure you want to use it? (y/N) ")))
132 iobuf_close(file);
133 xfree(filename);
134 filename=NULL;
135 continue;
139 photo=xmalloc(len);
140 iobuf_read(file,photo,len);
141 iobuf_close(file);
143 /* Is it a JPEG? */
144 if(photo[0]!=0xFF || photo[1]!=0xD8 ||
145 photo[6]!='J' || photo[7]!='F' || photo[8]!='I' || photo[9]!='F')
147 log_error(_("`%s' is not a JPEG file\n"),filename);
148 xfree(photo);
149 photo=NULL;
150 xfree(filename);
151 filename=NULL;
152 continue;
155 /* Build the packet */
156 build_attribute_subpkt(uid,1,photo,len,header,16);
157 parse_attribute_subpkts(uid);
158 make_attribute_uidname(uid, EXTRA_UID_NAME_SPACE);
160 /* Showing the photo is not safe when noninteractive since the
161 "user" may not be able to dismiss a viewer window! */
162 if(opt.command_fd==-1)
164 show_photos(uid->attribs,uid->numattribs,pk,NULL);
165 switch(cpr_get_answer_yes_no_quit("photoid.jpeg.okay",
166 _("Is this photo correct (y/N/q)? ")))
168 case -1:
169 goto scram;
170 case 0:
171 free_attributes(uid);
172 xfree(photo);
173 photo=NULL;
174 xfree(filename);
175 filename=NULL;
176 continue;
181 error=0;
182 uid->ref=1;
184 scram:
185 xfree(filename);
186 xfree(photo);
188 if(error)
190 free_attributes(uid);
191 xfree(uid);
192 return NULL;
195 return uid;
198 /* Returns 0 for error, 1 for valid */
199 int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len)
201 u16 headerlen;
203 if(attr->len<3)
204 return 0;
206 /* For historical reasons (i.e. "oops!"), the header length is
207 little endian. */
208 headerlen=(attr->data[1]<<8) | attr->data[0];
210 if(headerlen>attr->len)
211 return 0;
213 if(type && attr->len>=4)
215 if(attr->data[2]==1) /* header version 1 */
216 *type=attr->data[3];
217 else
218 *type=0;
221 *len=attr->len-headerlen;
223 if(*len==0)
224 return 0;
226 return 1;
229 /* style==0 for extension, 1 for name, 2 for MIME type. Remember that
230 the "name" style string could be used in a user ID name field, so
231 make sure it is not too big (see parse-packet.c:parse_attribute).
232 Extensions should be 3 characters long for the best cross-platform
233 compatibility. */
234 char *image_type_to_string(byte type,int style)
236 char *string;
238 switch(type)
240 case 1: /* jpeg */
241 if(style==0)
242 string="jpg";
243 else if(style==1)
244 string="jpeg";
245 else
246 string="image/jpeg";
247 break;
249 default:
250 if(style==0)
251 string="bin";
252 else if(style==1)
253 string="unknown";
254 else
255 string="image/x-unknown";
256 break;
259 return string;
262 #if !defined(FIXED_PHOTO_VIEWER) && !defined(DISABLE_PHOTO_VIEWER)
263 static const char *get_default_photo_command(void)
265 #if defined(_WIN32)
266 OSVERSIONINFO osvi;
268 memset(&osvi,0,sizeof(osvi));
269 osvi.dwOSVersionInfoSize=sizeof(osvi);
270 GetVersionEx(&osvi);
272 if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
273 return "start /w %i";
274 else
275 return "cmd /c start /w %i";
276 #elif defined(__APPLE__)
277 /* OS X. This really needs more than just __APPLE__. */
278 return "open %I";
279 #elif defined(__riscos__)
280 return "Filer_Run %I";
281 #else
282 return "xloadimage -fork -quiet -title 'KeyID 0x%k' stdin";
283 #endif
285 #endif
287 void show_photos(const struct user_attribute *attrs,
288 int count,PKT_public_key *pk,PKT_secret_key *sk)
290 #ifndef DISABLE_PHOTO_VIEWER
291 int i;
292 struct expando_args args;
293 u32 len;
294 u32 kid[2]={0,0};
296 memset(&args,0,sizeof(args));
297 args.pk=pk;
298 args.sk=sk;
300 if(pk)
301 keyid_from_pk(pk,kid);
302 else if(sk)
303 keyid_from_sk(sk,kid);
305 for(i=0;i<count;i++)
306 if(attrs[i].type==ATTRIB_IMAGE &&
307 parse_image_header(&attrs[i],&args.imagetype,&len))
309 char *command,*name;
310 struct exec_info *spawn;
311 int offset=attrs[i].len-len;
313 #ifdef FIXED_PHOTO_VIEWER
314 opt.photo_viewer=FIXED_PHOTO_VIEWER;
315 #else
316 if(!opt.photo_viewer)
317 opt.photo_viewer=get_default_photo_command();
318 #endif
320 /* make command grow */
321 command=pct_expando(opt.photo_viewer,&args);
322 if(!command)
323 goto fail;
325 name=xmalloc(16+strlen(EXTSEP_S)+
326 strlen(image_type_to_string(args.imagetype,0))+1);
328 /* Make the filename. Notice we are not using the image
329 encoding type for more than cosmetics. Most external image
330 viewers can handle a multitude of types, and even if one
331 cannot understand a particular type, we have no way to know
332 which. The spec permits this, by the way. -dms */
334 #ifdef USE_ONLY_8DOT3
335 sprintf(name,"%08lX" EXTSEP_S "%s",(ulong)kid[1],
336 image_type_to_string(args.imagetype,0));
337 #else
338 sprintf(name,"%08lX%08lX" EXTSEP_S "%s",(ulong)kid[0],(ulong)kid[1],
339 image_type_to_string(args.imagetype,0));
340 #endif
342 if(exec_write(&spawn,NULL,command,name,1,1)!=0)
344 xfree(name);
345 goto fail;
348 #ifdef __riscos__
349 riscos_set_filetype_by_mimetype(spawn->tempfile_in,
350 image_type_to_string(args.imagetype,2));
351 #endif
353 xfree(name);
355 fwrite(&attrs[i].data[offset],attrs[i].len-offset,1,spawn->tochild);
357 if(exec_read(spawn)!=0)
359 exec_finish(spawn);
360 goto fail;
363 if(exec_finish(spawn)!=0)
364 goto fail;
367 return;
369 fail:
370 log_error(_("unable to display photo ID!\n"));
371 #endif