1 | /* |
---|
2 | * A utility to test functionality of Gerald Carter''s regfio interface. |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Timothy D. Morgan |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; version 2 of the License. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
18 | * |
---|
19 | * $Id: reglookup.c 38 2005-07-31 15:00:59Z tim $ |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <string.h> |
---|
26 | #include <strings.h> |
---|
27 | #include "../include/regfio.h" |
---|
28 | #include "../include/void_stack.h" |
---|
29 | |
---|
30 | |
---|
31 | void bailOut(int code, char* message) |
---|
32 | { |
---|
33 | fprintf(stderr, message); |
---|
34 | exit(code); |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | void_stack* path2Stack(const char* s) |
---|
39 | { |
---|
40 | void_stack* ret_val; |
---|
41 | void_stack* rev_ret = void_stack_new(1024); |
---|
42 | const char* cur = s; |
---|
43 | char* next = NULL; |
---|
44 | char* copy; |
---|
45 | |
---|
46 | if (rev_ret == NULL) |
---|
47 | return NULL; |
---|
48 | if (s == NULL) |
---|
49 | return rev_ret; |
---|
50 | |
---|
51 | while((next = strchr(cur, '/')) != NULL) |
---|
52 | { |
---|
53 | if ((next-cur) > 0) |
---|
54 | { |
---|
55 | copy = (char*)malloc((next-cur+1)*sizeof(char)); |
---|
56 | if(copy == NULL) |
---|
57 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
58 | |
---|
59 | memcpy(copy, cur, next-cur); |
---|
60 | copy[next-cur] = '\0'; |
---|
61 | void_stack_push(rev_ret, copy); |
---|
62 | } |
---|
63 | cur = next+1; |
---|
64 | } |
---|
65 | if(strlen(cur) > 0) |
---|
66 | { |
---|
67 | copy = strdup(cur); |
---|
68 | void_stack_push(rev_ret, copy); |
---|
69 | } |
---|
70 | |
---|
71 | ret_val = void_stack_copy_reverse(rev_ret); |
---|
72 | void_stack_destroy(rev_ret); |
---|
73 | |
---|
74 | return ret_val; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | char* stack2Path(void_stack* nk_stack) |
---|
79 | { |
---|
80 | const REGF_NK_REC* cur; |
---|
81 | uint32 buf_left = 127; |
---|
82 | uint32 buf_len = buf_left+1; |
---|
83 | uint32 name_len = 0; |
---|
84 | uint32 grow_amt; |
---|
85 | char* buf; |
---|
86 | char* new_buf; |
---|
87 | void_stack_iterator* iter; |
---|
88 | |
---|
89 | buf = (char*)malloc((buf_len)*sizeof(char)); |
---|
90 | if (buf == NULL) |
---|
91 | return NULL; |
---|
92 | buf[0] = '\0'; |
---|
93 | |
---|
94 | iter = void_stack_iterator_new(nk_stack); |
---|
95 | if (iter == NULL) |
---|
96 | { |
---|
97 | free(buf); |
---|
98 | return NULL; |
---|
99 | } |
---|
100 | |
---|
101 | /* skip root element */ |
---|
102 | cur = void_stack_iterator_next(iter); |
---|
103 | |
---|
104 | while((cur = void_stack_iterator_next(iter)) != NULL) |
---|
105 | { |
---|
106 | buf[buf_len-buf_left-1] = '/'; |
---|
107 | buf_left -= 1; |
---|
108 | name_len = strlen(cur->keyname); |
---|
109 | if(name_len+1 > buf_left) |
---|
110 | { |
---|
111 | grow_amt = (uint32)(buf_len/2); |
---|
112 | buf_len += name_len+1+grow_amt-buf_left; |
---|
113 | if((new_buf = realloc(buf, buf_len)) == NULL) |
---|
114 | { |
---|
115 | free(buf); |
---|
116 | free(iter); |
---|
117 | return NULL; |
---|
118 | } |
---|
119 | buf = new_buf; |
---|
120 | buf_left = grow_amt + name_len + 1; |
---|
121 | } |
---|
122 | strncpy(buf+(buf_len-buf_left-1), cur->keyname, name_len); |
---|
123 | buf_left -= name_len; |
---|
124 | buf[buf_len-buf_left-1] = '\0'; |
---|
125 | } |
---|
126 | |
---|
127 | return buf; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | void printValue(REGF_VK_REC* vk, char* prefix) |
---|
132 | { |
---|
133 | const char* str_type; |
---|
134 | |
---|
135 | str_type = type_val2str(vk->type); |
---|
136 | printf("%s/%s:%s=\n", prefix, vk->valuename, str_type); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void printValueList(REGF_NK_REC* nk, char* prefix) |
---|
141 | { |
---|
142 | uint32 i; |
---|
143 | |
---|
144 | for(i=0; i < nk->num_values; i++) |
---|
145 | printValue(&nk->values[i], prefix); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | /* XXX: this function is god-awful. Needs to be re-designed. */ |
---|
150 | void printKeyTree(REGF_FILE* f, void_stack* nk_stack, char* prefix) |
---|
151 | { |
---|
152 | REGF_NK_REC* cur; |
---|
153 | REGF_NK_REC* sub; |
---|
154 | char* path; |
---|
155 | char* val_path; |
---|
156 | |
---|
157 | if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL) |
---|
158 | { |
---|
159 | cur->subkey_index = 0; |
---|
160 | path = stack2Path(nk_stack); |
---|
161 | |
---|
162 | if(strlen(path) > 0) |
---|
163 | printf("%s%s:KEY\n", prefix, path); |
---|
164 | printValueList(cur, path); |
---|
165 | while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL) |
---|
166 | { |
---|
167 | if((sub = regfio_fetch_subkey(f, cur)) != NULL) |
---|
168 | { |
---|
169 | sub->subkey_index = 0; |
---|
170 | void_stack_push(nk_stack, sub); |
---|
171 | path = stack2Path(nk_stack); |
---|
172 | if(path != NULL) |
---|
173 | { |
---|
174 | val_path = (char*)malloc(strlen(prefix)+strlen(path)+1); |
---|
175 | sprintf(val_path, "%s%s", prefix, path); |
---|
176 | printf("%s:KEY\n", val_path); |
---|
177 | printValueList(sub, val_path); |
---|
178 | free(val_path); |
---|
179 | free(path); |
---|
180 | } |
---|
181 | } |
---|
182 | else |
---|
183 | { |
---|
184 | cur = void_stack_pop(nk_stack); |
---|
185 | /* XXX: This is just a shallow free. Need to write deep free |
---|
186 | * routines to replace the Samba code for this. |
---|
187 | */ |
---|
188 | /* if(cur != NULL) |
---|
189 | free(cur);*/ |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | /* |
---|
197 | * Returns 0 if path was found. |
---|
198 | * Returns 1 if path was not found. |
---|
199 | * Returns less than 0 on other error. |
---|
200 | */ |
---|
201 | int retrievePath(REGF_FILE* f, void_stack* nk_stack, |
---|
202 | void_stack* path_stack) |
---|
203 | { |
---|
204 | REGF_NK_REC* sub; |
---|
205 | REGF_NK_REC* cur; |
---|
206 | void_stack* sub_nk_stack; |
---|
207 | char* prefix; |
---|
208 | char* cur_str = NULL; |
---|
209 | bool found_cur = true; |
---|
210 | uint32 i; |
---|
211 | uint16 path_depth; |
---|
212 | if(path_stack == NULL) |
---|
213 | return -1; |
---|
214 | |
---|
215 | path_depth = void_stack_size(path_stack); |
---|
216 | if(path_depth < 1) |
---|
217 | return -2; |
---|
218 | |
---|
219 | if(void_stack_size(nk_stack) < 1) |
---|
220 | return -3; |
---|
221 | cur = (REGF_NK_REC*)void_stack_cur(nk_stack); |
---|
222 | |
---|
223 | while(void_stack_size(path_stack) > 1) |
---|
224 | { |
---|
225 | /* Search key records only */ |
---|
226 | cur_str = (char*)void_stack_pop(path_stack); |
---|
227 | |
---|
228 | found_cur = false; |
---|
229 | while(!found_cur && |
---|
230 | (sub = regfio_fetch_subkey(f, cur)) != NULL) |
---|
231 | { |
---|
232 | if(strcasecmp(sub->keyname, cur_str) == 0) |
---|
233 | { |
---|
234 | cur = sub; |
---|
235 | void_stack_push(nk_stack, sub); |
---|
236 | found_cur = true; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | if(!found_cur) |
---|
241 | return 1; |
---|
242 | } |
---|
243 | |
---|
244 | /* Last round, search value and key records */ |
---|
245 | cur_str = (char*)void_stack_pop(path_stack); |
---|
246 | |
---|
247 | for(i=0; (i < cur->num_values); i++) |
---|
248 | { |
---|
249 | if(strcasecmp(sub->values[i].valuename, cur_str) == 0) |
---|
250 | { |
---|
251 | printValue(&sub->values[i], stack2Path(nk_stack)); |
---|
252 | return 0; |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | while((sub = regfio_fetch_subkey(f, cur)) != NULL) |
---|
257 | { |
---|
258 | if(strcasecmp(sub->keyname, cur_str) == 0) |
---|
259 | { |
---|
260 | sub_nk_stack = void_stack_new(1024); |
---|
261 | void_stack_push(sub_nk_stack, sub); |
---|
262 | void_stack_push(nk_stack, sub); |
---|
263 | prefix = stack2Path(nk_stack); |
---|
264 | printKeyTree(f, sub_nk_stack, prefix); |
---|
265 | return 0; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | return 1; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | static void usage(void) |
---|
274 | { |
---|
275 | fprintf(stderr, "Usage: readreg [-f <PREFIX_FILTER>] [-t <TYPE_FILTER>] " |
---|
276 | "[-v] [-s] <REGISTRY_FILE>\n"); |
---|
277 | /* XXX: replace version string with Subversion property? */ |
---|
278 | fprintf(stderr, "Version: 0.2\n"); |
---|
279 | fprintf(stderr, "\n\t-v\t sets verbose mode."); |
---|
280 | fprintf(stderr, "\n\t-f\t a simple prefix filter."); |
---|
281 | fprintf(stderr, "\n\t-t\t restrict results to a specific type."); |
---|
282 | fprintf(stderr, "\n\t-s\t prints security descriptors."); |
---|
283 | fprintf(stderr, "\n"); |
---|
284 | } |
---|
285 | |
---|
286 | /* Globals, influenced by command line parameters */ |
---|
287 | bool print_verbose = false; |
---|
288 | bool print_security = false; |
---|
289 | bool prefix_filter_enabled = false; |
---|
290 | bool type_filter_enabled = false; |
---|
291 | char* prefix_filter = NULL; |
---|
292 | char* type_filter = NULL; |
---|
293 | char* registry_file = NULL; |
---|
294 | |
---|
295 | |
---|
296 | int main(int argc, char** argv) |
---|
297 | { |
---|
298 | void_stack* nk_stack; |
---|
299 | void_stack* path_stack; |
---|
300 | REGF_FILE* f; |
---|
301 | REGF_NK_REC* root; |
---|
302 | int retr_path_ret; |
---|
303 | uint32 argi; |
---|
304 | |
---|
305 | /* Process command line arguments */ |
---|
306 | if(argc < 2) |
---|
307 | { |
---|
308 | usage(); |
---|
309 | bailOut(1, "ERROR: Requires 1 argument.\n"); |
---|
310 | } |
---|
311 | |
---|
312 | for(argi = 1; argi < argc; argi++) |
---|
313 | { |
---|
314 | if (strcmp("-f", argv[argi]) == 0) |
---|
315 | { |
---|
316 | if(++argi > argc) |
---|
317 | { |
---|
318 | usage(); |
---|
319 | bailOut(1, "ERROR: '-f' option requires parameter.\n"); |
---|
320 | } |
---|
321 | if((prefix_filter = strdup(argv[argi])) == NULL) |
---|
322 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
323 | |
---|
324 | prefix_filter_enabled = true; |
---|
325 | } |
---|
326 | else if (strcmp("-t", argv[argi]) == 0) |
---|
327 | { |
---|
328 | if(++argi > argc) |
---|
329 | { |
---|
330 | usage(); |
---|
331 | bailOut(1, "ERROR: '-t' option requires parameter.\n"); |
---|
332 | } |
---|
333 | if((prefix_filter = strdup(argv[argi])) == NULL) |
---|
334 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
335 | |
---|
336 | type_filter_enabled = true; |
---|
337 | } |
---|
338 | else if (strcmp("-s", argv[argi]) == 0) |
---|
339 | print_security = true; |
---|
340 | else if (strcmp("-v", argv[argi]) == 0) |
---|
341 | print_verbose = true; |
---|
342 | else if (argv[argi][0] == '-') |
---|
343 | { |
---|
344 | usage(); |
---|
345 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
346 | bailOut(1, ""); |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | if((registry_file = strdup(argv[argi])) == NULL) |
---|
351 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | f = regfio_open(registry_file); |
---|
356 | if(f == NULL) |
---|
357 | { |
---|
358 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
359 | bailOut(3, ""); |
---|
360 | } |
---|
361 | |
---|
362 | root = regfio_rootkey(f); |
---|
363 | nk_stack = void_stack_new(1024); |
---|
364 | |
---|
365 | if(void_stack_push(nk_stack, root)) |
---|
366 | { |
---|
367 | path_stack = path2Stack(prefix_filter); |
---|
368 | if(void_stack_size(path_stack) < 1) |
---|
369 | printKeyTree(f, nk_stack, ""); |
---|
370 | else |
---|
371 | { |
---|
372 | retr_path_ret = retrievePath(f, nk_stack, path_stack); |
---|
373 | if(retr_path_ret == 1) |
---|
374 | fprintf(stderr, "WARNING: specified path not found.\n"); |
---|
375 | else if(retr_path_ret != 0) |
---|
376 | bailOut(4, "ERROR:\n"); |
---|
377 | } |
---|
378 | } |
---|
379 | else |
---|
380 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
381 | |
---|
382 | void_stack_destroy_deep(nk_stack); |
---|
383 | regfio_close(f); |
---|
384 | |
---|
385 | return 0; |
---|
386 | } |
---|