s4:WINREG RPC server - Reintroduce the free operation on "CloseKey"
[ira/wip.git] / source4 / rpc_server / winreg / rpc_winreg.c
index c0242f8072b90e6767790375331af20fbc4cbd00..ba619c02f7c6e487e4793f53f9cb109290f61ff6 100644 (file)
-/* 
+/*
    Unix SMB/CIFS implementation.
 
    endpoint server for the winreg pipe
 
-   Copyright (C) Jelmer Vernooij 2004
-   
+   Copyright (C) 2004 Jelmer Vernooij, jelmer@samba.org
+   Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-#include "registry.h"
+#include "rpc_server/dcerpc_server.h"
+#include "lib/registry/registry.h"
 #include "librpc/gen_ndr/ndr_winreg.h"
-#include "rpc_server/common/common.h"
+#include "librpc/gen_ndr/ndr_security.h"
+#include "libcli/security/security.h"
 
 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
 
-static void winreg_destroy_hive(struct dcesrv_connection *c, struct dcesrv_handle *h)
+static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
+                                  const struct dcesrv_interface *iface)
 {
-       reg_close(((struct registry_key *)h->data)->hive->reg_ctx);
+       struct registry_context *ctx;
+       WERROR err;
+
+       err = reg_open_samba(dce_call->context,
+                            &ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx,
+                            dce_call->conn->auth_state.session_info,
+                            NULL);
+
+       if (!W_ERROR_IS_OK(err)) {
+               DEBUG(0, ("Error opening registry: %s\n", win_errstr(err)));
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       dce_call->context->private_data = ctx;
+
+       return NT_STATUS_OK;
 }
 
-static WERROR winreg_openhive (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, const char *hivename, struct policy_handle **outh)
+#define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
+
+static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call,
+                                    TALLOC_CTX *mem_ctx, uint32_t hkey,
+                                    struct policy_handle **outh)
 {
-       struct registry_context *ctx;
-       struct dcesrv_handle *h; 
-       WERROR error;
-       const char *conf = lp_parm_string(-1, "registry", hivename);
-       char *backend, *location;
-       
-       if (!conf) {
-               return WERR_NOT_SUPPORTED;
-       }
+       struct registry_context *ctx = dce_call->context->private_data;
+       struct dcesrv_handle *h;
+       WERROR result;
 
-       backend = talloc_strdup(mem_ctx, conf);
-       location = strchr(backend, ':');
+       h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
 
-       if (location) {
-               *location = '\0';
-               location++;
+       result = reg_get_predefined_key(ctx, hkey,
+                                      (struct registry_key **)&h->data);
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
        }
+       *outh = &h->wire_handle;
 
-       error = reg_open(&ctx, backend, location, NULL); 
-       if(!W_ERROR_IS_OK(error)) return error; 
-       
-       h = dcesrv_handle_new(dce_call->conn, HTYPE_REGKEY); 
-       h->data = ctx->hives[0]->root; 
-       SMB_ASSERT(h->data);
-       h->destroy = winreg_destroy_hive;
-       *outh = &h->wire_handle; 
-       return WERR_OK; 
+       return result;
 }
 
-#define func_winreg_OpenHive(k,n) static WERROR winreg_Open ## k (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_Open ## k *r) \
+#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) \
 { \
-       return winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
+       return dcesrv_winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
 }
 
-func_winreg_OpenHive(HKCR,"HKEY_CLASSES_ROOT")
-func_winreg_OpenHive(HKCU,"HKEY_CURRENT_USER")
-func_winreg_OpenHive(HKLM,"HKEY_LOCAL_MACHINE")
-func_winreg_OpenHive(HKPD,"HKEY_PERFORMANCE_DATA")
-func_winreg_OpenHive(HKU,"HKEY_USERS")
-func_winreg_OpenHive(HKCC,"HKEY_CC")
-func_winreg_OpenHive(HKDD,"HKEY_DD")
-func_winreg_OpenHive(HKPT,"HKEY_PT")
-func_winreg_OpenHive(HKPN,"HKEY_PN")
-
-/* 
-  winreg_CloseKey 
+func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
+func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
+func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
+func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
+func_winreg_OpenHive(HKU,HKEY_USERS)
+func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
+func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
+func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
+func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
+
+/*
+  winreg_CloseKey
 */
