lib/param: Merge "Ldap Options" section from source3/param
[Samba.git] / source4 / lib / policy / gp_ini.c
blobda2f5f43d54762f99bea292c8a253542044c0d35
2 /*
3 * Unix SMB/CIFS implementation.
4 * Group Policy Object Support
5 * Copyright (C) Wilco Baan Hofman 2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "lib/util/samba_util.h"
22 #include "lib/policy/policy.h"
24 struct gp_parse_context {
25 struct gp_ini_context *ini;
26 int32_t cur_section;
30 static bool gp_add_ini_section(const char *name, void *callback_data)
32 struct gp_parse_context *parse = callback_data;
33 struct gp_ini_context *ini = parse->ini;
35 ini->sections = talloc_realloc(ini, ini->sections, struct gp_ini_section, ini->num_sections+1);
36 if (ini->sections == NULL) return false;
37 ini->sections[ini->num_sections].name = talloc_strdup(ini, name);
38 if (ini->sections[ini->num_sections].name == NULL) return false;
39 parse->cur_section = ini->num_sections;
40 ini->num_sections++;
42 return true;
45 static bool gp_add_ini_param(const char *name, const char *value, void *callback_data)
47 struct gp_parse_context *parse = callback_data;
48 struct gp_ini_context *ini = parse->ini;
49 struct gp_ini_section *section;
51 if (parse->cur_section == -1) {
52 return false;
55 section = &ini->sections[parse->cur_section];
57 section->params = talloc_realloc(ini, ini->sections[parse->cur_section].params, struct gp_ini_param, section->num_params+1);
58 if (section->params == NULL) return false;
59 section->params[section->num_params].name = talloc_strdup(ini, name);
60 if (section->params[section->num_params].name == NULL) return false;
61 section->params[section->num_params].value = talloc_strdup(ini, value);
62 if (section->params[section->num_params].value == NULL) return false;
63 section->num_params++;
65 return true;
68 NTSTATUS gp_parse_ini(TALLOC_CTX *mem_ctx, struct gp_context *gp_ctx, const char *filename, struct gp_ini_context **ret)
70 struct gp_parse_context parse;
71 bool rv;
73 parse.ini = talloc_zero(mem_ctx, struct gp_ini_context);
74 NT_STATUS_HAVE_NO_MEMORY(parse.ini);
75 parse.cur_section = -1;
77 rv = pm_process(filename, gp_add_ini_section, gp_add_ini_param, &parse);
78 if (!rv) {
79 DEBUG(0, ("Error while processing ini file %s\n", filename));
80 return NT_STATUS_UNSUCCESSFUL;
83 *ret = parse.ini;
84 return NT_STATUS_OK;
87 NTSTATUS gp_get_ini_string(struct gp_ini_context *ini, const char *section, const char *name, char **ret)
89 uint16_t i;
90 int32_t cur_sec = -1;
91 for (i = 0; i < ini->num_sections; i++) {
92 if (strcmp(ini->sections[i].name, section) == 0) {
93 cur_sec = i;
94 break;
98 if (cur_sec == -1) {
99 return NT_STATUS_NOT_FOUND;
102 for (i = 0; i < ini->sections[cur_sec].num_params; i++) {
103 if (strcmp(ini->sections[cur_sec].params[i].name, name) == 0) {
104 *ret = ini->sections[cur_sec].params[i].value;
105 return NT_STATUS_OK;
108 return NT_STATUS_NOT_FOUND;
111 NTSTATUS gp_get_ini_uint(struct gp_ini_context *ini, const char *section, const char *name, uint32_t *ret)
113 uint16_t i;
114 int32_t cur_sec = -1;
115 for (i = 0; i < ini->num_sections; i++) {
116 if (strcmp(ini->sections[i].name, section) == 0) {
117 cur_sec = i;
118 break;
122 if (cur_sec == -1) {
123 return NT_STATUS_NOT_FOUND;
126 for (i = 0; i < ini->sections[cur_sec].num_params; i++) {
127 if (strcmp(ini->sections[cur_sec].params[i].name, name) == 0) {
128 *ret = atol(ini->sections[cur_sec].params[i].value);
129 return NT_STATUS_OK;
132 return NT_STATUS_NOT_FOUND;