r19972: put a placeholder of a new js function and use error strings
[Samba/ekacnet.git] / source / scripting / ejs / ejsnet.c
blobcef3b99d051d2517975c5b92ebba3d01619a0f47
1 /*
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.
23 #include "includes.h"
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);
39 Usage:
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;
48 struct MprVar obj;
49 struct event_context *ev;
51 if (!event_mem_ctx) {
52 ejsSetErrorMsg(eid, "talloc_new() failed");
53 return -1;
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
59 * make it a child */
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);
64 if (creds == NULL) {
65 ejsSetErrorMsg(eid, "cli_credential_init() failed");
66 talloc_free(ctx);
67 return -1;
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");
74 if (creds == NULL) {
75 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
76 talloc_free(ctx);
77 return -1;
79 } else {
80 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
81 talloc_free(ctx);
82 return -1;
84 ctx->cred = creds;
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);
92 mpr_Return(eid, obj);
94 return 0;
98 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
100 TALLOC_CTX *mem_ctx;
101 struct libnet_context *ctx;
102 struct libnet_Join *join;
103 NTSTATUS status;
104 ctx = mprGetThisPtr(eid, "ctx");
105 mem_ctx = talloc_new(mprMemCtx());
107 join = talloc(mem_ctx, struct libnet_Join);
108 if (!join) {
109 talloc_free(mem_ctx);
110 return -1;
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);
124 if (netbios_name) {
125 join->in.netbios_name = mprToString(netbios_name);
127 if (domain_name) {
128 join->in.domain_name = mprToString(domain_name);
130 if (join_type) {
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);
138 return -1;
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));
149 } else {
150 mpr_Return(eid, mprCreateBoolVar(True));
152 talloc_free(mem_ctx);
153 return 0;
157 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
159 TALLOC_CTX *mem_ctx;
160 struct libnet_context *ctx;
161 struct libnet_samsync_ldb *samsync;
162 NTSTATUS status;
163 ctx = mprGetThisPtr(eid, "ctx");
164 mem_ctx = talloc_new(mprMemCtx());
166 samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
167 if (!samsync) {
168 talloc_free(mem_ctx);
169 return -1;
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);
181 if (credentials) {
182 samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
184 if (session_info) {
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));
197 } else {
198 mpr_Return(eid, mprCreateBoolVar(True));
200 talloc_free(mem_ctx);
201 return 0;
206 Usage:
207 usrCtx = net.UserMgr(domain);
209 static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
211 TALLOC_CTX *mem_ctx;
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());
219 if (argc == 0) {
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]));
225 } else {
226 ejsSetErrorMsg(eid, "too many arguments");
227 goto done;
230 if (!userman_domain) {
231 ejsSetErrorMsg(eid, "a domain must be specified for user management");
232 goto done;
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);
243 return 0;
244 done:
245 talloc_free(mem_ctx);
246 return -1;
250 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
252 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
253 TALLOC_CTX *mem_ctx;
254 struct libnet_context *ctx;
255 const char *userman_domain = NULL;
256 struct libnet_CreateUser req;
258 if (argc != 1) {
259 ejsSetErrorMsg(eid, "argument 1 must be a string");
260 return -1;
263 ctx = mprGetThisPtr(eid, "ctx");
264 if (!ctx) {
265 ejsSetErrorMsg(eid, "ctx property returns null pointer");
266 return -1;
269 userman_domain = mprGetThisPtr(eid, "domain");
270 if (!userman_domain) {
271 ejsSetErrorMsg(eid, "domain property returns null pointer");
272 return -1;
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));
287 return 0;
291 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
293 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
294 TALLOC_CTX *mem_ctx;
295 struct libnet_context *ctx;
296 const char *userman_domain = NULL;
297 struct libnet_DeleteUser req;
299 if (argc != 1) {
300 ejsSetErrorMsg(eid, "argument 1 must be a string");
301 return -1;
304 ctx = mprGetThisPtr(eid, "ctx");
305 if (!ctx) {
306 ejsSetErrorMsg(eid, "ctx property returns null pointer");
307 return -1;
310 userman_domain = mprGetThisPtr(eid, "domain");
311 if (!userman_domain) {
312 ejsSetErrorMsg(eid, "domain property returns null pointer");
313 return -1;
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));
328 return 0;
332 static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv)
334 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
335 TALLOC_CTX *mem_ctx;
336 struct libnet_context *ctx;
337 const char *userman_domain = NULL;
338 struct libnet_UserInfo req;
340 if (argc != 1) {
341 ejsSetErrorMsg(eid, "argument 1 must be a string");
342 return -1;
345 ctx = mprGetThisPtr(eid, "ctx");
346 if (!ctx) {
347 ejsSetErrorMsg(eid, "ctx property returns null pointer");
348 return -1;
351 userman_domain = mprGetThisPtr(eid, "domain");
352 if (!userman_domain) {
353 ejsSetErrorMsg(eid, "domain property returns null pointer");
354 return -1;
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);
370 return 0;
374 void ejsnet_setup(void)
376 ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);