Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / source4 / rpc_server / winreg / rpc_winreg.c
index dbb51b9baf34dc2383186d9299d4db27b1a0e376..575902a164e85ff0475e1f8ef9b3f6b45bb19a37 100644 (file)
@@ -24,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 };
@@ -38,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)) {
@@ -46,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;
 }
@@ -57,18 +56,18 @@ 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 result;
 
        h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
+       W_ERROR_HAVE_NO_MEMORY(h);
 
        result = reg_get_predefined_key(ctx, hkey,
                                       (struct registry_key **)&h->data);
        if (!W_ERROR_IS_OK(result)) {
                return result;
        }
-
        *outh = &h->wire_handle;
 
        return result;
@@ -100,7 +99,7 @@ static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
 
        DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
 
-       talloc_free(h);
+       talloc_unlink(dce_call->context, h);
 
        ZERO_STRUCTP(r->out.handle);
 
@@ -116,16 +115,23 @@ static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
 {
        struct dcesrv_handle *h, *newh;
        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);
 
-       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
+               /* we support only non volatile keys */
+               if (r->in.options != REG_OPTION_NON_VOLATILE) {
+                       return WERR_NOT_SUPPORTED;
+               }
+
                /* the security descriptor is optional */
                if (r->in.secdesc != NULL) {
                        DATA_BLOB sdblob;
@@ -135,22 +141,30 @@ static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
                        if (sdblob.data == NULL) {
                                return WERR_INVALID_PARAM;
                        }
-                       ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, NULL, &sd,
+                       ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, &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, (struct registry_key *)h->data,
-                                        r->in.name.name, NULL, r->in.secdesc?&sd:NULL,
-                                        (struct registry_key **)&newh->data);
+               result = reg_key_add_name(newh, key, r->in.name.name, NULL,
+                       r->in.secdesc?&sd:NULL, (struct registry_key **)&newh->data);
+
+               r->out.action_taken = talloc(mem_ctx, enum winreg_CreateAction);
+               if (r->out.action_taken == NULL) {
+                       talloc_free(newh);
+                       return WERR_NOMEM;
+               }
+               *r->out.action_taken = REG_ACTION_NONE;
+
                if (W_ERROR_IS_OK(result)) {
                        r->out.new_handle = &newh->wire_handle;
+                       *r->out.action_taken = REG_CREATED_NEW_KEY;
                } else {
                        talloc_free(newh);
                }
-               
+
                return result;
        default:
                return WERR_ACCESS_DENIED;
@@ -166,14 +180,20 @@ static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
                                      struct winreg_DeleteKey *r)
 {
        struct dcesrv_handle *h;
+       struct registry_key *key;
+       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))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
-               return reg_key_del((struct registry_key *)h->data, r->in.key.name);
+               result = reg_key_del(mem_ctx, key, r->in.key.name);
+               talloc_unlink(dce_call->context, h);
+
+               return result;
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -188,14 +208,16 @@ static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
                                        struct winreg_DeleteValue *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))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
-               return reg_del_value((struct registry_key *)h->data, r->in.value.name);
+               return reg_del_value(mem_ctx, key, r->in.value.name);
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -210,15 +232,16 @@ static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
                                    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;
 
        result = reg_key_get_subkey_by_index(mem_ctx, 
-               (struct registry_key *)h->data, r->in.enum_index,
-               &name, &classname, &last_mod);
+               key, r->in.enum_index, &name, &classname, &last_mod);
 
        if (2*strlen_m_term(name) > r->in.name->size) {
                return WERR_MORE_DATA;
@@ -265,10 +288,9 @@ static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
        WERROR result;
 
        DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
-
        key = h->data;
 
-       result = reg_key_get_value_by_index(mem_ctx, (struct registry_key *)h->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)) {
@@ -279,25 +301,6 @@ static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
                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 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;
-       }
-
-       /* and 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;
@@ -308,6 +311,19 @@ static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
        }
        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;
        }
@@ -330,14 +346,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))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
-               return reg_key_flush(h->data);
+               return reg_key_flush(key);
        default:
                return WERR_ACCESS_DENIED;
        }
@@ -389,11 +407,13 @@ 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))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
@@ -403,9 +423,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)) {
@@ -417,7 +436,6 @@ static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
        default:
                return WERR_ACCESS_DENIED;
        }
-
 }
 
 
@@ -429,20 +447,31 @@ static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
                                         struct winreg_QueryInfoKey *r)
 {
        struct dcesrv_handle *h;
+       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))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
        case SECURITY_USER:
-               result = reg_key_get_info(mem_ctx, (struct registry_key *)h->data,
-                        &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);
+               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 (r->out.max_subkeylen != NULL) {
+                       /* for UTF16 encoding */
+                       *r->out.max_subkeylen *= 2;
+               }
+               if (r->out.max_valnamelen != NULL) {
+                       /* for UTF16 encoding */
+                       *r->out.max_valnamelen *= 2;
+               }
 
                if (classname != NULL) {
                        r->out.classname->name = classname;
@@ -468,45 +497,54 @@ static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
                                       struct winreg_QueryValue *r)
 {
        struct dcesrv_handle *h;
+       struct registry_key *key;
        uint32_t value_type;
        DATA_BLOB value_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))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
        case SECURITY_USER:
-               result = reg_key_get_value_by_name(mem_ctx,
-                       (struct registry_key *)h->data, r->in.value_name.name,
-                                                  &value_type, &value_data);
+               if ((r->in.type == NULL) || (r->in.data_length == NULL) ||
+                   (r->in.data_size == NULL)) {
+                       return WERR_INVALID_PARAM;
+               }
+
+               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.length;
+                       value_data.length = *r->in.data_length;
+               } else {
+                       if ((r->in.data != NULL)
+                           && (*r->in.data_size < value_data.length)) {
+                               result = WERR_MORE_DATA;
+                       }
                }
 
-               /* 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;
-               r->out.size = talloc(mem_ctx, uint32_t);
-               if (!r->out.size) {
+               *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.size = value_data.length;
-
+               *r->out.data_size = value_data.length;
                r->out.data = value_data.data;
 
                return result;
@@ -568,19 +606,20 @@ static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
                                     struct winreg_SetValue *r)
 {
        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;
 
-       switch (security_session_user_level(dce_call->conn->auth_state.session_info))
+       switch (security_session_user_level(dce_call->conn->auth_state.session_info, NULL))
        {
        case SECURITY_SYSTEM:
        case SECURITY_ADMINISTRATOR:
                data.data = r->in.data;
                data.length = r->in.size;
-               result = reg_val_set((struct registry_key *)h->data,
-                       r->in.name.name, r->in.type, data);
+               result = reg_val_set(key, r->in.name.name, r->in.type, data);
                return result;
        default:
                return WERR_ACCESS_DENIED;
@@ -684,6 +723,15 @@ static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_c
        return WERR_NOT_SUPPORTED;
 }
 
+/*
+  winreg_DeleteKeyEx
+*/
+static WERROR dcesrv_winreg_DeleteKeyEx(struct dcesrv_call_state *dce_call,
+                                       TALLOC_CTX *mem_ctx,
+                                       struct winreg_DeleteKeyEx *r)
+{
+       return WERR_NOT_SUPPORTED;
+}
 
 /* include the generated boilerplate */
 #include "librpc/gen_ndr/ndr_winreg_s.c"