~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source4/param/generic.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "param/param.h"
 
22
#include "param/loadparm.h"
 
23
#include "system/filesys.h"
 
24
 
 
25
struct param_section *param_get_section(struct param_context *ctx, const char *name)
 
26
{
 
27
        struct param_section *sect;
 
28
 
 
29
        if (name == NULL) 
 
30
                name = GLOBAL_NAME;
 
31
 
 
32
        for (sect = ctx->sections; sect; sect = sect->next) {
 
33
                if (!strcasecmp_m(sect->name, name)) 
 
34
                        return sect;
 
35
        }
 
36
 
 
37
        return NULL;
 
38
}
 
39
 
 
40
struct param_opt *param_section_get(struct param_section *section, 
 
41
                                    const char *name)
 
42
{
 
43
        struct param_opt *p;
 
44
 
 
45
        for (p = section->parameters; p; p = p->next) {
 
46
                if (strcasecmp_m(p->key, name) == 0) 
 
47
                        return p;
 
48
        }
 
49
 
 
50
        return NULL;
 
51
}
 
52
 
 
53
struct param_opt *param_get (struct param_context *ctx, const char *name, const char *section_name)
 
54
{
 
55
        struct param_section *section = param_get_section(ctx, section_name);
 
56
        if (section == NULL)
 
57
                return NULL;
 
58
 
 
59
        return param_section_get(section, name);
 
60
}
 
61
 
 
62
struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
 
63
{
 
64
        struct param_section *section;
 
65
        section = talloc_zero(ctx, struct param_section);
 
66
        if (section == NULL)
 
67
                return NULL;
 
68
 
 
69
        section->name = talloc_strdup(section, section_name);
 
70
        DLIST_ADD_END(ctx->sections, section, struct param_section *);
 
71
        return section;
 
72
}
 
73
 
 
74
/* Look up parameter. If it is not found, add it */
 
75
struct param_opt *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
 
76
{
 
77
        struct param_section *section;
 
78
        struct param_opt *p;
 
79
 
 
80
        SMB_ASSERT(section_name != NULL);
 
81
        SMB_ASSERT(name != NULL);
 
82
 
 
83
        section = param_get_section(ctx, section_name);
 
84
 
 
85
        if (section == NULL) {
 
86
                section = param_add_section(ctx, section_name);
 
87
        }
 
88
 
 
89
        p = param_section_get(section, name);
 
90
        if (p == NULL) {
 
91
                p = talloc_zero(section, struct param_opt);
 
92
                if (p == NULL)
 
93
                        return NULL;
 
94
 
 
95
                p->key = talloc_strdup(p, name);
 
96
                DLIST_ADD_END(section->parameters, p, struct param_opt *);
 
97
        }
 
98
        
 
99
        return p;
 
100
}
 
101
 
 
102
const char *param_get_string(struct param_context *ctx, const char *param, const char *section)
 
103
{
 
104
        struct param_opt *p = param_get(ctx, param, section);
 
105
 
 
106
        if (p == NULL)
 
107
                return NULL;
 
108
 
 
109
        return p->value;
 
110
}
 
111
 
 
112
int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section)
 
113
{
 
114
        struct param_opt *p = param_get_add(ctx, param, section);
 
115
 
 
116
        if (p == NULL)
 
117
                return -1;
 
118
 
 
119
        p->value = talloc_strdup(p, value);
 
120
 
 
121
        return 0;
 
122
}
 
123
 
 
124
const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section)
 
125
{
 
126
        struct param_opt *p = param_get(ctx, param, section);
 
127
        
 
128
        if (p == NULL)
 
129
                return NULL;
 
130
 
 
131
        return (const char **)str_list_make(ctx, p->value, separator);
 
132
}
 
133
 
 
134
int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
 
135
{
 
136
        struct param_opt *p = param_get_add(ctx, param, section);       
 
137
 
 
138
        p->value = str_list_join(p, list, ' ');
 
139
 
 
140
        return 0;
 
141
}
 
142
 
 
143
int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section)
 
144
{
 
145
        const char *value = param_get_string(ctx, param, section);
 
146
        
 
147
        if (value)
 
148
                return strtol(value, NULL, 0); 
 
149
 
 
150
        return default_v;
 
151
}
 
152
 
 
153
void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
 
154
{
 
155
        struct param_opt *p = param_get_add(ctx, section, param);
 
156
 
 
157
        if (!p) 
 
158
                return;
 
159
 
 
160
        p->value = talloc_asprintf(p, "%d", value);
 
161
}
 
162
 
 
163
unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
 
