a2db1f0d6d8c040954dca5e594c541fe2d8d1205
[tprouty/samba.git] / source4 / scripting / ejs / smbcalls_rand.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide access to randomisation functions
5
6    Copyright (C) Andrew Tridgell 2005
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 "librpc/gen_ndr/ndr_misc.h"
26
27 /*
28   usage:
29       var i = random();
30 */
31 static int ejs_random(MprVarHandle eid, int argc, struct MprVar **argv)
32 {
33         mpr_Return(eid, mprCreateIntegerVar(generate_random()));
34         return 0;
35 }
36
37 /*
38   usage:
39       var s = randpass(len);
40 */
41 static int ejs_randpass(MprVarHandle eid, int argc, struct MprVar **argv)
42 {
43         char *s;
44         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
45                 ejsSetErrorMsg(eid, "randpass invalid arguments");
46                 return -1;
47         }
48         s = generate_random_str(mprMemCtx(), mprToInt(argv[0]));
49         mpr_Return(eid, mprString(s));
50         talloc_free(s);
51         return 0;
52 }
53
54 /*
55   usage:
56       var guid = randguid();
57 */
58 static int ejs_randguid(MprVarHandle eid, int argc, struct MprVar **argv)
59 {
60         struct GUID guid = GUID_random();
61         char *s = GUID_string(mprMemCtx(), &guid);
62         mpr_Return(eid, mprString(s));
63         talloc_free(s);
64         return 0;
65 }
66
67 /*
68   usage:
69       var sid = randsid();
70 */
71 static int ejs_randsid(MprVarHandle eid, int argc, struct MprVar **argv)
72 {
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));
78         talloc_free(s);
79         return 0;
80 }
81
82 /*
83   initialise random ejs subsystem
84 */
85 static int ejs_random_init(MprVarHandle eid, int argc, struct MprVar **argv)
86 {
87         struct MprVar *obj = mprInitObject(eid, "random", argc, argv);
88
89         mprSetCFunction(obj, "random", ejs_random);
90         mprSetCFunction(obj, "randpass", ejs_randpass);
91         mprSetCFunction(obj, "randguid", ejs_randguid);
92         mprSetCFunction(obj, "randsid", ejs_randsid);
93         return 0;
94 }
95
96 /*
97   setup C functions that be called from ejs
98 */
99 NTSTATUS smb_setup_ejs_random(void)
100 {
101         ejsDefineCFunction(-1, "random_init", ejs_random_init, NULL, MPR_VAR_SCRIPT_HANDLE);
102         return NT_STATUS_OK;
103 }