2 Unix SMB/CIFS implementation.
4 provide access to randomisation functions
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "librpc/gen_ndr/ndr_misc.h"
31 static int ejs_random(MprVarHandle eid, int argc, struct MprVar **argv)
33 mpr_Return(eid, mprCreateIntegerVar(generate_random()));
39 var s = randpass(len);
41 static int ejs_randpass(MprVarHandle eid, int argc, struct MprVar **argv)
44 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
45 ejsSetErrorMsg(eid, "randpass invalid arguments");
48 s = generate_random_str(mprMemCtx(), mprToInt(argv[0]));
49 mpr_Return(eid, mprString(s));
56 var guid = randguid();
58 static int ejs_randguid(MprVarHandle eid, int argc, struct MprVar **argv)
60 struct GUID guid = GUID_random();
61 char *s = GUID_string(mprMemCtx(), &guid);
62 mpr_Return(eid, mprString(s));
71 static int ejs_randsid(MprVarHandle eid, int argc, struct MprVar **argv)
73 char *s = talloc_asprintf(mprMemCtx(), "S-1-5-21-%u-%u-%u",
74 (unsigned)generate_random(),
75 (unsigned)generate_random(),
76 (unsigned)generate_random());
77 mpr_Return(eid, mprString(s));
83 initialise random ejs subsystem
85 static int ejs_random_init(MprVarHandle eid, int argc, struct MprVar **argv)
87 struct MprVar *obj = mprInitObject(eid, "random", argc, argv);
89 mprSetCFunction(obj, "random", ejs_random);
90 mprSetCFunction(obj, "randpass", ejs_randpass);
91 mprSetCFunction(obj, "randguid", ejs_randguid);
92 mprSetCFunction(obj, "randsid", ejs_randsid);
97 setup C functions that be called from ejs
99 NTSTATUS smb_setup_ejs_random(void)
101 ejsDefineCFunction(-1, "random_init", ejs_random_init, NULL, MPR_VAR_SCRIPT_HANDLE);