-static WERROR winreg_CloseKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_CloseKey *r)
+static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct winreg_CloseKey *r)
 {
-       struct dcesrv_handle *h; 
+       struct dcesrv_handle *h;
+
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
 
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
+       talloc_unlink(dce_call->context, h);
 
-       dcesrv_handle_destroy(dce_call->conn, h);
+       ZERO_STRUCTP(r->out.handle);
 
        return WERR_OK;
 }
 
-
-/* 
-  winreg_CreateKey 
+/*
+  winreg_CreateKey
 */
-static WERROR winreg_CreateKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_CreateKey *r)
+static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct winreg_CreateKey *r)
 {
        struct dcesrv_handle *h, *newh;
-       WERROR error;
-
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
-       
-       newh = dcesrv_handle_new(dce_call->conn, HTYPE_REGKEY);
+       struct security_descriptor sd;
+       struct registry_key *key;
+       WERROR result;
 
-       error = reg_key_add_name(newh, (struct registry_key *)h->data, r->in.key.name, 
-                                                        r->in.access_mask, 
-                                                        r->in.sec_desc?r->in.sec_desc->sd:NULL, 
-                                                        (struct registry_key **)&newh->data);
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-       if(W_ERROR_IS_OK(error)) {
-               r->out.handle = &newh->wire_handle;
-       } else {
-               dcesrv_handle_destroy(dce_call->conn, newh);
+       newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
+
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+               /* the security descriptor is optional */
+               if (r->in.secdesc != NULL) {
+                       DATA_BLOB sdblob;
+                       enum ndr_err_code ndr_err;
+                       sdblob.data = r->in.secdesc->sd.data;
+                       sdblob.length = r->in.secdesc->sd.len;
+                       if (sdblob.data == NULL) {
+                               return WERR_INVALID_PARAM;
+                       }
+                       ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, NULL, &sd,
+                                                          (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
+                       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                               return WERR_INVALID_PARAM;
+                       }
+               }
+               
+               result = reg_key_add_name(newh, key, r->in.name.name, NULL,
+                       r->in.secdesc?&sd:NULL, (struct registry_key **)&newh->data);
+               if (W_ERROR_IS_OK(result)) {
+                       r->out.new_handle = &newh->wire_handle;
+               } else {
+                       talloc_free(newh);
+               }
+               
+               return result;
+       default:
+               return WERR_ACCESS_DENIED;
        }
-
-       return error;
 }
 
 
-/* 
-  winreg_DeleteKey 
+/*
+  winreg_DeleteKey
 */
-static WERROR winreg_DeleteKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_DeleteKey *r)
+static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct winreg_DeleteKey *r)
 {
        struct dcesrv_handle *h;
        struct registry_key *key;
-       WERROR result;
-
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
 
-       result = reg_open_key(mem_ctx, (struct registry_key *)h->data, r->in.key.name, &key);
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-       if (W_ERROR_IS_OK(result)) {
-               return reg_key_del(key);
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+               return reg_key_del(key, r->in.key.name);
+       default:
+               return WERR_ACCESS_DENIED;
        }
-
-       return result;
 }
 
 
-/* 
-  winreg_DeleteValue 
+/*
+  winreg_DeleteValue
 */
-static WERROR winreg_DeleteValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_DeleteValue *r)
+static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
+                                       TALLOC_CTX *mem_ctx,
+                                       struct winreg_DeleteValue *r)
 {
        struct dcesrv_handle *h;
-       struct registry_value *value;
-
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGVAL);
-       DCESRV_CHECK_HANDLE(h);
+       struct registry_key *key;
 
