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 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 "libnet/libnet.h"
26 #include "scripting/ejs/ejsnet/proto.h"
27 #include "scripting/ejs/smbcalls.h"
28 #include "events/events.h"
29 #include "auth/credentials/credentials.h"
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_userlist(MprVarHandle eid, int argc, struct MprVar **argv);
40 usrCtx = net.UserMgr(domain = <default from credentials>);
42 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
44 struct libnet_context *ctx;
45 const char *userman_domain = NULL;
49 ctx = mprGetThisPtr(eid, "ctx");
51 ejsSetErrorMsg(eid, "ctx property returns null pointer");
55 /* fetch the arguments: domain name */
57 /* default domain name is supplied in credentials */
58 userman_domain = cli_credentials_get_domain(ctx->cred);
60 } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
61 /* domain name can also be specified explicitly
62 (e.g. to connect BUILTIN domain) */
63 userman_domain = mprToString(argv[0]);
66 ejsSetErrorMsg(eid, "too many arguments");
70 /* any domain name must be specified anyway */
71 if (userman_domain == NULL) {
72 ejsSetErrorMsg(eid, "a domain must be specified for user management");
76 /* create 'net user' subcontext */
77 obj = mprObject("NetUsrCtx");
79 /* we need to make a copy of the string for this object */
80 userman_domain = talloc_strdup(ctx, userman_domain);
83 mprSetPtrChild(&obj, "ctx", ctx);
84 mprSetPtrChild(&obj, "domain", userman_domain);
87 mprSetStringCFunction(&obj, "Create", ejs_net_createuser);
88 mprSetStringCFunction(&obj, "Delete", ejs_net_deleteuser);
89 mprSetStringCFunction(&obj, "Info", ejs_net_userinfo);
90 mprSetCFunction(&obj, "List", ejs_net_userlist);
92 /* set the object returned by this function */
101 NTSTATUS = NetUsrCtx.Create(Username)
103 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
105 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
107 struct libnet_context *ctx;
108 const char *userman_domain = NULL;
109 const char *username = NULL;
110 struct libnet_CreateUser req;
112 mem_ctx = talloc_new(mprMemCtx());
113 if (mem_ctx == NULL) {
114 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
118 /* fetch the arguments: username */
120 ejsSetErrorMsg(eid, "too little arguments");
123 } else if (argc == 1) {
127 ejsSetErrorMsg(eid, "too many arguments");
132 ctx = mprGetThisPtr(eid, "ctx");
134 ejsSetErrorMsg(eid, "ctx property returns null pointer");
138 /* domain where the account is to be created */
139 userman_domain = mprGetThisPtr(eid, "domain");
140 if (userman_domain == NULL) {
141 ejsSetErrorMsg(eid, "domain property returns null pointer");
145 /* call the libnet function */
146 req.in.domain_name = userman_domain;
147 req.in.user_name = argv[0];
149 status = libnet_CreateUser(ctx, mem_ctx, &req);
150 if (!NT_STATUS_IS_OK(status)) {
151 ejsSetErrorMsg(eid, "%s", req.out.error_string);
155 talloc_free(mem_ctx);
156 mpr_Return(eid, mprNTSTATUS(status));
163 NTSTATUS = NetUsrCtx.Delete(Username)
165 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
167 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
169 struct libnet_context *ctx;
170 const char *userman_domain = NULL;
171 const char *username = NULL;
172 struct libnet_DeleteUser req;
174 mem_ctx = talloc_new(mprMemCtx());
175 if (mem_ctx == NULL) {
176 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
180 /* fetch the arguments: username */
182 ejsSetErrorMsg(eid, "too little arguments");
185 } else if (argc == 1) {
189 ejsSetErrorMsg(eid, "too many arguments");
194 ctx = mprGetThisPtr(eid, "ctx");
196 ejsSetErrorMsg(eid, "ctx property returns null pointer");
200 /* domain where the account is to be deleted */
201 userman_domain = mprGetThisPtr(eid, "domain");
202 if (!userman_domain) {
203 ejsSetErrorMsg(eid, "domain property returns null pointer");
207 /* call the libnet function */
208 req.in.domain_name = userman_domain;
209 req.in.user_name = username;
211 status = libnet_DeleteUser(ctx, mem_ctx, &req);
212 if (!NT_STATUS_IS_OK(status)) {
213 ejsSetErrorMsg(eid, "%s", req.out.error_string);
217 talloc_free(mem_ctx);
218 mpr_Return(eid, mprNTSTATUS(status));
225 UserInfo = NetUsrCtx.Info(Username)
227 static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv)
229 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
231 struct libnet_context *ctx;
232 const char *userman_domain = NULL;
233 const char *username = NULL;
234 struct libnet_UserInfo req;
235 struct MprVar mprUserInfo;
237 mem_ctx = talloc_new(mprMemCtx());
238 if (mem_ctx == NULL) {
239 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
243 /* fetch the arguments: username */
245 ejsSetErrorMsg(eid, "too little arguments");
248 } else if (argc == 1) {
252 ejsSetErrorMsg(eid, "too many arguments");
257 ctx = mprGetThisPtr(eid, "ctx");
259 ejsSetErrorMsg(eid, "ctx property returns null pointer");
263 /* domain where the user account is to be queried */
264 userman_domain = mprGetThisPtr(eid, "domain");
265 if (userman_domain == NULL) {
266 ejsSetErrorMsg(eid, "domain property returns null pointer");
270 /* call the libnet function */
271 req.in.domain_name = userman_domain;
272 req.in.user_name = username;
274 status = libnet_UserInfo(ctx, mem_ctx, &req);
275 if (!NT_STATUS_IS_OK(status)) {
276 ejsSetErrorMsg(eid, "%s", req.out.error_string);
278 /* create null object to return */
279 mprUserInfo = mprCreateNullVar();
283 /* create UserInfo object */
284 mprUserInfo = mprCreateUserInfo(ctx, &req);
287 talloc_free(mem_ctx);
288 mpr_Return(eid, mprUserInfo);
295 UserListCtx = NetUsrCtx.List(UserListCtx)
297 static int ejs_net_userlist(MprVarHandle eid, int argc, struct MprVar **argv)
301 struct libnet_context *ctx;
302 const char *userlist_domain;
303 int page_size = 512; /* TODO: this should be specified in a nicer way */
304 struct libnet_UserList req;
305 struct MprVar mprListCtx, *mprInListCtx;
307 mem_ctx = talloc_new(mprMemCtx());
308 if (mem_ctx == NULL) {
309 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
313 /* fetch the arguments */
315 ejsSetErrorMsg(eid, "too little arguments");
318 } else if (argc == 1) {
319 if (mprVarIsObject(argv[0]->type)) {
320 /* this is a continuation call */
321 mprInListCtx = argv[0];
322 req.in.resume_index = mprListGetResumeIndex(mprInListCtx);
325 /* this is a first call */
326 req.in.resume_index = 0;
330 ejsSetErrorMsg(eid, "too many arguments");
335 ctx = mprGetThisPtr(eid, "ctx");
337 ejsSetErrorMsg(eid, "ctx property returns null pointer");
341 /* domain where user accounts are to be enumerated */
342 userlist_domain = mprGetThisPtr(eid, "domain");
343 if (userlist_domain == NULL) {
344 ejsSetErrorMsg(eid, "domain property returns null pointer");
348 /* call the libnet function */
349 req.in.domain_name = userlist_domain;
350 req.in.page_size = page_size;
352 status = libnet_UserList(ctx, mem_ctx, &req);
353 mprListCtx = mprUserListCtx(mem_ctx, &req, status);
356 talloc_free(mem_ctx);
357 mpr_Return(eid, mprListCtx);