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