Imported Upstream version 2.4.3
[platform/upstream/gpg2.git] / agent / call-pinentry.c
1 /* call-pinentry.c - Spawn the pinentry to query stuff from the user
2  * Copyright (C) 2001, 2002, 2004, 2007, 2008,
3  *               2010  Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #ifndef HAVE_W32_SYSTEM
30 # include <sys/wait.h>
31 # include <sys/types.h>
32 # include <signal.h>
33 # include <sys/utsname.h>
34 #endif
35 #include <npth.h>
36
37 #include "agent.h"
38 #include <assuan.h>
39 #include "../common/sysutils.h"
40 #include "../common/i18n.h"
41 #include "../common/zb32.h"
42
43 #ifdef _POSIX_OPEN_MAX
44 #define MAX_OPEN_FDS _POSIX_OPEN_MAX
45 #else
46 #define MAX_OPEN_FDS 20
47 #endif
48
49
50 /* Because access to the pinentry must be serialized (it is and shall
51    be a global mutually exclusive dialog) we better timeout pending
52    requests after some time.  1 minute seem to be a reasonable
53    time. */
54 #define LOCK_TIMEOUT  (1*60)
55
56 /* Define the number of bits to use for a generated pin.  The
57  * passphrase will be rendered as zbase32 which results for 150 bits
58  * in a string of 30 characters.  That fits nicely into the 5
59  * character blocking which pinentry can do.  128 bits would actually
60  * be sufficient but can't be formatted nicely.  Please do not change
61  * this value because pattern check files may let such passwords
62  * always pass. */
63 #define DEFAULT_GENPIN_BITS 150
64
65 /* The assuan context of the current pinentry. */
66 static assuan_context_t entry_ctx;
67
68 /* A list of features of the current pinentry.  */
69 static struct
70 {
71   /* The Pinentry support RS+US tabbing.  This means that a RS (0x1e)
72    * starts a new tabbing block in which a US (0x1f) followed by a
73    * colon marks a colon.  A pinentry can use this to pretty print
74    * name value pairs.  */
75   unsigned int tabbing:1;
76 } entry_features;
77
78
79 /* A mutex used to serialize access to the pinentry. */
80 static npth_mutex_t entry_lock;
81
82 /* The thread ID of the popup working thread. */
83 static npth_t  popup_tid;
84
85 /* A flag used in communication between the popup working thread and
86    its stop function. */
87 static int popup_finished;
88
89
90
91 /* Data to be passed to our callbacks, */
92 struct entry_parm_s
93 {
94   int lines;
95   size_t size;
96   unsigned char *buffer;
97   int status;
98   unsigned int constraints_flags;
99 };
100
101
102
103 \f
104 /* This function must be called once to initialize this module.  This
105    has to be done before a second thread is spawned.  We can't do the
106    static initialization because Pth emulation code might not be able
107    to do a static init; in particular, it is not possible for W32. */
108 void
109 initialize_module_call_pinentry (void)
110 {
111   static int initialized;
112   int err;
113
114   if (!initialized)
115     {
116       err = npth_mutex_init (&entry_lock, NULL);
117       if (err)
118         log_fatal ("error initializing mutex: %s\n", strerror (err));
119
120       initialized = 1;
121     }
122 }
123
124
125
126 /* This function may be called to print information pertaining to the
127    current state of this module to the log. */
128 void
129 agent_query_dump_state (void)
130 {
131   log_info ("agent_query_dump_state: entry_ctx=%p pid=%ld popup_tid=%p\n",
132             entry_ctx, (long)assuan_get_pid (entry_ctx), (void*)popup_tid);
133 }
134
135 /* Called to make sure that a popup window owned by the current
136    connection gets closed. */
137 void
138 agent_reset_query (ctrl_t ctrl)
139 {
140   if (entry_ctx && popup_tid && ctrl->pinentry_active)
141     {
142       agent_popup_message_stop (ctrl);
143     }
144 }
145
146
147 /* Unlock the pinentry so that another thread can start one and
148    disconnect that pinentry - we do this after the unlock so that a
149    stalled pinentry does not block other threads.  Fixme: We should
150    have a timeout in Assuan for the disconnect operation. */
151 static gpg_error_t
152 unlock_pinentry (ctrl_t ctrl, gpg_error_t rc)
153 {
154   assuan_context_t ctx = entry_ctx;
155   int err;
156
157   if (rc)
158     {
159       if (DBG_IPC)
160         log_debug ("error calling pinentry: %s <%s>\n",
161                    gpg_strerror (rc), gpg_strsource (rc));
162
163       /* Change the source of the error to pinentry so that the final
164          consumer of the error code knows that the problem is with
165          pinentry.  For backward compatibility we do not do that for
166          some common error codes.  */
167       switch (gpg_err_code (rc))
168         {
169         case GPG_ERR_NO_PIN_ENTRY:
170         case GPG_ERR_CANCELED:
171         case GPG_ERR_FULLY_CANCELED:
172         case GPG_ERR_ASS_UNKNOWN_INQUIRE:
173         case GPG_ERR_ASS_TOO_MUCH_DATA:
174         case GPG_ERR_NO_PASSPHRASE:
175         case GPG_ERR_BAD_PASSPHRASE:
176         case GPG_ERR_BAD_PIN:
177           break;
178
179         case GPG_ERR_CORRUPTED_PROTECTION:
180           /* This comes from gpg-agent.  */
181           break;
182
183         default:
184           rc = gpg_err_make (GPG_ERR_SOURCE_PINENTRY, gpg_err_code (rc));
185           break;
186         }
187     }
188
189   if (--ctrl->pinentry_active == 0)
190     {
191       entry_ctx = NULL;
192       err = npth_mutex_unlock (&entry_lock);
193       if (err)
194         {
195           log_error ("failed to release the entry lock: %s\n", strerror (err));
196           if (!rc)
197             rc = gpg_error_from_errno (err);
198         }
199       assuan_release (ctx);
200     }
201   return rc;
202 }
203
204
205 /* Helper for at_fork_cb which can also be called by the parent to
206  * show which envvars will be set.  */
207 static void
208 atfork_core (ctrl_t ctrl, int debug_mode)
209 {
210   int iterator = 0;
211   const char *name, *assname, *value;
212
213   while ((name = session_env_list_stdenvnames (&iterator, &assname)))
214     {
215       /* For all new envvars (!ASSNAME) and the two medium old ones
216        * which do have an assuan name but are conveyed using
217        * environment variables, update the environment of the forked
218        * process.  We also pass DISPLAY despite that --display is also
219        * used when exec-ing the pinentry.  The reason is that for
220        * example the qt5ct tool does not have any arguments and thus
221        * relies on the DISPLAY envvar.  The use case here is a global
222        * envvar like "QT_QPA_PLATFORMTHEME=qt5ct" which for example is
223        * useful when using the Qt pinentry under GNOME or XFCE.
224        */
225       if (!assname
226           || (!opt.keep_display && !strcmp (name, "DISPLAY"))
227           || !strcmp (name, "XAUTHORITY")
228           || !strcmp (name, "PINENTRY_USER_DATA"))
229         {
230           value = session_env_getenv (ctrl->session_env, name);
231           if (value)
232             {
233               if (debug_mode)
234                 log_debug ("pinentry: atfork used setenv(%s,%s)\n",name,value);
235               else
236                 gnupg_setenv (name, value, 1);
237             }
238         }
239     }
240 }
241
242
243 /* To make sure we leave no secrets in our image after forking of the
244    pinentry, we use this callback. */
245 static void
246 atfork_cb (void *opaque, int where)
247 {
248   ctrl_t ctrl = opaque;
249
250   if (!where)
251     {
252       gcry_control (GCRYCTL_TERM_SECMEM);
253       atfork_core (ctrl, 0);
254     }
255 }
256
257
258 /* Status line callback for the FEATURES status.  */
259 static gpg_error_t
260 getinfo_features_cb (void *opaque, const char *line)
261 {
262   const char *args;
263   char **tokens;
264   int i;
265
266   (void)opaque;
267
268   if ((args = has_leading_keyword (line, "FEATURES")))
269     {
270       tokens = strtokenize (args, " ");
271       if (!tokens)
272         return gpg_error_from_syserror ();
273       for (i=0; tokens[i]; i++)
274         if (!strcmp (tokens[i], "tabbing"))
275           entry_features.tabbing = 1;
276       xfree (tokens);
277     }
278
279   return 0;
280 }
281
282
283 static gpg_error_t
284 getinfo_pid_cb (void *opaque, const void *buffer, size_t length)
285 {
286   unsigned long *pid = opaque;
287   char pidbuf[50];
288
289   /* There is only the pid in the server's response.  */
290   if (length >= sizeof pidbuf)
291     length = sizeof pidbuf -1;
292   if (length)
293     {
294       strncpy (pidbuf, buffer, length);
295       pidbuf[length] = 0;
296       *pid = strtoul (pidbuf, NULL, 10);
297     }
298   return 0;
299 }
300
301
302 /* Fork off the pin entry if this has not already been done.  Note,
303    that this function must always be used to acquire the lock for the
304    pinentry - we will serialize _all_ pinentry calls.
305  */
306 static gpg_error_t
307 start_pinentry (ctrl_t ctrl)
308 {
309   int rc = 0;
310   const char *full_pgmname;
311   const char *pgmname;
312   assuan_context_t ctx;
313   const char *argv[5];
314   assuan_fd_t no_close_list[3];
315   int i;
316   const char *tmpstr;
317   unsigned long pinentry_pid;
318   const char *value;
319   struct timespec abstime;
320   char *flavor_version;
321   int err;
322
323   if (ctrl->pinentry_active)
324     {
325       /* It's trying to use pinentry recursively.  In this situation,
326          the thread holds ENTRY_LOCK already.  */
327       ctrl->pinentry_active++;
328       return 0;
329     }
330
331   npth_clock_gettime (&abstime);
332   abstime.tv_sec += LOCK_TIMEOUT;
333   err = npth_mutex_timedlock (&entry_lock, &abstime);
334   if (err)
335     {
336       if (err == ETIMEDOUT)
337         rc = gpg_error (GPG_ERR_TIMEOUT);
338       else
339         rc = gpg_error_from_errno (rc);
340       log_error (_("failed to acquire the pinentry lock: %s\n"),
341                  gpg_strerror (rc));
342       return rc;
343     }
344
345   if (entry_ctx)
346     return 0;
347
348   if (opt.verbose)
349     log_info ("starting a new PIN Entry\n");
350
351 #ifdef HAVE_W32_SYSTEM
352   fflush (stdout);
353   fflush (stderr);
354 #endif
355   if (fflush (NULL))
356     {
357 #ifndef HAVE_W32_SYSTEM
358       gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
359 #endif
360       log_error ("error flushing pending output: %s\n", strerror (errno));
361       /* At least Windows XP fails here with EBADF.  According to docs
362          and Wine an fflush(NULL) is the same as _flushall.  However
363          the Wine implementation does not flush stdin,stdout and stderr
364          - see above.  Let's try to ignore the error. */
365 #ifndef HAVE_W32_SYSTEM
366       return unlock_pinentry (ctrl, tmperr);
367 #endif
368     }
369
370   full_pgmname = opt.pinentry_program;
371   if (!full_pgmname || !*full_pgmname)
372     full_pgmname = gnupg_module_name (GNUPG_MODULE_NAME_PINENTRY);
373   if ( !(pgmname = strrchr (full_pgmname, '/')))
374     pgmname = full_pgmname;
375   else
376     pgmname++;
377
378   /* OS X needs the entire file name in argv[0], so that it can locate
379      the resource bundle.  For other systems we stick to the usual
380      convention of supplying only the name of the program.  */
381 #ifdef __APPLE__
382   argv[0] = full_pgmname;
383 #else /*!__APPLE__*/
384   argv[0] = pgmname;
385 #endif /*__APPLE__*/
386
387   if (!opt.keep_display
388       && (value = session_env_getenv (ctrl->session_env, "DISPLAY")))
389     {
390       argv[1] = "--display";
391       argv[2] = value;
392       argv[3] = NULL;
393     }
394   else
395     argv[1] = NULL;
396
397   i=0;
398   if (!opt.running_detached)
399     no_close_list[i++] = assuan_fd_from_posix_fd (fileno (stderr));
400   no_close_list[i] = ASSUAN_INVALID_FD;
401
402   rc = assuan_new (&ctx);
403   if (rc)
404     {
405       log_error ("can't allocate assuan context: %s\n", gpg_strerror (rc));
406       return rc;
407     }
408
409   ctrl->pinentry_active = 1;
410   entry_ctx = ctx;
411
412   /* We don't want to log the pinentry communication to make the logs
413      easier to read.  We might want to add a new debug option to enable
414      pinentry logging.  */
415 #ifdef ASSUAN_NO_LOGGING
416   assuan_set_flag (ctx, ASSUAN_NO_LOGGING, !opt.debug_pinentry);
417 #endif
418
419   /* Connect to the pinentry and perform initial handshaking.  Note
420      that atfork is used to change the environment for pinentry.  We
421      start the server in detached mode to suppress the console window
422      under Windows.  */
423   rc = assuan_pipe_connect (entry_ctx, full_pgmname, argv,
424                             no_close_list, atfork_cb, ctrl,
425                             ASSUAN_PIPE_CONNECT_DETACHED);
426   if (rc)
427     {
428       log_error ("can't connect to the PIN entry module '%s': %s\n",
429                  full_pgmname, gpg_strerror (rc));
430       return unlock_pinentry (ctrl, gpg_error (GPG_ERR_NO_PIN_ENTRY));
431     }
432
433   if (DBG_IPC)
434     log_debug ("connection to PIN entry established\n");
435
436   if (opt.debug_pinentry)
437     atfork_core (ctrl, 1); /* Just show the envvars set after the fork.  */
438
439   value = session_env_getenv (ctrl->session_env, "PINENTRY_USER_DATA");
440   if (value != NULL)
441     {
442       char *optstr;
443       if (asprintf (&optstr, "OPTION pinentry-user-data=%s", value) < 0 )
444         return unlock_pinentry (ctrl, out_of_core ());
445       rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
446                             NULL);
447       xfree (optstr);
448       if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
449         return unlock_pinentry (ctrl, rc);
450     }
451
452   rc = assuan_transact (entry_ctx,
453                         opt.no_grab? "OPTION no-grab":"OPTION grab",
454                         NULL, NULL, NULL, NULL, NULL, NULL);
455   if (rc)
456     {
457       if (gpg_err_code (rc) == GPG_ERR_NOT_SUPPORTED
458           || gpg_err_code (rc) == GPG_ERR_UNKNOWN_OPTION)
459         {
460           if (opt.verbose)
461             log_info ("Option no-grab/grab is ignored by pinentry.\n");
462           /* Keep going even if the feature is not supported.  */
463         }
464       else
465         return unlock_pinentry (ctrl, rc);
466     }
467
468   value = session_env_getenv (ctrl->session_env, "GPG_TTY");
469   if (value)
470     {
471       char *optstr;
472       if (asprintf (&optstr, "OPTION ttyname=%s", value) < 0 )
473         return unlock_pinentry (ctrl, out_of_core ());
474       rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
475                             NULL);
476       xfree (optstr);
477       if (rc)
478         return unlock_pinentry (ctrl, rc);
479     }
480   value = session_env_getenv (ctrl->session_env, "TERM");
481   if (value && *value)
482     {
483       char *optstr;
484       if (asprintf (&optstr, "OPTION ttytype=%s", value) < 0 )
485         return unlock_pinentry (ctrl, out_of_core ());
486       rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
487                             NULL);
488       xfree (optstr);
489       if (rc)
490         return unlock_pinentry (ctrl, rc);
491     }
492   if (ctrl->lc_ctype)
493     {
494       char *optstr;
495       if (asprintf (&optstr, "OPTION lc-ctype=%s", ctrl->lc_ctype) < 0 )
496         return unlock_pinentry (ctrl, out_of_core ());
497       rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
498                             NULL);
499       xfree (optstr);
500       if (rc)
501         return unlock_pinentry (ctrl, rc);
502     }
503   if (ctrl->lc_messages)
504     {
505       char *optstr;
506       if (asprintf (&optstr, "OPTION lc-messages=%s", ctrl->lc_messages) < 0 )
507         return unlock_pinentry (ctrl, out_of_core ());
508       rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
509                             NULL);
510       xfree (optstr);
511       if (rc)
512         return unlock_pinentry (ctrl, rc);
513     }
514
515
516   if (opt.allow_external_cache)
517     {
518       /* Indicate to the pinentry that it may read from an external cache.
519
520          It is essential that the pinentry respect this.  If the
521          cached password is not up to date and retry == 1, then, using
522          a version of GPG Agent that doesn't support this, won't issue
523          another pin request and the user won't get a chance to
524          correct the password.  */
525       rc = assuan_transact (entry_ctx, "OPTION allow-external-password-cache",
526                             NULL, NULL, NULL, NULL, NULL, NULL);
527       if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
528         return unlock_pinentry (ctrl, rc);
529     }
530
531   if (opt.allow_emacs_pinentry)
532     {
533       /* Indicate to the pinentry that it may read passphrase through
534          Emacs minibuffer, if possible.  */
535       rc = assuan_transact (entry_ctx, "OPTION allow-emacs-prompt",
536                             NULL, NULL, NULL, NULL, NULL, NULL);
537       if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
538         return unlock_pinentry (ctrl, rc);
539     }
540
541
542   {
543     /* Provide a few default strings for use by the pinentries.  This
544      * may help a pinentry to avoid implementing localization code.
545      * Note that gpg-agent has been set to utf-8 so that the strings
546      * are in the expected encoding.  */
547     static const struct { const char *key, *value; int what; } tbl[] = {
548       /* TRANSLATORS: These are labels for buttons etc as used in
549        * Pinentries.  In your translation copy the text before the
550        * second vertical bar verbatim; translate only the following
551        * text.  An underscore indicates that the next letter should be
552        * used as an accelerator.  Double the underscore to have
553        * pinentry display a literal underscore.   */
554       { "ok",     N_("|pinentry-label|_OK") },
555       { "cancel", N_("|pinentry-label|_Cancel") },
556       { "yes",    N_("|pinentry-label|_Yes") },
557       { "no",     N_("|pinentry-label|_No") },
558       { "prompt", N_("|pinentry-label|PIN:") },
559       { "pwmngr", N_("|pinentry-label|_Save in password manager"), 1 },
560       { "cf-visi",N_("Do you really want to make your "
561                      "passphrase visible on the screen?") },
562       { "tt-visi",N_("|pinentry-tt|Make passphrase visible") },
563       { "tt-hide",N_("|pinentry-tt|Hide passphrase") },
564       { "capshint", N_("Caps Lock is on") },
565       { NULL, NULL}
566     };
567     char *optstr;
568     int idx;
569     const char *s, *s2;
570
571     for (idx=0; tbl[idx].key; idx++)
572       {
573         if (!opt.allow_external_cache && tbl[idx].what == 1)
574           continue;  /* No need for it.  */
575         s = L_(tbl[idx].value);
576         if (*s == '|' && (s2=strchr (s+1,'|')))
577           s = s2+1;
578         if (asprintf (&optstr, "OPTION default-%s=%s", tbl[idx].key, s) < 0 )
579           return unlock_pinentry (ctrl, out_of_core ());
580         assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
581                          NULL);
582         xfree (optstr);
583       }
584   }
585
586   /* Tell the pinentry that we would prefer that the given character
587      is used as the invisible character by the entry widget.  */
588   if (opt.pinentry_invisible_char)
589     {
590       char *optstr;
591       if ((optstr = xtryasprintf ("OPTION invisible-char=%s",
592                                   opt.pinentry_invisible_char)))
593         {
594           assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
595                            NULL);
596           /* We ignore errors because this is just a fancy thing and
597              older pinentries do not support this feature.  */
598           xfree (optstr);
599         }
600     }
601
602   if (opt.pinentry_timeout)
603     {
604       char *optstr;
605       if ((optstr = xtryasprintf ("SETTIMEOUT %lu", opt.pinentry_timeout)))
606         {
607           assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
608                            NULL);
609           /* We ignore errors because this is just a fancy thing.  */
610           xfree (optstr);
611         }
612     }
613
614   /* Tell the pinentry the name of a file it shall touch after having
615      messed with the tty.  This is optional and only supported by
616      newer pinentries and thus we do no error checking. */
617   tmpstr = opt.pinentry_touch_file;
618   if (tmpstr && !strcmp (tmpstr, "/dev/null"))
619     tmpstr = NULL;
620   else if (!tmpstr)
621     tmpstr = get_agent_socket_name ();
622   if (tmpstr)
623     {
624       char *optstr;
625
626       if (asprintf (&optstr, "OPTION touch-file=%s", tmpstr ) < 0 )
627         ;
628       else
629         {
630           assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
631                            NULL);
632           xfree (optstr);
633         }
634     }
635
636   /* Tell Pinentry about our client.  */
637   if (ctrl->client_pid)
638     {
639       char *optstr;
640       const char *nodename = "";
641
642 #ifndef HAVE_W32_SYSTEM
643       struct utsname utsbuf;
644       if (!uname (&utsbuf))
645         nodename = utsbuf.nodename;
646 #endif /*!HAVE_W32_SYSTEM*/
647
648       if ((optstr = xtryasprintf ("OPTION owner=%lu/%d %s",
649                                   ctrl->client_pid, ctrl->client_uid,
650                                   nodename)))
651         {
652           assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
653                            NULL);
654           /* We ignore errors because this is just a fancy thing and
655              older pinentries do not support this feature.  */
656           xfree (optstr);
657         }
658     }
659
660
661   /* Ask the pinentry for its version and flavor and store that as a
662    * string in MB.  This information is useful for helping users to
663    * figure out Pinentry problems.  Note that "flavor" may also return
664    * a status line with the features; we use a dedicated handler for
665    * that.  */
666   {
667     membuf_t mb;
668
669     init_membuf (&mb, 256);
670     if (assuan_transact (entry_ctx, "GETINFO flavor",
671                          put_membuf_cb, &mb,
672                          NULL, NULL,
673                          getinfo_features_cb, NULL))
674       put_membuf_str (&mb, "unknown");
675     put_membuf_str (&mb, " ");
676     if (assuan_transact (entry_ctx, "GETINFO version",
677                          put_membuf_cb, &mb, NULL, NULL, NULL, NULL))
678       put_membuf_str (&mb, "unknown");
679     put_membuf_str (&mb, " ");
680     if (assuan_transact (entry_ctx, "GETINFO ttyinfo",
681                          put_membuf_cb, &mb, NULL, NULL, NULL, NULL))
682       put_membuf_str (&mb, "? ? ?");
683     put_membuf (&mb, "", 1);
684     flavor_version = get_membuf (&mb, NULL);
685   }
686
687
688   /* Now ask the Pinentry for its PID.  If the Pinentry is new enough
689      it will send the pid back and we will use an inquire to notify
690      our client.  The client may answer the inquiry either with END or
691      with CAN to cancel the pinentry. */
692   rc = assuan_transact (entry_ctx, "GETINFO pid",
693                         getinfo_pid_cb, &pinentry_pid,
694                         NULL, NULL, NULL, NULL);
695   if (rc)
696     {
697       log_info ("You may want to update to a newer pinentry\n");
698       rc = 0;
699     }
700   else if (!rc && pinentry_pid == (unsigned long)(-1L))
701     log_error ("pinentry did not return a PID\n");
702   else
703     {
704       rc = agent_inq_pinentry_launched (ctrl, pinentry_pid, flavor_version);
705       if (gpg_err_code (rc) == GPG_ERR_CANCELED
706           || gpg_err_code (rc) == GPG_ERR_FULLY_CANCELED)
707         return unlock_pinentry (ctrl, gpg_err_make (GPG_ERR_SOURCE_DEFAULT,
708                                                     gpg_err_code (rc)));
709       rc = 0;
710     }
711
712   xfree (flavor_version);
713
714   return rc;
715 }
716
717
718 /* Returns True if the pinentry is currently active. If WAITSECONDS is
719    greater than zero the function will wait for this many seconds
720    before returning.  */
721 int
722 pinentry_active_p (ctrl_t ctrl, int waitseconds)
723 {
724   int err;
725   (void)ctrl;
726
727   if (waitseconds > 0)
728     {
729       struct timespec abstime;
730       int rc;
731
732       npth_clock_gettime (&abstime);
733       abstime.tv_sec += waitseconds;
734       err = npth_mutex_timedlock (&entry_lock, &abstime);
735       if (err)
736         {
737           if (err == ETIMEDOUT)
738             rc = gpg_error (GPG_ERR_TIMEOUT);
739           else
740             rc = gpg_error (GPG_ERR_INTERNAL);
741           return rc;
742         }
743     }
744   else
745     {
746       err = npth_mutex_trylock (&entry_lock);
747       if (err)
748         return gpg_error (GPG_ERR_LOCKED);
749     }
750
751   err = npth_mutex_unlock (&entry_lock);
752   if (err)
753     log_error ("failed to release the entry lock at %d: %s\n", __LINE__,
754                strerror (errno));
755   return 0;
756 }
757
758
759 static gpg_error_t
760 getpin_cb (void *opaque, const void *buffer, size_t length)
761 {
762   struct entry_parm_s *parm = opaque;
763
764   if (!buffer)
765     return 0;
766
767   /* we expect the pin to fit on one line */
768   if (parm->lines || length >= parm->size)
769     return gpg_error (GPG_ERR_ASS_TOO_MUCH_DATA);
770
771   /* fixme: we should make sure that the assuan buffer is allocated in
772      secure memory or read the response byte by byte */
773   memcpy (parm->buffer, buffer, length);
774   parm->buffer[length] = 0;
775   parm->lines++;
776   return 0;
777 }
778
779
780 static int
781 all_digitsp( const char *s)
782 {
783   for (; *s && *s >= '0' && *s <= '9'; s++)
784     ;
785   return !*s;
786 }
787
788
789 /* Return a new malloced string by unescaping the string S.  Escaping
790    is percent escaping and '+'/space mapping.  A binary Nul will
791    silently be replaced by a 0xFF.  Function returns NULL to indicate
792    an out of memory status.  Parsing stops at the end of the string or
793    a white space character. */
794 static char *
795 unescape_passphrase_string (const unsigned char *s)
796 {
797   char *buffer, *d;
798
799   buffer = d = xtrymalloc_secure (strlen ((const char*)s)+1);
800   if (!buffer)
801     return NULL;
802   while (*s && !spacep (s))
803     {
804       if (*s == '%' && s[1] && s[2])
805         {
806           s++;
807           *d = xtoi_2 (s);
808           if (!*d)
809             *d = '\xff';
810           d++;
811           s += 2;
812         }
813       else if (*s == '+')
814         {
815           *d++ = ' ';
816           s++;
817         }
818       else
819         *d++ = *s++;
820     }
821   *d = 0;
822   return buffer;
823 }
824
825
826 /* Estimate the quality of the passphrase PW and return a value in the
827    range 0..100.  */
828 static int
829 estimate_passphrase_quality (const char *pw)
830 {
831   int goodlength = opt.min_passphrase_len + opt.min_passphrase_len/3;
832   int length;
833   const char *s;
834
835   if (goodlength < 1)
836     return 0;
837
838   for (length = 0, s = pw; *s; s++)
839     if (!spacep (s))
840       length ++;
841
842   if (length > goodlength)
843     return 100;
844   return ((length*10) / goodlength)*10;
845 }
846
847
848 /* Generate a random passphrase in zBase32 encoding (RFC-6189) to be
849  * used by Pinentry to suggest a passphrase.  Note that we have the
850  * same algorithm in gpg.c for --gen-random at level 30.  It is
851  * important that we always output exactly 30 characters to match the
852  * special exception we have in the pattern file for symmetric
853  * encryption.  */
854 static char *
855 generate_pin (void)
856 {
857   unsigned int nbits = DEFAULT_GENPIN_BITS;
858   size_t nbytes = nbytes = (nbits + 7) / 8;
859   void *rand;
860   char *generated;
861
862    rand = gcry_random_bytes_secure (nbytes, GCRY_STRONG_RANDOM);
863   if (!rand)
864     {
865       log_error ("failed to generate random pin\n");
866       return NULL;
867     }
868
869   generated = zb32_encode (rand, nbits);
870   gcry_free (rand);
871   return generated;
872 }
873
874
875 /* Handle inquiries. */
876 struct inq_cb_parm_s
877 {
878   assuan_context_t ctx;
879   unsigned int flags;  /* CHECK_CONSTRAINTS_... */
880   int genpinhash_valid;
881   char genpinhash[32]; /* Hash of the last generated pin.  */
882 };
883
884
885 /* Return true if PIN is indentical to the last generated pin.  */
886 static int
887 is_generated_pin (struct inq_cb_parm_s *parm, const char *pin)
888 {
889   char hashbuf[32];
890
891   if (!parm->genpinhash_valid)
892     return 0;
893   if (!*pin)
894     return 0;
895   /* Note that we compare the hash so that we do not need to save the
896    * generated PIN longer than needed. */
897   gcry_md_hash_buffer (GCRY_MD_SHA256, hashbuf, pin, strlen (pin));
898
899   if (!memcmp (hashbuf, parm->genpinhash, 32))
900     return 1; /* yes, it is the same.  */
901
902   return 0;
903 }
904
905
906 static gpg_error_t
907 inq_cb (void *opaque, const char *line)
908 {
909   struct inq_cb_parm_s *parm = opaque;
910   gpg_error_t err;
911   const char *s;
912   char *pin;
913
914   if ((s = has_leading_keyword (line, "QUALITY")))
915     {
916       char numbuf[20];
917       int percent;
918
919       pin = unescape_passphrase_string (s);
920       if (!pin)
921         err = gpg_error_from_syserror ();
922       else
923         {
924           percent = estimate_passphrase_quality (pin);
925           if (check_passphrase_constraints (NULL, pin, parm->flags, NULL))
926             percent = -percent;
927           snprintf (numbuf, sizeof numbuf, "%d", percent);
928           err = assuan_send_data (parm->ctx, numbuf, strlen (numbuf));
929           xfree (pin);
930         }
931     }
932   else if ((s = has_leading_keyword (line, "CHECKPIN")))
933     {
934       char *errtext = NULL;
935       size_t errtextlen;
936
937       if (!opt.enforce_passphrase_constraints)
938         {
939           log_error ("unexpected inquiry 'CHECKPIN' without enforced "
940                      "passphrase constraints\n");
941           err = gpg_error (GPG_ERR_ASS_UNEXPECTED_CMD);
942           goto leave;
943         }
944
945       pin = unescape_passphrase_string (s);
946       if (!pin)
947         err = gpg_error_from_syserror ();
948       else
949         {
950           if (!is_generated_pin (parm, pin)
951               && check_passphrase_constraints (NULL, pin,parm->flags, &errtext))
952             {
953               if (errtext)
954                 {
955                   /* Unescape the percent-escaped errtext because
956                      assuan_send_data escapes it again. */
957                   errtextlen = percent_unescape_inplace (errtext, 0);
958                   err = assuan_send_data (parm->ctx, errtext, errtextlen);
959                 }
960               else
961                 {
962                   log_error ("passphrase check failed without error text\n");
963                   err = gpg_error (GPG_ERR_GENERAL);
964                 }
965             }
966           else
967             {
968               err = assuan_send_data (parm->ctx, NULL, 0);
969             }
970           xfree (errtext);
971           xfree (pin);
972         }
973     }
974   else if ((s = has_leading_keyword (line, "GENPIN")))
975     {
976       int wasconf;
977
978       parm->genpinhash_valid = 0;
979       pin = generate_pin ();
980       if (!pin)
981         {
982           log_error ("failed to generate a passphrase\n");
983           err = gpg_error (GPG_ERR_GENERAL);
984           goto leave;
985         }
986       wasconf = assuan_get_flag (entry_ctx, ASSUAN_CONFIDENTIAL);
987       assuan_begin_confidential (parm->ctx);
988       err = assuan_send_data (parm->ctx, pin, strlen (pin));
989       if (!wasconf)
990         assuan_end_confidential (parm->ctx);
991       gcry_md_hash_buffer (GCRY_MD_SHA256, parm->genpinhash, pin, strlen (pin));
992       parm->genpinhash_valid = 1;
993       xfree (pin);
994     }
995   else
996     {
997       log_error ("unsupported inquiry '%s' from pinentry\n", line);
998       err = gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
999     }
1000
1001  leave:
1002   return err;
1003 }
1004
1005
1006 /* Helper to setup pinentry for genpin action. */
1007 static gpg_error_t
1008 setup_genpin (ctrl_t ctrl)
1009 {
1010   gpg_error_t err;
1011   char line[ASSUAN_LINELENGTH];
1012   char *tmpstr, *tmpstr2;
1013   const char *tooltip;
1014
1015   (void)ctrl;
1016
1017   /* TRANSLATORS: This string is displayed by Pinentry as the label
1018      for generating a passphrase.  */
1019   tmpstr = try_percent_escape (L_("Suggest"), "\t\r\n\f\v");
1020   snprintf (line, DIM(line), "SETGENPIN %s", tmpstr? tmpstr:"");
1021   xfree (tmpstr);
1022   err = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1023   if (gpg_err_code (err) == 103 /*(Old assuan error code)*/
1024       || gpg_err_code (err) == GPG_ERR_ASS_UNKNOWN_CMD)
1025     ; /* Ignore Unknown Command from old Pinentry versions.  */
1026   else if (err)
1027     return err;
1028
1029   tmpstr2 = gnupg_get_help_string ("pinentry.genpin.tooltip", 0);
1030   if (tmpstr2)
1031     tooltip = tmpstr2;
1032   else
1033     {
1034       /* TRANSLATORS: This string is a tooltip, shown by pinentry when
1035          hovering over the generate button.  Please use an appropriate
1036          string to describe what this is about.  The length of the
1037          tooltip is limited to about 900 characters.  If you do not
1038          translate this entry, a default English text (see source)
1039          will be used.  The strcmp thingy is there to detect a
1040          non-translated string.  */
1041       tooltip = L_("pinentry.genpin.tooltip");
1042       if (!strcmp ("pinentry.genpin.tooltip", tooltip))
1043         tooltip = "Suggest a random passphrase.";
1044     }
1045   tmpstr = try_percent_escape (tooltip, "\t\r\n\f\v");
1046   xfree (tmpstr2);
1047   snprintf (line, DIM(line), "SETGENPIN_TT %s", tmpstr? tmpstr:"");
1048   xfree (tmpstr);
1049   err = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1050   if (gpg_err_code (err) == 103 /*(Old assuan error code)*/
1051           || gpg_err_code (err) == GPG_ERR_ASS_UNKNOWN_CMD)
1052     ; /* Ignore Unknown Command from old pinentry versions.  */
1053   else if (err)
1054     return err;
1055
1056   return 0;
1057 }
1058
1059
1060 /* Helper to setup pinentry for formatted passphrase. */
1061 static gpg_error_t
1062 setup_formatted_passphrase (ctrl_t ctrl)
1063 {
1064   static const struct { const char *key, *help_id, *value; } tbl[] = {
1065     { "hint",  "pinentry.formatted_passphrase.hint",
1066       /* TRANSLATORS: This is a text shown by pinentry if the option
1067          for formatted passphrase is enabled.  The length is
1068          limited to about 900 characters.  */
1069       N_("Note: The blanks are not part of the passphrase.") },
1070     { NULL, NULL }
1071   };
1072
1073   gpg_error_t rc;
1074   char line[ASSUAN_LINELENGTH];
1075   int idx;
1076   char *tmpstr;
1077   const char *s;
1078   char *escapedstr;
1079
1080   (void)ctrl;
1081
1082   if (opt.pinentry_formatted_passphrase)
1083     {
1084       snprintf (line, DIM(line), "OPTION formatted-passphrase");
1085       rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL,
1086                             NULL);
1087       if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
1088         return rc;
1089
1090       for (idx=0; tbl[idx].key; idx++)
1091         {
1092           tmpstr = gnupg_get_help_string (tbl[idx].help_id, 0);
1093           if (tmpstr)
1094             s = tmpstr;
1095           else
1096             s = L_(tbl[idx].value);
1097           escapedstr = try_percent_escape (s, "\t\r\n\f\v");
1098           xfree (tmpstr);
1099           if (escapedstr && *escapedstr)
1100             {
1101               snprintf (line, DIM(line), "OPTION formatted-passphrase-%s=%s",
1102                         tbl[idx].key, escapedstr);
1103               rc = assuan_transact (entry_ctx, line,
1104                                     NULL, NULL, NULL, NULL, NULL, NULL);
1105             }
1106           else
1107             rc = 0;
1108           xfree (escapedstr);
1109           if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
1110             return rc;
1111         }
1112     }
1113
1114   return 0;
1115 }
1116
1117
1118 /* Helper to setup pinentry for enforced passphrase constraints. */
1119 static gpg_error_t
1120 setup_enforced_constraints (ctrl_t ctrl)
1121 {
1122   static const struct { const char *key, *help_id, *value; } tbl[] = {
1123     { "hint-short", "pinentry.constraints.hint.short", NULL },
1124     { "hint-long",  "pinentry.constraints.hint.long", NULL },
1125     /* TRANSLATORS: This is a text shown by pinentry as title of a dialog
1126        telling the user that the entered new passphrase does not satisfy
1127        the passphrase constraints.  Please keep it short. */
1128     { "error-title", NULL, N_("Passphrase Not Allowed") },
1129     { NULL, NULL }
1130   };
1131
1132   gpg_error_t rc;
1133   char line[ASSUAN_LINELENGTH];
1134   int idx;
1135   char *tmpstr;
1136   const char *s;
1137   char *escapedstr;
1138
1139   (void)ctrl;
1140
1141   if (opt.enforce_passphrase_constraints)
1142     {
1143       snprintf (line, DIM(line), "OPTION constraints-enforce");
1144       rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL,
1145                             NULL);
1146       if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
1147         return rc;
1148
1149       for (idx=0; tbl[idx].key; idx++)
1150         {
1151           tmpstr = gnupg_get_help_string (tbl[idx].help_id, 0);
1152           if (tmpstr)
1153             s = tmpstr;
1154           else if (tbl[idx].value)
1155             s = L_(tbl[idx].value);
1156           else
1157             {
1158               log_error ("no help string found for %s\n", tbl[idx].help_id);
1159               continue;
1160             }
1161           escapedstr = try_percent_escape (s, "\t\r\n\f\v");
1162           xfree (tmpstr);
1163           if (escapedstr && *escapedstr)
1164             {
1165               snprintf (line, DIM(line), "OPTION constraints-%s=%s",
1166                         tbl[idx].key, escapedstr);
1167               rc = assuan_transact (entry_ctx, line,
1168                                     NULL, NULL, NULL, NULL, NULL, NULL);
1169             }
1170           else
1171             rc = 0;  /* Ignore an empty string (would give an IPC error).  */
1172           xfree (escapedstr);
1173           if (rc && gpg_err_code (rc) != GPG_ERR_UNKNOWN_OPTION)
1174             return rc;
1175         }
1176     }
1177
1178   return 0;
1179 }
1180
1181
1182 /* Helper for agent_askpin and agent_get_passphrase.  */
1183 static gpg_error_t
1184 setup_qualitybar (ctrl_t ctrl)
1185 {
1186   int rc;
1187   char line[ASSUAN_LINELENGTH];
1188   char *tmpstr, *tmpstr2;
1189   const char *tooltip;
1190
1191   (void)ctrl;
1192
1193   /* TRANSLATORS: This string is displayed by Pinentry as the label
1194      for the quality bar.  */
1195   tmpstr = try_percent_escape (L_("Quality:"), "\t\r\n\f\v");
1196   snprintf (line, DIM(line), "SETQUALITYBAR %s", tmpstr? tmpstr:"");
1197   xfree (tmpstr);
1198   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1199   if (rc == 103 /*(Old assuan error code)*/
1200       || gpg_err_code (rc) == GPG_ERR_ASS_UNKNOWN_CMD)
1201     ; /* Ignore Unknown Command from old Pinentry versions.  */
1202   else if (rc)
1203     return rc;
1204
1205   tmpstr2 = gnupg_get_help_string ("pinentry.qualitybar.tooltip", 0);
1206   if (tmpstr2)
1207     tooltip = tmpstr2;
1208   else
1209     {
1210       /* TRANSLATORS: This string is a tooltip, shown by pinentry when
1211          hovering over the quality bar.  Please use an appropriate
1212          string to describe what this is about.  The length of the
1213          tooltip is limited to about 900 characters.  If you do not
1214          translate this entry, a default english text (see source)
1215          will be used. */
1216       tooltip =  L_("pinentry.qualitybar.tooltip");
1217       if (!strcmp ("pinentry.qualitybar.tooltip", tooltip))
1218         tooltip = ("The quality of the text entered above.\n"
1219                    "Please ask your administrator for "
1220                    "details about the criteria.");
1221     }
1222   tmpstr = try_percent_escape (tooltip, "\t\r\n\f\v");
1223   xfree (tmpstr2);
1224   snprintf (line, DIM(line), "SETQUALITYBAR_TT %s", tmpstr? tmpstr:"");
1225   xfree (tmpstr);
1226   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1227   if (rc == 103 /*(Old assuan error code)*/
1228           || gpg_err_code (rc) == GPG_ERR_ASS_UNKNOWN_CMD)
1229     ; /* Ignore Unknown Command from old pinentry versions.  */
1230   else if (rc)
1231     return rc;
1232
1233   return 0;
1234 }
1235
1236 /* Check the button_info line for a close action.  Also check for the
1237    PIN_REPEATED flag.  */
1238 static gpg_error_t
1239 pinentry_status_cb (void *opaque, const char *line)
1240 {
1241   unsigned int *flag = opaque;
1242   const char *args;
1243
1244   if ((args = has_leading_keyword (line, "BUTTON_INFO")))
1245     {
1246       if (!strcmp (args, "close"))
1247         *flag |= PINENTRY_STATUS_CLOSE_BUTTON;
1248     }
1249   else if (has_leading_keyword (line, "PIN_REPEATED"))
1250     {
1251       *flag |= PINENTRY_STATUS_PIN_REPEATED;
1252     }
1253   else if (has_leading_keyword (line, "PASSWORD_FROM_CACHE"))
1254     {
1255       *flag |= PINENTRY_STATUS_PASSWORD_FROM_CACHE;
1256     }
1257
1258   return 0;
1259 }
1260
1261
1262 /* Build a SETDESC command line.  This is a dedicated function so that
1263  * it can remove control characters which are not supported by the
1264  * current Pinentry.  */
1265 static void
1266 build_cmd_setdesc (char *line, size_t linelen, const char *desc)
1267 {
1268   char *src, *dst;
1269
1270   snprintf (line, linelen, "SETDESC %s", desc);
1271   if (!entry_features.tabbing)
1272     {
1273       /* Remove RS and US.  */
1274       for (src=dst=line; *src; src++)
1275         if (!strchr ("\x1e\x1f", *src))
1276           *dst++ = *src;
1277       *dst = 0;
1278     }
1279 }
1280
1281
1282 \f
1283 /* Watch the socket's EOF condition, while checking finish of
1284    foreground thread.  When EOF condition is detected, terminate
1285    the pinentry process behind the assuan pipe.
1286  */
1287 static void *
1288 watch_sock (void *arg)
1289 {
1290   pid_t pid = assuan_get_pid (entry_ctx);
1291
1292   while (1)
1293     {
1294       int err;
1295       fd_set fdset;
1296       struct timeval timeout = { 0, 500000 };
1297       gnupg_fd_t sock = *(gnupg_fd_t *)arg;
1298
1299       if (sock == GNUPG_INVALID_FD)
1300         return NULL;
1301
1302       FD_ZERO (&fdset);
1303       FD_SET (FD2INT (sock), &fdset);
1304       err = npth_select (FD2INT (sock)+1, &fdset, NULL, NULL, &timeout);
1305
1306       if (err < 0)
1307         {
1308           if (errno == EINTR)
1309             continue;
1310           else
1311             return NULL;
1312         }
1313
1314       /* Possibly, it's EOF.  */
1315       if (err > 0)
1316         break;
1317     }
1318
1319   if (pid == (pid_t)(-1))
1320     ; /* No pid available can't send a kill. */
1321 #ifdef HAVE_W32_SYSTEM
1322   /* Older versions of assuan set PID to 0 on Windows to indicate an
1323      invalid value.  */
1324   else if (pid != (pid_t) INVALID_HANDLE_VALUE && pid != 0)
1325     TerminateProcess ((HANDLE)pid, 1);
1326 #else
1327   else if (pid > 0)
1328     kill (pid, SIGINT);
1329 #endif
1330
1331   return NULL;
1332 }
1333
1334
1335 static gpg_error_t
1336 watch_sock_start (gnupg_fd_t *sock_p, npth_t *thread_p)
1337 {
1338   npth_attr_t tattr;
1339   int err;
1340
1341   err = npth_attr_init (&tattr);
1342   if (err)
1343     {
1344       log_error ("do_getpin: error npth_attr_init: %s\n", strerror (err));
1345       return gpg_error_from_errno (err);
1346     }
1347   npth_attr_setdetachstate (&tattr, NPTH_CREATE_JOINABLE);
1348
1349   err = npth_create (thread_p, &tattr, watch_sock, sock_p);
1350   npth_attr_destroy (&tattr);
1351   if (err)
1352     {
1353       log_error ("do_getpin: error spawning thread: %s\n", strerror (err));
1354       return gpg_error_from_errno (err);
1355     }
1356
1357   return 0;
1358 }
1359
1360 static void
1361 watch_sock_end (gnupg_fd_t *sock_p, npth_t *thread_p)
1362 {
1363   int err;
1364
1365   *sock_p = GNUPG_INVALID_FD;
1366   err = npth_join (*thread_p, NULL);
1367   if (err)
1368     log_error ("watch_sock_end: error joining thread: %s\n", strerror (err));
1369 }
1370
1371
1372 /* Ask pinentry to get a pin by "GETPIN" command, spawning a thread
1373    detecting the socket's EOF.
1374  */
1375 static gpg_error_t
1376 do_getpin (ctrl_t ctrl, struct entry_parm_s *parm)
1377 {
1378   gpg_error_t rc;
1379   gnupg_fd_t sock_watched = ctrl->thread_startup.fd;
1380   npth_t thread;
1381   int wasconf;
1382   struct inq_cb_parm_s inq_cb_parm;
1383
1384   rc = watch_sock_start (&sock_watched, &thread);
1385   if (rc)
1386     return rc;
1387
1388   inq_cb_parm.ctx = entry_ctx;
1389   inq_cb_parm.flags = parm->constraints_flags;
1390   inq_cb_parm.genpinhash_valid = 0;
1391
1392   wasconf = assuan_get_flag (entry_ctx, ASSUAN_CONFIDENTIAL);
1393   assuan_begin_confidential (entry_ctx);
1394   rc = assuan_transact (entry_ctx, "GETPIN", getpin_cb, parm,
1395                         inq_cb, &inq_cb_parm,
1396                         pinentry_status_cb, &parm->status);
1397   if (!wasconf)
1398     assuan_end_confidential (entry_ctx);
1399
1400   if (!rc && parm->buffer && is_generated_pin (&inq_cb_parm, parm->buffer))
1401     parm->status |= PINENTRY_STATUS_PASSWORD_GENERATED;
1402   else
1403     parm->status &= ~PINENTRY_STATUS_PASSWORD_GENERATED;
1404
1405   /* Most pinentries out in the wild return the old Assuan error code
1406      for canceled which gets translated to an assuan Cancel error and
1407      not to the code for a user cancel.  Fix this here. */
1408   if (rc && gpg_err_source (rc) && gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
1409     rc = gpg_err_make (gpg_err_source (rc), GPG_ERR_CANCELED);
1410   /* Change error code in case the window close button was clicked
1411      to cancel the operation.  */
1412   if ((parm->status & PINENTRY_STATUS_CLOSE_BUTTON)
1413       && gpg_err_code (rc) == GPG_ERR_CANCELED)
1414     rc = gpg_err_make (gpg_err_source (rc), GPG_ERR_FULLY_CANCELED);
1415
1416   watch_sock_end (&sock_watched, &thread);
1417
1418   return rc;
1419 }
1420 \f
1421 /* Call the Entry and ask for the PIN.  We do check for a valid PIN
1422    number here and repeat it as long as we have invalid formed
1423    numbers.  KEYINFO and CACHE_MODE are used to tell pinentry something
1424    about the key. */
1425 gpg_error_t
1426 agent_askpin (ctrl_t ctrl,
1427               const char *desc_text, const char *prompt_text,
1428               const char *initial_errtext,
1429               struct pin_entry_info_s *pininfo,
1430               const char *keyinfo, cache_mode_t cache_mode)
1431 {
1432   gpg_error_t rc;
1433   char line[ASSUAN_LINELENGTH];
1434   struct entry_parm_s parm;
1435   const char *errtext = NULL;
1436   int is_pin = 0;
1437   int is_generated;
1438
1439   if (opt.batch)
1440     return 0; /* fixme: we should return BAD PIN */
1441
1442   if (ctrl->pinentry_mode != PINENTRY_MODE_ASK)
1443     {
1444       if (ctrl->pinentry_mode == PINENTRY_MODE_CANCEL)
1445         return gpg_error (GPG_ERR_CANCELED);
1446       if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
1447         {
1448           unsigned char *passphrase;
1449           size_t size;
1450
1451           *pininfo->pin = 0; /* Reset the PIN. */
1452           rc = pinentry_loopback (ctrl, "PASSPHRASE", &passphrase, &size,
1453                                   pininfo->max_length - 1);
1454           if (rc)
1455             return rc;
1456
1457           memcpy(&pininfo->pin, passphrase, size);
1458           xfree(passphrase);
1459           pininfo->pin[size] = 0;
1460           if (pininfo->check_cb)
1461             {
1462               /* More checks by utilizing the optional callback. */
1463               pininfo->cb_errtext = NULL;
1464               rc = pininfo->check_cb (pininfo);
1465             }
1466           return rc;
1467         }
1468       return gpg_error(GPG_ERR_NO_PIN_ENTRY);
1469     }
1470
1471   if (!pininfo || pininfo->max_length < 1)
1472     return gpg_error (GPG_ERR_INV_VALUE);
1473   if (!desc_text && pininfo->min_digits)
1474     desc_text = L_("Please enter your PIN, so that the secret key "
1475                    "can be unlocked for this session");
1476   else if (!desc_text)
1477     desc_text = L_("Please enter your passphrase, so that the secret key "
1478                    "can be unlocked for this session");
1479
1480   if (prompt_text)
1481     is_pin = !!strstr (prompt_text, "PIN");
1482   else
1483     is_pin = desc_text && strstr (desc_text, "PIN");
1484
1485   rc = start_pinentry (ctrl);
1486   if (rc)
1487     return rc;
1488
1489   /* If we have a KEYINFO string and are normal, user, or ssh cache
1490      mode, we tell that the Pinentry so it may use it for own caching
1491      purposes.  Most pinentries won't have this implemented and thus
1492      we do not error out in this case.  */
1493   if (keyinfo && (cache_mode == CACHE_MODE_NORMAL
1494                   || cache_mode == CACHE_MODE_USER
1495                   || cache_mode == CACHE_MODE_SSH))
1496     snprintf (line, DIM(line), "SETKEYINFO %c/%s",
1497               cache_mode == CACHE_MODE_USER? 'u' :
1498               cache_mode == CACHE_MODE_SSH? 's' : 'n',
1499               keyinfo);
1500   else
1501     snprintf (line, DIM(line), "SETKEYINFO --clear");
1502
1503   rc = assuan_transact (entry_ctx, line,
1504                         NULL, NULL, NULL, NULL, NULL, NULL);
1505   if (rc && gpg_err_code (rc) != GPG_ERR_ASS_UNKNOWN_CMD)
1506     return unlock_pinentry (ctrl, rc);
1507
1508   build_cmd_setdesc (line, DIM(line), desc_text);
1509   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1510   if (rc)
1511     return unlock_pinentry (ctrl, rc);
1512
1513   snprintf (line, DIM(line), "SETPROMPT %s",
1514             prompt_text? prompt_text : is_pin? L_("PIN:") : L_("Passphrase:"));
1515   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1516   if (rc)
1517     return unlock_pinentry (ctrl, rc);
1518
1519   /* If a passphrase quality indicator has been requested and a
1520      minimum passphrase length has not been disabled, send the command
1521      to the pinentry.  */
1522   if (pininfo->with_qualitybar && opt.min_passphrase_len )
1523     {
1524       rc = setup_qualitybar (ctrl);
1525       if (rc)
1526         return unlock_pinentry (ctrl, rc);
1527     }
1528
1529   if (initial_errtext)
1530     {
1531       snprintf (line, DIM(line), "SETERROR %s", initial_errtext);
1532       rc = assuan_transact (entry_ctx, line,
1533                             NULL, NULL, NULL, NULL, NULL, NULL);
1534       if (rc)
1535         return unlock_pinentry (ctrl, rc);
1536     }
1537
1538   if (pininfo->with_repeat)
1539     {
1540       snprintf (line, DIM(line), "SETREPEATERROR %s",
1541                 L_("does not match - try again"));
1542       rc = assuan_transact (entry_ctx, line,
1543                             NULL, NULL, NULL, NULL, NULL, NULL);
1544       if (rc)
1545         pininfo->with_repeat = 0; /* Pinentry does not support it.  */
1546
1547       if (pininfo->with_repeat)
1548         {
1549           snprintf (line, DIM(line), "SETREPEATOK %s",
1550                     L_("Passphrases match."));
1551           rc = assuan_transact (entry_ctx, line,
1552                                 NULL, NULL, NULL, NULL, NULL, NULL);
1553           if (rc)
1554             rc = 0; /* Pinentry does not support it. */
1555         }
1556
1557     }
1558   pininfo->repeat_okay = 0;
1559   pininfo->status = 0;
1560
1561   for (;pininfo->failed_tries < pininfo->max_tries; pininfo->failed_tries++)
1562     {
1563       memset (&parm, 0, sizeof parm);
1564       parm.size = pininfo->max_length;
1565       *pininfo->pin = 0; /* Reset the PIN. */
1566       parm.buffer = (unsigned char*)pininfo->pin;
1567       parm.constraints_flags = pininfo->constraints_flags;
1568
1569       if (errtext)
1570         {
1571           /* TRANSLATORS: The string is appended to an error message in
1572              the pinentry.  The %s is the actual error message, the
1573              two %d give the current and maximum number of tries. */
1574           snprintf (line, DIM(line), L_("SETERROR %s (try %d of %d)"),
1575                     errtext, pininfo->failed_tries+1, pininfo->max_tries);
1576           rc = assuan_transact (entry_ctx, line,
1577                                 NULL, NULL, NULL, NULL, NULL, NULL);
1578           if (rc)
1579             return unlock_pinentry (ctrl, rc);
1580           errtext = NULL;
1581         }
1582
1583       if (pininfo->with_repeat)
1584         {
1585           snprintf (line, DIM(line), "SETREPEAT %s", L_("Repeat:"));
1586           rc = assuan_transact (entry_ctx, line,
1587                                 NULL, NULL, NULL, NULL, NULL, NULL);
1588           if (rc)
1589             return unlock_pinentry (ctrl, rc);
1590         }
1591
1592       rc = do_getpin (ctrl, &parm);
1593       pininfo->status = parm.status;
1594       is_generated = !!(parm.status & PINENTRY_STATUS_PASSWORD_GENERATED);
1595
1596       if (gpg_err_code (rc) == GPG_ERR_ASS_TOO_MUCH_DATA)
1597         errtext = is_pin? L_("PIN too long")
1598                         : L_("Passphrase too long");
1599       else if (rc)
1600         return unlock_pinentry (ctrl, rc);
1601
1602       if (!errtext && pininfo->min_digits && !is_generated)
1603         {
1604           /* do some basic checks on the entered PIN. */
1605           if (!all_digitsp (pininfo->pin))
1606             errtext = L_("Invalid characters in PIN");
1607           else if (pininfo->max_digits
1608                    && strlen (pininfo->pin) > pininfo->max_digits)
1609             errtext = L_("PIN too long");
1610           else if (strlen (pininfo->pin) < pininfo->min_digits)
1611             errtext = L_("PIN too short");
1612         }
1613
1614       if (!errtext && pininfo->check_cb && !is_generated)
1615         {
1616           /* More checks by utilizing the optional callback. */
1617           pininfo->cb_errtext = NULL;
1618           rc = pininfo->check_cb (pininfo);
1619           /* When pinentry cache causes an error, return now.  */
1620           if (rc
1621               && (pininfo->status & PINENTRY_STATUS_PASSWORD_FROM_CACHE))
1622             return unlock_pinentry (ctrl, rc);
1623
1624           if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE)
1625             {
1626               if (pininfo->cb_errtext)
1627                 errtext = pininfo->cb_errtext;
1628               else if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
1629                        || gpg_err_code (rc) == GPG_ERR_BAD_PIN)
1630                 errtext = (is_pin? L_("Bad PIN") : L_("Bad Passphrase"));
1631             }
1632           else if (rc)
1633             return unlock_pinentry (ctrl, rc);
1634         }
1635
1636       if (!errtext)
1637         {
1638           if (pininfo->with_repeat
1639               && (pininfo->status & PINENTRY_STATUS_PIN_REPEATED))
1640             pininfo->repeat_okay = 1;
1641           return unlock_pinentry (ctrl, 0); /* okay, got a PIN or passphrase */
1642         }
1643
1644       if ((pininfo->status & PINENTRY_STATUS_PASSWORD_FROM_CACHE))
1645         {
1646           /* The password was read from the cache.  Don't count this
1647              against the retry count.  */
1648           pininfo->failed_tries --;
1649         }
1650     }
1651
1652   return unlock_pinentry (ctrl, gpg_error (pininfo->min_digits? GPG_ERR_BAD_PIN
1653                           : GPG_ERR_BAD_PASSPHRASE));
1654 }
1655
1656
1657 \f
1658 /* Ask for the passphrase using the supplied arguments.  The returned
1659    passphrase needs to be freed by the caller.  PININFO is optional
1660    and can be used to have constraints checking while the pinentry
1661    dialog is open (like what we do in agent_askpin).  This is very
1662    similar to agent_askpin and we should eventually merge the two
1663    functions. */
1664 int
1665 agent_get_passphrase (ctrl_t ctrl,
1666                       char **retpass, const char *desc, const char *prompt,
1667                       const char *errtext, int with_qualitybar,
1668                       const char *keyinfo, cache_mode_t cache_mode,
1669                       struct pin_entry_info_s *pininfo)
1670 {
1671   int rc;
1672   int is_pin;
1673   int is_generated;
1674   char line[ASSUAN_LINELENGTH];
1675   struct entry_parm_s parm;
1676
1677   *retpass = NULL;
1678   if (opt.batch)
1679     return gpg_error (GPG_ERR_BAD_PASSPHRASE);
1680
1681   if (ctrl->pinentry_mode != PINENTRY_MODE_ASK)
1682     {
1683       unsigned char *passphrase;
1684       size_t size;
1685
1686       if (ctrl->pinentry_mode == PINENTRY_MODE_CANCEL)
1687         return gpg_error (GPG_ERR_CANCELED);
1688
1689       if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK && pininfo)
1690         {
1691           *pininfo->pin = 0; /* Reset the PIN. */
1692           rc = pinentry_loopback (ctrl, "PASSPHRASE",
1693                                   &passphrase, &size,
1694                                   pininfo->max_length - 1);
1695           if (rc)
1696             return rc;
1697
1698           memcpy (&pininfo->pin, passphrase, size);
1699           wipememory (passphrase, size);
1700           xfree (passphrase);
1701           pininfo->pin[size] = 0;
1702           if (pininfo->check_cb)
1703             {
1704               /* More checks by utilizing the optional callback. */
1705               pininfo->cb_errtext = NULL;
1706               rc = pininfo->check_cb (pininfo);
1707             }
1708           return rc;
1709
1710         }
1711       else if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
1712         {
1713           /* Legacy variant w/o PININFO.  */
1714           return pinentry_loopback (ctrl, "PASSPHRASE",
1715                                     (unsigned char **)retpass, &size,
1716                                     MAX_PASSPHRASE_LEN);
1717         }
1718
1719       return gpg_error (GPG_ERR_NO_PIN_ENTRY);
1720     }
1721
1722   rc = start_pinentry (ctrl);
1723   if (rc)
1724     return rc;
1725
1726   /* Set IS_PIN and if needed a default prompt.  */
1727   if (prompt)
1728     is_pin = !!strstr (prompt, "PIN");
1729   else
1730     {
1731       is_pin = desc && strstr (desc, "PIN");
1732       prompt = is_pin? L_("PIN:"): L_("Passphrase:");
1733     }
1734
1735   /* If we have a KEYINFO string and are normal, user, or ssh cache
1736      mode, we tell that the Pinentry so it may use it for own caching
1737      purposes.  Most pinentries won't have this implemented and thus
1738      we do not error out in this case.  */
1739   if (keyinfo && (cache_mode == CACHE_MODE_NORMAL
1740                   || cache_mode == CACHE_MODE_USER
1741                   || cache_mode == CACHE_MODE_SSH))
1742     snprintf (line, DIM(line), "SETKEYINFO %c/%s",
1743               cache_mode == CACHE_MODE_USER? 'u' :
1744               cache_mode == CACHE_MODE_SSH? 's' : 'n',
1745               keyinfo);
1746   else
1747     snprintf (line, DIM(line), "SETKEYINFO --clear");
1748
1749   rc = assuan_transact (entry_ctx, line,
1750                         NULL, NULL, NULL, NULL, NULL, NULL);
1751   if (rc && gpg_err_code (rc) != GPG_ERR_ASS_UNKNOWN_CMD)
1752     return unlock_pinentry (ctrl, rc);
1753
1754   if (desc)
1755     build_cmd_setdesc (line, DIM(line), desc);
1756   else
1757     snprintf (line, DIM(line), "RESET");
1758   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1759   if (rc)
1760     return unlock_pinentry (ctrl, rc);
1761
1762   snprintf (line, DIM(line), "SETPROMPT %s", prompt);
1763   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1764   if (rc)
1765     return unlock_pinentry (ctrl, rc);
1766
1767   if ((with_qualitybar || (pininfo && pininfo->with_qualitybar))
1768        && opt.min_passphrase_len)
1769     {
1770       rc = setup_qualitybar (ctrl);
1771       if (rc)
1772         return unlock_pinentry (ctrl, rc);
1773     }
1774
1775   if (errtext)
1776     {
1777       snprintf (line, DIM(line), "SETERROR %s", errtext);
1778       rc = assuan_transact (entry_ctx, line,
1779                             NULL, NULL, NULL, NULL, NULL, NULL);
1780       if (rc)
1781         return unlock_pinentry (ctrl, rc);
1782     }
1783
1784   rc = setup_formatted_passphrase (ctrl);
1785   if (rc)
1786     return unlock_pinentry (ctrl, rc);
1787
1788   if (!pininfo)
1789     {
1790       /* Legacy method without PININFO.  */
1791       memset (&parm, 0, sizeof parm);
1792       parm.size = ASSUAN_LINELENGTH/2 - 5;
1793       parm.buffer = gcry_malloc_secure (parm.size+10);
1794       if (!parm.buffer)
1795         return unlock_pinentry (ctrl, out_of_core ());
1796
1797       rc = do_getpin (ctrl, &parm);
1798       if (rc)
1799         xfree (parm.buffer);
1800       else
1801         *retpass = parm.buffer;
1802       return unlock_pinentry (ctrl, rc);
1803     }
1804
1805   /* We got PININFO.  */
1806
1807   if (pininfo->with_repeat)
1808     {
1809       snprintf (line, DIM(line), "SETREPEATERROR %s",
1810                 L_("does not match - try again"));
1811       rc = assuan_transact (entry_ctx, line,
1812                             NULL, NULL, NULL, NULL, NULL, NULL);
1813       if (rc)
1814         pininfo->with_repeat = 0; /* Pinentry does not support it.  */
1815
1816       if (pininfo->with_repeat)
1817         {
1818           snprintf (line, DIM(line), "SETREPEATOK %s",
1819                     L_("Passphrases match."));
1820           rc = assuan_transact (entry_ctx, line,
1821                                 NULL, NULL, NULL, NULL, NULL, NULL);
1822           if (rc)
1823             rc = 0; /* Pinentry does not support it. */
1824         }
1825
1826       (void)setup_genpin (ctrl);
1827
1828       rc = setup_enforced_constraints (ctrl);
1829       if (rc)
1830         return unlock_pinentry (ctrl, rc);
1831     }
1832   pininfo->repeat_okay = 0;
1833   pininfo->status = 0;
1834
1835   for (;pininfo->failed_tries < pininfo->max_tries; pininfo->failed_tries++)
1836     {
1837       memset (&parm, 0, sizeof parm);
1838       parm.constraints_flags = pininfo->constraints_flags;
1839       parm.size = pininfo->max_length;
1840       parm.buffer = (unsigned char*)pininfo->pin;
1841       *pininfo->pin = 0; /* Reset the PIN. */
1842
1843       if (errtext)
1844         {
1845           /* TRANSLATORS: The string is appended to an error message in
1846              the pinentry.  The %s is the actual error message, the
1847              two %d give the current and maximum number of tries. */
1848           snprintf (line, DIM(line), L_("SETERROR %s (try %d of %d)"),
1849                     errtext, pininfo->failed_tries+1, pininfo->max_tries);
1850           rc = assuan_transact (entry_ctx, line,
1851                                 NULL, NULL, NULL, NULL, NULL, NULL);
1852           if (rc)
1853             return unlock_pinentry (ctrl, rc);
1854           errtext = NULL;
1855         }
1856
1857       if (pininfo->with_repeat)
1858         {
1859           snprintf (line, DIM(line), "SETREPEAT %s", L_("Repeat:"));
1860           rc = assuan_transact (entry_ctx, line,
1861                                 NULL, NULL, NULL, NULL, NULL, NULL);
1862           if (rc)
1863             return unlock_pinentry (ctrl, rc);
1864         }
1865
1866       rc = do_getpin (ctrl, &parm);
1867       pininfo->status = parm.status;
1868       is_generated = !!(parm.status & PINENTRY_STATUS_PASSWORD_GENERATED);
1869
1870       if (gpg_err_code (rc) == GPG_ERR_ASS_TOO_MUCH_DATA)
1871         errtext = is_pin? L_("PIN too long")
1872                         : L_("Passphrase too long");
1873       else if (rc)
1874         return unlock_pinentry (ctrl, rc);
1875
1876       if (!errtext && pininfo->min_digits && !is_generated)
1877         {
1878           /* do some basic checks on the entered PIN. */
1879           if (!all_digitsp (pininfo->pin))
1880             errtext = L_("Invalid characters in PIN");
1881           else if (pininfo->max_digits
1882                    && strlen (pininfo->pin) > pininfo->max_digits)
1883             errtext = L_("PIN too long");
1884           else if (strlen (pininfo->pin) < pininfo->min_digits)
1885             errtext = L_("PIN too short");
1886         }
1887
1888       if (!errtext && pininfo->check_cb && !is_generated)
1889         {
1890           /* More checks by utilizing the optional callback. */
1891           pininfo->cb_errtext = NULL;
1892           rc = pininfo->check_cb (pininfo);
1893           /* When pinentry cache causes an error, return now.  */
1894           if (rc && (pininfo->status & PINENTRY_STATUS_PASSWORD_FROM_CACHE))
1895             return unlock_pinentry (ctrl, rc);
1896
1897           if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE)
1898             {
1899               if (pininfo->cb_errtext)
1900                 errtext = pininfo->cb_errtext;
1901               else if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
1902                        || gpg_err_code (rc) == GPG_ERR_BAD_PIN)
1903                 errtext = (is_pin? L_("Bad PIN") : L_("Bad Passphrase"));
1904             }
1905           else if (rc)
1906             return unlock_pinentry (ctrl, rc);
1907         }
1908
1909       if (!errtext)
1910         {
1911           if (pininfo->with_repeat
1912               && (pininfo->status & PINENTRY_STATUS_PIN_REPEATED))
1913             pininfo->repeat_okay = 1;
1914           return unlock_pinentry (ctrl, 0); /* okay, got a PIN or passphrase */
1915         }
1916
1917       if ((pininfo->status & PINENTRY_STATUS_PASSWORD_FROM_CACHE))
1918         {
1919           /* The password was read from the Pinentry's own cache.
1920              Don't count this against the retry count.  */
1921           pininfo->failed_tries--;
1922         }
1923     }
1924
1925   return unlock_pinentry (ctrl, gpg_error (pininfo->min_digits? GPG_ERR_BAD_PIN
1926                           : GPG_ERR_BAD_PASSPHRASE));
1927 }
1928
1929
1930 \f
1931 /* Pop up the PIN-entry, display the text and the prompt and ask the
1932    user to confirm this.  We return 0 for success, ie. the user
1933    confirmed it, GPG_ERR_NOT_CONFIRMED for what the text says or an
1934    other error.  If WITH_CANCEL it true an extra cancel button is
1935    displayed to allow the user to easily return a GPG_ERR_CANCELED.
1936    if the Pinentry does not support this, the user can still cancel by
1937    closing the Pinentry window.  */
1938 int
1939 agent_get_confirmation (ctrl_t ctrl,
1940                         const char *desc, const char *ok,
1941                         const char *notok, int with_cancel)
1942 {
1943   int rc;
1944   char line[ASSUAN_LINELENGTH];
1945
1946   if (ctrl->pinentry_mode != PINENTRY_MODE_ASK)
1947     {
1948       if (ctrl->pinentry_mode == PINENTRY_MODE_CANCEL)
1949         return gpg_error (GPG_ERR_CANCELED);
1950
1951       if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
1952         return pinentry_loopback_confirm (ctrl, desc, 1, ok, notok);
1953
1954       return gpg_error (GPG_ERR_NO_PIN_ENTRY);
1955     }
1956
1957   rc = start_pinentry (ctrl);
1958   if (rc)
1959     return rc;
1960
1961   if (desc)
1962     build_cmd_setdesc (line, DIM(line), desc);
1963   else
1964     snprintf (line, DIM(line), "RESET");
1965   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
1966   /* Most pinentries out in the wild return the old Assuan error code
1967      for canceled which gets translated to an assuan Cancel error and
1968      not to the code for a user cancel.  Fix this here. */
1969   if (rc && gpg_err_source (rc) && gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
1970     rc = gpg_err_make (gpg_err_source (rc), GPG_ERR_CANCELED);
1971
1972   if (rc)
1973     return unlock_pinentry (ctrl, rc);
1974
1975   if (ok)
1976     {
1977       snprintf (line, DIM(line), "SETOK %s", ok);
1978       rc = assuan_transact (entry_ctx,
1979                             line, NULL, NULL, NULL, NULL, NULL, NULL);
1980       if (rc)
1981         return unlock_pinentry (ctrl, rc);
1982     }
1983   if (notok)
1984     {
1985       /* Try to use the newer NOTOK feature if a cancel button is
1986          requested.  If no cancel button is requested we keep on using
1987          the standard cancel.  */
1988       if (with_cancel)
1989         {
1990           snprintf (line, DIM(line), "SETNOTOK %s", notok);
1991           rc = assuan_transact (entry_ctx,
1992                                 line, NULL, NULL, NULL, NULL, NULL, NULL);
1993         }
1994       else
1995         rc = GPG_ERR_ASS_UNKNOWN_CMD;
1996
1997       if (gpg_err_code (rc) == GPG_ERR_ASS_UNKNOWN_CMD)
1998         {
1999           snprintf (line, DIM(line), "SETCANCEL %s", notok);
2000           rc = assuan_transact (entry_ctx, line,
2001                                 NULL, NULL, NULL, NULL, NULL, NULL);
2002         }
2003       if (rc)
2004         return unlock_pinentry (ctrl, rc);
2005     }
2006
2007   {
2008     gnupg_fd_t sock_watched = ctrl->thread_startup.fd;
2009     npth_t thread;
2010
2011     rc = watch_sock_start (&sock_watched, &thread);
2012     if (!rc)
2013       {
2014         rc = assuan_transact (entry_ctx, "CONFIRM",
2015                               NULL, NULL, NULL, NULL, NULL, NULL);
2016         if (rc && gpg_err_source (rc)
2017             && gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
2018           rc = gpg_err_make (gpg_err_source (rc), GPG_ERR_CANCELED);
2019
2020         watch_sock_end (&sock_watched, &thread);
2021       }
2022
2023     return unlock_pinentry (ctrl, rc);
2024   }
2025 }
2026
2027
2028 \f
2029 /* The thread running the popup message. */
2030 static void *
2031 popup_message_thread (void *arg)
2032 {
2033   gpg_error_t rc;
2034   gnupg_fd_t sock_watched = *(gnupg_fd_t *)arg;
2035   npth_t thread;
2036
2037   rc = watch_sock_start (&sock_watched, &thread);
2038   if (rc)
2039     return NULL;
2040
2041   /* We use the --one-button hack instead of the MESSAGE command to
2042      allow the use of old Pinentries.  Those old Pinentries will then
2043      show an additional Cancel button but that is mostly a visual
2044      annoyance. */
2045   assuan_transact (entry_ctx, "CONFIRM --one-button",
2046                    NULL, NULL, NULL, NULL, NULL, NULL);
2047   watch_sock_end (&sock_watched, &thread);
2048   popup_finished = 1;
2049   return NULL;
2050 }
2051
2052
2053 /* Pop up a message window similar to the confirm one but keep it open
2054    until agent_popup_message_stop has been called.  It is crucial for
2055    the caller to make sure that the stop function gets called as soon
2056    as the message is not anymore required because the message is
2057    system modal and all other attempts to use the pinentry will fail
2058    (after a timeout). */
2059 int
2060 agent_popup_message_start (ctrl_t ctrl, const char *desc, const char *ok_btn)
2061 {
2062   int rc;
2063   char line[ASSUAN_LINELENGTH];
2064   npth_attr_t tattr;
2065   int err;
2066
2067   if (ctrl->pinentry_mode != PINENTRY_MODE_ASK)
2068     {
2069       if (ctrl->pinentry_mode == PINENTRY_MODE_CANCEL)
2070         return gpg_error (GPG_ERR_CANCELED);
2071
2072       if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
2073         return pinentry_loopback_confirm (ctrl, desc, 0, ok_btn, NULL);
2074
2075       return gpg_error (GPG_ERR_NO_PIN_ENTRY);
2076     }
2077
2078   rc = start_pinentry (ctrl);
2079   if (rc)
2080     return rc;
2081
2082   if (desc)
2083     build_cmd_setdesc (line, DIM(line), desc);
2084   else
2085     snprintf (line, DIM(line), "RESET");
2086   rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
2087   if (rc)
2088     return unlock_pinentry (ctrl, rc);
2089
2090   if (ok_btn)
2091     {
2092       snprintf (line, DIM(line), "SETOK %s", ok_btn);
2093       rc = assuan_transact (entry_ctx, line, NULL,NULL,NULL,NULL,NULL,NULL);
2094       if (rc)
2095         return unlock_pinentry (ctrl, rc);
2096     }
2097
2098   err = npth_attr_init (&tattr);
2099   if (err)
2100     return unlock_pinentry (ctrl, gpg_error_from_errno (err));
2101   npth_attr_setdetachstate (&tattr, NPTH_CREATE_JOINABLE);
2102
2103   popup_finished = 0;
2104   err = npth_create (&popup_tid, &tattr, popup_message_thread,
2105                      &ctrl->thread_startup.fd);
2106   npth_attr_destroy (&tattr);
2107   if (err)
2108     {
2109       rc = gpg_error_from_errno (err);
2110       log_error ("error spawning popup message handler: %s\n",
2111                  strerror (err) );
2112       return unlock_pinentry (ctrl, rc);
2113     }
2114   npth_setname_np (popup_tid, "popup-message");
2115
2116   return 0;
2117 }
2118
2119 /* Close a popup window. */
2120 void
2121 agent_popup_message_stop (ctrl_t ctrl)
2122 {
2123   int rc;
2124   pid_t pid;
2125
2126   (void)ctrl;
2127
2128   if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
2129     return;
2130
2131   if (!popup_tid || !entry_ctx)
2132     {
2133       log_debug ("agent_popup_message_stop called with no active popup\n");
2134       return;
2135     }
2136
2137   pid = assuan_get_pid (entry_ctx);
2138   if (pid == (pid_t)(-1))
2139     ; /* No pid available can't send a kill. */
2140   else if (popup_finished)
2141     ; /* Already finished and ready for joining. */
2142 #ifdef HAVE_W32_SYSTEM
2143   /* Older versions of assuan set PID to 0 on Windows to indicate an
2144      invalid value.  */
2145   else if (pid != (pid_t) INVALID_HANDLE_VALUE
2146            && pid != 0)
2147     {
2148       HANDLE process = (HANDLE) pid;
2149
2150       /* Arbitrary error code.  */
2151       TerminateProcess (process, 1);
2152     }
2153 #else
2154   else if (pid > 0)
2155     kill (pid, SIGINT);
2156 #endif
2157
2158   /* Now wait for the thread to terminate. */
2159   rc = npth_join (popup_tid, NULL);
2160   if (rc)
2161     log_debug ("agent_popup_message_stop: pth_join failed: %s\n",
2162                strerror (rc));
2163   /* Thread IDs are opaque, but we try our best here by resetting it
2164      to the same content that a static global variable has.  */
2165   memset (&popup_tid, '\0', sizeof (popup_tid));
2166
2167   /* Now we can close the connection. */
2168   unlock_pinentry (ctrl, 0);
2169 }
2170
2171 int
2172 agent_clear_passphrase (ctrl_t ctrl,
2173                         const char *keyinfo, cache_mode_t cache_mode)
2174 {
2175   int rc;
2176   char line[ASSUAN_LINELENGTH];
2177
2178   if (! (keyinfo && (cache_mode == CACHE_MODE_NORMAL
2179                      || cache_mode == CACHE_MODE_USER
2180                      || cache_mode == CACHE_MODE_SSH)))
2181     return gpg_error (GPG_ERR_NOT_SUPPORTED);
2182
2183   rc = start_pinentry (ctrl);
2184   if (rc)
2185     return rc;
2186
2187   snprintf (line, DIM(line), "CLEARPASSPHRASE %c/%s",
2188             cache_mode == CACHE_MODE_USER? 'u' :
2189             cache_mode == CACHE_MODE_SSH? 's' : 'n',
2190             keyinfo);
2191   rc = assuan_transact (entry_ctx, line,
2192                         NULL, NULL, NULL, NULL, NULL, NULL);
2193
2194   return unlock_pinentry (ctrl, rc);
2195 }