hivex: Small updates to the documentation.
[libguestfs.git] / hivex / hivex.pod
1 =encoding utf8
2
3 =head1 NAME
4
5 hivex - Windows Registry "hive" extraction library
6
7 =head1 SYNOPSIS
8
9  hive_h *hivex_open (const char *filename, int flags);
10  int hivex_close (hive_h *h);
11
12 =head1 DESCRIPTION
13
14 libhivex is a library for extracting the contents of Windows Registry
15 "hive" files.  It is designed to be secure against buggy or malicious
16 registry files, and to have limited functionality (writing or
17 modifying these files is not in the scope of this library).
18
19 Unlike many other tools in this area, it doesn't use the textual .REG
20 format for output, because parsing that is as much trouble as parsing
21 the original binary format.  Instead it makes the file available
22 through a C API, or there is a separate program to export the hive as
23 XML (see L<hivexml(1)>), or to get individual keys (see
24 L<hivexget(1)>).
25
26 =head2 OPENING AND CLOSING A HIVE
27
28 =over 4
29
30 =item hive_h *hivex_open (const char *filename, int flags);
31
32 Opens the hive named C<filename> for reading.
33
34 Flags is an ORed list of the open flags (or C<0> if you don't
35 want to pass any flags).  Currently the only
36 flags defined are:
37
38 =over 4
39
40 =item HIVEX_OPEN_VERBOSE
41
42 Verbose messages.
43
44 =item HIVEX_OPEN_DEBUG
45
46 Very verbose messages, suitable for debugging problems in the library
47 itself.
48
49 This is also selected if the C<HIVEX_DEBUG> environment variable
50 is set to 1.
51
52 =back
53
54 C<hivex_open> returns a hive handle.  On error this returns NULL and
55 sets C<errno> to indicate the error.
56
57 =item int hivex_close (hive_h *h);
58
59 Close a hive handle and free all associated resources.
60
61 Returns 0 on success.  On error this returns -1 and sets errno.
62
63 =back
64
65 =head2 NAVIGATING THE TREE OF HIVE SUBKEYS
66
67 =over 4
68
69 =item hive_node_h
70
71 This is a node handle, an integer but opaque outside the library.
72 Valid node handles cannot be 0.  The library returns 0 in some
73 situations to indicate an error.
74
75 =item hive_node_h hivex_root (hive_h *h);
76
77 Return root node of the hive.  All valid registries must contain
78 a root node.
79
80 On error this returns 0 and sets errno.
81
82 =item char *hivex_node_name (hive_h *h, hive_node_h node);
83
84 Return the name of the node.  The name is reencoded as UTF-8
85 and returned as a C string.
86
87 The string should be freed by the caller when it is no longer needed.
88
89 Note that the name of the root node is a dummy, such as
90 C<$$$PROTO.HIV> (other names are possible: it seems to depend on the
91 tool or program that created the hive in the first place).  You can
92 only know the "real" name of the root node by knowing which registry
93 file this hive originally comes from, which is knowledge that is
94 outside the scope of this library.
95
96 On error this returns NULL and sets errno.
97
98 =item hive_node_h *hivex_node_children (hive_h *h, hive_node_h node);
99
100 Return a 0-terminated array of nodes which are the subkeys
101 (children) of C<node>.
102
103 The array should be freed by the caller when it is no longer needed.
104
105 On error this returns NULL and sets errno.
106
107 =item hive_node_h hivex_node_get_child (hive_h *h, hive_node_h node, const char *name);
108
109 Return the child of node with the name C<name>, if it exists.
110
111 The name is matched case insensitively.
112
113 If the child node does not exist, this returns 0 without
114 setting errno.
115
116 On error this returns 0 and sets errno.
117
118 =item hive_node_h hivex_node_parent (hive_h *h, hive_node_h node);
119
120 Return the parent of C<node>.
121
122 On error this returns 0 and sets errno.
123
124 The parent pointer of the root node in registry files that we
125 have examined seems to be invalid, and so this function will
126 return an error if called on the root node.
127
128 =back
129
130 =head2 GETTING VALUES AT A NODE
131
132 The enum below describes the possible types for the value(s)
133 stored at each node.
134
135  enum hive_type {
136    hive_t_none = 0,
137    hive_t_string = 1,
138    hive_t_expand_string = 2,
139    hive_t_binary = 3,
140    hive_t_dword = 4,
141    hive_t_dword_be = 5,
142    hive_t_link = 6,
143    hive_t_multiple_strings = 7,
144    hive_t_resource_list = 8,
145    hive_t_full_resource_description = 9,
146    hive_t_resource_requirements_list = 10,
147    hive_t_qword = 11
148  };
149
150 =over 4
151
152 =item hive_value_h
153
154 This is a value handle, an integer but opaque outside the library.
155 Valid value handles cannot be 0.  The library returns 0 in some
156 situations to indicate an error.
157
158 =item hive_value_h *hivex_node_values (hive_h *h, hive_node_h node);
159
160 Return the 0-terminated array of (key, value) pairs attached to
161 this node.
162
163 The array should be freed by the caller when it is no longer needed.
164
165 On error this returns NULL and sets errno.
166
167 =item hive_value_h hivex_node_get_value (hive_h *h, hive_node_h node, const char *key);
168
169 Return the value attached to this node which has the name C<key>,
170 if it exists.
171
172 The key name is matched case insensitively.
173
174 Note that to get the default key, you should pass the empty
175 string C<""> here.  The default key is often written C<"@">, but
176 inside hives that has no meaning and won't give you the
177 default key.
178
179 If no such key exists, this returns 0 and does not set errno.
180
181 On error this returns 0 and sets errno.
182
183 =item char *hivex_value_key (hive_h *h, hive_value_h value);
184
185 Return the key (name) of a (key, value) pair.  The name
186 is reencoded as UTF-8 and returned as a C string.
187
188 The string should be freed by the caller when it is no longer needed.
189
190 Note that this function can return a zero-length string.  In the
191 context of Windows Registries, this means that this value is the
192 default key for this node in the tree.  This is usually written
193 as C<"@">.
194
195 On error this returns NULL and sets errno.
196
197 =item int hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len);
198
199 Return the data type and length of the value in this (key, value)
200 pair.  See also C<hivex_value_value> which returns all this
201 information, and the value itself.  Also, C<hivex_value_*> functions
202 below which can be used to return the value in a more useful form when
203 you know the type in advance.
204
205 Returns 0 on success.  On error this returns -1 and sets errno.
206
207 =item char *hivex_value_value (hive_h *h, hive_value_h value, hive_type *t, size_t *len);
208
209 Return the value of this (key, value) pair.  The value should
210 be interpreted according to its type (see C<enum hive_type>).
211
212 The value is returned in an array of bytes of length C<len>.
213
214 The value should be freed by the caller when it is no longer needed.
215
216 On error this returns NULL and sets errno.
217
218 =item char *hivex_value_string (hive_h *h, hive_value_h value);
219
220 If this value is a string, return the string reencoded as UTF-8
221 (as a C string).  This only works for values which have type
222 C<hive_t_string>, C<hive_t_expand_string> or C<hive_t_link>.
223
224 The string should be freed by the caller when it is no longer needed.
225
226 On error this returns NULL and sets errno.
227
228 =item char **hivex_value_multiple_strings (hive_h *h, hive_value_h value);
229
230 If this value is a multiple-string, return the strings reencoded
231 as UTF-8 (as a NULL-terminated array of C strings).  This only
232 works for values which have type C<hive_t_multiple_strings>.
233
234 The string array and each string in it should be freed by the
235 caller when they are no longer needed.
236
237 On error this returns NULL and sets errno.
238
239 =item int32_t hivex_value_dword (hive_h *h, hive_value_h value);
240
241 If this value is a DWORD (Windows int32), return it.  This only works
242 for values which have type C<hive_t_dword> or C<hive_t_dword_be>.
243
244 =item int64_t hivex_value_qword (hive_h *h, hive_value_h value);
245
246 If this value is a QWORD (Windows int64), return it.  This only
247 works for values which have type C<hive_t_qword>.
248
249 =back
250
251 =head2 VISITING ALL NODES
252
253 The visitor pattern is useful if you want to visit all nodes
254 in the tree or all nodes below a certain point in the tree.
255
256 First you set up your own C<struct hivex_visitor> with your
257 callback functions.
258
259 Each of these callback functions should return 0 on success or -1
260 on error.  If any callback returns -1, then the entire visit
261 terminates immediately.  If you don't need a callback function at
262 all, set the function pointer to NULL.
263
264  struct hivex_visitor {
265    int (*node_start) (hive_h *, void *opaque, hive_node_h, const char *name);
266    int (*node_end) (hive_h *, void *opaque, hive_node_h, const char *name);
267    int (*value_string) (hive_h *, void *opaque, hive_node_h, hive_value_h,
268          hive_type t, size_t len, const char *key, const char *str);
269    int (*value_multiple_strings) (hive_h *, void *opaque, hive_node_h,
270          hive_value_h, hive_type t, size_t len, const char *key, char **argv);
271    int (*value_string_invalid_utf16) (hive_h *, void *opaque, hive_node_h,
272          hive_value_h, hive_type t, size_t len, const char *key,
273          const char *str);
274    int (*value_dword) (hive_h *, void *opaque, hive_node_h, hive_value_h,
275          hive_type t, size_t len, const char *key, int32_t);
276    int (*value_qword) (hive_h *, void *opaque, hive_node_h, hive_value_h,
277          hive_type t, size_t len, const char *key, int64_t);
278    int (*value_binary) (hive_h *, void *opaque, hive_node_h, hive_value_h,
279          hive_type t, size_t len, const char *key, const char *value);
280    int (*value_none) (hive_h *, void *opaque, hive_node_h, hive_value_h,
281          hive_type t, size_t len, const char *key, const char *value);
282    int (*value_other) (hive_h *, void *opaque, hive_node_h, hive_value_h,
283          hive_type t, size_t len, const char *key, const char *value);
284  };
285
286 =over 4
287
288 =item int hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len, void *opaque, int flags);
289
290 Visit all the nodes recursively in the hive C<h>.
291
292 C<visitor> should be a C<hivex_visitor> structure with callback
293 fields filled in as required (unwanted callbacks can be set to
294 NULL).  C<len> must be the length of the 'visitor' struct (you
295 should pass C<sizeof (struct hivex_visitor)> for this).
296
297 This returns 0 if the whole recursive visit was completed
298 successfully.  On error this returns -1.  If one of the callback
299 functions returned an error than we don't touch errno.  If the
300 error was generated internally then we set errno.
301
302 You can skip bad registry entries by setting C<flag> to
303 C<HIVEX_VISIT_SKIP_BAD>.  If this flag is not set, then a bad registry
304 causes the function to return an error immediately.
305
306 This function is robust if the registry contains cycles or
307 pointers which are invalid or outside the registry.  It detects
308 these cases and returns an error.
309
310 =item int hivex_visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *visitor, size_t len, void *opaque);
311
312 Same as C<hivex_visit> but instead of starting out at the root, this
313 starts at C<node>.
314
315 =back
316
317 =head1 THE STRUCTURE OF THE WINDOWS REGISTRY
318
319 Note: To understand the relationship between hives and the common
320 Windows Registry keys (like C<HKEY_LOCAL_MACHINE>) please see the
321 Wikipedia page on the Windows Registry.
322
323 The Windows Registry is split across various binary files, each
324 file being known as a "hive".  This library only handles a single
325 hive file at a time.
326
327 Hives are n-ary trees with a single root.  Each node in the tree
328 has a name.
329
330 Each node in the tree (including non-leaf nodes) may have an
331 arbitrary list of (key, value) pairs attached to it.  It may
332 be the case that one of these pairs has an empty key.  This
333 is referred to as the default key for the node.
334
335 The (key, value) pairs are the place where the useful data is
336 stored in the registry.  The key is always a string (possibly the
337 empty string for the default key).  The value is a typed object
338 (eg. string, int32, binary, etc.).
339
340 =head2 RELATIONSHIP TO .REG FILES
341
342 Although this library does not care about or deal with Windows reg
343 files, it's useful to look at the relationship between the registry
344 itself and reg files because they are so common.
345
346 A reg file is a text representation of the registry, or part of the
347 registry.  The actual registry hives that Windows uses are binary
348 files.  There are a number of Windows and Linux tools that let you
349 generate reg files, or merge reg files back into the registry hives.
350 Notable amongst them is Microsoft's REGEDIT program (formerly known as
351 REGEDT32).
352
353 A typical reg file will contain many sections looking like this:
354
355  [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Stack]
356  "@"="Generic Stack"
357  "TileInfo"="prop:System.FileCount"
358  "TilePath"=str(2):"%systemroot%\\system32"
359  "ThumbnailCutoff"=dword:00000000
360  "FriendlyTypeName"=hex(2):40,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,\
361   6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,\
362   33,00,32,00,5c,00,73,00,65,00,61,00,72,00,63,00,68,00,66,00,\
363   6f,00,6c,00,64,00,65,00,72,00,2e,00,64,00,6c,00,6c,00,2c,00,\
364   2d,00,39,00,30,00,32,00,38,00,00,00,d8
365
366 Taking this one piece at a time:
367
368  [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Stack]
369
370 This is the path to this node in the registry tree.  The first part,
371 C<HKEY_LOCAL_MACHINE\SOFTWARE> means that this comes from a hive
372 (file) called C<SOFTWARE>.  C<\Classes\Stack> is the real path part,
373 starting at the root node of the C<SOFTWARE> hive.
374
375 Below the node name is a list of zero or more key-value pairs.  Any
376 interior or leaf node in the registry may have key-value pairs
377 attached.
378
379  "@"="Generic Stack"
380
381 This is the "default key".  In reality (ie. inside the binary hive)
382 the key string is the empty string.  In reg files this is written as
383 C<@> but this has no meaning either in the hives themselves or in this
384 library.  The value is a string (type 1 - see C<enum hive_type>
385 above).
386
387  "TileInfo"="prop:System.FileCount"
388
389 This is a regular (key, value) pair, with the value being a type 1
390 string.  Note that inside the binary file the string is likely to be
391 UTF-16 encoded.  This library converts to and from UTF-8 strings
392 transparently.
393
394  "TilePath"=str(2):"%systemroot%\\system32"
395
396 The value in this case has type 2 (expanded string) meaning that some
397 %...% variables get expanded by Windows.  (This library doesn't know
398 or care about variable expansion).
399
400  "ThumbnailCutoff"=dword:00000000
401
402 The value in this case is a dword (type 4).
403
404  "FriendlyTypeName"=hex(2):40,00,....
405
406 This value is an expanded string (type 2) represented in the reg file
407 as a series of hex bytes.  In this case the string appears to be a
408 UTF-16 string.
409
410 =head1 NOTE ON THE USE OF ERRNO
411
412 Many functions in this library set errno to indicate errors.  These
413 are the values of errno you may encounter (this list is not
414 exhaustive):
415
416 =over 4
417
418 =item ENOTSUP
419
420 Corrupt or unsupported Registry file format.
421
422 =item ENOKEY
423
424 Missing root key.
425
426 =item EINVAL
427
428 Passed an invalid argument to the function.
429
430 =item EFAULT
431
432 Followed a Registry pointer which goes outside
433 the registry or outside a registry block.
434
435 =item ELOOP
436
437 Registry contains cycles.
438
439 =item ERANGE
440
441 Field in the registry out of range.
442
443 =back
444
445 =head1 SEE ALSO
446
447 L<hivexml(1)>,
448 L<hivexget(1)>,
449 L<virt-win-reg(1)>,
450 L<guestfs(3)>,
451 L<http://libguestfs.org/>,
452 L<virt-cat(1)>,
453 L<virt-edit(1)>,
454 L<http://en.wikipedia.org/wiki/Windows_Registry>.
455
456 =head1 AUTHORS
457
458 Richard W.M. Jones (C<rjones at redhat dot com>)
459
460 =head1 COPYRIGHT
461
462 Copyright (C) 2009-2010 Red Hat Inc.
463
464 Derived from code by Petter Nordahl-Hagen under a compatible license:
465 Copyright (C) 1997-2007 Petter Nordahl-Hagen.
466
467 Derived from code by Markus Stephany under a compatible license:
468 Copyright (C) 2000-2004 Markus Stephany.
469
470 This library is free software; you can redistribute it and/or
471 modify it under the terms of the GNU Lesser General Public
472 License as published by the Free Software Foundation;
473 version 2.1 of the License.
474
475 This library is distributed in the hope that it will be useful,
476 but WITHOUT ANY WARRANTY; without even the implied warranty of
477 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
478 Lesser General Public License for more details.
479
480 See file LICENSE for the full license.