Support for Windows Registry.
[libguestfs.git] / hivex / hivexget.c
1 /* hivexget - Get single subkeys or values from a hive.
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <inttypes.h>
24 #include <errno.h>
25
26 #include "hivex.h"
27
28 int
29 main (int argc, char *argv[])
30 {
31   if (argc < 3 || argc > 4) {
32     fprintf (stderr, "hivexget regfile path [key]\n");
33     exit (1);
34   }
35   char *file = argv[1];
36   char *path = argv[2];
37   char *key = argv[3];          /* could be NULL */
38
39   if (path[0] != '\\') {
40     fprintf (stderr, "hivexget: path must start with a \\ character\n");
41     exit (1);
42   }
43   if (path[1] == '\\') {
44   doubled:
45     fprintf (stderr, "hivexget: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n", path);
46     exit (1);
47   }
48
49   hive_h *h = hivex_open (file, 0);
50   if (h == NULL) {
51   error:
52     perror (file);
53     exit (1);
54   }
55
56   /* Navigate to the desired node. */
57   hive_node_h node = hivex_root (h);
58   if (!node)
59     goto error;
60
61   char *p = path+1, *pnext;
62   size_t len;
63   while (*p) {
64     len = strcspn (p, "\\");
65
66     if (len == 0)
67       goto doubled;
68
69     if (p[len] == '\\') {
70       p[len] = '\0';
71       pnext = p + len + 1;
72     } else
73       pnext = p + len;
74
75     errno = 0;
76     node = hivex_node_get_child (h, node, p);
77     if (node == 0) {
78       if (errno)
79         goto error;
80       /* else node not found */
81       fprintf (stderr, "hivexget: %s: %s: path element not found\n",
82                path, p);
83       exit (2);
84     }
85
86     p = pnext;
87   }
88
89   /* Get the desired key, or print all keys. */
90   if (key) {
91     hive_value_h value;
92
93     errno = 0;
94     if (key[0] == '@' && key[1] == '\0') /* default key written as "@" */
95       value = hivex_node_get_value (h, node, "");
96     else
97       value = hivex_node_get_value (h, node, key);
98
99     if (value == 0) {
100       if (errno)
101         goto error;
102       /* else key not found */
103       fprintf (stderr, "hivexget: %s: key not found\n", key);
104       exit (2);
105     }
106
107     /* Print the value. */
108     hive_type t;
109     size_t len;
110     if (hivex_value_type (h, value, &t, &len) == -1)
111       goto error;
112
113     switch (t) {
114     case hive_t_string:
115     case hive_t_expand_string:
116     case hive_t_link: {
117       char *str = hivex_value_string (h, value);
118       if (!str)
119         goto error;
120
121       puts (str); /* note: this adds a single \n character */
122       free (str);
123       break;
124     }
125
126     case hive_t_dword:
127     case hive_t_dword_be: {
128       int32_t j = hivex_value_dword (h, value);
129       printf ("%" PRIi32 "\n", j);
130       break;
131     }
132
133     case hive_t_qword: {
134       int64_t j = hivex_value_qword (h, value);
135       printf ("%" PRIi64 "\n", j);
136       break;
137     }
138
139     case hive_t_multiple_strings: {
140       char **strs = hivex_value_multiple_strings (h, value);
141       if (!strs)
142         goto error;
143       size_t j;
144       for (j = 0; strs[j] != NULL; ++j) {
145         puts (strs[j]);
146         free (strs[j]);
147       }
148       free (strs);
149       break;
150     }
151
152     case hive_t_none:
153     case hive_t_binary:
154     case hive_t_resource_list:
155     case hive_t_full_resource_description:
156     case hive_t_resource_requirements_list:
157     default: {
158       char *data = hivex_value_value (h, value, &t, &len);
159       if (!data)
160         goto error;
161
162       if (fwrite (data, 1, len, stdout) != len)
163         goto error;
164
165       free (data);
166       break;
167     }
168     } /* switch */
169   } else {
170     /* No key specified, so print all keys in this node.  We do this
171      * in a format which looks like the output of regedit, although
172      * this isn't a particularly useful format.
173      */
174     hive_value_h *values;
175
176     values = hivex_node_values (h, node);
177     if (values == NULL)
178       goto error;
179
180     size_t i;
181     for (i = 0; values[i] != 0; ++i) {
182       char *key = hivex_value_key (h, values[i]);
183       if (!key) goto error;
184
185       if (*key) {
186         putchar ('"');
187         size_t j;
188         for (j = 0; key[j] != 0; ++j) {
189           if (key[j] == '"' || key[j] == '\\')
190             putchar ('\\');
191           putchar (key[j]);
192         }
193         putchar ('"');
194       } else
195         printf ("\"@\"");       /* default key in regedit files */
196       putchar ('=');
197       free (key);
198
199       hive_type t;
200       size_t len;
201       if (hivex_value_type (h, values[i], &t, &len) == -1)
202         goto error;
203
204       switch (t) {
205       case hive_t_string:
206       case hive_t_expand_string:
207       case hive_t_link: {
208         char *str = hivex_value_string (h, values[i]);
209         if (!str)
210           goto error;
211
212         if (t != hive_t_string)
213           printf ("str(%d):", t);
214         putchar ('"');
215         size_t j;
216         for (j = 0; str[j] != 0; ++j) {
217           if (str[j] == '"' || str[j] == '\\')
218             putchar ('\\');
219           putchar (str[j]);
220         }
221         putchar ('"');
222         free (str);
223         break;
224       }
225
226       case hive_t_dword:
227       case hive_t_dword_be: {
228         int32_t j = hivex_value_dword (h, values[i]);
229         printf ("dword:%08" PRIx32 "\"", j);
230         break;
231       }
232
233       case hive_t_qword: /* sic */
234       case hive_t_none:
235       case hive_t_binary:
236       case hive_t_multiple_strings:
237       case hive_t_resource_list:
238       case hive_t_full_resource_description:
239       case hive_t_resource_requirements_list:
240       default: {
241         char *data = hivex_value_value (h, values[i], &t, &len);
242         if (!data)
243           goto error;
244
245         printf ("hex(%d):", t);
246         size_t j;
247         for (j = 0; j < len; ++j) {
248           if (j > 0)
249             putchar (',');
250           printf ("%02x", data[j]);
251         }
252         break;
253       }
254       } /* switch */
255
256       putchar ('\n');
257     } /* for */
258
259     free (values);
260   }
261
262   if (hivex_close (h) == -1)
263     goto error;
264
265   exit (0);
266 }