Introduce mprLpCtx() similar to mprMemCtx() for loadparm_context used by
[jelmer/samba4-debian.git] / source / scripting / ejs / smbcalls_reg.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Jelmer Vernooij 2007
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "librpc/ndr/libndr.h"
27 #include "lib/registry/registry.h"
28
29 /*
30   get the connected db
31  */
32 static struct registry_context *ejs_get_reg_context(int eid)
33 {
34         struct registry_context *rctx = (struct registry_context *)mprGetThisPtr(eid, "registry");
35         if (rctx == NULL) {
36                 ejsSetErrorMsg(eid, "unable to find registry");
37         }
38         return rctx;
39 }
40
41 static int ejs_apply_patchfile(MprVarHandle eid, int argc, struct MprVar **argv)
42 {
43         struct registry_context *rctx;
44         WERROR error;
45
46         /* validate arguments */
47         if (argc != 1) {
48                 ejsSetErrorMsg(eid, "reg.apply_patchfile invalid number of arguments");
49                 return -1;
50         }
51
52         rctx = ejs_get_reg_context(eid);
53         if (rctx == NULL) {
54                 return -1;
55         }
56         
57         error = reg_diff_apply(rctx, mprToString(argv[0]));
58
59         mpr_Return(eid, mprWERROR(error));
60
61         return 0;
62 }
63
64 /*
65   initialise registry ejs subsystem
66 */
67 static int ejs_reg_open(MprVarHandle eid, int argc, struct MprVar **argv)
68 {
69         struct MprVar *reg = mprInitObject(eid, "registry", argc, argv);
70         struct registry_context *rctx;
71         WERROR error;
72
73         error = reg_open_samba(mprMemCtx(), &rctx, mprLpCtx(), NULL, NULL);
74         SMB_ASSERT(W_ERROR_IS_OK(error));
75
76         mprSetPtrChild(reg, "registry", rctx);
77         mprSetCFunction(reg, "apply_patchfile", ejs_apply_patchfile);
78
79         return 0;
80 }
81
82
83 /*
84   setup C functions that be called from ejs
85 */
86 NTSTATUS smb_setup_ejs_reg(void)
87 {
88         ejsDefineCFunction(-1, "reg_open", ejs_reg_open, NULL, MPR_VAR_SCRIPT_HANDLE);
89         return NT_STATUS_OK;
90 }