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, int, struct MprVar**);
32 static int ejs_net_createuser(MprVarHandle, int, char**);
33 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv);
34 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv);
35 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv);
38 net = NetContext(credentials);
41 static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
43 TALLOC_CTX *event_mem_ctx = talloc_new(mprMemCtx());
44 struct cli_credentials *creds;
45 struct libnet_context *ctx;
47 struct event_context *ev;
50 ejsSetErrorMsg(eid, "talloc_new() failed");
53 ev = event_context_find(event_mem_ctx);
54 ctx = libnet_context_init(ev);
55 /* IF we generated a new event context, it will be under here,
56 * and we need it to last as long as the libnet context, so
58 talloc_steal(ctx, event_mem_ctx);
60 if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
61 creds = cli_credentials_init(ctx);
63 ejsSetErrorMsg(eid, "cli_credential_init() failed");
67 cli_credentials_set_conf(creds);
68 cli_credentials_set_anonymous(creds);
69 } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
70 /* get credential values from credentials object */
71 creds = mprGetPtr(argv[0], "creds");
73 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
78 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
84 obj = mprObject("NetCtx");
85 mprSetPtrChild(&obj, "ctx", ctx);
87 mprSetCFunction(&obj, "UserMgr", ejs_net_userman);
88 mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain);
89 mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb);
95 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
98 struct libnet_context *ctx;
99 struct libnet_Join *join;
101 ctx = mprGetThisPtr(eid, "ctx");
102 mem_ctx = talloc_new(mprMemCtx());
104 join = talloc(mem_ctx, struct libnet_Join);
106 talloc_free(mem_ctx);
110 /* prepare parameters for the join */
111 join->in.netbios_name = NULL;
112 join->in.join_type = SEC_CHAN_WKSTA;
113 join->in.domain_name = cli_credentials_get_domain(ctx->cred);
114 join->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
115 join->out.error_string = NULL;
117 if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
118 MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL);
119 MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL);
120 MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL);
122 join->in.netbios_name = mprToString(netbios_name);
125 join->in.domain_name = mprToString(domain_name);
128 join->in.join_type = mprToInt(join_type);
132 if (!join->in.domain_name) {
133 ejsSetErrorMsg(eid, "a domain must be specified for to join");
134 talloc_free(mem_ctx);
138 /* do the domain join */
139 status = libnet_Join(ctx, join, join);
141 if (!NT_STATUS_IS_OK(status)) {
142 MprVar error_string = mprString(join->out.error_string);
144 mprSetPropertyValue(argv[0], "error_string", error_string);
145 mpr_Return(eid, mprCreateBoolVar(False));
147 mpr_Return(eid, mprCreateBoolVar(True));
149 talloc_free(mem_ctx);
153 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
156 struct libnet_context *ctx;
157 struct libnet_samsync_ldb *samsync;
159 ctx = mprGetThisPtr(eid, "ctx");
160 mem_ctx = talloc_new(mprMemCtx());
162 samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
164 talloc_free(mem_ctx);
168 /* prepare parameters for the samsync */
169 samsync->in.machine_account = NULL;
170 samsync->in.session_info = NULL;
171 samsync->in.binding_string = NULL;
172 samsync->out.error_string = NULL;
174 if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
175 MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL);
176 MprVar *session_info = mprGetProperty(argv[0], "session_info", NULL);
178 samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
181 samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
185 /* do the domain samsync */
186 status = libnet_samsync_ldb(ctx, samsync, samsync);
188 if (!NT_STATUS_IS_OK(status)) {
189 MprVar error_string = mprString(samsync->out.error_string);
191 mprSetPropertyValue(argv[0], "error_string", error_string);
192 mpr_Return(eid, mprCreateBoolVar(False));
194 mpr_Return(eid, mprCreateBoolVar(True));
196 talloc_free(mem_ctx);
200 static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
203 struct libnet_context *ctx;
204 const char *userman_domain = NULL;
205 struct MprVar *obj = NULL;
207 ctx = mprGetThisPtr(eid, "ctx");
208 mem_ctx = talloc_new(mprMemCtx());
211 userman_domain = cli_credentials_get_domain(ctx->cred);
213 } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
214 userman_domain = talloc_strdup(ctx, mprToString(argv[0]));
217 ejsSetErrorMsg(eid, "too many arguments");
221 if (!userman_domain) {
222 ejsSetErrorMsg(eid, "a domain must be specified for user management");
226 obj = mprInitObject(eid, "NetUsrCtx", argc, argv);
227 mprSetPtrChild(obj, "ctx", ctx);
228 mprSetPtrChild(obj, "domain", userman_domain);
230 mprSetStringCFunction(obj, "Create", ejs_net_createuser);
231 mprSetStringCFunction(obj, "Delete", ejs_net_deleteuser);
235 talloc_free(mem_ctx);
240 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
242 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
244 struct libnet_context *ctx;
245 const char *userman_domain = NULL;
246 struct libnet_CreateUser req;
249 ejsSetErrorMsg(eid, "argument 1 must be a string");
253 ctx = mprGetThisPtr(eid, "ctx");
255 ejsSetErrorMsg(eid, "ctx property returns null pointer");
259 userman_domain = mprGetThisPtr(eid, "domain");
260 if (!userman_domain) {
261 ejsSetErrorMsg(eid, "domain property returns null pointer");
265 mem_ctx = talloc_new(mprMemCtx());
267 req.in.domain_name = userman_domain;
268 req.in.user_name = argv[0];
270 status = libnet_CreateUser(ctx, mem_ctx, &req);
271 if (!NT_STATUS_IS_OK(status)) {
272 ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status));
275 talloc_free(mem_ctx);
276 mpr_Return(eid, mprNTSTATUS(status));
280 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
282 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
284 struct libnet_context *ctx;
285 const char *userman_domain = NULL;
286 struct libnet_DeleteUser req;
289 ejsSetErrorMsg(eid, "argument 1 must be a string");
293 ctx = mprGetThisPtr(eid, "ctx");
295 ejsSetErrorMsg(eid, "ctx property returns null pointer");
299 userman_domain = mprGetThisPtr(eid, "domain");
300 if (!userman_domain) {
301 ejsSetErrorMsg(eid, "domain property returns null pointer");
305 mem_ctx = talloc_new(mprMemCtx());
307 req.in.domain_name = userman_domain;
308 req.in.user_name = argv[0];
310 status = libnet_DeleteUser(ctx, mem_ctx, &req);
311 if (!NT_STATUS_IS_OK(status)) {
312 ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status));
315 talloc_free(mem_ctx);
316 mpr_Return(eid, mprNTSTATUS(status));
321 void ejsnet_setup(void)
323 ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);