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/>.
21 #include "lib/util/samba_util.h"
22 #include "lib/policy/policy.h"
24 struct gp_parse_context
{
25 struct gp_ini_context
*ini
;
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
;
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) {
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
++;
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
;
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
);
79 DEBUG(0, ("Error while processing ini file %s\n", filename
));
80 return NT_STATUS_UNSUCCESSFUL
;
87 NTSTATUS
gp_get_ini_string(struct gp_ini_context
*ini
, const char *section
, const char *name
, char **ret
)
91 for (i
= 0; i
< ini
->num_sections
; i
++) {
92 if (strcmp(ini
->sections
[i
].name
, section
) == 0) {
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
;
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
)
114 int32_t cur_sec
= -1;
115 for (i
= 0; i
< ini
->num_sections
; i
++) {
116 if (strcmp(ini
->sections
[i
].name
, section
) == 0) {
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
);
132 return NT_STATUS_NOT_FOUND
;