wmi-1.3.16 from opsview.com
This commit is contained in:
@@ -0,0 +1,585 @@
|
||||
/*
|
||||
Unix SMB/CIFS implementation.
|
||||
Transparent registry backend handling
|
||||
Copyright (C) Jelmer Vernooij 2003-2004.
|
||||
|
||||
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
|
||||
(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.
|
||||
*/
|
||||
|
||||
#include "includes.h"
|
||||
#include "lib/util/dlinklist.h"
|
||||
#include "lib/registry/registry.h"
|
||||
#include "build.h"
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Main registry functions
|
||||
*/
|
||||
|
||||
/* List of available backends */
|
||||
static struct reg_init_function_entry *backends = NULL;
|
||||
|
||||
static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
|
||||
|
||||
/** Register a new backend. */
|
||||
_PUBLIC_ NTSTATUS registry_register(const void *_hive_ops)
|
||||
{
|
||||
const struct hive_operations *hive_ops = _hive_ops;
|
||||
struct reg_init_function_entry *entry = backends;
|
||||
|
||||
DEBUG(5,("Attempting to register registry backend %s\n", hive_ops->name));
|
||||
|
||||
/* Check for duplicates */
|
||||
if (reg_find_backend_entry(hive_ops->name)) {
|
||||
DEBUG(0,("There already is a registry backend registered with the name %s!\n", hive_ops->name));
|
||||
return NT_STATUS_OBJECT_NAME_COLLISION;
|
||||
}
|
||||
|
||||
entry = talloc(talloc_autofree_context(), struct reg_init_function_entry);
|
||||
entry->hive_functions = hive_ops;
|
||||
|
||||
DLIST_ADD(backends, entry);
|
||||
DEBUG(5,("Successfully added registry backend '%s'\n", hive_ops->name));
|
||||
return NT_STATUS_OK;
|
||||
}
|
||||
|
||||
/** Find a backend in the list of available backends */
|
||||
static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
|
||||
{
|
||||
struct reg_init_function_entry *entry;
|
||||
|
||||
entry = backends;
|
||||
|
||||
while(entry) {
|
||||
if (strcmp(entry->hive_functions->name, name) == 0) return entry;
|
||||
entry = entry->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** Initialize the registry subsystem */
|
||||
_PUBLIC_ NTSTATUS registry_init(void)
|
||||
{
|
||||
init_module_fn static_init[] = STATIC_registry_MODULES;
|
||||
init_module_fn *shared_init = load_samba_modules(NULL, "registry");
|
||||
|
||||
run_init_functions(static_init);
|
||||
run_init_functions(shared_init);
|
||||
|
||||
talloc_free(shared_init);
|
||||
|
||||
return NT_STATUS_OK;
|
||||
}
|
||||
|
||||
/** Check whether a certain backend is present. */
|
||||
_PUBLIC_ BOOL reg_has_backend(const char *backend)
|
||||
{
|
||||
return reg_find_backend_entry(backend) != NULL?True:False;
|
||||
}
|
||||
|
||||
const struct reg_predefined_key reg_predefined_keys[] = {
|
||||
{HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
|
||||
{HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
|
||||
{HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
|
||||
{HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
|
||||
{HKEY_USERS, "HKEY_USERS" },
|
||||
{HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
|
||||
{HKEY_DYN_DATA, "HKEY_DYN_DATA" },
|
||||
{HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
|
||||
{HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
/** Obtain a list of predefined keys. */
|
||||
_PUBLIC_ int reg_list_predefs(TALLOC_CTX *mem_ctx, char ***predefs, uint32_t **hkeys)
|
||||
{
|
||||
int i;
|
||||
*predefs = talloc_array(mem_ctx, char *, ARRAY_SIZE(reg_predefined_keys));
|
||||
*hkeys = talloc_array(mem_ctx, uint32_t, ARRAY_SIZE(reg_predefined_keys));
|
||||
|
||||
for (i = 0; reg_predefined_keys[i].name; i++) {
|
||||
(*predefs)[i] = talloc_strdup(mem_ctx, reg_predefined_keys[i].name);
|
||||
(*hkeys)[i] = reg_predefined_keys[i].handle;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/** Obtain name of specific hkey. */
|
||||
_PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; reg_predefined_keys[i].name; i++) {
|
||||
if (reg_predefined_keys[i].handle == hkey) return reg_predefined_keys[i].name;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** Get predefined key by name. */
|
||||
_PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx, const char *name, struct registry_key **key)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; reg_predefined_keys[i].name; i++) {
|
||||
if (!strcasecmp(reg_predefined_keys[i].name, name)) return reg_get_predefined_key(ctx, reg_predefined_keys[i].handle, key);
|
||||
}
|
||||
|
||||
DEBUG(1, ("No predefined key with name '%s'\n", name));
|
||||
|
||||
return WERR_BADFILE;
|
||||
}
|
||||
|
||||
/** Get predefined key by id. */
|
||||
_PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx, uint32_t hkey, struct registry_key **key)
|
||||
{
|
||||
WERROR ret = ctx->get_predefined_key(ctx, hkey, key);
|
||||
|
||||
if (W_ERROR_IS_OK(ret)) {
|
||||
(*key)->name = talloc_strdup(*key, reg_get_predef_name(hkey));
|
||||
(*key)->path = "";
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Open a registry file/host/etc */
|
||||
_PUBLIC_ WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *backend, const char *location, struct auth_session_info *session_info, struct cli_credentials *credentials, struct registry_key **root)
|
||||
{
|
||||
struct registry_hive *rethive;
|
||||
struct registry_key *retkey = NULL;
|
||||
struct reg_init_function_entry *entry;
|
||||
WERROR werr;
|
||||
|
||||
entry = reg_find_backend_entry(backend);
|
||||
|
||||
if (!entry) {
|
||||
DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
|
||||
return WERR_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
if(!entry->hive_functions || !entry->hive_functions->open_hive) {
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
rethive = talloc(parent_ctx, struct registry_hive);
|
||||
rethive->location = location?talloc_strdup(rethive, location):NULL;
|
||||
rethive->session_info = talloc_reference(rethive, session_info);
|
||||
rethive->credentials = talloc_reference(rethive, credentials);
|
||||
rethive->functions = entry->hive_functions;
|
||||
rethive->backend_data = NULL;
|
||||
|
||||
werr = entry->hive_functions->open_hive(rethive, &retkey);
|
||||
|
||||
if(!W_ERROR_IS_OK(werr)) {
|
||||
return werr;
|
||||
}
|
||||
|
||||
if(!retkey) {
|
||||
DEBUG(0, ("Backend %s didn't provide root key!\n", backend));
|
||||
return WERR_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
rethive->root = retkey;
|
||||
|
||||
retkey->hive = rethive;
|
||||
retkey->name = NULL;
|
||||
retkey->path = talloc_strdup(retkey, "");
|
||||
|
||||
*root = retkey;
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a key
|
||||
* First tries to use the open_key function from the backend
|
||||
* then falls back to get_subkey_by_name and later get_subkey_by_index
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
|
||||
{
|
||||
WERROR error;
|
||||
|
||||
if(!parent) {
|
||||
DEBUG(0, ("Invalid parent key specified for open of '%s'\n", name));
|
||||
return WERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
if(!parent->hive->functions->open_key &&
|
||||
(parent->hive->functions->get_subkey_by_name ||
|
||||
parent->hive->functions->get_subkey_by_index)) {
|
||||
char *orig = strdup(name),
|
||||
*curbegin = orig,
|
||||
*curend = strchr(orig, '\\');
|
||||
struct registry_key *curkey = parent;
|
||||
|
||||
while(curbegin && *curbegin) {
|
||||
if(curend)*curend = '\0';
|
||||
error = reg_key_get_subkey_by_name(mem_ctx, curkey, curbegin, &curkey);
|
||||
if(!W_ERROR_IS_OK(error)) {
|
||||
SAFE_FREE(orig);
|
||||
return error;
|
||||
}
|
||||
if(!curend) break;
|
||||
curbegin = curend + 1;
|
||||
curend = strchr(curbegin, '\\');
|
||||
}
|
||||
SAFE_FREE(orig);
|
||||
|
||||
*result = curkey;
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
if(!parent->hive->functions->open_key) {
|
||||
DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
error = parent->hive->functions->open_key(mem_ctx, parent, name, result);
|
||||
|
||||
if(!W_ERROR_IS_OK(error)) return error;
|
||||
|
||||
(*result)->hive = parent->hive;
|
||||
(*result)->path = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
|
||||
(*result)->hive = parent->hive;
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value by index
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_value **val)
|
||||
{
|
||||
if(!key) return WERR_INVALID_PARAM;
|
||||
|
||||
if(key->hive->functions->get_value_by_index) {
|
||||
WERROR status = key->hive->functions->get_value_by_index(mem_ctx, key, idx, val);
|
||||
if(!W_ERROR_IS_OK(status))
|
||||
return status;
|
||||
} else {
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of subkeys.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_num_subkeys(const struct registry_key *key, uint32_t *count)
|
||||
{
|
||||
if(!key) return WERR_INVALID_PARAM;
|
||||
|
||||
if(key->hive->functions->num_subkeys) {
|
||||
return key->hive->functions->num_subkeys(key, count);
|
||||
}
|
||||
|
||||
if(key->hive->functions->get_subkey_by_index) {
|
||||
int i;
|
||||
WERROR error;
|
||||
struct registry_key *dest = NULL;
|
||||
TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
|
||||
|
||||
for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, key, i, &dest)); i++);
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
*count = i;
|
||||
if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
|
||||
return error;
|
||||
}
|
||||
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of values of a key.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_num_values(const struct registry_key *key, uint32_t *count)
|
||||
{
|
||||
|
||||
if(!key) return WERR_INVALID_PARAM;
|
||||
|
||||
if (key->hive->functions->num_values) {
|
||||
return key->hive->functions->num_values(key, count);
|
||||
}
|
||||
|
||||
if(key->hive->functions->get_value_by_index) {
|
||||
int i;
|
||||
WERROR error;
|
||||
struct registry_value *dest;
|
||||
TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
|
||||
|
||||
for(i = 0; W_ERROR_IS_OK(error = key->hive->functions->get_value_by_index(mem_ctx, key, i, &dest)); i++);
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
*count = i;
|
||||
if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
|
||||
return error;
|
||||
}
|
||||
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subkey by index.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_key **subkey)
|
||||
{
|
||||
if(!key) return WERR_INVALID_PARAM;
|
||||
|
||||
if(key->hive->functions->get_subkey_by_index) {
|
||||
WERROR status = key->hive->functions->get_subkey_by_index(mem_ctx, key, idx, subkey);
|
||||
if(!NT_STATUS_IS_OK(status)) return status;
|
||||
} else {
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if(key->hive->root == key)
|
||||
(*subkey)->path = talloc_strdup(mem_ctx, (*subkey)->name);
|
||||
else
|
||||
(*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
|
||||
|
||||
(*subkey)->hive = key->hive;
|
||||
return WERR_OK;;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subkey by name.
|
||||
*/
|
||||
WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_key **subkey)
|
||||
{
|
||||
int i;
|
||||
WERROR error = WERR_OK;
|
||||
|
||||
if(!key) return WERR_INVALID_PARAM;
|
||||
|
||||
if(key->hive->functions->get_subkey_by_name) {
|
||||
error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
|
||||
} else if(key->hive->functions->open_key) {
|
||||
error = key->hive->functions->open_key(mem_ctx, key, name, subkey);
|
||||
} else if(key->hive->functions->get_subkey_by_index) {
|
||||
for(i = 0; W_ERROR_IS_OK(error); i++) {
|
||||
error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
|
||||
if(W_ERROR_IS_OK(error) && !strcasecmp((*subkey)->name, name)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
|
||||
error = WERR_DEST_NOT_FOUND;
|
||||
} else {
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if(!W_ERROR_IS_OK(error)) return error;
|
||||
|
||||
(*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
|
||||
(*subkey)->hive = key->hive;
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value by name.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_value **val)
|
||||
{
|
||||
int i;
|
||||
WERROR error = WERR_OK;
|
||||
|
||||
if(!key) return WERR_INVALID_PARAM;
|
||||
|
||||
if(key->hive->functions->get_value_by_name) {
|
||||
error = key->hive->functions->get_value_by_name(mem_ctx, key,name, val);
|
||||
} else {
|
||||
for(i = 0; W_ERROR_IS_OK(error); i++) {
|
||||
error = reg_key_get_value_by_index(mem_ctx, key, i, val);
|
||||
if(W_ERROR_IS_OK(error) && !strcasecmp((*val)->name, name)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
|
||||
return WERR_DEST_NOT_FOUND;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_del(struct registry_key *parent, const char *name)
|
||||
{
|
||||
WERROR error;
|
||||
if(!parent) return WERR_INVALID_PARAM;
|
||||
|
||||
|
||||
if(!parent->hive->functions->del_key)
|
||||
return WERR_NOT_SUPPORTED;
|
||||
|
||||
error = parent->hive->functions->del_key(parent, name);
|
||||
if(!W_ERROR_IS_OK(error)) return error;
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a key.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *desc, struct registry_key **newkey)
|
||||
{
|
||||
WERROR error;
|
||||
|
||||
if (!parent) return WERR_INVALID_PARAM;
|
||||
|
||||
if (!parent->hive->functions->add_key) {
|
||||
DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->hive->functions->name));
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
error = parent->hive->functions->add_key(mem_ctx, parent, name, access_mask, desc, newkey);
|
||||
|
||||
if(!W_ERROR_IS_OK(error)) return error;
|
||||
|
||||
if (!*newkey) {
|
||||
DEBUG(0, ("Backend returned WERR_OK, but didn't specify key!\n"));
|
||||
return WERR_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
(*newkey)->hive = parent->hive;
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value, uint32_t type, DATA_BLOB data)
|
||||
{
|
||||
/* A 'real' set function has preference */
|
||||
if (key->hive->functions->set_value)
|
||||
return key->hive->functions->set_value(key, value, type, data);
|
||||
|
||||
DEBUG(1, ("Backend '%s' doesn't support method set_value\n", key->hive->functions->name));
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the security descriptor on a key.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key, struct security_descriptor **secdesc)
|
||||
{
|
||||
/* A 'real' set function has preference */
|
||||
if (key->hive->functions->key_get_sec_desc)
|
||||
return key->hive->functions->key_get_sec_desc(ctx, key, secdesc);
|
||||
|
||||
DEBUG(1, ("Backend '%s' doesn't support method get_sec_desc\n", key->hive->functions->name));
|
||||
return WERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a value.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_del_value(const struct registry_key *key, const char *valname)
|
||||
{
|
||||
WERROR ret = WERR_OK;
|
||||
if(!key->hive->functions->del_value)
|
||||
return WERR_NOT_SUPPORTED;
|
||||
|
||||
ret = key->hive->functions->del_value(key, valname);
|
||||
|
||||
if(!W_ERROR_IS_OK(ret)) return ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush a key to disk.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_flush(const struct registry_key *key)
|
||||
{
|
||||
if (!key) {
|
||||
return WERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
if (key->hive->functions->flush_key) {
|
||||
return key->hive->functions->flush_key(key);
|
||||
}
|
||||
|
||||
/* No need for flushing, apparently */
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum name and data lengths of the subkeys.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_subkeysizes(const struct registry_key *key, uint32_t *max_subkeylen, uint32_t *max_subkeysize)
|
||||
{
|
||||
int i = 0;
|
||||
struct registry_key *subkey;
|
||||
WERROR error;
|
||||
TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
|
||||
|
||||
*max_subkeylen = *max_subkeysize = 0;
|
||||
|
||||
do {
|
||||
error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
|
||||
|
||||
if (W_ERROR_IS_OK(error)) {
|
||||
*max_subkeysize = MAX(*max_subkeysize, 0xFF);
|
||||
*max_subkeylen = MAX(*max_subkeylen, strlen(subkey->name));
|
||||
}
|
||||
|
||||
i++;
|
||||
} while (W_ERROR_IS_OK(error));
|
||||
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum name and data lengths of the values.
|
||||
*/
|
||||
_PUBLIC_ WERROR reg_key_valuesizes(const struct registry_key *key, uint32_t *max_valnamelen, uint32_t *max_valbufsize)
|
||||
{
|
||||
int i = 0;
|
||||
struct registry_value *value;
|
||||
WERROR error;
|
||||
TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
|
||||
|
||||
*max_valnamelen = *max_valbufsize = 0;
|
||||
|
||||
do {
|
||||
error = reg_key_get_value_by_index(mem_ctx, key, i, &value);
|
||||
|
||||
if (W_ERROR_IS_OK(error)) {
|
||||
if (value->name) {
|
||||
*max_valnamelen = MAX(*max_valnamelen, strlen(value->name));
|
||||
}
|
||||
*max_valbufsize = MAX(*max_valbufsize, value->data.length);
|
||||
}
|
||||
|
||||
i++;
|
||||
} while (W_ERROR_IS_OK(error));
|
||||
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
Unix SMB/CIFS implementation.
|
||||
Transparent registry backend handling
|
||||
Copyright (C) Jelmer Vernooij 2003-2004.
|
||||
|
||||
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
|
||||
(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.
|
||||
*/
|
||||
|
||||
#include "includes.h"
|
||||
#include "lib/registry/registry.h"
|
||||
#include "librpc/gen_ndr/winreg.h"
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Registry utility functions
|
||||
*/
|
||||
|
||||
static const struct {
|
||||
uint32_t id;
|
||||
const char *name;
|
||||
} reg_value_types[] = {
|
||||
{ REG_SZ, "REG_SZ" },
|
||||
{ REG_DWORD, "REG_DWORD" },
|
||||
{ REG_BINARY, "REG_BINARY" },
|
||||
{ REG_EXPAND_SZ, "REG_EXPAND_SZ" },
|
||||
{ REG_NONE, "REG_NONE" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
/** Return string description of registry value type */
|
||||
_PUBLIC_ const char *str_regtype(int type)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; reg_value_types[i].name; i++) {
|
||||
if (reg_value_types[i].id == type)
|
||||
return reg_value_types[i].name;
|
||||
}
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
_PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type, DATA_BLOB *data)
|
||||
{
|
||||
char *ret = NULL;
|
||||
|
||||
if(data->length == 0) return talloc_strdup(mem_ctx, "");
|
||||
|
||||
switch (type) {
|
||||
case REG_EXPAND_SZ:
|
||||
case REG_SZ:
|
||||
convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX, data->data, data->length, (void **)&ret);
|
||||
return ret;
|
||||
|
||||
case REG_BINARY:
|
||||
ret = data_blob_hex_string(mem_ctx, data);
|
||||
return ret;
|
||||
|
||||
case REG_DWORD:
|
||||
if (*(int *)data->data == 0)
|
||||
return talloc_strdup(mem_ctx, "0");
|
||||
|
||||
return talloc_asprintf(mem_ctx, "0x%x", *(int *)data->data);
|
||||
|
||||
case REG_MULTI_SZ:
|
||||
/* FIXME */
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Generate a string that describes a registry value */
|
||||
_PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, struct registry_value *val)
|
||||
{
|
||||
return talloc_asprintf(mem_ctx, "%s = %s : %s", val->name?val->name:"<No Name>", str_regtype(val->data_type), reg_val_data_string(mem_ctx, val->data_type, &val->data));
|
||||
}
|
||||
|
||||
_PUBLIC_ BOOL reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str, const char *data_str, uint32_t *type, DATA_BLOB *data)
|
||||
{
|
||||
int i;
|
||||
*type = -1;
|
||||
|
||||
/* Find the correct type */
|
||||
for (i = 0; reg_value_types[i].name; i++) {
|
||||
if (!strcmp(reg_value_types[i].name, type_str)) {
|
||||
*type = reg_value_types[i].id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (*type == -1)
|
||||
return False;
|
||||
|
||||
/* Convert data appropriately */
|
||||
|
||||
switch (*type)
|
||||
{
|
||||
case REG_SZ:
|
||||
case REG_EXPAND_SZ:
|
||||
data->length = convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16, data_str, strlen(data_str), (void **)&data->data);
|
||||
break;
|
||||
|
||||
case REG_DWORD: {
|
||||
uint32_t tmp = strtol(data_str, NULL, 0);
|
||||
*data = data_blob_talloc(mem_ctx, &tmp, 4);
|
||||
}
|
||||
break;
|
||||
|
||||
case REG_NONE:
|
||||
ZERO_STRUCTP(data);
|
||||
break;
|
||||
|
||||
case REG_BINARY:
|
||||
*data = strhex_to_data_blob(data_str);
|
||||
talloc_steal(mem_ctx, data->data);
|
||||
break;
|
||||
|
||||
default:
|
||||
/* FIXME */
|
||||
return False;
|
||||
}
|
||||
return True;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all \'s with /'s
|
||||
*/
|
||||
char *reg_path_win2unix(char *path)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; path[i]; i++) {
|
||||
if(path[i] == '\\') path[i] = '/';
|
||||
}
|
||||
return path;
|
||||
}
|
||||
/**
|
||||
* Replace all /'s with \'s
|
||||
*/
|
||||
char *reg_path_unix2win(char *path)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; path[i]; i++) {
|
||||
if(path[i] == '/') path[i] = '\\';
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/** Open a key by name (including the predefined key name!) */
|
||||
WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle, const char *name, struct registry_key **result)
|
||||
{
|
||||
struct registry_key *predef;
|
||||
WERROR error;
|
||||
int predeflength;
|
||||
char *predefname;
|
||||
|
||||
if(strchr(name, '\\')) predeflength = strchr(name, '\\')-name;
|
||||
else predeflength = strlen(name);
|
||||
|
||||
predefname = talloc_strndup(mem_ctx, name, predeflength);
|
||||
error = reg_get_predefined_key_by_name(handle, predefname, &predef);
|
||||
talloc_free(predefname);
|
||||
|
||||
if(!W_ERROR_IS_OK(error)) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (strchr(name, '\\')) {
|
||||
return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1, result);
|
||||
} else {
|
||||
*result = predef;
|
||||
return WERR_OK;
|
||||
}
|
||||
}
|
||||
|
||||
static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx, const char *path, struct registry_key **parent, const char **name)
|
||||
{
|
||||
char *parent_name;
|
||||
WERROR error;
|
||||
|
||||
if (strchr(path, '\\') == NULL) {
|
||||
return WERR_FOOBAR;
|
||||
}
|
||||
|
||||
parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-1-path);
|
||||
|
||||
error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
|
||||
if (!W_ERROR_IS_OK(error)) {
|
||||
return error;
|
||||
}
|
||||
|
||||
*name = talloc_strdup(mem_ctx, strchr(path, '\\')+1);
|
||||
|
||||
return WERR_OK;
|
||||
}
|
||||
|
||||
WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
|
||||
{
|
||||
struct registry_key *parent;
|
||||
const char *n;
|
||||
TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
|
||||
WERROR error;
|
||||
|
||||
if (!strchr(path, '\\')) {
|
||||
return WERR_FOOBAR;
|
||||
}
|
||||
|
||||
error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
|
||||
if (W_ERROR_IS_OK(error)) {
|
||||
error = reg_key_del(parent, n);
|
||||
}
|
||||
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx, const char *path, uint32_t access_mask, struct security_descriptor *sec_desc, struct registry_key **result)
|
||||
{
|
||||
struct registry_key *parent;
|
||||
const char *n;
|
||||
WERROR error;
|
||||
|
||||
if (!strchr(path, '\\')) {
|
||||
return WERR_FOOBAR;
|
||||
}
|
||||
|
||||
error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
|
||||
if (W_ERROR_IS_OK(error)) {
|
||||
error = reg_key_add_name(mem_ctx, parent, n, access_mask, sec_desc, result);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
Reference in New Issue
Block a user