2 * Samba Unix/Linux SMB client library
3 * Adapter to use reg_parse with the registry api
5 * Copyright (C) Gregor Beck 2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "reg_parse.h"
23 #include "reg_import.h"
25 #include "registry/reg_objects.h"
28 /* Debuglevel for tracing */
29 static const int TL
= 2;
33 struct reg_parse_callback reg_parse_callback
;
34 struct reg_import_callback call
;
39 reg_parse_callback_key(struct reg_import
* cb_private
,
40 const char* key
[], size_t n
,
44 reg_parse_callback_val(struct reg_import
* cb_private
,
45 const char* name
, uint32_t type
,
46 const uint8_t* data
, uint32_t len
);
49 reg_parse_callback_val_registry_value(struct reg_import
* cb_private
,
50 const char* name
, uint32_t type
,
51 const uint8_t* data
, uint32_t len
);
54 reg_parse_callback_val_regval_blob(struct reg_import
* cb_private
,
55 const char* name
, uint32_t type
,
56 const uint8_t* data
, uint32_t len
);
59 reg_parse_callback_val_del(struct reg_import
* cb_private
,
63 reg_parse_callback_comment(struct reg_import
* cb_private
,
67 /*******************************************************************************/
69 int reg_parse_callback_key(struct reg_import
* p
,
70 const char* key
[], size_t n
, bool del
)
72 WERROR werr
= WERR_OK
;
74 DEBUG(TL
, ("%s: %s\n", __FUNCTION__
, key
[0]));
76 if (p
->open_key
!= NULL
) {
77 werr
= p
->call
.closekey(p
->call
.data
, p
->open_key
);
78 if (!W_ERROR_IS_OK(werr
)) {
79 DEBUG(0, ("closekey failed: %s\n", win_errstr(werr
)));
84 werr
= p
->call
.deletekey(p
->call
.data
, NULL
, key
[0]);
85 if (W_ERROR_EQUAL(werr
, WERR_BADFILE
)) {
86 /* the key didn't exist, treat as success */
89 if (!W_ERROR_IS_OK(werr
)) {
90 DEBUG(0, ("deletekey %s failed: %s\n",
91 key
[0], win_errstr(werr
)));
96 werr
= p
->call
.createkey(p
->call
.data
, NULL
, key
[0],
97 &p
->open_key
, &existing
);
98 if (W_ERROR_IS_OK(werr
)) {
99 DEBUG(TL
, ("createkey %s %s\n",
100 existing
? "opened" : "created", key
[0]));
102 DEBUG(0, ("createkey %s failed: %s\n",
103 key
[0], win_errstr(werr
)));
107 return W_ERROR_IS_OK(werr
) ? 0 : -1;
110 #define DEBUG_ADD_HEX(LEV, PTR, LEN) \
113 const unsigned char* ptr = (const unsigned char*)PTR; \
114 for (i=0; i<LEN; i++) { \
115 DEBUGADD(LEV, ("'%c'(%02x)%s", \
116 isprint(ptr[i]) ? ptr[i] : '.', \
118 ((i+1 < LEN) && (i+1)%8) \
123 /*----------------------------------------------------------------------------*/
124 int reg_parse_callback_val(struct reg_import
* p
,
125 const char* name
, uint32_t type
,
126 const uint8_t* data
, uint32_t len
)
128 WERROR werr
= WERR_OK
;
130 DEBUG(TL
, ("%s(%x): >%s< = [%x]\n", __FUNCTION__
, type
, name
, len
));
131 DEBUG_ADD_HEX(TL
, data
, len
);
133 werr
= p
->call
.setval
.blob(p
->call
.data
, p
->open_key
, name
, type
,
135 if (!W_ERROR_IS_OK(werr
)) {
136 DEBUG(0, ("setval %s failed: %s\n",
137 name
, win_errstr(werr
)));
140 return W_ERROR_IS_OK(werr
) ? 0 : -1;
143 /*----------------------------------------------------------------------------*/
144 int reg_parse_callback_val_registry_value(struct reg_import
* p
,
145 const char* name
, uint32_t type
,
146 const uint8_t* data
, uint32_t len
)
148 WERROR werr
= WERR_OK
;
149 struct registry_value val
= {
151 .data
= data_blob_talloc(p
, data
, len
),
154 DEBUG(TL
, ("%s(%x): >%s< = [%x]\n", __FUNCTION__
, type
, name
, len
));
155 DEBUG_ADD_HEX(TL
, data
, len
);
157 werr
= p
->call
.setval
.registry_value(p
->call
.data
, p
->open_key
,
159 if (!W_ERROR_IS_OK(werr
)) {
160 DEBUG(0, ("setval %s failed: %s\n",
161 name
, win_errstr(werr
)));
164 data_blob_free(&val
.data
);
165 return W_ERROR_IS_OK(werr
) ? 0 : -1;
168 /*----------------------------------------------------------------------------*/
169 int reg_parse_callback_val_regval_blob(struct reg_import
* p
,
170 const char* name
, uint32_t type
,
171 const uint8_t* data
, uint32_t len
)
173 WERROR werr
= WERR_OK
;
174 void* mem_ctx
= talloc_new(p
);
175 struct regval_blob
* v
= NULL
;
177 DEBUG(TL
, ("%s(%x): >%s< = [%x]\n", __FUNCTION__
, type
, name
, len
));
178 DEBUG_ADD_HEX(TL
, data
, len
);
180 v
= regval_compose(mem_ctx
, name
, type
, data
, len
);
182 DEBUG(0, ("regval_compose %s failed\n", name
));
187 werr
= p
->call
.setval
.regval_blob(p
->call
.data
, p
->open_key
, v
);
188 if (!W_ERROR_IS_OK(werr
)) {
189 DEBUG(0, ("setval %s failed: %s\n",
190 name
, win_errstr(werr
)));
194 talloc_free(mem_ctx
);
196 return W_ERROR_IS_OK(werr
) ? 0 : -1;
200 /*----------------------------------------------------------------------------*/
202 int reg_parse_callback_val_del(struct reg_import
* p
,
205 WERROR werr
= WERR_OK
;
207 DEBUG(TL
, ("%s: %s\n", __FUNCTION__
, name
));
209 werr
= p
->call
.deleteval(p
->call
.data
, p
->open_key
, name
);
210 if (!W_ERROR_IS_OK(werr
)) {
211 DEBUG(0, ("deleteval %s failed: %s\n",
212 name
, win_errstr(werr
)));
215 return W_ERROR_IS_OK(werr
) ? 0 : -1;
219 int reg_parse_callback_comment(struct reg_import
* cb_private
,
222 DEBUG(TL
, ("%s: %s\n", __FUNCTION__
, txt
));
226 /******************************************************************************/
227 static int nop(void* data
)
233 struct reg_parse_callback
* reg_import_adapter(const void* talloc_ctx
,
234 struct reg_import_callback cb
)
236 struct reg_parse_callback
* ret
;
237 struct reg_import
* p
= talloc_zero(talloc_ctx
, struct reg_import
);
241 if (cb
.openkey
== NULL
) {
242 cb
.openkey
= (reg_import_callback_openkey_t
)&nop
;
244 if (cb
.closekey
== NULL
) {
245 cb
.closekey
= (reg_import_callback_closekey_t
)&nop
;
247 if (cb
.createkey
== NULL
) {
248 cb
.createkey
= (reg_import_callback_createkey_t
)&nop
;
250 if (cb
.deletekey
== NULL
) {
251 cb
.deletekey
= (reg_import_callback_deletekey_t
)&nop
;
253 if (cb
.deleteval
== NULL
) {
254 cb
.deleteval
= (reg_import_callback_deleteval_t
)&nop
;
259 ret
= &p
->reg_parse_callback
;
260 ret
->key
= (reg_parse_callback_key_t
) ®_parse_callback_key
;
261 ret
->val_del
= (reg_parse_callback_val_del_t
) ®_parse_callback_val_del
;
262 ret
->comment
= (reg_parse_callback_comment_t
) ®_parse_callback_comment
;
265 switch (cb
.setval_type
) {
267 assert(cb
.setval
.blob
!= NULL
);
268 ret
->val
= (reg_parse_callback_val_t
) ®_parse_callback_val
;
271 assert(cb
.setval
.registry_value
!= NULL
);
272 ret
->val
= (reg_parse_callback_val_t
) ®_parse_callback_val_registry_value
;
275 assert(cb
.setval
.regval_blob
!= NULL
);
276 ret
->val
= (reg_parse_callback_val_t
) ®_parse_callback_val_regval_blob
;
285 assert((struct reg_parse_callback
*)p
== ret
);