r14542: Remove librpc, libndr and libnbt from includes.h
[samba.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
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"
28
29 /*
30   usage:
31       var i = random();
32 */
33 static int ejs_random(MprVarHandle eid, int argc, struct MprVar **argv)
34 {
35         mpr_Return(eid, mprCreateIntegerVar(generate_random()));
36         return 0;
37 }
38
39 /*
40   usage:
41       var s = randpass(len);
42 */
43 static int ejs_randpass(MprVarHandle eid, int argc, struct MprVar **argv)
44 {
45         char *s;
46         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
47                 ejsSetErrorMsg(eid, "randpass invalid arguments");
48                 return -1;
49         }
50         s = generate_random_str(mprMemCtx(), mprToInt(argv[0]));
51         mpr_Return(eid, mprString(s));
52         talloc_free(s);
53         return 0;
54 }
55
56 /*
57   usage:
58       var guid = randguid();
59 */
60 static int ejs_randguid(MprVarHandle eid, int argc, struct MprVar **argv)
61 {
62         struct GUID guid = GUID_random();
63         char *s = GUID_string(mprMemCtx(), &guid);
64         mpr_Return(eid, mprString(s));
65         talloc_free(s);
66         return 0;
67 }
68
69 /*
70   usage:
71       var sid = randsid();
72 */
73 static int ejs_randsid(MprVarHandle eid, int argc, struct MprVar **argv)
74 {
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));
80         talloc_free(s);
81         return 0;
82 }
83
84 /*
85   initialise random ejs subsystem
86 */
87 static int ejs_random_init(MprVarHandle eid, int argc, struct MprVar **argv)
88 {
89         struct MprVar *obj = mprInitObject(eid, "random", argc, argv);
90
91         mprSetCFunction(obj, "random", ejs_random);
92         mprSetCFunction(obj, "randpass", ejs_randpass);
93         mprSetCFunction(obj, "randguid", ejs_randguid);
94         mprSetCFunction(obj, "randsid", ejs_randsid);
95         return 0;
96 }
97
98 /*
99   setup C functions that be called from ejs
100 */
101 void smb_setup_ejs_random(void)
102 {
103         ejsDefineCFunction(-1, "random_init", ejs_random_init, NULL, MPR_VAR_SCRIPT_HANDLE);
104 }