1 |
/* wptGPGParser.cpp - GPG config file parser |
/* wptGPGParser.cpp - Config file parser |
2 |
* Copyright (C) 2002, 2003, 2004, 2006 Timo Schulz |
* Copyright (C) 2002, 2003, 2004, 2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
59 |
|
|
60 |
|
|
61 |
static void |
static void |
62 |
add_opaque_option (gpg_optfile_t opt, const char *line, int used) |
add_opaque_option (config_file_t opt, const char *line, int used) |
63 |
{ |
{ |
64 |
gpg_option_t e; |
gpg_option_t e; |
65 |
|
|
72 |
|
|
73 |
|
|
74 |
static void |
static void |
75 |
add_single_option (gpg_optfile_t opt, const char *name, int used) |
add_single_option (config_file_t opt, const char *name, int used) |
76 |
{ |
{ |
77 |
gpg_option_t e; |
gpg_option_t e; |
78 |
|
|
85 |
|
|
86 |
|
|
87 |
static int |
static int |
88 |
add_group_option (gpg_optfile_t opt, const char *name, int used) |
add_group_option (config_file_t opt, const char *name, int used) |
89 |
{ |
{ |
90 |
gpg_option_t e; |
gpg_option_t e; |
91 |
|
|
115 |
|
|
116 |
|
|
117 |
static int |
static int |
118 |
add_multi_option (gpg_optfile_t opt, char *line, int used) |
add_multi_option (config_file_t opt, char *line, int used) |
119 |
{ |
{ |
120 |
gpg_option_t e; |
gpg_option_t e; |
121 |
char *p; |
char *p; |
175 |
|
|
176 |
|
|
177 |
static const char* |
static const char* |
178 |
group_from_cfgfile (gpg_optfile_t opt, const char *line) |
group_from_cfgfile (config_file_t opt, const char *line) |
179 |
{ |
{ |
180 |
char *p, *buf, *buf2, *name = NULL; |
char *p, *buf, *buf2, *name = NULL; |
181 |
gpg_group_t g; |
gpg_group_t g; |
246 |
|
|
247 |
|
|
248 |
void |
void |
249 |
release_gpg_options (gpg_optfile_t opt) |
release_config (config_file_t opt) |
250 |
{ |
{ |
251 |
gpg_option_t e, e2; |
gpg_option_t e, e2; |
252 |
gpg_group_t g, g2; |
gpg_group_t g, g2; |
253 |
|
|
254 |
|
if (!opt) |
255 |
|
return; |
256 |
e = opt->list; |
e = opt->list; |
257 |
while (e) { |
while (e) { |
258 |
e2 = e->next; |
e2 = e->next; |
270 |
|
|
271 |
|
|
272 |
int |
int |
273 |
commit_gpg_options (const char *file, gpg_optfile_t opt) |
commit_config (const char *file, config_file_t opt) |
274 |
{ |
{ |
275 |
FILE *inp; |
FILE *inp; |
276 |
gpg_option_t e; |
gpg_option_t e; |
314 |
} |
} |
315 |
|
|
316 |
|
|
317 |
|
void |
318 |
|
new_config (config_file_t *r_opt) |
319 |
|
{ |
320 |
|
config_file_t opt; |
321 |
|
|
322 |
|
opt = new gpg_optfile_s; |
323 |
|
if (!opt) |
324 |
|
BUG (NULL); |
325 |
|
memset (opt, 0, sizeof *opt); |
326 |
|
*r_opt = opt; |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
int |
int |
331 |
parse_gpg_options (const char *file, gpg_optfile_t *r_opt ) |
parse_config (const char *file, config_file_t *r_opt) |
332 |
{ |
{ |
333 |
FILE *inp; |
FILE *inp; |
334 |
char buf[1024], *p; |
char buf[1024], *p; |
335 |
gpg_optfile_t opt = NULL; |
config_file_t opt = NULL; |
336 |
|
|
337 |
|
if (!r_opt) |
338 |
|
return -1; |
339 |
|
|
340 |
inp = fopen( file, "rb"); |
inp = fopen( file, "rb"); |
341 |
if( inp == NULL ) { |
if (!inp) { |
342 |
inp = fopen( file, "wb" ); |
inp = fopen (file, "wb"); |
343 |
if( inp == NULL ) |
if (inp == NULL) |
344 |
return -1; |
return -1; |
345 |
} |
} |
346 |
opt = new gpg_optfile_s; |
opt = new gpg_optfile_s; |
347 |
if( !opt ) |
if (!opt) |
348 |
BUG( NULL ); |
BUG (NULL); |
349 |
memset( opt, 0, sizeof *opt ); |
memset (opt, 0, sizeof *opt); |
350 |
while( !feof( inp ) ) { |
while (!feof (inp)) { |
351 |
p = fgets( buf, sizeof buf -1, inp ); |
p = fgets (buf, sizeof (buf) -1, inp); |
352 |
if( !p ) |
if (!p) |
353 |
break; |
break; |
354 |
if( *p == '#' || *p == '\r' || *p == '\n' || *p == '\t' ) { |
if (*p == '#' || *p == '\r' || *p == '\n' || *p == '\t') { |
355 |
add_opaque_option( opt, p, 1 ); |
add_opaque_option (opt, p, 1); |
356 |
continue; |
continue; |
357 |
} |
} |
358 |
if( strstr( p, "\r\n" ) ) |
if( strstr (p, "\r\n" )) |
359 |
p[strlen( p ) - 2] = '\0'; |
p[strlen (p) - 2] = '\0'; |
360 |
else if( strstr( p, "\n" ) ) |
else if (strstr (p, "\n")) |
361 |
p[strlen( p ) - 1] = '\0'; |
p[strlen (p) - 1] = '\0'; |
362 |
if( !strchr( p, ' ' ) ) |
if (!strchr( p, ' ' )) |
363 |
add_single_option( opt, p, 1 ); |
add_single_option (opt, p, 1); |
364 |
else if( !strncmp( p, "group", 5 ) ) { |
else if (!strncmp (p, "group", 5)) { |
365 |
const char *s = group_from_cfgfile (opt, p); |
const char *s = group_from_cfgfile (opt, p); |
366 |
if (s) |
if (s) |
367 |
add_group_option (opt, s, 1); |
add_group_option (opt, s, 1); |
370 |
add_multi_option (opt, p, 1); |
add_multi_option (opt, p, 1); |
371 |
} |
} |
372 |
fclose (inp); |
fclose (inp); |
373 |
if (r_opt) |
*r_opt = opt; |
|
*r_opt = opt; |
|
374 |
return 0; |
return 0; |
375 |
} |
} |
376 |
|
|
377 |
|
|
378 |
gpg_option_t |
gpg_option_t |
379 |
find_option (gpg_optfile_t opt, const char *str) |
find_option (config_file_t opt, const char *str) |
380 |
{ |
{ |
381 |
gpg_option_t e; |
gpg_option_t e; |
382 |
|
|
392 |
|
|
393 |
|
|
394 |
gpg_group_t |
gpg_group_t |
395 |
find_group (gpg_optfile_t opt, const char *str) |
find_group (config_file_t opt, const char *str) |
396 |
{ |
{ |
397 |
gpg_group_t g; |
gpg_group_t g; |
398 |
|
|
406 |
|
|
407 |
|
|
408 |
gpg_member_t |
gpg_member_t |
409 |
find_member (gpg_optfile_t opt, const char *grp, const char *str) |
find_member (config_file_t opt, const char *grp, const char *str) |
410 |
{ |
{ |
|
gpg_group_t g = find_group (opt, grp); |
|
411 |
gpg_member_t m; |
gpg_member_t m; |
412 |
|
gpg_group_t g = find_group (opt, grp); |
413 |
|
|
414 |
if (!g) |
if (!g) |
415 |
return NULL; |
return NULL; |
423 |
|
|
424 |
|
|
425 |
int |
int |
426 |
delete_group (gpg_optfile_t opt, const char *str) |
delete_group (config_file_t opt, const char *str) |
427 |
{ |
{ |
428 |
gpg_group_t g = find_group (opt, str); |
gpg_group_t g = find_group (opt, str); |
429 |
|
|
434 |
|
|
435 |
|
|
436 |
int |
int |
437 |
delete_member (gpg_optfile_t opt, const char *grp, const char *str) |
delete_member (config_file_t opt, const char *grp, const char *str) |
438 |
{ |
{ |
439 |
gpg_member_t m = find_member (opt, grp, str); |
gpg_member_t m = find_member (opt, grp, str); |
440 |
|
|
445 |
|
|
446 |
|
|
447 |
int |
int |
448 |
delete_option (gpg_optfile_t opt, const char * str) |
delete_option (config_file_t opt, const char * str) |
449 |
{ |
{ |
450 |
gpg_option_t e = find_option (opt, str); |
gpg_option_t e = find_option (opt, str); |
451 |
|
|
456 |
|
|
457 |
|
|
458 |
int |
int |
459 |
add_entry (gpg_optfile_t opt, int type, const char *name, const char *val) |
add_entry_int (config_file_t opt, int type, const char *name, int val) |
460 |
|
{ |
461 |
|
char buf[64]; |
462 |
|
|
463 |
|
_snprintf (buf, DIM (buf)-1, "%d", val); |
464 |
|
add_entry (opt, type, name, buf); |
465 |
|
return 0; |
466 |
|
} |
467 |
|
|
468 |
|
int |
469 |
|
add_entry (config_file_t opt, int type, const char *name, const char *val) |
470 |
{ |
{ |
471 |
gpg_option_t e; |
gpg_option_t e; |
472 |
|
|
481 |
|
|
482 |
|
|
483 |
int |
int |
484 |
modify_entry (gpg_optfile_t opt, int type, const char *name, const char *val) |
modify_entry (config_file_t opt, int type, const char *name, const char *val) |
485 |
{ |
{ |
486 |
gpg_option_t e; |
gpg_option_t e; |
487 |
int rc = 0; |
int rc = 0; |
492 |
else if (type != ENTRY_SINGLE) { |
else if (type != ENTRY_SINGLE) { |
493 |
free_if_alloc (e->val); |
free_if_alloc (e->val); |
494 |
e->val = m_strdup (val); |
e->val = m_strdup (val); |
495 |
|
rc = 0; |
496 |
} |
} |
497 |
|
|
498 |
return rc; |
return rc; |
500 |
|
|
501 |
|
|
502 |
int |
int |
503 |
add_member (gpg_optfile_t opt, const char *grp, const char *str) |
add_member (config_file_t opt, const char *grp, const char *str) |
504 |
{ |
{ |
505 |
gpg_group_t g = find_group (opt, grp); |
gpg_group_t g = find_group (opt, grp); |
506 |
gpg_member_t m, t; |
gpg_member_t m, t; |
526 |
|
|
527 |
|
|
528 |
int |
int |
529 |
add_group (gpg_optfile_t opt, const char *str) |
add_group (config_file_t opt, const char *str) |
530 |
{ |
{ |
531 |
gpg_group_t g, t; |
gpg_group_t g, t; |
532 |
|
|
533 |
g = new gpg_group_s; |
g = new gpg_group_s; |
534 |
if (!g) |
if (!g) |
535 |
BUG (NULL); |
BUG (NULL); |
536 |
memset (g, 0, sizeof *g); |
memset (g, 0, sizeof *g); |
547 |
add_entry (opt, ENTRY_GROUP, str, NULL); |
add_entry (opt, ENTRY_GROUP, str, NULL); |
548 |
return 0; |
return 0; |
549 |
} |
} |
|
|
|
|
|
|
|
#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 |
|