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"
30 static int ejs_net_userman(MprVarHandle, int, struct MprVar**);
31 static int ejs_net_createuser(MprVarHandle, int, char**);
32 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv);
33 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv);
34 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv);
37 net = NetContext(credentials);
40 static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
42 struct cli_credentials *creds;
43 struct libnet_context *ctx;
46 /* TODO: Need to get the right event context in here */
47 ctx = libnet_context_init(NULL);
50 creds = cli_credentials_init(ctx);
52 ejsSetErrorMsg(eid, "cli_credential_init() failed");
56 cli_credentials_set_conf(creds);
57 cli_credentials_set_anonymous(creds);
58 } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
59 /* get credential values from credentials object */
60 creds = mprGetPtr(argv[0], "creds");
62 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
67 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
73 obj = mprObject("NetCtx");
74 mprSetPtrChild(&obj, "ctx", ctx);
76 mprSetCFunction(&obj, "UserMgr", ejs_net_userman);
77 mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain);
78 mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb);
84 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
87 struct libnet_context *ctx;
88 struct libnet_Join *join;
90 ctx = mprGetThisPtr(eid, "ctx");
91 mem_ctx = talloc_new(mprMemCtx());
93 join = talloc(mem_ctx, struct libnet_Join);
99 /* prepare parameters for the join */
100 join->in.netbios_name = NULL;
101 join->in.join_type = SEC_CHAN_WKSTA;
102 join->in.domain_name = cli_credentials_get_domain(ctx->cred);
103 join->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
104 join->out.error_string = NULL;
106 if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
107 MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL);
108 MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL);
109 MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL);
111 join->in.netbios_name = mprToString(netbios_name);
114 join->in.domain_name = mprToString(domain_name);
117 join->in.join_type = mprToInt(join_type);
121 if (!join->in.domain_name) {
122 ejsSetErrorMsg(eid, "a domain must be specified for to join");
123 talloc_free(mem_ctx);
127 /* do the domain join */
128 status = libnet_Join(ctx, join, join);
130 if (!NT_STATUS_IS_OK(status)) {
131 MprVar error_string = mprString(join->out.error_string);
133 mprSetPropertyValue(argv[0], "error_string", error_string);
134 mpr_Return(eid, mprCreateBoolVar(False));
136 mpr_Return(eid, mprCreateBoolVar(True));
138 talloc_free(mem_ctx);
142 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
145 struct libnet_context *ctx;
146 struct libnet_samsync_ldb *samsync;
148 ctx = mprGetThisPtr(eid, "ctx");
149 mem_ctx = talloc_new(mprMemCtx());
151 samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
153 talloc_free(mem_ctx);
157 /* prepare parameters for the samsync */
158 samsync->in.machine_account = NULL;
159 samsync->in.binding_string = NULL;
160 samsync->out.error_string = NULL;
162 if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
163 MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL);
165 samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
169 /* do the domain samsync */
170 status = libnet_samsync_ldb(ctx, samsync, samsync);
172 if (!NT_STATUS_IS_OK(status)) {
173 MprVar error_string = mprString(samsync->out.error_string);
175 mprSetPropertyValue(argv[0], "error_string", error_string);
176 mpr_Return(eid, mprCreateBoolVar(False));
178 mpr_Return(eid, mprCreateBoolVar(True));
180 talloc_free(mem_ctx);
184 static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
187 struct libnet_context *ctx;
188 const char *userman_domain = NULL;
189 struct MprVar *obj = NULL;
191 ctx = mprGetThisPtr(eid, "ctx");
192 mem_ctx = talloc_new(mprMemCtx());
195 userman_domain = cli_credentials_get_domain(ctx->cred);
197 } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
198 userman_domain = talloc_strdup(ctx, mprToString(argv[0]));
201 ejsSetErrorMsg(eid, "too many arguments");
205 if (!userman_domain) {
206 ejsSetErrorMsg(eid, "a domain must be specified for user management");
210 obj = mprInitObject(eid, "NetUsrCtx", argc, argv);
211 mprSetPtrChild(obj, "ctx", ctx);
212 mprSetPtrChild(obj, "domain", userman_domain);
214 mprSetStringCFunction(obj, "Create", ejs_net_createuser);
215 mprSetStringCFunction(obj, "Delete", ejs_net_deleteuser);
219 talloc_free(mem_ctx);
224 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
226 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
228 struct libnet_context *ctx;
229 const char *userman_domain = NULL;
230 struct libnet_CreateUser req;
233 ejsSetErrorMsg(eid, "argument 1 must be a string");
237 ctx = mprGetThisPtr(eid, "ctx");
239 ejsSetErrorMsg(eid, "ctx property returns null pointer");
243 userman_domain = mprGetThisPtr(eid, "domain");
244 if (!userman_domain) {
245 ejsSetErrorMsg(eid, "domain property returns null pointer");
249 mem_ctx = talloc_new(mprMemCtx());
251 req.in.domain_name = userman_domain;
252 req.in.user_name = argv[0];
254 status = libnet_CreateUser(ctx, mem_ctx, &req);
255 if (!NT_STATUS_IS_OK(status)) {
256 ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status));
259 talloc_free(mem_ctx);
260 mpr_Return(eid, mprNTSTATUS(status));
264 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
266 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
268 struct libnet_context *ctx;
269 const char *userman_domain = NULL;
270 struct libnet_DeleteUser req;
273 ejsSetErrorMsg(eid, "argument 1 must be a string");
277 ctx = mprGetThisPtr(eid, "ctx");
279 ejsSetErrorMsg(eid, "ctx property returns null pointer");
283 userman_domain = mprGetThisPtr(eid, "domain");
284 if (!userman_domain) {
285 ejsSetErrorMsg(eid, "domain property returns null pointer");
289 mem_ctx = talloc_new(mprMemCtx());
291 req.in.domain_name = userman_domain;
292 req.in.user_name = argv[0];
294 status = libnet_DeleteUser(ctx, mem_ctx, &req);
295 if (!NT_STATUS_IS_OK(status)) {
296 ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status));
299 talloc_free(mem_ctx);
300 mpr_Return(eid, mprNTSTATUS(status));
305 void ejsnet_setup(void)
307 ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);