1 |
|
---|
2 | /*
|
---|
3 | * Unix SMB/CIFS implementation.
|
---|
4 | * Group Policy Object Support
|
---|
5 | * Copyright (C) Wilco Baan Hofman 2010
|
---|
6 | *
|
---|
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.
|
---|
11 | *
|
---|
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.
|
---|
16 | *
|
---|
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/>.
|
---|
19 | */
|
---|
20 | #include "includes.h"
|
---|
21 | #include "lib/util/util.h"
|
---|
22 | #include "lib/policy/policy.h"
|
---|
23 |
|
---|
24 | struct gp_parse_context {
|
---|
25 | struct gp_ini_context *ini;
|
---|
26 | int32_t cur_section;
|
---|
27 | };
|
---|
28 |
|
---|
29 |
|
---|
30 | static bool gp_add_ini_section(const char *name, void *callback_data)
|
---|
31 | {
|
---|
32 | struct gp_parse_context *parse = callback_data;
|
---|
33 | struct gp_ini_context *ini = parse->ini;
|
---|
34 |
|
---|
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++;
|
---|
41 |
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | static bool gp_add_ini_param(const char *name, const char *value, void *callback_data)
|
---|
46 | {
|
---|
47 | struct gp_parse_context *parse = callback_data;
|
---|
48 | struct gp_ini_context *ini = parse->ini;
|
---|
49 | struct gp_ini_section *section;
|
---|
50 |
|
---|
51 | if (parse->cur_section == -1) {
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 |
|
---|
55 | section = &ini->sections[parse->cur_section];
|
---|
56 |
|
---|
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++;
|
---|
64 |
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | NTSTATUS gp_parse_ini(TALLOC_CTX *mem_ctx, struct gp_context *gp_ctx, const char *filename, struct gp_ini_context **ret)
|
---|
69 | {
|
---|
70 | struct gp_parse_context parse;
|
---|
71 | bool rv;
|
---|
72 |
|
---|
73 | parse.ini = talloc_zero(mem_ctx, struct gp_ini_context);
|
---|
74 | NT_STATUS_HAVE_NO_MEMORY(parse.ini);
|
---|
75 | parse.cur_section = -1;
|
---|
76 |
|
---|
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;
|
---|
81 | }
|
---|
82 |
|
---|
83 | *ret = parse.ini;
|
---|
84 | return NT_STATUS_OK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | NTSTATUS gp_get_ini_string(struct gp_ini_context *ini, const char *section, const char *name, char **ret)
|
---|
88 | {
|
---|
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;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (cur_sec == -1) {
|
---|
99 | return NT_STATUS_NOT_FOUND;
|
---|
100 | }
|
---|
101 |
|
---|
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;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return NT_STATUS_NOT_FOUND;
|
---|
109 | }
|
---|
110 |
|
---|
111 | NTSTATUS gp_get_ini_uint(struct gp_ini_context *ini, const char *section, const char *name, uint32_t *ret)
|
---|
112 | {
|
---|
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;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (cur_sec == -1) {
|
---|
123 | return NT_STATUS_NOT_FOUND;
|
---|
124 | }
|
---|
125 |
|
---|
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;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return NT_STATUS_NOT_FOUND;
|
---|
133 | }
|
---|