s4:WINREG RPC server - Cosmetic
[ira/wip.git] / source4 / rpc_server / winreg / rpc_winreg.c
index 22c60c354c5002b14a5469306212c5feb7f86360..86617a606a34d7984f2720a8551e2d7db15a30ec 100644 (file)
@@ -3,7 +3,8 @@
 
    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
@@ -23,9 +24,7 @@
 #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 "param/param.h"
 #include "libcli/security/security.h"
 
 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
@@ -37,7 +36,8 @@ static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
        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,
+                            &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)) {
@@ -45,7 +45,7 @@ static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       dce_call->context->private = ctx;
+       dce_call->context->private_data = ctx;
 
        return NT_STATUS_OK;
 }
@@ -56,21 +56,20 @@ 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 = dce_call->context->private;
+       struct registry_context *ctx = dce_call->context->private_data;
        struct dcesrv_handle *h;
-       WERROR error;
+       WERROR result;
 
        h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
 
-       error = reg_get_predefined_key(ctx, hkey,
+       result = reg_get_predefined_key(ctx, hkey,
                                       (struct registry_key **)&h->data);
-       if (!W_ERROR_IS_OK(error)) {
-               return error;
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
        }
-
        *outh = &h->wire_handle;
 
-       return error;
+       return result;
 }
 
 #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) \
@@ -99,8 +98,6 @@ static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
 
        DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
 
-       talloc_free(h);
-
        ZERO_STRUCTP(r->out.handle);
 
        return WERR_OK;
@@ -114,10 +111,12 @@ static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
                                      struct winreg_CreateKey *r)
 {
        struct dcesrv_handle *h, *newh;
-       WERROR error;
        struct security_descriptor sd;
+       struct registry_key *key;
+       WERROR result;
 
        DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
+       key = h->data;
 
        newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
 
@@ -141,16 +140,15 @@ static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
                        }
                }
                
-               error = reg_key_add_name(newh, (struct registry_key *)h->data,
-                                        r->in.name.name, NULL, r->in.secdesc?&sd:NULL,
-                                        (struct registry_key **)&newh->data);
-               if (W_ERROR_IS_OK(error)) {
+               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 error;
+               return result;
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -165,14 +163,16 @@ static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
                                      struct winreg_DeleteKey *r)
 {
        struct dcesrv_handle *h;
+       struct registry_key *key;
 
        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:
-               return reg_key_del((struct registry_key *)h->data, r->in.key.name);
+               return reg_key_del(key, r->in.key.name);
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -190,13 +190,12 @@ static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
        struct registry_key *key;
 
        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:
-               key = h->data;
-               
                return reg_del_value(key, r->in.value.name);
        default:
                return WERR_ACCESS_DENIED;
@@ -212,29 +211,44 @@ static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
                                    struct winreg_EnumKey *r)
 {
        struct dcesrv_handle *h;
-       const char *name;
+       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;
 
-       r->out.result = reg_key_get_subkey_by_index(mem_ctx,
-                                                   (struct registry_key *)h->data,
-                                                   r->in.enum_index,
-                                                   &name, NULL, &last_mod);
+       result = reg_key_get_subkey_by_index(mem_ctx, 
+               key, r->in.enum_index, &name, &classname, &last_mod);
 
-       if (W_ERROR_IS_OK(r->out.result)) {
-               if (2*strlen_m_term(name) > r->in.name->size) {
-                       return WERR_MORE_DATA;
-               }
-               r->out.name->length = 2*strlen_m_term(name);
+       if (2*strlen_m_term(name) > r->in.name->size) {
+               return WERR_MORE_DATA;
+       }
+
+       if (name != NULL) {
                r->out.name->name = name;
-               r->out.keyclass = talloc_zero(mem_ctx, struct winreg_StringBuf);
-               if (r->in.last_changed_time) {
-                       r->out.last_changed_time = &last_mod;
-               }
+               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;
+
+       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;
+       }
+       r->out.keyclass->size = r->in.keyclass->size;
 
-       return r->out.result;
+       if (r->in.last_changed_time != NULL)
+               r->out.last_changed_time = &last_mod;
+
+       return result;
 }
 
 
@@ -247,28 +261,45 @@ static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
 {
        struct dcesrv_handle *h;
        struct registry_key *key;
-       WERROR result;
        const char *data_name;
        uint32_t data_type;
        DATA_BLOB data;
+       WERROR result;
 
        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,
-                                           &data_name,
-                                           &data_type, &data);
+       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;
        }
 
