/[winpt]/trunk/Src/wptCardEditCB.cpp
ViewVC logotype

Annotation of /trunk/Src/wptCardEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 48 - (hide annotations)
Mon Oct 31 21:14:11 2005 UTC (19 years, 4 months ago) by werner
File size: 9104 byte(s)
More changes.  Compiles again but there are at least gettext issues with
w32-gettext.c.  I can't get a gpg-error build with ENABLE_NLS.

1 werner 36 /* wptCardEditCB.cpp - Card callbacks
2     * Copyright (C) 2003, 2004, 2005 Timo Schulz
3     * Copyright (C) 2005 g10 Code GmbH
4     *
5     * This file is part of WinPT.
6     *
7     * WinPT 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 2 of the License, or
10     * (at your option) any later version.
11     *
12     * WinPT 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, gpg_write to the Free Software Foundation,
19     * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20     */
21     #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <stdio.h>
27     #include <malloc.h>
28     #include <string.h>
29     #include <stdlib.h>
30    
31     #include "gpgme.h"
32     #include "wptCard.h"
33     #include "wptCardEdit.h"
34     #include "wptErrors.h"
35    
36     #define save_write(val) ((val)? (val) : "")
37     static const char *stat_key = "";
38    
39     /* Wrapper to emulate write(). */
40     static int
41     gpg_write (int fd, const void *buf, size_t buflen)
42     {
43     HANDLE hd = (HANDLE)fd;
44     DWORD n;
45    
46     MessageBox (NULL, (const char*)buf, stat_key, MB_OK);
47     WriteFile (hd, buf, buflen, &n, NULL);
48     WriteFile (hd, "\n", 1, &n, NULL);
49     return n;
50     }
51    
52    
53     /* Common card handler. Handle general card status events. */
54     static gpgme_error_t
55     common_card_handler (GpgCardEdit *c, gpgme_status_code_t code,
56     const char *key, int fd)
57     {
58     const char *s;
59    
60     if (!strcmp (key, "cardctrl.insert_card.okay")) {
61     if (c->card_cb) {
62     s = c->card_cb (CARD_CTL_INSERT, c->cb_value);
63     gpg_write (fd, s, strlen (s));
64     return 0;
65     }
66     }
67    
68     if (code == GPGME_STATUS_CARDCTRL &&
69     (*key == CARD_CTL_NO_CARD || *key == CARD_CTL_NO_READER))
70     c->setResult (GPG_CARDRES_NOCARD);
71    
72     /* This can happen for example if a wrong PIN were
73     submitted. In such a case, we quit ASAP. */
74     if (code == GPGME_STATUS_SC_OP_FAILURE) {
75     if (key && !strcmp (key, "2"))
76     c->setResult (GPG_CARDRES_BAD_PIN);
77     else if (key && !strcmp (key, "1"))
78     c->setResult (GPG_CARDRES_CANCEL);
79     s = "quit\n";
80     gpg_write (fd, s, strlen (s));
81     return 0;
82     }
83     return 0;
84     }
85    
86    
87     /* handler for changing PINs of cards. */
88     static gpgme_error_t
89     change_pin_handler (void *opaque, gpgme_status_code_t code,
90     const char *key, int fd)
91     {
92     GpgCardEdit *c = (GpgCardEdit *)opaque;
93     const char *s = "";
94     int type;
95    
96     if (!c)
97     return gpg_error (GPG_ERR_EOF);
98    
99     stat_key = key; /* XXX: debug code */
100    
101     common_card_handler (c, code, key, fd);
102    
103     type = c->getType ();
104     if (!strcmp (key, "cardedit.prompt")) {
105     if (c->cnt == 0) {
106     c->cnt++;
107     s = "admin";
108     }
109     else if (c->cnt == 1) {
110     c->cnt++;
111     s = "passwd";
112     }
113     else {
114     c->reset ();
115     s = "quit";
116     }
117     gpg_write (fd, s, strlen (s));
118     return 0;
119     }
120     if (!strcmp (key, "cardutil.change_pin.menu")) {
121     if (c->cnt == 2) {
122     c->cnt++;
123     switch (type) {
124     case GPG_EDITCARD_CHUPIN: s = "1"; break;
125     case GPG_EDITCARD_UNBPIN: s = "2"; break;
126     case GPG_EDITCARD_CHAPIN: s = "3"; break;
127     default: s = "Q";
128     }
129     gpg_write (fd, s, strlen (s));
130     return 0;
131     }
132     else if (c->cnt > 0) {
133     gpg_write (fd, "Q", 1);
134     return 0;
135     }
136     }
137     if (type == GPG_EDITCARD_CHUPIN) {
138     if (!strcmp (key, "passphrase.pin.ask")) {
139     s = c->pin;
140     c->cnt++;
141     }
142     if (!strcmp (key, "passphrase.pin.new.ask")) {
143     s = c->pin_new;
144     c->cnt++;
145     }
146     if (!strcmp (key, "passphrase.pin.repeat") ||
147     !strcmp (key, "passphrase.ask")) {
148     s = c->pin_new;
149     c->cnt++;
150     }
151     gpg_write (fd, s, strlen (s));
152     }
153     else if (type == GPG_EDITCARD_CHAPIN) {
154     if (!strcmp (key, "passphrase.adminpin.ask")) {
155     s = c->admin_pin;
156     c->cnt++;
157     }
158     if (!strcmp (key, "passphrase.adminpin.new.ask")) {
159     s = c->pin_new;
160     c->cnt++;
161     }
162     if (!strcmp (key, "passphrase.pin.repeat") ||
163     !strcmp (key, "passphrase.ask")) {
164     s = c->pin_new;
165     c->cnt++;
166     }
167     gpg_write (fd, s, strlen (s));
168     }
169     else if (type == GPG_EDITCARD_UNBPIN) {
170     /* todo */
171     }
172    
173     return 0;
174     }
175    
176    
177     /* edit card attributes handler. */
178     static gpgme_error_t
179     editcard_handler (void * opaque, gpgme_status_code_t code,
180     const char *key, int fd)
181     {
182     GpgCardEdit *c = (GpgCardEdit *)opaque;
183     const char *s = "";
184     int type;
185    
186     if (!c)
187     return gpg_error (GPG_ERR_EOF);
188    
189     stat_key = key;
190    
191     common_card_handler (c, code, key, fd);
192    
193     type = c->getType ();
194     if (!strcmp (key, "cardedit.prompt")) {
195     if (c->getResult ()) { /* in case of an error we quit. */
196     c->cnt = 0;
197     s = "quit";
198     gpg_write (fd, s, strlen (s));
199     return 0;
200     }
201     if (c->cnt == 0) { /* first switch in the admin mode */
202     c->cnt++;
203     s = "admin";
204     gpg_write (fd, s, strlen (s));
205     return 0;
206     }
207     if (c->cnt == 1) {/* then run the send command */
208     c->cnt++;
209     switch (type) {
210     case GPG_EDITCARD_NAME: s = "name"; break;
211     case GPG_EDITCARD_KEYURL: s = "url"; break;
212     case GPG_EDITCARD_LOGIN: s = "login"; break;
213     case GPG_EDITCARD_SEX: s = "sex"; break;
214     case GPG_EDITCARD_LANG: s = "lang"; break;
215     case GPG_EDITCARD_GENKEY: s = "generate"; break;
216     default: s = "quit";
217     }
218     gpg_write (fd, s, strlen (s));
219     return 0;
220     }
221     else if (c->cnt >= 2) {/* done: send exit */
222     c->reset ();
223     s = "quit";
224     gpg_write (fd, s, strlen (s));
225     return 0;
226     }
227     }
228     if (c->cnt > 0) {
229     if (!strcmp (key, "passphrase.adminpin.ask")) {
230     gpg_write (fd, c->admin_pin, strlen (c->admin_pin));
231     return 0;
232     }
233     if (!strcmp (key, "passphrase.pin.ask")) {
234     gpg_write (fd, c->pin, strlen (c->pin));
235     return 0;
236     }
237     }
238     switch (type) {
239     case GPG_EDITCARD_NAME:
240     if (!strcmp (key, "keygen.smartcard.surname")) {
241     s = save_write (c->edit.surname);
242     gpg_write (fd, s, strlen (s));
243     }
244     else if (!strcmp (key, "keygen.smartcard.givenname")) {
245     s = save_write (c->edit.givenname);
246     gpg_write (fd, s, strlen (s));
247     }
248     break;
249    
250     case GPG_EDITCARD_KEYURL:
251     if (!strcmp (key, "cardedit.change_url")) {
252     s = save_write (c->edit.keyurl);
253     gpg_write (fd, s, strlen (s));
254     }
255     break;
256    
257     case GPG_EDITCARD_LOGIN:
258     if (!strcmp (key, "cardedit.change_login")) {
259     s = save_write (c->edit.login);
260     gpg_write (fd, s, strlen (s));
261     }
262     break;
263    
264     case GPG_EDITCARD_SEX:
265     if (!strcmp (key, "cardedit.change_sex")) {
266     static char buf[2];
267     if (c->edit.sex != 'M'
268     && c->edit.sex != 'F'
269     && c->edit.sex != ' ') {
270     buf[0] = ' '; buf[1] = 0;
271     }
272     else {
273     buf[0] = c->edit.sex;
274     buf[1] = 0;
275     }
276     gpg_write (fd, buf, strlen (buf));
277     }
278     break;
279    
280     case GPG_EDITCARD_LANG:
281     if (!strcmp (key, "cardedit.change_lang")) {
282     s = save_write (c->edit.lang);
283     gpg_write (fd, s, strlen (s));
284     }
285     break;
286    
287     case GPG_EDITCARD_GENKEY:
288     if (!strcmp (key, "passphrase.enter"))
289     gpg_write (fd, c->keygen.pass, strlen (c->keygen.pass));
290     if (!strcmp (key, "cardedit.genkeys.backup_enc")) {
291     s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N";
292     gpg_write (fd, s, strlen (s));
293     }
294     if (!strcmp (key, "cardedit.genkeys.replace_keys")) {
295     if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE))
296     c->cancel = 1;
297     s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N";
298     gpg_write (fd, s, strlen (s));
299     }
300     else if (!strcmp (key, "keygen.valid")) {
301     s = save_write (c->keygen.expdate);
302     gpg_write (fd, s, strlen (s));
303     }
304     else if (!strcmp (key, "keygen.name")) {
305     s = save_write (c->keygen.name);
306     gpg_write (fd, s, strlen (s));
307     }
308     else if (!strcmp (key, "keygen.email")) {
309     s = save_write (c->keygen.email);
310     gpg_write (fd, s, strlen (s));
311     }
312     else if (!strcmp (key, "keygen.comment")) {
313     s = save_write (c->keygen.comment);
314     gpg_write (fd, s, strlen (s));
315     }
316     break;
317    
318     default:
319     break;
320     }
321    
322     return 0;
323     }
324    
325    
326     /* Check if a GPG status code occured which marks the
327     current operation as failed.
328     Return value: gpg error constant. */
329     static gpgme_error_t
330     map_result (GpgCardEdit *ce)
331     {
332     if (!ce->getResult ())
333     return gpg_error (GPG_ERR_NO_ERROR);
334     if (ce->getResult () & GPG_CARDRES_NOCARD)
335     return gpg_error (GPG_ERR_CARD_NOT_PRESENT);
336     if (ce->getResult () & GPG_CARDRES_BAD_PIN)
337     return gpg_error (GPG_ERR_BAD_PIN);
338     return 0;
339     }
340    
341    
342     /* Wrapper around the gpgme card interface.
343     @ctx: context to use.
344     @ce: card edit context.
345     Return value: 0 on success. */
346     gpgme_error_t
347     gpg_card_edit (gpgme_ctx_t ctx, GpgCardEdit *ce)
348     {
349     gpgme_data_t out;
350     gpgme_error_t err;
351    
352     err = gpgme_data_new (&out);
353     if (err)
354     return err;
355    
356     if (ce->getType () > GPG_EDITCARD_CHPIN_ID)
357     err = gpgme_op_card_edit (ctx, NULL, change_pin_handler, ce, out);
358     else
359     err = gpgme_op_card_edit (ctx, NULL, editcard_handler, ce, out);
360     if (!err)
361     err = map_result (ce);
362    
363     gpgme_data_release (out);
364     return err;
365     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26