e98ba59aeecd7aa7d3335838a5f8a3fd4824fd57
[metze/samba/wip.git] / source4 / scripting / ejs / ejsnet / net_ctx.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 "scripting/ejs/smbcalls.h"
26 #include "libnet/libnet.h"
27 #include "events/events.h"
28 #include "auth/credentials/credentials.h"
29
30
31 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar** argv);
32 int ejs_net_hostman(MprVarHandle eid, int argc, struct MprVar** argv);
33
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 /*
38   Usage:
39   net = NetContext(credentials);
40 */
41
42 static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
43 {
44         TALLOC_CTX *event_mem_ctx = talloc_new(mprMemCtx());
45         struct cli_credentials *creds;
46         struct libnet_context *ctx;
47         struct MprVar obj, mprCreds;
48         struct event_context *ev;
49
50         if (!event_mem_ctx) {
51                 ejsSetErrorMsg(eid, "talloc_new() failed");
52                 return -1;
53         }
54         ev = event_context_find(event_mem_ctx);
55
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);
61
62         if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
63                 /* 
64                    create the default credentials
65                 */
66                 creds = cli_credentials_init(ctx);
67                 if (creds == NULL) {
68                         ejsSetErrorMsg(eid, "cli_credential_init() failed");
69                         talloc_free(ctx);
70                         return -1;
71                 }
72                 cli_credentials_set_conf(creds);
73                 cli_credentials_set_anonymous(creds);
74
75                 mprCreds = mprCredentials(creds);
76
77         } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
78                 /*
79                   get credential values from credentials object
80                 */
81                 mprCreds = *(argv[0]);
82                 creds = mprGetPtr(&mprCreds, "creds");
83                 if (creds == NULL) {
84                         ejsSetErrorMsg(eid, "invalid credentials parameter");
85                         talloc_free(ctx);
86                         return -1;
87                 }
88
89         } else {
90                 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
91                 talloc_free(ctx);
92                 return -1;
93         }
94         
95         /* setup libnet_context credentials */
96         ctx->cred = creds;
97
98         /* create the NetContext object */
99         obj = mprObject("NetContext");
100
101         /* add internal libnet_context pointer to the NetContext object */
102         mprSetPtrChild(&obj, "ctx", ctx);
103
104         /* add properties publicly available from js code */
105         mprCreateProperty(&obj, "credentials", &mprCreds);
106         
107         /* add methods to the object */
108         mprSetCFunction(&obj, "UserMgr", ejs_net_userman);
109         mprSetCFunction(&obj, "HostMgr", ejs_net_hostman);
110         mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain);
111         mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb);
112
113         /* return the object */
114         mpr_Return(eid, obj);
115
116         return 0;
117 }
118
119
120 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
121 {
122         TALLOC_CTX *mem_ctx;
123         struct libnet_context *ctx;
124         struct libnet_Join *join;
125         NTSTATUS status;
126         ctx = mprGetThisPtr(eid, "ctx");
127         mem_ctx = talloc_new(mprMemCtx());
128
129         join = talloc(mem_ctx, struct libnet_Join);
130         if (!join) {
131                 talloc_free(mem_ctx);
132                 return -1;
133         }
134
135         /* prepare parameters for the join */
136         join->in.netbios_name  = NULL;
137         join->in.join_type     = SEC_CHAN_WKSTA;
138         join->in.domain_name   = cli_credentials_get_domain(ctx->cred);
139         join->in.level         = LIBNET_JOIN_AUTOMATIC;
140         join->out.error_string = NULL;
141
142         if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
143                 MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL);
144                 MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL);
145                 MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL);
146                 if (netbios_name) {
147                         join->in.netbios_name = mprToString(netbios_name);
148                 }
149                 if (domain_name) {
150                         join->in.domain_name = mprToString(domain_name);
151                 }
152                 if (join_type) {
153                         join->in.join_type = mprToInt(join_type);
154                 }
155         }
156
157         if (!join->in.domain_name) {
158                 ejsSetErrorMsg(eid, "a domain must be specified for to join");
159                 talloc_free(mem_ctx);
160                 return -1;
161         }
162
163         /* do the domain join */
164         status = libnet_Join(ctx, join, join);
165         
166         if (!NT_STATUS_IS_OK(status)) {
167                 MprVar error_string = mprString(join->out.error_string);
168                 
169                 mprSetPropertyValue(argv[0], "error_string", error_string);
170                 mpr_Return(eid, mprCreateBoolVar(False));
171         } else {
172                 mpr_Return(eid, mprCreateBoolVar(True));
173         }
174         talloc_free(mem_ctx);
175         return 0;
176 }
177
178
179 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
180 {
181         TALLOC_CTX *mem_ctx;
182         struct libnet_context *ctx;
183         struct libnet_samsync_ldb *samsync;
184         NTSTATUS status;
185         ctx = mprGetThisPtr(eid, "ctx");
186         mem_ctx = talloc_new(mprMemCtx());
187
188         samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
189         if (!samsync) {
190                 talloc_free(mem_ctx);
191                 return -1;
192         }
193
194         /* prepare parameters for the samsync */
195         samsync->in.machine_account = NULL;
196         samsync->in.session_info = NULL;
197         samsync->in.binding_string = NULL;
198         samsync->out.error_string = NULL;
199
200         if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
201                 MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL);
202                 MprVar *session_info = mprGetProperty(argv[0], "session_info", NULL);
203                 if (credentials) {
204                         samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
205                 }
206                 if (session_info) {
207                         samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
208                 }
209         }
210
211         /* do the domain samsync */
212         status = libnet_samsync_ldb(ctx, samsync, samsync);
213         
214         if (!NT_STATUS_IS_OK(status)) {
215                 MprVar error_string = mprString(samsync->out.error_string);
216                 
217                 mprSetPropertyValue(argv[0], "error_string", error_string);
218                 mpr_Return(eid, mprCreateBoolVar(False));
219         } else {
220                 mpr_Return(eid, mprCreateBoolVar(True));
221         }
222         talloc_free(mem_ctx);
223         return 0;
224 }
225
226
227 NTSTATUS smb_setup_ejs_net(void)
228 {
229         ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);
230         return NT_STATUS_OK;
231 }