chiark / gitweb /
dirmngr: New debug message on correctly initialized libdns.
[gnupg2.git] / g10 / photoid.c
1 /* photoid.c - photo ID handling code
2  * Copyright (C) 2001, 2002, 2005, 2006, 2008, 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef _WIN32
25 # ifdef HAVE_WINSOCK2_H
26 #  include <winsock2.h>
27 # endif
28 # include <windows.h>
29 # ifndef VER_PLATFORM_WIN32_WINDOWS
30 #  define VER_PLATFORM_WIN32_WINDOWS 1
31 # endif
32 #endif
33
34 #include "gpg.h"
35 #include "util.h"
36 #include "packet.h"
37 #include "status.h"
38 #include "exec.h"
39 #include "keydb.h"
40 #include "i18n.h"
41 #include "iobuf.h"
42 #include "options.h"
43 #include "main.h"
44 #include "photoid.h"
45 #include "ttyio.h"
46 #include "trustdb.h"
47
48 /* Generate a new photo id packet, or return NULL if canceled.
49    FIXME:  Should we add a duplicates check similar to generate_user_id? */
50 PKT_user_id *
51 generate_photo_id (ctrl_t ctrl, PKT_public_key *pk,const char *photo_name)
52 {
53   PKT_user_id *uid;
54   int error=1,i;
55   unsigned int len;
56   char *filename;
57   byte *photo=NULL;
58   byte header[16];
59   IOBUF file;
60   int overflow;
61
62   header[0]=0x10; /* little side of photo header length */
63   header[1]=0;    /* big side of photo header length */
64   header[2]=1;    /* 1 == version of photo header */
65   header[3]=1;    /* 1 == JPEG */
66
67   for(i=4;i<16;i++) /* The reserved bytes */
68     header[i]=0;
69
70 #define EXTRA_UID_NAME_SPACE 71
71   uid=xmalloc_clear(sizeof(*uid)+71);
72
73   if(photo_name && *photo_name)
74     filename=make_filename(photo_name,(void *)NULL);
75   else
76     {
77       tty_printf(_("\nPick an image to use for your photo ID."
78                    "  The image must be a JPEG file.\n"
79                    "Remember that the image is stored within your public key."
80                    "  If you use a\n"
81                    "very large picture, your key will become very large"
82                    " as well!\n"
83                    "Keeping the image close to 240x288 is a good size"
84                    " to use.\n"));
85       filename=NULL;
86     }
87
88   while(photo==NULL)
89     {
90       if(filename==NULL)
91         {
92           char *tempname;
93
94           tty_printf("\n");
95
96           tty_enable_completion(NULL);
97
98           tempname=cpr_get("photoid.jpeg.add",
99                            _("Enter JPEG filename for photo ID: "));
100
101           tty_disable_completion();
102
103           filename=make_filename(tempname,(void *)NULL);
104
105           xfree(tempname);
106
107           if(strlen(filename)==0)
108             goto scram;
109         }
110
111       file=iobuf_open(filename);
112       if (file && is_secured_file (iobuf_get_fd (file)))
113         {
114           iobuf_close (file);
115           file = NULL;
116           gpg_err_set_errno (EPERM);
117         }
118       if(!file)
119         {
120           log_error(_("unable to open JPEG file '%s': %s\n"),
121                     filename,strerror(errno));
122           xfree(filename);
123           filename=NULL;
124           continue;
125         }
126
127
128       len=iobuf_get_filelength(file, &overflow);
129       if(len>6144 || overflow)
130         {
131           tty_printf( _("This JPEG is really large (%d bytes) !\n"),len);
132           if(!cpr_get_answer_is_yes("photoid.jpeg.size",
133                             _("Are you sure you want to use it? (y/N) ")))
134           {
135             iobuf_close(file);
136             xfree(filename);
137             filename=NULL;
138             continue;
139           }
140         }
141
142       photo=xmalloc(len);
143       iobuf_read(file,photo,len);
144       iobuf_close(file);
145
146       /* Is it a JPEG? */
147       if(photo[0]!=0xFF || photo[1]!=0xD8)
148         {
149           log_error(_("'%s' is not a JPEG file\n"),filename);
150           xfree(photo);
151           photo=NULL;
152           xfree(filename);
153           filename=NULL;
154           continue;
155         }
156
157       /* Build the packet */
158       build_attribute_subpkt(uid,1,photo,len,header,16);
159       parse_attribute_subpkts(uid);
160       make_attribute_uidname(uid, EXTRA_UID_NAME_SPACE);
161
162       /* Showing the photo is not safe when noninteractive since the
163          "user" may not be able to dismiss a viewer window! */
164       if(opt.command_fd==-1)
165         {
166           show_photos (ctrl, uid->attribs, uid->numattribs, pk, uid);
167           switch(cpr_get_answer_yes_no_quit("photoid.jpeg.okay",
168                                          _("Is this photo correct (y/N/q)? ")))
169             {
170             case -1:
171               goto scram;
172             case 0:
173               free_attributes(uid);
174               xfree(photo);
175               photo=NULL;
176               xfree(filename);
177               filename=NULL;
178               continue;
179             }
180         }
181     }
182
183   error=0;
184   uid->ref=1;
185
186  scram:
187   xfree(filename);
188   xfree(photo);
189
190   if(error)
191     {
192       free_attributes(uid);
193       xfree(uid);
194       return NULL;
195     }
196
197   return uid;
198 }
199
200 /* Returns 0 for error, 1 for valid */
201 int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len)
202 {
203   u16 headerlen;
204
205   if(attr->len<3)
206     return 0;
207
208   /* For historical reasons (i.e. "oops!"), the header length is
209      little endian. */
210   headerlen=(attr->data[1]<<8) | attr->data[0];
211
212   if(headerlen>attr->len)
213     return 0;
214
215   if(type && attr->len>=4)
216     {
217       if(attr->data[2]==1) /* header version 1 */
218         *type=attr->data[3];
219       else
220         *type=0;
221     }
222
223   *len=attr->len-headerlen;
224
225   if(*len==0)
226     return 0;
227
228   return 1;
229 }
230
231 /* style==0 for extension, 1 for name, 2 for MIME type.  Remember that
232    the "name" style string could be used in a user ID name field, so
233    make sure it is not too big (see parse-packet.c:parse_attribute).
234    Extensions should be 3 characters long for the best cross-platform
235    compatibility. */
236 char *image_type_to_string(byte type,int style)
237 {
238   char *string;
239
240   switch(type)
241     {
242     case 1: /* jpeg */
243       if(style==0)
244         string="jpg";
245       else if(style==1)
246         string="jpeg";
247       else
248         string="image/jpeg";
249       break;
250
251     default:
252       if(style==0)
253         string="bin";
254       else if(style==1)
255         string="unknown";
256       else
257         string="image/x-unknown";
258       break;
259     }
260
261   return string;
262 }
263
264 #if !defined(FIXED_PHOTO_VIEWER) && !defined(DISABLE_PHOTO_VIEWER)
265 static const char *get_default_photo_command(void)
266 {
267 #if defined(_WIN32)
268   OSVERSIONINFO osvi;
269
270   memset(&osvi,0,sizeof(osvi));
271   osvi.dwOSVersionInfoSize=sizeof(osvi);
272   GetVersionEx(&osvi);
273
274   if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
275     return "start /w %i";
276   else
277     return "cmd /c start /w %i";
278 #elif defined(__APPLE__)
279   /* OS X.  This really needs more than just __APPLE__. */
280   return "open %I";
281 #elif defined(__riscos__)
282   return "Filer_Run %I";
283 #else
284   return "xloadimage -fork -quiet -title 'KeyID 0x%k' stdin";
285 #endif
286 }
287 #endif
288
289
290 void
291 show_photos (ctrl_t ctrl, const struct user_attribute *attrs, int count,
292              PKT_public_key *pk, PKT_user_id *uid)
293 {
294 #ifdef DISABLE_PHOTO_VIEWER
295   (void)attrs;
296   (void)count;
297   (void)pk;
298   (void)uid;
299 #else /*!DISABLE_PHOTO_VIEWER*/
300   int i;
301   struct expando_args args;
302   u32 len;
303   u32 kid[2]={0,0};
304
305   memset (&args, 0, sizeof(args));
306   args.pk = pk;
307   args.validity_info = get_validity_info (ctrl, NULL, pk, uid);
308   args.validity_string = get_validity_string (ctrl, pk, uid);
309   namehash_from_uid (uid);
310   args.namehash = uid->namehash;
311
312   if (pk)
313     keyid_from_pk (pk, kid);
314
315   for(i=0;i<count;i++)
316     if(attrs[i].type==ATTRIB_IMAGE &&
317        parse_image_header(&attrs[i],&args.imagetype,&len))
318       {
319         char *command,*name;
320         struct exec_info *spawn;
321         int offset=attrs[i].len-len;
322
323 #ifdef FIXED_PHOTO_VIEWER
324         opt.photo_viewer=FIXED_PHOTO_VIEWER;
325 #else
326         if(!opt.photo_viewer)
327           opt.photo_viewer=get_default_photo_command();
328 #endif
329
330         /* make command grow */
331         command=pct_expando(opt.photo_viewer,&args);
332         if(!command)
333           goto fail;
334
335         name=xmalloc(16+strlen(EXTSEP_S)+
336                      strlen(image_type_to_string(args.imagetype,0))+1);
337
338         /* Make the filename.  Notice we are not using the image
339            encoding type for more than cosmetics.  Most external image
340            viewers can handle a multitude of types, and even if one
341            cannot understand a particular type, we have no way to know
342            which.  The spec permits this, by the way. -dms */
343
344 #ifdef USE_ONLY_8DOT3
345         sprintf(name,"%08lX" EXTSEP_S "%s",(ulong)kid[1],
346                 image_type_to_string(args.imagetype,0));
347 #else
348         sprintf(name,"%08lX%08lX" EXTSEP_S "%s",(ulong)kid[0],(ulong)kid[1],
349                 image_type_to_string(args.imagetype,0));
350 #endif
351
352         if(exec_write(&spawn,NULL,command,name,1,1)!=0)
353           {
354             xfree(name);
355             goto fail;
356           }
357
358 #ifdef __riscos__
359         riscos_set_filetype_by_mimetype(spawn->tempfile_in,
360                                         image_type_to_string(args.imagetype,2));
361 #endif
362
363         xfree(name);
364
365         fwrite(&attrs[i].data[offset],attrs[i].len-offset,1,spawn->tochild);
366
367         if(exec_read(spawn)!=0)
368           {
369             exec_finish(spawn);
370             goto fail;
371           }
372
373         if(exec_finish(spawn)!=0)
374           goto fail;
375       }
376
377   return;
378
379  fail:
380   log_error(_("unable to display photo ID!\n"));
381 #endif /*!DISABLE_PHOTO_VIEWER*/
382 }