/**
 * Drop the whole configuration (restarting empty) - registry version
 */
static sbcErr smbconf_reg_drop(struct smbconf_ctx *ctx)
{
	char *path, *p;
	WERROR werr = WERR_OK;
	sbcErr err = SBC_ERR_OK;
	struct registry_key *parent_key = NULL;
	struct registry_key *new_key = NULL;
	TALLOC_CTX* mem_ctx = talloc_stackframe();
	enum winreg_CreateAction action;
	struct security_token *token;

	werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(1, ("Error creating admin token\n"));
		err = SBC_ERR_UNKNOWN_FAILURE;
		goto done;
	}

	path = talloc_strdup(mem_ctx, ctx->path);
	if (path == NULL) {
		err = SBC_ERR_NOMEM;
		goto done;
	}
	p = strrchr(path, '\\');
	if (p == NULL) {
		err = SBC_ERR_INVALID_PARAM;
		goto done;
	}
	*p = '\0';
	werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token,
			     &parent_key);
	if (!W_ERROR_IS_OK(werr)) {
		err = SBC_ERR_IO_FAILURE;
		goto done;
	}

	werr = reg_deletesubkeys_recursive(parent_key, p+1);
	if (!W_ERROR_IS_OK(werr)) {
		err = SBC_ERR_IO_FAILURE;
		goto done;
	}

	werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
			     &new_key, &action);
	if (!W_ERROR_IS_OK(werr)) {
		err = SBC_ERR_IO_FAILURE;
		goto done;
	}

done:
	talloc_free(mem_ctx);
	return err;
}
/**
 * initialize the registry smbconf backend
 */
static sbcErr smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
{
	WERROR werr = WERR_OK;
	sbcErr err;
	struct security_token *token;

	if (path == NULL) {
		path = KEY_SMBCONF;
	}
	ctx->path = talloc_strdup(ctx, path);
	if (ctx->path == NULL) {
		err = SBC_ERR_NOMEM;
		goto done;
	}

	ctx->data = talloc_zero(ctx, struct reg_private_data);

	werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(1, ("Error creating admin token\n"));
		err = SBC_ERR_UNKNOWN_FAILURE;
		goto done;
	}
	rpd(ctx)->open = false;

	werr = registry_init_smbconf(path);
	if (!W_ERROR_IS_OK(werr)) {
		err = SBC_ERR_BADFILE;
		goto done;
	}

	err = ctx->ops->open_conf(ctx);
	if (!SBC_ERROR_IS_OK(err)) {
		DEBUG(1, ("Error opening the registry.\n"));
		goto done;
	}

	werr = reg_open_path(ctx, ctx->path,
			     KEY_ENUMERATE_SUB_KEYS | REG_KEY_WRITE,
			     token, &rpd(ctx)->base_key);
	if (!W_ERROR_IS_OK(werr)) {
		err = SBC_ERR_UNKNOWN_FAILURE;
		goto done;
	}

done:
	return err;
}
Пример #3
0
/**
 * split given path into hive and remaining path and open the hive key
 */
