1 |
/* wptGPGParser.cpp - GPG config file parser |
/* wptGPGParser.cpp - GPG config file parser |
2 |
* Copyright (C) 2002, 2003, 2004 Timo Schulz |
* Copyright (C) 2002, 2003, 2004 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
15 |
* |
* |
16 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
|
#ifdef HAVE_CONFIG_H |
22 |
|
#include <config.h> |
23 |
|
#endif |
24 |
|
|
25 |
|
#include <stdio.h> |
26 |
#include <stdio.h> |
#include <stdio.h> |
27 |
#include <string.h> |
#include <string.h> |
28 |
#include <windows.h> |
#include <windows.h> |
29 |
|
|
30 |
#include "wptTypes.h" |
#include "wptTypes.h" |
31 |
#include "wptW32API.h" |
#include "wptW32API.h" |
32 |
#include "wptGPG.h" |
#include "wptGPG.h" |
33 |
#include "wptErrors.h" |
#include "wptErrors.h" |
34 |
|
|
35 |
|
static void |
36 |
|
add_opaque_option (gpg_optfile_t opt, const char * line, int used) |
37 |
|
{ |
38 |
|
gpg_option_t e, t; |
39 |
|
|
40 |
|
e = new gpg_option_s; |
41 |
|
if (!e) |
42 |
|
BUG (0); |
43 |
|
memset (e, 0, sizeof *e); |
44 |
|
e->used = used > 0? 1 : 0; |
45 |
|
e->name = m_strdup (line); |
46 |
|
e->type = ENTRY_OPAQUE; |
47 |
|
if (!opt->list) |
48 |
|
opt->list = e; |
49 |
|
else { |
50 |
|
for (t = opt->list; t->next; t = t->next) |
51 |
|
; |
52 |
|
t->next = e; |
53 |
|
} |
54 |
|
} /* add_opaque_option */ |
55 |
|
|
56 |
|
|
57 |
|
static void |
58 |
|
add_single_option (gpg_optfile_t opt, const char * name, int used) |
59 |
|
{ |
60 |
|
gpg_option_t e, t; |
61 |
|
|
62 |
|
e = new gpg_option_s; |
63 |
|
if (!e) |
64 |
|
BUG (0); |
65 |
|
memset (e, 0, sizeof *e); |
66 |
|
e->used = used > 0? 1 : 0; |
67 |
|
e->name = m_strdup (name); |
68 |
|
e->type = ENTRY_SINGLE; |
69 |
|
if (!opt->list) |
70 |
|
opt->list = e; |
71 |
|
else { |
72 |
|
for (t = opt->list; t->next; t = t->next) |
73 |
|
; |
74 |
|
t->next = e; |
75 |
|
} |
76 |
|
} /* add_single_option */ |
77 |
|
|
78 |
|
|
79 |
|
static int |
80 |
|
add_group_option( gpg_optfile_t opt, const char *name, int used ) |
81 |
|
{ |
82 |
|
gpg_option_t e, t; |
83 |
|
|
84 |
|
e = new gpg_option_s; |
85 |
|
if( !e ) |
86 |
|
BUG( NULL ); |
87 |
|
memset( e, 0, sizeof *e ); |
88 |
|
e->used = used > 0? 1 : 0; |
89 |
|
e->name = m_strdup( name ); |
90 |
|
e->type = ENTRY_GROUP; |
91 |
|
if( !opt->list ) |
92 |
|
opt->list = e; |
93 |
|
else { |
94 |
|
for( t = opt->list; t->next; t = t->next ) |
95 |
|
; |
96 |
|
t->next = e; |
97 |
|
} |
98 |
|
return 0; |
99 |
|
} /* add_group_option */ |
100 |
|
|
101 |
|
|
102 |
|
static size_t |
103 |
|
count_whitespaces( char *line ) |
104 |
|
{ |
105 |
|
size_t i = 0, ncount = 0; |
106 |
|
|
107 |
|
if( !strchr( line, ' ' ) ) |
108 |
|
return 0; |
109 |
|
for( i = 0; i < strlen( line ); i++ ) { |
110 |
|
if( line[i] == ' ' ) |
111 |
|
ncount++; |
112 |
|
} |
113 |
|
|
114 |
|
return ncount; |
115 |
|
} /* count_whitespaces */ |
116 |
|
|
117 |
|
|
118 |
|
static int |
119 |
|
add_multi_option( gpg_optfile_t opt, char *line, int used ) |
120 |
|
{ |
121 |
|
gpg_option_t e ,t; |
122 |
|
char *p; |
123 |
|
int state = 0; |
124 |
|
|
125 |
|
e = new gpg_option_s; |
126 |
|
if( !e ) |
127 |
|
BUG( NULL ); |
128 |
|
memset( e, 0, sizeof *e ); |
129 |
|
if( count_whitespaces( line ) == 1 ) { |
130 |
|
while( (p = strsep( &line, " " )) && state != 2 ) { |
131 |
|
switch( state ) { |
132 |
|
case 0: e->name = m_strdup( p ); break; |
133 |
|
case 1: e->val = m_strdup( p ); break; |
134 |
|
} |
135 |
|
state++; |
136 |
|
} |
137 |
|
} |
138 |
|
else { |
139 |
|
p = strchr( line, ' ' ); |
140 |
|
state = p - line; |
141 |
|
e->name = new char[state + 1]; |
142 |
|
if( !e->name ) |
143 |
|
BUG( NULL ); |
144 |
|
memcpy( e->name, line, state ); |
145 |
|
e->name[state] = '\0'; |
146 |
|
strncpy( e->name, line, state ); |
147 |
|
e->val = m_strdup( line + state + 1 ); |
148 |
|
} |
149 |
|
e->used = used; |
150 |
|
e->type = ENTRY_MULTI; |
151 |
|
if( !opt->list ) |
152 |
|
opt->list = e; |
153 |
|
else { |
154 |
|
for( t=opt->list; t->next; t = t->next ) |
155 |
|
; |
156 |
|
t->next = e; |
157 |
|
} |
158 |
|
return 0; |
159 |
|
} /* add_multi_option */ |
160 |
|
|
161 |
|
|
162 |
|
static gpg_group_t |
163 |
|
new_group (gpg_group_t * grp, const char * name) |
164 |
|
{ |
165 |
|
gpg_group_t g, t; |
166 |
|
|
167 |
|
g = new gpg_group_s; |
168 |
|
if( !g ) |
169 |
|
BUG( NULL ); |
170 |
|
memset( g, 0, sizeof *g ); |
171 |
|
g->used = 1; |
172 |
|
g->name = m_strdup( name ); |
173 |
|
if( !*grp ) |
174 |
|
*grp = g; |
175 |
|
else { |
176 |
|
for( t = *grp; t->next; t = t->next ) |
177 |
|
; |
178 |
|
t->next = g; |
179 |
|
} |
180 |
|
return g; |
181 |
|
} /* new_group */ |
182 |
|
|
183 |
|
|
184 |
|
static const char* |
185 |
|
_add_group( gpg_optfile_t opt, const char *line ) |
186 |
|
{ |
187 |
|
char *p, *buf, *buf2, *name = NULL; |
188 |
|
gpg_group_t g; |
189 |
|
gpg_member_t m = NULL, t; |
190 |
|
|
191 |
|
p = strchr( line, '=' ); |
192 |
|
if( p == NULL || strncmp( line, "group ", 6 ) ) |
193 |
|
return NULL; |
194 |
|
buf = m_strdup( line + (p - line + 1) ); |
195 |
|
if( !buf ) |
196 |
|
BUG( NULL ); |
197 |
|
line += 6; |
198 |
|
p = strchr( line, '=' ); |
199 |
|
if( p ) { |
200 |
|
size_t pos = p - line; |
201 |
|
name = new char[pos + 1]; |
202 |
|
if( !name ) |
203 |
|
BUG( NULL ); |
204 |
|
memset( name, 0, pos+1 ); |
205 |
|
strncpy( name, line, pos ); |
206 |
|
} |
207 |
|
g = new_group( &opt->grp, name ); |
208 |
|
buf2 = buf; |
209 |
|
while( (p = strsep( &buf2, " " )) && *p ) { |
210 |
|
m = new gpg_member_s; |
211 |
|
if( !m ) |
212 |
|
BUG( NULL ); |
213 |
|
memset( m, 0, sizeof *m ); |
214 |
|
m->used = 1; |
215 |
|
m->name = m_strdup( p ); |
216 |
|
if( !g->list ) |
217 |
|
g->list = m; |
218 |
|
else { |
219 |
|
for( t=g->list; t->next; t = t->next ) |
220 |
|
; |
221 |
|
t->next = m; |
222 |
|
} |
223 |
|
} |
224 |
|
free_if_alloc( buf ); |
225 |
|
if( !m ) |
226 |
|
return NULL; |
227 |
|
return g->name; |
228 |
|
} /* _add_group */ |
229 |
|
|
230 |
|
void |
231 |
|
release_group( gpg_group_t grp ) |
232 |
|
{ |
233 |
|
gpg_member_t m, m2; |
234 |
|
|
235 |
|
m = grp->list; |
236 |
|
while( m ) { |
237 |
|
m2 = m->next; |
238 |
|
free_if_alloc( m->name ); |
239 |
|
free_if_alloc( m ); |
240 |
|
m = m2; |
241 |
|
} |
242 |
|
} /* release_group */ |
243 |
|
|
244 |
|
|
245 |
|
void |
246 |
|
release_option (gpg_option_t opt) |
247 |
|
{ |
248 |
|
if (!opt) |
249 |
|
return; |
250 |
|
free_if_alloc( opt->name ); |
251 |
|
free_if_alloc( opt->val ); |
252 |
|
free_if_alloc( opt ); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
void |
257 |
|
release_gpg_options( gpg_optfile_t opt ) |
258 |
|
{ |
259 |
|
gpg_option_t e, e2; |
260 |
|
gpg_group_t g, g2; |
261 |
|
|
262 |
|
e = opt->list; |
263 |
|
while( e ) { |
264 |
|
e2 = e->next; |
265 |
|
release_option (e); |
266 |
|
e = e2; |
267 |
|
} |
268 |
|
g = opt->grp; |
269 |
|
while( g ) { |
270 |
|
g2 = g->next; |
271 |
|
release_group( g ); |
272 |
|
g = g2; |
273 |
|
} |
274 |
|
free_if_alloc( opt ); |
275 |
|
} /* release_gpg_options */ |
276 |
|
|
277 |
|
|
278 |
|
int |
279 |
|
commit_gpg_options (const char * file, gpg_optfile_t opt) |
280 |
|
{ |
281 |
|
FILE *inp; |
282 |
|
gpg_option_t e; |
283 |
|
gpg_group_t g; |
284 |
|
gpg_member_t m; |
285 |
|
int rc = 0; |
286 |
|
|
287 |
|
inp = fopen (file, "w+b"); |
288 |
|
if( !inp ) |
289 |
|
return -1; |
290 |
|
for( e = opt->list; e; e = e->next ) { |
291 |
|
if( !e->used ) |
292 |
|
continue; |
293 |
|
switch( e->type ) { |
294 |
|
case ENTRY_OPAQUE: |
295 |
|
fprintf( inp, "%s", e->name ); |
296 |
|
break; |
297 |
|
|
298 |
|
case ENTRY_MULTI: |
299 |
|
fprintf( inp, "%s %s\r\n", e->name, e->val ); |
300 |
|
break; |
301 |
|
|
302 |
|
case ENTRY_SINGLE: |
303 |
|
fprintf( inp, "%s\r\n", e->name ); |
304 |
|
break; |
305 |
|
|
306 |
|
case ENTRY_GROUP: |
307 |
|
g = find_group( opt, e->name ); |
308 |
|
if( g && g->used ) { |
309 |
|
fprintf( inp, "group %s=", g->name ); |
310 |
|
for( m = g->list; m; m = m->next ) { |
311 |
|
if( m->used ) |
312 |
|
fprintf( inp, "%s ", m->name ); |
313 |
|
} |
314 |
|
fprintf( inp, "\r\n" ); |
315 |
|
} |
316 |
|
break; |
317 |
|
} |
318 |
|
} |
319 |
|
fclose (inp); |
320 |
|
return 0; |
321 |
|
} /* commit_gpg_options */ |
322 |
|
|
323 |
|
|
324 |
|
int |
325 |
|
parse_gpg_options( const char *file, gpg_optfile_t *r_opt ) |
326 |
|
{ |
327 |
|
FILE *inp; |
328 |
|
char buf[1024], *p; |
329 |
|
gpg_optfile_t opt = NULL; |
330 |
|
|
331 |
|
inp = fopen( file, "rb" ); |
332 |
|
if( inp == NULL ) { |
333 |
|
inp = fopen( file, "wb" ); |
334 |
|
if( inp == NULL ) |
335 |
|
return -1; |
336 |
|
} |
337 |
|
opt = new gpg_optfile_s; |
338 |
|
if( !opt ) |
339 |
|
BUG( NULL ); |
340 |
|
memset( opt, 0, sizeof *opt ); |
341 |
|
while( !feof( inp ) ) { |
342 |
|
p = fgets( buf, sizeof buf -1, inp ); |
343 |
|
if( !p ) |
344 |
|
break; |
345 |
|
if( *p == '#' || *p == '\r' || *p == '\n' || *p == '\t' ) { |
346 |
|
add_opaque_option( opt, p, 1 ); |
347 |
|
continue; |
348 |
|
} |
349 |
|
if( strstr( p, "\r\n" ) ) |
350 |
|
p[strlen( p ) - 2] = '\0'; |
351 |
|
else if( strstr( p, "\n" ) ) |
352 |
|
p[strlen( p ) - 1] = '\0'; |
353 |
|
if( !strchr( p, ' ' ) ) |
354 |
|
add_single_option( opt, p, 1 ); |
355 |
|
else if( !strncmp( p, "group", 5 ) ) { |
356 |
|
const char *s = _add_group( opt, p ); |
357 |
|
if( s ) |
358 |
|
add_group_option( opt, s, 1 ); |
359 |
|
} |
360 |
|
else |
361 |
|
add_multi_option( opt, p, 1 ); |
362 |
|
} |
363 |
|
fclose( inp ); |
364 |
|
if( r_opt ) |
365 |
|
*r_opt = opt; |
366 |
|
return 0; |
367 |
|
} /* parse_gpg_options */ |
368 |
|
|
369 |
|
|
370 |
|
gpg_option_t |
371 |
|
find_option (gpg_optfile_t opt, const char * str) |
372 |
|
{ |
373 |
|
gpg_option_t e; |
374 |
|
|
375 |
|
for (e = opt->list; e; e = e->next) { |
376 |
|
if (e->type == ENTRY_OPAQUE) |
377 |
|
continue; |
378 |
|
if (!stricmp (e->name, str)) |
379 |
|
break; |
380 |
|
} |
381 |
|
|
382 |
|
return e; |
383 |
|
} /* find_option */ |
384 |
|
|
385 |
|
|
386 |
|
gpg_group_t |
387 |
|
find_group( gpg_optfile_t opt, const char *str ) |
388 |
|
{ |
389 |
|
gpg_group_t g; |
390 |
|
|
391 |
|
for( g = opt->grp; g; g = g->next ) { |
392 |
|
if( !stricmp( g->name, str ) ) |
393 |
|
break; |
394 |
|
} |
395 |
|
|
396 |
|
return g; |
397 |
|
} /* find_group */ |
398 |
|
|
399 |
|
|
400 |
|
gpg_member_t |
401 |
|
find_member( gpg_optfile_t opt, const char *grp, const char *str ) |
402 |
|
{ |
403 |
|
gpg_group_t g = find_group( opt, grp ); |
404 |
|
gpg_member_t m = NULL; |
405 |
|
if( g ) { |
406 |
|
for( m = g->list; m; m = m->next ) { |
407 |
|
if( !stricmp( m->name, str ) ) |
408 |
|
break; |
409 |
|
} |
410 |
|
} |
411 |
|
|
412 |
|
return m; |
413 |
|
} /* find_member */ |
414 |
|
|
415 |
|
|
416 |
|
int |
417 |
|
delete_group( gpg_optfile_t opt, const char *str ) |
418 |
|
{ |
419 |
|
gpg_group_t g = find_group( opt, str ); |
420 |
|
if( g ) { |
421 |
|
g->used = 0; |
422 |
|
return 0; |
423 |
|
} |
424 |
|
|
425 |
|
return -1; |
426 |
|
} /* delete_group */ |
427 |
|
|
428 |
|
|
429 |
|
int |
430 |
|
delete_member( gpg_optfile_t opt, const char *grp, const char *str ) |
431 |
|
{ |
432 |
|
gpg_member_t m = find_member( opt, grp, str ); |
433 |
|
if( m ) { |
434 |
|
m->used = 0; |
435 |
|
return 0; |
436 |
|
} |
437 |
|
|
438 |
|
return -1; |
439 |
|
} /* delete_member */ |
440 |
|
|
441 |
|
|
442 |
|
int |
443 |
|
delete_option (gpg_optfile_t opt, const char * str) |
444 |
|
{ |
445 |
|
gpg_option_t e = find_option (opt, str); |
446 |
|
if (e) { |
447 |
|
e->used = 0; |
448 |
|
return 0; |
449 |
|
} |
450 |
|
|
451 |
|
return -1; |
452 |
|
} /* delete_option */ |
453 |
|
|
454 |
|
|
455 |
|
int |
456 |
|
add_entry( gpg_optfile_t opt, int type, const char *name, const char *val ) |
457 |
|
{ |
458 |
|
gpg_option_t e, t; |
459 |
|
|
460 |
|
e = new gpg_option_s; |
461 |
|
if( !e ) |
462 |
|
BUG( NULL ); |
463 |
|
memset( e, 0, sizeof *e ); |
464 |
|
e->used = 1; |
465 |
|
e->type = type; |
466 |
|
e->name = m_strdup( name ); |
467 |
|
e->val = val? m_strdup( val ) : NULL; |
468 |
|
if( !opt->list ) |
469 |
|
opt->list = e; |
470 |
|
else { |
471 |
|
for( t = opt->list; t->next; t = t->next ) |
472 |
|
; |
473 |
|
t->next = e; |
474 |
|
} |
475 |
|
return 0; |
476 |
|
} /* add_entry */ |
477 |
|
|
478 |
|
|
479 |
|
int |
480 |
|
modify_entry( gpg_optfile_t opt, int type, const char *name, const char *val ) |
481 |
|
{ |
482 |
|
gpg_option_t e; |
483 |
|
int rc = 0; |
484 |
|
|
485 |
|
e = find_option( opt, name ); |
486 |
|
if( !e ) |
487 |
|
rc = add_entry( opt, type, name, val ); |
488 |
|
else if( type != ENTRY_SINGLE ) { |
489 |
|
free_if_alloc( e->val ); |
490 |
|
e->val = m_strdup( val ); |
491 |
|
} |
492 |
|
|
493 |
|
return rc; |
494 |
|
} /* modify_entry */ |
495 |
|
|
496 |
|
|
497 |
|
int |
498 |
|
add_member( gpg_optfile_t opt, const char *grp, const char *str ) |
499 |
|
{ |
500 |
|
gpg_group_t g = find_group( opt, grp ); |
501 |
|
gpg_member_t m, t; |
502 |
|
if( g ) { |
503 |
|
m = new gpg_member_s; |
504 |
|
if( !m ) |
505 |
|
BUG( NULL ); |
506 |
|
memset( m, 0, sizeof *m ); |
507 |
|
m->used = 1; |
508 |
|
m->name = m_strdup( str ); |
509 |
|
if( !m->name ) |
510 |
|
BUG( NULL ); |
511 |
|
if( !g->list ) |
512 |
|
g->list = m; |
513 |
|
else { |
514 |
|
for( t = g->list; t->next; t = t->next ) |
515 |
|
; |
516 |
|
t->next = m; |
517 |
|
} |
518 |
|
return 0; |
519 |
|
} |
520 |
|
return -1; |
521 |
|
} /* add_member */ |
522 |
|
|
523 |
|
|
524 |
|
int |
525 |
|
add_group( gpg_optfile_t opt, const char *str ) |
526 |
|
{ |
527 |
|
gpg_group_t g, t; |
528 |
|
|
529 |
|
g = new gpg_group_s; |
530 |
|
if( !g ) |
531 |
|
BUG( NULL ); |
532 |
|
memset( g, 0, sizeof *g ); |
533 |
|
g->used = 1; |
534 |
|
g->name = m_strdup( str ); |
535 |
|
|
536 |
|
if( !opt->grp ) |
537 |
|
opt->grp = g; |
538 |
|
else { |
539 |
|
for( t = opt->grp; t->next; t = t->next ) |
540 |
|
; |
541 |
|
t->next = g; |
542 |
|
} |
543 |
|
add_entry( opt, ENTRY_GROUP, str, NULL ); |
544 |
|
|
545 |
|
return 0; |
546 |
|
} /* add_group */ |
547 |
|
|
548 |
|
|
549 |
|
#if 0 |
550 |
|
void |
551 |
|
walk_group( const char *name, gpg_member_t mbr ) |
552 |
|
{ |
553 |
|
gpg_member_t m; |
554 |
|
|
555 |
|
printf( "name=%s\n", name ); |
556 |
|
for( m = mbr; m; m = m->next ) |
557 |
|
printf( "%s\n", m->name ); |
558 |
|
} |
559 |
|
|
560 |
|
|
561 |
|
int |
562 |
|
main( int argc, char **argv ) |
563 |
|
{ |
564 |
|
int rc; |
565 |
|
gpg_optfile_t opt; |
566 |
|
gpg_group_t g; |
567 |
|
gpg_option_t e; |
568 |
|
|
|
static void |
|
|
add_opaque_option (gpg_optfile_t opt, const char * line, int used) |
|
|
{ |
|
|
gpg_option_t e, t; |
|
|
|
|
|
e = new gpg_option_s; |
|
|
if (!e) |
|
|
BUG (0); |
|
|
memset (e, 0, sizeof *e); |
|
|
e->used = used > 0? 1 : 0; |
|
|
e->name = m_strdup (line); |
|
|
e->type = ENTRY_OPAQUE; |
|
|
if (!opt->list) |
|
|
opt->list = e; |
|
|
else { |
|
|
for (t = opt->list; t->next; t = t->next) |
|
|
; |
|
|
t->next = e; |
|
|
} |
|
|
} /* add_opaque_option */ |
|
|
|
|
|
|
|
|
static void |
|
|
add_single_option (gpg_optfile_t opt, const char * name, int used) |
|
|
{ |
|
|
gpg_option_t e, t; |
|
|
|
|
|
e = new gpg_option_s; |
|
|
if (!e) |
|
|
BUG (0); |
|
|
memset (e, 0, sizeof *e); |
|
|
e->used = used > 0? 1 : 0; |
|
|
e->name = m_strdup (name); |
|
|
e->type = ENTRY_SINGLE; |
|
|
if (!opt->list) |
|
|
opt->list = e; |
|
|
else { |
|
|
for (t = opt->list; t->next; t = t->next) |
|
|
; |
|
|
t->next = e; |
|
|
} |
|
|
} /* add_single_option */ |
|
|
|
|
|
|
|
|
static int |
|
|
add_group_option( gpg_optfile_t opt, const char *name, int used ) |
|
|
{ |
|
|
gpg_option_t e, t; |
|
|
|
|
|
e = new gpg_option_s; |
|
|
if( !e ) |
|
|
BUG( NULL ); |
|
|
memset( e, 0, sizeof *e ); |
|
|
e->used = used > 0? 1 : 0; |
|
|
e->name = m_strdup( name ); |
|
|
e->type = ENTRY_GROUP; |
|
|
if( !opt->list ) |
|
|
opt->list = e; |
|
|
else { |
|
|
for( t = opt->list; t->next; t = t->next ) |
|
|
; |
|
|
t->next = e; |
|
|
} |
|
|
return 0; |
|
|
} /* add_group_option */ |
|
|
|
|
|
|
|
|
static size_t |
|
|
count_whitespaces( char *line ) |
|
|
{ |
|
|
size_t i = 0, ncount = 0; |
|
|
|
|
|
if( !strchr( line, ' ' ) ) |
|
|
return 0; |
|
|
for( i = 0; i < strlen( line ); i++ ) { |
|
|
if( line[i] == ' ' ) |
|
|
ncount++; |
|
|
} |
|
|
|
|
|
return ncount; |
|
|
} /* count_whitespaces */ |
|
|
|
|
|
|
|
|
static int |
|
|
add_multi_option( gpg_optfile_t opt, char *line, int used ) |
|
|
{ |
|
|
gpg_option_t e ,t; |
|
|
char *p; |
|
|
int state = 0; |
|
|
|
|
|
e = new gpg_option_s; |
|
|
if( !e ) |
|
|
BUG( NULL ); |
|
|
memset( e, 0, sizeof *e ); |
|
|
if( count_whitespaces( line ) == 1 ) { |
|
|
while( (p = strsep( &line, " " )) && state != 2 ) { |
|
|
switch( state ) { |
|
|
case 0: e->name = m_strdup( p ); break; |
|
|
case 1: e->val = m_strdup( p ); break; |
|
|
} |
|
|
state++; |
|
|
} |
|
|
} |
|
|
else { |
|
|
p = strchr( line, ' ' ); |
|
|
state = p - line; |
|
|
e->name = new char[state + 1]; |
|
|
if( !e->name ) |
|
|
BUG( NULL ); |
|
|
memcpy( e->name, line, state ); |
|
|
e->name[state] = '\0'; |
|
|
strncpy( e->name, line, state ); |
|
|
e->val = m_strdup( line + state + 1 ); |
|
|
} |
|
|
e->used = used; |
|
|
e->type = ENTRY_MULTI; |
|
|
if( !opt->list ) |
|
|
opt->list = e; |
|
|
else { |
|
|
for( t=opt->list; t->next; t = t->next ) |
|
|
; |
|
|
t->next = e; |
|
|
} |
|
|
return 0; |
|
|
} /* add_multi_option */ |
|
|
|
|
|
|
|
|
static gpg_group_t |
|
|
new_group (gpg_group_t * grp, const char * name) |
|
|
{ |
|
|
gpg_group_t g, t; |
|
|
|
|
|
g = new gpg_group_s; |
|
|
if( !g ) |
|
|
BUG( NULL ); |
|
|
memset( g, 0, sizeof *g ); |
|
|
g->used = 1; |
|
|
g->name = m_strdup( name ); |
|
|
if( !*grp ) |
|
|
*grp = g; |
|
|
else { |
|
|
for( t = *grp; t->next; t = t->next ) |
|
|
; |
|
|
t->next = g; |
|
|
} |
|
|
return g; |
|
|
} /* new_group */ |
|
|
|
|
|
|
|
|
static const char* |
|
|
_add_group( gpg_optfile_t opt, const char *line ) |
|
|
{ |
|
|
char *p, *buf, *buf2, *name = NULL; |
|
|
gpg_group_t g; |
|
|
gpg_member_t m = NULL, t; |
|
|
|
|
|
p = strchr( line, '=' ); |
|
|
if( p == NULL || strncmp( line, "group ", 6 ) ) |
|
|
return NULL; |
|
|
buf = m_strdup( line + (p - line + 1) ); |
|
|
if( !buf ) |
|
|
BUG( NULL ); |
|
|
line += 6; |
|
|
p = strchr( line, '=' ); |
|
|
if( p ) { |
|
|
size_t pos = p - line; |
|
|
name = new char[pos + 1]; |
|
|
if( !name ) |
|
|
BUG( NULL ); |
|
|
memset( name, 0, pos+1 ); |
|
|
strncpy( name, line, pos ); |
|
|
} |
|
|
g = new_group( &opt->grp, name ); |
|
|
buf2 = buf; |
|
|
while( (p = strsep( &buf2, " " )) && *p ) { |
|
|
m = new gpg_member_s; |
|
|
if( !m ) |
|
|
BUG( NULL ); |
|
|
memset( m, 0, sizeof *m ); |
|
|
m->used = 1; |
|
|
m->name = m_strdup( p ); |
|
|
if( !g->list ) |
|
|
g->list = m; |
|
|
else { |
|
|
for( t=g->list; t->next; t = t->next ) |
|
|
; |
|
|
t->next = m; |
|
|
} |
|
|
} |
|
|
free_if_alloc( buf ); |
|
|
if( !m ) |
|
|
return NULL; |
|
|
return g->name; |
|
|
} /* _add_group */ |
|
|
|
|
|
void |
|
|
release_group( gpg_group_t grp ) |
|
|
{ |
|
|
gpg_member_t m, m2; |
|
|
|
|
|
m = grp->list; |
|
|
while( m ) { |
|
|
m2 = m->next; |
|
|
free_if_alloc( m->name ); |
|
|
free_if_alloc( m ); |
|
|
m = m2; |
|
|
} |
|
|
} /* release_group */ |
|
|
|
|
|
|
|
|
void |
|
|
release_option (gpg_option_t opt) |
|
|
{ |
|
|
if (!opt) |
|
|
return; |
|
|
free_if_alloc( opt->name ); |
|
|
free_if_alloc( opt->val ); |
|
|
free_if_alloc( opt ); |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
release_gpg_options( gpg_optfile_t opt ) |
|
|
{ |
|
|
gpg_option_t e, e2; |
|
|
gpg_group_t g, g2; |
|
|
|
|
|
e = opt->list; |
|
|
while( e ) { |
|
|
e2 = e->next; |
|
|
release_option (e); |
|
|
e = e2; |
|
|
} |
|
|
g = opt->grp; |
|
|
while( g ) { |
|
|
g2 = g->next; |
|
|
release_group( g ); |
|
|
g = g2; |
|
|
} |
|
|
free_if_alloc( opt ); |
|
|
} /* release_gpg_options */ |
|
|
|
|
|
|
|
|
int |
|
|
commit_gpg_options (const char * file, gpg_optfile_t opt) |
|
|
{ |
|
|
FILE *inp; |
|
|
gpg_option_t e; |
|
|
gpg_group_t g; |
|
|
gpg_member_t m; |
|
|
int rc = 0; |
|
|
|
|
|
inp = fopen (file, "w+b"); |
|
|
if( !inp ) |
|
|
return -1; |
|
|
for( e = opt->list; e; e = e->next ) { |
|
|
if( !e->used ) |
|
|
continue; |
|
|
switch( e->type ) { |
|
|
case ENTRY_OPAQUE: |
|
|
fprintf( inp, "%s", e->name ); |
|
|
break; |
|
|
|
|
|
case ENTRY_MULTI: |
|
|
fprintf( inp, "%s %s\r\n", e->name, e->val ); |
|
|
break; |
|
|
|
|
|
case ENTRY_SINGLE: |
|
|
fprintf( inp, "%s\r\n", e->name ); |
|
|
break; |
|
|
|
|
|
case ENTRY_GROUP: |
|
|
g = find_group( opt, e->name ); |
|
|
if( g && g->used ) { |
|
|
fprintf( inp, "group %s=", g->name ); |
|
|
for( m = g->list; m; m = m->next ) { |
|
|
if( m->used ) |
|
|
fprintf( inp, "%s ", m->name ); |
|
|
} |
|
|
fprintf( inp, "\r\n" ); |
|
|
} |
|
|
break; |
|
|
} |
|
|
} |
|
|
fclose (inp); |
|
|
return 0; |
|
|
} /* commit_gpg_options */ |
|
|
|
|
|
|
|
|
int |
|
|
parse_gpg_options( const char *file, gpg_optfile_t *r_opt ) |
|
|
{ |
|
|
FILE *inp; |
|
|
char buf[1024], *p; |
|
|
gpg_optfile_t opt = NULL; |
|
|
|
|
|
inp = fopen( file, "rb" ); |
|
|
if( inp == NULL ) { |
|
|
inp = fopen( file, "wb" ); |
|
|
if( inp == NULL ) |
|
|
return -1; |
|
|
} |
|
|
opt = new gpg_optfile_s; |
|
|
if( !opt ) |
|
|
BUG( NULL ); |
|
|
memset( opt, 0, sizeof *opt ); |
|
|
while( !feof( inp ) ) { |
|
|
p = fgets( buf, sizeof buf -1, inp ); |
|
|
if( !p ) |
|
|
break; |
|
|
if( *p == '#' || *p == '\r' || *p == '\n' || *p == '\t' ) { |
|
|
add_opaque_option( opt, p, 1 ); |
|
|
continue; |
|
|
} |
|
|
if( strstr( p, "\r\n" ) ) |
|
|
p[strlen( p ) - 2] = '\0'; |
|
|
else if( strstr( p, "\n" ) ) |
|
|
p[strlen( p ) - 1] = '\0'; |
|
|
if( !strchr( p, ' ' ) ) |
|
|
add_single_option( opt, p, 1 ); |
|
|
else if( !strncmp( p, "group", 5 ) ) { |
|
|
const char *s = _add_group( opt, p ); |
|
|
if( s ) |
|
|
add_group_option( opt, s, 1 ); |
|
|
} |
|
|
else |
|
|
add_multi_option( opt, p, 1 ); |
|
|
} |
|
|
fclose( inp ); |
|
|
if( r_opt ) |
|
|
*r_opt = opt; |
|
|
return 0; |
|
|
} /* parse_gpg_options */ |
|
|
|
|
|
|
|
|
gpg_option_t |
|
|
find_option (gpg_optfile_t opt, const char * str) |
|
|
{ |
|
|
gpg_option_t e; |
|
|
|
|
|
for (e = opt->list; e; e = e->next) { |
|
|
if (e->type == ENTRY_OPAQUE) |
|
|
continue; |
|
|
if (!stricmp (e->name, str)) |
|
|
break; |
|
|
} |
|
|
|
|
|
return e; |
|
|
} /* find_option */ |
|
|
|
|
|
|
|
|
gpg_group_t |
|
|
find_group( gpg_optfile_t opt, const char *str ) |
|
|
{ |
|
|
gpg_group_t g; |
|
|
|
|
|
for( g = opt->grp; g; g = g->next ) { |
|
|
if( !stricmp( g->name, str ) ) |
|
|
break; |
|
|
} |
|
|
|
|
|
return g; |
|
|
} /* find_group */ |
|
|
|
|
|
|
|
|
gpg_member_t |
|
|
find_member( gpg_optfile_t opt, const char *grp, const char *str ) |
|
|
{ |
|
|
gpg_group_t g = find_group( opt, grp ); |
|
|
gpg_member_t m = NULL; |
|
|
if( g ) { |
|
|
for( m = g->list; m; m = m->next ) { |
|
|
if( !stricmp( m->name, str ) ) |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
return m; |
|
|
} /* find_member */ |
|
|
|
|
|
|
|
|
int |
|
|
delete_group( gpg_optfile_t opt, const char *str ) |
|
|
{ |
|
|
gpg_group_t g = find_group( opt, str ); |
|
|
if( g ) { |
|
|
g->used = 0; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
return -1; |
|
|
} /* delete_group */ |
|
|
|
|
|
|
|
|
int |
|
|
delete_member( gpg_optfile_t opt, const char *grp, const char *str ) |
|
|
{ |
|
|
gpg_member_t m = find_member( opt, grp, str ); |
|
|
if( m ) { |
|
|
m->used = 0; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
return -1; |
|
|
} /* delete_member */ |
|
|
|
|
|
|
|
|
int |
|
|
delete_option (gpg_optfile_t opt, const char * str) |
|
|
{ |
|
|
gpg_option_t e = find_option (opt, str); |
|
|
if (e) { |
|
|
e->used = 0; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
return -1; |
|
|
} /* delete_option */ |
|
|
|
|
|
|
|
|
int |
|
|
add_entry( gpg_optfile_t opt, int type, const char *name, const char *val ) |
|
|
{ |
|
|
gpg_option_t e, t; |
|
|
|
|
|
e = new gpg_option_s; |
|
|
if( !e ) |
|
|
BUG( NULL ); |
|
|
memset( e, 0, sizeof *e ); |
|
|
e->used = 1; |
|
|
e->type = type; |
|
|
e->name = m_strdup( name ); |
|
|
e->val = val? m_strdup( val ) : NULL; |
|
|
if( !opt->list ) |
|
|
opt->list = e; |
|
|
else { |
|
|
for( t = opt->list; t->next; t = t->next ) |
|
|
; |
|
|
t->next = e; |
|
|
} |
|
|
return 0; |
|
|
} /* add_entry */ |
|
|
|
|
|
|
|
|
int |
|
|
modify_entry( gpg_optfile_t opt, int type, const char *name, const char *val ) |
|
|
{ |
|
|
gpg_option_t e; |
|
|
int rc = 0; |
|
|
|
|
|
e = find_option( opt, name ); |
|
|
if( !e ) |
|
|
rc = add_entry( opt, type, name, val ); |
|
|
else if( type != ENTRY_SINGLE ) { |
|
|
free_if_alloc( e->val ); |
|
|
e->val = m_strdup( val ); |
|
|
} |
|
|
|
|
|
return rc; |
|
|
} /* modify_entry */ |
|
|
|
|
|
|
|
|
int |
|
|
add_member( gpg_optfile_t opt, const char *grp, const char *str ) |
|
|
{ |
|
|
gpg_group_t g = find_group( opt, grp ); |
|
|
gpg_member_t m, t; |
|
|
if( g ) { |
|
|
m = new gpg_member_s; |
|
|
if( !m ) |
|
|
BUG( NULL ); |
|
|
memset( m, 0, sizeof *m ); |
|
|
m->used = 1; |
|
|
m->name = m_strdup( str ); |
|
|
if( !m->name ) |
|
|
BUG( NULL ); |
|
|
if( !g->list ) |
|
|
g->list = m; |
|
|
else { |
|
|
for( t = g->list; t->next; t = t->next ) |
|
|
; |
|
|
t->next = m; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
return -1; |
|
|
} /* add_member */ |
|
|
|
|
|
|
|
|
int |
|
|
add_group( gpg_optfile_t opt, const char *str ) |
|
|
{ |
|
|
gpg_group_t g, t; |
|
|
|
|
|
g = new gpg_group_s; |
|
|
if( !g ) |
|
|
BUG( NULL ); |
|
|
memset( g, 0, sizeof *g ); |
|
|
g->used = 1; |
|
|
g->name = m_strdup( str ); |
|
|
|
|
|
if( !opt->grp ) |
|
|
opt->grp = g; |
|
|
else { |
|
|
for( t = opt->grp; t->next; t = t->next ) |
|
|
; |
|
|
t->next = g; |
|
|
} |
|
|
add_entry( opt, ENTRY_GROUP, str, NULL ); |
|
|
|
|
|
return 0; |
|
|
} /* add_group */ |
|
|
|
|
|
|
|
|
#if 0 |
|
|
void |
|
|
walk_group( const char *name, gpg_member_t mbr ) |
|
|
{ |
|
|
gpg_member_t m; |
|
|
|
|
|
printf( "name=%s\n", name ); |
|
|
for( m = mbr; m; m = m->next ) |
|
|
printf( "%s\n", m->name ); |
|
|
} |
|
|
|
|
|
|
|
|
int |
|
|
main( int argc, char **argv ) |
|
|
{ |
|
|
int rc; |
|
|
gpg_optfile_t opt; |
|
|
gpg_group_t g; |
|
|
gpg_option_t e; |
|
|
|
|
|
rc = parse_gpg_options( "c:\\gnupg\\gpg.conf", &opt ); |
|
|
if( rc ) |
|
|
printf( "parse_option: error\n" ); |
|
|
else { |
|
|
#if 0 |
|
|
/*delete_option( opt, "encrypt-to" );*/ |
|
|
delete_member( opt, "bar", "richard" ); |
|
|
add_member( opt, "bar", "leo" ); |
|
|
add_group( opt, "foobar" ); |
|
|
delete_group( opt, "foobar" ); |
|
|
add_entry( opt, ENTRY_SINGLE, "use-agent", NULL ); |
|
|
#endif |
|
|
for( e = opt->list; e; e = e->next ) { |
|
|
if( e->type != 1 ) |
|
|
printf( "%d: %s %s\n", e->used, e->name, e->val ); |
|
|
} |
|
|
/*commit_gpg_options( "c:\\gnupg\\gpg2.conf", opt );*/ |
|
|
for( g = opt->grp; g; g = g->next ) |
|
|
walk_group( g->name, g->list ); |
|
|
release_option( opt ); |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
#endif |
|
569 |
|
rc = parse_gpg_options( "c:\\gnupg\\gpg.conf", &opt ); |
570 |
|
if( rc ) |
571 |
|
printf( "parse_option: error\n" ); |
572 |
|
else { |
573 |
|
#if 0 |
574 |
|
/*delete_option( opt, "encrypt-to" );*/ |
575 |
|
delete_member( opt, "bar", "richard" ); |
576 |
|
add_member( opt, "bar", "leo" ); |
577 |
|
add_group( opt, "foobar" ); |
578 |
|
delete_group( opt, "foobar" ); |
579 |
|
add_entry( opt, ENTRY_SINGLE, "use-agent", NULL ); |
580 |
|
#endif |
581 |
|
for( e = opt->list; e; e = e->next ) { |
582 |
|
if( e->type != 1 ) |
583 |
|
printf( "%d: %s %s\n", e->used, e->name, e->val ); |
584 |
|
} |
585 |
|
/*commit_gpg_options( "c:\\gnupg\\gpg2.conf", opt );*/ |
586 |
|
for( g = opt->grp; g; g = g->next ) |
587 |
|
walk_group( g->name, g->list ); |
588 |
|
release_option( opt ); |
589 |
|
} |
590 |
|
|
591 |
|
return 0; |
592 |
|
} |
593 |
|
#endif |