Improve the iterator wrt values ... Now prints keys and value names.
[Samba/gebeck_regimport.git] / source / utils / editreg.c
blob40fdd6eae977dda23b660039c06af598355abf1e
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 typedef struct key_sec_desc_s {
397 struct key_sec_desc_s *prev, *next;
398 int ref_cnt;
399 SEC_DESC *sec_desc;
400 } KEY_SEC_DESC;
404 * An API for accessing/creating/destroying items above
408 * Iterate over the keys, depth first, calling a function for each key
409 * and indicating if it is terminal or non-terminal and if it has values.
411 * In addition, for each value in the list, call a value list function
415 * There should eventually be one to deal with security keys as well
418 typedef int (*key_print_f)(char *path, char *key_name, char *class_name,
419 int root, int terminal, int values);
421 typedef int (*val_print_f)(char *path, char *val_name, int val_type,
422 int data_len, void *data_blk, int terminal,
423 int first, int last);
425 typedef struct regf_struct_s REGF;
427 int nt_key_iterator(REGF *regf, REG_KEY *key_tree, int bf, char *path,
428 key_print_f key_print, val_print_f val_print);
430 int nt_val_list_iterator(REGF *regf, VAL_LIST *val_list, int bf, char *path,
431 int terminal, val_print_f val_print)
433 int i;
435 if (!val_list) return 1;
437 if (!val_print) return 1;
439 for (i=0; i<val_list->val_count; i++) {
440 if (!val_print(path, val_list->vals[i]->name, val_list->vals[i]->data_type,
441 val_list->vals[i]->data_len, val_list->vals[i]->data_blk,
442 terminal,
443 (i == 0),
444 (i == val_list->val_count))) {
446 return 0;
451 return 1;
454 int nt_key_list_iterator(REGF *regf, KEY_LIST *key_list, int bf, char *path,
455 key_print_f key_print, val_print_f val_print)
457 int i;
459 if (!key_list) return 1;
461 for (i=0; i< key_list->key_count; i++) {
462 if (!nt_key_iterator(regf, key_list->keys[i], bf, path, key_print,
463 val_print)) {
464 return 0;
467 return 1;
470 int nt_key_iterator(REGF *regf, REG_KEY *key_tree, int bf, char *path,
471 key_print_f key_print, val_print_f val_print)
473 int path_len = strlen(path);
474 char *new_path;
476 if (!regf || !key_tree)
477 return -1;
479 /* List the key first, then the values, then the sub-keys */
481 if (key_print) {
483 if (!(*key_print)(path, key_tree->name,
484 key_tree->class_name,
485 (key_tree->type == REG_ROOT_KEY),
486 (key_tree->sub_keys == NULL),
487 (key_tree->values?(key_tree->values->val_count):0)))
488 return 0;
491 new_path = (char *)malloc(path_len + 1 + strlen(key_tree->name) + 1);
492 if (!new_path) return 0; /* Errors? */
493 new_path[0] = '\0';
494 strcat(new_path, path);
495 strcat(new_path, "\\");
496 strcat(new_path, key_tree->name);
499 * Now, iterate through the values in the val_list
502 if (key_tree->values &&
503 !nt_val_list_iterator(regf, key_tree->values, bf, new_path,
504 (key_tree->values?(key_tree->values->val_count):0),
505 val_print)) {
507 free(new_path);
508 return 0;
512 * Now, iterate through the keys in the key list
515 if (key_tree->sub_keys &&
516 !nt_key_list_iterator(regf, key_tree->sub_keys, bf, new_path, key_print,
517 val_print)) {
518 free(new_path);
519 return 0;
522 free(new_path);
523 return 1;
526 /* Make, delete keys */
528 int nt_delete_val_list(VAL_LIST *vl)
531 return 1;
534 int nt_delete_reg_key(REG_KEY *key)
537 return 1;
541 * Create/delete key lists and add delete keys to/from a list, count the keys
546 * Create/delete value lists, add/delete values, count them
551 * Create/delete security descriptors, add/delete SIDS, count SIDS, etc.
552 * We reference count the security descriptors. Any new reference increments
553 * the ref count. If we modify an SD, we copy the old one, dec the ref count
554 * and make the change. We also want to be able to check for equality so
555 * we can reduce the number of SDs in use.
560 * Load and unload a registry file.
562 * Load, loads it into memory as a tree, while unload sealizes/flattens it
566 * Get the starting record for NT Registry file
569 /* A map of sk offsets in the regf to KEY_SEC_DESCs for quick lookup etc */
570 typedef struct sk_map_s {
571 int sk_off;
572 KEY_SEC_DESC *key_sec_desc;
573 } SK_MAP;
576 * Where we keep all the regf stuff for one registry.
577 * This is the structure that we use to tie the in memory tree etc
578 * together. By keeping separate structs, we can operate on different
579 * registries at the same time.
580 * Currently, the SK_MAP is an array of mapping structure.
581 * Since we only need this on input and output, we fill in the structure
582 * as we go on input. On output, we know how many SK items we have, so
583 * we can allocate the structure as we need to.
584 * If you add stuff here that is dynamically allocated, add the
585 * appropriate free statements below.
588 #define REGF_REGTYPE_NONE 0
589 #define REGF_REGTYPE_NT 1
590 #define REGF_REGTYPE_W9X 2
592 #define TTTONTTIME(r, t1, t2) (r)->last_mod_time.low = (t1); \
593 (r)->last_mod_time.high = (t2);
595 #define REGF_HDR_BLKSIZ 0x1000
597 struct regf_struct_s {
598 int reg_type;
599 char *regfile_name, *outfile_name;
600 int fd;
601 struct stat sbuf;
602 char *base;
603 int modified;
604 NTTIME last_mod_time;
605 REG_KEY *root; /* Root of the tree for this file */
606 int sk_count, sk_map_size;
607 SK_MAP **sk_map;
611 * Structures for dealing with the on-disk format of the registry
614 #define IVAL(buf) ((unsigned int) \
615 (unsigned int)*((unsigned char *)(buf)+3)<<24| \
616 (unsigned int)*((unsigned char *)(buf)+2)<<16| \
617 (unsigned int)*((unsigned char *)(buf)+1)<<8| \
618 (unsigned int)*((unsigned char *)(buf)+0))
620 #define SVAL(buf) ((unsigned short) \
621 (unsigned short)*((unsigned char *)(buf)+1)<<8| \
622 (unsigned short)*((unsigned char *)(buf)+0))
624 #define OFF(f) ((f) + REGF_HDR_BLKSIZ + 4)
625 #define LOCN(base, f) ((base) + OFF(f))
628 * All of the structures below actually have a four-byte lenght before them
629 * which always seems to be negative. The following macro retrieves that
630 * size as an integer
633 #define BLK_SIZE(b) ((int)*(int *)(((int *)b)-1))
635 typedef unsigned int DWORD;
636 typedef unsigned short WORD;
638 #define REG_REGF_ID 0x66676572
640 typedef struct regf_block {
641 DWORD REGF_ID; /* regf */
642 DWORD uk1;
643 DWORD uk2;
644 DWORD tim1, tim2;
645 DWORD uk3; /* 1 */
646 DWORD uk4; /* 3 */
647 DWORD uk5; /* 0 */
648 DWORD uk6; /* 1 */
649 DWORD first_key; /* offset */
650 unsigned int dblk_size;
651 DWORD uk7[116]; /* 1 */
652 DWORD chksum;
653 } REGF_HDR;
655 typedef struct hbin_sub_struct {
656 DWORD dblocksize;
657 char data[1];
658 } HBIN_SUB_HDR;
660 #define REG_HBIN_ID 0x6E696268
662 typedef struct hbin_struct {
663 DWORD HBIN_ID; /* hbin */
664 DWORD next_off;
665 DWORD prev_off;
666 DWORD uk1;
667 DWORD uk2;
668 DWORD uk3;
669 DWORD uk4;
670 DWORD blk_size;
671 HBIN_SUB_HDR hbin_sub_hdr;
672 } HBIN_HDR;
674 #define REG_NK_ID 0x6B6E
676 typedef struct nk_struct {
677 WORD NK_ID;
678 WORD type;
679 DWORD t1, t2;
680 DWORD uk1;
681 DWORD own_off;
682 DWORD subk_num;
683 DWORD uk2;
684 DWORD lf_off;
685 DWORD uk3;
686 DWORD val_cnt;
687 DWORD val_off;
688 DWORD sk_off;
689 DWORD clsnam_off;
690 DWORD unk4[4];
691 DWORD unk5;
692 WORD nam_len;
693 WORD clsnam_len;
694 char key_nam[1]; /* Actual length determined by nam_len */
695 } NK_HDR;
697 #define REG_SK_ID 0x6B73
699 typedef struct sk_struct {
700 WORD SK_ID;
701 WORD uk1;
702 DWORD prev_off;
703 DWORD next_off;
704 DWORD ref_cnt;
705 DWORD rec_size;
706 char sec_desc[1];
707 } SK_HDR;
709 typedef struct hash_struct {
710 DWORD nk_off;
711 char hash[4];
712 } HASH_REC;
714 #define REG_LF_ID 0x666C
716 typedef struct lf_struct {
717 WORD LF_ID;
718 WORD key_count;
719 struct hash_struct hr[1]; /* Array of hash records, depending on key_count */
720 } LF_HDR;
722 typedef DWORD VL_TYPE[1]; /* Value list is an array of vk rec offsets */
724 #define REG_VK_ID 0x6B76
726 typedef struct vk_struct {
727 WORD VK_ID;
728 WORD nam_len;
729 DWORD dat_len; /* If top-bit set, offset contains the data */
730 DWORD dat_off;
731 DWORD dat_type;
732 WORD flag; /* =1, has name, else no name (=Default). */
733 WORD unk1;
734 char dat_name[1]; /* Name starts here ... */
735 } VK_HDR;
737 #define REG_TYPE_REGSZ 1
738 #define REG_TYPE_EXPANDSZ 2
739 #define REG_TYPE_BIN 3
740 #define REG_TYPE_DWORD 4
741 #define REG_TYPE_MULTISZ 7
743 typedef struct _val_str {
744 unsigned int val;
745 char * str;
746 } VAL_STR;
748 VAL_STR reg_type_names[] = {
749 { 1, "REG_SZ" },
750 { 2, "REG_EXPAND_SZ" },
751 { 3, "REG_BIN" },
752 { 4, "REG_DWORD" },
753 { 7, "REG_MULTI_SZ" },
754 { 0, NULL },
757 char *val_to_str(unsigned int val, VAL_STR *val_array)
759 int i = 0;
761 if (!val_array) return NULL;
763 while (val_array[i].val && val_array[i].str) {
765 if (val_array[i].val == val) return val_array[i].str;
766 i++;
770 return NULL;
774 REG_KEY *nt_get_key_tree(REGF *regf, NK_HDR *nk_hdr, int size);
776 int nt_set_regf_input_file(REGF *regf, char *filename)
778 return ((regf->regfile_name = strdup(filename)) != NULL);
781 int nt_set_regf_output_file(REGF *regf, char *filename)
783 return ((regf->outfile_name = strdup(filename)) != NULL);
786 /* Create a regf structure and init it */
788 REGF *nt_create_regf(void)
790 REGF *tmp = (REGF *)malloc(sizeof(REGF));
791 if (!tmp) return tmp;
792 bzero(tmp, sizeof(REGF));
793 return tmp;
796 /* Free all the bits and pieces ... Assumes regf was malloc'd */
797 /* If you add stuff to REGF, add the relevant free bits here */
798 int nt_free_regf(REGF *regf)
800 if (!regf) return 0;
802 if (regf->regfile_name) free(regf->regfile_name);
803 if (regf->outfile_name) free(regf->outfile_name);
805 /* Free the mmap'd area */
807 if (regf->base) munmap(regf->base, regf->sbuf.st_size);
808 regf->base = NULL;
809 close(regf->fd); /* Ignore the error :-) */
811 nt_delete_reg_key(regf->root); /* Free the tree */
812 free(regf->sk_map);
813 regf->sk_count = regf->sk_map_size = 0;
815 free(regf);
817 return 1;
821 * Convert from UniCode to Ascii ... Does not take into account other lang
822 * Restrict by ascii_max if > 0
824 int uni_to_ascii(unsigned char *uni, unsigned char *ascii, int ascii_max,
825 int uni_max)
827 int i = 0;
829 while (i < ascii_max && !(!uni[i*2] && !uni[i*2+1])) {
830 if (uni_max > 0 && (i*2) >= uni_max) break;
831 ascii[i] = uni[i*2];
832 i++;
836 ascii[i] = '\0';
838 return i;
841 /* Get the header of the registry. Return a pointer to the structure
842 * If the mmap'd area has not been allocated, then mmap the input file
844 REGF_HDR *nt_get_regf_hdr(REGF *regf)
846 if (!regf)
847 return NULL; /* What about errors */
849 if (!regf->regfile_name)
850 return NULL; /* What about errors */
852 if (!regf->base) { /* Try to mmap etc the file */
854 if ((regf->fd = open(regf->regfile_name, O_RDONLY, 0000)) <0) {
855 return NULL; /* What about errors? */
858 if (fstat(regf->fd, &regf->sbuf) < 0) {
859 return NULL;
862 regf->base = mmap(0, regf->sbuf.st_size, PROT_READ, MAP_SHARED, regf->fd, 0);
864 if ((int)regf->base == 1) {
865 fprintf(stderr, "Could not mmap file: %s, %s\n", regf->regfile_name,
866 strerror(errno));
867 return NULL;
872 * At this point, regf->base != NULL, and we should be able to read the
873 * header
876 assert(regf->base != NULL);
878 return (REGF_HDR *)regf->base;
882 * Validate a regf header
883 * For now, do nothing, but we should check the checksum
885 int valid_regf_hdr(REGF_HDR *regf_hdr)
887 if (!regf_hdr) return 0;
889 return 1;
893 * Process a VK header and return a value
895 VAL_KEY *process_vk(REGF *regf, VK_HDR *vk_hdr, int size)
897 char val_name[1024];
898 int nam_len, dat_len, flag, dat_type, dat_off, vk_id;
899 char *val_type;
900 VAL_KEY *tmp = NULL;
902 if (!vk_hdr) return NULL;
904 if ((vk_id = SVAL(&vk_hdr->VK_ID)) != REG_VK_ID) {
905 fprintf(stderr, "Unrecognized VK header ID: %0X, block: %0X, %s\n",
906 vk_id, (int)vk_hdr, regf->regfile_name);
907 return NULL;
910 nam_len = SVAL(&vk_hdr->nam_len);
911 val_name[nam_len] = '\0';
912 flag = SVAL(&vk_hdr->flag);
913 dat_type = IVAL(&vk_hdr->dat_type);
914 dat_len = IVAL(&vk_hdr->dat_len); /* If top bit, offset contains data */
915 dat_off = IVAL(&vk_hdr->dat_off);
917 tmp = (VAL_KEY *)malloc(sizeof(VAL_KEY));
918 if (!tmp) {
919 goto error;
921 bzero(tmp, sizeof(VAL_KEY));
922 tmp->has_name = flag;
923 tmp->data_type = dat_type;
925 if (flag & 0x01) {
926 strncpy(val_name, vk_hdr->dat_name, nam_len);
927 tmp->name = strdup(val_name);
928 if (!tmp->name) {
929 goto error;
932 else
933 strncpy(val_name, "<No Name>", 10);
936 * Allocate space and copy the data as a BLOB
939 if (dat_len) {
941 char *dtmp = (char *)malloc(dat_len&0x7FFFFFFF);
943 if (!dtmp) {
944 goto error;
947 tmp->data_blk = dtmp;
949 if ((dat_len&0x80000000) == 0) { /* The data is pointed to by the offset */
950 char *dat_ptr = LOCN(regf->base, dat_off);
951 bcopy(dat_ptr, dtmp, dat_len);
953 else { /* The data is in the offset */
954 dat_len = dat_len & 0x7FFFFFFF;
955 bcopy(&dat_off, dtmp, dat_len);
960 val_type = val_to_str(dat_type, reg_type_names);
963 * We need to save the data area as well
966 if (verbose) fprintf(stdout, " %s : %s : \n", val_name, val_type);
968 return tmp;
970 error:
971 /* XXX: FIXME, free the partially allocated struct */
972 return NULL;
977 * Process a VL Header and return a list of values
979 VAL_LIST *process_vl(REGF *regf, VL_TYPE vl, int count, int size)
981 int i, vk_off;
982 VK_HDR *vk_hdr;
983 VAL_LIST *tmp = NULL;
985 if (-size < (count+1)*sizeof(int)){
986 fprintf(stderr, "Error in VL header format. Size less than space required. %d\n", -size);
987 return NULL;
990 tmp = (VAL_LIST *)malloc(sizeof(VAL_LIST) + (count - 1) * sizeof(VAL_KEY *));
991 if (!tmp) {
992 goto error;
995 for (i=0; i<count; i++) {
996 vk_off = IVAL(&vl[i]);
997 vk_hdr = (VK_HDR *)LOCN(regf->base, vk_off);
998 tmp->vals[i] = process_vk(regf, vk_hdr, BLK_SIZE(vk_hdr));
999 if (!tmp->vals[i]){
1000 goto error;
1004 tmp->val_count = count;
1006 return tmp;
1008 error:
1009 /* XXX: FIXME, free the partially allocated structure */
1010 return NULL;
1014 * Process an LF Header and return a list of sub-keys
1016 KEY_LIST *process_lf(REGF *regf, LF_HDR *lf_hdr, int size)
1018 int count, i, nk_off;
1019 unsigned int lf_id;
1020 KEY_LIST *tmp;
1022 if (!lf_hdr) return NULL;
1024 if ((lf_id = SVAL(&lf_hdr->LF_ID)) != REG_LF_ID) {
1025 fprintf(stderr, "Unrecognized LF Header format: %0X, Block: %0X, %s.\n",
1026 lf_id, (int)lf_hdr, regf->regfile_name);
1027 return NULL;
1030 assert(size < 0);
1032 count = SVAL(&lf_hdr->key_count);
1034 if (count <= 0) return NULL;
1036 /* Now, we should allocate a KEY_LIST struct and fill it in ... */
1038 tmp = (KEY_LIST *)malloc(sizeof(KEY_LIST) + (count - 1) * sizeof(REG_KEY *));
1039 if (!tmp) {
1040 goto error;
1043 tmp->key_count = count;
1045 for (i=0; i<count; i++) {
1046 NK_HDR *nk_hdr;
1048 nk_off = IVAL(&lf_hdr->hr[i].nk_off);
1049 nk_hdr = (NK_HDR *)LOCN(regf->base, nk_off);
1050 tmp->keys[i] = nt_get_key_tree(regf, nk_hdr, BLK_SIZE(nk_hdr));
1051 if (!tmp->keys[i]) {
1052 goto error;
1056 return tmp;
1058 error:
1059 /* XXX: FIXME, free the partially allocated structure */
1060 return NULL;
1064 * This routine is passed a NK_HDR pointer and retrieves the entire tree
1065 * from there down. It return a REG_KEY *.
1067 REG_KEY *nt_get_key_tree(REGF *regf, NK_HDR *nk_hdr, int size)
1069 REG_KEY *tmp = NULL;
1070 int name_len, clsname_len, lf_off, val_off, val_count, sk_off;
1071 unsigned int nk_id;
1072 LF_HDR *lf_hdr;
1073 VL_TYPE *vl;
1074 SK_HDR *sk_hdr;
1075 char key_name[1024], cls_name[1024];
1077 if (!nk_hdr) return NULL;
1079 if ((nk_id = SVAL(&nk_hdr->NK_ID)) != REG_NK_ID) {
1080 fprintf(stderr, "Unrecognized NK Header format: %08X, Block: %0X. %s\n",
1081 nk_id, (int)nk_hdr, regf->regfile_name);
1082 return NULL;
1085 assert(size < 0);
1087 name_len = SVAL(&nk_hdr->nam_len);
1088 clsname_len = SVAL(&nk_hdr->clsnam_len);
1091 * The value of -size should be ge
1092 * (sizeof(NK_HDR) - 1 + name_len)
1093 * The -1 accounts for the fact that we included the first byte of
1094 * the name in the structure. clsname_len is the length of the thing
1095 * pointed to by clsnam_off
1098 if (-size < (sizeof(NK_HDR) - 1 + name_len)) {
1099 fprintf(stderr, "Incorrect NK_HDR size: %d, %0X\n", -size, (int)nk_hdr);
1100 fprintf(stderr, "Sizeof NK_HDR: %d, name_len %d, clsname_len %d\n",
1101 sizeof(NK_HDR), name_len, clsname_len);
1102 /*return NULL;*/
1105 if (verbose) fprintf(stdout, "NK HDR: Name len: %d, class name len: %d\n",
1106 name_len, clsname_len);
1108 /* Fish out the key name and process the LF list */
1110 assert(name_len < sizeof(key_name));
1112 /* Allocate the key struct now */
1113 tmp = (REG_KEY *)malloc(sizeof(REG_KEY));
1114 if (!tmp) return tmp;
1115 bzero(tmp, sizeof(REG_KEY));
1117 tmp->type = (SVAL(&nk_hdr->type)==0x2C?REG_ROOT_KEY:REG_SUB_KEY);
1119 strncpy(key_name, nk_hdr->key_nam, name_len);
1120 key_name[name_len] = '\0';
1122 if (verbose) fprintf(stdout, "Key name: %s\n", key_name);
1124 tmp->name = strdup(key_name);
1125 if (!tmp->name) {
1126 goto error;
1130 * Fish out the class name, it is in UNICODE, while the key name is
1131 * ASCII :-)
1134 if (clsname_len) { /* Just print in Ascii for now */
1135 char *clsnamep;
1136 int clsnam_off;
1138 clsnam_off = IVAL(&nk_hdr->clsnam_off);
1139 clsnamep = LOCN(regf->base, clsnam_off);
1141 bzero(cls_name, clsname_len);
1142 uni_to_ascii(clsnamep, cls_name, sizeof(cls_name), clsname_len);
1145 * I am keeping class name as an ascii string for the moment.
1146 * That means it needs to be converted on output.
1147 * XXX: FIXME
1150 tmp->class_name = strdup(cls_name);
1151 if (!tmp->class_name) {
1152 goto error;
1155 if (verbose) fprintf(stdout, " Class Name: %s\n", cls_name);
1160 * If there are any values, process them here
1163 val_count = IVAL(&nk_hdr->val_cnt);
1165 if (val_count) {
1167 val_off = IVAL(&nk_hdr->val_off);
1168 vl = (VL_TYPE *)LOCN(regf->base, val_off);
1170 tmp->values = process_vl(regf, *vl, val_count, BLK_SIZE(vl));
1171 if (!tmp->values) {
1172 goto error;
1178 * Also handle the SK header ...
1181 sk_off = IVAL(&nk_hdr->sk_off);
1182 sk_hdr = (SK_HDR *)LOCN(regf->base, sk_off);
1184 if (sk_off != -1) {
1186 /* To be coded */
1190 lf_off = IVAL(&nk_hdr->lf_off);
1193 * No more subkeys if lf_off == -1
1196 if (lf_off != -1) {
1198 lf_hdr = (LF_HDR *)LOCN(regf->base, lf_off);
1200 tmp->sub_keys = process_lf(regf, lf_hdr, BLK_SIZE(lf_hdr));
1201 if (!tmp->sub_keys){
1202 goto error;
1207 return tmp;
1209 error:
1210 if (tmp) nt_delete_reg_key(tmp);
1211 return NULL;
1214 int nt_load_registry(REGF *regf)
1216 REGF_HDR *regf_hdr;
1217 unsigned int regf_id, hbin_id;
1218 HBIN_HDR *hbin_hdr;
1219 NK_HDR *first_key;
1221 /* Get the header */
1223 if ((regf_hdr = nt_get_regf_hdr(regf)) == NULL) {
1224 return -1;
1227 /* Now process that header and start to read the rest in */
1229 if ((regf_id = IVAL(&regf_hdr->REGF_ID)) != REG_REGF_ID) {
1230 fprintf(stderr, "Unrecognized NT registry header id: %0X, %s\n",
1231 regf_id, regf->regfile_name);
1232 return -1;
1236 * Validate the header ...
1238 if (!valid_regf_hdr(regf_hdr)) {
1239 fprintf(stderr, "Registry file header does not validate: %s\n",
1240 regf->regfile_name);
1241 return -1;
1244 /* Update the last mod date, and then go get the first NK record and on */
1246 TTTONTTIME(regf, IVAL(&regf_hdr->tim1), IVAL(&regf_hdr->tim2));
1249 * The hbin hdr seems to be just uninteresting garbage. Check that
1250 * it is there, but that is all.
1253 hbin_hdr = (HBIN_HDR *)(regf->base + REGF_HDR_BLKSIZ);
1255 if ((hbin_id = IVAL(&hbin_hdr->HBIN_ID)) != REG_HBIN_ID) {
1256 fprintf(stderr, "Unrecognized registry hbin hdr ID: %0X, %s\n",
1257 hbin_id, regf->regfile_name);
1258 return -1;
1262 * Get a pointer to the first key from the hreg_hdr
1265 first_key = (NK_HDR *)LOCN(regf->base, IVAL(&regf_hdr->first_key));
1268 * Now, get the registry tree by processing that NK recursively
1271 regf->root = nt_get_key_tree(regf, first_key, BLK_SIZE(first_key));
1273 assert(regf->root != NULL);
1275 return 1;
1279 * Main code from here on ...
1283 * key print function here ...
1286 int print_key(char *path, char *name, char *class_name, int root,
1287 int terminal, int vals)
1290 if (terminal) fprintf(stdout, "%s\\%s\n", path, name);
1292 return 1;
1296 * Value print function here ...
1298 int print_val(char *path, char *val_name, int val_type, int data_len,
1299 void *data_blk, int terminal, int first, int last)
1301 if (terminal && first)
1302 fprintf(stdout, "%s\n", path);
1303 fprintf(stdout, " %s : %s : \n", (val_name?val_name:"<No Name>"),
1304 val_to_str(val_type, reg_type_names));
1305 return 1;
1308 void usage(void)
1310 fprintf(stderr, "Usage: editreg [-v] [-k] <registryfile>\n");
1311 fprintf(stderr, "Version: 0.1\n\n");
1312 fprintf(stderr, "\n\t-v\t sets verbose mode");
1315 int main(int argc, char *argv[])
1317 REGF *regf;
1318 extern char *optarg;
1319 extern int optind;
1320 int opt;
1322 if (argc < 2) {
1323 usage();
1324 exit(1);
1328 * Now, process the arguments
1331 while ((opt = getopt(argc, argv, "vk")) != EOF) {
1332 switch (opt) {
1333 case 'v':
1334 verbose++;
1335 break;
1337 case 'k':
1338 break;
1340 default:
1341 usage();
1342 exit(1);
1343 break;
1347 if ((regf = nt_create_regf()) == NULL) {
1348 fprintf(stderr, "Could not create registry object: %s\n", strerror(errno));
1349 exit(2);
1352 if (!nt_set_regf_input_file(regf, argv[optind])) {
1353 fprintf(stderr, "Could not set name of registry file: %s, %s\n",
1354 argv[1], strerror(errno));
1355 exit(3);
1358 /* Now, open it, and bring it into memory :-) */
1360 if (nt_load_registry(regf) < 0) {
1361 fprintf(stderr, "Could not load registry: %s\n", argv[1]);
1362 exit(4);
1366 * At this point, we should have a registry in memory and should be able
1367 * to iterate over it.
1370 nt_key_iterator(regf, regf->root, 0, "", print_key, print_val);
1371 return 0;