s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[sfrench/samba-autobuild/.git] / source4 / param / generic.c
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.
14  *  
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/>.
17  */
18
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"
25
26 struct param_section *param_get_section(struct param_context *ctx, const char *name)
27 {
28         struct param_section *sect;
29
30         if (name == NULL) 
31                 name = GLOBAL_NAME;
32
33         for (sect = ctx->sections; sect; sect = sect->next) {
34                 if (!strcasecmp_m(sect->name, name)) 
35                         return sect;
36         }
37
38         return NULL;
39 }
40
41 struct parmlist_entry *param_section_get(struct param_section *section, const char *name)
42 {
43         return parmlist_get(section->parameters, name);
44 }
45
46 struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
47 {
48         struct param_section *section;
49         section = talloc_zero(ctx, struct param_section);
50         if (section == NULL)
51                 return NULL;
52
53         section->name = talloc_strdup(section, section_name);
54         DLIST_ADD_END(ctx->sections, section, struct param_section *);
55         return section;
56 }
57
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)
60 {
61         struct param_section *section;
62         struct parmlist_entry *p;
63
64         SMB_ASSERT(section_name != NULL);
65         SMB_ASSERT(name != NULL);
66
67         section = param_get_section(ctx, section_name);
68
69         if (section == NULL) {
70                 section = param_add_section(ctx, section_name);
71         }
72
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;
78
79                 p->key = talloc_strdup(p, name);
80                 DLIST_ADD_END(section->parameters->entries, p, struct parmlist_entry *);
81         }
82         
83         return p;
84 }
85
86 const char *param_get_string(struct param_context *ctx, const char *param, const char *section_name)
87 {
88         struct param_section *section = param_get_section(ctx, section_name);
89
90         if (section == NULL)
91                 return NULL;
92
93         return parmlist_get_string(section->parameters, param, NULL);
94 }
95
96 int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section_name)
97 {
98         struct param_section *section = param_get_section(ctx, section_name);
99
100         if (section == NULL)
101                 return -1;
102
103         return parmlist_set_string(section->parameters, param, value);
104 }
105
106 const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section_name)
107 {
108         struct param_section *section = param_get_section(ctx, section_name);
109
110         if (section == NULL)
111                 return NULL;
112
113         return parmlist_get_string_list(section->parameters, param, separator);
114 }
115
116 int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
117 {
118         struct parmlist_entry *p = param_get_add(ctx, param, section);  
119
120         p->value = str_list_join(p, list, ' ');
121
122         return 0;
123 }
124
125 int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section_name)
126 {
127         struct param_section *section = param_get_section(ctx, section_name);
128
129         if (section == NULL)
130                 return default_v;
131
132         return parmlist_get_int(section->parameters, param, default_v);
133 }
134
135 void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
136 {
137         struct parmlist_entry *p = param_get_add(ctx, section, param);
138
139         if (!p) 
140                 return;
141
142         p->value = talloc_asprintf(p, "%d", value);
143 }
144
145 unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
146 {
147         const char *value = param_get_string(ctx, param, section);
148         
149         if (value)
150                 return strtoul(value, NULL, 0);
151
152         return default_v;
153 }
154
155 void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
156 {
157         struct parmlist_entry *p = param_get_add(ctx, name, section);
158
159         if (!p)
160                 return;
161
162         p->value = talloc_asprintf(p, "%lu", value);
163 }
164
165 static bool param_sfunc (const char *name, void *_ctx)
166 {
167         struct param_context *ctx = (struct param_context *)_ctx;
168         struct param_section *section = param_get_section(ctx, name);
169
170         if (section == NULL) {
171                 section = talloc_zero(ctx, struct param_section);
172                 if (section == NULL)
173                         return false;
174
175                 section->name = talloc_strdup(section, name);
176
177                 DLIST_ADD_END(ctx->sections, section, struct param_section *);
178         }
179
180         /* Make sure this section is on top of the list for param_pfunc */
181         DLIST_PROMOTE(ctx->sections, section);
182
183         return true;
184 }
185
186 static bool param_pfunc (const char *name, const char *value, void *_ctx)
187 {
188         struct param_context *ctx = (struct param_context *)_ctx;
189         struct parmlist_entry *p = param_section_get(ctx->sections, name);
190
191         if (!p) {
192                 p = talloc_zero(ctx->sections, struct parmlist_entry);
193                 if (p == NULL)
194                         return false;
195
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);
202         }
203
204         return true;
205 }
206
207 struct param_context *param_init(TALLOC_CTX *mem_ctx)
208 {
209         return talloc_zero(mem_ctx, struct param_context);
210 }
211
212
213 int param_read(struct param_context *ctx, const char *fn)
214 {
215         ctx->sections = talloc_zero(ctx, struct param_section);
216         if (ctx->sections == NULL)
217                 return -1;
218
219         ctx->sections->name = talloc_strdup(ctx->sections, "global");
220         if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
221                 return -1;
222         }
223
224         return 0;
225 }
226
227 int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
228 {
229         struct param_section *section;
230
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);
244                         }
245                 }
246         }
247         return 0;
248 }
249
250 int param_write(struct param_context *ctx, const char *fn)
251 {
252         int file;
253         struct param_section *section;
254
255         if (fn == NULL || ctx == NULL)
256                 return -1;
257
258         file = open(fn, O_WRONLY|O_CREAT, 0755);
259
260         if (file == -1)
261                 return -1;
262         
263         for (section = ctx->sections; section; section = section->next) {
264                 struct parmlist_entry *param;
265                 
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);
269                 }
270                 fdprintf(file, "\n");
271         }
272
273         close(file);
274
275         return 0;
276 }