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