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