registry: create and use shared libcli/registry/util_reg.h header.
[samba.git] / source3 / utils / net_registry_util.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Distributed SMB/CIFS Server Management Utility
4  * registry utility functions
5  *
6  * Copyright (C) Michael Adam 2008
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "registry.h"
24 #include "utils/net_registry_util.h"
25 #include "utils/net.h"
26 #include "../libcli/registry/util_reg.h"
27
28 void print_registry_key(const char *keyname, NTTIME *modtime)
29 {
30         d_printf(_("Keyname   = %s\n"), keyname);
31         d_printf(_("Modtime   = %s\n"),
32                  modtime
33                  ? http_timestring(talloc_tos(), nt_time_to_unix(*modtime))
34                  : _("None"));
35         d_printf("\n");
36 }
37
38 void print_registry_value(const struct registry_value *valvalue, bool raw)
39 {
40         if (!raw) {
41                 d_printf(_("Type       = %s\n"),
42                          str_regtype(valvalue->type));
43         }
44         switch(valvalue->type) {
45         case REG_DWORD: {
46                 uint32_t v = 0;
47                 if (valvalue->data.length >= 4) {
48                         v = IVAL(valvalue->data.data, 0);
49                 }
50                 if (!raw) {
51                         d_printf(_("Value      = "));
52                 }
53                 d_printf("%d\n", v);
54                 break;
55         }
56         case REG_SZ:
57         case REG_EXPAND_SZ: {
58                 const char *s;
59
60                 if (!pull_reg_sz(talloc_tos(), &valvalue->data, &s)) {
61                         break;
62                 }
63                 if (!raw) {
64                         d_printf(_("Value      = \""));
65                 }
66                 d_printf("%s", s);
67                 if (!raw) {
68                         d_printf("\"");
69                 }
70                 d_printf("\n");
71                 break;
72         }
73         case REG_MULTI_SZ: {
74                 uint32 j;
75                 const char **a;
76
77                 if (!pull_reg_multi_sz(talloc_tos(), &valvalue->data, &a)) {
78                         break;
79                 }
80                 for (j = 0; a[j] != NULL; j++) {
81                         if (!raw) {
82                                 d_printf(_("Value[%3.3d] = \""), j);
83                         }
84                         d_printf("%s", a[j]);
85                         if (!raw) {
86                                 d_printf("\"");
87                         }
88                         d_printf("\n");
89                 }
90                 break;
91         }
92         case REG_BINARY:
93                 if (!raw) {
94                         d_printf(_("Value      = "));
95                 }
96                 d_printf(_("%d bytes\n"), (int)valvalue->data.length);
97                 break;
98         default:
99                 if (!raw) {
100                         d_printf(_("Value      = "));
101                 }
102                 d_printf(_("<unprintable>\n"));
103                 break;
104         }
105 }
106
107 void print_registry_value_with_name(const char *valname,
108                                     const struct registry_value *valvalue)
109 {
110         d_printf(_("Valuename  = %s\n"), valname);
111         print_registry_value(valvalue, false);
112         d_printf("\n");
113 }
114
115 /**
116  * Split path into hive name and subkeyname
117  * normalizations performed:
118  *  - if the path contains no '\\' characters,
119  *    assume that the legacy format of using '/'
120  *    as a separator is used and  convert '/' to '\\'
121  *  - strip trailing '\\' chars
122  */
123 WERROR split_hive_key(TALLOC_CTX *ctx, const char *path, char **hivename,
124                       char **subkeyname)
125 {
126         char *p;
127         const char *tmp_subkeyname;
128
129         if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
130                 return WERR_INVALID_PARAM;
131         }
132
133         if (strlen(path) == 0) {
134                 return WERR_INVALID_PARAM;
135         }
136
137         if (strchr(path, '\\') == NULL) {
138                 *hivename = talloc_string_sub(ctx, path, "/", "\\");
139         } else {
140                 *hivename = talloc_strdup(ctx, path);
141         }
142
143         if (*hivename == NULL) {
144                 return WERR_NOMEM;
145         }
146
147         /* strip trailing '\\' chars */
148         p = strrchr(*hivename, '\\');
149         while ((p != NULL) && (p[1] == '\0')) {
150                 *p = '\0';
151                 p = strrchr(*hivename, '\\');
152         }
153
154         p = strchr(*hivename, '\\');
155
156         if ((p == NULL) || (*p == '\0')) {
157                 /* just the hive - no subkey given */
158                 tmp_subkeyname = "";
159         } else {
160                 *p = '\0';
161                 tmp_subkeyname = p+1;
162         }
163         *subkeyname = talloc_strdup(ctx, tmp_subkeyname);
164         if (*subkeyname == NULL) {
165                 return WERR_NOMEM;
166         }
167
168         return WERR_OK;
169 }