1 |
twoaday |
211 |
/* wptGroupManager.cpp |
2 |
|
|
* Copyright (C) 2006 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 WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
#if 0 /* XXX: not yet finished. */ |
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
twoaday |
121 |
|
25 |
twoaday |
211 |
#include <windows.h> |
26 |
|
|
#include "wptTypes.h" |
27 |
|
|
#include "wptCommonCtl.h" |
28 |
|
|
#include "wptGPG.h" |
29 |
|
|
#include "wptKeyManager.h" |
30 |
|
|
#include "wptKeylist.h" |
31 |
|
|
#include "wptErrors.h" |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
static void |
35 |
|
|
get_selection (HWND tree, char *text, DWORD tlen) |
36 |
|
|
{ |
37 |
|
|
TVITEM tvi; |
38 |
|
|
|
39 |
|
|
memset (&tvi, 0, sizeof tvi); |
40 |
|
|
tvi.hItem = TreeView_GetSelection (tree); |
41 |
|
|
tvi.pszText = text; |
42 |
|
|
tvi.cchTextMax = tlen; |
43 |
|
|
tvi.mask = TVIF_TEXT; |
44 |
|
|
TreeView_GetItem (tree, &tvi); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
static gpg_optfile_t |
49 |
|
|
groupdb_open (char **r_cfgfile) |
50 |
twoaday |
121 |
{ |
51 |
|
|
gpg_optfile_t opt; |
52 |
twoaday |
211 |
char *optfile; |
53 |
twoaday |
121 |
int err = 0; |
54 |
|
|
|
55 |
twoaday |
211 |
optfile = get_gnupg_cfgfile (); |
56 |
|
|
if (parse_gpg_options (optfile, &opt)) |
57 |
twoaday |
121 |
err = 1; |
58 |
twoaday |
211 |
if (r_cfgfile) |
59 |
|
|
*r_cfgfile = optfile; |
60 |
|
|
else |
61 |
|
|
free_if_alloc (optfile); |
62 |
twoaday |
121 |
return err? NULL : opt; |
63 |
twoaday |
211 |
} |
64 |
twoaday |
121 |
|
65 |
|
|
|
66 |
twoaday |
211 |
#if 0 |
67 |
twoaday |
121 |
int |
68 |
twoaday |
211 |
km_groupdb_expand_recipients (const char *name, gpgme_recipients_t rset) |
69 |
twoaday |
121 |
{ |
70 |
|
|
gpg_keycache_t kc; |
71 |
|
|
gpgme_key_t pk; |
72 |
|
|
gpg_optfile_t opt; |
73 |
|
|
gpg_group_t grp; |
74 |
|
|
gpg_member_t mbr; |
75 |
|
|
int no_trust = 0, n; |
76 |
|
|
|
77 |
|
|
kc = keycache_get_ctx( 1 ); |
78 |
|
|
if( !kc ) |
79 |
|
|
BUG( NULL ); |
80 |
|
|
|
81 |
|
|
opt = km_groupdb_open( ); |
82 |
|
|
if( !opt ) |
83 |
|
|
return WPTERR_FILE_OPEN; |
84 |
|
|
|
85 |
|
|
grp = find_group( opt, name ); |
86 |
|
|
if( !grp ) |
87 |
|
|
return WPTERR_GENERAL; |
88 |
|
|
|
89 |
|
|
/* we are paranoid and check that all group members exist in the |
90 |
|
|
key cache. there is no need that it is really the real key, but |
91 |
|
|
an entry should be available. the rest is up to GPG. */ |
92 |
|
|
for( mbr = grp->list; mbr; mbr = mbr->next ) { |
93 |
|
|
if( gpgme_keycache_find_key( kc, mbr->name, 0, &pk ) ) |
94 |
|
|
BUG( NULL ); |
95 |
|
|
n = count_userids (pk); |
96 |
|
|
while( n-- ) { |
97 |
|
|
gpgme_user_id_t u = get_nth_userid (pk, n); |
98 |
|
|
const char * s = u->uid; |
99 |
|
|
if( s && stristr( s, mbr->name ) |
100 |
|
|
&& u->validity < 3 ) |
101 |
|
|
no_trust++; |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
gpgme_recipients_add_name( rset, name ); |
106 |
|
|
release_gpg_options( opt ); |
107 |
|
|
|
108 |
|
|
return no_trust; |
109 |
|
|
} /* km_groupdb_expand_recipients */ |
110 |
twoaday |
211 |
#endif |
111 |
twoaday |
121 |
|
112 |
|
|
|
113 |
|
|
static HTREEITEM |
114 |
twoaday |
211 |
km_tv_insert_item (HWND tree, HTREEITEM parent, const char *text) |
115 |
twoaday |
121 |
{ |
116 |
|
|
TVINSERTSTRUCT tvis; |
117 |
|
|
HTREEITEM node; |
118 |
|
|
|
119 |
twoaday |
211 |
memset (&tvis, 0, sizeof tvis); |
120 |
twoaday |
121 |
tvis.hParent = parent; |
121 |
|
|
tvis.hInsertAfter = TVI_LAST; |
122 |
|
|
tvis.item.mask = TVIF_TEXT; |
123 |
|
|
tvis.item.pszText = (char *)text; |
124 |
twoaday |
211 |
node = TreeView_InsertItem (tree, &tvis); |
125 |
twoaday |
121 |
return node; |
126 |
twoaday |
211 |
} |
127 |
twoaday |
121 |
|
128 |
|
|
|
129 |
|
|
int |
130 |
twoaday |
211 |
km_groups_new (km_group_t *r_gc, HWND ctrl) |
131 |
twoaday |
121 |
{ |
132 |
|
|
km_group_t gc; |
133 |
|
|
|
134 |
|
|
gc = new km_group_s; |
135 |
|
|
if (!gc) |
136 |
|
|
BUG (NULL); |
137 |
twoaday |
211 |
memset (gc, 0, sizeof *gc); |
138 |
twoaday |
121 |
gc->tree = ctrl; |
139 |
twoaday |
211 |
gc->gh = groupdb_open (&gc->cfgfile); |
140 |
twoaday |
121 |
*r_gc = gc; |
141 |
|
|
return 0; |
142 |
twoaday |
211 |
} |
143 |
twoaday |
121 |
|
144 |
|
|
|
145 |
|
|
void |
146 |
twoaday |
211 |
km_groups_sync (km_group_t gc) |
147 |
twoaday |
121 |
{ |
148 |
twoaday |
211 |
commit_gpg_options (gc->cfgfile, gc->gh); |
149 |
twoaday |
121 |
gc->need_sync = 0; |
150 |
twoaday |
211 |
} |
151 |
twoaday |
121 |
|
152 |
|
|
|
153 |
|
|
void |
154 |
|
|
km_groups_release (km_group_t gc) |
155 |
|
|
{ |
156 |
twoaday |
211 |
if (!gc) |
157 |
|
|
return; |
158 |
twoaday |
121 |
|
159 |
twoaday |
211 |
/* xxx: this reset the default key (sync=1) */ |
160 |
|
|
gc->need_sync=0; |
161 |
|
|
if (gc->need_sync) |
162 |
|
|
km_groups_sync (gc); |
163 |
|
|
if (gc->gh) |
164 |
|
|
release_gpg_options (gc->gh); |
165 |
|
|
free_if_alloc (gc->cfgfile); |
166 |
|
|
gc->gh = NULL; |
167 |
|
|
gc->tree = NULL; |
168 |
|
|
gc->need_sync = 0; |
169 |
|
|
delete gc; |
170 |
|
|
} |
171 |
twoaday |
121 |
|
172 |
twoaday |
211 |
|
173 |
twoaday |
121 |
int |
174 |
twoaday |
211 |
km_groups_load (km_group_t gc) |
175 |
twoaday |
121 |
{ |
176 |
|
|
HTREEITEM n; |
177 |
|
|
gpg_group_t grp, g; |
178 |
|
|
gpg_member_t mbr; |
179 |
|
|
u32 gid = 0; |
180 |
|
|
|
181 |
twoaday |
211 |
if (!gc->gh) |
182 |
twoaday |
121 |
return 0; |
183 |
|
|
grp = gc->gh->grp; |
184 |
twoaday |
211 |
if (!grp) |
185 |
twoaday |
121 |
return 0; /* no groups */ |
186 |
|
|
|
187 |
twoaday |
211 |
for (g = grp; g; g = g->next) { |
188 |
|
|
n = km_tv_insert_item (gc->tree, NULL, g->name); |
189 |
|
|
for (mbr = g->list; mbr; mbr = mbr->next) { |
190 |
|
|
if (mbr->used && mbr->name) |
191 |
|
|
km_tv_insert_item (gc->tree, n, mbr->name); |
192 |
twoaday |
121 |
} |
193 |
|
|
} |
194 |
twoaday |
211 |
DragAcceptFiles (gc->tree, TRUE); |
195 |
twoaday |
121 |
gc->need_sync = 0; |
196 |
|
|
return 0; |
197 |
twoaday |
211 |
} |
198 |
twoaday |
121 |
|
199 |
|
|
|
200 |
|
|
int |
201 |
|
|
km_groups_add (km_group_t gc, listview_ctrl_t lv, int km_index) |
202 |
|
|
{ |
203 |
|
|
char uid[128], valid[64], text[128]; |
204 |
|
|
int i_valid; |
205 |
|
|
|
206 |
twoaday |
211 |
get_selection (gc->tree, text, sizeof (text)-1); |
207 |
|
|
listview_get_item_text( lv, km_index, KM_COL_UID, uid, sizeof uid -1 ); |
208 |
|
|
listview_get_item_text( lv, km_index, KM_COL_VALID, valid, sizeof valid -1 ); |
209 |
twoaday |
121 |
|
210 |
|
|
if( strstr( valid, "Ultimate" ) ) |
211 |
|
|
i_valid = 5; |
212 |
|
|
else if( !strstr( valid, "Full" ) ) |
213 |
|
|
i_valid = 4; |
214 |
|
|
else if( !strstr( valid, "Marginal" ) ) |
215 |
|
|
i_valid = 3; |
216 |
|
|
else |
217 |
|
|
i_valid = 0; |
218 |
|
|
|
219 |
|
|
/* we can't add the full name. one way would be to use the first |
220 |
|
|
text until a space appears. |
221 |
|
|
group_add_entry(&gc->gh, gid, i_valid, uid); |
222 |
|
|
treeview_add_item(gc->tree, tvi.hItem, uid); |
223 |
|
|
*/ |
224 |
|
|
gc->need_sync = 1; |
225 |
|
|
|
226 |
|
|
return 0; |
227 |
|
|
} /* km_groups_add */ |
228 |
|
|
|
229 |
|
|
|
230 |
|
|
static int |
231 |
twoaday |
211 |
km_groups_del_main (km_group_t gc) |
232 |
twoaday |
121 |
{ |
233 |
|
|
char text[128]; |
234 |
|
|
int id; |
235 |
twoaday |
211 |
TVITEM tvi; |
236 |
|
|
|
237 |
|
|
memset (&tvi, 0, sizeof tvi); |
238 |
|
|
tvi.hItem = TreeView_GetSelection (gc->tree); |
239 |
twoaday |
121 |
tvi.pszText = text; |
240 |
twoaday |
211 |
tvi.cchTextMax = sizeof (text)-1; |
241 |
twoaday |
121 |
tvi.mask = TVIF_TEXT; |
242 |
twoaday |
211 |
TreeView_GetItem (gc->tree, &tvi); |
243 |
|
|
|
244 |
twoaday |
121 |
id = log_box( _("Key Manager"), MB_INFO_ASK, |
245 |
|
|
_("Do you really want to delete this group?\n\n%s"), text); |
246 |
twoaday |
211 |
if (id == IDNO) |
247 |
twoaday |
121 |
return 0; |
248 |
twoaday |
211 |
delete_group (gc->gh, text); |
249 |
|
|
TreeView_DeleteItem (gc->tree, &tvi); |
250 |
twoaday |
121 |
gc->need_sync = 1; |
251 |
|
|
return 0; |
252 |
twoaday |
211 |
} |
253 |
twoaday |
121 |
|
254 |
|
|
|
255 |
|
|
static int |
256 |
twoaday |
211 |
km_groups_del_entry (km_group_t gc) |
257 |
twoaday |
121 |
{ |
258 |
|
|
TVITEM tvi; |
259 |
|
|
HTREEITEM root; |
260 |
|
|
int id; |
261 |
|
|
char text[128], info[256]; |
262 |
twoaday |
211 |
gpg_group_t grp = NULL; |
263 |
|
|
|
264 |
|
|
memset (&tvi, 0, sizeof tvi); |
265 |
|
|
tvi.hItem = TreeView_GetSelection (gc->tree); |
266 |
twoaday |
121 |
tvi.pszText = text; |
267 |
twoaday |
211 |
tvi.cchTextMax = sizeof (text)-1; |
268 |
twoaday |
121 |
tvi.mask = TVIF_TEXT; |
269 |
twoaday |
211 |
TreeView_GetItem (gc->tree, &tvi); |
270 |
|
|
|
271 |
twoaday |
121 |
_snprintf( info, sizeof info -1, |
272 |
|
|
_("Do you really want to delete this entry?\n\n%s"), text ); |
273 |
|
|
|
274 |
|
|
id = msg_box( gc->tree, info, _("Key Manager"), MB_INFO_ASK ); |
275 |
|
|
if( id == IDNO ) |
276 |
|
|
return 0; |
277 |
|
|
|
278 |
|
|
root = TreeView_GetParent( gc->tree, tvi.hItem ); |
279 |
|
|
if( root ) { |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
delete_member( gc->gh, /*fixme*/NULL, text ); |
283 |
|
|
TreeView_DeleteItem( gc->tree, &tvi ); |
284 |
|
|
gc->need_sync = 1; |
285 |
|
|
return 0; |
286 |
|
|
} /* km_groups_del_entry */ |
287 |
|
|
|
288 |
|
|
|
289 |
|
|
int |
290 |
twoaday |
211 |
km_groups_del (km_group_t gc) |
291 |
twoaday |
121 |
{ |
292 |
twoaday |
211 |
if (TreeView_GetParent (gc->tree, TreeView_GetSelection (gc->tree))) |
293 |
twoaday |
121 |
return km_groups_del_entry( gc ); |
294 |
|
|
else |
295 |
|
|
return km_groups_del_main( gc ); |
296 |
twoaday |
211 |
} |
297 |
twoaday |
121 |
#endif |
298 |
twoaday |
211 |
|