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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/*
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/appweb/ejs/ejs.h"
#include "lib/ldb/include/ldb.h"
#include "system/time.h"
#include "system/network.h"
#include "lib/socket/netif.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 = mprArray("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 seconds and microseconds
*/
static int ejs_sys_gettimeofday(MprVarHandle eid, int argc, struct MprVar **argv)
{
struct timeval tv = timeval_current();
struct MprVar v = mprObject("timeval");
struct MprVar sec = mprCreateIntegerVar(tv.tv_sec);
struct MprVar usec = mprCreateIntegerVar(tv.tv_usec);
mprCreateProperty(&v, "sec", &sec);
mprCreateProperty(&v, "usec", &usec);
mpr_Return(eid, v);
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 time as a 64 bit nttime value from a 32 bit time_t value
*/
static int ejs_sys_unix2nttime(MprVarHandle eid, int argc, struct MprVar **argv)
{
NTTIME nt;
struct MprVar v;
if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
ejsSetErrorMsg(eid, "sys_unix2nttime invalid arguments");
return -1;
}
unix_to_nt_time(&nt, mprVarToNumber(argv[0]));
v = mprCreateNumberVar(nt);
mpr_Return(eid, v);
return 0;
}
/*
return the GMT time represented by the struct tm argument, as a time_t value
*/
static int ejs_sys_gmmktime(MprVarHandle eid, int argc, struct MprVar **argv)
{
struct MprVar *o;
struct tm tm;
if (argc != 1 || !mprVarIsObject(argv[0]->type)) {
ejsSetErrorMsg(eid, "sys_gmmktime invalid arguments");
return -1;
}
o = argv[0];
#define TM_EL(n) tm.n = mprVarToNumber(mprGetProperty(o, #n, NULL))
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);
#undef TM_EL
mpr_Return(eid, mprCreateIntegerVar(mktime(&tm)));
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 = (time_t) 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);
#undef TM_EL
mpr_Return(eid, ret);
return 0;
}
/*
return the given NT time as a time_t value
*/
static int ejs_sys_nttime2unix(MprVarHandle eid, int argc, struct MprVar **argv)
{
time_t t;
struct MprVar v;
if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments");
return -1;
}
t = nt_time_to_unix(mprVarToNumber(argv[0]));
v = mprCreateNumberVar(t);
mpr_Return(eid, v);
return 0;
}
/*
return the given NT time as a gmtime structure
*/
static int ejs_sys_ntgmtime(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_ntgmtime 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);
#undef TM_EL
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 = ldb_timestring(mprMemCtx(), t);
mpr_Return(eid, mprString(s));
talloc_free(s);
return 0;
}
/*
return a http time string from a nttime
*/
static int ejs_sys_httptime(MprVarHandle eid, int argc, struct MprVar **argv)
{
char *s;
time_t t;
if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
ejsSetErrorMsg(eid, "sys_httptime invalid arguments");
return -1;
}
t = nt_time_to_unix(mprVarToNumber(argv[0]));
s = http_timestring(mprMemCtx(), t);
mpr_Return(eid, mprString(s));
talloc_free(s);
return 0;
}
/*
unlink a file
ok = sys.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;
}
/*
mkdir()
usage:
ok = sys.mkdir(dirname, mode);
*/
static int ejs_sys_mkdir(MprVarHandle eid, int argc, struct MprVar **argv)
{
BOOL ret;
char *name;
if (argc != 2) {
ejsSetErrorMsg(eid, "sys_mkdir invalid arguments, need mkdir(dirname, mode)");
return -1;
}
if (!mprVarIsString(argv[0]->type)) {
ejsSetErrorMsg(eid, "sys_mkdir dirname not a string");
return -1;
}
if (!mprVarIsNumber(argv[1]->type)) {
ejsSetErrorMsg(eid, "sys_mkdir mode not a number");
return -1;
}
mprVarToString(&name, 0, NULL, argv[0]);
ret = mkdir(name, mprVarToNumber(argv[1]));
mpr_Return(eid, mprCreateBoolVar(ret == 0));
return 0;
}
/*
return fields of a stat() call
*/
static struct MprVar mpr_stat(struct stat *st)
{
struct MprVar ret;
ret = mprObject("stat");
#define ST_EL(n) mprSetVar(&ret, #n, mprCreateNumberVar(st->n))
ST_EL(st_dev);
ST_EL(st_ino);
ST_EL(st_mode);
ST_EL(st_nlink);
ST_EL(st_uid);
ST_EL(st_gid);
ST_EL(st_rdev);
ST_EL(st_size);
ST_EL(st_blksize);
ST_EL(st_blocks);
ST_EL(st_atime);
ST_EL(st_mtime);
ST_EL(st_ctime);
return ret;
}
/*
usage:
var st = sys.stat(filename);
returns an object containing struct stat elements
*/
static int ejs_sys_stat(MprVarHandle eid, int argc, char **argv)
{
struct stat st;
/* validate arguments */
if (argc != 1) {
ejsSetErrorMsg(eid, "sys.stat invalid arguments");
return -1;
}
if (stat(argv[0], &st) != 0) {
mpr_Return(eid, mprCreateUndefinedVar());
} else {
mpr_Return(eid, mpr_stat(&st));
}
return 0;
}
/*
usage:
var st = sys.lstat(filename);
returns an object containing struct stat elements
*/
static int ejs_sys_lstat(MprVarHandle eid, int argc, char **argv)
{
struct stat st;
/* validate arguments */
if (argc != 1) {
ejsSetErrorMsg(eid, "sys.stat invalid arguments");
return -1;
}
if (lstat(argv[0], &st) != 0) {
mpr_Return(eid, mprCreateUndefinedVar());
} else {
mpr_Return(eid, mpr_stat(&st));
}
return 0;
}
/*
bitwise AND
usage:
var z = sys.bitAND(x, 0x70);
*/
static int ejs_sys_bitAND(MprVarHandle eid, int argc, struct MprVar **argv)
{
int x, y, z;
if (argc != 2 ||
!mprVarIsNumber(argv[0]->type) ||
!mprVarIsNumber(argv[1]->type)) {
ejsSetErrorMsg(eid, "bitand invalid arguments");
return -1;
}
x = mprToInt(argv[0]);
y = mprToInt(argv[1]);
z = x & y;
mpr_Return(eid, mprCreateIntegerVar(z));
return 0;
}
/*
bitwise OR
usage:
var z = sys.bitOR(x, 0x70);
*/
static int ejs_sys_bitOR(MprVarHandle eid, int argc, struct MprVar **argv)
{
int x, y, z;
if (argc != 2 ||
!mprVarIsNumber(argv[0]->type) ||
!mprVarIsNumber(argv[1]->type)) {
ejsSetErrorMsg(eid, "bitand invalid arguments");
return -1;
}
x = mprToInt(argv[0]);
y = mprToInt(argv[1]);
z = x | y;
mpr_Return(eid, mprCreateIntegerVar(z));
return 0;
}
/*
initialise sys ejs subsystem
*/
static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
{
struct MprVar *obj = mprInitObject(eid, "sys", argc, argv);
mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
mprSetCFunction(obj, "hostname", ejs_sys_hostname);
mprSetCFunction(obj, "nttime", ejs_sys_nttime);
mprSetCFunction(obj, "gettimeofday", ejs_sys_gettimeofday);
mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime);
mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
mprSetCFunction(obj, "nttime2unix", ejs_sys_nttime2unix);
mprSetCFunction(obj, "ntgmtime", ejs_sys_ntgmtime);
mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime);
mprSetCFunction(obj, "httptime", ejs_sys_httptime);
mprSetCFunction(obj, "mkdir", ejs_sys_mkdir);
mprSetStringCFunction(obj, "unlink", ejs_sys_unlink);
mprSetStringCFunction(obj, "file_load", ejs_sys_file_load);
mprSetStringCFunction(obj, "file_save", ejs_sys_file_save);
mprSetStringCFunction(obj, "stat", ejs_sys_stat);
mprSetStringCFunction(obj, "lstat", ejs_sys_lstat);
mprSetCFunction(obj, "bitAND", ejs_sys_bitAND);
mprSetCFunction(obj, "bitOR", ejs_sys_bitOR);
return 0;
}
/*
setup C functions that be called from ejs
*/
NTSTATUS smb_setup_ejs_system(void)
{
ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE);
return NT_STATUS_OK;
}
|