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