s3-registry: only include registry headers when really needed.
[kai/samba-autobuild/.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
27 void print_registry_key(const char *keyname, NTTIME *modtime)
28 {
29         d_printf(_("Keyname   = %s\n"), keyname);
30         d_printf(_("Modtime   = %s\n"),
31                  modtime
32                  ? http_timestring(talloc_tos(), nt_time_to_unix(*modtime))
33                  : _("None"));
34         d_printf("\n");
35 }
36
37 void print_registry_value(const struct registry_value *valvalue, bool raw)
38 {
39         if (!raw) {
40                 d_printf(_("Type       = %s\n"),
41                          str_regtype(valvalue->type));
42         }
43         switch(valvalue->type) {
44         case REG_DWORD:
45                 if (!raw) {
46                         d_printf(_("Value      = "));
47                 }
48                 d_printf("%d\n", valvalue->v.dword);
49                 break;
50         case REG_SZ:
51         case REG_EXPAND_SZ:
52                 if (!raw) {
53                         d_printf(_("Value      = \""));
54                 }
55                 d_printf("%s", valvalue->v.sz.str);
56                 if (!raw) {
57                         d_printf("\"");
58                 }
59                 d_printf("\n");
60                 break;
61         case REG_MULTI_SZ: {
62                 uint32 j;
63                 for (j = 0; j < valvalue->v.multi_sz.num_strings; j++) {
64                         if (!raw) {
65                                 d_printf(_("Value[%3.3d] = \""), j);
66                         }
67                         d_printf("%s", valvalue->v.multi_sz.strings[j]);
68                         if (!raw) {
69                                 d_printf("\"");
70                         }
71                         d_printf("\n");
72                 }
73                 break;
74         }
75         case REG_BINARY:
76                 if (!raw) {
77                         d_printf(_("Value      = "));
78                 }
79                 d_printf(_("%d bytes\n"), (int)valvalue->v.binary.length);
80                 break;
81         default:
82                 if (!raw) {
83                         d_printf(_("Value      = "));
84                 }
85                 d_printf(_("<unprintable>\n"));
86                 break;
87         }
88 }
89
90 void print_registry_value_with_name(const char *valname,
91                                     const struct registry_value *valvalue)
92 {
93         d_printf(_("Valuename  = %s\n"), valname);
94         print_registry_value(valvalue, false);
95         d_printf("\n");
96 }
97
98 /**
99  * Split path into hive name and subkeyname
100  * normalizations performed:
101  *  - convert '/' to '\\'
102  *  - strip trailing '\\' chars
103  */
104 WERROR split_hive_key(TALLOC_CTX *ctx, const char *path, char **hivename,
105                       char **subkeyname)
106 {
107         char *p;
108         const char *tmp_subkeyname;
109
110         if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
111                 return WERR_INVALID_PARAM;
112         }
113
114         if (strlen(path) == 0) {
115                 return WERR_INVALID_PARAM;
116         }
117
118         *hivename = talloc_string_sub(ctx, path, "/", "\\");
119         if (*hivename == NULL) {
120                 return WERR_NOMEM;
121         }
122
123         /* strip trailing '\\' chars */
124         p = strrchr(*hivename, '\\');
125         while ((p != NULL) && (p[1] == '\0')) {
126                 *p = '\0';
127                 p = strrchr(*hivename, '\\');
128         }
129
130         p = strchr(*hivename, '\\');
131
132         if ((p == NULL) || (*p == '\0')) {
133                 /* just the hive - no subkey given */
134                 tmp_subkeyname = "";
135         } else {
136                 *p = '\0';
137                 tmp_subkeyname = p+1;
138         }
139         *subkeyname = talloc_strdup(ctx, tmp_subkeyname);
140         if (*subkeyname == NULL) {
141                 return WERR_NOMEM;
142         }
143
144         return WERR_OK;
145 }