1 /* passphrase.c - Passphrase callback.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GPGME; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
38 char *passphrase_info;
44 release_op_data (void *hook)
46 op_data_t opd = (op_data_t) hook;
48 free (opd->passphrase_info);
49 free (opd->userid_hint);
54 _gpgme_passphrase_status_handler (void *priv, gpgme_status_code_t code, char *args)
56 gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
60 if (!ctx->passphrase_cb)
63 err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &opd,
64 sizeof (*opd), release_op_data);
70 case GPGME_STATUS_USERID_HINT:
72 free (opd->userid_hint);
73 if (!(opd->userid_hint = strdup (args)))
74 return GPGME_Out_Of_Core;
77 case GPGME_STATUS_BAD_PASSPHRASE:
78 opd->bad_passphrase++;
79 opd->no_passphrase = 0;
82 case GPGME_STATUS_GOOD_PASSPHRASE:
83 opd->bad_passphrase = 0;
84 opd->no_passphrase = 0;
87 case GPGME_STATUS_NEED_PASSPHRASE:
88 case GPGME_STATUS_NEED_PASSPHRASE_SYM:
89 if (opd->passphrase_info)
90 free (opd->passphrase_info);
91 opd->passphrase_info = strdup (args);
92 if (!opd->passphrase_info)
93 return GPGME_Out_Of_Core;
96 case GPGME_STATUS_MISSING_PASSPHRASE:
97 opd->no_passphrase = 1;
100 case GPGME_STATUS_EOF:
101 if (opd->no_passphrase || opd->bad_passphrase)
102 return GPGME_Bad_Passphrase;
106 /* Ignore all other codes. */
114 _gpgme_passphrase_command_handler (void *priv, gpgme_status_code_t code,
115 const char *key, const char **result)
117 gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
121 if (!ctx->passphrase_cb)
124 err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &opd,
125 sizeof (*opd), release_op_data);
131 /* We have been called for cleanup. */
132 if (ctx->passphrase_cb)
133 /* FIXME: Take the key in account. */
134 err = ctx->passphrase_cb (ctx->passphrase_cb_value, NULL,
135 &opd->last_pw_handle, NULL);
140 if (!key || !ctx->passphrase_cb)
146 if (code == GPGME_STATUS_GET_HIDDEN && !strcmp (key, "passphrase.enter"))
148 const char *userid_hint = opd->userid_hint;
149 const char *passphrase_info = opd->passphrase_info;
150 int bad_passphrase = opd->bad_passphrase;
153 opd->bad_passphrase = 0;
155 userid_hint = "[User ID hint missing]";
156 if (!passphrase_info)
157 passphrase_info = "[passphrase info missing]";
158 buf = malloc (20 + strlen (userid_hint)
159 + strlen (passphrase_info) + 3);
161 return GPGME_Out_Of_Core;
162 sprintf (buf, "%s\n%s\n%s",
163 bad_passphrase ? "TRY_AGAIN":"ENTER",
164 userid_hint, passphrase_info);
166 err = ctx->passphrase_cb (ctx->passphrase_cb_value, buf,
167 &opd->last_pw_handle, result);