static WERROR open_hive(TALLOC_CTX *ctx, const char *path,
			uint32 desired_access,
			struct registry_key **hive,
			char **subkeyname)
{
	WERROR werr;
	struct security_token *token = NULL;
	char *hivename = NULL;
	char *tmp_subkeyname = NULL;
	TALLOC_CTX *tmp_ctx = talloc_stackframe();

	if ((hive == NULL) || (subkeyname == NULL)) {
		werr = WERR_INVALID_PARAM;
		goto done;
	}

	werr = split_hive_key(tmp_ctx, path, &hivename, &tmp_subkeyname);
	if (!W_ERROR_IS_OK(werr)) {
		goto done;
	}
	*subkeyname = talloc_strdup(ctx, tmp_subkeyname);
	if (*subkeyname == NULL) {
		werr = WERR_NOMEM;
		goto done;
	}

	werr = ntstatus_to_werror(registry_create_admin_token(tmp_ctx, &token));
	if (!W_ERROR_IS_OK(werr)) {
		goto done;
	}

	werr = reg_openhive(ctx, hivename, desired_access, token, hive);
	if (!W_ERROR_IS_OK(werr)) {
		goto done;
	}

	werr = WERR_OK;

done:
	TALLOC_FREE(tmp_ctx);
	return werr;
}
Пример #4
0
/*********************************************************************
 for an eventlog, add in a source name. If the eventlog doesn't
 exist (not in the list) do nothing.   If a source for the log
 already exists, change the information (remove, replace)
*********************************************************************/
static bool eventlog_add_source( const char *eventlog, const char *sourcename,
				 const char *messagefile )
{
	/* Find all of the eventlogs, add keys for each of them */
	/* need to add to the value KEY_EVENTLOG/<eventlog>/Sources string (Creating if necessary)
	   need to add KEY of source to KEY_EVENTLOG/<eventlog>/<source> */

	const char **elogs = lp_eventlog_list(  );
	const char **wrklist, **wp;
	char *evtlogpath = NULL;
	int ii = 0;
	bool already_in;
	int i;
	int numsources = 0;
	TALLOC_CTX *ctx = talloc_stackframe();
	WERROR werr;
	struct registry_key *key_hive, *key_eventlog, *key_source;
	struct security_token *token = NULL;
	const char *hive_name, *relpath;
	enum winreg_CreateAction action;
	struct registry_value *value;
	static const uint32_t ACCESS = REG_KEY_READ | REG_KEY_WRITE;
	bool ret = false;

	if (!elogs) {
		d_printf("No Eventlogs configured\n");
		goto done;
	}

	for ( i = 0; elogs[i]; i++ ) {
		if ( strequal( elogs[i], eventlog ) )
			break;
	}

	if ( !elogs[i] ) {
		d_printf("Eventlog [%s] not found in list of valid event logs\n",
			 eventlog);
		goto done;
	}

	/* have to assume that the evenlog key itself exists at this point */
	/* add in a key of [sourcename] under the eventlog key */

	/* todo add to Sources */

	evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog);
	if (!evtlogpath) {
		d_printf("Out of memory\n");
		goto done;
	}

	relpath = evtlogpath + sizeof(KEY_EVENTLOG);
	hive_name = talloc_strndup(ctx, evtlogpath, relpath - evtlogpath);
	if (!hive_name) {
		d_printf("Out of memory\n");
		goto done;
	}
	relpath++;

	werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
	if (!W_ERROR_IS_OK(werr)) {
		d_printf("Failed to create admin token: %s\n", win_errstr(werr));
		goto done;
	}

	werr = reg_openhive(ctx, hive_name, ACCESS, token, &key_hive);
	if (!W_ERROR_IS_OK(werr)) {
		d_printf("Failed to open hive [%s]: %s\n", hive_name, win_errstr(werr));
		goto done;
	}

	werr = reg_openkey(ctx, key_hive, relpath, ACCESS, &key_eventlog);
	if (!W_ERROR_IS_OK(werr)) {
		d_printf("Failed to open key [%s]: %s\n", evtlogpath, win_errstr(werr));
		goto done;
	}

	werr = reg_queryvalue(ctx, key_eventlog, "Sources", &value);
	if (!W_ERROR_IS_OK(werr)) {
		d_printf("Failed to get value \"Sources\" for [%s]: %s\n", evtlogpath, win_errstr(werr));
		goto done;
	}
	/* perhaps this adding a new string to a multi_sz should be a fn? */
	/* check to see if it's there already */

	if ( value->type != REG_MULTI_SZ ) {
		d_printf("Wrong type for \"Sources\", should be REG_MULTI_SZ\n");
		goto done;
	}
	/* convert to a 'regulah' chars to do some comparisons */

	already_in = false;
	wrklist = NULL;
	dump_data(1, value->data.data, value->data.length);

	if (!pull_reg_multi_sz(ctx, &value->data, &wrklist)) {
		d_printf("Failed to pull REG_MULTI_SZ from \"Sources\"\n");
		goto done;
	}

	for (ii=0; wrklist[ii]; ii++) {
		numsources++;
	}

	if (numsources > 0) {
		/* see if it's in there already */
		wp = wrklist;

		while (wp && *wp ) {
			if ( strequal( *wp, sourcename ) ) {
				d_printf("Source name [%s] already in list for [%s] \n",
					 sourcename, eventlog);
				already_in = true;
				break;
			}
			wp++;
		}
	} else {
		d_printf("Nothing in the sources list, this might be a problem\n");
	}

	if ( !already_in ) {
		/* make a new list with an additional entry; copy values, add another */
		wp = talloc_realloc(ctx, wrklist, const char *, numsources + 2 );
		if ( !wp ) {
			d_printf("Out of memory\n");
			goto done;
		}

		wp[numsources] = sourcename;
		wp[numsources+1] = NULL;
		if (!push_reg_multi_sz(ctx, &value->data, wp)) {
			d_printf("Failed to push Sources\n");
			goto done;
		}
		dump_data( 1, value->data.data, value->data.length);
		werr = reg_setvalue(key_eventlog,  "Sources", value);
		if (!W_ERROR_IS_OK(werr)) {
			d_printf("Failed to set value Sources:  %s\n", win_errstr(werr));
			goto done;
		}
	} else {