/[winpt]/trunk/Src/wptGPGParser.cpp
ViewVC logotype

Annotation of /trunk/Src/wptGPGParser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (hide annotations)
Thu Oct 27 15:25:13 2005 UTC (19 years, 4 months ago) by werner
File size: 12244 byte(s)
First set of changes to use autotools for building.
1 werner 36 /* 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 twoaday 2 #include <stdio.h>
26 werner 36 #include <stdio.h>
27     #include <string.h>
28     #include <windows.h>
29    
30     #include "wptTypes.h"
31 twoaday 2 #include "wptW32API.h"
32 werner 36 #include "wptGPG.h"
33     #include "wptErrors.h"
34 twoaday 2
35 werner 36 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    
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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26