ba0bad09531f6ab4146f41f22bd235ced3f0a77e
[metze/samba/wip.git] / source4 / scripting / ejs / ejsnet / net_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provides interfaces to libnet calls from ejs scripts
5
6    Copyright (C) Rafal Szczesniak  2005-2007
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
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"
30
31
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);
36
37
38 /*
39   Usage:
40   usrCtx = net.UserMgr(domain = <default from credentials>);
41 */
42 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
43 {
44         struct libnet_context *ctx;
45         const char *userman_domain = NULL;
46         struct MprVar obj;
47
48         /* libnet context */
49         ctx = mprGetThisPtr(eid, "ctx");
50         if (ctx == NULL) {
51                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
52                 return 0;
53         }
54
55         /* fetch the arguments: domain name */
56         if (argc == 0) {
57                 /* default domain name is supplied in credentials */
58                 userman_domain = cli_credentials_get_domain(ctx->cred);
59
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]);
64
65         } else {
66                 ejsSetErrorMsg(eid, "too many arguments");
67                 return 0;
68         }
69
70         /* any domain name must be specified anyway */
71         if (userman_domain == NULL) {
72                 ejsSetErrorMsg(eid, "a domain must be specified for user management");
73                 return 0;
74         }
75
76         /* create 'net user' subcontext */
77         obj = mprObject("NetUsrCtx");
78
79         /* we need to make a copy of the string for this object */
80         userman_domain = talloc_strdup(ctx, userman_domain);
81
82         /* add properties */
83         mprSetPtrChild(&obj, "ctx", ctx);
84         mprSetPtrChild(&obj, "domain", userman_domain);
85
86         /* add methods */
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);
91
92         /* set the object returned by this function */
93         mpr_Return(eid, obj);
94
95         return 0;
96 }
97
98
99 /*
100   Usage:
101   NTSTATUS = NetUsrCtx.Create(Username)
102 */
103 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
104 {
105         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
106         TALLOC_CTX *mem_ctx;
107         struct libnet_context *ctx;
108         const char *userman_domain = NULL;
109         const char *username = NULL;
110         struct libnet_CreateUser req;
111
112         mem_ctx = talloc_new(mprMemCtx());
113         if (mem_ctx == NULL) {
114                 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
115                 goto done;
116         }
117
118         /* fetch the arguments: username */
119         if (argc == 0) {
120                 ejsSetErrorMsg(eid, "too little arguments");
121                 goto done;
122
123         } else if (argc == 1) {
124                 username = argv[0];
125
126         } else {
127                 ejsSetErrorMsg(eid, "too many arguments");
128                 goto done;
129         }
130         
131         /* libnet context */
132         ctx = mprGetThisPtr(eid, "ctx");
133         if (ctx == NULL) {
134                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
135                 goto done;
136         }
137
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");
142                 goto done;
143         }
144         
145         /* call the libnet function */
146         req.in.domain_name = userman_domain;
147         req.in.user_name   = argv[0];
148
149         status = libnet_CreateUser(ctx, mem_ctx, &req);
150         if (!NT_STATUS_IS_OK(status)) {
151                 ejsSetErrorMsg(eid, "%s", req.out.error_string);
152         }
153
154 done:
155         talloc_free(mem_ctx);
156         mpr_Return(eid, mprNTSTATUS(status));
157         return 0;
158 }
159
160
161 /*
162   Usage:
163   NTSTATUS = NetUsrCtx.Delete(Username)
164 */
165 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
166 {
167         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
168         TALLOC_CTX *mem_ctx;
169         struct libnet_context *ctx;
170         const char *userman_domain = NULL;
171         const char *username = NULL;
172         struct libnet_DeleteUser req;
173
174         mem_ctx = talloc_new(mprMemCtx());
175         if (mem_ctx == NULL) {
176                 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
177                 goto done;
178         }
179
180         /* fetch the arguments: username */
181         if (argc == 0) {
182                 ejsSetErrorMsg(eid, "too little arguments");
183                 goto done;
184
185         } else if (argc == 1) {
186                 username = argv[0];
187
188         } else {
189                 ejsSetErrorMsg(eid, "too many arguments");
190                 goto done;
191         }
192
193         /* libnet context */
194         ctx = mprGetThisPtr(eid, "ctx");
195         if (ctx == NULL) {
196                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
197                 goto done;
198         }
199         
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");
204                 goto done;
205         }
206         
207         /* call the libnet function */
208         req.in.domain_name = userman_domain;
209         req.in.user_name   = username;
210
211         status = libnet_DeleteUser(ctx, mem_ctx, &req);
212         if (!NT_STATUS_IS_OK(status)) {
213                 ejsSetErrorMsg(eid, "%s", req.out.error_string);
214         }
215
216 done:
217         talloc_free(mem_ctx);
218         mpr_Return(eid, mprNTSTATUS(status));
219         return 0;
220 }
221
222
223 /*
224   Usage:
225   UserInfo = NetUsrCtx.Info(Username)
226 */
227 static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv)
228 {
229         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
230         TALLOC_CTX *mem_ctx;
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;
236
237         mem_ctx = talloc_new(mprMemCtx());
238         if (mem_ctx == NULL) {
239                 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
240                 goto done;
241         }
242         
243         /* fetch the arguments: username */
244         if (argc == 0) {
245                 ejsSetErrorMsg(eid, "too little arguments");
246                 goto done;
247
248         } else if (argc == 1) {
249                 username = argv[0];
250
251         } else {
252                 ejsSetErrorMsg(eid, "too many arguments");
253                 goto done;
254         }
255
256         /* libnet context */
257         ctx = mprGetThisPtr(eid, "ctx");
258         if (ctx == NULL) {
259                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
260                 goto done;
261         }
262         
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");
267                 return -1;
268         }
269
270         /* call the libnet function */
271         req.in.domain_name = userman_domain;
272         req.in.user_name   = username;
273         
274         status = libnet_UserInfo(ctx, mem_ctx, &req);
275         if (!NT_STATUS_IS_OK(status)) {
276                 ejsSetErrorMsg(eid, "%s", req.out.error_string);
277                 
278                 /* create null object to return */
279                 mprUserInfo = mprCreateNullVar();
280                 goto done;
281         }
282
283         /* create UserInfo object */
284         mprUserInfo = mprCreateUserInfo(ctx, &req);
285
286 done:
287         talloc_free(mem_ctx);
288         mpr_Return(eid, mprUserInfo);
289         return 0;
290 }
291
292
293 /*
294   Usage:
295   UserListCtx = NetUsrCtx.List(UserListCtx)
296 */
297 static int ejs_net_userlist(MprVarHandle eid, int argc, struct MprVar **argv)
298 {
299         TALLOC_CTX *mem_ctx;
300         NTSTATUS status;
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;
306         
307         mem_ctx = talloc_new(mprMemCtx());
308         if (mem_ctx == NULL) {
309                 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
310                 goto done;
311         }
312         
313         /* fetch the arguments */
314         if (argc == 0) {
315                 ejsSetErrorMsg(eid, "too little arguments");
316                 goto done;
317
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);
323
324                 } else {
325                         /* this is a first call */
326                         req.in.resume_index = 0;
327                 }
328
329         } else {
330                 ejsSetErrorMsg(eid, "too many arguments");
331                 goto done;
332         }
333
334         /* libnet context */
335         ctx = mprGetThisPtr(eid, "ctx");
336         if (ctx == NULL) {
337                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
338                 goto done;
339         }
340         
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");
345                 goto done;
346         }
347
348         /* call the libnet function */
349         req.in.domain_name   = userlist_domain;
350         req.in.page_size     = page_size;
351         
352         status = libnet_UserList(ctx, mem_ctx, &req);
353         mprListCtx = mprUserListCtx(mem_ctx, &req, status);
354
355 done:
356         talloc_free(mem_ctx);
357         mpr_Return(eid, mprListCtx);
358         return 0;
359 }