-       value = h->data;
-       
-       /* FIXME */
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-       return WERR_NOT_SUPPORTED;
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+               return reg_del_value(key, r->in.value.name);
+       default:
+               return WERR_ACCESS_DENIED;
+       }
 }
 
 
-/* 
-  winreg_EnumKey 
+/*
+  winreg_EnumKey
 */
-static WERROR winreg_EnumKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_EnumKey *r)
+static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct winreg_EnumKey *r)
 {
        struct dcesrv_handle *h;
        struct registry_key *key;
+       const char *name, *classname;
+       NTTIME last_mod;
+       WERROR result;
+
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
+       result = reg_key_get_subkey_by_index(mem_ctx, 
+               key, r->in.enum_index, &name, &classname, &last_mod);
 
-       r->out.result = reg_key_get_subkey_by_index(mem_ctx, (struct registry_key *)h->data, r->in.enum_index, &key);
+       if (2*strlen_m_term(name) > r->in.name->size) {
+               return WERR_MORE_DATA;
+       }
+
+       if (name != NULL) {
+               r->out.name->name = name;
+               r->out.name->length = 2*strlen_m_term(name);
+       } else {
+               r->out.name->name = r->in.name->name;
+               r->out.name->length = r->in.name->length;
+       }
+       r->out.name->size = r->in.name->size;
 
-       if (W_ERROR_IS_OK(r->out.result)) {
-               r->out.key_name_len = strlen(key->name);
-               r->out.out_name = talloc_zero_p(mem_ctx, struct winreg_EnumKeyNameResponse);
-               r->out.out_name->name = key->name;
-               r->out.class = talloc_zero_p(mem_ctx, struct winreg_String);
-               r->out.last_changed_time = talloc_zero_p(mem_ctx, struct winreg_Time);
+       r->out.keyclass = r->in.keyclass;
+       if (classname != NULL) {
+               r->out.keyclass->name = classname;
+               r->out.keyclass->length = 2*strlen_m_term(classname);
+       } else {
+               r->out.keyclass->name = r->in.keyclass->name;
+               r->out.keyclass->length = r->in.keyclass->length;
        }
-       
-       return r->out.result;
+       r->out.keyclass->size = r->in.keyclass->size;
+
+       if (r->in.last_changed_time != NULL)
+               r->out.last_changed_time = &last_mod;
+
+       return result;
 }
 
 
-/* 
-  winreg_EnumValue 
+/*
+  winreg_EnumValue
 */
-static WERROR winreg_EnumValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_EnumValue *r)
+static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct winreg_EnumValue *r)
 {
        struct dcesrv_handle *h;
        struct registry_key *key;
-       struct registry_value *value;
+       const char *data_name;
+       uint32_t data_type;
+       DATA_BLOB data;
        WERROR result;
 
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
-
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
        key = h->data;
 
-       result = reg_key_get_value_by_index(mem_ctx, key, r->in.enum_index, &value);
+       result = reg_key_get_value_by_index(mem_ctx, key,
+               r->in.enum_index, &data_name, &data_type, &data);
+
        if (!W_ERROR_IS_OK(result)) {
-               return result;
+               /* if the lookup wasn't successful, send client query back */
+               data_name = r->in.name->name;
+               data_type = *r->in.type;
+               data.data = r->in.value;
+               data.length = *r->in.length;
        }
-       
-       r->out.type = &value->data_type;
-       r->out.name_out.name = value->name;
-       r->out.value_out = talloc_p(mem_ctx, struct EnumValueOut);
-       r->out.value_out->offset = 0;
-       r->out.value_out->buffer = data_blob_talloc(mem_ctx, value->data_blk, value->data_len);
-       r->out.value_len1 = r->in.value_len1;
-       r->out.value_len2 = r->in.value_len2;
-       
 
-       return WERR_OK;
+       /* check if there is enough room for the name */
+       if (r->in.name->size < 2*strlen_m_term(data_name)) {
+               return WERR_MORE_DATA;
+       }
+
+       /* "data_name" is NULL when we query the default attribute */
+       if (data_name != NULL) {
+               r->out.name->name = data_name;
+               r->out.name->length = 2*strlen_m_term(data_name);
+       } else {
+               r->out.name->name = r->in.name->name;
+               r->out.name->length = r->in.name->length;
+       }
+       r->out.name->size = r->in.name->size;
+
+       r->out.type = talloc(mem_ctx, uint32_t);
+       if (!r->out.type) {
+               return WERR_NOMEM;
+       }
+       *r->out.type = data_type;
+
+       /* check the client has enough room for the value */
+       if (r->in.value != NULL &&
+           r->in.size != NULL &&
+           data.length > *r->in.size) {
+               return WERR_MORE_DATA;
+       }
+
+       if (r->in.value != NULL) {
+               r->out.value = data.data;
+       }
+
+       if (r->in.size != NULL) {
+               r->out.size = talloc(mem_ctx, uint32_t);
+               *r->out.size = data.length;
+               r->out.length = r->out.size;
+       }
+
+       return result;
 }
 
 
