2 Unix SMB/CIFS implementation.
4 provide interfaces to libnet calls from ejs scripts
6 Copyright (C) Rafal Szczesniak 2005
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.
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.
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.
24 #include "lib/appweb/ejs/ejs.h"
25 #include "scripting/ejs/smbcalls.h"
26 #include "scripting/ejs/ejsnet.h"
27 #include "libnet/libnet.h"
28 #include "events/events.h"
29 #include "auth/credentials/credentials.h"
31 static int ejs_net_userman(MprVarHandle eid
, int argc
, struct MprVar
** argv
);
32 static int ejs_net_createuser(MprVarHandle eid
, int argc
, char **argv
);
33 static int ejs_net_deleteuser(MprVarHandle eid
, int argc
, char **argv
);
34 static int ejs_net_userinfo(MprVarHandle eid
, int argc
, char **argv
);
35 static int ejs_net_join_domain(MprVarHandle eid
, int argc
, struct MprVar
**argv
);
36 static int ejs_net_samsync_ldb(MprVarHandle eid
, int argc
, struct MprVar
**argv
);
40 net = NetContext(credentials);
43 static int ejs_net_context(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
45 TALLOC_CTX
*event_mem_ctx
= talloc_new(mprMemCtx());
46 struct cli_credentials
*creds
;
47 struct libnet_context
*ctx
;
49 struct event_context
*ev
;
52 ejsSetErrorMsg(eid
, "talloc_new() failed");
55 ev
= event_context_find(event_mem_ctx
);
56 ctx
= libnet_context_init(ev
);
57 /* IF we generated a new event context, it will be under here,
58 * and we need it to last as long as the libnet context, so
60 talloc_steal(ctx
, event_mem_ctx
);
62 if (argc
== 0 || (argc
== 1 && argv
[0]->type
== MPR_TYPE_NULL
)) {
63 creds
= cli_credentials_init(ctx
);
65 ejsSetErrorMsg(eid
, "cli_credential_init() failed");
69 cli_credentials_set_conf(creds
);
70 cli_credentials_set_anonymous(creds
);
71 } else if (argc
== 1 && argv
[0]->type
== MPR_TYPE_OBJECT
) {
72 /* get credential values from credentials object */
73 creds
= mprGetPtr(argv
[0], "creds");
75 ejsSetErrorMsg(eid
, "userAuth requires a 'creds' first parameter");
80 ejsSetErrorMsg(eid
, "NetContext invalid arguments, this function requires an object.");
86 obj
= mprObject("NetCtx");
87 mprSetPtrChild(&obj
, "ctx", ctx
);
89 mprSetCFunction(&obj
, "UserMgr", ejs_net_userman
);
90 mprSetCFunction(&obj
, "JoinDomain", ejs_net_join_domain
);
91 mprSetCFunction(&obj
, "SamSyncLdb", ejs_net_samsync_ldb
);
98 static int ejs_net_join_domain(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
101 struct libnet_context
*ctx
;
102 struct libnet_Join
*join
;
104 ctx
= mprGetThisPtr(eid
, "ctx");
105 mem_ctx
= talloc_new(mprMemCtx());
107 join
= talloc(mem_ctx
, struct libnet_Join
);
109 talloc_free(mem_ctx
);
113 /* prepare parameters for the join */
114 join
->in
.netbios_name
= NULL
;
115 join
->in
.join_type
= SEC_CHAN_WKSTA
;
116 join
->in
.domain_name
= cli_credentials_get_domain(ctx
->cred
);
117 join
->in
.level
= LIBNET_JOIN_AUTOMATIC
;
118 join
->out
.error_string
= NULL
;
120 if (argc
== 1 && argv
[0]->type
== MPR_TYPE_OBJECT
) {
121 MprVar
*netbios_name
= mprGetProperty(argv
[0], "netbios_name", NULL
);
122 MprVar
*domain_name
= mprGetProperty(argv
[0], "domain_name", NULL
);
123 MprVar
*join_type
= mprGetProperty(argv
[0], "join_type", NULL
);
125 join
->in
.netbios_name
= mprToString(netbios_name
);
128 join
->in
.domain_name
= mprToString(domain_name
);
131 join
->in
.join_type
= mprToInt(join_type
);
135 if (!join
->in
.domain_name
) {
136 ejsSetErrorMsg(eid
, "a domain must be specified for to join");
137 talloc_free(mem_ctx
);
141 /* do the domain join */
142 status
= libnet_Join(ctx
, join
, join
);
144 if (!NT_STATUS_IS_OK(status
)) {
145 MprVar error_string
= mprString(join
->out
.error_string
);
147 mprSetPropertyValue(argv
[0], "error_string", error_string
);
148 mpr_Return(eid
, mprCreateBoolVar(False
));
150 mpr_Return(eid
, mprCreateBoolVar(True
));
152 talloc_free(mem_ctx
);
157 static int ejs_net_samsync_ldb(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
160 struct libnet_context
*ctx
;
161 struct libnet_samsync_ldb
*samsync
;
163 ctx
= mprGetThisPtr(eid
, "ctx");
164 mem_ctx
= talloc_new(mprMemCtx());
166 samsync
= talloc(mem_ctx
, struct libnet_samsync_ldb
);
168 talloc_free(mem_ctx
);
172 /* prepare parameters for the samsync */
173 samsync
->in
.machine_account
= NULL
;
174 samsync
->in
.session_info
= NULL
;
175 samsync
->in
.binding_string
= NULL
;
176 samsync
->out
.error_string
= NULL
;
178 if (argc
== 1 && argv
[0]->type
== MPR_TYPE_OBJECT
) {
179 MprVar
*credentials
= mprGetProperty(argv
[0], "machine_account", NULL
);
180 MprVar
*session_info
= mprGetProperty(argv
[0], "session_info", NULL
);
182 samsync
->in
.machine_account
= talloc_get_type(mprGetPtr(credentials
, "creds"), struct cli_credentials
);
185 samsync
->in
.session_info
= talloc_get_type(mprGetPtr(session_info
, "session_info"), struct auth_session_info
);
189 /* do the domain samsync */
190 status
= libnet_samsync_ldb(ctx
, samsync
, samsync
);
192 if (!NT_STATUS_IS_OK(status
)) {
193 MprVar error_string
= mprString(samsync
->out
.error_string
);
195 mprSetPropertyValue(argv
[0], "error_string", error_string
);
196 mpr_Return(eid
, mprCreateBoolVar(False
));
198 mpr_Return(eid
, mprCreateBoolVar(True
));
200 talloc_free(mem_ctx
);
207 usrCtx = net.UserMgr(domain);
209 static int ejs_net_userman(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
212 struct libnet_context
*ctx
;
213 const char *userman_domain
= NULL
;
214 struct MprVar
*obj
= NULL
;
216 ctx
= mprGetThisPtr(eid
, "ctx");
217 mem_ctx
= talloc_new(mprMemCtx());
220 userman_domain
= cli_credentials_get_domain(ctx
->cred
);
222 } else if (argc
== 1 && mprVarIsString(argv
[0]->type
)) {
223 userman_domain
= talloc_strdup(ctx
, mprToString(argv
[0]));
226 ejsSetErrorMsg(eid
, "too many arguments");
230 if (!userman_domain
) {
231 ejsSetErrorMsg(eid
, "a domain must be specified for user management");
235 obj
= mprInitObject(eid
, "NetUsrCtx", argc
, argv
);
236 mprSetPtrChild(obj
, "ctx", ctx
);
237 mprSetPtrChild(obj
, "domain", userman_domain
);
239 mprSetStringCFunction(obj
, "Create", ejs_net_createuser
);
240 mprSetStringCFunction(obj
, "Delete", ejs_net_deleteuser
);
241 mprSetStringCFunction(obj
, "Info", ejs_net_userinfo
);
245 talloc_free(mem_ctx
);
250 static int ejs_net_createuser(MprVarHandle eid
, int argc
, char **argv
)
252 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
254 struct libnet_context
*ctx
;
255 const char *userman_domain
= NULL
;
256 struct libnet_CreateUser req
;
259 ejsSetErrorMsg(eid
, "argument 1 must be a string");
263 ctx
= mprGetThisPtr(eid
, "ctx");
265 ejsSetErrorMsg(eid
, "ctx property returns null pointer");
269 userman_domain
= mprGetThisPtr(eid
, "domain");
270 if (!userman_domain
) {
271 ejsSetErrorMsg(eid
, "domain property returns null pointer");
275 mem_ctx
= talloc_new(mprMemCtx());
277 req
.in
.domain_name
= userman_domain
;
278 req
.in
.user_name
= argv
[0];
280 status
= libnet_CreateUser(ctx
, mem_ctx
, &req
);
281 if (!NT_STATUS_IS_OK(status
)) {
282 ejsSetErrorMsg(eid
, "%s", req
.out
.error_string
);
285 talloc_free(mem_ctx
);
286 mpr_Return(eid
, mprNTSTATUS(status
));
291 static int ejs_net_deleteuser(MprVarHandle eid
, int argc
, char **argv
)
293 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
295 struct libnet_context
*ctx
;
296 const char *userman_domain
= NULL
;
297 struct libnet_DeleteUser req
;
300 ejsSetErrorMsg(eid
, "argument 1 must be a string");
304 ctx
= mprGetThisPtr(eid
, "ctx");
306 ejsSetErrorMsg(eid
, "ctx property returns null pointer");
310 userman_domain
= mprGetThisPtr(eid
, "domain");
311 if (!userman_domain
) {
312 ejsSetErrorMsg(eid
, "domain property returns null pointer");
316 mem_ctx
= talloc_new(mprMemCtx());
318 req
.in
.domain_name
= userman_domain
;
319 req
.in
.user_name
= argv
[0];
321 status
= libnet_DeleteUser(ctx
, mem_ctx
, &req
);
322 if (!NT_STATUS_IS_OK(status
)) {
323 ejsSetErrorMsg(eid
, "%s", req
.out
.error_string
);
326 talloc_free(mem_ctx
);
327 mpr_Return(eid
, mprNTSTATUS(status
));
332 static int ejs_net_userinfo(MprVarHandle eid
, int argc
, char **argv
)
334 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
336 struct libnet_context
*ctx
;
337 const char *userman_domain
= NULL
;
338 struct libnet_UserInfo req
;
341 ejsSetErrorMsg(eid
, "argument 1 must be a string");
345 ctx
= mprGetThisPtr(eid
, "ctx");
347 ejsSetErrorMsg(eid
, "ctx property returns null pointer");
351 userman_domain
= mprGetThisPtr(eid
, "domain");
352 if (!userman_domain
) {
353 ejsSetErrorMsg(eid
, "domain property returns null pointer");
357 mem_ctx
= talloc_new(mprMemCtx());
359 req
.in
.domain_name
= userman_domain
;
360 req
.in
.user_name
= argv
[0];
362 status
= libnet_UserInfo(ctx
, mem_ctx
, &req
);
363 if (!NT_STATUS_IS_OK(status
)) {
364 ejsSetErrorMsg(eid
, "%s", req
.out
.error_string
);
367 /* TODO: create user info object and pass received properties */
369 talloc_free(mem_ctx
);
374 void ejsnet_setup(void)
376 ejsDefineCFunction(-1, "NetContext", ejs_net_context
, NULL
, MPR_VAR_SCRIPT_HANDLE
);