From 5bf62260505d1e8dfddcb3acef69ef0217841a3f Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Fri, 12 Sep 2008 20:26:13 +0200 Subject: Registry client: Various cleanups and charset conversions This patch contains various changes to the "rpc.c" file to clean it up and introduces the right string charset conversions (UNIX <-> UTF16). --- source4/lib/registry/rpc.c | 191 +++++++++++++++++++++++++++++---------------- 1 file changed, 125 insertions(+), 66 deletions(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index 18b7607713..3fdba02852 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -19,6 +19,7 @@ #include "includes.h" #include "registry.h" #include "librpc/gen_ndr/ndr_winreg_c.h" +#include "param/param.h" struct rpc_key { struct registry_key key; @@ -43,26 +44,82 @@ static struct registry_operations reg_backend_rpc; * This is the RPC backend for the registry library. */ -static void init_winreg_String(struct winreg_String *name, const char *s) +/* + * Converts a SAMBA (UNIX) string into a WINREG (UTF16) string + */ +static void chars_to_winreg_String(TALLOC_CTX *mem_ctx, struct winreg_String + *winregStr, const char *str) { - name->name = s; + winregStr->name = NULL; + winregStr->name_len = 0; + if (str != NULL) + winregStr->name_len = convert_string_talloc(mem_ctx, + lp_iconv_convenience(global_loadparm), CH_UNIX, + CH_UTF16, (void *) str, strlen(str), + (void **) &winregStr->name); + winregStr->name_size = winregStr->name_len; } +/* + * Converts a WINREG (UTF16) string into a SAMBA (UNIX) string + */ +static void winreg_String_to_chars(TALLOC_CTX *mem_ctx, const char **str, + struct winreg_String *winregStr) +{ + *str = NULL; + if (winregStr->name != NULL) + convert_string_talloc(mem_ctx, + lp_iconv_convenience(global_loadparm), CH_UTF16, + CH_UNIX, (void *) winregStr->name, winregStr->name_len, + (void **) str); +} + +/* + * Converts a SAMBA (UNIX) string into a WINREG (UTF16) string buffer + */ +static void chars_to_winreg_StringBuf(TALLOC_CTX *mem_ctx, struct winreg_StringBuf + *winregStrBuf, const char *str, uint16_t size) +{ + winregStrBuf->name = NULL; + winregStrBuf->length = 0; + if (str != NULL) + winregStrBuf->length = convert_string_talloc(mem_ctx, + lp_iconv_convenience(global_loadparm), CH_UNIX, + CH_UTF16, (void *) str, strlen(str), + (void **) &winregStrBuf->name); + winregStrBuf->size = size; +} + +/* + * Converts a WINREG (UTF16) string buffer into a SAMBA (UNIX) string + */ +static void winreg_StringBuf_to_chars(TALLOC_CTX *mem_ctx, const char **str, + struct winreg_StringBuf *winregStrBuf) +{ + *str = NULL; + if (winregStrBuf->name != NULL) + convert_string_talloc(mem_ctx, + lp_iconv_convenience(global_loadparm), CH_UTF16, + CH_UNIX, (void *) winregStrBuf->name, + winregStrBuf->length, (void **) str); +} #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \ { \ struct winreg_Open ## u r; \ NTSTATUS status; \ - \ +\ + ZERO_STRUCT(r); \ r.in.system_name = NULL; \ r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \ r.out.handle = hnd;\ - \ +\ status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \ - if (NT_STATUS_IS_ERR(status)) {\ - DEBUG(0,("Error executing open\n"));\ - return ntstatus_to_werror(status);\ - }\ +\ + if (!NT_STATUS_IS_OK(status)) { \ + DEBUG(1, ("OpenHive failed - %s\n", nt_errstr(status))); \ + return ntstatus_to_werror(status); \ + } \ \ return r.out.result;\ } @@ -133,9 +190,9 @@ static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k) /* Then, open the handle using the hive */ - memset(&r, 0, sizeof(struct winreg_OpenKey)); + ZERO_STRUCT(r); r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol); - init_winreg_String(&r.in.keyname, k->path); + chars_to_winreg_String(mem_ctx, &r.in.keyname, k->path); r.in.unknown = 0x00000000; r.in.access_mask = 0x02000000; r.out.handle = &mykeydata->pol; @@ -166,14 +223,15 @@ static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, /* Then, open the handle using the hive */ ZERO_STRUCT(r); r.in.parent_handle = &parentkeydata->pol; - init_winreg_String(&r.in.keyname, name); + chars_to_winreg_String(mem_ctx, &r.in.keyname, name); r.in.unknown = 0x00000000; r.in.access_mask = 0x02000000; r.out.handle = &mykeydata->pol; status = dcerpc_winreg_OpenKey(mykeydata->pipe, mem_ctx, &r); - if (NT_STATUS_IS_ERR(status)) { - DEBUG(0,("Error executing openkey: %s\n", nt_errstr(status))); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("OpenKey failed - %s\n", nt_errstr(status))); return ntstatus_to_werror(status); } @@ -188,47 +246,45 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, DATA_BLOB *data) { struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key); - WERROR error; struct winreg_EnumValue r; - uint32_t in_type = 0; - NTSTATUS status; struct winreg_StringBuf name; - uint32_t zero = 0; - - ZERO_STRUCT(r); + uint8_t value; + uint32_t zero = 0, zero2 = 0; + WERROR error; + NTSTATUS status; if (mykeydata->num_values == -1) { error = rpc_query_key(parent); if(!W_ERROR_IS_OK(error)) return error; } - name.length = 0; - name.size = mykeydata->max_valnamelen * 2; - name.name = NULL; + chars_to_winreg_StringBuf(mem_ctx, &name, "", 1024); + ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; r.in.enum_index = n; r.in.name = &name; - r.in.type = &in_type; - r.in.value = talloc_zero_array(mem_ctx, uint8_t, 0); - r.in.length = &zero; + r.in.type = type; + r.in.value = &value; r.in.size = &mykeydata->max_valdatalen; + r.in.length = &zero; r.out.name = &name; r.out.type = type; + r.out.value = &value; + r.out.size = &zero2; + r.out.length = &zero; status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r); - if(NT_STATUS_IS_ERR(status)) { - DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status))); - return WERR_GENERAL_FAILURE; - } - if(NT_STATUS_IS_OK(status) && - W_ERROR_IS_OK(r.out.result) && r.out.length) { - *value_name = talloc_strdup(mem_ctx, r.out.name->name); - *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length); - return WERR_OK; + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("EnumValue failed - %s\n", nt_errstr(status))); + return ntstatus_to_werror(status); } + winreg_StringBuf_to_chars(mem_ctx, value_name, r.out.name); + *type = *(r.out.type); + *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length); + return r.out.result; } @@ -241,34 +297,33 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, { struct winreg_EnumKey r; struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key); - NTSTATUS status; struct winreg_StringBuf namebuf, classbuf; NTTIME change_time = 0; + NTSTATUS status; - ZERO_STRUCT(r); - - namebuf.length = 0; - namebuf.size = 1024; - namebuf.name = NULL; - classbuf.length = 0; - classbuf.size = 0; - classbuf.name = NULL; + chars_to_winreg_StringBuf(mem_ctx, &namebuf, "", 1024); + chars_to_winreg_StringBuf(mem_ctx, &classbuf, NULL, 0); + ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; r.in.enum_index = n; r.in.name = &namebuf; r.in.keyclass = &classbuf; r.in.last_changed_time = &change_time; - r.out.name = &namebuf; + r.out.keyclass = &classbuf; + r.out.last_changed_time = &change_time; status = dcerpc_winreg_EnumKey(mykeydata->pipe, mem_ctx, &r); - if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) { - *name = talloc_strdup(mem_ctx, r.out.name->name); - if (keyclass != NULL) - *keyclass = talloc_strdup(mem_ctx, r.out.keyclass->name); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("EnumKey failed - %s\n", nt_errstr(status))); + return ntstatus_to_werror(status); } + winreg_StringBuf_to_chars(mem_ctx, name, r.out.name); + winreg_StringBuf_to_chars(mem_ctx, keyclass, r.out.keyclass); + return r.out.result; } @@ -278,19 +333,22 @@ static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, struct security_descriptor *sec, struct registry_key **key) { - NTSTATUS status; struct winreg_CreateKey r; struct rpc_key *parentkd = talloc_get_type(parent, struct rpc_key); struct rpc_key *rpck = talloc(mem_ctx, struct rpc_key); + + NTSTATUS status; - init_winreg_String(&r.in.name, name); - init_winreg_String(&r.in.keyclass, NULL); - + ZERO_STRUCT(r); r.in.handle = &parentkd->pol; - r.out.new_handle = &rpck->pol; + chars_to_winreg_String(mem_ctx, &r.in.name, name); + chars_to_winreg_String(mem_ctx, &r.in.keyclass, NULL); r.in.options = 0; r.in.access_mask = SEC_STD_ALL; r.in.secdesc = NULL; + r.in.action_taken = NULL; + r.out.new_handle = &rpck->pol; + r.out.action_taken = NULL; status = dcerpc_winreg_CreateKey(parentkd->pipe, mem_ctx, &r); @@ -300,26 +358,25 @@ static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, return ntstatus_to_werror(status); } - if (W_ERROR_IS_OK(r.out.result)) { - rpck->pipe = talloc_reference(rpck, parentkd->pipe); - *key = (struct registry_key *)rpck; - } + rpck->pipe = talloc_reference(rpck, parentkd->pipe); + *key = (struct registry_key *)rpck; return r.out.result; } static WERROR rpc_query_key(const struct registry_key *k) { - NTSTATUS status; struct winreg_QueryInfoKey r; struct rpc_key *mykeydata = talloc_get_type(k, struct rpc_key); TALLOC_CTX *mem_ctx = talloc_init("query_key"); uint32_t max_subkeysize; uint32_t secdescsize; NTTIME last_changed_time; + NTSTATUS status; - ZERO_STRUCT(r.out); - + ZERO_STRUCT(r); + r.in.handle = &mykeydata->pol; + chars_to_winreg_String(mem_ctx, r.in.classname, NULL); r.out.num_subkeys = &mykeydata->num_subkeys; r.out.max_subkeylen = &mykeydata->max_subkeynamelen; r.out.max_valnamelen = &mykeydata->max_valnamelen; @@ -330,10 +387,9 @@ static WERROR rpc_query_key(const struct registry_key *k) r.out.last_changed_time = &last_changed_time; r.out.classname = r.in.classname = talloc_zero(mem_ctx, struct winreg_String); - init_winreg_String(r.in.classname, NULL); - r.in.handle = &mykeydata->pol; status = dcerpc_winreg_QueryInfoKey(mykeydata->pipe, mem_ctx, &r); + talloc_free(mem_ctx); if (!NT_STATUS_IS_OK(status)) { @@ -341,9 +397,6 @@ static WERROR rpc_query_key(const struct registry_key *k) return ntstatus_to_werror(status); } - if (W_ERROR_IS_OK(r.out.result)) { - } - return r.out.result; } @@ -354,13 +407,19 @@ static WERROR rpc_del_key(struct registry_key *parent, const char *name) struct winreg_DeleteKey r; TALLOC_CTX *mem_ctx = talloc_init("del_key"); + ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; - init_winreg_String(&r.in.key, name); + chars_to_winreg_String(mem_ctx, &r.in.key, name); status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r); talloc_free(mem_ctx); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("DeleteKey failed - %s\n", nt_errstr(status))); + return ntstatus_to_werror(status); + } + return r.out.result; } -- cgit From c1fef1fabf03e9c2f412b03d9f8cd314a5d34760 Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Sat, 13 Sep 2008 12:30:47 +0200 Subject: Registry client library: Various fixup's and corrections I reverted the change with the string conversions because they aren't needed and included many fixup's and improvements in the code. We should be able now to connect to a Windows WINREG server without a problem. --- source4/lib/registry/rpc.c | 127 +++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 63 deletions(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index 3fdba02852..2792bd556b 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -2,6 +2,7 @@ Samba Unix/Linux SMB implementation RPC backend for the registry library Copyright (C) 2003-2007 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 @@ -19,18 +20,21 @@ #include "includes.h" #include "registry.h" #include "librpc/gen_ndr/ndr_winreg_c.h" -#include "param/param.h" struct rpc_key { struct registry_key key; struct policy_handle pol; struct dcerpc_pipe *pipe; - uint32_t num_values; + const char* classname; uint32_t num_subkeys; + uint32_t max_subkeylen; + uint32_t max_subkeysize; + uint32_t num_values; uint32_t max_valnamelen; - uint32_t max_valdatalen; - uint32_t max_subkeynamelen; + uint32_t max_valbufsize; + uint32_t secdescsize; + NTTIME last_changed_time; }; struct rpc_registry_context { @@ -45,63 +49,55 @@ static struct registry_operations reg_backend_rpc; */ /* - * Converts a SAMBA (UNIX) string into a WINREG (UTF16) string + * Converts a SAMBA string into a WINREG string */ static void chars_to_winreg_String(TALLOC_CTX *mem_ctx, struct winreg_String *winregStr, const char *str) { winregStr->name = NULL; winregStr->name_len = 0; - if (str != NULL) - winregStr->name_len = convert_string_talloc(mem_ctx, - lp_iconv_convenience(global_loadparm), CH_UNIX, - CH_UTF16, (void *) str, strlen(str), - (void **) &winregStr->name); + if (str != NULL) { + winregStr->name = talloc_strdup(mem_ctx, str); + winregStr->name_len = strlen(str); + } winregStr->name_size = winregStr->name_len; } /* - * Converts a WINREG (UTF16) string into a SAMBA (UNIX) string + * Converts a WINREG string into a SAMBA string */ static void winreg_String_to_chars(TALLOC_CTX *mem_ctx, const char **str, struct winreg_String *winregStr) { *str = NULL; if (winregStr->name != NULL) - convert_string_talloc(mem_ctx, - lp_iconv_convenience(global_loadparm), CH_UTF16, - CH_UNIX, (void *) winregStr->name, winregStr->name_len, - (void **) str); + *str = talloc_strdup(mem_ctx, winregStr->name); } /* - * Converts a SAMBA (UNIX) string into a WINREG (UTF16) string buffer + * Converts a SAMBA string into a WINREG string buffer */ static void chars_to_winreg_StringBuf(TALLOC_CTX *mem_ctx, struct winreg_StringBuf *winregStrBuf, const char *str, uint16_t size) { winregStrBuf->name = NULL; winregStrBuf->length = 0; - if (str != NULL) - winregStrBuf->length = convert_string_talloc(mem_ctx, - lp_iconv_convenience(global_loadparm), CH_UNIX, - CH_UTF16, (void *) str, strlen(str), - (void **) &winregStrBuf->name); + if (str != NULL) { + winregStrBuf->name = talloc_strdup(mem_ctx, str); + winregStrBuf->length = strlen(str); + } winregStrBuf->size = size; } /* - * Converts a WINREG (UTF16) string buffer into a SAMBA (UNIX) string + * Converts a WINREG string buffer into a SAMBA string */ static void winreg_StringBuf_to_chars(TALLOC_CTX *mem_ctx, const char **str, struct winreg_StringBuf *winregStrBuf) { *str = NULL; if (winregStrBuf->name != NULL) - convert_string_talloc(mem_ctx, - lp_iconv_convenience(global_loadparm), CH_UTF16, - CH_UNIX, (void *) winregStrBuf->name, - winregStrBuf->length, (void **) str); + *str = talloc_strdup(mem_ctx, winregStrBuf->name); } #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \ @@ -147,7 +143,7 @@ static struct { { 0, NULL } }; -static WERROR rpc_query_key(const struct registry_key *k); +static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k); static WERROR rpc_get_predefined_key(struct registry_context *ctx, uint32_t hkey_type, @@ -254,11 +250,11 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, NTSTATUS status; if (mykeydata->num_values == -1) { - error = rpc_query_key(parent); + error = rpc_query_key(mem_ctx, parent); if(!W_ERROR_IS_OK(error)) return error; } - chars_to_winreg_StringBuf(mem_ctx, &name, "", 1024); + chars_to_winreg_StringBuf(mem_ctx, &name, "", mykeydata->max_valbufsize); ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; @@ -266,7 +262,7 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, r.in.name = &name; r.in.type = type; r.in.value = &value; - r.in.size = &mykeydata->max_valdatalen; + r.in.size = &mykeydata->max_valbufsize; r.in.length = &zero; r.out.name = &name; r.out.type = type; @@ -301,7 +297,7 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, NTTIME change_time = 0; NTSTATUS status; - chars_to_winreg_StringBuf(mem_ctx, &namebuf, "", 1024); + chars_to_winreg_StringBuf(mem_ctx, &namebuf, " ", 1024); chars_to_winreg_StringBuf(mem_ctx, &classbuf, NULL, 0); ZERO_STRUCT(r); @@ -321,8 +317,12 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, return ntstatus_to_werror(status); } - winreg_StringBuf_to_chars(mem_ctx, name, r.out.name); - winreg_StringBuf_to_chars(mem_ctx, keyclass, r.out.keyclass); + if (name != NULL) + winreg_StringBuf_to_chars(mem_ctx, name, r.out.name); + if (keyclass != NULL) + winreg_StringBuf_to_chars(mem_ctx, keyclass, r.out.keyclass); + if (last_changed_time != NULL) + *last_changed_time = *(r.out.last_changed_time); return r.out.result; } @@ -364,39 +364,37 @@ static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, return r.out.result; } -static WERROR rpc_query_key(const struct registry_key *k) +static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k) { struct winreg_QueryInfoKey r; struct rpc_key *mykeydata = talloc_get_type(k, struct rpc_key); - TALLOC_CTX *mem_ctx = talloc_init("query_key"); - uint32_t max_subkeysize; - uint32_t secdescsize; - NTTIME last_changed_time; + struct winreg_String classname; NTSTATUS status; + chars_to_winreg_String(mem_ctx, &classname, NULL); + ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; - chars_to_winreg_String(mem_ctx, r.in.classname, NULL); + r.in.classname = &classname; + r.out.classname = &classname; r.out.num_subkeys = &mykeydata->num_subkeys; - r.out.max_subkeylen = &mykeydata->max_subkeynamelen; - r.out.max_valnamelen = &mykeydata->max_valnamelen; - r.out.max_valbufsize = &mykeydata->max_valdatalen; - r.out.max_subkeysize = &max_subkeysize; + r.out.max_subkeylen = &mykeydata->max_subkeylen; + r.out.max_subkeysize = &mykeydata->max_subkeysize; r.out.num_values = &mykeydata->num_values; - r.out.secdescsize = &secdescsize; - r.out.last_changed_time = &last_changed_time; - - r.out.classname = r.in.classname = talloc_zero(mem_ctx, struct winreg_String); + r.out.max_valnamelen = &mykeydata->max_valnamelen; + r.out.max_valbufsize = &mykeydata->max_valbufsize; + r.out.secdescsize = &mykeydata->secdescsize; + r.out.last_changed_time = &mykeydata->last_changed_time; status = dcerpc_winreg_QueryInfoKey(mykeydata->pipe, mem_ctx, &r); - talloc_free(mem_ctx); - if (!NT_STATUS_IS_OK(status)) { DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status))); return ntstatus_to_werror(status); } + winreg_String_to_chars(mem_ctx, &mykeydata->classname, r.out.classname); + return r.out.result; } @@ -425,10 +423,10 @@ static WERROR rpc_del_key(struct registry_key *parent, const char *name) static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char **classname, - uint32_t *numsubkeys, - uint32_t *numvalue, + uint32_t *num_subkeys, + uint32_t *num_values, NTTIME *last_changed_time, - uint32_t *max_subkeynamelen, + uint32_t *max_subkeylen, uint32_t *max_valnamelen, uint32_t *max_valbufsize) { @@ -436,27 +434,30 @@ static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key, WERROR error; if (mykeydata->num_values == -1) { - error = rpc_query_key(key); + error = rpc_query_key(mem_ctx, key); if(!W_ERROR_IS_OK(error)) return error; } - /* FIXME: *classname = talloc_strdup(mem_ctx, mykeydata->classname); */ - /* FIXME: *last_changed_time = mykeydata->last_changed_time */ + if (classname != NULL) + *classname = mykeydata->classname; + + if (num_subkeys != NULL) + *num_subkeys = mykeydata->num_subkeys; - if (numvalue != NULL) - *numvalue = mykeydata->num_values; + if (num_values != NULL) + *num_values = mykeydata->num_values; - if (numsubkeys != NULL) - *numsubkeys = mykeydata->num_subkeys; + if (last_changed_time != NULL) + *last_changed_time = mykeydata->last_changed_time; + + if (max_subkeylen != NULL) + *max_subkeylen = mykeydata->max_subkeylen; if (max_valnamelen != NULL) *max_valnamelen = mykeydata->max_valnamelen; if (max_valbufsize != NULL) - *max_valbufsize = mykeydata->max_valdatalen; - - if (max_subkeynamelen != NULL) - *max_subkeynamelen = mykeydata->max_subkeynamelen; + *max_valbufsize = mykeydata->max_valbufsize; return WERR_OK; } -- cgit From 1e178ffc03456064bfd2ec330b9b6b6217c8561d Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Sat, 13 Sep 2008 14:26:08 +0200 Subject: Registry client library: Fix some buffer problems This buffer maximum values are used in Windows (2000), so I take them also for SAMBA 4. --- source4/lib/registry/rpc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index 2792bd556b..7469bb60d8 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -21,6 +21,9 @@ #include "registry.h" #include "librpc/gen_ndr/ndr_winreg_c.h" +#define MAX_NAMESIZE 512 +#define MAX_VALSIZE 32768 + struct rpc_key { struct registry_key key; struct policy_handle pol; @@ -245,7 +248,8 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct winreg_EnumValue r; struct winreg_StringBuf name; uint8_t value; - uint32_t zero = 0, zero2 = 0; + uint32_t val_size = MAX_VALSIZE; + uint32_t zero = 0; WERROR error; NTSTATUS status; @@ -254,7 +258,7 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, if(!W_ERROR_IS_OK(error)) return error; } - chars_to_winreg_StringBuf(mem_ctx, &name, "", mykeydata->max_valbufsize); + chars_to_winreg_StringBuf(mem_ctx, &name, "", MAX_NAMESIZE); ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; @@ -262,12 +266,12 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, r.in.name = &name; r.in.type = type; r.in.value = &value; - r.in.size = &mykeydata->max_valbufsize; + r.in.size = &val_size; r.in.length = &zero; r.out.name = &name; r.out.type = type; r.out.value = &value; - r.out.size = &zero2; + r.out.size = &val_size; r.out.length = &zero; status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r); @@ -297,7 +301,7 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, NTTIME change_time = 0; NTSTATUS status; - chars_to_winreg_StringBuf(mem_ctx, &namebuf, " ", 1024); + chars_to_winreg_StringBuf(mem_ctx, &namebuf, " ", MAX_NAMESIZE); chars_to_winreg_StringBuf(mem_ctx, &classbuf, NULL, 0); ZERO_STRUCT(r); -- cgit From 04f29b84a4bf2378d326b86a983792095231922e Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Sat, 13 Sep 2008 15:02:56 +0200 Subject: Registry client library: Fixes the creation of new keys Giving the right permissions --- source4/lib/registry/rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index 7469bb60d8..69da90128e 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -348,7 +348,7 @@ static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, chars_to_winreg_String(mem_ctx, &r.in.name, name); chars_to_winreg_String(mem_ctx, &r.in.keyclass, NULL); r.in.options = 0; - r.in.access_mask = SEC_STD_ALL; + r.in.access_mask = 0x02000000; r.in.secdesc = NULL; r.in.action_taken = NULL; r.out.new_handle = &rpck->pol; -- cgit From 9b70485207735a8d4c1395963944e5a19a791f53 Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Mon, 15 Sep 2008 13:04:40 +0200 Subject: Registry client library: Use "talloc_zero" to avoid uninitialized values --- source4/lib/registry/rpc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index 69da90128e..83c6dc762c 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -183,7 +183,7 @@ static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k) struct winreg_OpenKey r; struct rpc_key_data *mykeydata; - k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data); + k->backend_data = mykeydata = talloc_zero(mem_ctx, struct rpc_key_data); mykeydata->num_values = -1; mykeydata->num_subkeys = -1; @@ -211,8 +211,7 @@ static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, struct winreg_OpenKey r; NTSTATUS status; - mykeydata = talloc(mem_ctx, struct rpc_key); - + mykeydata = talloc_zero(mem_ctx, struct rpc_key); mykeydata->key.context = parentkeydata->key.context; mykeydata->pipe = talloc_reference(mykeydata, parentkeydata->pipe); mykeydata->num_values = -1; -- cgit From f25f0dd74489bfea39170a9277afbf96deb8f389 Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Mon, 15 Sep 2008 18:59:17 +0200 Subject: Registry client: Implement the "winreg_QueryValue" call This is needed for the registry patchfile library --- source4/lib/registry/rpc.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index 83c6dc762c..b5918dc4db 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -287,6 +287,53 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, return r.out.result; } +static WERROR rpc_get_value_by_name(TALLOC_CTX *mem_ctx, + const struct registry_key *parent, + const char *value_name, + uint32_t *type, + DATA_BLOB *data) +{ + struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key); + struct winreg_QueryValue r; + struct winreg_String name; + uint8_t value; + uint32_t val_size = MAX_VALSIZE; + uint32_t zero = 0; + WERROR error; + NTSTATUS status; + + if (mykeydata->num_values == -1) { + error = rpc_query_key(mem_ctx, parent); + if(!W_ERROR_IS_OK(error)) return error; + } + + chars_to_winreg_String(mem_ctx, &name, value_name); + + ZERO_STRUCT(r); + r.in.handle = &mykeydata->pol; + r.in.value_name = name; + r.in.type = type; + r.in.data = &value; + r.in.size = &val_size; + r.in.length = &zero; + r.out.type = type; + r.out.data = &value; + r.out.size = &val_size; + r.out.length = &zero; + + status = dcerpc_winreg_QueryValue(mykeydata->pipe, mem_ctx, &r); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("QueryValue failed - %s\n", nt_errstr(status))); + return ntstatus_to_werror(status); + } + + *type = *(r.out.type); + *data = data_blob_talloc(mem_ctx, r.out.data, *r.out.length); + + return r.out.result; +} + static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *parent, uint32_t n, @@ -471,6 +518,7 @@ static struct registry_operations reg_backend_rpc = { .get_predefined_key = rpc_get_predefined_key, .enum_key = rpc_get_subkey_by_index, .enum_value = rpc_get_value_by_index, + .get_value = rpc_get_value_by_name, .create_key = rpc_add_key, .delete_key = rpc_del_key, .get_key_info = rpc_get_info, -- cgit From 4c7203367fb487de616a137d74ad70c67e8781c9 Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Fri, 19 Sep 2008 14:35:35 +0200 Subject: Registry client library: Remove two elementar conversion functions --- source4/lib/registry/rpc.c | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index b5918dc4db..caf5318693 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -66,17 +66,6 @@ static void chars_to_winreg_String(TALLOC_CTX *mem_ctx, struct winreg_String winregStr->name_size = winregStr->name_len; } -/* - * Converts a WINREG string into a SAMBA string - */ -static void winreg_String_to_chars(TALLOC_CTX *mem_ctx, const char **str, - struct winreg_String *winregStr) -{ - *str = NULL; - if (winregStr->name != NULL) - *str = talloc_strdup(mem_ctx, winregStr->name); -} - /* * Converts a SAMBA string into a WINREG string buffer */ @@ -92,17 +81,6 @@ static void chars_to_winreg_StringBuf(TALLOC_CTX *mem_ctx, struct winreg_StringB winregStrBuf->size = size; } -/* - * Converts a WINREG string buffer into a SAMBA string - */ -static void winreg_StringBuf_to_chars(TALLOC_CTX *mem_ctx, const char **str, - struct winreg_StringBuf *winregStrBuf) -{ - *str = NULL; - if (winregStrBuf->name != NULL) - *str = talloc_strdup(mem_ctx, winregStrBuf->name); -} - #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \ { \ struct winreg_Open ## u r; \ @@ -280,7 +258,7 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, return ntstatus_to_werror(status); } - winreg_StringBuf_to_chars(mem_ctx, value_name, r.out.name); + *value_name = talloc_strdup(mem_ctx, r.out.name->name); *type = *(r.out.type); *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length); @@ -368,9 +346,9 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, } if (name != NULL) - winreg_StringBuf_to_chars(mem_ctx, name, r.out.name); + *name = talloc_strdup(mem_ctx, r.out.name->name); if (keyclass != NULL) - winreg_StringBuf_to_chars(mem_ctx, keyclass, r.out.keyclass); + *keyclass = talloc_strdup(mem_ctx, r.out.keyclass->name); if (last_changed_time != NULL) *last_changed_time = *(r.out.last_changed_time); @@ -443,7 +421,7 @@ static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k) return ntstatus_to_werror(status); } - winreg_String_to_chars(mem_ctx, &mykeydata->classname, r.out.classname); + mykeydata->classname = talloc_strdup(mem_ctx, r.out.classname->name); return r.out.result; } -- cgit From 08953e44db2d3369c51324bb21474830cd24dd4b Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Fri, 19 Sep 2008 15:53:02 +0200 Subject: Registry client: Remove all conversion helper functions --- source4/lib/registry/rpc.c | 61 +++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 44 deletions(-) (limited to 'source4/lib/registry/rpc.c') diff --git a/source4/lib/registry/rpc.c b/source4/lib/registry/rpc.c index caf5318693..6429a390db 100644 --- a/source4/lib/registry/rpc.c +++ b/source4/lib/registry/rpc.c @@ -51,36 +51,6 @@ static struct registry_operations reg_backend_rpc; * This is the RPC backend for the registry library. */ -/* - * Converts a SAMBA string into a WINREG string - */ -static void chars_to_winreg_String(TALLOC_CTX *mem_ctx, struct winreg_String - *winregStr, const char *str) -{ - winregStr->name = NULL; - winregStr->name_len = 0; - if (str != NULL) { - winregStr->name = talloc_strdup(mem_ctx, str); - winregStr->name_len = strlen(str); - } - winregStr->name_size = winregStr->name_len; -} - -/* - * Converts a SAMBA string into a WINREG string buffer - */ -static void chars_to_winreg_StringBuf(TALLOC_CTX *mem_ctx, struct winreg_StringBuf - *winregStrBuf, const char *str, uint16_t size) -{ - winregStrBuf->name = NULL; - winregStrBuf->length = 0; - if (str != NULL) { - winregStrBuf->name = talloc_strdup(mem_ctx, str); - winregStrBuf->length = strlen(str); - } - winregStrBuf->size = size; -} - #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \ { \ struct winreg_Open ## u r; \ @@ -169,7 +139,7 @@ static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k) ZERO_STRUCT(r); r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol); - chars_to_winreg_String(mem_ctx, &r.in.keyname, k->path); + r.in.keyname.name = k->path; r.in.unknown = 0x00000000; r.in.access_mask = 0x02000000; r.out.handle = &mykeydata->pol; @@ -199,7 +169,7 @@ static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, /* Then, open the handle using the hive */ ZERO_STRUCT(r); r.in.parent_handle = &parentkeydata->pol; - chars_to_winreg_String(mem_ctx, &r.in.keyname, name); + r.in.keyname.name = name; r.in.unknown = 0x00000000; r.in.access_mask = 0x02000000; r.out.handle = &mykeydata->pol; @@ -235,7 +205,8 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, if(!W_ERROR_IS_OK(error)) return error; } - chars_to_winreg_StringBuf(mem_ctx, &name, "", MAX_NAMESIZE); + name.name = ""; + name.size = MAX_NAMESIZE; ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; @@ -258,7 +229,7 @@ static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, return ntstatus_to_werror(status); } - *value_name = talloc_strdup(mem_ctx, r.out.name->name); + *value_name = talloc_reference(mem_ctx, r.out.name->name); *type = *(r.out.type); *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length); @@ -285,7 +256,7 @@ static WERROR rpc_get_value_by_name(TALLOC_CTX *mem_ctx, if(!W_ERROR_IS_OK(error)) return error; } - chars_to_winreg_String(mem_ctx, &name, value_name); + name.name = value_name; ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; @@ -325,8 +296,10 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, NTTIME change_time = 0; NTSTATUS status; - chars_to_winreg_StringBuf(mem_ctx, &namebuf, " ", MAX_NAMESIZE); - chars_to_winreg_StringBuf(mem_ctx, &classbuf, NULL, 0); + namebuf.name = ""; + namebuf.size = MAX_NAMESIZE; + classbuf.name = NULL; + classbuf.size = 0; ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; @@ -346,9 +319,9 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, } if (name != NULL) - *name = talloc_strdup(mem_ctx, r.out.name->name); + *name = talloc_reference(mem_ctx, r.out.name->name); if (keyclass != NULL) - *keyclass = talloc_strdup(mem_ctx, r.out.keyclass->name); + *keyclass = talloc_reference(mem_ctx, r.out.keyclass->name); if (last_changed_time != NULL) *last_changed_time = *(r.out.last_changed_time); @@ -369,8 +342,8 @@ static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, ZERO_STRUCT(r); r.in.handle = &parentkd->pol; - chars_to_winreg_String(mem_ctx, &r.in.name, name); - chars_to_winreg_String(mem_ctx, &r.in.keyclass, NULL); + r.in.name.name = name; + r.in.keyclass.name = NULL; r.in.options = 0; r.in.access_mask = 0x02000000; r.in.secdesc = NULL; @@ -399,7 +372,7 @@ static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k) struct winreg_String classname; NTSTATUS status; - chars_to_winreg_String(mem_ctx, &classname, NULL); + classname.name = NULL; ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; @@ -421,7 +394,7 @@ static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k) return ntstatus_to_werror(status); } - mykeydata->classname = talloc_strdup(mem_ctx, r.out.classname->name); + mykeydata->classname = talloc_reference(mem_ctx, r.out.classname->name); return r.out.result; } @@ -435,7 +408,7 @@ static WERROR rpc_del_key(struct registry_key *parent, const char *name) ZERO_STRUCT(r); r.in.handle = &mykeydata->pol; - chars_to_winreg_String(mem_ctx, &r.in.key, name); + r.in.key.name = name; status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r); -- cgit