-/* 
-  winreg_FlushKey 
+/*
+  winreg_FlushKey
 */
-static WERROR winreg_FlushKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_FlushKey *r)
+static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct winreg_FlushKey *r)
 {
        struct dcesrv_handle *h;
+       struct registry_key *key;
 
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-       return reg_key_flush(h->data);
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+               return reg_key_flush(key);
+       default:
+               return WERR_ACCESS_DENIED;
+       }
 }
 
 
-/* 
-  winreg_GetKeySecurity 
+/*
+  winreg_GetKeySecurity
 */
-static WERROR winreg_GetKeySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_GetKeySecurity *r)
+static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call,
+                                          TALLOC_CTX *mem_ctx,
+                                          struct winreg_GetKeySecurity *r)
 {
+       struct dcesrv_handle *h;
+
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_LoadKey 
+/*
+  winreg_LoadKey
 */
-static WERROR winreg_LoadKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_LoadKey *r)
+static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct winreg_LoadKey *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_NotifyChangeKeyValue 
+/*
+  winreg_NotifyChangeKeyValue
 */
-static WERROR winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_NotifyChangeKeyValue *r)
+static WERROR dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call,
+                                                TALLOC_CTX *mem_ctx,
+                                                struct winreg_NotifyChangeKeyValue *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_OpenKey 
+/*
+  winreg_OpenKey
 */
-static WERROR winreg_OpenKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_OpenKey *r)
+static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct winreg_OpenKey *r)
 {
        struct dcesrv_handle *h, *newh;
+       struct registry_key *key;
        WERROR result;
 
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
-
-       newh = dcesrv_handle_new(dce_call->conn, HTYPE_REGKEY);
-
-       result = reg_open_key(newh, (struct registry_key *)h->data, 
-                               r->in.keyname.name, (struct registry_key **)&newh->data);
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
+       key = h->data;
 
-       if (W_ERROR_IS_OK(result)) {
-               r->out.handle = &newh->wire_handle; 
-       } else {
-               dcesrv_handle_destroy(dce_call->conn, newh);
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+       case SECURITY_USER:
+               if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
+                       newh = talloc_reference(dce_call->context, h);
+                       result = WERR_OK;
+               } else {
+                       newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
+                       result = reg_open_key(newh, key, r->in.keyname.name,
+                               (struct registry_key **)&newh->data);
+               }
+               
+               if (W_ERROR_IS_OK(result)) {
+                       r->out.handle = &newh->wire_handle;
+               } else {
+                       talloc_free(newh);
+               }
+               return result;
+       default:
+               return WERR_ACCESS_DENIED;
        }
-       
-       return result;
 }
 
 
-/* 
-  winreg_QueryInfoKey 
+/*
+  winreg_QueryInfoKey
 */
