2007-07-05 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / photoid.c
blobf330f2eab39bb1dc55e0f38cd87878effae281cb
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef _WIN32
25 # include <windows.h>
26 # ifndef VER_PLATFORM_WIN32_WINDOWS
27 # define VER_PLATFORM_WIN32_WINDOWS 1
28 # endif
29 #endif
31 #include "gpg.h"
32 #include "packet.h"
33 #include "status.h"
34 #include "exec.h"
35 #include "keydb.h"
36 #include "util.h"
37 #include "i18n.h"
38 #include "iobuf.h"
39 #include "options.h"
40 #include "main.h"
41 #include "photoid.h"
42 #include "ttyio.h"
44 /* Generate a new photo id packet, or return NULL if canceled */
45 PKT_user_id *
46 generate_photo_id(PKT_public_key *pk,const char *photo_name)
48 PKT_user_id *uid;
49 int error=1,i;
50 unsigned int len;
51 char *filename;
52 byte *photo=NULL;
53 byte header[16];
54 IOBUF file;
55 int overflow;
57 header[0]=0x10; /* little side of photo header length */
58 header[1]=0; /* big side of photo header length */
59 header[2]=1; /* 1 == version of photo header */
60 header[3]=1; /* 1 == JPEG */
62 for(i=4;i<16;i++) /* The reserved bytes */
63 header[i]=0;
65 #define EXTRA_UID_NAME_SPACE 71
66 uid=xmalloc_clear(sizeof(*uid)+71);
68 if(photo_name && *photo_name)
69 filename=make_filename(photo_name,(void *)NULL);
70 else
72 tty_printf(_("\nPick an image to use for your photo ID."
73 " The image must be a JPEG file.\n"
74 "Remember that the image is stored within your public key."
75 " If you use a\n"
76 "very large picture, your key will become very large"
77 " as well!\n"
78 "Keeping the image close to 240x288 is a good size"
79 " to use.\n"));
80 filename=NULL;
83 while(photo==NULL)
85 if(filename==NULL)
87 char *tempname;
89 tty_printf("\n");
91 tty_enable_completion(NULL);
93 tempname=cpr_get("photoid.jpeg.add",
94 _("Enter JPEG filename for photo ID: "));
96 tty_disable_completion();
98 filename=make_filename(tempname,(void *)NULL);
100 xfree(tempname);
102 if(strlen(filename)==0)
103 goto scram;
106 file=iobuf_open(filename);
107 if (file && is_secured_file (iobuf_get_fd (file)))
109 iobuf_close (file);
110 file = NULL;
111 errno = EPERM;
113 if(!file)
115 log_error(_("unable to open JPEG file `%s': %s\n"),
116 filename,strerror(errno));
117 xfree(filename);
118 filename=NULL;
119 continue;
123 len=iobuf_get_filelength(file, &overflow);
124 if(len>6144 || overflow)
126 tty_printf( _("This JPEG is really large (%d bytes) !\n"),len);
127 if(!cpr_get_answer_is_yes("photoid.jpeg.size",
128 _("Are you sure you want to use it? (y/N) ")))
130 iobuf_close(file);
131 xfree(filename);
132 filename=NULL;
133 continue;
137 photo=xmalloc(len);
138 iobuf_read(file,photo,len);
139 iobuf_close(file);
141 /* Is it a JPEG? */
142 if(photo[0]!=0xFF || photo[1]!=0xD8 ||
143 photo[6]!='J' || photo[7]!='F' || photo[8]!='I' || photo[9]!='F')
145 log_error(_("`%s' is not a JPEG file\n"),filename);
146 xfree(photo);
147 photo=NULL;
148 xfree(filename);
149 filename=NULL;
150 continue;
153 /* Build the packet */
154 build_attribute_subpkt(uid,1,photo,len,header,16);
155 parse_attribute_subpkts(uid);
156 make_attribute_uidname(uid, EXTRA_UID_NAME_SPACE);
158 /* Showing the photo is not safe when noninteractive since the
159 "user" may not be able to dismiss a viewer window! */
160 if(opt.command_fd==-1)
162 show_photos(uid->attribs,uid->numattribs,pk,NULL);
163 switch(cpr_get_answer_yes_no_quit("photoid.jpeg.okay",
164 _("Is this photo correct (y/N/q)? ")))
166 case -1:
167 goto scram;
168 case 0:
169 free_attributes(uid);
170 xfree(photo);
171 photo=NULL;
172 xfree(filename);
173 filename=NULL;
174 continue;
179 error=0;
180 uid->ref=1;
182 scram:
183 xfree(filename);
184 xfree(photo);
186 if(error)
188 free_attributes(uid);
189 xfree(uid);
190 return NULL;
193 return uid;
196 /* Returns 0 for error, 1 for valid */
197 int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len)
199 u16 headerlen;
201 if(attr->len<3)
202 return 0;
204 /* For historical reasons (i.e. "oops!"), the header length is
205 little endian. */
206 headerlen=(attr->data[1]<<8) | attr->data[0];
208 if(headerlen>attr->len)
209 return 0;
211 if(type && attr->len>=4)
213 if(attr->data[2]==1) /* header version 1 */
214 *type=attr->data[3];
215 else
216 *type=0;
219 *len=attr->len-headerlen;
221 if(*len==0)
222 return 0;
224 return 1;
227 /* style==0 for extension, 1 for name, 2 for MIME type. Remember that
228 the "name" style string could be used in a user ID name field, so
229 make sure it is not too big (see parse-packet.c:parse_attribute).
230 Extensions should be 3 characters long for the best cross-platform
231 compatibility. */
232 char *image_type_to_string(byte type,int style)
234 char *string;
236 switch(type)
238 case 1: /* jpeg */
239 if(style==0)
240 string="jpg";
241 else if(style==1)
242 string="jpeg";
243 else
244 string="image/jpeg";
245 break;
247 default:
248 if(style==0)
249 string="bin";
250 else if(style==1)
251 string="unknown";
252 else
253 string="image/x-unknown";
254 break;
257 return string;
260 #if !defined(FIXED_PHOTO_VIEWER) && !defined(DISABLE_PHOTO_VIEWER)
261 static const char *get_default_photo_command(void)
263 #if defined(_WIN32)
264 OSVERSIONINFO osvi;
266 memset(&osvi,0,sizeof(osvi));
267 osvi.dwOSVersionInfoSize=sizeof(osvi);
268 GetVersionEx(&osvi);
270 if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
271 return "start /w %i";
272 else
273 return "cmd /c start /w %i";
274 #elif defined(__APPLE__)
275 /* OS X. This really needs more than just __APPLE__. */
276 return "open %I";
277 #elif defined(__riscos__)
278 return "Filer_Run %I";
279 #else
280 return "xloadimage -fork -quiet -title 'KeyID 0x%k' stdin";
281 #endif
283 #endif
285 void show_photos(const struct user_attribute *attrs,
286 int count,PKT_public_key *pk,PKT_secret_key *sk)
288 #ifndef DISABLE_PHOTO_VIEWER
289 int i;
290 struct expando_args args;
291 u32 len;
292 u32 kid[2]={0,0};
294 memset(&args,0,sizeof(args));
295 args.pk=pk;
296 args.sk=sk;
298 if(pk)
299 keyid_from_pk(pk,kid);
300 else if(sk)
301 keyid_from_sk(sk,kid);
303 for(i=0;i<count;i++)
304 if(attrs[i].type==ATTRIB_IMAGE &&
305 parse_image_header(&attrs[i],&args.imagetype,&len))
307 char *command,*name;
308 struct exec_info *spawn;
309 int offset=attrs[i].len-len;
311 #ifdef FIXED_PHOTO_VIEWER
312 opt.photo_viewer=FIXED_PHOTO_VIEWER;
313 #else
314 if(!opt.photo_viewer)
315 opt.photo_viewer=get_default_photo_command();
316 #endif
318 /* make command grow */
319 command=pct_expando(opt.photo_viewer,&args);
320 if(!command)
321 goto fail;
323 name=xmalloc(16+strlen(EXTSEP_S)+
324 strlen(image_type_to_string(args.imagetype,0))+1);
326 /* Make the filename. Notice we are not using the image
327 encoding type for more than cosmetics. Most external image
328 viewers can handle a multitude of types, and even if one
329 cannot understand a particular type, we have no way to know
330 which. The spec permits this, by the way. -dms */
332 #ifdef USE_ONLY_8DOT3
333 sprintf(name,"%08lX" EXTSEP_S "%s",(ulong)kid[1],
334 image_type_to_string(args.imagetype,0));
335 #else
336 sprintf(name,"%08lX%08lX" EXTSEP_S "%s",(ulong)kid[0],(ulong)kid[1],
337 image_type_to_string(args.imagetype,0));
338 #endif
340 if(exec_write(&spawn,NULL,command,name,1,1)!=0)
342 xfree(name);
343 goto fail;
346 #ifdef __riscos__
347 riscos_set_filetype_by_mimetype(spawn->tempfile_in,
348 image_type_to_string(args.imagetype,2));
349 #endif
351 xfree(name);
353 fwrite(&attrs[i].data[offset],attrs[i].len-offset,1,spawn->tochild);
355 if(exec_read(spawn)!=0)
357 exec_finish(spawn);
358 goto fail;
361 if(exec_finish(spawn)!=0)
362 goto fail;
365 return;
367 fail:
368 log_error(_("unable to display photo ID!\n"));
369 #endif