summaryrefslogtreecommitdiff
path: root/source4/scripting/ejs/smbcalls_sys.c
blob: f098f884bba2a2f99a310ce8ac1d0b3b9991ff50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* 
   Unix SMB/CIFS implementation.

   provide access to system functions

   Copyright (C) Andrew Tridgell 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "scripting/ejs/smbcalls.h"
#include "lib/ejs/ejs.h"
#include "system/time.h"

/*
  return the list of configured network interfaces
*/
static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
{
	int i, count = iface_count();
	struct MprVar ret = mprObject("interfaces");
	for (i=0;i<count;i++) {
		mprAddArray(&ret, i, mprString(iface_n_ip(i)));
	}
	mpr_Return(eid, ret);
	return 0;	
}

/*
  return the hostname from gethostname()
*/
static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
{
	char name[200];
	if (gethostname(name, sizeof(name)-1) == -1) {
		ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno));
		return -1;
	}
	mpr_Return(eid, mprString(name));
	return 0;	
}


/*
  return current time as a 64 bit nttime value
*/
static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct timeval tv = timeval_current();
	struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv));
	mpr_Return(eid, v);
	return 0;
}

/*
  return the given time as a gmtime structure
*/
static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
{
	time_t t;
	struct MprVar ret;
	struct tm *tm;
	if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
		ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
		return -1;
	}
	t = nt_time_to_unix(mprVarToNumber(argv[0]));
	tm = gmtime(&t);
	if (tm == NULL) {
		mpr_Return(eid, mprCreateUndefinedVar());
		return 0;
	}
	ret = mprObject("gmtime");
#define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
	TM_EL(tm_sec);
	TM_EL(tm_min);
	TM_EL(tm_hour);
	TM_EL(tm_mday);
	TM_EL(tm_mon);
	TM_EL(tm_year);
	TM_EL(tm_wday);
	TM_EL(tm_yday);
	TM_EL(tm_isdst);

	mpr_Return(eid, ret);
	return 0;
}

/*
  return a ldap time string from a nttime
*/
static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
{
	char *s;
	time_t t;
	if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
		ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments");
		return -1;
	}
	t = nt_time_to_unix(mprVarToNumber(argv[0]));
	s = ldap_timestring(mprMemCtx(), t);
	mpr_Return(eid, mprString(s));
	talloc_free(s);
	return 0;
}

/*
  unlink a file
   ok = unlink(fname);
*/
static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
{
	int ret;
	if (argc != 1) {
		ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
		return -1;
	}
	ret = unlink(argv[0]);
	mpr_Return(eid, mprCreateBoolVar(ret == 0));
	return 0;
}

/*
  load a file as a string
  usage:
     string = sys_file_load(filename);
*/
static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
{
	char *s;
	if (argc != 1) {
		ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
		return -1;
	}

	s = file_load(argv[0], NULL, mprMemCtx());
	mpr_Return(eid, mprString(s));
	talloc_free(s);
	return 0;
}

/*
  save a file from a string
  usage:
     ok = sys_file_save(filename, str);
*/
static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
{
	BOOL ret;
	if (argc != 2) {
		ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
		return -1;
	}
	ret = file_save(argv[0], argv[1], strlen(argv[1]));
	mpr_Return(eid, mprCreateBoolVar(ret));
	return 0;
}


/*
  setup C functions that be called from ejs
*/
void smb_setup_ejs_system(void)
{
	ejsDefineCFunction(-1, "sys_interfaces", ejs_sys_interfaces, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineCFunction(-1, "sys_hostname", ejs_sys_hostname, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineCFunction(-1, "sys_nttime", ejs_sys_nttime, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineCFunction(-1, "sys_gmtime", ejs_sys_gmtime, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineCFunction(-1, "sys_ldaptime", ejs_sys_ldaptime, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineStringCFunction(-1, "sys_unlink", ejs_sys_unlink, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineStringCFunction(-1, "sys_file_load", ejs_sys_file_load, NULL, MPR_VAR_SCRIPT_HANDLE);
	ejsDefineStringCFunction(-1, "sys_file_save", ejs_sys_file_save, NULL, MPR_VAR_SCRIPT_HANDLE);
}