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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
/*
Unix SMB/CIFS implementation.
provide hooks into smbd C calls from ejs scripts
Copyright (C) Tim Potter 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "scripting/ejs/smbcalls.h"
#include "lib/appweb/ejs/ejs.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/composite/composite.h"
#include "libcli/smb_composite/smb_composite.h"
#include "libcli/libcli.h"
#include "auth/credentials/credentials.h"
#include "param/param.h"
#if 0
#include "librpc/gen_ndr/ndr_nbt.h"
/* Connect to a server */
static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv)
{
struct smbcli_socket *sock;
struct smbcli_transport *transport;
struct nbt_name calling, called;
NTSTATUS result;
if (argc != 1) {
ejsSetErrorMsg(eid, "connect invalid arguments");
return -1;
}
/* Socket connect */
sock = smbcli_sock_init(NULL, NULL);
if (!sock) {
ejsSetErrorMsg(eid, "socket initialisation failed");
return -1;
}
if (!smbcli_sock_connect_byname(sock, argv[0], 0)) {
ejsSetErrorMsg(eid, "socket connect failed");
return -1;
}
transport = smbcli_transport_init(sock, sock, False);
if (!transport) {
ejsSetErrorMsg(eid, "transport init failed");
return -1;
}
/* Send a netbios session request */
make_nbt_name_client(&calling, lp_netbios_name());
nbt_choose_called_name(NULL, &called, argv[0], NBT_NAME_SERVER);
if (!smbcli_transport_connect(transport, &calling, &called)) {
ejsSetErrorMsg(eid, "transport establishment failed");
return -1;
}
result = smb_raw_negotiate(transport, lp_maxprotocol());
if (!NT_STATUS_IS_OK(result)) {
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
/* Return a socket object */
mpr_Return(eid, mprCreatePtrVar(transport));
return 0;
}
/* Perform a session setup:
session_setup(conn, "DOMAIN\\USERNAME%PASSWORD");
session_setup(conn, USERNAME, PASSWORD);
session_setup(conn, DOMAIN, USERNAME, PASSWORD);
session_setup(conn); // anonymous
*/
static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_transport *transport;
struct smbcli_session *session;
struct smb_composite_sesssetup setup;
struct cli_credentials *creds;
NTSTATUS status;
int result = -1;
/* Argument parsing */
if (argc < 1 || argc > 4) {
ejsSetErrorMsg(eid, "session_setup invalid arguments");
return -1;
}
if (!mprVarIsPtr(argv[0]->type)) {
ejsSetErrorMsg(eid, "first arg is not a connect handle");
return -1;
}
transport = argv[0]->ptr;
creds = cli_credentials_init(transport);
cli_credentials_set_conf(creds);
if (argc == 4) {
/* DOMAIN, USERNAME, PASSWORD form */
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg 1 must be a string");
goto done;
}
cli_credentials_set_domain(creds, argv[1]->string,
CRED_SPECIFIED);
if (!mprVarIsString(argv[2]->type)) {
ejsSetErrorMsg(eid, "arg 2 must be a string");
goto done;
}
cli_credentials_set_username(creds, argv[2]->string,
CRED_SPECIFIED);
if (!mprVarIsString(argv[3]->type)) {
ejsSetErrorMsg(eid, "arg 3 must be a string");
goto done;
}
cli_credentials_set_password(creds, argv[3]->string,
CRED_SPECIFIED);
} else if (argc == 3) {
/* USERNAME, PASSWORD form */
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg1 must be a string");
goto done;
}
cli_credentials_set_username(creds, argv[1]->string,
CRED_SPECIFIED);
if (!mprVarIsString(argv[2]->type)) {
ejsSetErrorMsg(eid, "arg2 must be a string");
goto done;
}
cli_credentials_set_password(creds, argv[2]->string,
CRED_SPECIFIED);
} else if (argc == 2) {
/* DOMAIN/USERNAME%PASSWORD form */
cli_credentials_parse_string(creds, argv[1]->string,
CRED_SPECIFIED);
} else {
/* Anonymous connection */
cli_credentials_set_anonymous(creds);
}
/* Do session setup */
session = smbcli_session_init(transport, transport, False);
if (!session) {
ejsSetErrorMsg(eid, "session init failed");
return -1;
}
setup.in.sesskey = transport->negotiate.sesskey;
setup.in.capabilities = transport->negotiate.capabilities;
setup.in.credentials = creds;
setup.in.workgroup = lp_workgroup();
status = smb_composite_sesssetup(session, &setup);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "session_setup: %s", nt_errstr(status));
return -1;
}
session->vuid = setup.out.vuid;
/* Return a session object */
mpr_Return(eid, mprCreatePtrVar(session));
result = 0;
done:
talloc_free(creds);
return result;
}
/* Perform a tree connect
tree_connect(session, SHARE);
*/
static int ejs_cli_tree_connect(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_session *session;
struct smbcli_tree *tree;
union smb_tcon tcon;
TALLOC_CTX *mem_ctx;
NTSTATUS status;
const char *password = "";
/* Argument parsing */
if (argc != 2) {
ejsSetErrorMsg(eid, "tree_connect invalid arguments");
return -1;
}
if (!mprVarIsPtr(argv[0]->type)) {
ejsSetErrorMsg(eid, "first arg is not a session handle");
return -1;
}
session = argv[0]->ptr;
tree = smbcli_tree_init(session, session, False);
if (!tree) {
ejsSetErrorMsg(eid, "tree init failed");
return -1;
}
mem_ctx = talloc_init("tcon");
if (!mem_ctx) {
ejsSetErrorMsg(eid, "talloc_init failed");
return -1;
}
/* Do tree connect */
tcon.generic.level = RAW_TCON_TCONX;
tcon.tconx.in.flags = 0;
if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
tcon.tconx.in.password = data_blob(NULL, 0);
} else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
if (session->transport->negotiate.secblob.length < 8) {
ejsSetErrorMsg(eid, "invalid security blob");
return -1;
}
SMBencrypt(password, session->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
} else {
tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
}
tcon.tconx.in.path = argv[1]->string;
tcon.tconx.in.device = "?????";
status = smb_tree_connect(tree, mem_ctx, &tcon);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "tree_connect: %s", nt_errstr(status));
return -1;
}
tree->tid = tcon.tconx.out.tid;
talloc_free(mem_ctx);
mpr_Return(eid, mprCreatePtrVar(tree));
return 0;
}
/* Perform a tree disconnect
tree_disconnect(tree);
*/
static int ejs_cli_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
NTSTATUS status;
/* Argument parsing */
if (argc != 1) {
ejsSetErrorMsg(eid, "tree_disconnect invalid arguments");
return -1;
}
if (!mprVarIsPtr(argv[0]->type)) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = argv[0]->ptr;
status = smb_tree_disconnect(tree);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "tree_disconnect: %s", nt_errstr(status));
return -1;
}
talloc_free(tree);
return 0;
}
/* Perform a ulogoff
session_logoff(session);
*/
static int ejs_cli_session_logoff(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_session *session;
NTSTATUS status;
/* Argument parsing */
if (argc != 1) {
ejsSetErrorMsg(eid, "session_logoff invalid arguments");
return -1;
}
if (!mprVarIsPtr(argv[0]->type)) {
ejsSetErrorMsg(eid, "first arg is not a session handle");
return -1;
}
session = argv[0]->ptr;
status = smb_raw_ulogoff(session);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "session_logoff: %s", nt_errstr(status));
return -1;
}
talloc_free(session);
return 0;
}
/* Perform a connection close
disconnect(conn);
*/
static int ejs_cli_disconnect(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_sock *sock;
/* Argument parsing */
if (argc != 1) {
ejsSetErrorMsg(eid, "disconnect invalid arguments");
return -1;
}
if (!mprVarIsPtr(argv[0]->type)) {
ejsSetErrorMsg(eid, "first arg is not a connect handle");
return -1;
}
sock = argv[0]->ptr;
talloc_free(sock);
return 0;
}
#endif
/* Perform a tree connect:
tree_handle = tree_connect("\\\\frogurt\\homes", "user%pass");
*/
static int ejs_tree_connect(MprVarHandle eid, int argc, char **argv)
{
struct cli_credentials *creds;
struct smb_composite_connect io;
struct smbcli_tree *tree;
char *hostname, *sharename;
NTSTATUS result;
TALLOC_CTX *mem_ctx;
if (argc != 2) {
ejsSetErrorMsg(eid, "tree_connect(): invalid number of args");
return -1;
}
/* Set up host, share destination */
mem_ctx = talloc_new(mprMemCtx());
smbcli_parse_unc(argv[0], mem_ctx, &hostname, &sharename);
/* Set up credentials */
creds = cli_credentials_init(NULL);
cli_credentials_set_conf(creds);
cli_credentials_parse_string(creds, argv[1], CRED_SPECIFIED);
/* Do connect */
io.in.dest_host = hostname;
io.in.port = 0;
io.in.called_name = strupper_talloc(mem_ctx, hostname);
io.in.service = sharename;
io.in.service_type = "?????";
io.in.credentials = creds;
io.in.fallback_to_anonymous = False;
io.in.workgroup = lp_workgroup();
result = smb_composite_connect(&io, mem_ctx, NULL);
tree = io.out.tree;
talloc_free(mem_ctx);
if (!NT_STATUS_IS_OK(result)) {
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
mpr_Return(eid, mprCreatePtrVar(tree));
return 0;
}
#define IS_TREE_HANDLE(x) (mprVarIsPtr((x)->type) && \
talloc_check_name((x)->ptr, "struct smbcli_tree"))
/* Perform a tree disconnect:
tree_disconnect(tree_handle);
*/
static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
NTSTATUS result;
if (argc != 1) {
ejsSetErrorMsg(eid,
"tree_disconnect(): invalid number of args");
return -1;
}
if (!IS_TREE_HANDLE(argv[0])) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = talloc_get_type(argv[0]->ptr, struct smbcli_tree);
result = smb_tree_disconnect(tree);
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
/* Create a directory:
result = mkdir(tree_handle, DIRNAME);
*/
static int ejs_mkdir(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
NTSTATUS result;
if (argc != 2) {
ejsSetErrorMsg(eid, "mkdir(): invalid number of args");
return -1;
}
if (!IS_TREE_HANDLE(argv[0])) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = (struct smbcli_tree *)argv[0]->ptr;
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg 2 must be a string");
return -1;
}
result = smbcli_mkdir(tree, argv[1]->string);
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
/* Remove a directory:
result = rmdir(tree_handle, DIRNAME);
*/
static int ejs_rmdir(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
NTSTATUS result;
if (argc != 2) {
ejsSetErrorMsg(eid, "rmdir(): invalid number of args");
return -1;
}
if (!IS_TREE_HANDLE(argv[0])) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = (struct smbcli_tree *)argv[0]->ptr;
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg 2 must be a string");
return -1;
}
result = smbcli_rmdir(tree, argv[1]->string);
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
/* Rename a file or directory:
result = rename(tree_handle, SRCFILE, DESTFILE);
*/
static int ejs_rename(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
NTSTATUS result;
if (argc != 3) {
ejsSetErrorMsg(eid, "rename(): invalid number of args");
return -1;
}
if (!IS_TREE_HANDLE(argv[0])) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = (struct smbcli_tree *)argv[0]->ptr;
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg 2 must be a string");
return -1;
}
if (!mprVarIsString(argv[2]->type)) {
ejsSetErrorMsg(eid, "arg 3 must be a string");
return -1;
}
result = smbcli_rename(tree, argv[1]->string, argv[2]->string);
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
/* Unlink a file or directory:
result = unlink(tree_handle, FILENAME);
*/
static int ejs_unlink(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
NTSTATUS result;
if (argc != 2) {
ejsSetErrorMsg(eid, "unlink(): invalid number of args");
return -1;
}
if (!IS_TREE_HANDLE(argv[0])) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = (struct smbcli_tree *)argv[0]->ptr;
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg 2 must be a string");
return -1;
}
result = smbcli_unlink(tree, argv[1]->string);
mpr_Return(eid, mprNTSTATUS(result));
return 0;
}
/* List directory contents
result = list(tree_handle, ARG1, ...);
*/
static void ejs_list_helper(struct clilist_file_info *info, const char *mask,
void *state)
{
MprVar *result = (MprVar *)state;
char idx[16];
mprItoa(result->properties->numDataItems, idx, sizeof(idx));
mprSetVar(result, idx, mprString(info->name));
}
static int ejs_list(MprVarHandle eid, int argc, MprVar **argv)
{
struct smbcli_tree *tree;
char *mask;
uint16_t attribute;
MprVar result;
if (argc != 3) {
ejsSetErrorMsg(eid, "list(): invalid number of args");
return -1;
}
if (!IS_TREE_HANDLE(argv[0])) {
ejsSetErrorMsg(eid, "first arg is not a tree handle");
return -1;
}
tree = (struct smbcli_tree *)argv[0]->ptr;
if (!mprVarIsString(argv[1]->type)) {
ejsSetErrorMsg(eid, "arg 2 must be a string");
return -1;
}
mask = argv[1]->string;
if (!mprVarIsNumber(argv[2]->type)) {
ejsSetErrorMsg(eid, "arg 3 must be a number");
return -1;
}
attribute = mprVarToInteger(argv[2]);
result = mprObject("list");
smbcli_list(tree, mask, attribute, ejs_list_helper, &result);
mpr_Return(eid, result);
return 0;
}
/*
setup C functions that be called from ejs
*/
void smb_setup_ejs_cli(void)
{
ejsDefineStringCFunction(-1, "tree_connect", ejs_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "tree_disconnect", ejs_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "mkdir", ejs_mkdir, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "rmdir", ejs_rmdir, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "rename", ejs_rename, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "unlink", ejs_unlink, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "list", ejs_list, NULL, MPR_VAR_SCRIPT_HANDLE);
#if 0
ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "tree_connect", ejs_cli_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "tree_disconnect", ejs_cli_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "session_logoff", ejs_cli_session_logoff, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "disconnect", ejs_cli_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE);
#endif
}
|