net (registry util): refactor printing of value without name out.
[kai/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 "utils/net_registry_util.h"
24
25 void print_registry_key(const char *keyname, NTTIME *modtime)
26 {
27         d_printf("Keyname   = %s\n", keyname);
28         d_printf("Modtime   = %s\n",
29                  modtime
30                  ? http_timestring(nt_time_to_unix(*modtime))
31                  : "None");
32         d_printf("\n");
33 }
34
35 void print_registry_value(const struct registry_value *valvalue)
36 {
37         d_printf("Type       = %s\n",
38                  reg_type_lookup(valvalue->type));
39         switch(valvalue->type) {
40         case REG_DWORD:
41                 d_printf("Value      = %d\n", valvalue->v.dword);
42                 break;
43         case REG_SZ:
44         case REG_EXPAND_SZ:
45                 d_printf("Value      = \"%s\"\n", valvalue->v.sz.str);
46                 break;
47         case REG_MULTI_SZ: {
48                 uint32 j;
49                 for (j = 0; j < valvalue->v.multi_sz.num_strings; j++) {
50                         d_printf("Value[%3.3d] = \"%s\"\n", j,
51                                  valvalue->v.multi_sz.strings[j]);
52                 }
53                 break;
54         }
55         case REG_BINARY:
56                 d_printf("Value      = %d bytes\n",
57                          (int)valvalue->v.binary.length);
58                 break;
59         default:
60                 d_printf("Value      = <unprintable>\n");
61                 break;
62         }
63 }
64
65 void print_registry_value_with_name(const char *valname,
66                                     const struct registry_value *valvalue)
67 {
68         d_printf("Valuename  = %s\n", valname);
69         print_registry_value(valvalue);
70         d_printf("\n");
71 }
72
73 /**
74  * Split path into hive name and subkeyname
75  * normalizations performed:
76  *  - convert '/' to '\\'
77  *  - strip trailing '\\' chars
78  */
79 WERROR split_hive_key(TALLOC_CTX *ctx, const char *path, char **hivename,
80                       char **subkeyname)
81 {
82         char *p;
83         const char *tmp_subkeyname;
84
85         if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
86                 return WERR_INVALID_PARAM;
87         }
88
89         if (strlen(path) == 0) {
90                 return WERR_INVALID_PARAM;
91         }
92
93         *hivename = talloc_string_sub(ctx, path, "/", "\\");
94         if (*hivename == NULL) {
95                 return WERR_NOMEM;
96         }
97
98         /* strip trailing '\\' chars */
99         p = strrchr(*hivename, '\\');
100         while ((p != NULL) && (p[1] == '\0')) {
101                 *p = '\0';
102                 p = strrchr(*hivename, '\\');
103         }
104
105         p = strchr(*hivename, '\\');
106
107         if ((p == NULL) || (*p == '\0')) {
108                 /* just the hive - no subkey given */
109                 tmp_subkeyname = "";
110         } else {
111                 *p = '\0';
112                 tmp_subkeyname = p+1;
113         }
114         *subkeyname = talloc_strdup(ctx, tmp_subkeyname);
115         if (*subkeyname == NULL) {
116                 return WERR_NOMEM;
117         }
118
119         return WERR_OK;
120 }