-       /* the client can optionally pass a NULL for type, meaning they don't
-          want that back */
-       if (r->in.type != NULL) {
-               r->out.type = talloc(mem_ctx, enum winreg_Type);
-               *r->out.type = data_type;
+       /* 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 &&
@@ -277,26 +308,17 @@ static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
                return WERR_MORE_DATA;
        }
 
-       /* and enough room for the name */
-       if (r->in.name->size < 2*strlen_m_term(data_name)) {
-               return WERR_MORE_DATA;
-       }
-
-       r->out.name->name = data_name;
-       r->out.name->length = 2*strlen_m_term(data_name);
-       r->out.name->size = r->in.name->size;
-
-       if (r->in.value) {
+       if (r->in.value != NULL) {
                r->out.value = data.data;
        }
 
-       if (r->in.size) {
+       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 WERR_OK;
+       return result;
 }
 
 
@@ -308,14 +330,16 @@ static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
                                     struct winreg_FlushKey *r)
 {
        struct dcesrv_handle *h;
+       struct registry_key *key;
 
        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:
-               return reg_key_flush(h->data);
+               return reg_key_flush(key);
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -367,9 +391,11 @@ static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
                                    struct winreg_OpenKey *r)
 {
        struct dcesrv_handle *h, *newh;
+       struct registry_key *key;
        WERROR result;
 
        DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
+       key = h->data;
 
        switch (security_session_user_level(dce_call->conn->auth_state.session_info))
        {
@@ -381,9 +407,8 @@ static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
                        result = WERR_OK;
                } else {
                        newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
-                       result = reg_open_key(newh, (struct registry_key *)h->data,
-                                             r->in.keyname.name,
-                                             (struct registry_key **)&newh->data);
+                       result = reg_open_key(newh, key, r->in.keyname.name,
+                               (struct registry_key **)&newh->data);
                }
                
                if (W_ERROR_IS_OK(result)) {
@@ -395,7 +420,6 @@ static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
        default:
                return WERR_ACCESS_DENIED;
        }
-
 }
 
 
@@ -407,28 +431,33 @@ static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
                                         struct winreg_QueryInfoKey *r)
 {
        struct dcesrv_handle *h;
-       struct registry_key *k;
-       WERROR ret;
+       struct registry_key *key;
        const char *classname = NULL;
+       WERROR result;
 
        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:
-               k = h->data;
-               
-               ret = reg_key_get_info(mem_ctx, k, &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 (r->out.classname != NULL)
+               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;
-               
-               return ret;
+                       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;
+
+               return result;
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -449,41 +478,41 @@ static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
        WERROR result;
 
        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:
-               key = h->data;
-               
-               result = reg_key_get_value_by_name(mem_ctx, key, r->in.value_name.name,
-                                                  &value_type, &value_data);
+               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)) {
-                       return 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;
                }
-               
-               /* Just asking for the size of the buffer */
+
                r->out.type = talloc(mem_ctx, uint32_t);
                if (!r->out.type) {
                        return WERR_NOMEM;
                }
                *r->out.type = value_type;
-               r->out.length = talloc(mem_ctx, uint32_t);
-               if (!r->out.length) {
+               r->out.data_length = talloc(mem_ctx, uint32_t);
+               if (!r->out.data_length) {
                        return WERR_NOMEM;
                }
-               *r->out.length = value_data.length;
-               if (r->in.data == NULL) {
-                       r->out.size = talloc(mem_ctx, uint32_t);
-                       *r->out.size = value_data.length;
-               } else {
-                       r->out.size = r->in.size;
-                       r->out.data = value_data.data;
+               *r->out.data_length = value_data.length;
+               r->out.data_size = talloc(mem_ctx, uint32_t);
+               if (!r->out.data_size) {
+                       return WERR_NOMEM;
                }
-               
-               return WERR_OK;
+               *r->out.data_size = value_data.length;
+               r->out.data = value_data.data;
+
+               return result;
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -543,11 +572,10 @@ static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
 {
        struct dcesrv_handle *h;
        struct registry_key *key;
-       WERROR result;
        DATA_BLOB data;
+       WERROR result;
 
        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))