summaryrefslogtreecommitdiff
path: root/lib/smbconf/smbconf_util.c
blob: b309a3454b13d5fee8ad164b9d99ac30fca7236c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 *  Unix SMB/CIFS implementation.
 *  libsmbconf - Samba configuration library, utility functions
 *  Copyright (C) Michael Adam 2008
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "smbconf_private.h"


static int smbconf_destroy_ctx(struct smbconf_ctx *ctx)
{
	return ctx->ops->shutdown(ctx);
}

/**
 * Initialize the configuration.
 *
 * This should be the first function in a sequence of calls to smbconf
 * functions:
 *
 * Upon success, this creates and returns the conf context
 * that should be passed around in subsequent calls to the other
 * smbconf functions.
 *
 * After the work with the configuration is completed, smbconf_shutdown()
 * should be called.
 */
WERROR smbconf_init_internal(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
			     const char *path, struct smbconf_ops *ops)
{
	WERROR werr = WERR_OK;
	struct smbconf_ctx *ctx;

	if (conf_ctx == NULL) {
		return WERR_INVALID_PARAM;
	}

	ctx = talloc_zero(mem_ctx, struct smbconf_ctx);
	if (ctx == NULL) {
		return WERR_NOMEM;
	}

	ctx->ops = ops;

	werr = ctx->ops->init(ctx, path);
	if (!W_ERROR_IS_OK(werr)) {
		goto fail;
	}

	talloc_set_destructor(ctx, smbconf_destroy_ctx);

	*conf_ctx = ctx;
	return werr;

fail:
	talloc_free(ctx);
	return werr;
}


/**
 * add a string to a talloced array of strings.
 */
WERROR smbconf_add_string_to_array(TALLOC_CTX *mem_ctx,
				   char ***array,
				   uint32_t count,
				   const char *string)
{
	char **new_array = NULL;

	if (array == NULL) {
		return WERR_INVALID_PARAM;
	}

	new_array = talloc_realloc(mem_ctx, *array, char *, count + 1);
	if (new_array == NULL) {
		return WERR_NOMEM;
	}

	if (string == NULL) {
		new_array[count] = NULL;
	} else {
		new_array[count] = talloc_strdup(new_array, string);
		if (new_array[count] == NULL) {
			talloc_free(new_array);
			return WERR_NOMEM;
		}
	}

	*array = new_array;

	return WERR_OK;
}

bool smbconf_find_in_array(const char *string, char **list,
			   uint32_t num_entries, uint32_t *entry)
{
	uint32_t i;

	if (list == NULL) {
		return false;
	}

	for (i = 0; i < num_entries; i++) {
		if (((string == NULL) && (list[i] == NULL)) ||
		    strequal(string, list[i]))
		{
			if (entry != NULL) {
				*entry = i;
			}
			return true;
		}
	}

	return false;
}

bool smbconf_reverse_find_in_array(const char *string, char **list,
				   uint32_t num_entries, uint32_t *entry)
{
	int32_t i;

	if ((string == NULL) || (list == NULL) || (num_entries == 0)) {
		return false;
	}

	for (i = num_entries - 1; i >= 0; i--) {
		if (strequal(string, list[i])) {
			if (entry != NULL) {
				*entry = i;
			}
			return true;
		}
	}

	return false;
}