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
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!
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
54 0x00000024 D-Word Offset of 1st key record
55 0x00000028 D-Word Size of the data-blocks (Filesize-4kb)
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...
66 I don't know what "hbin" stands for, but this block is always a multiple
69 Inside these hbin-blocks the different records are placed. The memory-
70 management looks like a C-compiler heap management to me...
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
87 0x0000 D-Word Data-block size //this size must be a
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!
97 The data is stored as one record per block. Block size is a multiple
98 of 4 and the last block reaches the next hbin-block, leaving no room.
100 (That also seems incorrect, in that the block size if a multiple of 8.
101 That is, the block, including the 4 byte header, is always a multiple of
102 8 bytes. Richard Sharpe.)
104 Records in the hbin-blocks
105 ==========================
109 The nk-record can be treated as a kombination of tree-record and
110 key-record of the win 95 registry.
114 The lf-record is the counterpart to the RGKN-record (the
119 The vk-record consists information to a single value.
123 sk (? Security Key ?) is the ACL of the registry.
127 The value-lists contain information about which values are inside a
128 sub-key and don't have a header.
132 The datas of the registry are (like the value-list) stored without a
135 All offset-values are relative to the first hbin-block and point to the
136 block-size field of the record-entry. to get the file offset, you have to add
137 the header size (4kb) and the size field (4 bytes)...
142 0x0000 Word ID: ASCII-"nk" = 0x6B6E
143 0x0002 Word for the root-key: 0x2C, otherwise 0x20 //key symbolic links 0x10. Nigel
144 0x0004 Q-Word write-date/time in windows nt notation
145 0x0010 D-Word Offset of Owner/Parent key
146 0x0014 D-Word number of sub-Keys
147 0x001C D-Word Offset of the sub-key lf-Records
148 0x0024 D-Word number of values
149 0x0028 D-Word Offset of the Value-List
150 0x002C D-Word Offset of the sk-Record
152 0x0030 D-Word Offset of the Class-Name //see NK structure for the use of these fields. Nigel
153 0x0044 D-Word Unused (data-trash) //some kind of run time index. Does not appear to be important. Nigel
154 0x0048 Word name-length
155 0x004A Word class-name length
161 0x0000 D-Word Offset 1st Value
162 0x0004 D-Word Offset 2nd Value
163 0x???? D-Word Offset nth Value
165 To determine the number of values, you have to look at the owner-nk-record!
170 0x0000 Word ID: ASCII-"vk" = 0x6B76
171 0x0002 Word name length
172 0x0004 D-Word length of the data //if top bit is set when offset contains data. Nigel
173 0x0008 D-Word Offset of Data
174 0x000C D-Word Type of value
176 0x0012 Word Unused (data-trash)
179 If bit 0 of the flag-word is set, a name is present, otherwise the value has no name (=default)
181 If the data-size is lower 5, the data-offset value is used to store the data itself!
186 0x0001 RegSZ: character string (in UNICODE!)
187 0x0002 ExpandSZ: string with "%var%" expanding (UNICODE!)
188 0x0003 RegBin: raw-binary value
189 0x0004 RegDWord: Dword
190 0x0007 RegMultiSZ: multiple strings, seperated with 0
196 0x0000 Word ID: ASCII-"lf" = 0x666C
197 0x0002 Word number of keys
198 0x0004 ???? Hash-Records
203 0x0000 D-Word Offset of corresponding "nk"-Record
204 0x0004 D-Word ASCII: the first 4 characters of the key-name, padded with 0's. Case sensitiv!
206 Keep in mind, that the value at 0x0004 is used for checking the data-consistency! If you change the
207 key-name you have to change the hash-value too!
209 //These hashrecords must be sorted low to high within the lf record. Nigel.
213 (due to the complexity of the SAM-info, not clear jet)
214 (This is just a self-relative security descriptor in the data. R Sharpe.)
218 0x0000 Word ID: ASCII-"sk" = 0x6B73
220 0x0004 D-Word Offset of previous "sk"-Record
221 0x0008 D-Word Offset of next "sk"-Record
222 0x000C D-Word usage-counter
223 0x0010 D-Word Size of "sk"-record in bytes
225 relative security desciptor. Nigel
226 ???? ???? Security and auditing settings...
229 The usage counter counts the number of references to this
230 "sk"-record. You can use one "sk"-record for the entire registry!
232 Windows nt date/time format
233 ===========================
234 The time-format is a 64-bit integer which is incremented every
235 0,0000001 seconds by 1 (I don't know how accurate it realy is!)
236 It starts with 0 at the 1st of january 1601 0:00! All values are
237 stored in GMT time! The time-zone is important to get the real
240 Common values for win95 and win-nt
241 ==================================
242 Offset values marking an "end of list", are either 0 or -1 (0xFFFFFFFF).
243 If a value has no name (length=0, flag(bit 0)=0), it is treated as the
245 If a value has no data (length=0), it is displayed as empty.
247 simplyfied win-3.?? registry:
248 =============================
251 | next rec. |---+ +----->+------------+
252 | first sub | | | | Usage cnt. |
253 | name | | +-->+------------+ | | length |
254 | value | | | | next rec. | | | text |------->+-------+
255 +-----------+ | | | name rec. |--+ +------------+ | xxxxx |
256 +------------+ | | value rec. |-------->+------------+ +-------+
257 v | +------------+ | Usage cnt. |
258 +-----------+ | | length |
259 | next rec. | | | text |------->+-------+
260 | first sub |------+ +------------+ | xxxxx |
265 Greatly simplyfied structure of the nt-registry:
266 ================================================
268 +---------------------------------------------------------------+
271 +---------+ +---------->+-----------+ +----->+---------+ |
272 | "nk" | | | lf-rec. | | | nk-rec. | |
273 | ID | | | # of keys | | | parent |---+
274 | Date | | | 1st key |--+ | .... |
275 | parent | | +-----------+ +---------+
277 | values |--------------------->+----------+
278 | SK-rec. |---------------+ | 1. value |--> +----------+
279 | class |--+ | +----------+ | vk-rec. |
280 +---------+ | | | .... |
281 v | | data |--> +-------+
282 +------------+ | +----------+ | xxxxx |
283 | Class name | | +-------+
286 +---------+ +---------+
287 +----->| next sk |--->| Next sk |--+
288 | +---| prev sk |<---| prev sk | |
289 | | | .... | | ... | |
290 | | +---------+ +---------+ |
293 | +--------------------+ |
294 +----------------------------------+
296 ---------------------------------------------------------------------------
298 Hope this helps.... (Although it was "fun" for me to uncover this things,
299 it took me several sleepless nights ;)
303 *************************************************************************/
309 #include <sys/types.h>
310 #include <sys/stat.h>
312 #include <sys/mman.h>
318 #define REG_KEY_LIST_SIZE 10
321 * Structures for dealing with the on-disk format of the registry
324 #define IVAL(buf) ((unsigned int) \
325 (unsigned int)*((unsigned char *)(buf)+3)<<24| \
326 (unsigned int)*((unsigned char *)(buf)+2)<<16| \
327 (unsigned int)*((unsigned char *)(buf)+1)<<8| \
328 (unsigned int)*((unsigned char *)(buf)+0))
330 #define SVAL(buf) ((unsigned short) \
331 (unsigned short)*((unsigned char *)(buf)+1)<<8| \
332 (unsigned short)*((unsigned char *)(buf)+0))
334 #define CVAL(buf) ((unsigned char)*((unsigned char *)(buf)))
336 #define SIVAL(buf, val) \
337 ((unsigned char)buf[0]=(unsigned char)((val)&0xFF),\
338 (unsigned char)buf[1]=(unsigned char)(((val)>>8)&0xFF),\
339 (unsigned char)buf[2]=(unsigned char)(((val)>>16)&0xFF),\
340 (unsigned char)buf[3]=(unsigned char)((val)>>24))
342 #define SSVAL(buf, val) \
343 ((unsigned char)buf[0]=(unsigned char)((val)&0xFF),\
344 (unsigned char)buf[1]=(unsigned char)(((val)>>8)&0xFF))
346 static int verbose
= 0;
347 static int print_security
= 0;
348 static int full_print
= 0;
349 static char *def_owner_sid_str
= NULL
;
352 * These definitions are for the in-memory registry structure.
353 * It is a tree structure that mimics what you see with tools like regedit
357 * DateTime struct for Windows
360 typedef struct date_time_s
{
361 unsigned int low
, high
;
365 * Definition of a Key. It has a name, classname, date/time last modified,
366 * sub-keys, values, and a security descriptor
369 #define REG_ROOT_KEY 1
370 #define REG_SUB_KEY 2
371 #define REG_SYM_LINK 3
373 typedef struct key_sec_desc_s KEY_SEC_DESC
;
375 typedef struct reg_key_s
{
376 char *name
; /* Name of the key */
378 int type
; /* One of REG_ROOT_KEY or REG_SUB_KEY */
379 NTTIME last_mod
; /* Time last modified */
380 struct reg_key_s
*owner
;
381 struct key_list_s
*sub_keys
;
382 struct val_list_s
*values
;
383 KEY_SEC_DESC
*security
;
384 unsigned int offset
; /* Offset of the record in the file */
388 * The KEY_LIST struct lists sub-keys.
391 typedef struct key_list_s
{
397 typedef struct val_key_s
{
402 void *data_blk
; /* Might want a separate block */
405 typedef struct val_list_s
{
412 #define MAXSUBAUTHS 15
415 typedef struct dom_sid_s
{
416 unsigned char ver
, auths
;
417 unsigned char auth
[6];
418 unsigned int sub_auths
[MAXSUBAUTHS
];
421 typedef struct ace_struct_s
{
422 unsigned char type
, flags
;
423 unsigned int perms
; /* Perhaps a better def is in order */
427 typedef struct acl_struct_s
{
428 unsigned short rev
, refcnt
;
429 unsigned short num_aces
;
433 typedef struct sec_desc_s
{
434 unsigned int rev
, type
;
435 DOM_SID
*owner
, *group
;
439 #define SEC_DESC_NON 0
440 #define SEC_DESC_RES 1
441 #define SEC_DESC_OCU 2
442 #define SEC_DESC_NBK 3
443 typedef struct sk_struct SK_HDR
;
444 struct key_sec_desc_s
{
445 struct key_sec_desc_s
*prev
, *next
;
449 SK_HDR
*sk_hdr
; /* This means we must keep the registry in memory */
454 * All of the structures below actually have a four-byte length before them
455 * which always seems to be negative. The following macro retrieves that
459 #define BLK_SIZE(b) ((int)*(int *)(((int *)b)-1))
461 typedef unsigned int DWORD
;
462 typedef unsigned short WORD
;
464 #define REG_REGF_ID 0x66676572
466 typedef struct regf_block
{
467 DWORD REGF_ID
; /* regf */
475 DWORD first_key
; /* offset */
476 unsigned int dblk_size
;
477 DWORD uk7
[116]; /* 1 */
481 typedef struct hbin_sub_struct
{
486 #define REG_HBIN_ID 0x6E696268
488 typedef struct hbin_struct
{
489 DWORD HBIN_ID
; /* hbin */
490 DWORD off_from_first
;
497 HBIN_SUB_HDR hbin_sub_hdr
;
500 #define REG_NK_ID 0x6B6E
502 typedef struct nk_struct
{
520 char key_nam
[1]; /* Actual length determined by nam_len */
523 #define REG_SK_ID 0x6B73
535 typedef struct ace_struct
{
538 unsigned short length
;
543 typedef struct acl_struct
{
547 REG_ACE
*aces
; /* One or more ACEs */
550 typedef struct sec_desc_rec
{
559 typedef struct hash_struct
{
564 #define REG_LF_ID 0x666C
566 typedef struct lf_struct
{
569 struct hash_struct hr
[1]; /* Array of hash records, depending on key_count */
572 typedef DWORD VL_TYPE
[1]; /* Value list is an array of vk rec offsets */
574 #define REG_VK_ID 0x6B76
576 typedef struct vk_struct
{
579 DWORD dat_len
; /* If top-bit set, offset contains the data */
582 WORD flag
; /* =1, has name, else no name (=Default). */
584 char dat_name
[1]; /* Name starts here ... */
587 #define REG_TYPE_DELETE -1
588 #define REG_TYPE_NONE 0
589 #define REG_TYPE_REGSZ 1
590 #define REG_TYPE_EXPANDSZ 2
591 #define REG_TYPE_BIN 3
592 #define REG_TYPE_DWORD 4
593 #define REG_TYPE_MULTISZ 7
595 typedef struct _val_str
{
600 /* A map of sk offsets in the regf to KEY_SEC_DESCs for quick lookup etc */
601 typedef struct sk_map_s
{
603 KEY_SEC_DESC
*key_sec_desc
;
607 * This structure keeps track of the output format of the registry
609 #define REG_OUTBLK_HDR 1
610 #define REG_OUTBLK_HBIN 2
612 typedef struct hbin_blk_s
{
614 struct hbin_blk_s
*next
;
615 char *data
; /* The data block */
616 unsigned int file_offset
; /* Offset in file */
617 unsigned int free_space
; /* Amount of free space in block */
618 unsigned int fsp_off
; /* Start of free space in block */
619 int complete
, stored
;
623 * This structure keeps all the registry stuff in one place
625 typedef struct regf_struct_s
{
627 char *regfile_name
, *outfile_name
;
632 NTTIME last_mod_time
;
633 REG_KEY
*root
; /* Root of the tree for this file */
634 int sk_count
, sk_map_size
;
637 SEC_DESC
*def_sec_desc
;
639 * These next pointers point to the blocks used to contain the
640 * keys when we are preparing to write them to a file
642 HBIN_BLK
*blk_head
, *blk_tail
, *free_space
;
646 * An API for accessing/creating/destroying items above
650 * Iterate over the keys, depth first, calling a function for each key
651 * and indicating if it is terminal or non-terminal and if it has values.
653 * In addition, for each value in the list, call a value list function
656 typedef int (*key_print_f
)(const char *path
, char *key_name
, char *class_name
,
657 int root
, int terminal
, int values
);
659 typedef int (*val_print_f
)(const char *path
, char *val_name
, int val_type
,
660 int data_len
, void *data_blk
, int terminal
,
661 int first
, int last
);
663 typedef int (*sec_print_f
)(SEC_DESC
*sec_desc
);
665 int nt_key_iterator(REGF
*regf
, REG_KEY
*key_tree
, int bf
, const char *path
,
666 key_print_f key_print
, sec_print_f sec_print
,
667 val_print_f val_print
);
669 int nt_val_list_iterator(REGF
*regf
, VAL_LIST
*val_list
, int bf
, char *path
,
670 int terminal
, val_print_f val_print
)
674 if (!val_list
) return 1;
676 if (!val_print
) return 1;
678 for (i
=0; i
<val_list
->val_count
; i
++) {
679 if (!val_print(path
, val_list
->vals
[i
]->name
, val_list
->vals
[i
]->data_type
,
680 val_list
->vals
[i
]->data_len
, val_list
->vals
[i
]->data_blk
,
683 (i
== val_list
->val_count
))) {
693 int nt_key_list_iterator(REGF
*regf
, KEY_LIST
*key_list
, int bf
,
695 key_print_f key_print
, sec_print_f sec_print
,
696 val_print_f val_print
)
700 if (!key_list
) return 1;
702 for (i
=0; i
< key_list
->key_count
; i
++) {
703 if (!nt_key_iterator(regf
, key_list
->keys
[i
], bf
, path
, key_print
,
704 sec_print
, val_print
)) {
711 int nt_key_iterator(REGF
*regf
, REG_KEY
*key_tree
, int bf
, const char *path
,
712 key_print_f key_print
, sec_print_f sec_print
,
713 val_print_f val_print
)
715 int path_len
= strlen(path
);
718 if (!regf
|| !key_tree
)
721 /* List the key first, then the values, then the sub-keys */
725 if (!(*key_print
)(path
, key_tree
->name
,
726 key_tree
->class_name
,
727 (key_tree
->type
== REG_ROOT_KEY
),
728 (key_tree
->sub_keys
== NULL
),
729 (key_tree
->values
?(key_tree
->values
->val_count
):0)))
734 * If we have a security print routine, call it
735 * If the security print routine returns false, stop.
738 if (key_tree
->security
&& !(*sec_print
)(key_tree
->security
->sec_desc
))
742 new_path
= (char *)malloc(path_len
+ 1 + strlen(key_tree
->name
) + 1);
743 if (!new_path
) return 0; /* Errors? */
745 strcat(new_path
, path
);
746 strcat(new_path
, key_tree
->name
);
747 strcat(new_path
, "\\");
750 * Now, iterate through the values in the val_list
753 if (key_tree
->values
&&
754 !nt_val_list_iterator(regf
, key_tree
->values
, bf
, new_path
,
755 (key_tree
->values
!=NULL
),
763 * Now, iterate through the keys in the key list
766 if (key_tree
->sub_keys
&&
767 !nt_key_list_iterator(regf
, key_tree
->sub_keys
, bf
, new_path
, key_print
,
768 sec_print
, val_print
)) {
777 REG_KEY
*nt_find_key_by_name(REG_KEY
*tree
, char *key
);
780 * Find key by name in a list ...
781 * Take the first component and search for that in the list
783 REG_KEY
*nt_find_key_in_list_by_name(KEY_LIST
*list
, char *key
)
788 if (!list
|| !key
|| !*key
) return NULL
;
790 for (i
= 0; i
< list
->key_count
; i
++)
791 if ((res
= nt_find_key_by_name(list
->keys
[i
], key
)))
798 * Find key by name in a tree ... We will assume absolute names here, but we
799 * need the root of the tree ...
801 REG_KEY
*nt_find_key_by_name(REG_KEY
*tree
, char *key
)
803 char *lname
= NULL
, *c1
, *c2
;
806 if (!tree
|| !key
|| !*key
) return NULL
;
809 if (!lname
) return NULL
;
812 * Make sure that the first component is correct ...
815 c2
= strchr(c1
, '\\');
816 if (c2
) { /* Split here ... */
820 if (strcmp(c1
, tree
->name
) != 0) goto error
;
823 tmp
= nt_find_key_in_list_by_name(tree
->sub_keys
, c2
);
828 if (lname
) free(lname
);
832 if (lname
) free(lname
);
836 /* Make, delete keys */
838 int nt_delete_val_key(VAL_KEY
*val_key
)
842 if (val_key
->name
) free(val_key
->name
);
843 if (val_key
->data_blk
) free(val_key
->data_blk
);
849 int nt_delete_val_list(VAL_LIST
*vl
)
854 for (i
=0; i
<vl
->val_count
; i
++)
855 nt_delete_val_key(vl
->vals
[i
]);
861 int nt_delete_reg_key(REG_KEY
*key
, int delete_name
);
862 int nt_delete_key_list(KEY_LIST
*key_list
, int delete_name
)
867 for (i
=0; i
<key_list
->key_count
; i
++)
868 nt_delete_reg_key(key_list
->keys
[i
], False
);
875 * Find the key, and if it exists, delete it ...
877 int nt_delete_key_by_name(REGF
*regf
, char *name
)
881 if (!name
|| !*name
) return 0;
883 key
= nt_find_key_by_name(regf
->root
, name
);
886 if (key
== regf
->root
) regf
->root
= NULL
;
887 return nt_delete_reg_key(key
, True
);
894 int nt_delete_sid(DOM_SID
*sid
)
902 int nt_delete_ace(ACE
*ace
)
906 nt_delete_sid(ace
->trustee
);
913 int nt_delete_acl(ACL
*acl
)
919 for (i
=0; i
<acl
->num_aces
; i
++)
920 nt_delete_ace(acl
->aces
[i
]);
927 int nt_delete_sec_desc(SEC_DESC
*sec_desc
)
932 nt_delete_sid(sec_desc
->owner
);
933 nt_delete_sid(sec_desc
->group
);
934 nt_delete_acl(sec_desc
->sacl
);
935 nt_delete_acl(sec_desc
->dacl
);
942 int nt_delete_key_sec_desc(KEY_SEC_DESC
*key_sec_desc
)
946 key_sec_desc
->ref_cnt
--;
947 if (key_sec_desc
->ref_cnt
<=0) {
949 * There should always be a next and prev, even if they point to us
951 key_sec_desc
->next
->prev
= key_sec_desc
->prev
;
952 key_sec_desc
->prev
->next
= key_sec_desc
->next
;
953 nt_delete_sec_desc(key_sec_desc
->sec_desc
);
959 int nt_delete_reg_key(REG_KEY
*key
, int delete_name
)
963 if (key
->name
) free(key
->name
);
964 if (key
->class_name
) free(key
->class_name
);
967 * We will delete the owner if we are not the root and told to ...
970 if (key
->owner
&& key
->owner
->sub_keys
&& delete_name
) {
974 /* Find our owner, look in keylist for us and shuffle up */
975 /* Perhaps should be a function */
980 for (i
=0; i
< kl
->key_count
&& kl
->keys
[i
] != key
; i
++) {
981 /* Just find the entry ... */
984 if (i
== kl
->key_count
) {
985 fprintf(stderr
, "Bad data structure. Key not found in key list of owner\n");
991 * Shuffle up. Works for the last one also
993 for (j
= i
+ 1; j
< kl
->key_count
; j
++) {
994 kl
->keys
[j
- 1] = kl
->keys
[j
];
1001 if (key
->sub_keys
) nt_delete_key_list(key
->sub_keys
, False
);
1002 if (key
->values
) nt_delete_val_list(key
->values
);
1003 if (key
->security
) nt_delete_key_sec_desc(key
->security
);
1010 * Convert a string to a value ...
1011 * FIXME: Error handling and convert this at command parse time ...
1013 void *str_to_val(int type
, char *val
, int *len
)
1015 unsigned int *dwordp
= NULL
;
1017 if (!len
|| !val
) return NULL
;
1020 case REG_TYPE_REGSZ
:
1024 case REG_TYPE_DWORD
:
1025 dwordp
= (unsigned int *)malloc(sizeof(unsigned int));
1026 if (!dwordp
) return NULL
;
1027 /* Allow for ddddd and 0xhhhhh and 0ooooo */
1028 if (strncmp(val
, "0x", 2) == 0 || strncmp(val
, "0X", 2) == 0) {
1029 sscanf(&val
[2], "%X", dwordp
);
1031 else if (*val
== '0') {
1032 sscanf(&val
[1], "%o", dwordp
);
1035 sscanf(val
, "%d", dwordp
);
1037 *len
= sizeof(unsigned int);
1038 return (void *)dwordp
;
1040 /* FIXME: Implement more of these */
1051 * Add a value to the key specified ... We have to parse the value some more
1052 * based on the type to get it in the correct internal form
1053 * An empty name will be converted to "<No Name>" before here
1054 * Hmmm, maybe not. has_name is for that
1056 VAL_KEY
*nt_add_reg_value(REG_KEY
*key
, char *name
, int type
, char *value
)
1059 VAL_KEY
*tmp
= NULL
;
1061 if (!key
|| !key
->values
|| !name
|| !*name
) return NULL
;
1063 assert(type
!= REG_TYPE_DELETE
); /* We never process deletes here */
1065 for (i
= 0; i
< key
->values
->val_count
; i
++) {
1066 if ((!key
->values
->vals
[i
]->has_name
&& !*name
) ||
1067 (key
->values
->vals
[i
]->has_name
&&
1068 strcmp(name
, key
->values
->vals
[i
]->name
) == 0)){ /* Change the value */
1069 free(key
->values
->vals
[i
]->data_blk
);
1070 key
->values
->vals
[i
]->data_blk
= str_to_val(type
, value
, &
1071 key
->values
->vals
[i
]->data_len
);
1072 return key
->values
->vals
[i
];
1077 * If we get here, the name was not found, so insert it
1080 tmp
= (VAL_KEY
*)malloc(sizeof(VAL_KEY
));
1081 if (!tmp
) goto error
;
1083 bzero(tmp
, sizeof(VAL_KEY
));
1084 tmp
->name
= strdup(name
);
1085 tmp
->has_name
= True
;
1086 if (!tmp
->name
) goto error
;
1087 tmp
->data_type
= type
;
1088 tmp
->data_blk
= str_to_val(type
, value
, &tmp
->data_len
);
1090 /* Now, add to val list */
1092 if (key
->values
->val_count
>= key
->values
->max_vals
) {
1094 * Allocate some more space
1097 if ((key
->values
= (VAL_LIST
*)realloc(key
->values
, sizeof(VAL_LIST
) +
1098 key
->values
->val_count
- 1 +
1099 REG_KEY_LIST_SIZE
))) {
1100 key
->values
->max_vals
+= REG_KEY_LIST_SIZE
;
1105 i
= key
->values
->val_count
;
1106 key
->values
->val_count
++;
1107 key
->values
->vals
[i
] = tmp
;
1111 if (tmp
) nt_delete_val_key(tmp
);
1116 * Delete a value. We return the value and let the caller deal with it.
1118 VAL_KEY
*nt_delete_reg_value(REG_KEY
*key
, char *name
)
1122 if (!key
|| !key
->values
|| !name
|| !*name
) return NULL
;
1124 /* FIXME: Allow empty value name */
1125 for (i
= 0; i
< key
->values
->val_count
; i
++) {
1126 if ((!key
->values
->vals
[i
]->has_name
&& !*name
) ||
1127 (key
->values
->vals
[i
]->has_name
&&
1128 strcmp(name
, key
->values
->vals
[i
]->name
) == 0)) {
1131 val
= key
->values
->vals
[i
];
1134 for (j
= i
+ 1; j
< key
->values
->val_count
; j
++)
1135 key
->values
->vals
[j
- 1] = key
->values
->vals
[j
];
1137 key
->values
->val_count
--;
1146 * Add a key to the tree ... We walk down the components matching until
1147 * we don't find any. There must be a match on the first component ...
1148 * We return the key structure for the final component as that is
1149 * often where we want to add values ...
1153 * Create a 1 component key name and set its parent to parent
1155 REG_KEY
*nt_create_reg_key1(char *name
, REG_KEY
*parent
)
1159 if (!name
|| !*name
) return NULL
; /* A key's name cannot be empty */
1161 /* There should not be more than one component */
1162 if (strchr(name
, '\\')) return NULL
;
1164 if (!(tmp
= (REG_KEY
*)malloc(sizeof(REG_KEY
)))) return NULL
;
1166 bzero(tmp
, sizeof(REG_KEY
));
1168 if (!(tmp
->name
= strdup(name
))) goto error
;
1176 * Convert a string of the form S-1-5-x[-y-z-r] to a SID
1178 int string_to_sid(DOM_SID
**sid
, char *sid_str
)
1183 *sid
= (DOM_SID
*)malloc(sizeof(DOM_SID
));
1184 if (!*sid
) return 0;
1186 bzero(*sid
, sizeof(DOM_SID
));
1188 if (strncmp(sid_str
, "S-1-5", 5)) {
1189 fprintf(stderr
, "Does not conform to S-1-5...: %s\n", sid_str
);
1193 /* We only allow strings of form S-1-5... */
1196 (*sid
)->auth
[5] = 5;
1201 if (!lstr
|| !lstr
[0] || sscanf(lstr
, "-%u", &auth
) == 0) {
1203 fprintf(stderr
, "Not of form -d-d...: %s, %u\n", lstr
, i
);
1210 (*sid
)->sub_auths
[i
] = auth
;
1212 lstr
= strchr(lstr
+ 1, '-');
1221 ACE
*nt_create_ace(int type
, int flags
, unsigned int perms
, char *sid
)
1225 ace
= (ACE
*)malloc(sizeof(ACE
));
1226 if (!ace
) goto error
;
1230 if (!string_to_sid(&ace
->trustee
, sid
))
1235 if (ace
) nt_delete_ace(ace
);
1240 * Create a default ACL
1242 ACL
*nt_create_default_acl(REGF
*regf
)
1246 acl
= (ACL
*)malloc(sizeof(ACL
) + 7*sizeof(ACE
*));
1247 if (!acl
) goto error
;
1253 acl
->aces
[0] = nt_create_ace(0x00, 0x0, 0xF003F, regf
->owner_sid_str
);
1254 if (!acl
->aces
[0]) goto error
;
1255 acl
->aces
[1] = nt_create_ace(0x00, 0x0, 0xF003F, "S-1-5-18");
1256 if (!acl
->aces
[1]) goto error
;
1257 acl
->aces
[2] = nt_create_ace(0x00, 0x0, 0xF003F, "S-1-5-32-544");
1258 if (!acl
->aces
[2]) goto error
;
1259 acl
->aces
[3] = nt_create_ace(0x00, 0x0, 0x20019, "S-1-5-12");
1260 if (!acl
->aces
[3]) goto error
;
1261 acl
->aces
[4] = nt_create_ace(0x00, 0x0B, 0x10000000, regf
->owner_sid_str
);
1262 if (!acl
->aces
[4]) goto error
;
1263 acl
->aces
[5] = nt_create_ace(0x00, 0x0B, 0x10000000, "S-1-5-18");
1264 if (!acl
->aces
[5]) goto error
;
1265 acl
->aces
[6] = nt_create_ace(0x00, 0x0B, 0x10000000, "S-1-5-32-544");
1266 if (!acl
->aces
[6]) goto error
;
1267 acl
->aces
[7] = nt_create_ace(0x00, 0x0B, 0x80000000, "S-1-5-12");
1268 if (!acl
->aces
[7]) goto error
;
1272 if (acl
) nt_delete_acl(acl
);
1277 * Create a default security descriptor. We pull in things from env
1280 SEC_DESC
*nt_create_def_sec_desc(REGF
*regf
)
1284 tmp
= (SEC_DESC
*)malloc(sizeof(SEC_DESC
));
1285 if (!tmp
) return NULL
;
1289 if (!string_to_sid(&tmp
->owner
, "S-1-5-32-544")) goto error
;
1290 if (!string_to_sid(&tmp
->group
, "S-1-5-18")) goto error
;
1292 tmp
->dacl
= nt_create_default_acl(regf
);
1297 if (tmp
) nt_delete_sec_desc(tmp
);
1302 * We will implement inheritence that is based on what the parent's SEC_DESC
1303 * says, but the Owner and Group SIDs can be overwridden from the command line
1304 * and additional ACEs can be applied from the command line etc.
1306 KEY_SEC_DESC
*nt_inherit_security(REG_KEY
*key
)
1309 if (!key
) return NULL
;
1310 return key
->security
;
1314 * Create an initial security descriptor and init other structures, if needed
1315 * We assume that the initial security stuff is empty ...
1317 KEY_SEC_DESC
*nt_create_init_sec(REGF
*regf
)
1319 KEY_SEC_DESC
*tsec
= NULL
;
1321 tsec
= (KEY_SEC_DESC
*)malloc(sizeof(KEY_SEC_DESC
));
1322 if (!tsec
) return NULL
;
1325 tsec
->state
= SEC_DESC_NBK
;
1328 tsec
->sec_desc
= regf
->def_sec_desc
;
1336 REG_KEY
*nt_add_reg_key_list(REGF
*regf
, REG_KEY
*key
, char * name
, int create
)
1339 REG_KEY
*ret
= NULL
, *tmp
= NULL
;
1341 char *lname
, *c1
, *c2
;
1343 if (!key
|| !name
|| !*name
) return NULL
;
1345 list
= key
->sub_keys
;
1346 if (!list
) { /* Create an empty list */
1348 list
= (KEY_LIST
*)malloc(sizeof(KEY_LIST
) + (REG_KEY_LIST_SIZE
- 1) * sizeof(REG_KEY
*));
1349 list
->key_count
= 0;
1350 list
->max_keys
= REG_KEY_LIST_SIZE
;
1354 lname
= strdup(name
);
1355 if (!lname
) return NULL
;
1358 c2
= strchr(c1
, '\\');
1359 if (c2
) { /* Split here ... */
1364 for (i
= 0; i
< list
->key_count
; i
++) {
1365 if (strcmp(list
->keys
[i
]->name
, c1
) == 0) {
1366 ret
= nt_add_reg_key_list(regf
, list
->keys
[i
], c2
, create
);
1373 * If we reach here we could not find the the first component
1377 if (list
->key_count
< list
->max_keys
){
1380 else { /* Create more space in the list ... */
1381 if (!(list
= (KEY_LIST
*)realloc(list
, sizeof(KEY_LIST
) +
1382 (list
->max_keys
+ REG_KEY_LIST_SIZE
- 1)
1383 * sizeof(REG_KEY
*))));
1386 list
->max_keys
+= REG_KEY_LIST_SIZE
;
1391 * add the new key at the new slot
1392 * FIXME: Sort the list someday
1396 * We want to create the key, and then do the rest
1399 tmp
= (REG_KEY
*)malloc(sizeof(REG_KEY
));
1401 bzero(tmp
, sizeof(REG_KEY
));
1403 tmp
->name
= strdup(c1
);
1404 if (!tmp
->name
) goto error
;
1406 tmp
->type
= REG_SUB_KEY
;
1408 * Next, pull security from the parent, but override with
1409 * anything passed in on the command line
1411 tmp
->security
= nt_inherit_security(key
);
1413 list
->keys
[list
->key_count
- 1] = tmp
;
1416 ret
= nt_add_reg_key_list(regf
, key
, c2
, True
);
1419 if (lname
) free(lname
);
1425 if (lname
) free(lname
);
1430 * This routine only adds a key from the root down.
1431 * It calls helper functions to handle sub-key lists and sub-keys
1433 REG_KEY
*nt_add_reg_key(REGF
*regf
, char *name
, int create
)
1435 char *lname
= NULL
, *c1
, *c2
;
1436 REG_KEY
* tmp
= NULL
;
1439 * Look until we hit the first component that does not exist, and
1440 * then add from there. However, if the first component does not
1441 * match and the path we are given is the root, then it must match
1443 if (!regf
|| !name
|| !*name
) return NULL
;
1445 lname
= strdup(name
);
1446 if (!lname
) return NULL
;
1449 c2
= strchr(c1
, '\\');
1450 if (c2
) { /* Split here ... */
1456 * If the root does not exist, create it and make it equal to the
1457 * first component ...
1462 tmp
= (REG_KEY
*)malloc(sizeof(REG_KEY
));
1463 if (!tmp
) goto error
;
1464 bzero(tmp
, sizeof(REG_KEY
));
1465 tmp
->name
= strdup(c1
);
1466 if (!tmp
->name
) goto error
;
1467 tmp
->security
= nt_create_init_sec(regf
);
1468 if (!tmp
->security
) goto error
;
1474 * If we don't match, then we have to return error ...
1475 * If we do match on this component, check the next one in the
1476 * list, and if not found, add it ... short circuit, add all the
1480 if (strcmp(c1
, regf
->root
->name
) != 0)
1484 tmp
= nt_add_reg_key_list(regf
, regf
->root
, c2
, True
);
1490 if (lname
) free(lname
);
1495 * Load and unload a registry file.
1497 * Load, loads it into memory as a tree, while unload sealizes/flattens it
1501 * Get the starting record for NT Registry file
1505 * Where we keep all the regf stuff for one registry.
1506 * This is the structure that we use to tie the in memory tree etc
1507 * together. By keeping separate structs, we can operate on different
1508 * registries at the same time.
1509 * Currently, the SK_MAP is an array of mapping structure.
1510 * Since we only need this on input and output, we fill in the structure
1511 * as we go on input. On output, we know how many SK items we have, so
1512 * we can allocate the structure as we need to.
1513 * If you add stuff here that is dynamically allocated, add the
1514 * appropriate free statements below.
1517 #define REGF_REGTYPE_NONE 0
1518 #define REGF_REGTYPE_NT 1
1519 #define REGF_REGTYPE_W9X 2
1521 #define TTTONTTIME(r, t1, t2) (r)->last_mod_time.low = (t1); \
1522 (r)->last_mod_time.high = (t2);
1524 #define REGF_HDR_BLKSIZ 0x1000
1526 #define OFF(f) ((f) + REGF_HDR_BLKSIZ + 4)
1527 #define LOCN(base, f) ((base) + OFF(f))
1529 const VAL_STR reg_type_names
[] = {
1530 { REG_TYPE_REGSZ
, "REG_SZ" },
1531 { REG_TYPE_EXPANDSZ
, "REG_EXPAND_SZ" },
1532 { REG_TYPE_BIN
, "REG_BIN" },
1533 { REG_TYPE_DWORD
, "REG_DWORD" },
1534 { REG_TYPE_MULTISZ
, "REG_MULTI_SZ" },
1538 const char *val_to_str(unsigned int val
, const VAL_STR
*val_array
)
1542 if (!val_array
) return NULL
;
1544 while (val_array
[i
].val
&& val_array
[i
].str
) {
1546 if (val_array
[i
].val
== val
) return val_array
[i
].str
;
1556 * Convert from UniCode to Ascii ... Does not take into account other lang
1557 * Restrict by ascii_max if > 0
1559 int uni_to_ascii(unsigned char *uni
, unsigned char *ascii
, int ascii_max
,
1564 while (i
< ascii_max
&& !(!uni
[i
*2] && !uni
[i
*2+1])) {
1565 if (uni_max
> 0 && (i
*2) >= uni_max
) break;
1566 ascii
[i
] = uni
[i
*2];
1577 * Convert a data value to a string for display
1579 int data_to_ascii(unsigned char *datap
, int len
, int type
, char *ascii
, int ascii_max
)
1581 unsigned char *asciip
;
1585 case REG_TYPE_REGSZ
:
1586 if (verbose
) fprintf(stderr
, "Len: %d\n", len
);
1587 /* FIXME. This has to be fixed. It has to be UNICODE */
1588 return uni_to_ascii(datap
, ascii
, len
, ascii_max
);
1591 case REG_TYPE_EXPANDSZ
:
1592 return uni_to_ascii(datap
, ascii
, len
, ascii_max
);
1597 for (i
=0; (i
<len
)&&(i
+1)*3<ascii_max
; i
++) {
1598 int str_rem
= ascii_max
- ((int)asciip
- (int)ascii
);
1599 asciip
+= snprintf(asciip
, str_rem
, "%02x", *(unsigned char *)(datap
+i
));
1600 if (i
< len
&& str_rem
> 0)
1601 *asciip
= ' '; asciip
++;
1604 return ((int)asciip
- (int)ascii
);
1607 case REG_TYPE_DWORD
:
1608 if (*(int *)datap
== 0)
1609 return snprintf(ascii
, ascii_max
, "0");
1611 return snprintf(ascii
, ascii_max
, "0x%x", *(int *)datap
);
1614 case REG_TYPE_MULTISZ
:
1627 REG_KEY
*nt_get_key_tree(REGF
*regf
, NK_HDR
*nk_hdr
, int size
, REG_KEY
*parent
);
1629 int nt_set_regf_input_file(REGF
*regf
, char *filename
)
1631 return ((regf
->regfile_name
= strdup(filename
)) != NULL
);
1634 int nt_set_regf_output_file(REGF
*regf
, char *filename
)
1636 return ((regf
->outfile_name
= strdup(filename
)) != NULL
);
1639 /* Create a regf structure and init it */
1641 REGF
*nt_create_regf(void)
1643 REGF
*tmp
= (REGF
*)malloc(sizeof(REGF
));
1644 if (!tmp
) return tmp
;
1645 bzero(tmp
, sizeof(REGF
));
1646 tmp
->owner_sid_str
= def_owner_sid_str
;
1650 /* Free all the bits and pieces ... Assumes regf was malloc'd */
1651 /* If you add stuff to REGF, add the relevant free bits here */
1652 int nt_free_regf(REGF
*regf
)
1654 if (!regf
) return 0;
1656 if (regf
->regfile_name
) free(regf
->regfile_name
);
1657 if (regf
->outfile_name
) free(regf
->outfile_name
);
1659 nt_delete_reg_key(regf
->root
, False
); /* Free the tree */
1661 regf
->sk_count
= regf
->sk_map_size
= 0;
1668 /* Get the header of the registry. Return a pointer to the structure
1669 * If the mmap'd area has not been allocated, then mmap the input file
1671 REGF_HDR
*nt_get_regf_hdr(REGF
*regf
)
1674 return NULL
; /* What about errors */
1676 if (!regf
->regfile_name
)
1677 return NULL
; /* What about errors */
1679 if (!regf
->base
) { /* Try to mmap etc the file */
1681 if ((regf
->fd
= open(regf
->regfile_name
, O_RDONLY
, 0000)) <0) {
1682 return NULL
; /* What about errors? */
1685 if (fstat(regf
->fd
, ®f
->sbuf
) < 0) {
1689 regf
->base
= mmap(0, regf
->sbuf
.st_size
, PROT_READ
, MAP_SHARED
, regf
->fd
, 0);
1691 if ((int)regf
->base
== 1) {
1692 fprintf(stderr
, "Could not mmap file: %s, %s\n", regf
->regfile_name
,
1699 * At this point, regf->base != NULL, and we should be able to read the
1703 assert(regf
->base
!= NULL
);
1705 return (REGF_HDR
*)regf
->base
;
1709 * Validate a regf header
1710 * For now, do nothing, but we should check the checksum
1712 int valid_regf_hdr(REGF_HDR
*regf_hdr
)
1714 if (!regf_hdr
) return 0;
1720 * Process an SK header ...
1721 * Every time we see a new one, add it to the map. Otherwise, just look it up.
1722 * We will do a simple linear search for the moment, since many KEYs have the
1723 * same security descriptor.
1724 * We allocate the map in increments of 10 entries.
1728 * Create a new entry in the map, and increase the size of the map if needed
1731 SK_MAP
*alloc_sk_map_entry(REGF
*regf
, KEY_SEC_DESC
*tmp
, int sk_off
)
1733 if (!regf
->sk_map
) { /* Allocate a block of 10 */
1734 regf
->sk_map
= (SK_MAP
*)malloc(sizeof(SK_MAP
) * 10);
1735 if (!regf
->sk_map
) {
1739 regf
->sk_map_size
= 10;
1741 (regf
->sk_map
)[0].sk_off
= sk_off
;
1742 (regf
->sk_map
)[0].key_sec_desc
= tmp
;
1744 else { /* Simply allocate a new slot, unless we have to expand the list */
1745 int ndx
= regf
->sk_count
;
1746 if (regf
->sk_count
>= regf
->sk_map_size
) {
1747 regf
->sk_map
= (SK_MAP
*)realloc(regf
->sk_map
,
1748 (regf
->sk_map_size
+ 10)*sizeof(SK_MAP
));
1749 if (!regf
->sk_map
) {
1754 * ndx already points at the first entry of the new block
1756 regf
->sk_map_size
+= 10;
1758 (regf
->sk_map
)[ndx
].sk_off
= sk_off
;
1759 (regf
->sk_map
)[ndx
].key_sec_desc
= tmp
;
1762 return regf
->sk_map
;
1766 * Search for a KEY_SEC_DESC in the sk_map, but don't create one if not
1770 KEY_SEC_DESC
*lookup_sec_key(SK_MAP
*sk_map
, int count
, int sk_off
)
1774 if (!sk_map
) return NULL
;
1776 for (i
= 0; i
< count
; i
++) {
1778 if (sk_map
[i
].sk_off
== sk_off
)
1779 return sk_map
[i
].key_sec_desc
;
1788 * Allocate a KEY_SEC_DESC if we can't find one in the map
1791 KEY_SEC_DESC
*lookup_create_sec_key(REGF
*regf
, SK_MAP
*sk_map
, int sk_off
)
1793 KEY_SEC_DESC
*tmp
= lookup_sec_key(regf
->sk_map
, regf
->sk_count
, sk_off
);
1798 else { /* Allocate a new one */
1799 tmp
= (KEY_SEC_DESC
*)malloc(sizeof(KEY_SEC_DESC
));
1803 bzero(tmp
, sizeof(KEY_SEC_DESC
)); /* Neatly sets offset to 0 */
1804 tmp
->state
= SEC_DESC_RES
;
1805 if (!alloc_sk_map_entry(regf
, tmp
, sk_off
)) {
1813 * Allocate storage and duplicate a SID
1814 * We could allocate the SID to be only the size needed, but I am too lazy.
1816 DOM_SID
*dup_sid(DOM_SID
*sid
)
1818 DOM_SID
*tmp
= (DOM_SID
*)malloc(sizeof(DOM_SID
));
1821 if (!tmp
) return NULL
;
1822 tmp
->ver
= sid
->ver
;
1823 tmp
->auths
= sid
->auths
;
1824 for (i
=0; i
<6; i
++) {
1825 tmp
->auth
[i
] = sid
->auth
[i
];
1827 for (i
=0; i
<tmp
->auths
&&i
<MAXSUBAUTHS
; i
++) {
1828 tmp
->sub_auths
[i
] = sid
->sub_auths
[i
];
1834 * Allocate space for an ACE and duplicate the registry encoded one passed in
1836 ACE
*dup_ace(REG_ACE
*ace
)
1840 tmp
= (ACE
*)malloc(sizeof(ACE
));
1842 if (!tmp
) return NULL
;
1844 tmp
->type
= CVAL(&ace
->type
);
1845 tmp
->flags
= CVAL(&ace
->flags
);
1846 tmp
->perms
= IVAL(&ace
->perms
);
1847 tmp
->trustee
= dup_sid(&ace
->trustee
);
1852 * Allocate space for an ACL and duplicate the registry encoded one passed in
1854 ACL
*dup_acl(REG_ACL
*acl
)
1860 num_aces
= IVAL(&acl
->num_aces
);
1862 tmp
= (ACL
*)malloc(sizeof(ACL
) + (num_aces
- 1)*sizeof(ACE
*));
1863 if (!tmp
) return NULL
;
1865 tmp
->num_aces
= num_aces
;
1867 tmp
->rev
= SVAL(&acl
->rev
);
1868 if (verbose
) fprintf(stdout
, "ACL: refcnt: %u, rev: %u\n", tmp
->refcnt
,
1870 ace
= (REG_ACE
*)&acl
->aces
;
1871 for (i
=0; i
<num_aces
; i
++) {
1872 tmp
->aces
[i
] = dup_ace(ace
);
1873 ace
= (REG_ACE
*)((char *)ace
+ SVAL(&ace
->length
));
1874 /* XXX: FIXME, should handle malloc errors */
1880 SEC_DESC
*process_sec_desc(REGF
*regf
, REG_SEC_DESC
*sec_desc
)
1882 SEC_DESC
*tmp
= NULL
;
1884 tmp
= (SEC_DESC
*)malloc(sizeof(SEC_DESC
));
1890 tmp
->rev
= SVAL(&sec_desc
->rev
);
1891 tmp
->type
= SVAL(&sec_desc
->type
);
1892 if (verbose
) fprintf(stdout
, "SEC_DESC Rev: %0X, Type: %0X\n",
1893 tmp
->rev
, tmp
->type
);
1894 if (verbose
) fprintf(stdout
, "SEC_DESC Owner Off: %0X\n",
1895 IVAL(&sec_desc
->owner_off
));
1896 if (verbose
) fprintf(stdout
, "SEC_DESC Group Off: %0X\n",
1897 IVAL(&sec_desc
->group_off
));
1898 if (verbose
) fprintf(stdout
, "SEC_DESC DACL Off: %0X\n",
1899 IVAL(&sec_desc
->dacl_off
));
1900 tmp
->owner
= dup_sid((DOM_SID
*)((char *)sec_desc
+ IVAL(&sec_desc
->owner_off
)));
1905 tmp
->group
= dup_sid((DOM_SID
*)((char *)sec_desc
+ IVAL(&sec_desc
->group_off
)));
1911 /* Now pick up the SACL and DACL */
1913 if (sec_desc
->sacl_off
)
1914 tmp
->sacl
= dup_acl((REG_ACL
*)((char *)sec_desc
+ IVAL(&sec_desc
->sacl_off
)));
1918 if (sec_desc
->dacl_off
)
1919 tmp
->dacl
= dup_acl((REG_ACL
*)((char *)sec_desc
+ IVAL(&sec_desc
->dacl_off
)));
1926 KEY_SEC_DESC
*process_sk(REGF
*regf
, SK_HDR
*sk_hdr
, int sk_off
, int size
)
1928 KEY_SEC_DESC
*tmp
= NULL
;
1929 int sk_next_off
, sk_prev_off
, sk_size
;
1930 REG_SEC_DESC
*sec_desc
;
1932 if (!sk_hdr
) return NULL
;
1934 if (SVAL(&sk_hdr
->SK_ID
) != REG_SK_ID
) {
1935 fprintf(stderr
, "Unrecognized SK Header ID: %08X, %s\n", (int)sk_hdr
,
1936 regf
->regfile_name
);
1940 if (-size
< (sk_size
= IVAL(&sk_hdr
->rec_size
))) {
1941 fprintf(stderr
, "Incorrect SK record size: %d vs %d. %s\n",
1942 -size
, sk_size
, regf
->regfile_name
);
1947 * Now, we need to look up the SK Record in the map, and return it
1948 * Since the map contains the SK_OFF mapped to KEY_SEC_DESC, we can
1953 ((tmp
= lookup_sec_key(regf
->sk_map
, regf
->sk_count
, sk_off
)) != NULL
)
1954 && (tmp
->state
== SEC_DESC_OCU
)) {
1959 /* Here, we have an item in the map that has been reserved, or tmp==NULL. */
1961 assert(tmp
== NULL
|| (tmp
&& tmp
->state
!= SEC_DESC_NON
));
1964 * Now, allocate a KEY_SEC_DESC, and parse the structure here, and add the
1965 * new KEY_SEC_DESC to the mapping structure, since the offset supplied is
1966 * the actual offset of structure. The same offset will be used by
1967 * all future references to this structure
1968 * We could put all this unpleasantness in a function.
1972 tmp
= (KEY_SEC_DESC
*)malloc(sizeof(KEY_SEC_DESC
));
1973 if (!tmp
) return NULL
;
1974 bzero(tmp
, sizeof(KEY_SEC_DESC
));
1977 * Allocate an entry in the SK_MAP ...
1978 * We don't need to free tmp, because that is done for us if the
1979 * sm_map entry can't be expanded when we need more space in the map.
1982 if (!alloc_sk_map_entry(regf
, tmp
, sk_off
)) {
1988 tmp
->state
= SEC_DESC_OCU
;
1991 * Now, process the actual sec desc and plug the values in
1994 sec_desc
= (REG_SEC_DESC
*)&sk_hdr
->sec_desc
[0];
1995 tmp
->sec_desc
= process_sec_desc(regf
, sec_desc
);
1998 * Now forward and back links. Here we allocate an entry in the sk_map
1999 * if it does not exist, and mark it reserved
2002 sk_prev_off
= IVAL(&sk_hdr
->prev_off
);
2003 tmp
->prev
= lookup_create_sec_key(regf
, regf
->sk_map
, sk_prev_off
);
2004 assert(tmp
->prev
!= NULL
);
2005 sk_next_off
= IVAL(&sk_hdr
->next_off
);
2006 tmp
->next
= lookup_create_sec_key(regf
, regf
->sk_map
, sk_next_off
);
2007 assert(tmp
->next
!= NULL
);
2013 * Process a VK header and return a value
2015 VAL_KEY
*process_vk(REGF
*regf
, VK_HDR
*vk_hdr
, int size
)
2017 char val_name
[1024];
2018 int nam_len
, dat_len
, flag
, dat_type
, dat_off
, vk_id
;
2019 const char *val_type
;
2020 VAL_KEY
*tmp
= NULL
;
2022 if (!vk_hdr
) return NULL
;
2024 if ((vk_id
= SVAL(&vk_hdr
->VK_ID
)) != REG_VK_ID
) {
2025 fprintf(stderr
, "Unrecognized VK header ID: %0X, block: %0X, %s\n",
2026 vk_id
, (int)vk_hdr
, regf
->regfile_name
);
2030 nam_len
= SVAL(&vk_hdr
->nam_len
);
2031 val_name
[nam_len
] = '\0';
2032 flag
= SVAL(&vk_hdr
->flag
);
2033 dat_type
= IVAL(&vk_hdr
->dat_type
);
2034 dat_len
= IVAL(&vk_hdr
->dat_len
); /* If top bit, offset contains data */
2035 dat_off
= IVAL(&vk_hdr
->dat_off
);
2037 tmp
= (VAL_KEY
*)malloc(sizeof(VAL_KEY
));
2041 bzero(tmp
, sizeof(VAL_KEY
));
2042 tmp
->has_name
= flag
;
2043 tmp
->data_type
= dat_type
;
2046 strncpy(val_name
, vk_hdr
->dat_name
, nam_len
);
2047 tmp
->name
= strdup(val_name
);
2053 strncpy(val_name
, "<No Name>", 10);
2056 * Allocate space and copy the data as a BLOB
2061 char *dtmp
= (char *)malloc(dat_len
&0x7FFFFFFF);
2067 tmp
->data_blk
= dtmp
;
2069 if ((dat_len
&0x80000000) == 0) { /* The data is pointed to by the offset */
2070 char *dat_ptr
= LOCN(regf
->base
, dat_off
);
2071 bcopy(dat_ptr
, dtmp
, dat_len
);
2073 else { /* The data is in the offset or type */
2076 * Some registry files seem to have wierd fields. If top bit is set,
2077 * but len is 0, the type seems to be the value ...
2078 * Not sure how to handle this last type for the moment ...
2080 dat_len
= dat_len
& 0x7FFFFFFF;
2081 bcopy(&dat_off
, dtmp
, dat_len
);
2084 tmp
->data_len
= dat_len
;
2087 val_type
= val_to_str(dat_type
, reg_type_names
);
2090 * We need to save the data area as well
2093 if (verbose
) fprintf(stdout
, " %s : %s : \n", val_name
, val_type
);
2098 if (tmp
) nt_delete_val_key(tmp
);
2104 * Process a VL Header and return a list of values
2106 VAL_LIST
*process_vl(REGF
*regf
, VL_TYPE vl
, int count
, int size
)
2110 VAL_LIST
*tmp
= NULL
;
2112 if (!vl
) return NULL
;
2114 if (-size
< (count
+1)*sizeof(int)){
2115 fprintf(stderr
, "Error in VL header format. Size less than space required. %d\n", -size
);
2119 tmp
= (VAL_LIST
*)malloc(sizeof(VAL_LIST
) + (count
- 1) * sizeof(VAL_KEY
*));
2124 for (i
=0; i
<count
; i
++) {
2125 vk_off
= IVAL(&vl
[i
]);
2126 vk_hdr
= (VK_HDR
*)LOCN(regf
->base
, vk_off
);
2127 tmp
->vals
[i
] = process_vk(regf
, vk_hdr
, BLK_SIZE(vk_hdr
));
2133 tmp
->val_count
= count
;
2134 tmp
->max_vals
= count
;
2139 /* XXX: FIXME, free the partially allocated structure */
2144 * Process an LF Header and return a list of sub-keys
2146 KEY_LIST
*process_lf(REGF
*regf
, LF_HDR
*lf_hdr
, int size
, REG_KEY
*parent
)
2148 int count
, i
, nk_off
;
2152 if (!lf_hdr
) return NULL
;
2154 if ((lf_id
= SVAL(&lf_hdr
->LF_ID
)) != REG_LF_ID
) {
2155 fprintf(stderr
, "Unrecognized LF Header format: %0X, Block: %0X, %s.\n",
2156 lf_id
, (int)lf_hdr
, regf
->regfile_name
);
2162 count
= SVAL(&lf_hdr
->key_count
);
2163 if (verbose
) fprintf(stdout
, "Key Count: %u\n", count
);
2164 if (count
<= 0) return NULL
;
2166 /* Now, we should allocate a KEY_LIST struct and fill it in ... */
2168 tmp
= (KEY_LIST
*)malloc(sizeof(KEY_LIST
) + (count
- 1) * sizeof(REG_KEY
*));
2173 tmp
->key_count
= count
;
2174 tmp
->max_keys
= count
;
2176 for (i
=0; i
<count
; i
++) {
2179 nk_off
= IVAL(&lf_hdr
->hr
[i
].nk_off
);
2180 if (verbose
) fprintf(stdout
, "NK Offset: %0X\n", nk_off
);
2181 nk_hdr
= (NK_HDR
*)LOCN(regf
->base
, nk_off
);
2182 tmp
->keys
[i
] = nt_get_key_tree(regf
, nk_hdr
, BLK_SIZE(nk_hdr
), parent
);
2183 if (!tmp
->keys
[i
]) {
2191 if (tmp
) nt_delete_key_list(tmp
, False
);
2196 * This routine is passed an NK_HDR pointer and retrieves the entire tree
2197 * from there down. It returns a REG_KEY *.
2199 REG_KEY
*nt_get_key_tree(REGF
*regf
, NK_HDR
*nk_hdr
, int size
, REG_KEY
*parent
)
2201 REG_KEY
*tmp
= NULL
, *own
;
2202 int name_len
, clsname_len
, lf_off
, val_off
, val_count
, sk_off
, own_off
;
2207 char key_name
[1024], cls_name
[1024];
2209 if (!nk_hdr
) return NULL
;
2211 if ((nk_id
= SVAL(&nk_hdr
->NK_ID
)) != REG_NK_ID
) {
2212 fprintf(stderr
, "Unrecognized NK Header format: %08X, Block: %0X. %s\n",
2213 nk_id
, (int)nk_hdr
, regf
->regfile_name
);
2219 name_len
= SVAL(&nk_hdr
->nam_len
);
2220 clsname_len
= SVAL(&nk_hdr
->clsnam_len
);
2223 * The value of -size should be ge
2224 * (sizeof(NK_HDR) - 1 + name_len)
2225 * The -1 accounts for the fact that we included the first byte of
2226 * the name in the structure. clsname_len is the length of the thing
2227 * pointed to by clsnam_off
2230 if (-size
< (sizeof(NK_HDR
) - 1 + name_len
)) {
2231 fprintf(stderr
, "Incorrect NK_HDR size: %d, %0X\n", -size
, (int)nk_hdr
);
2232 fprintf(stderr
, "Sizeof NK_HDR: %d, name_len %d, clsname_len %d\n",
2233 sizeof(NK_HDR
), name_len
, clsname_len
);
2237 if (verbose
) fprintf(stdout
, "NK HDR: Name len: %d, class name len: %d\n",
2238 name_len
, clsname_len
);
2240 /* Fish out the key name and process the LF list */
2242 assert(name_len
< sizeof(key_name
));
2244 /* Allocate the key struct now */
2245 tmp
= (REG_KEY
*)malloc(sizeof(REG_KEY
));
2246 if (!tmp
) return tmp
;
2247 bzero(tmp
, sizeof(REG_KEY
));
2249 tmp
->type
= (SVAL(&nk_hdr
->type
)==0x2C?REG_ROOT_KEY
:REG_SUB_KEY
);
2251 strncpy(key_name
, nk_hdr
->key_nam
, name_len
);
2252 key_name
[name_len
] = '\0';
2254 if (verbose
) fprintf(stdout
, "Key name: %s\n", key_name
);
2256 tmp
->name
= strdup(key_name
);
2262 * Fish out the class name, it is in UNICODE, while the key name is
2266 if (clsname_len
) { /* Just print in Ascii for now */
2270 clsnam_off
= IVAL(&nk_hdr
->clsnam_off
);
2271 clsnamep
= LOCN(regf
->base
, clsnam_off
);
2272 if (verbose
) fprintf(stdout
, "Class Name Offset: %0X\n", clsnam_off
);
2274 bzero(cls_name
, clsname_len
);
2275 uni_to_ascii(clsnamep
, cls_name
, sizeof(cls_name
), clsname_len
);
2278 * I am keeping class name as an ascii string for the moment.
2279 * That means it needs to be converted on output.
2280 * It will also piss off people who need Unicode/UTF-8 strings. Sorry.
2284 tmp
->class_name
= strdup(cls_name
);
2285 if (!tmp
->class_name
) {
2289 if (verbose
) fprintf(stdout
, " Class Name: %s\n", cls_name
);
2294 * Process the owner offset ...
2297 own_off
= IVAL(&nk_hdr
->own_off
);
2298 own
= (REG_KEY
*)LOCN(regf
->base
, own_off
);
2299 if (verbose
) fprintf(stdout
, "Owner Offset: %0X\n", own_off
);
2301 if (verbose
) fprintf(stdout
, " Owner locn: %0X, Our locn: %0X\n",
2302 (unsigned int)own
, (unsigned int)nk_hdr
);
2305 * We should verify that the owner field is correct ...
2306 * for now, we don't worry ...
2309 tmp
->owner
= parent
;
2312 * If there are any values, process them here
2315 val_count
= IVAL(&nk_hdr
->val_cnt
);
2316 if (verbose
) fprintf(stdout
, "Val Count: %d\n", val_count
);
2319 val_off
= IVAL(&nk_hdr
->val_off
);
2320 vl
= (VL_TYPE
*)LOCN(regf
->base
, val_off
);
2321 if (verbose
) fprintf(stdout
, "Val List Offset: %0X\n", val_off
);
2323 tmp
->values
= process_vl(regf
, *vl
, val_count
, BLK_SIZE(vl
));
2331 * Also handle the SK header ...
2334 sk_off
= IVAL(&nk_hdr
->sk_off
);
2335 sk_hdr
= (SK_HDR
*)LOCN(regf
->base
, sk_off
);
2336 if (verbose
) fprintf(stdout
, "SK Offset: %0X\n", sk_off
);
2340 tmp
->security
= process_sk(regf
, sk_hdr
, sk_off
, BLK_SIZE(sk_hdr
));
2344 lf_off
= IVAL(&nk_hdr
->lf_off
);
2345 if (verbose
) fprintf(stdout
, "SubKey list offset: %0X\n", lf_off
);
2348 * No more subkeys if lf_off == -1
2353 lf_hdr
= (LF_HDR
*)LOCN(regf
->base
, lf_off
);
2355 tmp
->sub_keys
= process_lf(regf
, lf_hdr
, BLK_SIZE(lf_hdr
), tmp
);
2356 if (!tmp
->sub_keys
){
2365 if (tmp
) nt_delete_reg_key(tmp
, False
);
2369 int nt_load_registry(REGF
*regf
)
2372 unsigned int regf_id
, hbin_id
;
2376 /* Get the header */
2378 if ((regf_hdr
= nt_get_regf_hdr(regf
)) == NULL
) {
2382 /* Now process that header and start to read the rest in */
2384 if ((regf_id
= IVAL(®f_hdr
->REGF_ID
)) != REG_REGF_ID
) {
2385 fprintf(stderr
, "Unrecognized NT registry header id: %0X, %s\n",
2386 regf_id
, regf
->regfile_name
);
2391 * Validate the header ...
2393 if (!valid_regf_hdr(regf_hdr
)) {
2394 fprintf(stderr
, "Registry file header does not validate: %s\n",
2395 regf
->regfile_name
);
2399 /* Update the last mod date, and then go get the first NK record and on */
2401 TTTONTTIME(regf
, IVAL(®f_hdr
->tim1
), IVAL(®f_hdr
->tim2
));
2404 * The hbin hdr seems to be just uninteresting garbage. Check that
2405 * it is there, but that is all.
2408 hbin_hdr
= (HBIN_HDR
*)(regf
->base
+ REGF_HDR_BLKSIZ
);
2410 if ((hbin_id
= IVAL(&hbin_hdr
->HBIN_ID
)) != REG_HBIN_ID
) {
2411 fprintf(stderr
, "Unrecognized registry hbin hdr ID: %0X, %s\n",
2412 hbin_id
, regf
->regfile_name
);
2417 * Get a pointer to the first key from the hreg_hdr
2420 if (verbose
) fprintf(stdout
, "First Key: %0X\n",
2421 IVAL(®f_hdr
->first_key
));
2423 first_key
= (NK_HDR
*)LOCN(regf
->base
, IVAL(®f_hdr
->first_key
));
2424 if (verbose
) fprintf(stdout
, "First Key Offset: %0X\n",
2425 IVAL(®f_hdr
->first_key
));
2427 if (verbose
) fprintf(stdout
, "Data Block Size: %d\n",
2428 IVAL(®f_hdr
->dblk_size
));
2430 if (verbose
) fprintf(stdout
, "Offset to next hbin block: %0X\n",
2431 IVAL(&hbin_hdr
->off_to_next
));
2433 if (verbose
) fprintf(stdout
, "HBIN block size: %0X\n",
2434 IVAL(&hbin_hdr
->blk_size
));
2437 * Now, get the registry tree by processing that NK recursively
2440 regf
->root
= nt_get_key_tree(regf
, first_key
, BLK_SIZE(first_key
), NULL
);
2442 assert(regf
->root
!= NULL
);
2445 * Unmap the registry file, as we might want to read in another
2449 if (regf
->base
) munmap(regf
->base
, regf
->sbuf
.st_size
);
2451 close(regf
->fd
); /* Ignore the error :-) */
2457 * Allocate a new hbin block, set up the header for the block etc
2459 HBIN_BLK
*nt_create_hbin_blk(REGF
*regf
, int size
)
2464 if (!regf
|| !size
) return NULL
;
2466 /* Round size up to multiple of REGF_HDR_BLKSIZ */
2468 size
= (size
+ (REGF_HDR_BLKSIZ
- 1)) & ~(REGF_HDR_BLKSIZ
- 1);
2470 tmp
= (HBIN_BLK
*)malloc(sizeof(HBIN_BLK
));
2471 bzero(tmp
, sizeof(HBIN_BLK
));
2473 tmp
->data
= malloc(size
);
2474 if (!tmp
->data
) goto error
;
2476 bzero(tmp
->data
, size
); /* Make it pristine */
2479 tmp
->file_offset
= regf
->blk_tail
->file_offset
+ regf
->blk_tail
->size
;
2481 tmp
->free_space
= size
- (sizeof(HBIN_HDR
) - sizeof(HBIN_SUB_HDR
));
2482 tmp
->fsp_off
= size
- tmp
->free_space
;
2485 * Now, build the header in the data block
2487 hdr
= (HBIN_HDR
*)tmp
->data
;
2488 hdr
->HBIN_ID
= REG_HBIN_ID
;
2489 hdr
->off_from_first
= tmp
->file_offset
- REGF_HDR_BLKSIZ
;
2490 hdr
->off_to_next
= tmp
->size
;
2491 hdr
->blk_size
= tmp
->size
;
2497 regf
->blk_tail
->next
= tmp
;
2498 regf
->blk_tail
= tmp
;
2499 if (!regf
->free_space
) regf
->free_space
= tmp
;
2508 * Allocate a unit of space ... and return a pointer as function param
2509 * and the block's offset as a side effect
2511 void *nt_alloc_regf_space(REGF
*regf
, int size
, int *off
)
2517 if (!regf
|| !size
|| !off
) return NULL
;
2519 assert(regf
->blk_head
!= NULL
);
2522 * round up size to include header and then to 8-byte boundary
2524 size
= (size
+ 4 + 7) & ~7;
2527 * Check if there is space, if none, grab a block
2529 if (!regf
->free_space
) {
2530 if (!nt_create_hbin_blk(regf
, REGF_HDR_BLKSIZ
))
2535 * Now, chain down the list of blocks looking for free space
2538 for (blk
= regf
->free_space
; blk
!= NULL
; blk
= blk
->next
) {
2539 if (blk
->free_space
<= size
) {
2540 tmp
= blk
->file_offset
+ blk
->fsp_off
- REGF_HDR_BLKSIZ
;
2541 ret
= blk
->data
+ blk
->fsp_off
;
2542 blk
->free_space
-= size
;
2543 blk
->fsp_off
+= size
;
2545 /* Insert the header */
2546 ((HBIN_SUB_HDR
*)ret
)->dblocksize
= -size
;
2549 * Fix up the free space ptr
2550 * If it is NULL, we fix it up next time
2553 if (!blk
->free_space
)
2554 regf
->free_space
= blk
->next
;
2557 return (((char *)ret
)+4);/* The pointer needs to be to the data struct */
2562 * If we got here, we need to add another block, which might be
2563 * larger than one block -- deal with that later
2565 if (nt_create_hbin_blk(regf
, REGF_HDR_BLKSIZ
)) {
2566 blk
= regf
->free_space
;
2567 tmp
= blk
->file_offset
+ blk
->fsp_off
- REGF_HDR_BLKSIZ
;
2568 ret
= blk
->data
+ blk
->fsp_off
;
2569 blk
->free_space
-= size
;
2570 blk
->fsp_off
+= size
;
2572 /* Insert the header */
2573 ((HBIN_SUB_HDR
*)ret
)->dblocksize
= -size
;
2576 * Fix up the free space ptr
2577 * If it is NULL, we fix it up next time
2580 if (!blk
->free_space
)
2581 regf
->free_space
= blk
->next
;
2584 return (((char *)ret
) + 4);/* The pointer needs to be to the data struct */
2591 * Compute the size of a SID stored ...
2594 unsigned int sid_size(DOM_SID
*sid
)
2600 size
= 8 + (sid
->auths
* sizeof(unsigned int));
2606 * Compute the size of an ACE on disk from its components
2609 unsigned int ace_size(ACE
*ace
)
2615 size
= 8 + sid_size(ace
->trustee
);
2621 * Compute the size of an ACL from its components ...
2623 unsigned int acl_size(ACL
*acl
)
2631 for (i
= 0; i
< acl
->num_aces
; i
++)
2632 size
+= ace_size(acl
->aces
[i
]);
2638 * Compute the size of the sec desc as a self-relative SD
2640 unsigned int sec_desc_size(SEC_DESC
*sd
)
2648 if (sd
->owner
) size
+= sid_size(sd
->owner
);
2649 if (sd
->group
) size
+= sid_size(sd
->group
);
2650 if (sd
->sacl
) size
+= acl_size(sd
->sacl
);
2651 if (sd
->dacl
) size
+= acl_size(sd
->dacl
);
2657 * Store a SID at the location provided
2660 int nt_store_SID(REGF
*regf
, DOM_SID
*sid
, unsigned char *locn
)
2663 unsigned char *p
= locn
;
2665 if (!regf
|| !sid
|| !locn
) return 0;
2668 *p
= sid
->auths
; p
++;
2670 for (i
=0; i
< 6; i
++) {
2671 *p
= sid
->auth
[i
]; p
++;
2674 for (i
=0; i
< sid
->auths
; i
++) {
2675 SIVAL(p
, sid
->sub_auths
[i
]); p
+=4;
2682 int nt_store_ace(REGF
*regf
, ACE
*ace
, unsigned char *locn
)
2685 REG_ACE
*reg_ace
= (REG_ACE
*)locn
;
2688 if (!regf
|| !ace
|| !locn
) return 0;
2690 reg_ace
->type
= ace
->type
;
2691 reg_ace
->flags
= ace
->flags
;
2693 /* Deal with the length when we have stored the SID */
2695 p
= (unsigned char *)®_ace
->perms
;
2697 SIVAL(p
, ace
->perms
); p
+= 4;
2699 size
= nt_store_SID(regf
, ace
->trustee
, p
);
2701 size
+= 8; /* Size of the fixed header */
2703 p
= (unsigned char *)®_ace
->length
;
2711 * Store an ACL at the location provided
2714 int nt_store_acl(REGF
*regf
, ACL
*acl
, unsigned char *locn
)
2717 unsigned char *p
= locn
, *s
;
2719 if (!regf
|| !acl
|| !locn
) return 0;
2722 * Now store the header and then the ACEs ...
2727 p
+= 2; s
= p
; /* Save this for the size field */
2731 SIVAL(p
, acl
->num_aces
);
2735 for (i
= 0; i
< acl
->num_aces
; i
++) {
2736 size
= nt_store_ace(regf
, acl
->aces
[i
], p
);
2746 * Flatten and store the Sec Desc
2747 * Windows lays out the DACL first, but since there is no SACL, it might be
2748 * that first, then the owner, then the group SID. So, we do it that way
2751 unsigned int nt_store_sec_desc(REGF
*regf
, SEC_DESC
*sd
, char *locn
)
2753 REG_SEC_DESC
*rsd
= (REG_SEC_DESC
*)locn
;
2754 unsigned int size
= 0, off
= 0;
2756 if (!regf
|| !sd
|| !locn
) return 0;
2759 * Now, fill in the first two fields, then lay out the various fields
2764 /* Self relative, DACL pres, owner and group not defaulted */
2767 off
= 4 * sizeof(DWORD
) + 4;
2770 size
= nt_store_acl(regf
, sd
->sacl
, (char *)(locn
+ off
));
2771 rsd
->sacl_off
= off
;
2779 rsd
->dacl_off
= off
;
2780 size
= nt_store_acl(regf
, sd
->dacl
, (char *)(locn
+ off
));
2788 /* Now the owner and group SIDs */
2791 rsd
->owner_off
= off
;
2792 size
= nt_store_SID(regf
, sd
->owner
, (char *)(locn
+ off
));
2801 rsd
->group_off
= off
;
2802 size
= nt_store_SID(regf
, sd
->group
, (char *)(locn
+ off
));
2814 * Store the security information
2816 * If it has already been stored, just get its offset from record
2817 * otherwise, store it and record its offset
2820 unsigned int nt_store_security(REGF
*regf
, KEY_SEC_DESC
*sec
)
2823 unsigned int sk_off
;
2826 if (sec
->offset
) return sec
->offset
;
2829 * OK, we don't have this one in the file yet. We must compute the
2830 * size taken by the security descriptor as a self-relative SD, which
2831 * means making one pass over each structure and figuring it out
2834 size
= sec_desc_size(sec
->sec_desc
);
2836 /* Allocate that much space */
2838 sk_hdr
= nt_alloc_regf_space(regf
, size
, &sk_off
);
2839 sec
->sk_hdr
= sk_hdr
;
2841 if (!sk_hdr
) return 0;
2843 /* Now, lay out the sec_desc in the space provided */
2845 sk_hdr
->SK_ID
= REG_SK_ID
;
2848 * We can't deal with the next and prev offset in the SK_HDRs until the
2849 * whole tree has been stored, then we can go and deal with them
2852 sk_hdr
->ref_cnt
= sec
->ref_cnt
;
2853 sk_hdr
->rec_size
= size
; /* Is this correct */
2855 /* Now, lay out the sec_desc */
2857 if (!nt_store_sec_desc(regf
, sec
->sec_desc
, (char *)&sk_hdr
->sec_desc
))
2868 int nt_store_val_list(REGF
*regf
, VAL_LIST
* values
)
2875 * Store a KEY in the file ...
2877 * We store this depth first, and defer storing the lf struct until
2878 * all the sub-keys have been stored.
2880 * We store the NK hdr, any SK header, class name, and VK structure, then
2881 * recurse down the LF structures ...
2883 * We return the offset of the NK struct
2884 * FIXME, FIXME, FIXME: Convert to using SIVAL and SSVAL ...
2886 int nt_store_reg_key(REGF
*regf
, REG_KEY
*key
)
2889 unsigned int nk_off
, sk_off
, val_off
, clsnam_off
, size
;
2891 if (!regf
|| !key
) return 0;
2893 size
= sizeof(NK_HDR
) + strlen(key
->name
) - 1;
2894 nk_hdr
= nt_alloc_regf_space(regf
, size
, &nk_off
);
2895 if (!nk_hdr
) goto error
;
2897 key
->offset
= nk_off
; /* We will need this later */
2900 * Now fill in each field etc ...
2903 nk_hdr
->NK_ID
= REG_NK_ID
;
2904 if (key
->type
== REG_ROOT_KEY
)
2905 nk_hdr
->type
= 0x2C;
2907 nk_hdr
->type
= 0x20;
2909 /* FIXME: Fill in the time of last update */
2911 if (key
->type
!= REG_ROOT_KEY
)
2912 nk_hdr
->own_off
= key
->owner
->offset
;
2915 nk_hdr
->subk_num
= key
->sub_keys
->key_count
;
2918 * Now, process the Sec Desc and then store its offset
2921 sk_off
= nt_store_security(regf
, key
->security
);
2922 nk_hdr
->sk_off
= sk_off
;
2925 * Then, store the val list and store its offset
2928 nk_hdr
->val_cnt
= key
->values
->val_count
;
2929 nk_hdr
->val_off
= nt_store_val_list(regf
, key
->values
);
2932 nk_hdr
->val_off
= -1;
2933 nk_hdr
->val_cnt
= 0;
2937 * Finally, store the subkeys, and their offsets
2945 * Store the registry header ...
2946 * We actually create the registry header block and link it to the chain
2949 REGF_HDR
*nt_get_reg_header(REGF
*regf
)
2951 HBIN_BLK
*tmp
= NULL
;
2953 tmp
= (HBIN_BLK
*)malloc(sizeof(HBIN_BLK
));
2956 bzero(tmp
, sizeof(HBIN_BLK
));
2957 tmp
->type
= REG_OUTBLK_HDR
;
2958 tmp
->size
= REGF_HDR_BLKSIZ
;
2959 tmp
->data
= malloc(REGF_HDR_BLKSIZ
);
2960 if (!tmp
->data
) goto error
;
2962 bzero(tmp
->data
, REGF_HDR_BLKSIZ
); /* Make it pristine, unlike Windows */
2963 regf
->blk_head
= regf
->blk_tail
= tmp
;
2965 return (REGF_HDR
*)tmp
->data
;
2973 * Store the registry in the output file
2974 * We write out the header and then each of the keys etc into the file
2975 * We have to flatten the data structure ...
2977 * The structures are stored in a depth-first fashion, with all records
2978 * aligned on 8-byte boundaries, with sub-keys and values layed down before
2979 * the lists that contain them. SK records are layed down first, however.
2980 * The lf fields are layed down after all sub-keys have been layed down, it
2981 * seems, including the whole tree associated with each sub-key.
2983 int nt_store_registry(REGF
*regf
)
2989 * Get a header ... and partially fill it in ...
2991 reg
= nt_get_reg_header(regf
);
2994 * Store the first key, which will store the whole thing
2996 fkey
= nt_store_reg_key(regf
, regf
->root
);
2999 * At this point we have the registry as a series of blocks, so
3000 * run down that series of blocks and save them ...
3003 if (!regf
->outfile_name
) {
3004 fprintf(stderr
, "Cannot write file without a name!\n");
3008 if ((fd
= open(regf
->outfile_name
, O_WRONLY
, 0666)) < 0) {
3009 fprintf(stderr
, "Unable to create file %s: %s\n", regf
->outfile_name
,
3018 * Routines to parse a REGEDIT4 file
3020 * The file consists of:
3027 * [cmd:]name=type:value
3029 * cmd = a|d|c|add|delete|change|as|ds|cs
3031 * There can be more than one key-path and value-spec.
3033 * Since we want to support more than one type of file format, we
3034 * construct a command-file structure that keeps info about the command file
3037 #define FMT_UNREC -1
3038 #define FMT_REGEDIT4 0
3039 #define FMT_EDITREG1_1 1
3041 #define FMT_STRING_REGEDIT4 "REGEDIT4"
3042 #define FMT_STRING_EDITREG1_0 "EDITREG1.0"
3045 #define CMD_ADD_KEY 1
3046 #define CMD_DEL_KEY 2
3051 typedef struct val_spec_list
{
3052 struct val_spec_list
*next
;
3055 char *val
; /* Kept as a char string, really? */
3058 typedef struct command_s
{
3062 VAL_SPEC_LIST
*val_spec_list
, *val_spec_last
;
3065 typedef struct cmd_line
{
3070 void free_val_spec_list(VAL_SPEC_LIST
*vl
)
3073 if (vl
->name
) free(vl
->name
);
3074 if (vl
->val
) free(vl
->val
);
3080 * Some routines to handle lines of info in the command files
3082 void skip_to_eol(int fd
)
3087 while ((rc
= read(fd
, &ch
, 1)) == 1) {
3088 if (ch
== 0x0A) return;
3091 fprintf(stderr
, "Could not read file descriptor: %d, %s\n",
3092 fd
, strerror(errno
));
3097 void free_cmd(CMD
*cmd
)
3101 while (cmd
->val_spec_list
) {
3104 tmp
= cmd
->val_spec_list
;
3105 cmd
->val_spec_list
= tmp
->next
;
3113 void free_cmd_line(CMD_LINE
*cmd_line
)
3116 if (cmd_line
->line
) free(cmd_line
->line
);
3121 void print_line(struct cmd_line
*cl
)
3127 if ((pl
= malloc(cl
->line_len
+ 1)) == NULL
) {
3128 fprintf(stderr
, "Unable to allocate space to print line: %s\n",
3133 strncpy(pl
, cl
->line
, cl
->line_len
);
3134 pl
[cl
->line_len
] = 0;
3136 fprintf(stdout
, "%s\n", pl
);
3140 #define INIT_ALLOC 10
3143 * Read a line from the input file.
3144 * NULL returned when EOF and no chars read
3145 * Otherwise we return a cmd_line *
3146 * Exit if other errors
3148 struct cmd_line
*get_cmd_line(int fd
)
3150 struct cmd_line
*cl
= (CMD_LINE
*)malloc(sizeof(CMD_LINE
));
3155 fprintf(stderr
, "Unable to allocate structure for command line: %s\n",
3160 cl
->len
= INIT_ALLOC
;
3163 * Allocate some space for the line. We extend later if needed.
3166 if ((cl
->line
= (char *)malloc(INIT_ALLOC
)) == NULL
) {
3167 fprintf(stderr
, "Unable to allocate initial space for line: %s\n",
3173 * Now read in the chars to EOL. Don't store the EOL in the
3174 * line. What about CR?
3177 while ((rc
= read(fd
, &ch
, 1)) == 1 && ch
!= '\n') {
3178 if (ch
== '\r') continue; /* skip CR */
3181 * Allocate some more memory
3183 if ((cl
->line
= realloc(cl
->line
, cl
->len
+ INIT_ALLOC
)) == NULL
) {
3184 fprintf(stderr
, "Unable to realloc space for line: %s\n",
3188 cl
->len
+= INIT_ALLOC
;
3194 /* read 0 and we were at loc'n 0, return NULL */
3195 if (rc
== 0 && i
== 0) {
3207 * parse_value: parse out a value. We pull it apart as:
3209 * <value> ::= <value-name>=<type>:<value-string>
3211 * <value-name> ::= char-string-without-spaces | '"' char-string '"'
3213 * If it parsed OK, return the <value-name> as a string, and the
3214 * value type and value-string in parameters.
3216 * The value name can be empty. There can only be one empty name in
3217 * a list of values. A value of - removes the value entirely.
3220 char *dup_str(char *s
, int len
)
3223 nstr
= (char *)malloc(len
+ 1);
3225 memcpy(nstr
, s
, len
);
3231 char *parse_name(char *nstr
)
3233 int len
= 0, start
= 0;
3234 if (!nstr
) return NULL
;
3238 while (len
&& nstr
[len
- 1] == ' ') len
--;
3240 nstr
[len
] = 0; /* Trim any spaces ... if there were none, doesn't matter */
3243 * Beginning and end should be '"' or neither should be so
3245 if ((nstr
[0] == '"' && nstr
[len
- 1] != '"') ||
3246 (nstr
[0] != '"' && nstr
[len
- 1] == '"'))
3249 if (nstr
[0] == '"') {
3254 return dup_str(&nstr
[start
], len
);
3257 int parse_value_type(char *tstr
)
3259 int len
= strlen(tstr
);
3261 while (len
&& tstr
[len
- 1] == ' ') len
--;
3264 if (strcmp(tstr
, "REG_DWORD") == 0)
3265 return REG_TYPE_DWORD
;
3266 else if (strcmp(tstr
, "dword") == 0)
3267 return REG_TYPE_DWORD
;
3268 else if (strcmp(tstr
, "REG_EXPAND_SZ") == 0)
3269 return REG_TYPE_EXPANDSZ
;
3270 else if (strcmp(tstr
, "REG_BIN") == 0)
3271 return REG_TYPE_BIN
;
3272 else if (strcmp(tstr
, "REG_SZ") == 0)
3273 return REG_TYPE_REGSZ
;
3274 else if (strcmp(tstr
, "REG_MULTI_SZ") == 0)
3275 return REG_TYPE_MULTISZ
;
3276 else if (strcmp(tstr
, "-") == 0)
3277 return REG_TYPE_DELETE
;
3282 char *parse_val_str(char *vstr
)
3285 return dup_str(vstr
, strlen(vstr
));
3289 char *parse_value(struct cmd_line
*cl
, int *vtype
, char **val
)
3291 char *p1
= NULL
, *p2
= NULL
, *nstr
= NULL
, *tstr
= NULL
, *vstr
= NULL
;
3293 if (!cl
|| !vtype
|| !val
) return NULL
;
3294 if (!cl
->line_len
) return NULL
;
3296 p1
= dup_str(cl
->line
, cl
->line_len
);
3297 /* FIXME: Better return codes etc ... */
3298 if (!p1
) return NULL
;
3299 p2
= strchr(p1
, '=');
3300 if (!p2
) return NULL
;
3302 *p2
= 0; p2
++; /* Split into two strings at p2 */
3304 /* Now, parse the name ... */
3306 nstr
= parse_name(p1
);
3307 if (!nstr
) goto error
;
3309 /* Now, split the remainder and parse on type and val ... */
3312 while (*tstr
== ' ') tstr
++; /* Skip leading white space */
3313 p2
= strchr(p2
, ':');
3316 *p2
= 0; p2
++; /* split on the : */
3319 *vtype
= parse_value_type(tstr
);
3321 if (!vtype
) goto error
;
3323 if (!p2
|| !*p2
) return nstr
;
3325 /* Now, parse the value string. It should return a newly malloc'd string */
3327 while (*p2
== ' ') p2
++; /* Skip leading space */
3328 vstr
= parse_val_str(p2
);
3330 if (!vstr
) goto error
;
3338 if (nstr
) free(nstr
);
3339 if (vstr
) free(vstr
);
3344 * Parse out a key. Look for a correctly formatted key [...]
3345 * and whether it is a delete or add? A delete is signalled
3346 * by a - in front of the key.
3347 * Assumes that there are no leading and trailing spaces
3350 char *parse_key(struct cmd_line
*cl
, int *cmd
)
3355 if (cl
->line
[0] != '[' ||
3356 cl
->line
[cl
->line_len
- 1] != ']') return NULL
;
3357 if (cl
->line_len
== 2) return NULL
;
3359 if (cl
->line
[1] == '-') {
3360 if (cl
->line_len
== 3) return NULL
;
3364 tmp
= malloc(cl
->line_len
- 1 - start
+ 1);
3365 if (!tmp
) return tmp
; /* Bail out on no mem ... FIXME */
3366 strncpy(tmp
, &cl
->line
[start
], cl
->line_len
- 1 - start
);
3367 tmp
[cl
->line_len
- 1 - start
] = 0;
3372 * Parse a line to determine if we have a key or a value
3373 * We only check for key or val ...
3376 int parse_line(struct cmd_line
*cl
)
3379 if (!cl
|| cl
->len
== 0) return 0;
3381 if (cl
->line
[0] == '[') /* No further checking for now */
3388 * We seek to offset 0, read in the required number of bytes,
3389 * and compare to the correct value.
3390 * We then seek back to the original location
3392 int regedit4_file_type(int fd
)
3397 cur_ofs
= lseek(fd
, 0, SEEK_CUR
); /* Get current offset */
3399 fprintf(stderr
, "Unable to get current offset: %s\n", strerror(errno
));
3400 exit(1); /* FIXME */
3404 lseek(fd
, 0, SEEK_SET
);
3407 if (read(fd
, desc
, 8) < 8) {
3408 fprintf(stderr
, "Unable to read command file format\n");
3409 exit(2); /* FIXME */
3414 if (strcmp(desc
, FMT_STRING_REGEDIT4
) == 0) {
3416 lseek(fd
, cur_ofs
, SEEK_SET
);
3421 return FMT_REGEDIT4
;
3428 * Run though the data in the line and strip anything after a comment
3431 void strip_comment(struct cmd_line
*cl
)
3437 for (i
= 0; i
< cl
->line_len
; i
++) {
3438 if (cl
->line
[i
] == ';') {
3446 * trim leading space
3449 void trim_leading_spaces(struct cmd_line
*cl
)
3455 for (i
= 0; i
< cl
->line_len
; i
++) {
3456 if (cl
->line
[i
] != ' '){
3457 if (i
) memcpy(cl
->line
, &cl
->line
[i
], cl
->line_len
- i
);
3464 * trim trailing spaces
3466 void trim_trailing_spaces(struct cmd_line
*cl
)
3472 for (i
= cl
->line_len
; i
== 0; i
--) {
3473 if (cl
->line
[i
-1] != ' ' &&
3474 cl
->line
[i
-1] != '\t') {
3481 * Get a command ... This consists of possibly multiple lines:
3484 * possibly Empty line
3486 * value ::= <value-name>=<value-type>':'<value-string>
3487 * <value-name> is some path, possibly enclosed in quotes ...
3488 * We alctually look for the next key to terminate a previous key
3489 * if <value-type> == '-', then it is a delete type.
3491 CMD
*regedit4_get_cmd(int fd
)
3493 struct command_s
*cmd
= NULL
;
3494 struct cmd_line
*cl
= NULL
;
3495 struct val_spec_list
*vl
= NULL
;
3497 if ((cmd
= (struct command_s
*)malloc(sizeof(struct command_s
))) == NULL
) {
3498 fprintf(stderr
, "Unable to malloc space for command: %s\n",
3503 cmd
->cmd
= CMD_NONE
;
3506 cmd
->val_spec_list
= cmd
->val_spec_last
= NULL
;
3507 while ((cl
= get_cmd_line(fd
))) {
3510 * If it is an empty command line, and we already have a key
3511 * then exit from here ... FIXME: Clean up the parser
3514 if (cl
->line_len
== 0 && cmd
->key
) {
3519 strip_comment(cl
); /* remove anything beyond a comment char */
3520 trim_trailing_spaces(cl
);
3521 trim_leading_spaces(cl
);
3523 if (cl
->line_len
== 0) { /* An empty line */
3526 else { /* Else, non-empty ... */
3528 * Parse out the bits ...
3530 switch (parse_line(cl
)) {
3532 if ((cmd
->key
= parse_key(cl
, &cmd
->cmd
)) == NULL
) {
3533 fprintf(stderr
, "Error parsing key from line: ");
3535 fprintf(stderr
, "\n");
3541 * We need to add the value stuff to the list
3542 * There could be a \ on the end which we need to
3543 * handle at some time
3545 vl
= (struct val_spec_list
*)malloc(sizeof(struct val_spec_list
));
3546 if (!vl
) goto error
;
3549 vl
->name
= parse_value(cl
, &vl
->type
, &vl
->val
);
3550 if (!vl
->name
) goto error
;
3551 if (cmd
->val_spec_list
== NULL
) {
3552 cmd
->val_spec_list
= cmd
->val_spec_last
= vl
;
3555 cmd
->val_spec_last
->next
= vl
;
3556 cmd
->val_spec_last
= vl
;
3562 fprintf(stderr
, "Unrecognized line in command file: \n");
3569 if (!cmd
->cmd
) goto error
; /* End of file ... */
3575 if (cmd
) free_cmd(cmd
);
3579 int regedit4_exec_cmd(CMD
*cmd
)
3585 int editreg_1_0_file_type(int fd
)
3590 cur_ofs
= lseek(fd
, 0, SEEK_CUR
); /* Get current offset */
3592 fprintf(stderr
, "Unable to get current offset: %s\n", strerror(errno
));
3593 exit(1); /* FIXME */
3597 lseek(fd
, 0, SEEK_SET
);
3600 if (read(fd
, desc
, 10) < 10) {
3601 fprintf(stderr
, "Unable to read command file format\n");
3602 exit(2); /* FIXME */
3607 if (strcmp(desc
, FMT_STRING_EDITREG1_0
) == 0) {
3608 lseek(fd
, cur_ofs
, SEEK_SET
);
3609 return FMT_REGEDIT4
;
3615 CMD
*editreg_1_0_get_cmd(int fd
)
3620 int editreg_1_0_exec_cmd(CMD
*cmd
)
3626 typedef struct command_ops_s
{
3628 int (*file_type
)(int fd
);
3629 CMD
*(*get_cmd
)(int fd
);
3630 int (*exec_cmd
)(CMD
*cmd
);
3633 CMD_OPS default_cmd_ops
[] = {
3634 {0, regedit4_file_type
, regedit4_get_cmd
, regedit4_exec_cmd
},
3635 {1, editreg_1_0_file_type
, editreg_1_0_get_cmd
, editreg_1_0_exec_cmd
},
3636 {-1, NULL
, NULL
, NULL
}
3639 typedef struct command_file_s
{
3646 * Create a new command file structure
3649 CMD_FILE
*cmd_file_create(char *file
)
3656 * Let's check if the file exists ...
3657 * No use creating the cmd_file structure if the file does not exist
3660 if (stat(file
, &sbuf
) < 0) { /* Not able to access file */
3665 tmp
= (CMD_FILE
*)malloc(sizeof(CMD_FILE
));
3671 * Let's fill in some of the fields;
3674 tmp
->name
= strdup(file
);
3676 if ((tmp
->fd
= open(file
, O_RDONLY
, 666)) < 0) {
3682 * Now, try to find the format by indexing through the table
3684 while (default_cmd_ops
[i
].type
!= -1) {
3685 if ((tmp
->type
= default_cmd_ops
[i
].file_type(tmp
->fd
)) >= 0) {
3686 tmp
->cmd_ops
= default_cmd_ops
[i
];
3693 * If we got here, return NULL, as we could not figure out the type
3696 * What about errors?
3704 * Extract commands from the command file, and execute them.
3705 * We pass a table of command callbacks for that
3709 * Main code from here on ...
3713 * key print function here ...
3716 int print_key(const char *path
, char *name
, char *class_name
, int root
,
3717 int terminal
, int vals
)
3720 if (full_print
|| terminal
) fprintf(stdout
, "[%s%s]\n", path
, name
);
3726 * Sec Desc print functions
3729 void print_type(unsigned char type
)
3733 fprintf(stdout
, " ALLOW");
3736 fprintf(stdout
, " DENY");
3739 fprintf(stdout
, " AUDIT");
3742 fprintf(stdout
, " ALARM");
3745 fprintf(stdout
, "ALLOW CPD");
3748 fprintf(stdout
, "OBJ ALLOW");
3751 fprintf(stdout
, " OBJ DENY");
3753 fprintf(stdout
, " UNKNOWN");
3758 void print_flags(unsigned char flags
)
3760 char flg_output
[21];
3765 fprintf(stdout
, " ");
3769 if (some
) strcat(flg_output
, ",");
3771 strcat(flg_output
, "OI");
3774 if (some
) strcat(flg_output
, ",");
3776 strcat(flg_output
, "CI");
3779 if (some
) strcat(flg_output
, ",");
3781 strcat(flg_output
, "NP");
3784 if (some
) strcat(flg_output
, ",");
3786 strcat(flg_output
, "IO");
3789 if (some
) strcat(flg_output
, ",");
3791 strcat(flg_output
, "IA");
3794 if (some
) strcat(flg_output
, ",");
3796 strcat(flg_output
, "VI");
3798 fprintf(stdout
, " %s", flg_output
);
3801 void print_perms(int perms
)
3803 fprintf(stdout
, " %8X", perms
);
3806 void print_sid(DOM_SID
*sid
)
3808 int i
, comps
= sid
->auths
;
3809 fprintf(stdout
, "S-%u-%u", sid
->ver
, sid
->auth
[5]);
3811 for (i
= 0; i
< comps
; i
++) {
3813 fprintf(stdout
, "-%u", sid
->sub_auths
[i
]);
3816 fprintf(stdout
, "\n");
3819 void print_acl(ACL
*acl
, char *prefix
)
3823 for (i
= 0; i
< acl
->num_aces
; i
++) {
3824 fprintf(stdout
, ";;%s", prefix
);
3825 print_type(acl
->aces
[i
]->type
);
3826 print_flags(acl
->aces
[i
]->flags
);
3827 print_perms(acl
->aces
[i
]->perms
);
3828 fprintf(stdout
, " ");
3829 print_sid(acl
->aces
[i
]->trustee
);
3833 int print_sec(SEC_DESC
*sec_desc
)
3835 if (!print_security
) return 1;
3836 fprintf(stdout
, ";; SECURITY\n");
3837 fprintf(stdout
, ";; Owner: ");
3838 print_sid(sec_desc
->owner
);
3839 fprintf(stdout
, ";; Group: ");
3840 print_sid(sec_desc
->group
);
3841 if (sec_desc
->sacl
) {
3842 fprintf(stdout
, ";; SACL:\n");
3843 print_acl(sec_desc
->sacl
, " ");
3845 if (sec_desc
->dacl
) {
3846 fprintf(stdout
, ";; DACL:\n");
3847 print_acl(sec_desc
->dacl
, " ");
3853 * Value print function here ...
3855 int print_val(const char *path
, char *val_name
, int val_type
, int data_len
,
3856 void *data_blk
, int terminal
, int first
, int last
)
3858 char data_asc
[1024];
3860 bzero(data_asc
, sizeof(data_asc
));
3861 if (!terminal
&& first
)
3862 fprintf(stdout
, "%s\n", path
);
3863 data_to_ascii((unsigned char *)data_blk
, data_len
, val_type
, data_asc
,
3864 sizeof(data_asc
) - 1);
3865 fprintf(stdout
, " %s = %s : %s\n", (val_name
?val_name
:"<No Name>"),
3866 val_to_str(val_type
, reg_type_names
), data_asc
);
3872 fprintf(stderr
, "Usage: editreg [-f] [-v] [-p] [-k] [-s] [-c <command-file>] <registryfile>\n");
3873 fprintf(stderr
, "Version: 0.1\n\n");
3874 fprintf(stderr
, "\n\t-v\t sets verbose mode");
3875 fprintf(stderr
, "\n\t-f\t sets full print mode where non-terminals are printed");
3876 fprintf(stderr
, "\n\t-p\t prints the registry");
3877 fprintf(stderr
, "\n\t-s\t prints security descriptors");
3878 fprintf(stderr
, "\n\t-c <command-file>\t specifies a command file");
3879 fprintf(stderr
, "\n");
3882 int main(int argc
, char *argv
[])
3885 extern char *optarg
;
3887 int opt
, print_keys
= 0;
3888 int regf_opt
= 1; /* Command name */
3889 int commands
= 0, modified
= 0;
3890 char *cmd_file_name
= NULL
;
3891 char *out_file_name
= NULL
;
3892 CMD_FILE
*cmd_file
= NULL
;
3901 * Now, process the arguments
3904 while ((opt
= getopt(argc
, argv
, "fspvko:O:c:")) != EOF
) {
3908 cmd_file_name
= optarg
;
3918 out_file_name
= optarg
;
3923 def_owner_sid_str
= strdup(optarg
);
3925 if (!string_to_sid(&lsid
, def_owner_sid_str
)) {
3926 fprintf(stderr
, "Default Owner SID: %s is incorrectly formatted\n",
3928 free(def_owner_sid_str
);
3929 def_owner_sid_str
= NULL
;
3932 nt_delete_sid(lsid
);
3963 * We only want to complain about the lack of a default owner SID if
3964 * we need one. This approximates that need
3966 if (!def_owner_sid_str
) {
3967 def_owner_sid_str
= "S-1-5-21-1-2-3-4";
3968 if (out_file_name
|| verbose
)
3969 fprintf(stderr
, "Warning, default owner SID not set. Setting to %s\n",
3973 if ((regf
= nt_create_regf()) == NULL
) {
3974 fprintf(stderr
, "Could not create registry object: %s\n", strerror(errno
));
3978 if (regf_opt
< argc
) { /* We have a registry file */
3979 if (!nt_set_regf_input_file(regf
, argv
[regf_opt
])) {
3980 fprintf(stderr
, "Could not set name of registry file: %s, %s\n",
3981 argv
[regf_opt
], strerror(errno
));
3985 /* Now, open it, and bring it into memory :-) */
3987 if (nt_load_registry(regf
) < 0) {
3988 fprintf(stderr
, "Could not load registry: %s\n", argv
[1]);
3993 if (out_file_name
) {
3994 if (!nt_set_regf_output_file(regf
, out_file_name
)) {
3995 fprintf(stderr
, "Could not set name of output registry file: %s, %s\n",
3996 out_file_name
, strerror(errno
));
4005 cmd_file
= cmd_file_create(cmd_file_name
);
4007 while ((cmd
= cmd_file
->cmd_ops
.get_cmd(cmd_file
->fd
)) != NULL
) {
4010 * Now, apply the requests to the tree ...
4014 REG_KEY
*tmp
= NULL
;
4016 tmp
= nt_find_key_by_name(regf
->root
, cmd
->key
);
4018 /* If we found it, apply the other bits, else create such a key */
4021 tmp
= nt_add_reg_key(regf
, cmd
->key
, True
);
4025 while (cmd
->val_count
) {
4026 VAL_SPEC_LIST
*val
= cmd
->val_spec_list
;
4027 VAL_KEY
*reg_val
= NULL
;
4029 if (val
->type
== REG_TYPE_DELETE
) {
4030 reg_val
= nt_delete_reg_value(tmp
, val
-> name
);
4031 if (reg_val
) nt_delete_val_key(reg_val
);
4035 reg_val
= nt_add_reg_value(tmp
, val
->name
, val
->type
,
4040 cmd
->val_spec_list
= val
->next
;
4041 free_val_spec_list(val
);
4050 * Any value does not matter ...
4051 * Find the key if it exists, and delete it ...
4054 nt_delete_key_by_name(regf
, cmd
->key
);
4063 * At this point, we should have a registry in memory and should be able
4064 * to iterate over it.
4068 nt_key_iterator(regf
, regf
->root
, 0, "", print_key
, print_sec
, print_val
);
4072 * If there was an out_file_name and the tree was modified, print it
4074 if (modified
&& out_file_name
)
4075 if (!nt_store_registry(regf
)) {
4076 fprintf(stdout
, "Error storing registry\n");