Registry server: Enhances commit "type" != NULL (when getting values)
[Samba/fernandojvsilva.git] / source4 / rpc_server / winreg / rpc_winreg.c
blob30c1ed3db652f5f3264e5e54f4558da52d65ba91
1 /*
2 Unix SMB/CIFS implementation.
4 endpoint server for the winreg pipe
6 Copyright (C) 2004 Jelmer Vernooij, jelmer@samba.org
7 Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "lib/registry/registry.h"
26 #include "librpc/gen_ndr/ndr_winreg.h"
27 #include "rpc_server/common/common.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "param/param.h"
30 #include "libcli/security/security.h"
32 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
34 static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
35 const struct dcesrv_interface *iface)
37 struct registry_context *ctx;
38 WERROR err;
40 err = reg_open_samba(dce_call->context,
41 &ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info,
42 NULL);
44 if (!W_ERROR_IS_OK(err)) {
45 DEBUG(0, ("Error opening registry: %s\n", win_errstr(err)));
46 return NT_STATUS_UNSUCCESSFUL;
49 dce_call->context->private = ctx;
51 return NT_STATUS_OK;
54 #define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
56 static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call,
57 TALLOC_CTX *mem_ctx, uint32_t hkey,
58 struct policy_handle **outh)
60 struct registry_context *ctx = dce_call->context->private;
61 struct dcesrv_handle *h;
62 WERROR result;
64 h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
66 result = reg_get_predefined_key(ctx, hkey,
67 (struct registry_key **)&h->data);
68 if (!W_ERROR_IS_OK(result)) {
69 return result;
71 *outh = &h->wire_handle;
73 return result;
76 #define func_winreg_OpenHive(k,n) static WERROR dcesrv_winreg_Open ## k (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_Open ## k *r) \
77 { \
78 return dcesrv_winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
81 func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
82 func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
83 func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
84 func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
85 func_winreg_OpenHive(HKU,HKEY_USERS)
86 func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
87 func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
88 func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
89 func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
92 winreg_CloseKey
94 static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
95 TALLOC_CTX *mem_ctx,
96 struct winreg_CloseKey *r)
98 struct dcesrv_handle *h;
100 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
102 talloc_free(h);
104 ZERO_STRUCTP(r->out.handle);
106 return WERR_OK;
110 winreg_CreateKey
112 static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
113 TALLOC_CTX *mem_ctx,
114 struct winreg_CreateKey *r)
116 struct dcesrv_handle *h, *newh;
117 struct security_descriptor sd;
118 WERROR result;
120 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
122 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
124 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
126 case SECURITY_SYSTEM:
127 case SECURITY_ADMINISTRATOR:
128 /* the security descriptor is optional */
129 if (r->in.secdesc != NULL) {
130 DATA_BLOB sdblob;
131 enum ndr_err_code ndr_err;
132 sdblob.data = r->in.secdesc->sd.data;
133 sdblob.length = r->in.secdesc->sd.len;
134 if (sdblob.data == NULL) {
135 return WERR_INVALID_PARAM;
137 ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, NULL, &sd,
138 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
140 return WERR_INVALID_PARAM;
144 result = reg_key_add_name(newh, (struct registry_key *)h->data,
145 r->in.name.name, NULL, r->in.secdesc?&sd:NULL,
146 (struct registry_key **)&newh->data);
147 if (W_ERROR_IS_OK(result)) {
148 r->out.new_handle = &newh->wire_handle;
149 } else {
150 talloc_free(newh);
153 return result;
154 default:
155 return WERR_ACCESS_DENIED;
161 winreg_DeleteKey
163 static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
164 TALLOC_CTX *mem_ctx,
165 struct winreg_DeleteKey *r)
167 struct dcesrv_handle *h;
169 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
171 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
173 case SECURITY_SYSTEM:
174 case SECURITY_ADMINISTRATOR:
175 return reg_key_del((struct registry_key *)h->data, r->in.key.name);
176 default:
177 return WERR_ACCESS_DENIED;
183 winreg_DeleteValue
185 static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
186 TALLOC_CTX *mem_ctx,
187 struct winreg_DeleteValue *r)
189 struct dcesrv_handle *h;
191 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
193 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
195 case SECURITY_SYSTEM:
196 case SECURITY_ADMINISTRATOR:
197 return reg_del_value((struct registry_key *)h->data, r->in.value.name);
198 default:
199 return WERR_ACCESS_DENIED;
205 winreg_EnumKey
207 static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
208 TALLOC_CTX *mem_ctx,
209 struct winreg_EnumKey *r)
211 struct dcesrv_handle *h;
212 const char *name, *classname;
213 NTTIME last_mod;
214 WERROR result;
216 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
218 result = reg_key_get_subkey_by_index(mem_ctx,
219 (struct registry_key *)h->data, r->in.enum_index,
220 &name, &classname, &last_mod);
222 if (2*strlen_m_term(name) > r->in.name->size) {
223 return WERR_MORE_DATA;
226 if (name != NULL) {
227 r->out.name->name = name;
228 r->out.name->length = 2*strlen_m_term(name);
229 } else {
230 r->out.name->name = r->in.name->name;
231 r->out.name->length = r->in.name->length;
233 r->out.name->size = r->in.name->size;
235 r->out.keyclass = r->in.keyclass;
236 if (classname != NULL) {
237 r->out.keyclass->name = classname;
238 r->out.keyclass->length = 2*strlen_m_term(classname);
239 } else {
240 r->out.keyclass->name = r->in.keyclass->name;
241 r->out.keyclass->length = r->in.keyclass->length;
243 r->out.keyclass->size = r->in.keyclass->size;
245 if (r->in.last_changed_time != NULL)
246 r->out.last_changed_time = &last_mod;
248 return result;
253 winreg_EnumValue
255 static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
256 TALLOC_CTX *mem_ctx,
257 struct winreg_EnumValue *r)
259 struct dcesrv_handle *h;
260 struct registry_key *key;
261 const char *data_name;
262 DATA_BLOB data;
263 WERROR result;
265 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
267 key = h->data;
269 result = reg_key_get_value_by_index(mem_ctx, (struct registry_key *)h->data,
270 r->in.enum_index, &data_name, r->out.type, &data);
272 if (!W_ERROR_IS_OK(result)) {
273 /* if the lookup wasn't successful, send client query back */
274 data_name = r->in.name->name;
275 data.data = r->in.value;
276 data.length = *r->in.length;
279 /* and enough room for the name */
280 if (r->in.name->size < 2*strlen_m_term(data_name)) {
281 return WERR_MORE_DATA;
284 /* "data_name" is NULL when we query the default attribute */
285 if (data_name != NULL) {
286 r->out.name->name = data_name;
287 r->out.name->length = 2*strlen_m_term(data_name);
288 } else {
289 r->out.name->name = r->in.name->name;
290 r->out.name->length = r->in.name->length;
292 r->out.name->size = r->in.name->size;
294 /* check the client has enough room for the value */
295 if (r->in.value != NULL &&
296 r->in.size != NULL &&
297 data.length > *r->in.size) {
298 return WERR_MORE_DATA;
301 if (r->in.value != NULL) {
302 r->out.value = data.data;
305 if (r->in.size != NULL) {
306 r->out.size = talloc(mem_ctx, uint32_t);
307 *r->out.size = data.length;
308 r->out.length = r->out.size;
311 return result;
316 winreg_FlushKey
318 static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
319 TALLOC_CTX *mem_ctx,
320 struct winreg_FlushKey *r)
322 struct dcesrv_handle *h;
324 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
326 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
328 case SECURITY_SYSTEM:
329 case SECURITY_ADMINISTRATOR:
330 return reg_key_flush(h->data);
331 default:
332 return WERR_ACCESS_DENIED;
338 winreg_GetKeySecurity
340 static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call,
341 TALLOC_CTX *mem_ctx,
342 struct winreg_GetKeySecurity *r)
344 struct dcesrv_handle *h;
346 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
348 return WERR_NOT_SUPPORTED;
353 winreg_LoadKey
355 static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call,
356 TALLOC_CTX *mem_ctx,
357 struct winreg_LoadKey *r)
359 return WERR_NOT_SUPPORTED;
364 winreg_NotifyChangeKeyValue
366 static WERROR dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call,
367 TALLOC_CTX *mem_ctx,
368 struct winreg_NotifyChangeKeyValue *r)
370 return WERR_NOT_SUPPORTED;
375 winreg_OpenKey
377 static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
378 TALLOC_CTX *mem_ctx,
379 struct winreg_OpenKey *r)
381 struct dcesrv_handle *h, *newh;
382 WERROR result;
384 DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
386 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
388 case SECURITY_SYSTEM:
389 case SECURITY_ADMINISTRATOR:
390 case SECURITY_USER:
391 if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
392 newh = talloc_reference(dce_call->context, h);
393 result = WERR_OK;
394 } else {
395 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
396 result = reg_open_key(newh, (struct registry_key *)h->data,
397 r->in.keyname.name,
398 (struct registry_key **)&newh->data);
401 if (W_ERROR_IS_OK(result)) {
402 r->out.handle = &newh->wire_handle;
403 } else {
404 talloc_free(newh);
406 return result;
407 default:
408 return WERR_ACCESS_DENIED;
415 winreg_QueryInfoKey
417 static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
418 TALLOC_CTX *mem_ctx,
419 struct winreg_QueryInfoKey *r)
421 struct dcesrv_handle *h;
422 const char *classname = NULL;
423 WERROR result;
425 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
427 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
429 case SECURITY_SYSTEM:
430 case SECURITY_ADMINISTRATOR:
431 case SECURITY_USER:
432 result = reg_key_get_info(mem_ctx, (struct registry_key *)h->data,
433 &classname, r->out.num_subkeys, r->out.num_values,
434 r->out.last_changed_time, r->out.max_subkeylen,
435 r->out.max_valnamelen, r->out.max_valbufsize);
437 if (classname != NULL) {
438 r->out.classname->name = classname;
439 r->out.classname->name_len = 2*strlen_m_term(classname);
440 } else {
441 r->out.classname->name = r->in.classname->name;
442 r->out.classname->name_len = r->in.classname->name_len;
444 r->out.classname->name_size = r->in.classname->name_size;
446 return result;
447 default:
448 return WERR_ACCESS_DENIED;
454 winreg_QueryValue
456 static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
457 TALLOC_CTX *mem_ctx,
458 struct winreg_QueryValue *r)
460 struct dcesrv_handle *h;
461 DATA_BLOB value_data;
462 WERROR result;
464 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
466 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
468 case SECURITY_SYSTEM:
469 case SECURITY_ADMINISTRATOR:
470 case SECURITY_USER:
471 result = reg_key_get_value_by_name(mem_ctx,
472 (struct registry_key *)h->data, r->in.value_name.name,
473 r->out.type, &value_data);
475 if (!W_ERROR_IS_OK(result)) {
476 /* if the lookup wasn't successful, send client query back */
477 value_data.data = r->in.data;
478 value_data.length = *r->in.length;
481 /* Just asking for the size of the buffer */
482 r->out.length = talloc(mem_ctx, uint32_t);
483 if (!r->out.length) {
484 return WERR_NOMEM;
486 *r->out.length = value_data.length;
487 r->out.size = talloc(mem_ctx, uint32_t);
488 if (!r->out.size) {
489 return WERR_NOMEM;
491 *r->out.size = value_data.length;
493 r->out.data = value_data.data;
495 return result;
496 default:
497 return WERR_ACCESS_DENIED;
503 winreg_ReplaceKey
505 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call,
506 TALLOC_CTX *mem_ctx,
507 struct winreg_ReplaceKey *r)
509 return WERR_NOT_SUPPORTED;
514 winreg_RestoreKey
516 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call,
517 TALLOC_CTX *mem_ctx,
518 struct winreg_RestoreKey *r)
520 return WERR_NOT_SUPPORTED;
525 winreg_SaveKey
527 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call,
528 TALLOC_CTX *mem_ctx,
529 struct winreg_SaveKey *r)
531 return WERR_NOT_SUPPORTED;
536 winreg_SetKeySecurity
538 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call,
539 TALLOC_CTX *mem_ctx,
540 struct winreg_SetKeySecurity *r)
542 return WERR_NOT_SUPPORTED;
547 winreg_SetValue
549 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
550 TALLOC_CTX *mem_ctx,
551 struct winreg_SetValue *r)
553 struct dcesrv_handle *h;
554 DATA_BLOB data;
555 WERROR result;
557 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
559 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
561 case SECURITY_SYSTEM:
562 case SECURITY_ADMINISTRATOR:
563 data.data = r->in.data;
564 data.length = r->in.size;
565 result = reg_val_set((struct registry_key *)h->data,
566 r->in.name.name, r->in.type, data);
567 return result;
568 default:
569 return WERR_ACCESS_DENIED;
575 winreg_UnLoadKey
577 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call,
578 TALLOC_CTX *mem_ctx,
579 struct winreg_UnLoadKey *r)
581 return WERR_NOT_SUPPORTED;
586 winreg_InitiateSystemShutdown
588 static WERROR dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call,
589 TALLOC_CTX *mem_ctx,
590 struct winreg_InitiateSystemShutdown *r)
592 return WERR_NOT_SUPPORTED;
597 winreg_AbortSystemShutdown
599 static WERROR dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call,
600 TALLOC_CTX *mem_ctx,
601 struct winreg_AbortSystemShutdown *r)
603 return WERR_NOT_SUPPORTED;
608 winreg_GetVersion
610 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call,
611 TALLOC_CTX *mem_ctx,
612 struct winreg_GetVersion *r)
614 struct dcesrv_handle *h;
616 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
618 r->out.version = talloc(mem_ctx, uint32_t);
619 W_ERROR_HAVE_NO_MEMORY(r->out.version);
621 *r->out.version = 5;
623 return WERR_OK;
628 winreg_QueryMultipleValues
630 static WERROR dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call,
631 TALLOC_CTX *mem_ctx,
632 struct winreg_QueryMultipleValues *r)
634 return WERR_NOT_SUPPORTED;
639 winreg_InitiateSystemShutdownEx
641 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call,
642 TALLOC_CTX *mem_ctx,
643 struct winreg_InitiateSystemShutdownEx *r)
645 return WERR_NOT_SUPPORTED;
650 winreg_SaveKeyEx
652 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call,
653 TALLOC_CTX *mem_ctx,
654 struct winreg_SaveKeyEx *r)
656 return WERR_NOT_SUPPORTED;
661 winreg_QueryMultipleValues2
663 static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call,
664 TALLOC_CTX *mem_ctx,
665 struct winreg_QueryMultipleValues2 *r)
667 return WERR_NOT_SUPPORTED;
671 /* include the generated boilerplate */
672 #include "librpc/gen_ndr/ndr_winreg_s.c"