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

Annotation of /trunk/Src/wptCardEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91 - (hide annotations)
Tue Nov 22 09:23:02 2005 UTC (19 years, 3 months ago) by twoaday
File size: 9137 byte(s)
Remove debug output for card code.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26