s4-torture: Add a lsarpc test_LookupSids3_fail function.
[Samba/bb.git] / lib / param / generic.c
blob8becffbce5b6171d517bdf59a12cbe0385eb493b
1 /*
2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jelmer Vernooij 2005
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "../lib/util/dlinklist.h"
21 #include "../lib/util/parmlist.h"
22 #include "param/param.h"
23 #include "param/loadparm.h"
24 #include "system/filesys.h"
26 struct param_section *param_get_section(struct param_context *ctx, const char *name)
28 struct param_section *sect;
30 if (name == NULL)
31 name = GLOBAL_NAME;
33 for (sect = ctx->sections; sect; sect = sect->next) {
34 if (!strcasecmp_m(sect->name, name))
35 return sect;
38 return NULL;
41 struct parmlist_entry *param_section_get(struct param_section *section, const char *name)
43 return parmlist_get(section->parameters, name);
46 struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
48 struct param_section *section;
49 section = talloc_zero(ctx, struct param_section);
50 if (section == NULL)
51 return NULL;
53 section->name = talloc_strdup(section, section_name);
54 DLIST_ADD_END(ctx->sections, section, struct param_section *);
55 return section;
58 /* Look up parameter. If it is not found, add it */
59 struct parmlist_entry *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
61 struct param_section *section;
62 struct parmlist_entry *p;
64 SMB_ASSERT(section_name != NULL);
65 SMB_ASSERT(name != NULL);
67 section = param_get_section(ctx, section_name);
69 if (section == NULL) {
70 section = param_add_section(ctx, section_name);
73 p = param_section_get(section, name);
74 if (p == NULL) {
75 p = talloc_zero(section, struct parmlist_entry);
76 if (p == NULL)
77 return NULL;
79 p->key = talloc_strdup(p, name);
80 DLIST_ADD_END(section->parameters->entries, p, struct parmlist_entry *);
83 return p;
86 const char *param_get_string(struct param_context *ctx, const char *param, const char *section_name)
88 struct param_section *section = param_get_section(ctx, section_name);
90 if (section == NULL)
91 return NULL;
93 return parmlist_get_string(section->parameters, param, NULL);
96 int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section_name)
98 struct param_section *section = param_get_section(ctx, section_name);
100 if (section == NULL)
101 return -1;
103 return parmlist_set_string(section->parameters, param, value);
106 const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section_name)
108 struct param_section *section = param_get_section(ctx, section_name);
110 if (section == NULL)
111 return NULL;
113 return parmlist_get_string_list(section->parameters, param, separator);
116 int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
118 struct parmlist_entry *p = param_get_add(ctx, param, section);
120 p->value = str_list_join(p, list, ' ');
122 return 0;
125 int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section_name)
127 struct param_section *section = param_get_section(ctx, section_name);
129 if (section == NULL)
130 return default_v;
132 return parmlist_get_int(section->parameters, param, default_v);
135 void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
137 struct parmlist_entry *p = param_get_add(ctx, section, param);
139 if (!p)
140 return;
142 p->value = talloc_asprintf(p, "%d", value);
145 unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
147 const char *value = param_get_string(ctx, param, section);
149 if (value)
150 return strtoul(value, NULL, 0);
152 return default_v;
155 void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
157 struct parmlist_entry *p = param_get_add(ctx, name, section);
159 if (!p)
160 return;
162 p->value = talloc_asprintf(p, "%lu", value);
165 static bool param_sfunc (const char *name, void *_ctx)
167 struct param_context *ctx = (struct param_context *)_ctx;
168 struct param_section *section = param_get_section(ctx, name);
170 if (section == NULL) {
171 section = talloc_zero(ctx, struct param_section);
172 if (section == NULL)
173 return false;
175 section->name = talloc_strdup(section, name);
177 DLIST_ADD_END(ctx->sections, section, struct param_section *);
180 /* Make sure this section is on top of the list for param_pfunc */
181 DLIST_PROMOTE(ctx->sections, section);
183 return true;
186 static bool param_pfunc (const char *name, const char *value, void *_ctx)
188 struct param_context *ctx = (struct param_context *)_ctx;
189 struct parmlist_entry *p = param_section_get(ctx->sections, name);
191 if (!p) {
192 p = talloc_zero(ctx->sections, struct parmlist_entry);
193 if (p == NULL)
194 return false;
196 p->key = talloc_strdup(p, name);
197 p->value = talloc_strdup(p, value);
198 DLIST_ADD(ctx->sections->parameters->entries, p);
199 } else { /* Replace current value */
200 talloc_free(p->value);
201 p->value = talloc_strdup(p, value);
204 return true;
207 struct param_context *param_init(TALLOC_CTX *mem_ctx)
209 return talloc_zero(mem_ctx, struct param_context);
213 int param_read(struct param_context *ctx, const char *fn)
215 ctx->sections = talloc_zero(ctx, struct param_section);
216 if (ctx->sections == NULL)
217 return -1;
219 ctx->sections->name = talloc_strdup(ctx->sections, "global");
220 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
221 return -1;
224 return 0;
227 int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
229 struct param_section *section;
231 for (section = ctx->sections; section; section = section->next) {
232 struct parmlist_entry *param;
233 bool isglobal = strcmp(section->name, "global") == 0;
234 for (param = section->parameters->entries; param; param = param->next) {
235 if (isglobal)
236 lpcfg_do_global_parameter(lp_ctx, param->key,
237 param->value);
238 else {
239 struct loadparm_service *service =
240 lpcfg_service(lp_ctx, section->name);
241 if (service == NULL)
242 service = lpcfg_add_service(lp_ctx, lpcfg_default_service(lp_ctx), section->name);
243 lpcfg_do_service_parameter(lp_ctx, service, param->key, param->value);
247 return 0;
250 int param_write(struct param_context *ctx, const char *fn)
252 int file;
253 struct param_section *section;
255 if (fn == NULL || ctx == NULL)
256 return -1;
258 file = open(fn, O_WRONLY|O_CREAT, 0755);
260 if (file == -1)
261 return -1;
263 for (section = ctx->sections; section; section = section->next) {
264 struct parmlist_entry *param;
266 fdprintf(file, "[%s]\n", section->name);
267 for (param = section->parameters->entries; param; param = param->next) {
268 fdprintf(file, "\t%s = %s\n", param->key, param->value);
270 fdprintf(file, "\n");
273 close(file);
275 return 0;