1 |
twoaday |
23 |
/* wptCardEditCB.cpp - Card callbacks |
2 |
|
|
* Copyright (C) 2003, 2004, 2005 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <stdio.h> |
22 |
|
|
#include <malloc.h> |
23 |
|
|
#include <string.h> |
24 |
|
|
#include <stdlib.h> |
25 |
|
|
#include <io.h> |
26 |
|
|
|
27 |
|
|
#include "w32gpgme.h" |
28 |
|
|
#include "wptCardEdit.h" |
29 |
|
|
#include "wptCard.h" |
30 |
|
|
|
31 |
|
|
#define save_write(val) ((val)? (val) : "") |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
static gpgme_error_t |
36 |
|
|
change_pin_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
37 |
|
|
{ |
38 |
|
|
GpgCardEdit *c = (GpgCardEdit *)opaque; |
39 |
|
|
const char *s = ""; |
40 |
|
|
|
41 |
|
|
if (!c) |
42 |
|
|
return gpg_error (GPG_ERR_EOF); |
43 |
|
|
|
44 |
|
|
if (!strcmp (key, "cardctrl.insert_card_okay")) { |
45 |
|
|
if (c->card_cb) { |
46 |
|
|
s = c->card_cb (1, c->cb_value); |
47 |
|
|
write (fd, s, strlen (s)); |
48 |
|
|
return 0; |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
if (!strcmp (key, "cardedit.prompt")) { |
52 |
|
|
if (c->cnt++ == 0) |
53 |
|
|
s = "admin"; |
54 |
|
|
else if (c->cnt++ == 1) |
55 |
|
|
s = "passwd"; |
56 |
|
|
else |
57 |
|
|
s = "quit"; |
58 |
|
|
write (fd, s, strlen (s)); |
59 |
|
|
return 0; |
60 |
|
|
} |
61 |
|
|
if (!strcmp (key, "cardutil.change_pin.menu")) { |
62 |
|
|
if (c->cnt++ == 2) { |
63 |
|
|
switch (c->type) { |
64 |
|
|
case GPG_EDITCARD_CHUPIN: s = "1"; |
65 |
|
|
case GPG_EDITCARD_UNBPIN: s = "2"; |
66 |
|
|
case GPG_EDITCARD_CHAPIN: s = "3"; |
67 |
|
|
default: c->cnt = 0; s = "Q"; |
68 |
|
|
} |
69 |
|
|
write (fd, s, strlen (s)); |
70 |
|
|
return 0; |
71 |
|
|
} |
72 |
|
|
else if (c->cnt > 0) { |
73 |
|
|
c->cnt = 0; |
74 |
|
|
write (fd, "Q", 1); |
75 |
|
|
return 0; |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
if (c->type == GPG_EDITCARD_CHUPIN) { |
79 |
|
|
if (!strcmp (key, "passphrase.pin.ask")) { |
80 |
|
|
s = c->pin; |
81 |
|
|
c->cnt++; |
82 |
|
|
} |
83 |
|
|
if (!strcmp (key, "passphrase.pin.new.ask")) { |
84 |
|
|
s = c->pin_new; |
85 |
|
|
c->cnt++; |
86 |
|
|
} |
87 |
|
|
if (!strcmp (key, "passphrase.pin.repeat")) { |
88 |
|
|
s = c->pin_new; |
89 |
|
|
c->cnt++; |
90 |
|
|
} |
91 |
|
|
write (fd, s, strlen (s)); |
92 |
|
|
} |
93 |
|
|
else if (c->type == GPG_EDITCARD_CHAPIN) { |
94 |
|
|
if (!strcmp (key, "passphrase.adminpin.ask")) { |
95 |
|
|
s = c->pin; |
96 |
|
|
c->cnt++; |
97 |
|
|
} |
98 |
|
|
if (!strcmp (key, "passphrase.adminpin.new.ask")) { |
99 |
|
|
s = c->pin_new; |
100 |
|
|
c->cnt++; |
101 |
|
|
} |
102 |
|
|
if (!strcmp (key, "passphrase.pin.repeat")) { |
103 |
|
|
s = c->pin_new; |
104 |
|
|
c->cnt++; |
105 |
|
|
} |
106 |
|
|
write (fd, s, strlen (s)); |
107 |
|
|
} |
108 |
|
|
else if (c->type == GPG_EDITCARD_UNBPIN) { |
109 |
|
|
/* todo */ |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
return 0; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
static gpgme_error_t |
117 |
|
|
editcard_handler (void * opaque, gpgme_status_code_t code, |
118 |
|
|
const char *key, int fd) |
119 |
|
|
{ |
120 |
|
|
GpgCardEdit *c = (GpgCardEdit *)opaque; |
121 |
|
|
const char *s = ""; |
122 |
|
|
|
123 |
|
|
if (!c) |
124 |
|
|
return gpg_error (GPG_ERR_EOF); |
125 |
|
|
|
126 |
|
|
if (!strcmp (key, "cardctrl.insert_card.okay")) { |
127 |
|
|
if (c->card_cb) { |
128 |
|
|
s = c->card_cb (1, c->cb_value); |
129 |
|
|
write (fd, s, strlen (s)); |
130 |
|
|
return 0; |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
if (!strcmp (key, "cardedit.prompt")) { |
135 |
|
|
if (c->cnt++ == 0) { /* first switch in the admin mode */ |
136 |
|
|
s = "admin"; |
137 |
|
|
write (fd, s, strlen (s)); |
138 |
|
|
return 0; |
139 |
|
|
} |
140 |
|
|
if (c->cnt == 1) {/* then run the send command */ |
141 |
|
|
c->cnt++; |
142 |
|
|
switch (c->type) { |
143 |
|
|
case GPG_EDITCARD_NAME: s = "name"; |
144 |
|
|
case GPG_EDITCARD_KEYURL: s = "url"; |
145 |
|
|
case GPG_EDITCARD_LOGIN: s = "login"; |
146 |
|
|
case GPG_EDITCARD_SEX: s = "sex"; |
147 |
|
|
case GPG_EDITCARD_LANG: s = "lang"; |
148 |
|
|
case GPG_EDITCARD_GENKEY: s = "generate"; |
149 |
|
|
default: s = "quit"; |
150 |
|
|
} |
151 |
|
|
write (fd, s, strlen (s)); |
152 |
|
|
return 0; |
153 |
|
|
} |
154 |
|
|
else if (c->cnt >= 2) {/* done: send exit */ |
155 |
|
|
c->cnt = 0; |
156 |
|
|
s = "quit"; |
157 |
|
|
write (fd, s, strlen (s)); |
158 |
|
|
return 0; |
159 |
|
|
} |
160 |
|
|
} |
161 |
|
|
if (c->cnt > 0) { |
162 |
|
|
if (!strcmp (key, "passphrase.adminpin.ask")) { |
163 |
|
|
write (fd, c->admin_pin, strlen (c->admin_pin)); |
164 |
|
|
return 0; |
165 |
|
|
} |
166 |
|
|
if (!strcmp (key, "passphrase.pin.ask")) { |
167 |
|
|
write (fd, c->pin, strlen (c->pin)); |
168 |
|
|
return 0; |
169 |
|
|
} |
170 |
|
|
} |
171 |
|
|
switch (c->type) { |
172 |
|
|
case GPG_EDITCARD_NAME: |
173 |
|
|
if (!strcmp (key, "keygen.smartcard.surname")) { |
174 |
|
|
s = save_write (c->edit.surname); |
175 |
|
|
write (fd, s, strlen (s)); |
176 |
|
|
} |
177 |
|
|
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
178 |
|
|
s = save_write (c->edit.givenname); |
179 |
|
|
write (fd, s, strlen (s)); |
180 |
|
|
} |
181 |
|
|
break; |
182 |
|
|
|
183 |
|
|
case GPG_EDITCARD_KEYURL: |
184 |
|
|
if (!strcmp (key, "cardedit.change_url")) { |
185 |
|
|
s = save_write (c->edit.keyurl); |
186 |
|
|
write (fd, s, strlen (s)); |
187 |
|
|
} |
188 |
|
|
break; |
189 |
|
|
|
190 |
|
|
case GPG_EDITCARD_LOGIN: |
191 |
|
|
if (!strcmp (key, "cardedit.change_login")) { |
192 |
|
|
s = save_write (c->edit.login); |
193 |
|
|
write (fd, s, strlen (s)); |
194 |
|
|
} |
195 |
|
|
break; |
196 |
|
|
|
197 |
|
|
case GPG_EDITCARD_SEX: |
198 |
|
|
if (!strcmp (key, "cardedit.change_sex")) { |
199 |
|
|
static char buf[2]; |
200 |
|
|
if (c->edit.sex != 'M' |
201 |
|
|
&& c->edit.sex != 'F' |
202 |
|
|
&& c->edit.sex != ' ') { |
203 |
|
|
buf[0] = ' '; buf[1] = 0; |
204 |
|
|
} |
205 |
|
|
else { |
206 |
|
|
buf[0] = c->edit.sex; buf[1] = 0; |
207 |
|
|
} |
208 |
|
|
write (fd, buf, strlen (buf)); |
209 |
|
|
} |
210 |
|
|
break; |
211 |
|
|
|
212 |
|
|
case GPG_EDITCARD_LANG: |
213 |
|
|
if (!strcmp (key, "cardedit.change_lang")) { |
214 |
|
|
s = save_write (c->edit.lang); |
215 |
|
|
write (fd, s, strlen (s)); |
216 |
|
|
} |
217 |
|
|
break; |
218 |
|
|
|
219 |
|
|
case GPG_EDITCARD_GENKEY: |
220 |
|
|
if (!strcmp (key, "passphrase.enter")) |
221 |
|
|
write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
222 |
|
|
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
223 |
|
|
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
224 |
|
|
write (fd, s, strlen (s)); |
225 |
|
|
} |
226 |
|
|
if (!strcmp (key, "cardedit.genkeys.replace_keys")) { |
227 |
|
|
if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE)) |
228 |
|
|
c->cancel = 1; |
229 |
|
|
s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N"; |
230 |
|
|
write (fd, s, strlen (s)); |
231 |
|
|
} |
232 |
|
|
else if (!strcmp (key, "keygen.valid")) { |
233 |
|
|
s = save_write (c->keygen.expdate); |
234 |
|
|
write (fd, s, strlen (s)); |
235 |
|
|
} |
236 |
|
|
else if (!strcmp (key, "keygen.name")) { |
237 |
|
|
s = save_write (c->keygen.name); |
238 |
|
|
write (fd, s, strlen (s)); |
239 |
|
|
} |
240 |
|
|
else if (!strcmp (key, "keygen.email")) { |
241 |
|
|
s = save_write (c->keygen.email); |
242 |
|
|
write (fd, s, strlen (s)); |
243 |
|
|
} |
244 |
|
|
else if (!strcmp (key, "keygen.comment")) { |
245 |
|
|
s = save_write (c->keygen.comment); |
246 |
|
|
write (fd, s, strlen (s)); |
247 |
|
|
} |
248 |
|
|
break; |
249 |
|
|
|
250 |
|
|
default: |
251 |
|
|
break; |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
return 0; |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
|
258 |
|
|
gpgme_error_t |
259 |
|
|
gpg_card_edit (gpgme_ctx_t ctx, GpgCardEdit *ce) |
260 |
|
|
{ |
261 |
|
|
gpgme_error_t err; |
262 |
|
|
|
263 |
|
|
if (ce->type > GPG_EDITCARD_CHPIN_ID) |
264 |
|
|
err = gpgme_op_card_edit (ctx, NULL, change_pin_handler, ce, NULL); |
265 |
|
|
else |
266 |
|
|
err = gpgme_op_card_edit (ctx, NULL, editcard_handler, ce, NULL); |
267 |
|
|
return err; |
268 |
|
|
} |