-static WERROR winreg_QueryInfoKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_QueryInfoKey *r)
+static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
+                                        TALLOC_CTX *mem_ctx,
+                                        struct winreg_QueryInfoKey *r)
 {
        struct dcesrv_handle *h;
-       struct registry_key *k;
-       WERROR ret;
+       struct registry_key *key;
+       const char *classname = NULL;
+       WERROR result;
 
-       h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
-       DCESRV_CHECK_HANDLE(h);
-       k = h->data;
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-       ret = reg_key_num_subkeys(k, &r->out.num_subkeys);
-       if (!W_ERROR_IS_OK(ret)) { 
-               return ret;
-       }
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+       case SECURITY_USER:
+               result = reg_key_get_info(mem_ctx, key, &classname,
+                       r->out.num_subkeys, r->out.num_values,
+                       r->out.last_changed_time, r->out.max_subkeylen,
+                       r->out.max_valnamelen, r->out.max_valbufsize);
+
+               if (classname != NULL) {
+                       r->out.classname->name = classname;
+                       r->out.classname->name_len = 2*strlen_m_term(classname);
+               } else {
+                       r->out.classname->name = r->in.classname->name;
+                       r->out.classname->name_len = r->in.classname->name_len;
+               }
+               r->out.classname->name_size = r->in.classname->name_size;
 
-       ret = reg_key_num_values(k, &r->out.num_values);
-       if (!W_ERROR_IS_OK(ret)) { 
-               return ret;
+               return result;
+       default:
+               return WERR_ACCESS_DENIED;
        }
+}
 
-       ret = reg_key_subkeysizes(k, &r->out.max_subkeysize, &r->out.max_subkeylen);
-       if (!W_ERROR_IS_OK(ret)) { 
-               return ret;
-       }
 
-       ret = reg_key_valuesizes(k, &r->out.max_valnamelen, &r->out.max_valbufsize);
-       if (!W_ERROR_IS_OK(ret)) { 
-               return ret;
-       }
+/*
+  winreg_QueryValue
+*/
+static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct winreg_QueryValue *r)
+{
+       struct dcesrv_handle *h;
+       struct registry_key *key;
+       uint32_t value_type;
+       DATA_BLOB value_data;
+       WERROR result;
 
-       r->out.secdescsize = 0; /* FIXME */
-       ZERO_STRUCT(r->out.last_changed_time); /* FIXME */      if (!W_ERROR_IS_OK(ret)) { 
-               return ret;
-       }
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+       case SECURITY_USER:
+               result = reg_key_get_value_by_name(mem_ctx, key, 
+                        r->in.value_name->name, &value_type, &value_data);
+               
+               if (!W_ERROR_IS_OK(result)) {
+                       /* if the lookup wasn't successful, send client query back */
+                       value_type = *r->in.type;
+                       value_data.data = r->in.data;
+                       value_data.length = *r->in.data_length;
+               }
+
+               r->out.type = talloc(mem_ctx, uint32_t);
+               if (!r->out.type) {
+                       return WERR_NOMEM;
+               }
+               *r->out.type = value_type;
+               r->out.data_length = talloc(mem_ctx, uint32_t);
+               if (!r->out.data_length) {
+                       return WERR_NOMEM;
+               }
+               *r->out.data_length = value_data.length;
+               r->out.data_size = talloc(mem_ctx, uint32_t);
+               if (!r->out.data_size) {
+                       return WERR_NOMEM;
+               }
+               *r->out.data_size = value_data.length;
+               r->out.data = value_data.data;
 
-       return WERR_OK;
+               return result;
+       default:
+               return WERR_ACCESS_DENIED;
+       }
 }
 
 
-/* 
-  winreg_QueryValue 
+/*
+  winreg_ReplaceKey
 */
-static WERROR winreg_QueryValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_QueryValue *r)
+static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct winreg_ReplaceKey *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_ReplaceKey 
+/*
+  winreg_RestoreKey
 */
-static WERROR winreg_ReplaceKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_ReplaceKey *r)
+static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct winreg_RestoreKey *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_RestoreKey 
+/*
+  winreg_SaveKey
 */
