a76690b597871cb70db26f8f7390bdb300024ba1
[ira/wip.git] / source4 / scripting / ejs / smbcalls_sys.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide access to system 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/ejs/ejs.h"
26
27 /*
28   return the list of configured network interfaces
29 */
30 static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
31 {
32         int i, count = iface_count();
33         struct MprVar ret = mprObject("interfaces");
34         for (i=0;i<count;i++) {
35                 mprAddArray(&ret, i, mprString(iface_n_ip(i)));
36         }
37         mpr_Return(eid, ret);
38         return 0;       
39 }
40
41 /*
42   return the hostname from gethostname()
43 */
44 static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
45 {
46         char name[200];
47         if (gethostname(name, sizeof(name)-1) == -1) {
48                 ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno));
49                 return -1;
50         }
51         mpr_Return(eid, mprString(name));
52         return 0;       
53 }
54
55
56 /*
57   return current time as a 64 bit nttime value
58 */
59 static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
60 {
61         struct timeval tv = timeval_current();
62         struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv));
63         mpr_Return(eid, v);
64         return 0;
65 }
66
67 /*
68   return a ldap time string from a nttime
69 */
70 static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
71 {
72         char *s;
73         time_t t;
74         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
75                 ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments");
76                 return -1;
77         }
78         t = nt_time_to_unix(mprVarToNumber(argv[0]));
79         s = ldap_timestring(mprMemCtx(), t);
80         mpr_Return(eid, mprString(s));
81         talloc_free(s);
82         return 0;
83 }
84
85
86 /*
87   setup C functions that be called from ejs
88 */
89 void smb_setup_ejs_system(void)
90 {
91         ejsDefineCFunction(-1, "sys_interfaces", ejs_sys_interfaces, NULL, MPR_VAR_SCRIPT_HANDLE);
92         ejsDefineCFunction(-1, "sys_hostname", ejs_sys_hostname, NULL, MPR_VAR_SCRIPT_HANDLE);
93         ejsDefineCFunction(-1, "sys_nttime", ejs_sys_nttime, NULL, MPR_VAR_SCRIPT_HANDLE);
94         ejsDefineCFunction(-1, "sys_ldaptime", ejs_sys_ldaptime, NULL, MPR_VAR_SCRIPT_HANDLE);
95 }