3f0c10e8954e4f3ca5e49ef29508ca8c0ca15e46
[sfrench/samba-autobuild/.git] / source3 / include / regfio.h
1 /*
2  * Unix SMB/CIFS implementation.
3  * Windows NT registry I/O library
4  * Copyright (c) Gerald (Jerry) Carter               2005
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /************************************************************
22  * Most of this information was obtained from 
23  * http://www.wednesday.demon.co.uk/dosreg.html
24  * Thanks Nigel!
25  ***********************************************************/
26
27
28 #ifndef _REGFIO_H
29 #define _REGFIO_H
30
31 /* Macros */
32  
33 #define REGF_BLOCKSIZE          0x1000
34 #define REGF_ALLOC_BLOCK        0x1000
35
36 /* header sizes for various records */
37
38 #define REGF_HDR_SIZE           4
39 #define HBIN_HDR_SIZE           4
40 #define HBIN_HEADER_REC_SIZE    0x24
41 #define REC_HDR_SIZE            2
42
43 #define REGF_OFFSET_NONE        0xffffffff
44
45 /* Flags for the vk records */
46
47 #define VK_FLAG_NAME_PRESENT    0x0001
48 #define VK_DATA_IN_OFFSET       0x80000000
49
50 /* NK record macros */
51
52 #define NK_TYPE_LINKKEY         0x0010
53 #define NK_TYPE_NORMALKEY       0x0020
54 #define NK_TYPE_ROOTKEY         0x002c
55
56 #define HBIN_STORE_REF(x, y)    { x->hbin = y; y->ref_count++ };
57 #define HBIN_REMOVE_REF(x, y)   { x->hbin = NULL; y->ref_count-- /* if the count == 0; we can clean up */ };
58
59
60 /* HBIN block */
61 struct regf_hbin;
62 typedef struct regf_hbin {
63         struct regf_hbin *prev, *next;
64         uint32 file_off;                /* my offset in the registry file */
65         uint32 free_off;                /* offset to free space within the hbin record */
66         uint32 free_size;               /* amount of data left in the block */
67         int ref_count;                  /* how many active records are pointing to this block (not used currently) */
68         
69         char   header[HBIN_HDR_SIZE];   /* "hbin" */
70         uint32 first_hbin_off;          /* offset from first hbin block */
71         uint32 block_size;              /* block size of this blockually a multiple of 4096Kb) */
72
73         prs_struct ps;                  /* data */
74
75         BOOL dirty;                     /* has this hbin block been modified? */
76 } REGF_HBIN;
77
78 /* ??? List -- list of key offsets and hashed names for consistency */
79
80 typedef struct {
81         uint32 nk_off;
82         uint8 keycheck[sizeof(uint32)];
83         char *fullname;
84 } REGF_HASH_REC;
85
86 typedef struct {
87         REGF_HBIN *hbin;        /* pointer to HBIN record (in memory) containing this nk record */
88         uint32 hbin_off;        /* offset from beginning of this hbin block */
89         uint32 rec_size;        /* ((start_offset - end_offset) & 0xfffffff8) */
90
91         char header[REC_HDR_SIZE];
92         uint16 num_keys;
93         REGF_HASH_REC *hashes;
94 } REGF_LF_REC;
95
96 /* Key Value */
97
98 typedef struct {
99         REGF_HBIN *hbin;        /* pointer to HBIN record (in memory) containing this nk record */
100         uint32 hbin_off;        /* offset from beginning of this hbin block */
101         uint32 rec_size;        /* ((start_offset - end_offset) & 0xfffffff8) */
102         uint32 rec_off;         /* offset stored in the value list */
103         
104         char header[REC_HDR_SIZE];
105         char *valuename;
106         uint32 data_size;
107         uint32 data_off;
108         uint8  *data;
109         uint32 type;
110         uint16 flag;
111 } REGF_VK_REC;
112
113
114 /* Key Security */
115 struct _regf_sk_rec;
116
117 typedef struct _regf_sk_rec {
118         struct _regf_sk_rec *next, *prev;
119         REGF_HBIN *hbin;        /* pointer to HBIN record (in memory) containing this nk record */
120         uint32 hbin_off;        /* offset from beginning of this hbin block */
121         uint32 rec_size;        /* ((start_offset - end_offset) & 0xfffffff8) */
122
123         uint32 sk_off;          /* offset parsed from NK record used as a key
124                                    to lookup reference to this SK record */
125
126         char header[REC_HDR_SIZE];
127         uint32 prev_sk_off;
128         uint32 next_sk_off;
129         uint32 ref_count;
130         uint32 size;
131         SEC_DESC *sec_desc;
132 } REGF_SK_REC;
133
134 /* Key Name */ 
135
136 typedef struct {
137         REGF_HBIN *hbin;        /* pointer to HBIN record (in memory) containing this nk record */
138         uint32 hbin_off;        /* offset from beginning of this hbin block */
139         uint32 subkey_index;    /* index to next subkey record to return */
140         uint32 rec_size;        /* ((start_offset - end_offset) & 0xfffffff8) */
141         
142         /* header information */
143         
144         char header[REC_HDR_SIZE];
145         uint16 key_type;
146         NTTIME mtime;
147         uint32 parent_off;      /* back pointer in registry hive */
148         uint32 classname_off;   
149         char *classname;
150         char *keyname;
151
152         /* max lengths */
153
154         uint32 max_bytes_subkeyname;            /* max subkey name * 2 */
155         uint32 max_bytes_subkeyclassname;       /* max subkey classname length (as if) */
156         uint32 max_bytes_valuename;             /* max valuename * 2 */
157         uint32 max_bytes_value;                 /* max value data size */
158
159         /* unknowns */
160
161         uint32 unk_index;                       /* nigel says run time index ? */
162         
163         /* children */
164         
165         uint32 num_subkeys;
166         uint32 subkeys_off;     /* hash records that point to NK records */     
167         uint32 num_values;
168         uint32 values_off;      /* value lists which point to VK records */
169         uint32 sk_off;          /* offset to SK record */
170         
171         /* link in the other records here */
172         
173         REGF_LF_REC subkeys;
174         REGF_VK_REC *values;
175         REGF_SK_REC *sec_desc;
176         
177 } REGF_NK_REC;
178
179 /* REGF block */
180  
181 typedef struct {
182         /* run time information */
183
184         int fd;                         /* file descriptor */
185         int open_flags;                 /* flags passed to the open() call */
186         TALLOC_CTX *mem_ctx;            /* memory context for run-time file access information */
187         REGF_HBIN *block_list;          /* list of open hbin blocks */
188
189         /* file format information */
190
191         char   header[REGF_HDR_SIZE];   /* "regf" */
192         uint32 data_offset;             /* offset to record in the first (or any?) hbin block */
193         uint32 last_block;              /* offset to last hbin block in file */
194         uint32 checksum;                /* XOR of bytes 0x0000 - 0x01FB */
195         NTTIME mtime;
196         
197         REGF_SK_REC *sec_desc_list;     /* list of security descriptors referenced by NK records */
198
199         /* unknowns used to simply writing */
200         
201         uint32 unknown1;
202         uint32 unknown2;
203         uint32 unknown3;
204         uint32 unknown4;
205         uint32 unknown5;
206         uint32 unknown6;
207         
208 } REGF_FILE;
209
210 /* Function Declarations */
211  
212 REGF_FILE*    regfio_open( const char *filename, int flags, int mode );
213 int           regfio_close( REGF_FILE *r );
214
215 REGF_NK_REC*  regfio_rootkey( REGF_FILE *file );
216 REGF_NK_REC*  regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk );
217 REGF_NK_REC*  regfio_write_key ( REGF_FILE *file, const char *name,
218                                  REGVAL_CTR *values, REGSUBKEY_CTR *subkeys,
219                                  SEC_DESC *sec_desc, REGF_NK_REC *parent );
220
221
222 #endif  /* _REGFIO_H */
223