-static WERROR winreg_RestoreKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_RestoreKey *r)
+static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct winreg_SaveKey *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_SaveKey 
+/*
+  winreg_SetKeySecurity
 */
-static WERROR winreg_SaveKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_SaveKey *r)
+static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call,
+                                          TALLOC_CTX *mem_ctx,
+                                          struct winreg_SetKeySecurity *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_SetKeySecurity 
+/*
+  winreg_SetValue
 */
-static WERROR winreg_SetKeySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_SetKeySecurity *r)
+static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct winreg_SetValue *r)
 {
-       return WERR_NOT_SUPPORTED;
-}
+       struct dcesrv_handle *h;
+       struct registry_key *key;
+       DATA_BLOB data;
+       WERROR result;
 
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
-/* 
-  winreg_SetValue 
-*/
-static WERROR winreg_SetValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_SetValue *r)
-{
-       return WERR_NOT_SUPPORTED;
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       {
+       case SECURITY_SYSTEM:
+       case SECURITY_ADMINISTRATOR:
+               data.data = r->in.data;
+               data.length = r->in.size;
+               result = reg_val_set(key, r->in.name.name, r->in.type, data);
+               return result;
+       default:
+               return WERR_ACCESS_DENIED;
+       }
 }
 
 
-/* 
-  winreg_UnLoadKey 
+/*
+  winreg_UnLoadKey
 */
-static WERROR winreg_UnLoadKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_UnLoadKey *r)
+static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct winreg_UnLoadKey *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_InitiateSystemShutdown 
+/*
+  winreg_InitiateSystemShutdown
 */
-static WERROR winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_InitiateSystemShutdown *r)
+static WERROR dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call,
+                                                  TALLOC_CTX *mem_ctx,
+                                                  struct winreg_InitiateSystemShutdown *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_AbortSystemShutdown 
+/*
+  winreg_AbortSystemShutdown
 */
-static WERROR winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_AbortSystemShutdown *r)
+static WERROR dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call,
+                                               TALLOC_CTX *mem_ctx,
+                                               struct winreg_AbortSystemShutdown *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_GetVersion 
+/*
+  winreg_GetVersion
 */
-static WERROR winreg_GetVersion(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_GetVersion *r)
+static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct winreg_GetVersion *r)
 {
-       r->out.version = 5;
+       struct dcesrv_handle *h;
+
+       DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+
+       r->out.version = talloc(mem_ctx, uint32_t);
+       W_ERROR_HAVE_NO_MEMORY(r->out.version);
+
+       *r->out.version = 5;
+
        return WERR_OK;
 }
 
 
-/* 
-  winreg_QueryMultipleValues 
+/*
+  winreg_QueryMultipleValues
 */
-static WERROR winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_QueryMultipleValues *r)
+static WERROR dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call,
+                                               TALLOC_CTX *mem_ctx,
+                                               struct winreg_QueryMultipleValues *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_InitiateSystemShutdownEx 
+/*
+  winreg_InitiateSystemShutdownEx
 */
-static WERROR winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_InitiateSystemShutdownEx *r)
+static WERROR dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call,
+                                                    TALLOC_CTX *mem_ctx,
+                                                    struct winreg_InitiateSystemShutdownEx *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_SaveKeyEx 
+/*
+  winreg_SaveKeyEx
 */
-static WERROR winreg_SaveKeyEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_SaveKeyEx *r)
+static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct winreg_SaveKeyEx *r)
 {
        return WERR_NOT_SUPPORTED;
 }
 
 
-/* 
-  winreg_QueryMultipleValues2 
+/*
+  winreg_QueryMultipleValues2
 */
-static WERROR winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct winreg_QueryMultipleValues2 *r)
+static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call,
+                                                TALLOC_CTX *mem_ctx,
+                                                struct winreg_QueryMultipleValues2 *r)
 {
        return WERR_NOT_SUPPORTED;
 }