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
|
/*
Unix SMB/CIFS implementation.
provide interfaces to libnet calls from ejs scripts
Copyright (C) Rafal Szczesniak 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 "lib/appweb/ejs/ejs.h"
#include "scripting/ejs/smbcalls.h"
#include "scripting/ejs/ejsnet.h"
#include "libnet/libnet.h"
#include "events/events.h"
#include "auth/credentials/credentials.h"
static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar** argv);
static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv);
static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv);
static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv);
static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv);
static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv);
/*
Usage:
net = NetContext(credentials);
*/
static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
{
TALLOC_CTX *event_mem_ctx = talloc_new(mprMemCtx());
struct cli_credentials *creds;
struct libnet_context *ctx;
struct MprVar obj;
struct event_context *ev;
if (!event_mem_ctx) {
ejsSetErrorMsg(eid, "talloc_new() failed");
return -1;
}
ev = event_context_find(event_mem_ctx);
ctx = libnet_context_init(ev);
/* IF we generated a new event context, it will be under here,
* and we need it to last as long as the libnet context, so
* make it a child */
talloc_steal(ctx, event_mem_ctx);
if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
creds = cli_credentials_init(ctx);
if (creds == NULL) {
ejsSetErrorMsg(eid, "cli_credential_init() failed");
talloc_free(ctx);
return -1;
}
cli_credentials_set_conf(creds);
cli_credentials_set_anonymous(creds);
} else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
/* get credential values from credentials object */
creds = mprGetPtr(argv[0], "creds");
if (creds == NULL) {
ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
talloc_free(ctx);
return -1;
}
} else {
ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
talloc_free(ctx);
return -1;
}
ctx->cred = creds;
obj = mprObject("NetCtx");
mprSetPtrChild(&obj, "ctx", ctx);
mprSetCFunction(&obj, "UserMgr", ejs_net_userman);
mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain);
mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb);
mpr_Return(eid, obj);
return 0;
}
static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
{
TALLOC_CTX *mem_ctx;
struct libnet_context *ctx;
struct libnet_Join *join;
NTSTATUS status;
ctx = mprGetThisPtr(eid, "ctx");
mem_ctx = talloc_new(mprMemCtx());
join = talloc(mem_ctx, struct libnet_Join);
if (!join) {
talloc_free(mem_ctx);
return -1;
}
/* prepare parameters for the join */
join->in.netbios_name = NULL;
join->in.join_type = SEC_CHAN_WKSTA;
join->in.domain_name = cli_credentials_get_domain(ctx->cred);
join->in.level = LIBNET_JOIN_AUTOMATIC;
join->out.error_string = NULL;
if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL);
MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL);
MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL);
if (netbios_name) {
join->in.netbios_name = mprToString(netbios_name);
}
if (domain_name) {
join->in.domain_name = mprToString(domain_name);
}
if (join_type) {
join->in.join_type = mprToInt(join_type);
}
}
if (!join->in.domain_name) {
ejsSetErrorMsg(eid, "a domain must be specified for to join");
talloc_free(mem_ctx);
return -1;
}
/* do the domain join */
status = libnet_Join(ctx, join, join);
if (!NT_STATUS_IS_OK(status)) {
MprVar error_string = mprString(join->out.error_string);
mprSetPropertyValue(argv[0], "error_string", error_string);
mpr_Return(eid, mprCreateBoolVar(False));
} else {
mpr_Return(eid, mprCreateBoolVar(True));
}
talloc_free(mem_ctx);
return 0;
}
static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
{
TALLOC_CTX *mem_ctx;
struct libnet_context *ctx;
struct libnet_samsync_ldb *samsync;
NTSTATUS status;
ctx = mprGetThisPtr(eid, "ctx");
mem_ctx = talloc_new(mprMemCtx());
samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
if (!samsync) {
talloc_free(mem_ctx);
return -1;
}
/* prepare parameters for the samsync */
samsync->in.machine_account = NULL;
samsync->in.session_info = NULL;
samsync->in.binding_string = NULL;
samsync->out.error_string = NULL;
if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL);
MprVar *session_info = mprGetProperty(argv[0], "session_info", NULL);
if (credentials) {
samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
}
if (session_info) {
samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
}
}
/* do the domain samsync */
status = libnet_samsync_ldb(ctx, samsync, samsync);
if (!NT_STATUS_IS_OK(status)) {
MprVar error_string = mprString(samsync->out.error_string);
mprSetPropertyValue(argv[0], "error_string", error_string);
mpr_Return(eid, mprCreateBoolVar(False));
} else {
mpr_Return(eid, mprCreateBoolVar(True));
}
talloc_free(mem_ctx);
return 0;
}
/*
Usage:
usrCtx = net.UserMgr(domain);
*/
static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
{
TALLOC_CTX *mem_ctx;
struct libnet_context *ctx;
const char *userman_domain = NULL;
struct MprVar *obj = NULL;
ctx = mprGetThisPtr(eid, "ctx");
mem_ctx = talloc_new(mprMemCtx());
if (argc == 0) {
userman_domain = cli_credentials_get_domain(ctx->cred);
} else if (argc == 1 && mprVarIsString(argv[0]->type)) {
userman_domain = talloc_strdup(ctx, mprToString(argv[0]));
} else {
ejsSetErrorMsg(eid, "too many arguments");
goto done;
}
if (!userman_domain) {
ejsSetErrorMsg(eid, "a domain must be specified for user management");
goto done;
}
obj = mprInitObject(eid, "NetUsrCtx", argc, argv);
mprSetPtrChild(obj, "ctx", ctx);
mprSetPtrChild(obj, "domain", userman_domain);
mprSetStringCFunction(obj, "Create", ejs_net_createuser);
mprSetStringCFunction(obj, "Delete", ejs_net_deleteuser);
mprSetStringCFunction(obj, "Info", ejs_net_userinfo);
return 0;
done:
talloc_free(mem_ctx);
return -1;
}
static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
{
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
TALLOC_CTX *mem_ctx;
struct libnet_context *ctx;
const char *userman_domain = NULL;
struct libnet_CreateUser req;
if (argc != 1) {
ejsSetErrorMsg(eid, "argument 1 must be a string");
return -1;
}
ctx = mprGetThisPtr(eid, "ctx");
if (!ctx) {
ejsSetErrorMsg(eid, "ctx property returns null pointer");
return -1;
}
userman_domain = mprGetThisPtr(eid, "domain");
if (!userman_domain) {
ejsSetErrorMsg(eid, "domain property returns null pointer");
return -1;
}
mem_ctx = talloc_new(mprMemCtx());
req.in.domain_name = userman_domain;
req.in.user_name = argv[0];
status = libnet_CreateUser(ctx, mem_ctx, &req);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "%s", req.out.error_string);
}
talloc_free(mem_ctx);
mpr_Return(eid, mprNTSTATUS(status));
return 0;
}
static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
{
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
TALLOC_CTX *mem_ctx;
struct libnet_context *ctx;
const char *userman_domain = NULL;
struct libnet_DeleteUser req;
if (argc != 1) {
ejsSetErrorMsg(eid, "argument 1 must be a string");
return -1;
}
ctx = mprGetThisPtr(eid, "ctx");
if (!ctx) {
ejsSetErrorMsg(eid, "ctx property returns null pointer");
return -1;
}
userman_domain = mprGetThisPtr(eid, "domain");
if (!userman_domain) {
ejsSetErrorMsg(eid, "domain property returns null pointer");
return -1;
}
mem_ctx = talloc_new(mprMemCtx());
req.in.domain_name = userman_domain;
req.in.user_name = argv[0];
status = libnet_DeleteUser(ctx, mem_ctx, &req);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "%s", req.out.error_string);
}
talloc_free(mem_ctx);
mpr_Return(eid, mprNTSTATUS(status));
return 0;
}
static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv)
{
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
TALLOC_CTX *mem_ctx;
struct libnet_context *ctx;
const char *userman_domain = NULL;
struct libnet_UserInfo req;
struct MprVar mprUserInfo;
struct MprVar mprAccountName, mprFullName, mprDescription;
struct MprVar mprHomeDir, mprHomeDrive, mprComment;
struct MprVar mprLogonScript;
struct MprVar mprAcctExpiry, mprAllowPassChange, mprForcePassChange;
if (argc != 1) {
ejsSetErrorMsg(eid, "argument 1 must be a string");
return -1;
}
ctx = mprGetThisPtr(eid, "ctx");
if (!ctx) {
ejsSetErrorMsg(eid, "ctx property returns null pointer");
return -1;
}
userman_domain = mprGetThisPtr(eid, "domain");
if (!userman_domain) {
ejsSetErrorMsg(eid, "domain property returns null pointer");
return -1;
}
mem_ctx = talloc_new(mprMemCtx());
req.in.domain_name = userman_domain;
req.in.user_name = argv[0];
status = libnet_UserInfo(ctx, mem_ctx, &req);
if (!NT_STATUS_IS_OK(status)) {
ejsSetErrorMsg(eid, "%s", req.out.error_string);
/* create null object to return */
mprUserInfo = mprCreateNullVar();
goto done;
}
/* create UserInfo object */
mprUserInfo = mprObject("UserInfo");
mprAccountName = mprString(req.out.account_name);
mprFullName = mprString(req.out.full_name);
mprDescription = mprString(req.out.description);
mprHomeDir = mprString(req.out.home_directory);
mprHomeDrive = mprString(req.out.home_drive);
mprComment = mprString(req.out.comment);
mprLogonScript = mprString(req.out.logon_script);
mprAcctExpiry = mprString(timestring(mem_ctx, req.out.acct_expiry->tv_sec));
mprAllowPassChange = mprString(timestring(ctx, req.out.allow_password_change->tv_sec));
mprForcePassChange = mprString(timestring(ctx, req.out.force_password_change->tv_sec));
status = mprSetVar(&mprUserInfo, "AccountName", mprAccountName);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "FullName", mprFullName);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "Description", mprDescription);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "HomeDirectory", mprHomeDir);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "HomeDrive", mprHomeDrive);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "Comment", mprComment);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "LogonScript", mprLogonScript);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "AcctExpiry", mprAcctExpiry);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "AllowPasswordChange", mprAllowPassChange);
if (!NT_STATUS_IS_OK(status)) goto done;
status = mprSetVar(&mprUserInfo, "ForcePasswordChange", mprForcePassChange);
if (!NT_STATUS_IS_OK(status)) goto done;
done:
talloc_free(mem_ctx);
mpr_Return(eid, mprUserInfo);
return 0;
}
void ejsnet_setup(void)
{
ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);
}
|