Start adding code to allow the in memory version of the registry tree
[Samba/gebeck_regimport.git] / source3 / utils / editreg.c
blob532dffe06831569d3030e23ac192c486ee60aa00
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 static int verbose = 0;
314 * These definitions are for the in-memory registry structure.
315 * It is a tree structure that mimics what you see with tools like regedit
319 * DateTime struct for Windows
322 typedef struct date_time_s {
323 unsigned int low, high;
324 } NTTIME;
327 * Definition of a Key. It has a name, classname, date/time last modified,
328 * sub-keys, values, and a security descriptor
331 #define REG_ROOT_KEY 1
332 #define REG_SUB_KEY 2
333 #define REG_SYM_LINK 3
335 typedef struct reg_key_s {
336 char *name; /* Name of the key */
337 char *class_name;
338 int type; /* One of REG_ROOT_KEY or REG_SUB_KEY */
339 NTTIME last_mod; /* Time last modified */
340 struct reg_key_s *owner;
341 struct key_list_s *sub_keys;
342 struct val_list_s *values;
343 struct key_sec_desc_s *security;
344 } REG_KEY;
347 * The KEY_LIST struct lists sub-keys.
350 typedef struct key_list_s {
351 int key_count;
352 REG_KEY *keys[1];
353 } KEY_LIST;
355 typedef struct val_key_s {
356 char *name;
357 int has_name;
358 int data_type;
359 int data_len;
360 void *data_blk; /* Might want a separate block */
361 } VAL_KEY;
363 typedef struct val_list_s {
364 int val_count;
365 VAL_KEY *vals[1];
366 } VAL_LIST;
368 #ifndef MAXSUBAUTHS
369 #define MAXSUBAUTHS 15
370 #endif
372 typedef struct dom_sid_s {
373 unsigned char ver, auths;
374 unsigned char auth[6];
375 unsigned int sub_auths[MAXSUBAUTHS];
376 } DOM_SID;
378 typedef struct ace_struct_s {
379 unsigned char type, flags;
380 unsigned int perms; /* Perhaps a better def is in order */
381 DOM_SID *trustee;
382 } ACE;
384 typedef struct acl_struct_s {
385 unsigned short rev, refcnt;
386 unsigned short num_aces;
387 ACE *aces[1];
388 } ACL;
390 typedef struct sec_desc_s {
391 unsigned int rev, type;
392 DOM_SID *owner, *group;
393 ACL *sacl, *dacl;
394 } SEC_DESC;
396 #define SEC_DESC_NON 0
397 #define SEC_DESC_RES 1
398 #define SEC_DESC_OCU 2
400 typedef struct key_sec_desc_s {
401 struct key_sec_desc_s *prev, *next;
402 int ref_cnt;
403 int state;
404 SEC_DESC *sec_desc;
405 } KEY_SEC_DESC;
409 * An API for accessing/creating/destroying items above
413 * Iterate over the keys, depth first, calling a function for each key
414 * and indicating if it is terminal or non-terminal and if it has values.
416 * In addition, for each value in the list, call a value list function
420 * There should eventually be one to deal with security keys as well
423 typedef int (*key_print_f)(char *path, char *key_name, char *class_name,
424 int root, int terminal, int values);
426 typedef int (*val_print_f)(char *path, char *val_name, int val_type,
427 int data_len, void *data_blk, int terminal,
428 int first, int last);
430 typedef int (*sec_print_f)(SEC_DESC *sec_desc);
432 typedef struct regf_struct_s REGF;
434 int nt_key_iterator(REGF *regf, REG_KEY *key_tree, int bf, char *path,
435 key_print_f key_print, sec_print_f sec_print,
436 val_print_f val_print);
438 int nt_val_list_iterator(REGF *regf, VAL_LIST *val_list, int bf, char *path,
439 int terminal, val_print_f val_print)
441 int i;
443 if (!val_list) return 1;
445 if (!val_print) return 1;
447 for (i=0; i<val_list->val_count; i++) {
448 if (!val_print(path, val_list->vals[i]->name, val_list->vals[i]->data_type,
449 val_list->vals[i]->data_len, val_list->vals[i]->data_blk,
450 terminal,
451 (i == 0),
452 (i == val_list->val_count))) {
454 return 0;
459 return 1;
462 int nt_key_list_iterator(REGF *regf, KEY_LIST *key_list, int bf, char *path,
463 key_print_f key_print, sec_print_f sec_print,
464 val_print_f val_print)
466 int i;
468 if (!key_list) return 1;
470 for (i=0; i< key_list->key_count; i++) {
471 if (!nt_key_iterator(regf, key_list->keys[i], bf, path, key_print,
472 sec_print, val_print)) {
473 return 0;
476 return 1;
479 int nt_key_iterator(REGF *regf, REG_KEY *key_tree, int bf, char *path,
480 key_print_f key_print, sec_print_f sec_print,
481 val_print_f val_print)
483 int path_len = strlen(path);
484 char *new_path;
486 if (!regf || !key_tree)
487 return -1;
489 /* List the key first, then the values, then the sub-keys */
491 if (key_print) {
493 if (!(*key_print)(path, key_tree->name,
494 key_tree->class_name,
495 (key_tree->type == REG_ROOT_KEY),
496 (key_tree->sub_keys == NULL),
497 (key_tree->values?(key_tree->values->val_count):0)))
498 return 0;
502 * If we have a security print routine, call it
504 if (sec_print) {
505 if (key_tree->security && !(*sec_print)(key_tree->security->sec_desc))
506 return 0;
509 new_path = (char *)malloc(path_len + 1 + strlen(key_tree->name) + 1);
510 if (!new_path) return 0; /* Errors? */
511 new_path[0] = '\0';
512 strcat(new_path, path);
513 strcat(new_path, "\\");
514 strcat(new_path, key_tree->name);
517 * Now, iterate through the values in the val_list
520 if (key_tree->values &&
521 !nt_val_list_iterator(regf, key_tree->values, bf, new_path,
522 (key_tree->values!=NULL),
523 val_print)) {
525 free(new_path);
526 return 0;
530 * Now, iterate through the keys in the key list
533 if (key_tree->sub_keys &&
534 !nt_key_list_iterator(regf, key_tree->sub_keys, bf, new_path, key_print,
535 sec_print, val_print)) {
536 free(new_path);
537 return 0;
540 free(new_path);
541 return 1;
544 /* Make, delete keys */
548 int nt_delete_val_list(VAL_LIST *vl)
551 return 1;
554 int nt_delete_val_key(VAL_KEY *val_key)
557 return 1;
560 int nt_delete_key_list(KEY_LIST *key_list)
563 return 1;
566 int nt_delete_sid(DOM_SID *sid)
569 if (sid) free(sid);
570 return 1;
574 int nt_delete_ace(ACE *ace)
577 if (ace) {
578 nt_delete_sid(ace->trustee);
579 free(ace);
581 return 1;
585 int nt_delete_acl(ACL *acl)
588 if (acl) {
589 int i;
591 for (i=0; i<acl->num_aces; i++)
592 nt_delete_ace(acl->aces[i]);
594 free(acl);
596 return 1;
599 int nt_delete_sec_desc(SEC_DESC *sec_desc)
602 if (sec_desc) {
604 nt_delete_sid(sec_desc->owner);
605 nt_delete_sid(sec_desc->group);
606 nt_delete_acl(sec_desc->sacl);
607 nt_delete_acl(sec_desc->dacl);
608 free(sec_desc);
611 return 1;
614 int nt_delete_key_sec_desc(KEY_SEC_DESC *key_sec_desc)
617 if (key_sec_desc) {
618 key_sec_desc->ref_cnt--;
619 if (key_sec_desc->ref_cnt<=0) {
621 * There should always be a next and prev, even if they point to us
623 key_sec_desc->next->prev = key_sec_desc->prev;
624 key_sec_desc->prev->next = key_sec_desc->next;
625 nt_delete_sec_desc(key_sec_desc->sec_desc);
628 return 1;
631 int nt_delete_reg_key(REG_KEY *key)
634 return 1;
638 * Create/delete key lists and add delete keys to/from a list, count the keys
643 * Create/delete value lists, add/delete values, count them
648 * Create/delete security descriptors, add/delete SIDS, count SIDS, etc.
649 * We reference count the security descriptors. Any new reference increments
650 * the ref count. If we modify an SD, we copy the old one, dec the ref count
651 * and make the change. We also want to be able to check for equality so
652 * we can reduce the number of SDs in use.
656 * Code to parse registry specification from command line or files
658 * Format:
659 * [cmd:]key:type:value
661 * cmd = a|d|c|add|delete|change|as|ds|cs
667 * Load and unload a registry file.
669 * Load, loads it into memory as a tree, while unload sealizes/flattens it
673 * Get the starting record for NT Registry file
676 /* A map of sk offsets in the regf to KEY_SEC_DESCs for quick lookup etc */
677 typedef struct sk_map_s {
678 int sk_off;
679 KEY_SEC_DESC *key_sec_desc;
680 } SK_MAP;
683 * Where we keep all the regf stuff for one registry.
684 * This is the structure that we use to tie the in memory tree etc
685 * together. By keeping separate structs, we can operate on different
686 * registries at the same time.
687 * Currently, the SK_MAP is an array of mapping structure.
688 * Since we only need this on input and output, we fill in the structure
689 * as we go on input. On output, we know how many SK items we have, so
690 * we can allocate the structure as we need to.
691 * If you add stuff here that is dynamically allocated, add the
692 * appropriate free statements below.
695 #define REGF_REGTYPE_NONE 0
696 #define REGF_REGTYPE_NT 1
697 #define REGF_REGTYPE_W9X 2
699 #define TTTONTTIME(r, t1, t2) (r)->last_mod_time.low = (t1); \
700 (r)->last_mod_time.high = (t2);
702 #define REGF_HDR_BLKSIZ 0x1000
704 struct regf_struct_s {
705 int reg_type;
706 char *regfile_name, *outfile_name;
707 int fd;
708 struct stat sbuf;
709 char *base;
710 int modified;
711 NTTIME last_mod_time;
712 REG_KEY *root; /* Root of the tree for this file */
713 int sk_count, sk_map_size;
714 SK_MAP *sk_map;
718 * Structures for dealing with the on-disk format of the registry
721 #define IVAL(buf) ((unsigned int) \
722 (unsigned int)*((unsigned char *)(buf)+3)<<24| \
723 (unsigned int)*((unsigned char *)(buf)+2)<<16| \
724 (unsigned int)*((unsigned char *)(buf)+1)<<8| \
725 (unsigned int)*((unsigned char *)(buf)+0))
727 #define SVAL(buf) ((unsigned short) \
728 (unsigned short)*((unsigned char *)(buf)+1)<<8| \
729 (unsigned short)*((unsigned char *)(buf)+0))
731 #define CVAL(buf) ((unsigned char)*((unsigned char *)(buf)))
733 #define OFF(f) ((f) + REGF_HDR_BLKSIZ + 4)
734 #define LOCN(base, f) ((base) + OFF(f))
737 * All of the structures below actually have a four-byte lenght before them
738 * which always seems to be negative. The following macro retrieves that
739 * size as an integer
742 #define BLK_SIZE(b) ((int)*(int *)(((int *)b)-1))
744 typedef unsigned int DWORD;
745 typedef unsigned short WORD;
747 #define REG_REGF_ID 0x66676572
749 typedef struct regf_block {
750 DWORD REGF_ID; /* regf */
751 DWORD uk1;
752 DWORD uk2;
753 DWORD tim1, tim2;
754 DWORD uk3; /* 1 */
755 DWORD uk4; /* 3 */
756 DWORD uk5; /* 0 */
757 DWORD uk6; /* 1 */
758 DWORD first_key; /* offset */
759 unsigned int dblk_size;
760 DWORD uk7[116]; /* 1 */
761 DWORD chksum;
762 } REGF_HDR;
764 typedef struct hbin_sub_struct {
765 DWORD dblocksize;
766 char data[1];
767 } HBIN_SUB_HDR;
769 #define REG_HBIN_ID 0x6E696268
771 typedef struct hbin_struct {
772 DWORD HBIN_ID; /* hbin */
773 DWORD next_off;
774 DWORD prev_off;
775 DWORD uk1;
776 DWORD uk2;
777 DWORD uk3;
778 DWORD uk4;
779 DWORD blk_size;
780 HBIN_SUB_HDR hbin_sub_hdr;
781 } HBIN_HDR;
783 #define REG_NK_ID 0x6B6E
785 typedef struct nk_struct {
786 WORD NK_ID;
787 WORD type;
788 DWORD t1, t2;
789 DWORD uk1;
790 DWORD own_off;
791 DWORD subk_num;
792 DWORD uk2;
793 DWORD lf_off;
794 DWORD uk3;
795 DWORD val_cnt;
796 DWORD val_off;
797 DWORD sk_off;
798 DWORD clsnam_off;
799 DWORD unk4[4];
800 DWORD unk5;
801 WORD nam_len;
802 WORD clsnam_len;
803 char key_nam[1]; /* Actual length determined by nam_len */
804 } NK_HDR;
806 #define REG_SK_ID 0x6B73
808 typedef struct sk_struct {
809 WORD SK_ID;
810 WORD uk1;
811 DWORD prev_off;
812 DWORD next_off;
813 DWORD ref_cnt;
814 DWORD rec_size;
815 char sec_desc[1];
816 } SK_HDR;
818 typedef struct ace_struct {
819 unsigned char type;
820 unsigned char flags;
821 unsigned short length;
822 unsigned int perms;
823 DOM_SID trustee;
824 } REG_ACE;
826 typedef struct acl_struct {
827 WORD rev;
828 WORD size;
829 DWORD num_aces;
830 REG_ACE *aces; /* One or more ACEs */
831 } REG_ACL;
833 typedef struct sec_desc_rec {
834 WORD rev;
835 WORD type;
836 DWORD owner_off;
837 DWORD group_off;
838 DWORD sacl_off;
839 DWORD dacl_off;
840 } REG_SEC_DESC;
842 typedef struct hash_struct {
843 DWORD nk_off;
844 char hash[4];
845 } HASH_REC;
847 #define REG_LF_ID 0x666C
849 typedef struct lf_struct {
850 WORD LF_ID;
851 WORD key_count;
852 struct hash_struct hr[1]; /* Array of hash records, depending on key_count */
853 } LF_HDR;
855 typedef DWORD VL_TYPE[1]; /* Value list is an array of vk rec offsets */
857 #define REG_VK_ID 0x6B76
859 typedef struct vk_struct {
860 WORD VK_ID;
861 WORD nam_len;
862 DWORD dat_len; /* If top-bit set, offset contains the data */
863 DWORD dat_off;
864 DWORD dat_type;
865 WORD flag; /* =1, has name, else no name (=Default). */
866 WORD unk1;
867 char dat_name[1]; /* Name starts here ... */
868 } VK_HDR;
870 #define REG_TYPE_REGSZ 1
871 #define REG_TYPE_EXPANDSZ 2
872 #define REG_TYPE_BIN 3
873 #define REG_TYPE_DWORD 4
874 #define REG_TYPE_MULTISZ 7
876 typedef struct _val_str {
877 unsigned int val;
878 char * str;
879 } VAL_STR;
881 VAL_STR reg_type_names[] = {
882 { 1, "REG_SZ" },
883 { 2, "REG_EXPAND_SZ" },
884 { 3, "REG_BIN" },
885 { 4, "REG_DWORD" },
886 { 7, "REG_MULTI_SZ" },
887 { 0, NULL },
890 char *val_to_str(unsigned int val, VAL_STR *val_array)
892 int i = 0;
894 if (!val_array) return NULL;
896 while (val_array[i].val && val_array[i].str) {
898 if (val_array[i].val == val) return val_array[i].str;
899 i++;
903 return NULL;
908 * Convert from UniCode to Ascii ... Does not take into account other lang
909 * Restrict by ascii_max if > 0
911 int uni_to_ascii(unsigned char *uni, unsigned char *ascii, int ascii_max,
912 int uni_max)
914 int i = 0;
916 while (i < ascii_max && !(!uni[i*2] && !uni[i*2+1])) {
917 if (uni_max > 0 && (i*2) >= uni_max) break;
918 ascii[i] = uni[i*2];
919 i++;
923 ascii[i] = '\0';
925 return i;
929 * Convert a data value to a string for display
931 int data_to_ascii(unsigned char *datap, int len, int type, char *ascii, int ascii_max)
933 unsigned char *asciip;
934 int i;
936 switch (type) {
937 case REG_TYPE_REGSZ:
938 fprintf(stderr, "Len: %d\n", len);
939 return uni_to_ascii(datap, ascii, len, ascii_max);
940 break;
942 case REG_TYPE_EXPANDSZ:
943 return uni_to_ascii(datap, ascii, len, ascii_max);
944 break;
946 case REG_TYPE_BIN:
947 asciip = ascii;
948 for (i=0; (i<len)&&(i+1)*3<ascii_max; i++) {
949 int str_rem = ascii_max - ((int)asciip - (int)ascii);
950 asciip += snprintf(asciip, str_rem, "%02x", *(unsigned char *)(datap+i));
951 if (i < len && str_rem > 0)
952 *asciip = ' '; asciip++;
954 *asciip = '\0';
955 return ((int)asciip - (int)ascii);
956 break;
958 case REG_TYPE_DWORD:
959 if (*(int *)datap == 0)
960 return snprintf(ascii, ascii_max, "0");
961 else
962 return snprintf(ascii, ascii_max, "0x%x", *(int *)datap);
963 break;
965 case REG_TYPE_MULTISZ:
967 break;
969 default:
970 return 0;
971 break;
974 return len;
978 REG_KEY *nt_get_key_tree(REGF *regf, NK_HDR *nk_hdr, int size);
980 int nt_set_regf_input_file(REGF *regf, char *filename)
982 return ((regf->regfile_name = strdup(filename)) != NULL);
985 int nt_set_regf_output_file(REGF *regf, char *filename)
987 return ((regf->outfile_name = strdup(filename)) != NULL);
990 /* Create a regf structure and init it */
992 REGF *nt_create_regf(void)
994 REGF *tmp = (REGF *)malloc(sizeof(REGF));
995 if (!tmp) return tmp;
996 bzero(tmp, sizeof(REGF));
997 return tmp;
1000 /* Free all the bits and pieces ... Assumes regf was malloc'd */
1001 /* If you add stuff to REGF, add the relevant free bits here */
1002 int nt_free_regf(REGF *regf)
1004 if (!regf) return 0;
1006 if (regf->regfile_name) free(regf->regfile_name);
1007 if (regf->outfile_name) free(regf->outfile_name);
1009 /* Free the mmap'd area */
1011 if (regf->base) munmap(regf->base, regf->sbuf.st_size);
1012 regf->base = NULL;
1013 close(regf->fd); /* Ignore the error :-) */
1015 nt_delete_reg_key(regf->root); /* Free the tree */
1016 free(regf->sk_map);
1017 regf->sk_count = regf->sk_map_size = 0;
1019 free(regf);
1021 return 1;
1024 /* Get the header of the registry. Return a pointer to the structure
1025 * If the mmap'd area has not been allocated, then mmap the input file
1027 REGF_HDR *nt_get_regf_hdr(REGF *regf)
1029 if (!regf)
1030 return NULL; /* What about errors */
1032 if (!regf->regfile_name)
1033 return NULL; /* What about errors */
1035 if (!regf->base) { /* Try to mmap etc the file */
1037 if ((regf->fd = open(regf->regfile_name, O_RDONLY, 0000)) <0) {
1038 return NULL; /* What about errors? */
1041 if (fstat(regf->fd, &regf->sbuf) < 0) {
1042 return NULL;
1045 regf->base = mmap(0, regf->sbuf.st_size, PROT_READ, MAP_SHARED, regf->fd, 0);
1047 if ((int)regf->base == 1) {
1048 fprintf(stderr, "Could not mmap file: %s, %s\n", regf->regfile_name,
1049 strerror(errno));
1050 return NULL;
1055 * At this point, regf->base != NULL, and we should be able to read the
1056 * header
1059 assert(regf->base != NULL);
1061 return (REGF_HDR *)regf->base;
1065 * Validate a regf header
1066 * For now, do nothing, but we should check the checksum
1068 int valid_regf_hdr(REGF_HDR *regf_hdr)
1070 if (!regf_hdr) return 0;
1072 return 1;
1076 * Process an SK header ...
1077 * Every time we see a new one, add it to the map. Otherwise, just look it up.
1078 * We will do a simple linear search for the moment, since many KEYs have the
1079 * same security descriptor.
1080 * We allocate the map in increments of 10 entries.
1084 * Create a new entry in the map, and increase the size of the map if needed
1087 SK_MAP *alloc_sk_map_entry(REGF *regf, KEY_SEC_DESC *tmp, int sk_off)
1089 if (!regf->sk_map) { /* Allocate a block of 10 */
1090 regf->sk_map = (SK_MAP *)malloc(sizeof(SK_MAP) * 10);
1091 if (!regf->sk_map) {
1092 free(tmp);
1093 return NULL;
1095 regf->sk_map_size = 10;
1096 regf->sk_count = 1;
1097 (regf->sk_map)[0].sk_off = sk_off;
1098 (regf->sk_map)[0].key_sec_desc = tmp;
1100 else { /* Simply allocate a new slot, unless we have to expand the list */
1101 int index = regf->sk_count;
1102 if (regf->sk_count >= regf->sk_map_size) {
1103 regf->sk_map = (SK_MAP *)realloc(regf->sk_map,
1104 (regf->sk_map_size + 10)*sizeof(SK_MAP));
1105 if (!regf->sk_map) {
1106 free(tmp);
1107 return NULL;
1110 * Index already points at the first entry of the new block
1112 regf->sk_map_size += 10;
1114 (regf->sk_map)[index].sk_off = sk_off;
1115 (regf->sk_map)[index].key_sec_desc = tmp;
1116 regf->sk_count++;
1117 if (regf->sk_map[2].key_sec_desc == 0x19) { /* Take us over */
1118 fprintf(stderr, "%0x\n", regf->sk_map[7].key_sec_desc->sec_desc);
1121 return regf->sk_map;
1125 * Search for a KEY_SEC_DESC in the sk_map, but dont create one if not
1126 * found
1129 KEY_SEC_DESC *lookup_sec_key(SK_MAP *sk_map, int count, int sk_off)
1131 int i;
1133 if (!sk_map) return NULL;
1135 for (i = 0; i < count; i++) {
1137 if (sk_map[i].sk_off == sk_off)
1138 return sk_map[i].key_sec_desc;
1142 return NULL;
1147 * Allocate a KEY_SEC_DESC if we can't find one in the map
1150 KEY_SEC_DESC *lookup_create_sec_key(REGF *regf, SK_MAP *sk_map, int sk_off)
1152 KEY_SEC_DESC *tmp = lookup_sec_key(regf->sk_map, regf->sk_count, sk_off);
1154 if (tmp) {
1155 return tmp;
1157 else { /* Allocate a new one */
1158 tmp = (KEY_SEC_DESC *)malloc(sizeof(KEY_SEC_DESC));
1159 if (!tmp) {
1160 return NULL;
1162 tmp->state = SEC_DESC_RES;
1163 if (!alloc_sk_map_entry(regf, tmp, sk_off)) {
1164 return NULL;
1166 return tmp;
1171 * Allocate storage and duplicate a SID
1172 * We could allocate the SID to be only the size needed, but I am too lazy.
1174 DOM_SID *dup_sid(DOM_SID *sid)
1176 DOM_SID *tmp = (DOM_SID *)malloc(sizeof(DOM_SID));
1177 int i;
1179 if (!tmp) return NULL;
1180 tmp->ver = sid->ver;
1181 tmp->auths = sid->auths;
1182 for (i=0; i<6; i++) {
1183 tmp->auth[i] = sid->auth[i];
1185 for (i=0; i<tmp->auths&&i<MAXSUBAUTHS; i++) {
1186 tmp->sub_auths[i] = sid->sub_auths[i];
1188 return tmp;
1192 * Allocate space for an ACE and duplicate the registry encoded one passed in
1194 ACE *dup_ace(REG_ACE *ace)
1196 ACE *tmp = NULL;
1198 tmp = (ACE *)malloc(sizeof(ACE));
1200 if (!tmp) return NULL;
1202 tmp->type = CVAL(&ace->type);
1203 tmp->flags = CVAL(&ace->flags);
1204 tmp->perms = IVAL(&ace->perms);
1205 tmp->trustee = dup_sid(&ace->trustee);
1206 return tmp;
1210 * Allocate space for an ACL and duplicate the registry encoded one passed in
1212 ACL *dup_acl(REG_ACL *acl)
1214 ACL *tmp = NULL;
1215 REG_ACE* ace;
1216 int i, num_aces;
1218 num_aces = IVAL(&acl->num_aces);
1220 tmp = (ACL *)malloc(sizeof(ACL) + (num_aces - 1)*sizeof(ACE *));
1221 if (!tmp) return NULL;
1223 tmp->num_aces = num_aces;
1224 tmp->refcnt = 1;
1225 tmp->rev = SVAL(&acl->rev);
1226 ace = (REG_ACE *)&acl->aces;
1227 for (i=0; i<num_aces; i++) {
1228 tmp->aces[i] = dup_ace(ace);
1229 ace = (REG_ACE *)((char *)ace + SVAL(&ace->length));
1230 /* XXX: FIXME, should handle malloc errors */
1233 return tmp;
1236 SEC_DESC *process_sec_desc(REGF *regf, REG_SEC_DESC *sec_desc)
1238 SEC_DESC *tmp = NULL;
1240 tmp = (SEC_DESC *)malloc(sizeof(SEC_DESC));
1242 if (!tmp) {
1243 return NULL;
1246 tmp->rev = SVAL(&sec_desc->rev);
1247 tmp->type = SVAL(&sec_desc->type);
1248 tmp->owner = dup_sid((DOM_SID *)((char *)sec_desc + IVAL(&sec_desc->owner_off)));
1249 if (!tmp->owner) {
1250 free(tmp);
1251 return NULL;
1253 tmp->group = dup_sid((DOM_SID *)((char *)sec_desc + IVAL(&sec_desc->group_off)));
1254 if (!tmp->group) {
1255 free(tmp);
1256 return NULL;
1259 /* Now pick up the SACL and DACL */
1261 if (sec_desc->sacl_off)
1262 tmp->sacl = dup_acl((REG_ACL *)((char *)sec_desc + IVAL(&sec_desc->sacl_off)));
1263 else
1264 tmp->sacl = NULL;
1266 if (sec_desc->dacl_off)
1267 tmp->dacl = dup_acl((REG_ACL *)((char *)sec_desc + IVAL(&sec_desc->dacl_off)));
1268 else
1269 tmp->dacl = NULL;
1271 return tmp;
1274 KEY_SEC_DESC *process_sk(REGF *regf, SK_HDR *sk_hdr, int sk_off, int size)
1276 KEY_SEC_DESC *tmp = NULL;
1277 int sk_next_off, sk_prev_off, sk_size;
1278 REG_SEC_DESC *sec_desc;
1280 if (!sk_hdr) return NULL;
1282 if (SVAL(&sk_hdr->SK_ID) != REG_SK_ID) {
1283 fprintf(stderr, "Unrecognized SK Header ID: %08X, %s\n", (int)sk_hdr,
1284 regf->regfile_name);
1285 return NULL;
1288 if (-size < (sk_size = IVAL(&sk_hdr->rec_size))) {
1289 fprintf(stderr, "Incorrect SK record size: %d vs %d. %s\n",
1290 -size, sk_size, regf->regfile_name);
1291 return NULL;
1295 * Now, we need to look up the SK Record in the map, and return it
1296 * Since the map contains the SK_OFF mapped to KEY_SEC_DESC, we can
1297 * use that
1300 if (regf->sk_map &&
1301 ((tmp = lookup_sec_key(regf->sk_map, regf->sk_count, sk_off)) != NULL)
1302 && (tmp->state == SEC_DESC_OCU)) {
1303 tmp->ref_cnt++;
1304 return tmp;
1307 /* Here, we have an item in the map that has been reserved, or tmp==NULL. */
1309 assert(tmp == NULL || (tmp && tmp->state != SEC_DESC_NON));
1312 * Now, allocate a KEY_SEC_DESC, and parse the structure here, and add the
1313 * new KEY_SEC_DESC to the mapping structure, since the offset supplied is
1314 * the actual offset of structure. The same offset will be used by all
1315 * all future references to this structure
1316 * We chould put all this unpleasantness in a function.
1319 if (!tmp) {
1320 tmp = (KEY_SEC_DESC *)malloc(sizeof(KEY_SEC_DESC));
1321 if (!tmp) return NULL;
1322 bzero(tmp, sizeof(KEY_SEC_DESC));
1325 * Allocate an entry in the SK_MAP ...
1326 * We don't need to free tmp, because that is done for us if the
1327 * sm_map entry can't be expanded when we need more space in the map.
1330 if (!alloc_sk_map_entry(regf, tmp, sk_off)) {
1331 return NULL;
1335 tmp->ref_cnt++;
1336 tmp->state = SEC_DESC_OCU;
1339 * Now, process the actual sec desc and plug the values in
1342 sec_desc = (REG_SEC_DESC *)&sk_hdr->sec_desc[0];
1343 tmp->sec_desc = process_sec_desc(regf, sec_desc);
1346 * Now forward and back links. Here we allocate an entry in the sk_map
1347 * if it does not exist, and mark it reserved
1350 sk_prev_off = IVAL(&sk_hdr->prev_off);
1351 tmp->prev = lookup_create_sec_key(regf, regf->sk_map, sk_prev_off);
1352 assert(tmp->prev != NULL);
1353 sk_next_off = IVAL(&sk_hdr->next_off);
1354 tmp->next = lookup_create_sec_key(regf, regf->sk_map, sk_next_off);
1355 assert(tmp->next != NULL);
1357 return tmp;
1361 * Process a VK header and return a value
1363 VAL_KEY *process_vk(REGF *regf, VK_HDR *vk_hdr, int size)
1365 char val_name[1024];
1366 int nam_len, dat_len, flag, dat_type, dat_off, vk_id;
1367 char *val_type;
1368 VAL_KEY *tmp = NULL;
1370 if (!vk_hdr) return NULL;
1372 if ((vk_id = SVAL(&vk_hdr->VK_ID)) != REG_VK_ID) {
1373 fprintf(stderr, "Unrecognized VK header ID: %0X, block: %0X, %s\n",
1374 vk_id, (int)vk_hdr, regf->regfile_name);
1375 return NULL;
1378 nam_len = SVAL(&vk_hdr->nam_len);
1379 val_name[nam_len] = '\0';
1380 flag = SVAL(&vk_hdr->flag);
1381 dat_type = IVAL(&vk_hdr->dat_type);
1382 dat_len = IVAL(&vk_hdr->dat_len); /* If top bit, offset contains data */
1383 dat_off = IVAL(&vk_hdr->dat_off);
1385 tmp = (VAL_KEY *)malloc(sizeof(VAL_KEY));
1386 if (!tmp) {
1387 goto error;
1389 bzero(tmp, sizeof(VAL_KEY));
1390 tmp->has_name = flag;
1391 tmp->data_type = dat_type;
1393 if (flag & 0x01) {
1394 strncpy(val_name, vk_hdr->dat_name, nam_len);
1395 tmp->name = strdup(val_name);
1396 if (!tmp->name) {
1397 goto error;
1400 else
1401 strncpy(val_name, "<No Name>", 10);
1404 * Allocate space and copy the data as a BLOB
1407 if (dat_len) {
1409 char *dtmp = (char *)malloc(dat_len&0x7FFFFFFF);
1411 if (!dtmp) {
1412 goto error;
1415 tmp->data_blk = dtmp;
1417 if ((dat_len&0x80000000) == 0) { /* The data is pointed to by the offset */
1418 char *dat_ptr = LOCN(regf->base, dat_off);
1419 bcopy(dat_ptr, dtmp, dat_len);
1421 else { /* The data is in the offset */
1422 dat_len = dat_len & 0x7FFFFFFF;
1423 bcopy(&dat_off, dtmp, dat_len);
1426 tmp->data_len = dat_len;
1429 val_type = val_to_str(dat_type, reg_type_names);
1432 * We need to save the data area as well
1435 if (verbose) fprintf(stdout, " %s : %s : \n", val_name, val_type);
1437 return tmp;
1439 error:
1440 /* XXX: FIXME, free the partially allocated struct */
1441 return NULL;
1446 * Process a VL Header and return a list of values
1448 VAL_LIST *process_vl(REGF *regf, VL_TYPE vl, int count, int size)
1450 int i, vk_off;
1451 VK_HDR *vk_hdr;
1452 VAL_LIST *tmp = NULL;
1454 if (!vl) return NULL;
1456 if (-size < (count+1)*sizeof(int)){
1457 fprintf(stderr, "Error in VL header format. Size less than space required. %d\n", -size);
1458 return NULL;
1461 tmp = (VAL_LIST *)malloc(sizeof(VAL_LIST) + (count - 1) * sizeof(VAL_KEY *));
1462 if (!tmp) {
1463 goto error;
1466 for (i=0; i<count; i++) {
1467 vk_off = IVAL(&vl[i]);
1468 vk_hdr = (VK_HDR *)LOCN(regf->base, vk_off);
1469 tmp->vals[i] = process_vk(regf, vk_hdr, BLK_SIZE(vk_hdr));
1470 if (!tmp->vals[i]){
1471 goto error;
1475 tmp->val_count = count;
1477 return tmp;
1479 error:
1480 /* XXX: FIXME, free the partially allocated structure */
1481 return NULL;
1485 * Process an LF Header and return a list of sub-keys
1487 KEY_LIST *process_lf(REGF *regf, LF_HDR *lf_hdr, int size)
1489 int count, i, nk_off;
1490 unsigned int lf_id;
1491 KEY_LIST *tmp;
1493 if (!lf_hdr) return NULL;
1495 if ((lf_id = SVAL(&lf_hdr->LF_ID)) != REG_LF_ID) {
1496 fprintf(stderr, "Unrecognized LF Header format: %0X, Block: %0X, %s.\n",
1497 lf_id, (int)lf_hdr, regf->regfile_name);
1498 return NULL;
1501 assert(size < 0);
1503 count = SVAL(&lf_hdr->key_count);
1505 if (count <= 0) return NULL;
1507 /* Now, we should allocate a KEY_LIST struct and fill it in ... */
1509 tmp = (KEY_LIST *)malloc(sizeof(KEY_LIST) + (count - 1) * sizeof(REG_KEY *));
1510 if (!tmp) {
1511 goto error;
1514 tmp->key_count = count;
1516 for (i=0; i<count; i++) {
1517 NK_HDR *nk_hdr;
1519 nk_off = IVAL(&lf_hdr->hr[i].nk_off);
1520 nk_hdr = (NK_HDR *)LOCN(regf->base, nk_off);
1521 tmp->keys[i] = nt_get_key_tree(regf, nk_hdr, BLK_SIZE(nk_hdr));
1522 if (!tmp->keys[i]) {
1523 goto error;
1527 return tmp;
1529 error:
1530 /* XXX: FIXME, free the partially allocated structure */
1531 return NULL;
1535 * This routine is passed a NK_HDR pointer and retrieves the entire tree
1536 * from there down. It return a REG_KEY *.
1538 REG_KEY *nt_get_key_tree(REGF *regf, NK_HDR *nk_hdr, int size)
1540 REG_KEY *tmp = NULL;
1541 int name_len, clsname_len, lf_off, val_off, val_count, sk_off;
1542 unsigned int nk_id;
1543 LF_HDR *lf_hdr;
1544 VL_TYPE *vl;
1545 SK_HDR *sk_hdr;
1546 char key_name[1024], cls_name[1024];
1548 if (!nk_hdr) return NULL;
1550 if ((nk_id = SVAL(&nk_hdr->NK_ID)) != REG_NK_ID) {
1551 fprintf(stderr, "Unrecognized NK Header format: %08X, Block: %0X. %s\n",
1552 nk_id, (int)nk_hdr, regf->regfile_name);
1553 return NULL;
1556 assert(size < 0);
1558 name_len = SVAL(&nk_hdr->nam_len);
1559 clsname_len = SVAL(&nk_hdr->clsnam_len);
1562 * The value of -size should be ge
1563 * (sizeof(NK_HDR) - 1 + name_len)
1564 * The -1 accounts for the fact that we included the first byte of
1565 * the name in the structure. clsname_len is the length of the thing
1566 * pointed to by clsnam_off
1569 if (-size < (sizeof(NK_HDR) - 1 + name_len)) {
1570 fprintf(stderr, "Incorrect NK_HDR size: %d, %0X\n", -size, (int)nk_hdr);
1571 fprintf(stderr, "Sizeof NK_HDR: %d, name_len %d, clsname_len %d\n",
1572 sizeof(NK_HDR), name_len, clsname_len);
1573 /*return NULL;*/
1576 if (verbose) fprintf(stdout, "NK HDR: Name len: %d, class name len: %d\n",
1577 name_len, clsname_len);
1579 /* Fish out the key name and process the LF list */
1581 assert(name_len < sizeof(key_name));
1583 /* Allocate the key struct now */
1584 tmp = (REG_KEY *)malloc(sizeof(REG_KEY));
1585 if (!tmp) return tmp;
1586 bzero(tmp, sizeof(REG_KEY));
1588 tmp->type = (SVAL(&nk_hdr->type)==0x2C?REG_ROOT_KEY:REG_SUB_KEY);
1590 strncpy(key_name, nk_hdr->key_nam, name_len);
1591 key_name[name_len] = '\0';
1593 if (verbose) fprintf(stdout, "Key name: %s\n", key_name);
1595 tmp->name = strdup(key_name);
1596 if (!tmp->name) {
1597 goto error;
1601 * Fish out the class name, it is in UNICODE, while the key name is
1602 * ASCII :-)
1605 if (clsname_len) { /* Just print in Ascii for now */
1606 char *clsnamep;
1607 int clsnam_off;
1609 clsnam_off = IVAL(&nk_hdr->clsnam_off);
1610 clsnamep = LOCN(regf->base, clsnam_off);
1612 bzero(cls_name, clsname_len);
1613 uni_to_ascii(clsnamep, cls_name, sizeof(cls_name), clsname_len);
1616 * I am keeping class name as an ascii string for the moment.
1617 * That means it needs to be converted on output.
1618 * XXX: FIXME
1621 tmp->class_name = strdup(cls_name);
1622 if (!tmp->class_name) {
1623 goto error;
1626 if (verbose) fprintf(stdout, " Class Name: %s\n", cls_name);
1631 * If there are any values, process them here
1634 val_count = IVAL(&nk_hdr->val_cnt);
1636 if (val_count) {
1638 val_off = IVAL(&nk_hdr->val_off);
1639 vl = (VL_TYPE *)LOCN(regf->base, val_off);
1641 tmp->values = process_vl(regf, *vl, val_count, BLK_SIZE(vl));
1642 if (!tmp->values) {
1643 goto error;
1649 * Also handle the SK header ...
1652 sk_off = IVAL(&nk_hdr->sk_off);
1653 sk_hdr = (SK_HDR *)LOCN(regf->base, sk_off);
1655 if (sk_off != -1) {
1657 tmp->security = process_sk(regf, sk_hdr, sk_off, BLK_SIZE(sk_hdr));
1661 lf_off = IVAL(&nk_hdr->lf_off);
1664 * No more subkeys if lf_off == -1
1667 if (lf_off != -1) {
1669 lf_hdr = (LF_HDR *)LOCN(regf->base, lf_off);
1671 tmp->sub_keys = process_lf(regf, lf_hdr, BLK_SIZE(lf_hdr));
1672 if (!tmp->sub_keys){
1673 goto error;
1678 return tmp;
1680 error:
1681 if (tmp) nt_delete_reg_key(tmp);
1682 return NULL;
1685 int nt_load_registry(REGF *regf)
1687 REGF_HDR *regf_hdr;
1688 unsigned int regf_id, hbin_id;
1689 HBIN_HDR *hbin_hdr;
1690 NK_HDR *first_key;
1692 /* Get the header */
1694 if ((regf_hdr = nt_get_regf_hdr(regf)) == NULL) {
1695 return -1;
1698 /* Now process that header and start to read the rest in */
1700 if ((regf_id = IVAL(&regf_hdr->REGF_ID)) != REG_REGF_ID) {
1701 fprintf(stderr, "Unrecognized NT registry header id: %0X, %s\n",
1702 regf_id, regf->regfile_name);
1703 return -1;
1707 * Validate the header ...
1709 if (!valid_regf_hdr(regf_hdr)) {
1710 fprintf(stderr, "Registry file header does not validate: %s\n",
1711 regf->regfile_name);
1712 return -1;
1715 /* Update the last mod date, and then go get the first NK record and on */
1717 TTTONTTIME(regf, IVAL(&regf_hdr->tim1), IVAL(&regf_hdr->tim2));
1720 * The hbin hdr seems to be just uninteresting garbage. Check that
1721 * it is there, but that is all.
1724 hbin_hdr = (HBIN_HDR *)(regf->base + REGF_HDR_BLKSIZ);
1726 if ((hbin_id = IVAL(&hbin_hdr->HBIN_ID)) != REG_HBIN_ID) {
1727 fprintf(stderr, "Unrecognized registry hbin hdr ID: %0X, %s\n",
1728 hbin_id, regf->regfile_name);
1729 return -1;
1733 * Get a pointer to the first key from the hreg_hdr
1736 first_key = (NK_HDR *)LOCN(regf->base, IVAL(&regf_hdr->first_key));
1739 * Now, get the registry tree by processing that NK recursively
1742 regf->root = nt_get_key_tree(regf, first_key, BLK_SIZE(first_key));
1744 assert(regf->root != NULL);
1746 return 1;
1750 * Main code from here on ...
1754 * key print function here ...
1757 int print_key(char *path, char *name, char *class_name, int root,
1758 int terminal, int vals)
1761 if (terminal) fprintf(stdout, "%s\\%s\n", path, name);
1763 return 1;
1767 * Sec Desc print functions
1770 void print_sid(DOM_SID *sid)
1772 int i, comps = sid->auths;
1773 fprintf(stdout, "S-%u-%u", sid->ver, sid->auth[5]);
1775 for (i = 0; i < comps; i++) {
1777 fprintf(stdout, "-%u", sid->sub_auths[i]);
1780 fprintf(stdout, "\n");
1783 int print_sec(SEC_DESC *sec_desc)
1786 fprintf(stdout, " SECURITY\n");
1787 fprintf(stdout, " Owner: ");
1788 print_sid(sec_desc->owner);
1789 fprintf(stdout, " Group: ");
1790 print_sid(sec_desc->group);
1791 return 1;
1795 * Value print function here ...
1797 int print_val(char *path, char *val_name, int val_type, int data_len,
1798 void *data_blk, int terminal, int first, int last)
1800 char data_asc[1024];
1802 bzero(data_asc, sizeof(data_asc));
1803 if (!terminal && first)
1804 fprintf(stdout, "%s\n", path);
1805 data_to_ascii((unsigned char *)data_blk, data_len, val_type, data_asc,
1806 sizeof(data_asc) - 1);
1807 fprintf(stdout, " %s : %s : %s\n", (val_name?val_name:"<No Name>"),
1808 val_to_str(val_type, reg_type_names), data_asc);
1809 return 1;
1812 void usage(void)
1814 fprintf(stderr, "Usage: editreg [-v] [-k] <registryfile>\n");
1815 fprintf(stderr, "Version: 0.1\n\n");
1816 fprintf(stderr, "\n\t-v\t sets verbose mode");
1819 int main(int argc, char *argv[])
1821 REGF *regf;
1822 extern char *optarg;
1823 extern int optind;
1824 int opt;
1826 if (argc < 2) {
1827 usage();
1828 exit(1);
1832 * Now, process the arguments
1835 while ((opt = getopt(argc, argv, "vk")) != EOF) {
1836 switch (opt) {
1837 case 'v':
1838 verbose++;
1839 break;
1841 case 'k':
1842 break;
1844 default:
1845 usage();
1846 exit(1);
1847 break;
1851 if ((regf = nt_create_regf()) == NULL) {
1852 fprintf(stderr, "Could not create registry object: %s\n", strerror(errno));
1853 exit(2);
1856 if (!nt_set_regf_input_file(regf, argv[optind])) {
1857 fprintf(stderr, "Could not set name of registry file: %s, %s\n",
1858 argv[1], strerror(errno));
1859 exit(3);
1862 /* Now, open it, and bring it into memory :-) */
1864 if (nt_load_registry(regf) < 0) {
1865 fprintf(stderr, "Could not load registry: %s\n", argv[1]);
1866 exit(4);
1870 * At this point, we should have a registry in memory and should be able
1871 * to iterate over it.
1874 nt_key_iterator(regf, regf->root, 0, "", print_key, print_sec, print_val);
1875 return 0;