From 9f4b32996c1c97122b198a13216c35ad40f6ea2d Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 30 Aug 2005 14:44:33 +0000 Subject: r9800: Add EJS interface to param. tridge, sorry this overlaps a bit with your loadparm interface. :-/ (This used to be commit bb0cef581a09a86113f3212c776c011ae73def14) --- source4/lib/samba3/samba3.c | 8 +- source4/param/generic.c | 46 ++++--- source4/scripting/ejs/config.mk | 1 + source4/scripting/ejs/smbcalls.c | 1 + source4/scripting/ejs/smbcalls_param.c | 207 ++++++++++++++++++++++++++++++++ source4/scripting/ejs/smbcalls_samba3.c | 5 +- source4/scripting/libjs/upgrade.js | 6 +- 7 files changed, 252 insertions(+), 22 deletions(-) create mode 100644 source4/scripting/ejs/smbcalls_param.c diff --git a/source4/lib/samba3/samba3.c b/source4/lib/samba3/samba3.c index d90d5edbfe..b919366ce3 100644 --- a/source4/lib/samba3/samba3.c +++ b/source4/lib/samba3/samba3.c @@ -32,15 +32,17 @@ struct samba3_domainsecrets *samba3_find_domainsecrets(struct samba3 *db, const return NULL; } -NTSTATUS samba3_read(const char *smbconf, const char *libdir, TALLOC_CTX *ctx, struct samba3 **samba3) +NTSTATUS samba3_read(const char *libdir, const char *smbconf, TALLOC_CTX *ctx, struct samba3 **samba3) { struct samba3 *ret; char *dbfile = NULL; ret = talloc_zero(ctx, struct samba3); - if (smbconf) - ret->configuration = param_read(ret, smbconf); + if (smbconf) { + ret->configuration = param_init(ret); + param_read(ret->configuration, smbconf); + } dbfile = talloc_asprintf(ctx, "%s/account_policy.tdb", libdir); samba3_read_account_policy(dbfile, ctx, &ret->policy); diff --git a/source4/param/generic.c b/source4/param/generic.c index 0129f82eeb..2e18b69f03 100644 --- a/source4/param/generic.c +++ b/source4/param/generic.c @@ -20,6 +20,7 @@ #include "includes.h" #include "dlinklist.h" #include "param/generic.h" +#include "system/filesys.h" struct param_section *param_get_section(struct param_context *ctx, const char *name) { @@ -41,7 +42,7 @@ struct param *param_section_get (struct param_section *section, const char *name struct param *p; for (p = section->parameters; p; p = p->next) { - if (strcasecmp(p->name, name) == 0) + if (strcasecmp_m(p->name, name) == 0) return p; } @@ -91,11 +92,16 @@ const char *param_get_string(struct param_context *ctx, const char *section, con return p->value; } -void param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value) +int param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value) { struct param *p = param_get_add(ctx, section, param); + if (p == NULL) + return -1; + p->value = talloc_strdup(p, value); + + return 0; } const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param, @@ -105,6 +111,9 @@ const char **param_get_string_list(struct param_context *ctx, const char *sectio if (p == NULL) return NULL; + + if (separator == NULL) + separator = LIST_SEP; if (p->list_value == NULL) { p->list_value = str_list_make(ctx, p->value, separator); @@ -167,13 +176,12 @@ static BOOL param_sfunc (const char *name, void *_ctx) DLIST_ADD(ctx->sections, section); } + /* Make sure this section is on top of the list for param_pfunc */ DLIST_PROMOTE(ctx->sections, section); return True; } - - static BOOL param_pfunc (const char *name, const char *value, void *_ctx) { struct param_context *ctx = _ctx; @@ -192,40 +200,48 @@ static BOOL param_pfunc (const char *name, const char *value, void *_ctx) return True; } -struct param_context *param_read(TALLOC_CTX *mem_ctx, const char *fn) +struct param_context *param_init(TALLOC_CTX *mem_ctx) { - struct param_context *ctx = talloc_zero(mem_ctx, struct param_context); + return talloc_zero(mem_ctx, struct param_context); +} + +int param_read(struct param_context *ctx, const char *fn) +{ ctx->sections = talloc_zero(ctx, struct param_section); ctx->sections->name = talloc_strdup(ctx->sections, "global"); if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) { - talloc_free(ctx); - return NULL; + return -1; } - return ctx; + return 0; } -int param_write(FILE *file, struct param_context *ctx) +int param_write(struct param_context *ctx, const char *fn) { + XFILE *file; struct param_section *section; - if (file == NULL) + if (fn == NULL || ctx == NULL) return -1; - if (ctx == NULL) + file = x_fopen(fn, O_WRONLY|O_CREAT, 0755); + + if (file == NULL) return -1; for (section = ctx->sections; section; section = section->next) { struct param *param; - fprintf(file, "[%s]\n", section->name); + x_fprintf(file, "[%s]\n", section->name); for (param = section->parameters; param; param = param->next) { - fprintf(file, "\t%s = %s\n", param->name, param->value); + x_fprintf(file, "\t%s = %s\n", param->name, param->value); } - fprintf(file, "\n"); + x_fprintf(file, "\n"); } + x_fclose(file); + return 0; } diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk index e5711f1424..b997a656ab 100644 --- a/source4/scripting/ejs/config.mk +++ b/source4/scripting/ejs/config.mk @@ -27,6 +27,7 @@ OBJ_FILES = \ scripting/ejs/smbcalls_sys.o \ scripting/ejs/smbcalls_creds.o \ scripting/ejs/smbcalls_samba3.o \ + scripting/ejs/smbcalls_param.o \ scripting/ejs/mprutil.o REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC EJSRPC MESSAGING LIBSAMBA3 # End SUBSYSTEM SMBCALLS diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index a73fbe52c3..d0ce1366a5 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -129,6 +129,7 @@ void smb_setup_ejs_functions(void) smb_setup_ejs_system(); smb_setup_ejs_credentials(); smb_setup_ejs_samba3(); + smb_setup_ejs_param(); smb_setup_ejs_datablob(); ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); diff --git a/source4/scripting/ejs/smbcalls_param.c b/source4/scripting/ejs/smbcalls_param.c new file mode 100644 index 0000000000..e5553cac0b --- /dev/null +++ b/source4/scripting/ejs/smbcalls_param.c @@ -0,0 +1,207 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Jelmer Vernooij 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "scripting/ejs/smbcalls.h" +#include "lib/appweb/ejs/ejs.h" +#include "param/generic.h" +#include "dynconfig.h" + +/* + get parameter + + value = param.get("name"); + value = param.get("section", "name"); +*/ +static int ejs_param_get(MprVarHandle eid, int argc, char **argv) +{ + struct param_context *ctx; + const char *ret; + if (argc != 1 && argc != 2) { + ejsSetErrorMsg(eid, "param.get invalid argument count"); + return -1; + } + + ctx = mprGetThisPtr(eid, "param"); + mprAssert(ctx); + + if (argc == 2) { + ret = param_get_string(ctx, argv[0], argv[1]); + } else { + ret = param_get_string(ctx, NULL, argv[0]); + } + + if (ret) { + mpr_Return(eid, mprString(ret)); + } else { + mpr_Return(eid, mprCreateUndefinedVar()); + } + return 0; +} + +/* + get list parameter + + ok = param.get_list("name"); + ok = param.get_list("section", "name"); +*/ +static int ejs_param_get_list(MprVarHandle eid, int argc, char **argv) +{ + struct param_context *ctx; + const char **ret; + + if (argc != 1 && argc != 2) { + ejsSetErrorMsg(eid, "param.get invalid argument count"); + return -1; + } + + ctx = mprGetThisPtr(eid, "param"); + mprAssert(ctx); + + if (argc == 2) { + ret = param_get_string_list(ctx, argv[0], argv[1], NULL); + } else { + ret = param_get_string_list(ctx, NULL, argv[0], NULL); + } + + if (ret) { + mpr_Return(eid, mprList("array", ret)); + } else { + mpr_Return(eid, mprCreateUndefinedVar()); + } + return 0; +} + +/* + set parameter + + ok = param.set("name", "value"); + ok = param.set("section", "name", "value"); +*/ +static int ejs_param_set(MprVarHandle eid, int argc, char **argv) +{ + struct param_context *ctx; + bool ret; + if (argc != 2 && argc != 3) { + ejsSetErrorMsg(eid, "param.set invalid argument count"); + return -1; + } + + ctx = mprGetThisPtr(eid, "param"); + mprAssert(ctx); + + if (argc == 3) { + ret = param_set_string(ctx, argv[0], argv[1], argv[2]); + } else { + ret = param_set_string(ctx, NULL, argv[0], argv[2]); + } + + mpr_Return(eid, mprCreateBoolVar(ret)); + return 0; +} + +/* + load file + + ok = param.load(file); +*/ +static int ejs_param_load(MprVarHandle eid, int argc, char **argv) +{ + struct param_context *ctx; + bool ret; + + if (argc != 1) { + ejsSetErrorMsg(eid, "param.load invalid argument count"); + return -1; + } + + ctx = mprGetThisPtr(eid, "param"); + mprAssert(ctx); + + ret = param_read(ctx, argv[0]); + + mpr_Return(eid, mprCreateBoolVar(ret)); + return 0; +} + +/* + save file + + ok = param.save(file); +*/ +static int ejs_param_save(MprVarHandle eid, int argc, char **argv) +{ + struct param_context *ctx; + bool ret; + + if (argc != 1) { + ejsSetErrorMsg(eid, "param.save invalid argument count"); + return -1; + } + + ctx = mprGetThisPtr(eid, "param"); + mprAssert(ctx); + + ret = param_write(ctx, argv[0]); + + mpr_Return(eid, mprCreateBoolVar(ret)); + return 0; +} + +static void param_add_members(struct MprVar *obj) +{ + mprSetStringCFunction(obj, "get", ejs_param_get); + mprSetStringCFunction(obj, "get_list", ejs_param_get_list); + mprSetStringCFunction(obj, "set", ejs_param_set); + mprSetStringCFunction(obj, "load", ejs_param_load); + mprSetStringCFunction(obj, "save", ejs_param_save); +} + +/* + initialise param ejs subsystem +*/ +static int ejs_param_init(MprVarHandle eid, int argc, struct MprVar **argv) +{ + struct MprVar *obj = mprInitObject(eid, "param", argc, argv); + + mprSetPtrChild(obj, "param", param_init(mprMemCtx())); + + param_add_members(obj); + + return 0; +} + +struct MprVar mprParam(struct param_context *ctx) +{ + struct MprVar mpv = mprObject("param"); + mprSetPtrChild(&mpv, "param", ctx); + param_add_members(&mpv); + return mpv; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_param(void) +{ + ejsDefineCFunction(-1, "param_init", ejs_param_init, NULL, MPR_VAR_SCRIPT_HANDLE); +} diff --git a/source4/scripting/ejs/smbcalls_samba3.c b/source4/scripting/ejs/smbcalls_samba3.c index e8e6b416e2..0d54d54dcc 100644 --- a/source4/scripting/ejs/smbcalls_samba3.c +++ b/source4/scripting/ejs/smbcalls_samba3.c @@ -403,6 +403,8 @@ static int ejs_find_domainsecrets(MprVarHandle eid, int argc, struct MprVar **ar /* initialise samba3 ejs subsystem + + samba3 = samba3_read(libdir,smbconf) */ static int ejs_samba3_read(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -415,7 +417,7 @@ static int ejs_samba3_read(MprVarHandle eid, int argc, struct MprVar **argv) return -1; } - status = samba3_read(mprToString(argv[0]), mprToString(argv[0]), mprMemCtx(), &samba3); + status = samba3_read(mprToString(argv[0]), mprToString(argv[1]), mprMemCtx(), &samba3); if (NT_STATUS_IS_ERR(status)) { ejsSetErrorMsg(eid, "samba3_read: error"); @@ -434,6 +436,7 @@ static int ejs_samba3_read(MprVarHandle eid, int argc, struct MprVar **argv) mprSetVar(&mpv, "idmapdb", mprIdmapDb(&samba3->idmap)); mprSetVar(&mpv, "policy", mprPolicy(&samba3->policy)); mprSetVar(&mpv, "registry", mprRegistry(&samba3->registry)); + mprSetVar(&mpv, "configuration", mprParam(samba3->configuration)); mprSetCFunction(&mpv, "find_domainsecrets", ejs_find_domainsecrets); mpr_Return(eid, mpv); diff --git a/source4/scripting/libjs/upgrade.js b/source4/scripting/libjs/upgrade.js index c6a65e01c0..aa94dbbbc4 100644 --- a/source4/scripting/libjs/upgrade.js +++ b/source4/scripting/libjs/upgrade.js @@ -210,8 +210,8 @@ function upgrade_provision(samba3) var lp = loadparm_init(); var rdn_list; - var domainname = samba3.get_param("global", "workgroup"); - + var domainname = samba3.configuration.get("workgroup"); + if (domainname == undefined) { domainname = samba3.secrets.domains[0].name; println("No domain specified in smb.conf file, assuming '" + domainname + "'"); @@ -219,7 +219,7 @@ function upgrade_provision(samba3) var domsec = samba3.find_domainsecrets(domainname); var hostsec = samba3.find_domainsecrets(hostname()); - var realm = samba3.get_param("global", "realm"); + var realm = samba3.configuration.get("realm"); if (realm == undefined) { realm = domainname; -- cgit