1 |
/* wptGPGParser.cpp - GPG config file parser |
2 |
* Copyright (C) 2002, 2003, 2004 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (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 GNU |
14 |
* 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 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <stdio.h> |
27 |
#include <string.h> |
28 |
#include <windows.h> |
29 |
|
30 |
#include "wptTypes.h" |
31 |
#include "wptW32API.h" |
32 |
#include "wptGPG.h" |
33 |
#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 |
|
286 |
inp = fopen (file, "w+b"); |
287 |
if( !inp ) |
288 |
return -1; |
289 |
for( e = opt->list; e; e = e->next ) { |
290 |
if( !e->used ) |
291 |
continue; |
292 |
switch( e->type ) { |
293 |
case ENTRY_OPAQUE: |
294 |
fprintf( inp, "%s", e->name ); |
295 |
break; |
296 |
|
297 |
case ENTRY_MULTI: |
298 |
fprintf( inp, "%s %s\r\n", e->name, e->val ); |
299 |
break; |
300 |
|
301 |
case ENTRY_SINGLE: |
302 |
fprintf( inp, "%s\r\n", e->name ); |
303 |
break; |
304 |
|
305 |
case ENTRY_GROUP: |
306 |
g = find_group( opt, e->name ); |
307 |
if( g && g->used ) { |
308 |
fprintf( inp, "group %s=", g->name ); |
309 |
for( m = g->list; m; m = m->next ) { |
310 |
if( m->used ) |
311 |
fprintf( inp, "%s ", m->name ); |
312 |
} |
313 |
fprintf( inp, "\r\n" ); |
314 |
} |
315 |
break; |
316 |
} |
317 |
} |
318 |
fclose (inp); |
319 |
return 0; |
320 |
} /* commit_gpg_options */ |
321 |
|
322 |
|
323 |
int |
324 |
parse_gpg_options( const char *file, gpg_optfile_t *r_opt ) |
325 |
{ |
326 |
FILE *inp; |
327 |
char buf[1024], *p; |
328 |
gpg_optfile_t opt = NULL; |
329 |
|
330 |
inp = fopen( file, "rb" ); |
331 |
if( inp == NULL ) { |
332 |
inp = fopen( file, "wb" ); |
333 |
if( inp == NULL ) |
334 |
return -1; |
335 |
} |
336 |
opt = new gpg_optfile_s; |
337 |
if( !opt ) |
338 |
BUG( NULL ); |
339 |
memset( opt, 0, sizeof *opt ); |
340 |
while( !feof( inp ) ) { |
341 |
p = fgets( buf, sizeof buf -1, inp ); |
342 |
if( !p ) |
343 |
break; |
344 |
if( *p == '#' || *p == '\r' || *p == '\n' || *p == '\t' ) { |
345 |
add_opaque_option( opt, p, 1 ); |
346 |
continue; |
347 |
} |
348 |
if( strstr( p, "\r\n" ) ) |
349 |
p[strlen( p ) - 2] = '\0'; |
350 |
else if( strstr( p, "\n" ) ) |
351 |
p[strlen( p ) - 1] = '\0'; |
352 |
if( !strchr( p, ' ' ) ) |
353 |
add_single_option( opt, p, 1 ); |
354 |
else if( !strncmp( p, "group", 5 ) ) { |
355 |
const char *s = _add_group( opt, p ); |
356 |
if( s ) |
357 |
add_group_option( opt, s, 1 ); |
358 |
} |
359 |
else |
360 |
add_multi_option( opt, p, 1 ); |
361 |
} |
362 |
fclose( inp ); |
363 |
if( r_opt ) |
364 |
*r_opt = opt; |
365 |
return 0; |
366 |
} /* parse_gpg_options */ |
367 |
|
368 |
|
369 |
gpg_option_t |
370 |
find_option (gpg_optfile_t opt, const char * str) |
371 |
{ |
372 |
gpg_option_t e; |
373 |
|
374 |
for (e = opt->list; e; e = e->next) { |
375 |
if (e->type == ENTRY_OPAQUE) |
376 |
continue; |
377 |
if (!stricmp (e->name, str)) |
378 |
break; |
379 |
} |
380 |
|
381 |
return e; |
382 |
} /* find_option */ |
383 |
|
384 |
|
385 |
gpg_group_t |
386 |
find_group( gpg_optfile_t opt, const char *str ) |
387 |
{ |
388 |
gpg_group_t g; |
389 |
|
390 |
for( g = opt->grp; g; g = g->next ) { |
391 |
if( !stricmp( g->name, str ) ) |
392 |
break; |
393 |
} |
394 |
|
395 |
return g; |
396 |
} /* find_group */ |
397 |
|
398 |
|
399 |
gpg_member_t |
400 |
find_member( gpg_optfile_t opt, const char *grp, const char *str ) |
401 |
{ |
402 |
gpg_group_t g = find_group( opt, grp ); |
403 |
gpg_member_t m = NULL; |
404 |
if( g ) { |
405 |
for( m = g->list; m; m = m->next ) { |
406 |
if( !stricmp( m->name, str ) ) |
407 |
break; |
408 |
} |
409 |
} |
410 |
|
411 |
return m; |
412 |
} /* find_member */ |
413 |
|
414 |
|
415 |
int |
416 |
delete_group( gpg_optfile_t opt, const char *str ) |
417 |
{ |
418 |
gpg_group_t g = find_group( opt, str ); |
419 |
if( g ) { |
420 |
g->used = 0; |
421 |
return 0; |
422 |
} |
423 |
|
424 |
return -1; |
425 |
} /* delete_group */ |
426 |
|
427 |
|
428 |
int |
429 |
delete_member( gpg_optfile_t opt, const char *grp, const char *str ) |
430 |
{ |
431 |
gpg_member_t m = find_member( opt, grp, str ); |
432 |
if( m ) { |
433 |
m->used = 0; |
434 |
return 0; |
435 |
} |
436 |
|
437 |
return -1; |
438 |
} /* delete_member */ |
439 |
|
440 |
|
441 |
int |
442 |
delete_option (gpg_optfile_t opt, const char * str) |
443 |
{ |
444 |
gpg_option_t e = find_option (opt, str); |
445 |
if (e) { |
446 |
e->used = 0; |
447 |
return 0; |
448 |
} |
449 |
|
450 |
return -1; |
451 |
} /* delete_option */ |
452 |
|
453 |
|
454 |
int |
455 |
add_entry( gpg_optfile_t opt, int type, const char *name, const char *val ) |
456 |
{ |
457 |
gpg_option_t e, t; |
458 |
|
459 |
e = new gpg_option_s; |
460 |
if( !e ) |
461 |
BUG( NULL ); |
462 |
memset( e, 0, sizeof *e ); |
463 |
e->used = 1; |
464 |
e->type = type; |
465 |
e->name = m_strdup( name ); |
466 |
e->val = val? m_strdup( val ) : NULL; |
467 |
if( !opt->list ) |
468 |
opt->list = e; |
469 |
else { |
470 |
for( t = opt->list; t->next; t = t->next ) |
471 |
; |
472 |
t->next = e; |
473 |
} |
474 |
return 0; |
475 |
} /* add_entry */ |
476 |
|
477 |
|
478 |
int |
479 |
modify_entry( gpg_optfile_t opt, int type, const char *name, const char *val ) |
480 |
{ |
481 |
gpg_option_t e; |
482 |
int rc = 0; |
483 |
|
484 |
e = find_option( opt, name ); |
485 |
if( !e ) |
486 |
rc = add_entry( opt, type, name, val ); |
487 |
else if( type != ENTRY_SINGLE ) { |
488 |
free_if_alloc( e->val ); |
489 |
e->val = m_strdup( val ); |
490 |
} |
491 |
|
492 |
return rc; |
493 |
} /* modify_entry */ |
494 |
|
495 |
|
496 |
int |
497 |
add_member( gpg_optfile_t opt, const char *grp, const char *str ) |
498 |
{ |
499 |
gpg_group_t g = find_group( opt, grp ); |
500 |
gpg_member_t m, t; |
501 |
if( g ) { |
502 |
m = new gpg_member_s; |
503 |
if( !m ) |
504 |
BUG( NULL ); |
505 |
memset( m, 0, sizeof *m ); |
506 |
m->used = 1; |
507 |
m->name = m_strdup( str ); |
508 |
if( !m->name ) |
509 |
BUG( NULL ); |
510 |
if( !g->list ) |
511 |
g->list = m; |
512 |
else { |
513 |
for( t = g->list; t->next; t = t->next ) |
514 |
; |
515 |
t->next = m; |
516 |
} |
517 |
return 0; |
518 |
} |
519 |
return -1; |
520 |
} /* add_member */ |
521 |
|
522 |
|
523 |
int |
524 |
add_group( gpg_optfile_t opt, const char *str ) |
525 |
{ |
526 |
gpg_group_t g, t; |
527 |
|
528 |
g = new gpg_group_s; |
529 |
if( !g ) |
530 |
BUG( NULL ); |
531 |
memset( g, 0, sizeof *g ); |
532 |
g->used = 1; |
533 |
g->name = m_strdup( str ); |
534 |
|
535 |
if( !opt->grp ) |
536 |
opt->grp = g; |
537 |
else { |
538 |
for( t = opt->grp; t->next; t = t->next ) |
539 |
; |
540 |
t->next = g; |
541 |
} |
542 |
add_entry( opt, ENTRY_GROUP, str, NULL ); |
543 |
|
544 |
return 0; |
545 |
} /* add_group */ |
546 |
|
547 |
|
548 |
#if 0 |
549 |
void |
550 |
walk_group( const char *name, gpg_member_t mbr ) |
551 |
{ |
552 |
gpg_member_t m; |
553 |
|
554 |
printf( "name=%s\n", name ); |
555 |
for( m = mbr; m; m = m->next ) |
556 |
printf( "%s\n", m->name ); |
557 |
} |
558 |
|
559 |
|
560 |
int |
561 |
main( int argc, char **argv ) |
562 |
{ |
563 |
int rc; |
564 |
gpg_optfile_t opt; |
565 |
gpg_group_t g; |
566 |
gpg_option_t e; |
567 |
|
568 |
rc = parse_gpg_options( "c:\\gnupg\\gpg.conf", &opt ); |
569 |
if( rc ) |
570 |
printf( "parse_option: error\n" ); |
571 |
else { |
572 |
#if 0 |
573 |
/*delete_option( opt, "encrypt-to" );*/ |
574 |
delete_member( opt, "bar", "richard" ); |
575 |
add_member( opt, "bar", "leo" ); |
576 |
add_group( opt, "foobar" ); |
577 |
delete_group( opt, "foobar" ); |
578 |
add_entry( opt, ENTRY_SINGLE, "use-agent", NULL ); |
579 |
#endif |
580 |
for( e = opt->list; e; e = e->next ) { |
581 |
if( e->type != 1 ) |
582 |
printf( "%d: %s %s\n", e->used, e->name, e->val ); |
583 |
} |
584 |
/*commit_gpg_options( "c:\\gnupg\\gpg2.conf", opt );*/ |
585 |
for( g = opt->grp; g; g = g->next ) |
586 |
walk_group( g->name, g->list ); |
587 |
release_option( opt ); |
588 |
} |
589 |
|
590 |
return 0; |
591 |
} |
592 |
#endif |