2 Unix SMB/CIFS implementation.
4 provides interfaces to libnet calls from ejs scripts
6 Copyright (C) Rafal Szczesniak 2005-2007
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "lib/appweb/ejs/ejs.h"
24 #include "libnet/libnet.h"
25 #include "scripting/ejs/ejsnet/proto.h"
26 #include "scripting/ejs/smbcalls.h"
27 #include "events/events.h"
28 #include "auth/credentials/credentials.h"
31 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv);
32 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv);
33 static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv);
34 static int ejs_net_userlist(MprVarHandle eid, int argc, struct MprVar **argv);
39 usrCtx = net.UserMgr(domain = <default from credentials>);
41 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
43 struct libnet_context *ctx;
44 const char *userman_domain = NULL;
48 ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
50 ejsSetErrorMsg(eid, "ctx property returns null pointer");
54 /* fetch the arguments: domain name */
56 /* default domain name is supplied in credentials */
57 userman_domain = cli_credentials_get_domain(ctx->cred);
59 } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
60 /* domain name can also be specified explicitly
61 (e.g. to connect BUILTIN domain) */
62 userman_domain = mprToString(argv[0]);
65 ejsSetErrorMsg(eid, "too many arguments");
69 /* any domain name must be specified anyway */
70 if (userman_domain == NULL) {
71 ejsSetErrorMsg(eid, "a domain must be specified for user management");
75 /* create 'net user' subcontext */
76 obj = mprObject("NetUsrCtx");
78 /* we need to make a copy of the string for this object */
79 userman_domain = talloc_strdup(ctx, userman_domain);
82 mprSetPtrChild(&obj, "ctx", ctx);
83 mprSetPtrChild(&obj, "domain", userman_domain);
86 mprSetStringCFunction(&obj, "Create", ejs_net_createuser);
87 mprSetStringCFunction(&obj, "Delete", ejs_net_deleteuser);
88 mprSetStringCFunction(&obj, "Info", ejs_net_userinfo);
89 mprSetCFunction(&obj, "List", ejs_net_userlist);
91 /* set the object returned by this function */
100 NTSTATUS = NetUsrCtx.Create(Username)
102 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
104 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
106 struct libnet_context *ctx;
107 const char *userman_domain = NULL;
108 const char *username = NULL;
109 struct libnet_CreateUser req;
111 mem_ctx = talloc_new(mprMemCtx());
112 if (mem_ctx == NULL) {
113 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
117 /* fetch the arguments: username */
119 ejsSetErrorMsg(eid, "too little arguments");
122 } else if (argc == 1) {
126 ejsSetErrorMsg(eid, "too many arguments");
131 ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
133 ejsSetErrorMsg(eid, "ctx property returns null pointer");
137 /* domain where the account is to be created */
138 userman_domain = (const char *)mprGetThisPtr(eid, "domain");
139 if (userman_domain == NULL) {
140 ejsSetErrorMsg(eid, "domain property returns null pointer");
144 /* call the libnet function */
145 req.in.domain_name = userman_domain;
146 req.in.user_name = argv[0];
148 status = libnet_CreateUser(ctx, mem_ctx, &req);
149 if (!NT_STATUS_IS_OK(status)) {
150 ejsSetErrorMsg(eid, "%s", req.out.error_string);
154 talloc_free(mem_ctx);
155 mpr_Return(eid, mprNTSTATUS(status));
162 NTSTATUS = NetUsrCtx.Delete(Username)
164 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
166 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
168 struct libnet_context *ctx;
169 const char *userman_domain = NULL;
170 const char *username = NULL;
171 struct libnet_DeleteUser req;
173 mem_ctx = talloc_new(mprMemCtx());
174 if (mem_ctx == NULL) {
175 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
179 /* fetch the arguments: username */
181 ejsSetErrorMsg(eid, "too little arguments");
184 } else if (argc == 1) {
188 ejsSetErrorMsg(eid, "too many arguments");
193 ctx = mprGetThisPtr(eid, "ctx");
195 ejsSetErrorMsg(eid, "ctx property returns null pointer");
199 /* domain where the account is to be deleted */
200 userman_domain = (const char*)mprGetThisPtr(eid, "domain");
201 if (!userman_domain) {
202 ejsSetErrorMsg(eid, "domain property returns null pointer");
206 /* call the libnet function */
207 req.in.domain_name = userman_domain;
208 req.in.user_name = username;
210 status = libnet_DeleteUser(ctx, mem_ctx, &req);
211 if (!NT_STATUS_IS_OK(status)) {
212 ejsSetErrorMsg(eid, "%s", req.out.error_string);
216 talloc_free(mem_ctx);
217 mpr_Return(eid, mprNTSTATUS(status));
224 UserInfo = NetUsrCtx.Info(Username)
226 static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv)
228 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
230 struct libnet_context *ctx;
231 const char *userman_domain = NULL;
232 const char *username = NULL;
233 struct libnet_UserInfo req;
234 struct MprVar mprUserInfo;
236 mem_ctx = talloc_new(mprMemCtx());
237 if (mem_ctx == NULL) {
238 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
242 /* fetch the arguments: username */
244 ejsSetErrorMsg(eid, "too little arguments");
247 } else if (argc == 1) {
251 ejsSetErrorMsg(eid, "too many arguments");
256 ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
258 ejsSetErrorMsg(eid, "ctx property returns null pointer");
262 /* domain where the user account is to be queried */
263 userman_domain = (const char *)mprGetThisPtr(eid, "domain");
264 if (userman_domain == NULL) {
265 ejsSetErrorMsg(eid, "domain property returns null pointer");
269 /* call the libnet function */
270 req.in.domain_name = userman_domain;
271 req.in.user_name = username;
273 status = libnet_UserInfo(ctx, mem_ctx, &req);
274 if (!NT_STATUS_IS_OK(status)) {
275 ejsSetErrorMsg(eid, "%s", req.out.error_string);
277 /* create null object to return */
278 mprUserInfo = mprCreateNullVar();
282 /* create UserInfo object */
283 mprUserInfo = mprCreateUserInfo(ctx, &req);
286 talloc_free(mem_ctx);
287 mpr_Return(eid, mprUserInfo);
294 UserListCtx = NetUsrCtx.List(UserListCtx)
296 static int ejs_net_userlist(MprVarHandle eid, int argc, struct MprVar **argv)
300 struct libnet_context *ctx;
301 const char *userlist_domain;
302 int page_size = 512; /* TODO: this should be specified in a nicer way */
303 struct libnet_UserList req;
304 struct MprVar mprListCtx, *mprInListCtx;
306 mem_ctx = talloc_new(mprMemCtx());
307 if (mem_ctx == NULL) {
308 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
312 /* fetch the arguments */
314 ejsSetErrorMsg(eid, "too little arguments");
317 } else if (argc == 1) {
318 if (mprVarIsObject(argv[0]->type)) {
319 /* this is a continuation call */
320 mprInListCtx = argv[0];
321 req.in.resume_index = mprListGetResumeIndex(mprInListCtx);
324 /* this is a first call */
325 req.in.resume_index = 0;
329 ejsSetErrorMsg(eid, "too many arguments");
334 ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
336 ejsSetErrorMsg(eid, "ctx property returns null pointer");
340 /* domain where user accounts are to be enumerated */
341 userlist_domain = mprGetThisPtr(eid, "domain");
342 if (userlist_domain == NULL) {
343 ejsSetErrorMsg(eid, "domain property returns null pointer");
347 /* call the libnet function */
348 req.in.domain_name = userlist_domain;
349 req.in.page_size = page_size;
351 status = libnet_UserList(ctx, mem_ctx, &req);
352 mprListCtx = mprUserListCtx(mem_ctx, &req, status);
355 talloc_free(mem_ctx);
356 mpr_Return(eid, mprListCtx);