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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "system/passwd.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
33 static int ejs_random(MprVarHandle eid, int argc, struct MprVar **argv)
35 mpr_Return(eid, mprCreateIntegerVar(generate_random()));
41 var s = randpass(len);
43 static int ejs_randpass(MprVarHandle eid, int argc, struct MprVar **argv)
46 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
47 ejsSetErrorMsg(eid, "randpass invalid arguments");
50 s = generate_random_str(mprMemCtx(), mprToInt(argv[0]));
51 mpr_Return(eid, mprString(s));
58 var guid = randguid();
60 static int ejs_randguid(MprVarHandle eid, int argc, struct MprVar **argv)
62 struct GUID guid = GUID_random();
63 char *s = GUID_string(mprMemCtx(), &guid);
64 mpr_Return(eid, mprString(s));
73 static int ejs_randsid(MprVarHandle eid, int argc, struct MprVar **argv)
75 char *s = talloc_asprintf(mprMemCtx(), "S-1-5-21-%8u-%8u-%8u",
76 (unsigned)generate_random(),
77 (unsigned)generate_random(),
78 (unsigned)generate_random());
79 mpr_Return(eid, mprString(s));
85 initialise random ejs subsystem
87 static int ejs_random_init(MprVarHandle eid, int argc, struct MprVar **argv)
89 struct MprVar *obj = mprInitObject(eid, "random", argc, argv);
91 mprSetCFunction(obj, "random", ejs_random);
92 mprSetCFunction(obj, "randpass", ejs_randpass);
93 mprSetCFunction(obj, "randguid", ejs_randguid);
94 mprSetCFunction(obj, "randsid", ejs_randsid);
99 setup C functions that be called from ejs
101 void smb_setup_ejs_random(void)
103 ejsDefineCFunction(-1, "random_init", ejs_random_init, NULL, MPR_VAR_SCRIPT_HANDLE);