164
{
 
165
        const char *value = param_get_string(ctx, param, section);
 
166
        
 
167
        if (value)
 
168
                return strtoul(value, NULL, 0);
 
169
 
 
170
        return default_v;
 
171
}
 
172
 
 
173
void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
 
174
{
 
175
        struct param_opt *p = param_get_add(ctx, name, section);
 
176
 
 
177
        if (!p)
 
178
                return;
 
179
 
 
180
        p->value = talloc_asprintf(p, "%lu", value);
 
181
}
 
182
 
 
183
static bool param_sfunc (const char *name, void *_ctx)
 
184
{
 
185
        struct param_context *ctx = (struct param_context *)_ctx;
 
186
        struct param_section *section = param_get_section(ctx, name);
 
187
 
 
188
        if (section == NULL) {
 
189
                section = talloc_zero(ctx, struct param_section);
 
190
                if (section == NULL)
 
191
                        return false;
 
192
 
 
193
                section->name = talloc_strdup(section, name);
 
194
 
 
195
                DLIST_ADD_END(ctx->sections, section, struct param_section *);
 
196
        }
 
197
 
 
198
        /* Make sure this section is on top of the list for param_pfunc */
 
199
        DLIST_PROMOTE(ctx->sections, section);
 
200
 
 
201
        return true;
 
202
}
 
203
 
 
204
static bool param_pfunc (const char *name, const char *value, void *_ctx)
 
205
{
 
206
        struct param_context *ctx = (struct param_context *)_ctx;
 
207
        struct param_opt *p = param_section_get(ctx->sections, name);
 
208
 
 
209
        if (!p) {
 
210
                p = talloc_zero(ctx->sections, struct param_opt);
 
211
                if (p == NULL)
 
212
                        return false;
 
213
 
 
214
                p->key = talloc_strdup(p, name);
 
215
                p->value = talloc_strdup(p, value);
 
216
                DLIST_ADD(ctx->sections->parameters, p);
 
217
        } else { /* Replace current value */
 
218
                talloc_free(p->value);
 
219
                p->value = talloc_strdup(p, value);
 
220
        }
 
221
 
 
222
        return true;
 
223
}
 
224
 
 
225
struct param_context *param_init(TALLOC_CTX *mem_ctx)
 
226
{
 
227
        return talloc_zero(mem_ctx, struct param_context);
 
228
}
 
229
 
 
230
 
 
231
int param_read(struct param_context *ctx, const char *fn)
 
232
{
 
233
        ctx->sections = talloc_zero(ctx, struct param_section);
 
234
        if (ctx->sections == NULL)
 
235
                return -1;
 
236
 
 
237
        ctx->sections->name = talloc_strdup(ctx->sections, "global");
 
238
        if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
 
239
                return -1;
 
240
        }
 
241
 
 
242
        return 0;
 
243
}
 
244
 
 
245
int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
 
246
{
 
247
        struct param_section *section;
 
248
 
 
249
        for (section = ctx->sections; section; section = section->next) {
 
250
                struct param_opt *param;
 
251
                bool isglobal = strcmp(section->name, "global") == 0;
 
252
                for (param = section->parameters; param; param = param->next) {
 
253
                        if (isglobal)
 
254
                                lp_do_global_parameter(lp_ctx, param->key,
 
255
                                                       param->value);
 
256
                        else {
 
257
                                struct loadparm_service *service = 
 
258
                                                        lp_service(lp_ctx, section->name);
 
259
                                if (service == NULL)
 
260
                                        service = lp_add_service(lp_ctx, lp_default_service(lp_ctx), section->name);
 
261
                                lp_do_service_parameter(lp_ctx, service, param->key, param->value);
 
262
                        }
 
263
                }
 
264
        }
 
265
        return 0;
 
266
}
 
267
 
 
268
int param_write(struct param_context *ctx, const char *fn)
 
269
{
 
270
        int file;
 
271
        struct param_section *section;
 
272
 
 
273
        if (fn == NULL || ctx == NULL)
 
274
                return -1;
 
275
 
 
276
        file = open(fn, O_WRONLY|O_CREAT, 0755);
 
277
 
 
278
        if (file == -1)
 
279
                return -1;
 
280
        
 
281
        for (section = ctx->sections; section; section = section->next) {
 
282
                struct param_opt *param;
 
283
                
 
284
                fdprintf(file, "[%s]\n", section->name);
 
285
                for (param = section->parameters; param; param = param->next) {
 
286
                        fdprintf(file, "\t%s = %s\n", param->key, param->value);
 
287
                }
 
288
                fdprintf(file, "\n");
 
289
        }
 
290
 
 
291
        close(file);
 
292
 
 
293
        return 0;
 
294
}