r14363: Remove credentials.h from the global includes.
[samba.git] / source4 / scripting / ejs / ejsnet.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide interfaces to libnet calls from ejs scripts
5
6    Copyright (C) Rafal Szczesniak  2005
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 "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"
30
31 static int ejs_net_userman(MprVarHandle, int, struct MprVar**);
32 static int ejs_net_createuser(MprVarHandle, int, char**);
33 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv);
34 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv);
35 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv);
36
37 /* Usage:
38    net = NetContext(credentials);
39 */
40
41 static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
42 {
43         TALLOC_CTX *event_mem_ctx = talloc_new(mprMemCtx());
44         struct cli_credentials *creds;
45         struct libnet_context *ctx;
46         struct MprVar obj;
47         struct event_context *ev;
48
49         if (!event_mem_ctx) {
50                 ejsSetErrorMsg(eid, "talloc_new() failed");
51                 return -1;
52         }
53         ev = event_context_find(event_mem_ctx);
54         ctx = libnet_context_init(ev);
55         /* IF we generated a new event context, it will be under here,
56          * and we need it to last as long as the libnet context, so
57          * make it a child */
58         talloc_steal(ctx, event_mem_ctx);
59
60         if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
61                 creds = cli_credentials_init(ctx);
62                 if (creds == NULL) {
63                         ejsSetErrorMsg(eid, "cli_credential_init() failed");
64                         talloc_free(ctx);
65                         return -1;
66                 }
67                 cli_credentials_set_conf(creds);
68                 cli_credentials_set_anonymous(creds);
69         } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
70                 /* get credential values from credentials object */
71                 creds = mprGetPtr(argv[0], "creds");
72                 if (creds == NULL) {
73                         ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
74                         talloc_free(ctx);
75                         return -1;
76                 }
77         } else {
78                 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
79                 talloc_free(ctx);
80                 return -1;
81         }
82         ctx->cred = creds;
83
84         obj = mprObject("NetCtx");
85         mprSetPtrChild(&obj, "ctx", ctx);
86         
87         mprSetCFunction(&obj, "UserMgr", ejs_net_userman);
88         mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain);
89         mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb);
90         mpr_Return(eid, obj);
91
92         return 0;
93 }
94
95 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
96 {
97         TALLOC_CTX *mem_ctx;
98         struct libnet_context *ctx;
99         struct libnet_Join *join;
100         NTSTATUS status;
101         ctx = mprGetThisPtr(eid, "ctx");
102         mem_ctx = talloc_new(mprMemCtx());
103
104         join = talloc(mem_ctx, struct libnet_Join);
105         if (!join) {
106                 talloc_free(mem_ctx);
107                 return -1;
108         }
109
110         /* prepare parameters for the join */
111         join->in.netbios_name  = NULL;
112         join->in.join_type     = SEC_CHAN_WKSTA;
113         join->in.domain_name   = cli_credentials_get_domain(ctx->cred);
114         join->in.level         = LIBNET_JOINDOMAIN_AUTOMATIC;
115         join->out.error_string = NULL;
116
117         if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
118                 MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL);
119                 MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL);
120                 MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL);
121                 if (netbios_name) {
122                         join->in.netbios_name = mprToString(netbios_name);
123                 }
124                 if (domain_name) {
125                         join->in.domain_name = mprToString(domain_name);
126                 }
127                 if (join_type) {
128                         join->in.join_type = mprToInt(join_type);
129                 }
130         }
131
132         if (!join->in.domain_name) {
133                 ejsSetErrorMsg(eid, "a domain must be specified for to join");
134                 talloc_free(mem_ctx);
135                 return -1;
136         }
137
138         /* do the domain join */
139         status = libnet_Join(ctx, join, join);
140         
141         if (!NT_STATUS_IS_OK(status)) {
142                 MprVar error_string = mprString(join->out.error_string);
143                 
144                 mprSetPropertyValue(argv[0], "error_string", error_string);
145                 mpr_Return(eid, mprCreateBoolVar(False));
146         } else {
147                 mpr_Return(eid, mprCreateBoolVar(True));
148         }
149         talloc_free(mem_ctx);
150         return 0;
151 }
152
153 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
154 {
155         TALLOC_CTX *mem_ctx;
156         struct libnet_context *ctx;
157         struct libnet_samsync_ldb *samsync;
158         NTSTATUS status;
159         ctx = mprGetThisPtr(eid, "ctx");
160         mem_ctx = talloc_new(mprMemCtx());
161
162         samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
163         if (!samsync) {
164                 talloc_free(mem_ctx);
165                 return -1;
166         }
167
168         /* prepare parameters for the samsync */
169         samsync->in.machine_account = NULL;
170         samsync->in.session_info = NULL;
171         samsync->in.binding_string = NULL;
172         samsync->out.error_string = NULL;
173
174         if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
175                 MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL);
176                 MprVar *session_info = mprGetProperty(argv[0], "session_info", NULL);
177                 if (credentials) {
178                         samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
179                 }
180                 if (session_info) {
181                         samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
182                 }
183         }
184
185         /* do the domain samsync */
186         status = libnet_samsync_ldb(ctx, samsync, samsync);
187         
188         if (!NT_STATUS_IS_OK(status)) {
189                 MprVar error_string = mprString(samsync->out.error_string);
190                 
191                 mprSetPropertyValue(argv[0], "error_string", error_string);
192                 mpr_Return(eid, mprCreateBoolVar(False));
193         } else {
194                 mpr_Return(eid, mprCreateBoolVar(True));
195         }
196         talloc_free(mem_ctx);
197         return 0;
198 }
199
200 static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
201 {
202         TALLOC_CTX *mem_ctx;
203         struct libnet_context *ctx;
204         const char *userman_domain = NULL;
205         struct MprVar *obj = NULL;
206
207         ctx = mprGetThisPtr(eid, "ctx");
208         mem_ctx = talloc_new(mprMemCtx());
209
210         if (argc == 0) {
211                 userman_domain = cli_credentials_get_domain(ctx->cred);
212
213         } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
214                 userman_domain = talloc_strdup(ctx, mprToString(argv[0]));
215
216         } else {
217                 ejsSetErrorMsg(eid, "too many arguments");
218                 goto done;
219         }
220         
221         if (!userman_domain) {
222                 ejsSetErrorMsg(eid, "a domain must be specified for user management");
223                 goto done;
224         }
225
226         obj = mprInitObject(eid, "NetUsrCtx", argc, argv);
227         mprSetPtrChild(obj, "ctx", ctx);
228         mprSetPtrChild(obj, "domain", userman_domain);
229
230         mprSetStringCFunction(obj, "Create", ejs_net_createuser);
231         mprSetStringCFunction(obj, "Delete", ejs_net_deleteuser);
232
233         return 0;
234 done:
235         talloc_free(mem_ctx);
236         return -1;
237 }
238
239
240 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
241 {
242         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
243         TALLOC_CTX *mem_ctx;
244         struct libnet_context *ctx;
245         const char *userman_domain = NULL;
246         struct libnet_CreateUser req;
247
248         if (argc != 1) {
249                 ejsSetErrorMsg(eid, "argument 1 must be a string");
250                 return -1;
251         }
252
253         ctx = mprGetThisPtr(eid, "ctx");
254         if (!ctx) {
255                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
256                 return -1;
257         }
258
259         userman_domain = mprGetThisPtr(eid, "domain");
260         if (!userman_domain) {
261                 ejsSetErrorMsg(eid, "domain property returns null pointer");
262                 return -1;
263         }
264         
265         mem_ctx = talloc_new(mprMemCtx());
266
267         req.in.domain_name = userman_domain;
268         req.in.user_name   = argv[0];
269
270         status = libnet_CreateUser(ctx, mem_ctx, &req);
271         if (!NT_STATUS_IS_OK(status)) {
272                 ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status));
273         }
274
275         talloc_free(mem_ctx);
276         mpr_Return(eid, mprNTSTATUS(status));
277         return 0;
278 }
279
280 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
281 {
282         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
283         TALLOC_CTX *mem_ctx;
284         struct libnet_context *ctx;
285         const char *userman_domain = NULL;
286         struct libnet_DeleteUser req;
287
288         if (argc != 1) {
289                 ejsSetErrorMsg(eid, "argument 1 must be a string");
290                 return -1;
291         }
292
293         ctx = mprGetThisPtr(eid, "ctx");
294         if (!ctx) {
295                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
296                 return -1;
297         }
298
299         userman_domain = mprGetThisPtr(eid, "domain");
300         if (!userman_domain) {
301                 ejsSetErrorMsg(eid, "domain property returns null pointer");
302                 return -1;
303         }
304         
305         mem_ctx = talloc_new(mprMemCtx());
306
307         req.in.domain_name = userman_domain;
308         req.in.user_name   = argv[0];
309
310         status = libnet_DeleteUser(ctx, mem_ctx, &req);
311         if (!NT_STATUS_IS_OK(status)) {
312                 ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status));
313         }
314
315         talloc_free(mem_ctx);
316         mpr_Return(eid, mprNTSTATUS(status));
317         return 0;
318 }
319
320
321 void ejsnet_setup(void)
322 {
323         ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);
324 }