Updates to editreg.c to provide more functionality ...
[Samba/vl.git] / source3 / utils / editreg.c
blob01df68521c13f1ac9e8a4ebcfa7123316e9b6ca1
1 /*
2 Samba Unix/Linux SMB client utility editreg.c
3 Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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.
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. */
19 /*************************************************************************
21 A utility to edit a Windows NT/2K etc registry file.
23 Many of the ideas in here come from other people and software.
24 I first looked in Wine in misc/registry.c and was also influenced by
25 http://www.wednesday.demon.co.uk/dosreg.html
27 Which seems to contain comments from someone else. I reproduce them here
28 incase the site above disappears. It actually comes from
29 http://home.eunet.no/~pnordahl/ntpasswd/WinReg.txt.
31 The goal here is to read the registry into memory, manipulate it, and then
32 write it out if it was changed by any actions of the user.
34 The windows NT registry has 2 different blocks, where one can occur many
35 times...
37 the "regf"-Block
38 ================
40 "regf" is obviosly the abbreviation for "Registry file". "regf" is the
41 signature of the header-block which is always 4kb in size, although only
42 the first 64 bytes seem to be used and a checksum is calculated over
43 the first 0x200 bytes only!
45 Offset Size Contents
46 0x00000000 D-Word ID: ASCII-"regf" = 0x66676572
47 0x00000004 D-Word ???? //see struct REGF
48 0x00000008 D-Word ???? Always the same value as at 0x00000004
49 0x0000000C Q-Word last modify date in WinNT date-format
50 0x00000014 D-Word 1
51 0x00000018 D-Word 3
52 0x0000001C D-Word 0
53 0x00000020 D-Word 1
54 0x00000024 D-Word Offset of 1st key record
55 0x00000028 D-Word Size of the data-blocks (Filesize-4kb)
56 0x0000002C D-Word 1
57 0x000001FC D-Word Sum of all D-Words from 0x00000000 to
58 0x000001FB //XOR of all words. Nigel
60 I have analyzed more registry files (from multiple machines running
61 NT 4.0 german version) and could not find an explanation for the values
62 marked with ???? the rest of the first 4kb page is not important...
64 the "hbin"-Block
65 ================
66 I don't know what "hbin" stands for, but this block is always a multiple
67 of 4kb in size.
69 Inside these hbin-blocks the different records are placed. The memory-
70 management looks like a C-compiler heap management to me...
72 hbin-Header
73 ===========
74 Offset Size Contents
75 0x0000 D-Word ID: ASCII-"hbin" = 0x6E696268
76 0x0004 D-Word Offset from the 1st hbin-Block
77 0x0008 D-Word Offset to the next hbin-Block
78 0x001C D-Word Block-size
80 The values in 0x0008 and 0x001C should be the same, so I don't know
81 if they are correct or swapped...
83 From offset 0x0020 inside a hbin-block data is stored with the following
84 format:
86 Offset Size Contents
87 0x0000 D-Word Data-block size //this size must be a
88 multiple of 8. Nigel
89 0x0004 ???? Data
91 If the size field is negative (bit 31 set), the corresponding block
92 is free and has a size of -blocksize!
94 That does not seem to be true. All block lengths seem to be negative! (Richard Sharpe)
96 The data is stored as one record per block. Block size is a multiple
97 of 4 and the last block reaches the next hbin-block, leaving no room.
99 Records in the hbin-blocks
100 ==========================
102 nk-Record
104 The nk-record can be treated as a kombination of tree-record and
105 key-record of the win 95 registry.
107 lf-Record
109 The lf-record is the counterpart to the RGKN-record (the
110 hash-function)
112 vk-Record
114 The vk-record consists information to a single value.
116 sk-Record
118 sk (? Security Key ?) is the ACL of the registry.
120 Value-Lists
122 The value-lists contain information about which values are inside a
123 sub-key and don't have a header.
125 Datas
127 The datas of the registry are (like the value-list) stored without a
128 header.
130 All offset-values are relative to the first hbin-block and point to the
131 block-size field of the record-entry. to get the file offset, you have to add
132 the header size (4kb) and the size field (4 bytes)...
134 the nk-Record
135 =============
136 Offset Size Contents
137 0x0000 Word ID: ASCII-"nk" = 0x6B6E
138 0x0002 Word for the root-key: 0x2C, otherwise 0x20 //key symbolic links 0x10. Nigel
139 0x0004 Q-Word write-date/time in windows nt notation
140 0x0010 D-Word Offset of Owner/Parent key
141 0x0014 D-Word number of sub-Keys
142 0x001C D-Word Offset of the sub-key lf-Records
143 0x0024 D-Word number of values
144 0x0028 D-Word Offset of the Value-List
145 0x002C D-Word Offset of the sk-Record
147 0x0030 D-Word Offset of the Class-Name //see NK structure for the use of these fields. Nigel
148 0x0044 D-Word Unused (data-trash) //some kind of run time index. Does not appear to be important. Nigel
149 0x0048 Word name-length
150 0x004A Word class-name length
151 0x004C ???? key-name
153 the Value-List
154 ==============
155 Offset Size Contents
156 0x0000 D-Word Offset 1st Value
157 0x0004 D-Word Offset 2nd Value
158 0x???? D-Word Offset nth Value
160 To determine the number of values, you have to look at the owner-nk-record!
162 Der vk-Record
163 =============
164 Offset Size Contents
165 0x0000 Word ID: ASCII-"vk" = 0x6B76
166 0x0002 Word name length
167 0x0004 D-Word length of the data //if top bit is set when offset contains data. Nigel
168 0x0008 D-Word Offset of Data
169 0x000C D-Word Type of value
170 0x0010 Word Flag
171 0x0012 Word Unused (data-trash)
172 0x0014 ???? Name
174 If bit 0 of the flag-word is set, a name is present, otherwise the value has no name (=default)
176 If the data-size is lower 5, the data-offset value is used to store the data itself!
178 The data-types
179 ==============
180 Wert Beteutung
181 0x0001 RegSZ: character string (in UNICODE!)
182 0x0002 ExpandSZ: string with "%var%" expanding (UNICODE!)
183 0x0003 RegBin: raw-binary value
184 0x0004 RegDWord: Dword
185 0x0007 RegMultiSZ: multiple strings, seperated with 0
186 (UNICODE!)
188 The "lf"-record
189 ===============
190 Offset Size Contents
191 0x0000 Word ID: ASCII-"lf" = 0x666C
192 0x0002 Word number of keys
193 0x0004 ???? Hash-Records
195 Hash-Record
196 ===========
197 Offset Size Contents
198 0x0000 D-Word Offset of corresponding "nk"-Record
199 0x0004 D-Word ASCII: the first 4 characters of the key-name, padded with 0's. Case sensitiv!
201 Keep in mind, that the value at 0x0004 is used for checking the data-consistency! If you change the
202 key-name you have to change the hash-value too!
204 //These hashrecords must be sorted low to high within the lf record. Nigel.
206 The "sk"-block
207 ==============
208 (due to the complexity of the SAM-info, not clear jet)
209 (This is just a security descriptor in the data. R Sharpe.)
212 Offset Size Contents
213 0x0000 Word ID: ASCII-"sk" = 0x6B73
214 0x0002 Word Unused
215 0x0004 D-Word Offset of previous "sk"-Record
216 0x0008 D-Word Offset of next "sk"-Record
217 0x000C D-Word usage-counter
218 0x0010 D-Word Size of "sk"-record in bytes
219 ???? //standard self
220 relative security desciptor. Nigel
221 ???? ???? Security and auditing settings...
222 ????
224 The usage counter counts the number of references to this
225 "sk"-record. You can use one "sk"-record for the entire registry!
227 Windows nt date/time format
228 ===========================
229 The time-format is a 64-bit integer which is incremented every
230 0,0000001 seconds by 1 (I don't know how accurate it realy is!)
231 It starts with 0 at the 1st of january 1601 0:00! All values are
232 stored in GMT time! The time-zone is important to get the real
233 time!
235 Common values for win95 and win-nt
236 ==================================
237 Offset values marking an "end of list", are either 0 or -1 (0xFFFFFFFF).
238 If a value has no name (length=0, flag(bit 0)=0), it is treated as the
239 "Default" entry...
240 If a value has no data (length=0), it is displayed as empty.
242 simplyfied win-3.?? registry:
243 =============================
245 +-----------+
246 | next rec. |---+ +----->+------------+
247 | first sub | | | | Usage cnt. |
248 | name | | +-->+------------+ | | length |
249 | value | | | | next rec. | | | text |------->+-------+
250 +-----------+ | | | name rec. |--+ +------------+ | xxxxx |
251 +------------+ | | value rec. |-------->+------------+ +-------+
252 v | +------------+ | Usage cnt. |
253 +-----------+ | | length |
254 | next rec. | | | text |------->+-------+
255 | first sub |------+ +------------+ | xxxxx |
256 | name | +-------+
257 | value |
258 +-----------+
260 Greatly simplyfied structure of the nt-registry:
261 ================================================
263 +---------------------------------------------------------------+
266 +---------+ +---------->+-----------+ +----->+---------+ |
267 | "nk" | | | lf-rec. | | | nk-rec. | |
268 | ID | | | # of keys | | | parent |---+
269 | Date | | | 1st key |--+ | .... |
270 | parent | | +-----------+ +---------+
271 | suk-keys|-----+
272 | values |--------------------->+----------+
273 | SK-rec. |---------------+ | 1. value |--> +----------+
274 | class |--+ | +----------+ | vk-rec. |
275 +---------+ | | | .... |
276 v | | data |--> +-------+
277 +------------+ | +----------+ | xxxxx |
278 | Class name | | +-------+
279 +------------+ |
281 +---------+ +---------+
282 +----->| next sk |--->| Next sk |--+
283 | +---| prev sk |<---| prev sk | |
284 | | | .... | | ... | |
285 | | +---------+ +---------+ |
286 | | ^ |
287 | | | |
288 | +--------------------+ |
289 +----------------------------------+
291 ---------------------------------------------------------------------------
293 Hope this helps.... (Although it was "fun" for me to uncover this things,
294 it took me several sleepless nights ;)
296 B.D.
298 *************************************************************************/
300 #include <stdio.h>
301 #include <stdlib.h>
302 #include <errno.h>
303 #include <assert.h>
304 #include <sys/types.h>
305 #include <sys/stat.h>
306 #include <unistd.h>
307 #include <sys/mman.h>
308 #include <string.h>
309 #include <fcntl.h>
311 #define False 0
312 #define True 1
314 static int verbose = 0;
315 static int print_security = 0;
318 * These definitions are for the in-memory registry structure.
319 * It is a tree structure that mimics what you see with tools like regedit
323 * DateTime struct for Windows
326 typedef struct date_time_s {
327 unsigned int low, high;
328 } NTTIME;
331 * Definition of a Key. It has a name, classname, date/time last modified,
332 * sub-keys, values, and a security descriptor
335 #define REG_ROOT_KEY 1
336 #define REG_SUB_KEY 2
337 #define REG_SYM_LINK 3
339 typedef struct reg_key_s {
340 char *name; /* Name of the key */
341 char *class_name;
342 int type; /* One of REG_ROOT_KEY or REG_SUB_KEY */
343 NTTIME last_mod; /* Time last modified */
344 struct reg_key_s *owner;
345 struct key_list_s *sub_keys;
346 struct val_list_s *values;
347 struct key_sec_desc_s *security;
348 } REG_KEY;
351 * The KEY_LIST struct lists sub-keys.
354 typedef struct key_list_s {
355 int key_count;
356 int max_keys;
357 REG_KEY *keys[1];
358 } KEY_LIST;
360 typedef struct val_key_s {
361 char *name;
362 int has_name;
363 int data_type;
364 int data_len;
365 void *data_blk; /* Might want a separate block */
366 } VAL_KEY;
368 typedef struct val_list_s {
369 int val_count;
370 VAL_KEY *vals[1];
371 } VAL_LIST;
373 #ifndef MAXSUBAUTHS
374 #define MAXSUBAUTHS 15
375 #endif
377 typedef struct dom_sid_s {
378 unsigned char ver, auths;
379 unsigned char auth[6];
380 unsigned int sub_auths[MAXSUBAUTHS];
381 } DOM_SID;
383 typedef struct ace_struct_s {
384 unsigned char type, flags;
385 unsigned int perms; /* Perhaps a better def is in order */
386 DOM_SID *trustee;
387 } ACE;
389 typedef struct acl_struct_s {
390 unsigned short rev, refcnt;
391 unsigned short num_aces;
392 ACE *aces[1];
393 } ACL;
395 typedef struct sec_desc_s {
396 unsigned int rev, type;
397 DOM_SID *owner, *group;
398 ACL *sacl, *dacl;
399 } SEC_DESC;
401 #define SEC_DESC_NON 0
402 #define SEC_DESC_RES 1
403 #define SEC_DESC_OCU 2
405 typedef struct key_sec_desc_s {
406 struct key_sec_desc_s *prev, *next;
407 int ref_cnt;
408 int state;
409 SEC_DESC *sec_desc;
410 } KEY_SEC_DESC;
413 * All of the structures below actually have a four-byte lenght before them
414 * which always seems to be negative. The following macro retrieves that
415 * size as an integer
418 #define BLK_SIZE(b) ((int)*(int *)(((int *)b)-1))
420 typedef unsigned int DWORD;
421 typedef unsigned short WORD;
423 #define REG_REGF_ID 0x66676572
425 typedef struct regf_block {
426 DWORD REGF_ID; /* regf */
427 DWORD uk1;
428 DWORD uk2;
429 DWORD tim1, tim2;
430 DWORD uk3; /* 1 */
431 DWORD uk4; /* 3 */
432 DWORD uk5; /* 0 */
433 DWORD uk6; /* 1 */
434 DWORD first_key; /* offset */
435 unsigned int dblk_size;
436 DWORD uk7[116]; /* 1 */
437 DWORD chksum;
438 } REGF_HDR;
440 typedef struct hbin_sub_struct {
441 DWORD dblocksize;
442 char data[1];
443 } HBIN_SUB_HDR;
445 #define REG_HBIN_ID 0x6E696268
447 typedef struct hbin_struct {
448 DWORD HBIN_ID; /* hbin */
449 DWORD next_off;
450 DWORD prev_off;
451 DWORD uk1;
452 DWORD uk2;
453 DWORD uk3;
454 DWORD uk4;
455 DWORD blk_size;
456 HBIN_SUB_HDR hbin_sub_hdr;
457 } HBIN_HDR;
459 #define REG_NK_ID 0x6B6E
461 typedef struct nk_struct {
462 WORD NK_ID;
463 WORD type;
464 DWORD t1, t2;
465 DWORD uk1;
466 DWORD own_off;
467 DWORD subk_num;
468 DWORD uk2;
469 DWORD lf_off;
470 DWORD uk3;
471 DWORD val_cnt;
472 DWORD val_off;
473 DWORD sk_off;
474 DWORD clsnam_off;
475 DWORD unk4[4];
476 DWORD unk5;
477 WORD nam_len;
478 WORD clsnam_len;
479 char key_nam[1]; /* Actual length determined by nam_len */
480 } NK_HDR;
482 #define REG_SK_ID 0x6B73
484 typedef struct sk_struct {
485 WORD SK_ID;
486 WORD uk1;
487 DWORD prev_off;
488 DWORD next_off;
489 DWORD ref_cnt;
490 DWORD rec_size;
491 char sec_desc[1];
492 } SK_HDR;
494 typedef struct ace_struct {
495 unsigned char type;
496 unsigned char flags;
497 unsigned short length;
498 unsigned int perms;
499 DOM_SID trustee;
500 } REG_ACE;
502 typedef struct acl_struct {
503 WORD rev;
504 WORD size;
505 DWORD num_aces;
506 REG_ACE *aces; /* One or more ACEs */
507 } REG_ACL;
509 typedef struct sec_desc_rec {
510 WORD rev;
511 WORD type;
512 DWORD owner_off;
513 DWORD group_off;
514 DWORD sacl_off;
515 DWORD dacl_off;
516 } REG_SEC_DESC;
518 typedef struct hash_struct {
519 DWORD nk_off;
520 char hash[4];
521 } HASH_REC;
523 #define REG_LF_ID 0x666C
525 typedef struct lf_struct {
526 WORD LF_ID;
527 WORD key_count;
528 struct hash_struct hr[1]; /* Array of hash records, depending on key_count */
529 } LF_HDR;
531 typedef DWORD VL_TYPE[1]; /* Value list is an array of vk rec offsets */
533 #define REG_VK_ID 0x6B76
535 typedef struct vk_struct {
536 WORD VK_ID;
537 WORD nam_len;
538 DWORD dat_len; /* If top-bit set, offset contains the data */
539 DWORD dat_off;
540 DWORD dat_type;
541 WORD flag; /* =1, has name, else no name (=Default). */
542 WORD unk1;
543 char dat_name[1]; /* Name starts here ... */
544 } VK_HDR;
546 #define REG_TYPE_NONE 0
547 #define REG_TYPE_REGSZ 1
548 #define REG_TYPE_EXPANDSZ 2
549 #define REG_TYPE_BIN 3
550 #define REG_TYPE_DWORD 4
551 #define REG_TYPE_MULTISZ 7
553 typedef struct _val_str {
554 unsigned int val;
555 const char * str;
556 } VAL_STR;
558 /* A map of sk offsets in the regf to KEY_SEC_DESCs for quick lookup etc */
559 typedef struct sk_map_s {
560 int sk_off;
561 KEY_SEC_DESC *key_sec_desc;
562 } SK_MAP;
564 struct regf_struct_s {
565 int reg_type;
566 char *regfile_name, *outfile_name;
567 int fd;
568 struct stat sbuf;
569 char *base;
570 int modified;
571 NTTIME last_mod_time;
572 REG_KEY *root; /* Root of the tree for this file */
573 int sk_count, sk_map_size;
574 SK_MAP *sk_map;
577 typedef struct regf_struct_s REGF;
580 * An API for accessing/creating/destroying items above
584 * Iterate over the keys, depth first, calling a function for each key
585 * and indicating if it is terminal or non-terminal and if it has values.
587 * In addition, for each value in the list, call a value list function
590 typedef int (*key_print_f)(const char *path, char *key_name, char *class_name,
591 int root, int terminal, int values);
593 typedef int (*val_print_f)(const char *path, char *val_name, int val_type,
594 int data_len, void *data_blk, int terminal,
595 int first, int last);
597 typedef int (*sec_print_f)(SEC_DESC *sec_desc);
599 int nt_key_iterator(REGF *regf, REG_KEY *key_tree, int bf, const char *path,
600 key_print_f key_print, sec_print_f sec_print,
601 val_print_f val_print);
603 int nt_val_list_iterator(REGF *regf, VAL_LIST *val_list, int bf, char *path,
604 int terminal, val_print_f val_print)
606 int i;
608 if (!val_list) return 1;
610 if (!val_print) return 1;
612 for (i=0; i<val_list->val_count; i++) {
613 if (!val_print(path, val_list->vals[i]->name, val_list->vals[i]->data_type,
614 val_list->vals[i]->data_len, val_list->vals[i]->data_blk,
615 terminal,
616 (i == 0),
617 (i == val_list->val_count))) {
619 return 0;
624 return 1;
627 int nt_key_list_iterator(REGF *regf, KEY_LIST *key_list, int bf,
628 const char *path,
629 key_print_f key_print, sec_print_f sec_print,
630 val_print_f val_print)
632 int i;
634 if (!key_list) return 1;
636 for (i=0; i< key_list->key_count; i++) {
637 if (!nt_key_iterator(regf, key_list->keys[i], bf, path, key_print,
638 sec_print, val_print)) {
639 return 0;
642 return 1;
645 int nt_key_iterator(REGF *regf, REG_KEY *key_tree, int bf, const char *path,
646 key_print_f key_print, sec_print_f sec_print,
647 val_print_f val_print)
649 int path_len = strlen(path);
650 char *new_path;
652 if (!regf || !key_tree)
653 return -1;
655 /* List the key first, then the values, then the sub-keys */
657 if (key_print) {
659 if (!(*key_print)(path, key_tree->name,
660 key_tree->class_name,
661 (key_tree->type == REG_ROOT_KEY),
662 (key_tree->sub_keys == NULL),
663 (key_tree->values?(key_tree->values->val_count):0)))
664 return 0;
668 * If we have a security print routine, call it
669 * If the security print routine returns false, stop.
671 if (sec_print) {
672 if (key_tree->security && !(*sec_print)(key_tree->security->sec_desc))
673 return 0;
676 new_path = (char *)malloc(path_len + 1 + strlen(key_tree->name) + 1);
677 if (!new_path) return 0; /* Errors? */
678 new_path[0] = '\0';
679 strcat(new_path, path);
680 strcat(new_path, key_tree->name);
681 strcat(new_path, "\\");
684 * Now, iterate through the values in the val_list
687 if (key_tree->values &&
688 !nt_val_list_iterator(regf, key_tree->values, bf, new_path,
689 (key_tree->values!=NULL),
690 val_print)) {
692 free(new_path);
693 return 0;
697 * Now, iterate through the keys in the key list
700 if (key_tree->sub_keys &&
701 !nt_key_list_iterator(regf, key_tree->sub_keys, bf, new_path, key_print,
702 sec_print, val_print)) {
703 free(new_path);
704 return 0;
707 free(new_path);
708 return 1;
711 REG_KEY *nt_find_key_by_name(REG_KEY *tree, char *key);
714 * Find key by name in a list ...
715 * Take the first component and search for that in the list
717 REG_KEY *nt_find_key_in_list_by_name(KEY_LIST *list, char *key)
719 int i;
720 REG_KEY *res = NULL;
722 if (!list || !key || !*key) return NULL;
724 for (i = 0; i<= list->key_count; i++)
725 if ((res = nt_find_key_by_name(list->keys[i], key)))
726 return res;
728 return NULL;
732 * Find key by name in a tree ... We will assume absolute names here, but we
733 * need the root of the tree ...
735 REG_KEY *nt_find_key_by_name(REG_KEY *tree, char *key)
737 char *lname = NULL, *c1, *c2;
738 REG_KEY *tmp;
740 if (!tree || !key || !*key) return NULL;
742 lname = strdup(key);
743 if (!lname) return NULL;
746 * Make sure that the first component is correct ...
748 c1 = lname;
749 c2 = strchr(c1, '\\');
750 if (c2) { /* Split here ... */
751 *c2 = 0;
752 c2++;
754 if (strcmp(c1, tree->name) != 0) goto error;
756 if (c2) {
757 tmp = nt_find_key_in_list_by_name(tree->sub_keys, c2);
758 free(lname);
759 return tmp;
761 else {
762 if (lname) free(lname);
763 return tree;
765 error:
766 if (lname) free(lname);
767 return NULL;
770 /* Make, delete keys */
772 int nt_delete_val_key(VAL_KEY *val_key)
775 if (val_key) {
776 if (val_key->data_blk) free(val_key->data_blk);
777 free(val_key);
779 return 1;
782 int nt_delete_val_list(VAL_LIST *vl)
784 int i;
786 if (vl) {
787 for (i=0; i<vl->val_count; i++)
788 nt_delete_val_key(vl->vals[i]);
789 free(vl);
791 return 1;
794 int nt_delete_reg_key(REG_KEY *key, int delete_name);
795 int nt_delete_key_list(KEY_LIST *key_list, int delete_name)
797 int i;
799 if (key_list) {
800 for (i=0; i<key_list->key_count; i++)
801 nt_delete_reg_key(key_list->keys[i], False);
802 free(key_list);
804 return 1;
808 * Find the key, and if it exists, delete it ...
810 int nt_delete_key_by_name(REGF *regf, char *name)
812 REG_KEY *key;
814 if (!name || !*name) return 0;
816 key = nt_find_key_by_name(regf->root, name);
818 if (key) {
819 return nt_delete_reg_key(key, True);
822 return 0;
826 int nt_delete_sid(DOM_SID *sid)
829 if (sid) free(sid);
830 return 1;
834 int nt_delete_ace(ACE *ace)
837 if (ace) {
838 nt_delete_sid(ace->trustee);
839 free(ace);
841 return 1;
845 int nt_delete_acl(ACL *acl)
848 if (acl) {
849 int i;
851 for (i=0; i<acl->num_aces; i++)
852 nt_delete_ace(acl->aces[i]);
854 free(acl);
856 return 1;
859 int nt_delete_sec_desc(SEC_DESC *sec_desc)
862 if (sec_desc) {
864 nt_delete_sid(sec_desc->owner);
865 nt_delete_sid(sec_desc->group);
866 nt_delete_acl(sec_desc->sacl);
867 nt_delete_acl(sec_desc->dacl);
868 free(sec_desc);
871 return 1;
874 int nt_delete_key_sec_desc(KEY_SEC_DESC *key_sec_desc)
877 if (key_sec_desc) {
878 key_sec_desc->ref_cnt--;
879 if (key_sec_desc->ref_cnt<=0) {
881 * There should always be a next and prev, even if they point to us
883 key_sec_desc->next->prev = key_sec_desc->prev;
884 key_sec_desc->prev->next = key_sec_desc->next;
885 nt_delete_sec_desc(key_sec_desc->sec_desc);
888 return 1;
891 int nt_delete_reg_key(REG_KEY *key, int delete_name)
894 if (key) {
895 if (key->name) free(key->name);
896 if (key->class_name) free(key->class_name);
899 * We will delete the owner if we are not the root and told to ...
902 if (key->owner && key->owner->sub_keys && delete_name) {
903 REG_KEY *own;
904 KEY_LIST *kl;
905 int i;
906 /* Find our owner, look in keylist for us and shuffle up */
907 /* Perhaps should be a function */
909 own = key->owner;
910 kl = own->sub_keys;
912 for (i=0; i < kl->key_count && kl->keys[i] != key ; i++) {
913 /* Just find the entry ... */
916 if (i == kl->key_count) {
917 fprintf(stderr, "Bad data structure. Key not found in key list of owner\n");
919 else {
920 int j;
923 * Shuffle up. Works for the last one also
925 for (j = i + 1; j < kl->key_count; j++) {
926 kl->keys[j - 1] = kl->keys[j];
929 kl->key_count--;
933 if (key->sub_keys) nt_delete_key_list(key->sub_keys, False);
934 if (key->values) nt_delete_val_list(key->values);
935 if (key->security) nt_delete_key_sec_desc(key->security);
936 free(key);
938 return 1;
942 * Add a key to the tree ... We walk down the components matching until
943 * we don't find any. There must be a match on the first component ...
944 * We return the key structure for the final component as that is
945 * often where we want to add values ...
947 REG_KEY *nt_add_reg_key_list(KEY_LIST *list, char * name, REG_KEY *key, int xxx)
950 return NULL;
954 REG_KEY *nt_add_reg_key(REG_KEY *key, char *name)
956 char *lname = NULL, *c1, *c2;
957 REG_KEY * tmp;
960 * Look until we hit the first component that does not exist, and
961 * then add from there. However, if the first component does not
962 * match and the path we are given is the root, then it must match
964 if (!key || !name || !*name) return NULL;
966 lname = strdup(name);
967 if (!lname) return NULL;
969 c1 = lname;
970 c2 = strchr(c1, '\\');
971 if (c2) { /* Split here ... */
972 *c2 = 0;
973 c2++;
977 * If we don't match, then we have to return error ...
978 * If we do match on this component, check the next one in the
979 * list, and if not found, add it ... short circuit, add all the
980 * way down
983 if (strcmp(c1, key->name) != 0)
984 goto error;
986 tmp = nt_add_reg_key_list(key->sub_keys, c2, key, True);
987 free(lname);
988 return tmp;
990 error:
991 if (lname) free(lname);
992 return NULL;
996 * Create/delete value lists, add/delete values, count them
1001 * Create/delete security descriptors, add/delete SIDS, count SIDS, etc.
1002 * We reference count the security descriptors. Any new reference increments
1003 * the ref count. If we modify an SD, we copy the old one, dec the ref count
1004 * and make the change. We also want to be able to check for equality so
1005 * we can reduce the number of SDs in use.
1009 * Load and unload a registry file.
1011 * Load, loads it into memory as a tree, while unload sealizes/flattens it
1015 * Get the starting record for NT Registry file
1019 * Where we keep all the regf stuff for one registry.
1020 * This is the structure that we use to tie the in memory tree etc
1021 * together. By keeping separate structs, we can operate on different
1022 * registries at the same time.
1023 * Currently, the SK_MAP is an array of mapping structure.
1024 * Since we only need this on input and output, we fill in the structure
1025 * as we go on input. On output, we know how many SK items we have, so
1026 * we can allocate the structure as we need to.
1027 * If you add stuff here that is dynamically allocated, add the
1028 * appropriate free statements below.
1031 #define REGF_REGTYPE_NONE 0
1032 #define REGF_REGTYPE_NT 1
1033 #define REGF_REGTYPE_W9X 2
1035 #define TTTONTTIME(r, t1, t2) (r)->last_mod_time.low = (t1); \
1036 (r)->last_mod_time.high = (t2);
1038 #define REGF_HDR_BLKSIZ 0x1000
1041 * Structures for dealing with the on-disk format of the registry
1044 #define IVAL(buf) ((unsigned int) \
1045 (unsigned int)*((unsigned char *)(buf)+3)<<24| \
1046 (unsigned int)*((unsigned char *)(buf)+2)<<16| \
1047 (unsigned int)*((unsigned char *)(buf)+1)<<8| \
1048 (unsigned int)*((unsigned char *)(buf)+0))
1050 #define SVAL(buf) ((unsigned short) \
1051 (unsigned short)*((unsigned char *)(buf)+1)<<8| \
1052 (unsigned short)*((unsigned char *)(buf)+0))
1054 #define CVAL(buf) ((unsigned char)*((unsigned char *)(buf)))
1056 #define OFF(f) ((f) + REGF_HDR_BLKSIZ + 4)
1057 #define LOCN(base, f) ((base) + OFF(f))
1059 const VAL_STR reg_type_names[] = {
1060 { REG_TYPE_REGSZ, "REG_SZ" },
1061 { REG_TYPE_EXPANDSZ, "REG_EXPAND_SZ" },
1062 { REG_TYPE_BIN, "REG_BIN" },
1063 { REG_TYPE_DWORD, "REG_DWORD" },
1064 { REG_TYPE_MULTISZ, "REG_MULTI_SZ" },
1065 { 0, NULL },
1068 const char *val_to_str(unsigned int val, const VAL_STR *val_array)
1070 int i = 0;
1072 if (!val_array) return NULL;
1074 while (val_array[i].val && val_array[i].str) {
1076 if (val_array[i].val == val) return val_array[i].str;
1077 i++;
1081 return NULL;
1086 * Convert from UniCode to Ascii ... Does not take into account other lang
1087 * Restrict by ascii_max if > 0
1089 int uni_to_ascii(unsigned char *uni, unsigned char *ascii, int ascii_max,
1090 int uni_max)
1092 int i = 0;
1094 while (i < ascii_max && !(!uni[i*2] && !uni[i*2+1])) {
1095 if (uni_max > 0 && (i*2) >= uni_max) break;
1096 ascii[i] = uni[i*2];
1097 i++;
1101 ascii[i] = '\0';
1103 return i;
1107 * Convert a data value to a string for display
1109 int data_to_ascii(unsigned char *datap, int len, int type, char *ascii, int ascii_max)
1111 unsigned char *asciip;
1112 int i;
1114 switch (type) {
1115 case REG_TYPE_REGSZ:
1116 if (verbose) fprintf(stderr, "Len: %d\n", len);
1117 return uni_to_ascii(datap, ascii, len, ascii_max);
1118 break;
1120 case REG_TYPE_EXPANDSZ:
1121 return uni_to_ascii(datap, ascii, len, ascii_max);
1122 break;
1124 case REG_TYPE_BIN:
1125 asciip = ascii;
1126 for (i=0; (i<len)&&(i+1)*3<ascii_max; i++) {
1127 int str_rem = ascii_max - ((int)asciip - (int)ascii);
1128 asciip += snprintf(asciip, str_rem, "%02x", *(unsigned char *)(datap+i));
1129 if (i < len && str_rem > 0)
1130 *asciip = ' '; asciip++;
1132 *asciip = '\0';
1133 return ((int)asciip - (int)ascii);
1134 break;
1136 case REG_TYPE_DWORD:
1137 if (*(int *)datap == 0)
1138 return snprintf(ascii, ascii_max, "0");
1139 else
1140 return snprintf(ascii, ascii_max, "0x%x", *(int *)datap);
1141 break;
1143 case REG_TYPE_MULTISZ:
1145 break;
1147 default:
1148 return 0;
1149 break;
1152 return len;
1156 REG_KEY *nt_get_key_tree(REGF *regf, NK_HDR *nk_hdr, int size, REG_KEY *parent);
1158 int nt_set_regf_input_file(REGF *regf, char *filename)
1160 return ((regf->regfile_name = strdup(filename)) != NULL);
1163 int nt_set_regf_output_file(REGF *regf, char *filename)
1165 return ((regf->outfile_name = strdup(filename)) != NULL);
1168 /* Create a regf structure and init it */
1170 REGF *nt_create_regf(void)
1172 REGF *tmp = (REGF *)malloc(sizeof(REGF));
1173 if (!tmp) return tmp;
1174 bzero(tmp, sizeof(REGF));
1175 return tmp;
1178 /* Free all the bits and pieces ... Assumes regf was malloc'd */
1179 /* If you add stuff to REGF, add the relevant free bits here */
1180 int nt_free_regf(REGF *regf)
1182 if (!regf) return 0;
1184 if (regf->regfile_name) free(regf->regfile_name);
1185 if (regf->outfile_name) free(regf->outfile_name);
1187 /* Free the mmap'd area */
1189 if (regf->base) munmap(regf->base, regf->sbuf.st_size);
1190 regf->base = NULL;
1191 close(regf->fd); /* Ignore the error :-) */
1193 nt_delete_reg_key(regf->root, False); /* Free the tree */
1194 free(regf->sk_map);
1195 regf->sk_count = regf->sk_map_size = 0;
1197 free(regf);
1199 return 1;
1202 /* Get the header of the registry. Return a pointer to the structure
1203 * If the mmap'd area has not been allocated, then mmap the input file
1205 REGF_HDR *nt_get_regf_hdr(REGF *regf)
1207 if (!regf)
1208 return NULL; /* What about errors */
1210 if (!regf->regfile_name)
1211 return NULL; /* What about errors */
1213 if (!regf->base) { /* Try to mmap etc the file */
1215 if ((regf->fd = open(regf->regfile_name, O_RDONLY, 0000)) <0) {
1216 return NULL; /* What about errors? */
1219 if (fstat(regf->fd, &regf->sbuf) < 0) {
1220 return NULL;
1223 regf->base = mmap(0, regf->sbuf.st_size, PROT_READ, MAP_SHARED, regf->fd, 0);
1225 if ((int)regf->base == 1) {
1226 fprintf(stderr, "Could not mmap file: %s, %s\n", regf->regfile_name,
1227 strerror(errno));
1228 return NULL;
1233 * At this point, regf->base != NULL, and we should be able to read the
1234 * header
1237 assert(regf->base != NULL);
1239 return (REGF_HDR *)regf->base;
1243 * Validate a regf header
1244 * For now, do nothing, but we should check the checksum
1246 int valid_regf_hdr(REGF_HDR *regf_hdr)
1248 if (!regf_hdr) return 0;
1250 return 1;
1254 * Process an SK header ...
1255 * Every time we see a new one, add it to the map. Otherwise, just look it up.
1256 * We will do a simple linear search for the moment, since many KEYs have the
1257 * same security descriptor.
1258 * We allocate the map in increments of 10 entries.
1262 * Create a new entry in the map, and increase the size of the map if needed
1265 SK_MAP *alloc_sk_map_entry(REGF *regf, KEY_SEC_DESC *tmp, int sk_off)
1267 if (!regf->sk_map) { /* Allocate a block of 10 */
1268 regf->sk_map = (SK_MAP *)malloc(sizeof(SK_MAP) * 10);
1269 if (!regf->sk_map) {
1270 free(tmp);
1271 return NULL;
1273 regf->sk_map_size = 10;
1274 regf->sk_count = 1;
1275 (regf->sk_map)[0].sk_off = sk_off;
1276 (regf->sk_map)[0].key_sec_desc = tmp;
1278 else { /* Simply allocate a new slot, unless we have to expand the list */
1279 int ndx = regf->sk_count;
1280 if (regf->sk_count >= regf->sk_map_size) {
1281 regf->sk_map = (SK_MAP *)realloc(regf->sk_map,
1282 (regf->sk_map_size + 10)*sizeof(SK_MAP));
1283 if (!regf->sk_map) {
1284 free(tmp);
1285 return NULL;
1288 * ndx already points at the first entry of the new block
1290 regf->sk_map_size += 10;
1292 (regf->sk_map)[ndx].sk_off = sk_off;
1293 (regf->sk_map)[ndx].key_sec_desc = tmp;
1294 regf->sk_count++;
1296 return regf->sk_map;
1300 * Search for a KEY_SEC_DESC in the sk_map, but don't create one if not
1301 * found
1304 KEY_SEC_DESC *lookup_sec_key(SK_MAP *sk_map, int count, int sk_off)
1306 int i;
1308 if (!sk_map) return NULL;
1310 for (i = 0; i < count; i++) {
1312 if (sk_map[i].sk_off == sk_off)
1313 return sk_map[i].key_sec_desc;
1317 return NULL;
1322 * Allocate a KEY_SEC_DESC if we can't find one in the map
1325 KEY_SEC_DESC *lookup_create_sec_key(REGF *regf, SK_MAP *sk_map, int sk_off)
1327 KEY_SEC_DESC *tmp = lookup_sec_key(regf->sk_map, regf->sk_count, sk_off);
1329 if (tmp) {
1330 return tmp;
1332 else { /* Allocate a new one */
1333 tmp = (KEY_SEC_DESC *)malloc(sizeof(KEY_SEC_DESC));
1334 if (!tmp) {
1335 return NULL;
1337 tmp->state = SEC_DESC_RES;
1338 if (!alloc_sk_map_entry(regf, tmp, sk_off)) {
1339 return NULL;
1341 return tmp;
1346 * Allocate storage and duplicate a SID
1347 * We could allocate the SID to be only the size needed, but I am too lazy.
1349 DOM_SID *dup_sid(DOM_SID *sid)
1351 DOM_SID *tmp = (DOM_SID *)malloc(sizeof(DOM_SID));
1352 int i;
1354 if (!tmp) return NULL;
1355 tmp->ver = sid->ver;
1356 tmp->auths = sid->auths;
1357 for (i=0; i<6; i++) {
1358 tmp->auth[i] = sid->auth[i];
1360 for (i=0; i<tmp->auths&&i<MAXSUBAUTHS; i++) {
1361 tmp->sub_auths[i] = sid->sub_auths[i];
1363 return tmp;
1367 * Allocate space for an ACE and duplicate the registry encoded one passed in
1369 ACE *dup_ace(REG_ACE *ace)
1371 ACE *tmp = NULL;
1373 tmp = (ACE *)malloc(sizeof(ACE));
1375 if (!tmp) return NULL;
1377 tmp->type = CVAL(&ace->type);
1378 tmp->flags = CVAL(&ace->flags);
1379 tmp->perms = IVAL(&ace->perms);
1380 tmp->trustee = dup_sid(&ace->trustee);
1381 return tmp;
1385 * Allocate space for an ACL and duplicate the registry encoded one passed in
1387 ACL *dup_acl(REG_ACL *acl)
1389 ACL *tmp = NULL;
1390 REG_ACE* ace;
1391 int i, num_aces;
1393 num_aces = IVAL(&acl->num_aces);
1395 tmp = (ACL *)malloc(sizeof(ACL) + (num_aces - 1)*sizeof(ACE *));
1396 if (!tmp) return NULL;
1398 tmp->num_aces = num_aces;
1399 tmp->refcnt = 1;
1400 tmp->rev = SVAL(&acl->rev);
1401 ace = (REG_ACE *)&acl->aces;
1402 for (i=0; i<num_aces; i++) {
1403 tmp->aces[i] = dup_ace(ace);
1404 ace = (REG_ACE *)((char *)ace + SVAL(&ace->length));
1405 /* XXX: FIXME, should handle malloc errors */
1408 return tmp;
1411 SEC_DESC *process_sec_desc(REGF *regf, REG_SEC_DESC *sec_desc)
1413 SEC_DESC *tmp = NULL;
1415 tmp = (SEC_DESC *)malloc(sizeof(SEC_DESC));
1417 if (!tmp) {
1418 return NULL;
1421 tmp->rev = SVAL(&sec_desc->rev);
1422 tmp->type = SVAL(&sec_desc->type);
1423 tmp->owner = dup_sid((DOM_SID *)((char *)sec_desc + IVAL(&sec_desc->owner_off)));
1424 if (!tmp->owner) {
1425 free(tmp);
1426 return NULL;
1428 tmp->group = dup_sid((DOM_SID *)((char *)sec_desc + IVAL(&sec_desc->group_off)));
1429 if (!tmp->group) {
1430 free(tmp);
1431 return NULL;
1434 /* Now pick up the SACL and DACL */
1436 if (sec_desc->sacl_off)
1437 tmp->sacl = dup_acl((REG_ACL *)((char *)sec_desc + IVAL(&sec_desc->sacl_off)));
1438 else
1439 tmp->sacl = NULL;
1441 if (sec_desc->dacl_off)
1442 tmp->dacl = dup_acl((REG_ACL *)((char *)sec_desc + IVAL(&sec_desc->dacl_off)));
1443 else
1444 tmp->dacl = NULL;
1446 return tmp;
1449 KEY_SEC_DESC *process_sk(REGF *regf, SK_HDR *sk_hdr, int sk_off, int size)
1451 KEY_SEC_DESC *tmp = NULL;
1452 int sk_next_off, sk_prev_off, sk_size;
1453 REG_SEC_DESC *sec_desc;
1455 if (!sk_hdr) return NULL;
1457 if (SVAL(&sk_hdr->SK_ID) != REG_SK_ID) {
1458 fprintf(stderr, "Unrecognized SK Header ID: %08X, %s\n", (int)sk_hdr,
1459 regf->regfile_name);
1460 return NULL;
1463 if (-size < (sk_size = IVAL(&sk_hdr->rec_size))) {
1464 fprintf(stderr, "Incorrect SK record size: %d vs %d. %s\n",
1465 -size, sk_size, regf->regfile_name);
1466 return NULL;
1470 * Now, we need to look up the SK Record in the map, and return it
1471 * Since the map contains the SK_OFF mapped to KEY_SEC_DESC, we can
1472 * use that
1475 if (regf->sk_map &&
1476 ((tmp = lookup_sec_key(regf->sk_map, regf->sk_count, sk_off)) != NULL)
1477 && (tmp->state == SEC_DESC_OCU)) {
1478 tmp->ref_cnt++;
1479 return tmp;
1482 /* Here, we have an item in the map that has been reserved, or tmp==NULL. */
1484 assert(tmp == NULL || (tmp && tmp->state != SEC_DESC_NON));
1487 * Now, allocate a KEY_SEC_DESC, and parse the structure here, and add the
1488 * new KEY_SEC_DESC to the mapping structure, since the offset supplied is
1489 * the actual offset of structure. The same offset will be used by
1490 * all future references to this structure
1491 * We could put all this unpleasantness in a function.
1494 if (!tmp) {
1495 tmp = (KEY_SEC_DESC *)malloc(sizeof(KEY_SEC_DESC));
1496 if (!tmp) return NULL;
1497 bzero(tmp, sizeof(KEY_SEC_DESC));
1500 * Allocate an entry in the SK_MAP ...
1501 * We don't need to free tmp, because that is done for us if the
1502 * sm_map entry can't be expanded when we need more space in the map.
1505 if (!alloc_sk_map_entry(regf, tmp, sk_off)) {
1506 return NULL;
1510 tmp->ref_cnt++;
1511 tmp->state = SEC_DESC_OCU;
1514 * Now, process the actual sec desc and plug the values in
1517 sec_desc = (REG_SEC_DESC *)&sk_hdr->sec_desc[0];
1518 tmp->sec_desc = process_sec_desc(regf, sec_desc);
1521 * Now forward and back links. Here we allocate an entry in the sk_map
1522 * if it does not exist, and mark it reserved
1525 sk_prev_off = IVAL(&sk_hdr->prev_off);
1526 tmp->prev = lookup_create_sec_key(regf, regf->sk_map, sk_prev_off);
1527 assert(tmp->prev != NULL);
1528 sk_next_off = IVAL(&sk_hdr->next_off);
1529 tmp->next = lookup_create_sec_key(regf, regf->sk_map, sk_next_off);
1530 assert(tmp->next != NULL);
1532 return tmp;
1536 * Process a VK header and return a value
1538 VAL_KEY *process_vk(REGF *regf, VK_HDR *vk_hdr, int size)
1540 char val_name[1024];
1541 int nam_len, dat_len, flag, dat_type, dat_off, vk_id;
1542 const char *val_type;
1543 VAL_KEY *tmp = NULL;
1545 if (!vk_hdr) return NULL;
1547 if ((vk_id = SVAL(&vk_hdr->VK_ID)) != REG_VK_ID) {
1548 fprintf(stderr, "Unrecognized VK header ID: %0X, block: %0X, %s\n",
1549 vk_id, (int)vk_hdr, regf->regfile_name);
1550 return NULL;
1553 nam_len = SVAL(&vk_hdr->nam_len);
1554 val_name[nam_len] = '\0';
1555 flag = SVAL(&vk_hdr->flag);
1556 dat_type = IVAL(&vk_hdr->dat_type);
1557 dat_len = IVAL(&vk_hdr->dat_len); /* If top bit, offset contains data */
1558 dat_off = IVAL(&vk_hdr->dat_off);
1560 tmp = (VAL_KEY *)malloc(sizeof(VAL_KEY));
1561 if (!tmp) {
1562 goto error;
1564 bzero(tmp, sizeof(VAL_KEY));
1565 tmp->has_name = flag;
1566 tmp->data_type = dat_type;
1568 if (flag & 0x01) {
1569 strncpy(val_name, vk_hdr->dat_name, nam_len);
1570 tmp->name = strdup(val_name);
1571 if (!tmp->name) {
1572 goto error;
1575 else
1576 strncpy(val_name, "<No Name>", 10);
1579 * Allocate space and copy the data as a BLOB
1582 if (dat_len) {
1584 char *dtmp = (char *)malloc(dat_len&0x7FFFFFFF);
1586 if (!dtmp) {
1587 goto error;
1590 tmp->data_blk = dtmp;
1592 if ((dat_len&0x80000000) == 0) { /* The data is pointed to by the offset */
1593 char *dat_ptr = LOCN(regf->base, dat_off);
1594 bcopy(dat_ptr, dtmp, dat_len);
1596 else { /* The data is in the offset */
1597 dat_len = dat_len & 0x7FFFFFFF;
1598 bcopy(&dat_off, dtmp, dat_len);
1601 tmp->data_len = dat_len;
1604 val_type = val_to_str(dat_type, reg_type_names);
1607 * We need to save the data area as well
1610 if (verbose) fprintf(stdout, " %s : %s : \n", val_name, val_type);
1612 return tmp;
1614 error:
1615 /* XXX: FIXME, free the partially allocated struct */
1616 return NULL;
1621 * Process a VL Header and return a list of values
1623 VAL_LIST *process_vl(REGF *regf, VL_TYPE vl, int count, int size)
1625 int i, vk_off;
1626 VK_HDR *vk_hdr;
1627 VAL_LIST *tmp = NULL;
1629 if (!vl) return NULL;
1631 if (-size < (count+1)*sizeof(int)){
1632 fprintf(stderr, "Error in VL header format. Size less than space required. %d\n", -size);
1633 return NULL;
1636 tmp = (VAL_LIST *)malloc(sizeof(VAL_LIST) + (count - 1) * sizeof(VAL_KEY *));
1637 if (!tmp) {
1638 goto error;
1641 for (i=0; i<count; i++) {
1642 vk_off = IVAL(&vl[i]);
1643 vk_hdr = (VK_HDR *)LOCN(regf->base, vk_off);
1644 tmp->vals[i] = process_vk(regf, vk_hdr, BLK_SIZE(vk_hdr));
1645 if (!tmp->vals[i]){
1646 goto error;
1650 tmp->val_count = count;
1652 return tmp;
1654 error:
1655 /* XXX: FIXME, free the partially allocated structure */
1656 return NULL;
1660 * Process an LF Header and return a list of sub-keys
1662 KEY_LIST *process_lf(REGF *regf, LF_HDR *lf_hdr, int size, REG_KEY *parent)
1664 int count, i, nk_off;
1665 unsigned int lf_id;
1666 KEY_LIST *tmp;
1668 if (!lf_hdr) return NULL;
1670 if ((lf_id = SVAL(&lf_hdr->LF_ID)) != REG_LF_ID) {
1671 fprintf(stderr, "Unrecognized LF Header format: %0X, Block: %0X, %s.\n",
1672 lf_id, (int)lf_hdr, regf->regfile_name);
1673 return NULL;
1676 assert(size < 0);
1678 count = SVAL(&lf_hdr->key_count);
1680 if (count <= 0) return NULL;
1682 /* Now, we should allocate a KEY_LIST struct and fill it in ... */
1684 tmp = (KEY_LIST *)malloc(sizeof(KEY_LIST) + (count - 1) * sizeof(REG_KEY *));
1685 if (!tmp) {
1686 goto error;
1689 tmp->key_count = count;
1690 tmp->max_keys = count;
1692 for (i=0; i<count; i++) {
1693 NK_HDR *nk_hdr;
1695 nk_off = IVAL(&lf_hdr->hr[i].nk_off);
1696 nk_hdr = (NK_HDR *)LOCN(regf->base, nk_off);
1697 tmp->keys[i] = nt_get_key_tree(regf, nk_hdr, BLK_SIZE(nk_hdr), parent);
1698 if (!tmp->keys[i]) {
1699 goto error;
1703 return tmp;
1705 error:
1706 if (tmp) nt_delete_key_list(tmp, False);
1707 return NULL;
1711 * This routine is passed a NK_HDR pointer and retrieves the entire tree
1712 * from there down. It returns a REG_KEY *.
1714 REG_KEY *nt_get_key_tree(REGF *regf, NK_HDR *nk_hdr, int size, REG_KEY *parent)
1716 REG_KEY *tmp = NULL, *own;
1717 int name_len, clsname_len, lf_off, val_off, val_count, sk_off, own_off;
1718 unsigned int nk_id;
1719 LF_HDR *lf_hdr;
1720 VL_TYPE *vl;
1721 SK_HDR *sk_hdr;
1722 char key_name[1024], cls_name[1024];
1724 if (!nk_hdr) return NULL;
1726 if ((nk_id = SVAL(&nk_hdr->NK_ID)) != REG_NK_ID) {
1727 fprintf(stderr, "Unrecognized NK Header format: %08X, Block: %0X. %s\n",
1728 nk_id, (int)nk_hdr, regf->regfile_name);
1729 return NULL;
1732 assert(size < 0);
1734 name_len = SVAL(&nk_hdr->nam_len);
1735 clsname_len = SVAL(&nk_hdr->clsnam_len);
1738 * The value of -size should be ge
1739 * (sizeof(NK_HDR) - 1 + name_len)
1740 * The -1 accounts for the fact that we included the first byte of
1741 * the name in the structure. clsname_len is the length of the thing
1742 * pointed to by clsnam_off
1745 if (-size < (sizeof(NK_HDR) - 1 + name_len)) {
1746 fprintf(stderr, "Incorrect NK_HDR size: %d, %0X\n", -size, (int)nk_hdr);
1747 fprintf(stderr, "Sizeof NK_HDR: %d, name_len %d, clsname_len %d\n",
1748 sizeof(NK_HDR), name_len, clsname_len);
1749 /*return NULL;*/
1752 if (verbose) fprintf(stdout, "NK HDR: Name len: %d, class name len: %d\n",
1753 name_len, clsname_len);
1755 /* Fish out the key name and process the LF list */
1757 assert(name_len < sizeof(key_name));
1759 /* Allocate the key struct now */
1760 tmp = (REG_KEY *)malloc(sizeof(REG_KEY));
1761 if (!tmp) return tmp;
1762 bzero(tmp, sizeof(REG_KEY));
1764 tmp->type = (SVAL(&nk_hdr->type)==0x2C?REG_ROOT_KEY:REG_SUB_KEY);
1766 strncpy(key_name, nk_hdr->key_nam, name_len);
1767 key_name[name_len] = '\0';
1769 if (verbose) fprintf(stdout, "Key name: %s\n", key_name);
1771 tmp->name = strdup(key_name);
1772 if (!tmp->name) {
1773 goto error;
1777 * Fish out the class name, it is in UNICODE, while the key name is
1778 * ASCII :-)
1781 if (clsname_len) { /* Just print in Ascii for now */
1782 char *clsnamep;
1783 int clsnam_off;
1785 clsnam_off = IVAL(&nk_hdr->clsnam_off);
1786 clsnamep = LOCN(regf->base, clsnam_off);
1788 bzero(cls_name, clsname_len);
1789 uni_to_ascii(clsnamep, cls_name, sizeof(cls_name), clsname_len);
1792 * I am keeping class name as an ascii string for the moment.
1793 * That means it needs to be converted on output.
1794 * It will also piss off people who need Unicode/UTF-8 strings. Sorry.
1795 * XXX: FIXME
1798 tmp->class_name = strdup(cls_name);
1799 if (!tmp->class_name) {
1800 goto error;
1803 if (verbose) fprintf(stdout, " Class Name: %s\n", cls_name);
1808 * Process the owner offset ...
1811 own_off = IVAL(&nk_hdr->own_off);
1812 own = (REG_KEY *)LOCN(regf->base, own_off);
1814 if (verbose) fprintf(stdout, " Owner offset: %0X, Our Offset: %0X\n",
1815 own, nk_hdr);
1818 * We should verify that the owner field is correct ...
1819 * for now, we don't worry ...
1822 tmp->owner = parent;
1825 * If there are any values, process them here
1828 val_count = IVAL(&nk_hdr->val_cnt);
1830 if (val_count) {
1832 val_off = IVAL(&nk_hdr->val_off);
1833 vl = (VL_TYPE *)LOCN(regf->base, val_off);
1835 tmp->values = process_vl(regf, *vl, val_count, BLK_SIZE(vl));
1836 if (!tmp->values) {
1837 goto error;
1843 * Also handle the SK header ...
1846 sk_off = IVAL(&nk_hdr->sk_off);
1847 sk_hdr = (SK_HDR *)LOCN(regf->base, sk_off);
1849 if (sk_off != -1) {
1851 tmp->security = process_sk(regf, sk_hdr, sk_off, BLK_SIZE(sk_hdr));
1855 lf_off = IVAL(&nk_hdr->lf_off);
1858 * No more subkeys if lf_off == -1
1861 if (lf_off != -1) {
1863 lf_hdr = (LF_HDR *)LOCN(regf->base, lf_off);
1865 tmp->sub_keys = process_lf(regf, lf_hdr, BLK_SIZE(lf_hdr), tmp);
1866 if (!tmp->sub_keys){
1867 goto error;
1872 return tmp;
1874 error:
1875 if (tmp) nt_delete_reg_key(tmp, False);
1876 return NULL;
1879 int nt_load_registry(REGF *regf)
1881 REGF_HDR *regf_hdr;
1882 unsigned int regf_id, hbin_id;
1883 HBIN_HDR *hbin_hdr;
1884 NK_HDR *first_key;
1886 /* Get the header */
1888 if ((regf_hdr = nt_get_regf_hdr(regf)) == NULL) {
1889 return -1;
1892 /* Now process that header and start to read the rest in */
1894 if ((regf_id = IVAL(&regf_hdr->REGF_ID)) != REG_REGF_ID) {
1895 fprintf(stderr, "Unrecognized NT registry header id: %0X, %s\n",
1896 regf_id, regf->regfile_name);
1897 return -1;
1901 * Validate the header ...
1903 if (!valid_regf_hdr(regf_hdr)) {
1904 fprintf(stderr, "Registry file header does not validate: %s\n",
1905 regf->regfile_name);
1906 return -1;
1909 /* Update the last mod date, and then go get the first NK record and on */
1911 TTTONTTIME(regf, IVAL(&regf_hdr->tim1), IVAL(&regf_hdr->tim2));
1914 * The hbin hdr seems to be just uninteresting garbage. Check that
1915 * it is there, but that is all.
1918 hbin_hdr = (HBIN_HDR *)(regf->base + REGF_HDR_BLKSIZ);
1920 if ((hbin_id = IVAL(&hbin_hdr->HBIN_ID)) != REG_HBIN_ID) {
1921 fprintf(stderr, "Unrecognized registry hbin hdr ID: %0X, %s\n",
1922 hbin_id, regf->regfile_name);
1923 return -1;
1927 * Get a pointer to the first key from the hreg_hdr
1930 first_key = (NK_HDR *)LOCN(regf->base, IVAL(&regf_hdr->first_key));
1933 * Now, get the registry tree by processing that NK recursively
1936 regf->root = nt_get_key_tree(regf, first_key, BLK_SIZE(first_key), NULL);
1938 assert(regf->root != NULL);
1940 return 1;
1944 * Story the registry in the output file
1946 int nt_store_registry(REGF *regf)
1949 return 1;
1953 * Routines to parse a REGEDIT4 file
1955 * The file consists of:
1957 * REGEDIT4
1958 * \[[-]key-path\]\n
1959 * <value-spec>*
1961 * Format:
1962 * [cmd:]name=type:value
1964 * cmd = a|d|c|add|delete|change|as|ds|cs
1966 * There can be more than one key-path and value-spec.
1968 * Since we want to support more than one type of file format, we
1969 * construct a command-file structure that keeps info about the command file
1972 #define FMT_UNREC -1
1973 #define FMT_REGEDIT4 0
1974 #define FMT_EDITREG1_1 1
1976 #define FMT_STRING_REGEDIT4 "REGEDIT4"
1977 #define FMT_STRING_EDITREG1_0 "EDITREG1.0"
1979 #define CMD_NONE 0
1980 #define CMD_ADD_KEY 1
1981 #define CMD_DEL_KEY 2
1983 #define CMD_KEY 1
1984 #define CMD_VAL 2
1986 typedef struct val_spec_list {
1987 struct val_spec_list *next;
1988 char *name;
1989 int type;
1990 char *val; /* Kept as a char string, really? */
1991 } VAL_SPEC_LIST;
1993 typedef struct command_s {
1994 int cmd;
1995 char *key;
1996 int val_count;
1997 VAL_SPEC_LIST *val_spec_list, *val_spec_last;
1998 } CMD;
2000 typedef struct cmd_line {
2001 int len, line_len;
2002 char *line;
2003 } CMD_LINE;
2006 * Some routines to handle lines of info in the command files
2008 void skip_to_eol(int fd)
2010 int rc;
2011 char ch = 0;
2013 while ((rc = read(fd, &ch, 1)) == 1) {
2014 if (ch == 0x0A) return;
2016 if (rc < 0) {
2017 fprintf(stderr, "Could not read file descriptor: %d, %s\n",
2018 fd, strerror(errno));
2019 exit(1);
2023 void free_cmd(CMD *cmd)
2025 if (!cmd) return;
2027 while (cmd->val_spec_list) {
2028 VAL_SPEC_LIST *tmp;
2030 tmp = cmd->val_spec_list;
2031 cmd->val_spec_list = tmp->next;
2032 free(tmp);
2035 free(cmd);
2039 void free_cmd_line(CMD_LINE *cmd_line)
2041 if (cmd_line) {
2042 if (cmd_line->line) free(cmd_line->line);
2043 free(cmd_line);
2047 void print_line(struct cmd_line *cl)
2049 char *pl;
2051 if (!cl) return;
2053 if ((pl = malloc(cl->line_len + 1)) == NULL) {
2054 fprintf(stderr, "Unable to allocate space to print line: %s\n",
2055 strerror(errno));
2056 exit(1);
2059 strncpy(pl, cl->line, cl->line_len);
2060 pl[cl->line_len] = 0;
2062 fprintf(stdout, "%s\n", pl);
2063 free(pl);
2066 #define INIT_ALLOC 10
2069 * Read a line from the input file.
2070 * NULL returned when EOF and no chars read
2071 * Otherwise we return a cmd_line *
2072 * Exit if other errors
2074 struct cmd_line *get_cmd_line(int fd)
2076 struct cmd_line *cl = (CMD_LINE *)malloc(sizeof(CMD_LINE));
2077 int i = 0, rc;
2078 unsigned char ch;
2080 if (!cl) {
2081 fprintf(stderr, "Unable to allocate structure for command line: %s\n",
2082 strerror(errno));
2083 exit(1);
2086 cl->len = INIT_ALLOC;
2089 * Allocate some space for the line. We extend later if needed.
2092 if ((cl->line = (char *)malloc(INIT_ALLOC)) == NULL) {
2093 fprintf(stderr, "Unable to allocate initial space for line: %s\n",
2094 strerror(errno));
2095 exit(1);
2099 * Now read in the chars to EOL. Don't store the EOL in the
2100 * line. What about CR?
2103 while ((rc = read(fd, &ch, 1)) == 1 && ch != '\n') {
2104 if (ch == '\r') continue; /* skip CR */
2105 if (i == cl->len) {
2107 * Allocate some more memory
2109 if ((cl->line = realloc(cl->line, cl->len + INIT_ALLOC)) == NULL) {
2110 fprintf(stderr, "Unable to realloc space for line: %s\n",
2111 strerror(errno));
2112 exit(1);
2114 cl->len += INIT_ALLOC;
2116 cl->line[i] = ch;
2117 i++;
2120 /* read 0 and we were at loc'n 0, return NULL */
2121 if (rc == 0 && i == 0) {
2122 free_cmd_line(cl);
2123 return NULL;
2126 cl->line_len = i;
2128 return cl;
2133 * parse_value: parse out a value. We pull it apart as:
2135 * <value> ::= <value-name>=<type>:<value-string>
2137 * <value-name> ::= char-string-without-spaces | '"' char-string '"'
2139 * If it parsed OK, return the <value-name> as a string, and the
2140 * value type and value-string in parameters.
2143 char *dup_str(char *s, int len)
2145 char *nstr;
2146 nstr = (char *)malloc(len + 1);
2147 if (nstr) {
2148 memcpy(nstr, s, len);
2149 nstr[len] = 0;
2151 return nstr;
2154 char *parse_name(char *nstr)
2156 int len = 0, start = 0;
2157 if (!nstr) return NULL;
2159 len = strlen(nstr);
2161 while (len && nstr[len - 1] == ' ') len--;
2163 nstr[len] = 0; /* Trim any spaces ... if there were none, doesn't matter */
2166 * Beginning and end should be '"' or neither should be so
2168 if ((nstr[0] == '"' && nstr[len - 1] != '"') ||
2169 (nstr[0] != '"' && nstr[len - 1] == '"'))
2170 return NULL;
2172 if (nstr[0] == '"') {
2173 start = 1;
2174 len -= 2;
2177 return dup_str(&nstr[start], len);
2180 int parse_value_type(char *tstr)
2182 int len = strlen(tstr);
2184 while (len && tstr[len - 1] == ' ') len--;
2185 tstr[len] = 0;
2187 if (strcmp(tstr, "REG_DWORD") == 0)
2188 return REG_TYPE_DWORD;
2189 else if (strcmp(tstr, "dword") == 0)
2190 return REG_TYPE_DWORD;
2191 else if (strcmp(tstr, "REG_EXPAND_SZ") == 0)
2192 return REG_TYPE_EXPANDSZ;
2193 else if (strcmp(tstr, "REG_BIN") == 0)
2194 return REG_TYPE_BIN;
2195 else if (strcmp(tstr, "REG_SZ") == 0)
2196 return REG_TYPE_REGSZ;
2197 else if (strcmp(tstr, "REG_MULTI_SZ") == 0)
2198 return REG_TYPE_MULTISZ;
2200 return 0;
2203 char *parse_val_str(char *vstr)
2206 return dup_str(vstr, strlen(vstr));
2210 char *parse_value(struct cmd_line *cl, int *vtype, char **val)
2212 char *p1 = NULL, *p2 = NULL, *nstr = NULL, *tstr = NULL, *vstr = NULL;
2214 if (!cl || !vtype || !val) return NULL;
2215 if (!cl->line_len) return NULL;
2217 p1 = dup_str(cl->line, cl->line_len);
2218 /* FIXME: Better return codes etc ... */
2219 if (!p1) return NULL;
2220 p2 = strchr(p1, '=');
2221 if (!p2) return NULL;
2223 *p2 = 0; p2++; /* Split into two strings at p2 */
2225 /* Now, parse the name ... */
2227 nstr = parse_name(p1);
2228 if (!nstr) goto error;
2230 /* Now, split the remainder and parse on type and val ... */
2232 tstr = p2;
2233 while (*tstr == ' ') tstr++; /* Skip leading white space */
2234 p2 = strchr(p2, ':');
2236 if (!p2) goto error;
2238 *p2 = 0; p2++; /* split on the : */
2240 *vtype = parse_value_type(tstr);
2242 if (!vtype) goto error;
2244 /* Now, parse the value string. It should return a newly malloc'd string */
2246 while (*p2 == ' ') p2++; /* Skip leading space */
2247 vstr = parse_val_str(p2);
2249 if (!vstr) goto error;
2251 *val = vstr;
2253 return nstr;
2255 error:
2256 if (p1) free(p1);
2257 if (nstr) free(nstr);
2258 if (vstr) free(vstr);
2259 return NULL;
2263 * Parse out a key. Look for a correctly formatted key [...]
2264 * and whether it is a delete or add? A delete is signalled
2265 * by a - in front of the key.
2266 * Assumes that there are no leading and trailing spaces
2269 char *parse_key(struct cmd_line *cl, int *cmd)
2271 int start = 1;
2272 char *tmp;
2274 if (cl->line[0] != '[' ||
2275 cl->line[cl->line_len - 1] != ']') return NULL;
2276 if (cl->line_len == 2) return NULL;
2277 *cmd = CMD_ADD_KEY;
2278 if (cl->line[1] == '-') {
2279 if (cl->line_len == 3) return NULL;
2280 start = 2;
2281 *cmd = CMD_DEL_KEY;
2283 tmp = malloc(cl->line_len - 1 - start + 1);
2284 if (!tmp) return tmp; /* Bail out on no mem ... FIXME */
2285 strncpy(tmp, &cl->line[start], cl->line_len - 1 - start);
2286 tmp[cl->line_len - 1 - start] = 0;
2287 return tmp;
2291 * Parse a line to determine if we have a key or a value
2292 * We only check for key or val ...
2295 int parse_line(struct cmd_line *cl)
2298 if (!cl || cl->len == 0) return 0;
2300 if (cl->line[0] == '[') /* No further checking for now */
2301 return CMD_KEY;
2302 else
2303 return CMD_VAL;
2307 * We seek to offset 0, read in the required number of bytes,
2308 * and compare to the correct value.
2309 * We then seek back to the original location
2311 int regedit4_file_type(int fd)
2313 int cur_ofs = 0;
2314 char desc[9];
2316 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
2317 if (cur_ofs < 0) {
2318 fprintf(stderr, "Unable to get current offset: %s\n", strerror(errno));
2319 exit(1); /* FIXME */
2322 if (cur_ofs) {
2323 lseek(fd, 0, SEEK_SET);
2326 if (read(fd, desc, 8) < 8) {
2327 fprintf(stderr, "Unable to read command file format\n");
2328 exit(2); /* FIXME */
2331 desc[8] = 0;
2333 if (strcmp(desc, FMT_STRING_REGEDIT4) == 0) {
2334 if (cur_ofs) {
2335 lseek(fd, cur_ofs, SEEK_SET);
2337 else {
2338 skip_to_eol(fd);
2340 return FMT_REGEDIT4;
2343 return FMT_UNREC;
2347 * Run though the data in the line and strip anything after a comment
2348 * char.
2350 void strip_comment(struct cmd_line *cl)
2352 int i;
2354 if (!cl) return;
2356 for (i = 0; i < cl->line_len; i++) {
2357 if (cl->line[i] == ';') {
2358 cl->line_len = i;
2359 return;
2365 * trim leading space
2368 void trim_leading_spaces(struct cmd_line *cl)
2370 int i;
2372 if (!cl) return;
2374 for (i = 0; i < cl->line_len; i++) {
2375 if (cl->line[i] != ' '){
2376 if (i) memcpy(cl->line, &cl->line[i], cl->line_len - i);
2377 return;
2383 * trim trailing spaces
2385 void trim_trailing_spaces(struct cmd_line *cl)
2387 int i;
2389 if (!cl) return;
2391 for (i = cl->line_len; i == 0; i--) {
2392 if (cl->line[i-1] != ' ' &&
2393 cl->line[i-1] != '\t') {
2394 cl->line_len = i;
2400 * Get a command ... This consists of possibly multiple lines:
2401 * [key]
2402 * values*
2403 * possibly Empty line
2405 * value ::= <value-name>=<value-type>':'<value-string>
2406 * <value-name> is some path, possibly enclosed in quotes ...
2407 * We alctually look for the next key to terminate a previous key
2409 CMD *regedit4_get_cmd(int fd)
2411 struct command_s *cmd = NULL;
2412 struct cmd_line *cl = NULL;
2413 struct val_spec_list *vl = NULL;
2415 if ((cmd = (struct command_s *)malloc(sizeof(struct command_s))) == NULL) {
2416 fprintf(stderr, "Unable to malloc space for command: %s\n",
2417 strerror(errno));
2418 exit(1);
2421 cmd->cmd = CMD_NONE;
2422 cmd->key = NULL;
2423 cmd->val_spec_list = cmd->val_spec_last = NULL;
2424 while (cl = get_cmd_line(fd)) {
2426 strip_comment(cl); /* remove anything beyond a comment char */
2427 trim_trailing_spaces(cl);
2428 trim_leading_spaces(cl);
2430 if (cl->line_len == 0) { /* An empty line */
2431 free_cmd_line(cl);
2433 else { /* Else, non-empty ... */
2435 * Parse out the bits ...
2437 switch (parse_line(cl)) {
2438 case CMD_KEY:
2439 if ((cmd->key = parse_key(cl, &cmd->cmd)) == NULL) {
2440 fprintf(stderr, "Error parsing key from line: ");
2441 print_line(cl);
2442 fprintf(stderr, "\n");
2444 break;
2446 case CMD_VAL:
2448 * We need to add the value stuff to the list
2449 * There could be a \ on the end which we need to
2450 * handle at some time
2452 vl = (struct val_spec_list *)malloc(sizeof(struct val_spec_list));
2453 if (!vl) goto error;
2454 vl->next = NULL;
2455 vl->name = parse_value(cl, &vl->type, &vl->val);
2456 if (!vl->name) goto error;
2457 if (cmd->val_spec_list == NULL) {
2458 cmd->val_spec_list = cmd->val_spec_last = vl;
2460 else {
2461 cmd->val_spec_last->next = vl;
2462 cmd->val_spec_last = vl;
2464 cmd->val_count++;
2465 break;
2467 default:
2468 fprintf(stderr, "Unrecognized line in command file: \n");
2469 print_line(cl);
2470 break;
2475 if (!cmd->cmd) goto error; /* End of file ... */
2477 return cmd;
2479 error:
2480 if (vl) free(vl);
2481 if (cmd) free_cmd(cmd);
2482 return NULL;
2485 int regedit4_exec_cmd(CMD *cmd)
2488 return 0;
2491 int editreg_1_0_file_type(int fd)
2493 int cur_ofs = 0;
2494 char desc[11];
2496 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
2497 if (cur_ofs < 0) {
2498 fprintf(stderr, "Unable to get current offset: %s\n", strerror(errno));
2499 exit(1); /* FIXME */
2502 if (cur_ofs) {
2503 lseek(fd, 0, SEEK_SET);
2506 if (read(fd, desc, 10) < 10) {
2507 fprintf(stderr, "Unable to read command file format\n");
2508 exit(2); /* FIXME */
2511 desc[10] = 0;
2513 if (strcmp(desc, FMT_STRING_EDITREG1_0) == 0) {
2514 lseek(fd, cur_ofs, SEEK_SET);
2515 return FMT_REGEDIT4;
2518 return FMT_UNREC;
2521 CMD *editreg_1_0_get_cmd(int fd)
2523 return NULL;
2526 int editreg_1_0_exec_cmd(CMD *cmd)
2529 return -1;
2532 typedef struct command_ops_s {
2533 int type;
2534 int (*file_type)(int fd);
2535 CMD *(*get_cmd)(int fd);
2536 int (*exec_cmd)(CMD *cmd);
2537 } CMD_OPS;
2539 CMD_OPS default_cmd_ops[] = {
2540 {0, regedit4_file_type, regedit4_get_cmd, regedit4_exec_cmd},
2541 {1, editreg_1_0_file_type, editreg_1_0_get_cmd, editreg_1_0_exec_cmd},
2542 {-1, NULL, NULL, NULL}
2545 typedef struct command_file_s {
2546 char *name;
2547 int type, fd;
2548 CMD_OPS cmd_ops;
2549 } CMD_FILE;
2552 * Create a new command file structure
2555 CMD_FILE *cmd_file_create(char *file)
2557 CMD_FILE *tmp;
2558 struct stat sbuf;
2559 int i = 0;
2562 * Let's check if the file exists ...
2563 * No use creating the cmd_file structure if the file does not exist
2566 if (stat(file, &sbuf) < 0) { /* Not able to access file */
2568 return NULL;
2571 tmp = (CMD_FILE *)malloc(sizeof(CMD_FILE));
2572 if (!tmp) {
2573 return NULL;
2577 * Let's fill in some of the fields;
2580 tmp->name = strdup(file);
2582 if ((tmp->fd = open(file, O_RDONLY, 666)) < 0) {
2583 free(tmp);
2584 return NULL;
2588 * Now, try to find the format by indexing through the table
2590 while (default_cmd_ops[i].type != -1) {
2591 if ((tmp->type = default_cmd_ops[i].file_type(tmp->fd)) >= 0) {
2592 tmp->cmd_ops = default_cmd_ops[i];
2593 return tmp;
2595 i++;
2599 * If we got here, return NULL, as we could not figure out the type
2600 * of command file.
2602 * What about errors?
2605 free(tmp);
2606 return NULL;
2610 * Extract commands from the command file, and execute them.
2611 * We pass a table of command callbacks for that
2615 * Main code from here on ...
2619 * key print function here ...
2622 int print_key(const char *path, char *name, char *class_name, int root,
2623 int terminal, int vals)
2626 /*if (terminal)*/ fprintf(stdout, "[%s%s]\n", path, name);
2628 return 1;
2632 * Sec Desc print functions
2635 void print_type(unsigned char type)
2637 switch (type) {
2638 case 0x00:
2639 fprintf(stdout, " ALLOW");
2640 break;
2641 case 0x01:
2642 fprintf(stdout, " DENY");
2643 break;
2644 case 0x02:
2645 fprintf(stdout, " AUDIT");
2646 break;
2647 case 0x03:
2648 fprintf(stdout, " ALARM");
2649 break;
2650 case 0x04:
2651 fprintf(stdout, "ALLOW CPD");
2652 break;
2653 case 0x05:
2654 fprintf(stdout, "OBJ ALLOW");
2655 break;
2656 case 0x06:
2657 fprintf(stdout, " OBJ DENY");
2658 default:
2659 fprintf(stdout, " UNKNOWN");
2660 break;
2664 void print_flags(unsigned char flags)
2666 char flg_output[21];
2667 int some = 0;
2669 flg_output[0] = 0;
2670 if (!flags) {
2671 fprintf(stdout, " ");
2672 return;
2674 if (flags & 0x01) {
2675 if (some) strcat(flg_output, ",");
2676 some = 1;
2677 strcat(flg_output, "OI");
2679 if (flags & 0x02) {
2680 if (some) strcat(flg_output, ",");
2681 some = 1;
2682 strcat(flg_output, "CI");
2684 if (flags & 0x04) {
2685 if (some) strcat(flg_output, ",");
2686 some = 1;
2687 strcat(flg_output, "NP");
2689 if (flags & 0x08) {
2690 if (some) strcat(flg_output, ",");
2691 some = 1;
2692 strcat(flg_output, "IO");
2694 if (flags & 0x10) {
2695 if (some) strcat(flg_output, ",");
2696 some = 1;
2697 strcat(flg_output, "IA");
2699 if (flags == 0xF) {
2700 if (some) strcat(flg_output, ",");
2701 some = 1;
2702 strcat(flg_output, "VI");
2704 fprintf(stdout, " %s", flg_output);
2707 void print_perms(int perms)
2709 fprintf(stdout, " %8X", perms);
2712 void print_sid(DOM_SID *sid)
2714 int i, comps = sid->auths;
2715 fprintf(stdout, "S-%u-%u", sid->ver, sid->auth[5]);
2717 for (i = 0; i < comps; i++) {
2719 fprintf(stdout, "-%u", sid->sub_auths[i]);
2722 fprintf(stdout, "\n");
2725 void print_acl(ACL *acl, char *prefix)
2727 int i;
2729 for (i = 0; i < acl->num_aces; i++) {
2730 fprintf(stdout, ";;%s", prefix);
2731 print_type(acl->aces[i]->type);
2732 print_flags(acl->aces[i]->flags);
2733 print_perms(acl->aces[i]->perms);
2734 fprintf(stdout, " ");
2735 print_sid(acl->aces[i]->trustee);
2739 int print_sec(SEC_DESC *sec_desc)
2741 if (!print_security) return 1;
2742 fprintf(stdout, ";; SECURITY\n");
2743 fprintf(stdout, ";; Owner: ");
2744 print_sid(sec_desc->owner);
2745 fprintf(stdout, ";; Group: ");
2746 print_sid(sec_desc->group);
2747 if (sec_desc->sacl) {
2748 fprintf(stdout, ";; SACL:\n");
2749 print_acl(sec_desc->sacl, " ");
2751 if (sec_desc->dacl) {
2752 fprintf(stdout, ";; DACL:\n");
2753 print_acl(sec_desc->dacl, " ");
2755 return 1;
2759 * Value print function here ...
2761 int print_val(const char *path, char *val_name, int val_type, int data_len,
2762 void *data_blk, int terminal, int first, int last)
2764 char data_asc[1024];
2766 bzero(data_asc, sizeof(data_asc));
2767 if (!terminal && first)
2768 fprintf(stdout, "%s\n", path);
2769 data_to_ascii((unsigned char *)data_blk, data_len, val_type, data_asc,
2770 sizeof(data_asc) - 1);
2771 fprintf(stdout, " %s = %s : %s\n", (val_name?val_name:"<No Name>"),
2772 val_to_str(val_type, reg_type_names), data_asc);
2773 return 1;
2776 void usage(void)
2778 fprintf(stderr, "Usage: editreg [-v] [-p] [-k] [-s] [-c <command-file>] <registryfile>\n");
2779 fprintf(stderr, "Version: 0.1\n\n");
2780 fprintf(stderr, "\n\t-v\t sets verbose mode");
2781 fprintf(stderr, "\n\t-p\t prints the registry");
2782 fprintf(stderr, "\n\t-s\t prints security descriptors");
2783 fprintf(stderr, "\n\t-c <command-file>\t specifies a command file");
2784 fprintf(stderr, "\n");
2787 int main(int argc, char *argv[])
2789 REGF *regf;
2790 extern char *optarg;
2791 extern int optind;
2792 int opt, print_keys = 0;
2793 int regf_opt = 1; /* Command name */
2794 int commands = 0;
2795 char *cmd_file_name = NULL;
2796 char *out_file_name = NULL;
2797 CMD_FILE *cmd_file = NULL;
2799 if (argc < 2) {
2800 usage();
2801 exit(1);
2805 * Now, process the arguments
2808 while ((opt = getopt(argc, argv, "spvko:c:")) != EOF) {
2809 switch (opt) {
2810 case 'c':
2811 commands = 1;
2812 cmd_file_name = optarg;
2813 regf_opt += 2;
2814 break;
2816 case 'o':
2817 out_file_name = optarg;
2818 regf_opt += 2;
2819 break;
2821 case 'p':
2822 print_keys++;
2823 regf_opt++;
2824 break;
2826 case 's':
2827 print_security++;
2828 regf_opt++;
2829 break;
2831 case 'v':
2832 verbose++;
2833 regf_opt++;
2834 break;
2836 case 'k':
2837 regf_opt++;
2838 break;
2840 default:
2841 usage();
2842 exit(1);
2843 break;
2847 if ((regf = nt_create_regf()) == NULL) {
2848 fprintf(stderr, "Could not create registry object: %s\n", strerror(errno));
2849 exit(2);
2852 if (regf_opt < argc) { /* We have a registry file */
2853 if (!nt_set_regf_input_file(regf, argv[regf_opt])) {
2854 fprintf(stderr, "Could not set name of registry file: %s, %s\n",
2855 argv[regf_opt], strerror(errno));
2856 exit(3);
2859 /* Now, open it, and bring it into memory :-) */
2861 if (nt_load_registry(regf) < 0) {
2862 fprintf(stderr, "Could not load registry: %s\n", argv[1]);
2863 exit(4);
2867 if (out_file_name) {
2868 if (!nt_set_regf_output_file(regf, out_file_name)) {
2869 fprintf(stderr, "Could not set name of output registry file: %s, %s\n",
2870 out_file_name, strerror(errno));
2871 exit(3);
2876 if (commands) {
2877 CMD *cmd;
2879 cmd_file = cmd_file_create(cmd_file_name);
2881 while ((cmd = cmd_file->cmd_ops.get_cmd(cmd_file->fd)) != NULL) {
2884 * Now, apply the requests to the tree ...
2886 switch (cmd->cmd) {
2887 case CMD_ADD_KEY:
2889 break;
2891 case CMD_DEL_KEY:
2893 * Any value does not matter ...
2894 * Find the key if it exists, and delete it ...
2897 nt_delete_key_by_name(regf, cmd->key);
2898 break;
2901 free_cmd(cmd);
2905 * At this point, we should have a registry in memory and should be able
2906 * to iterate over it.
2909 if (print_keys) {
2910 nt_key_iterator(regf, regf->root, 0, "", print_key, print_sec, print_val);
2913 return 0;