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 "scripting/ejs/smbcalls.h"
26 #include "libnet/libnet.h"
27 #include "events/events.h"
28 #include "auth/credentials/credentials.h"
31 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar** argv);
32 int ejs_net_hostman(MprVarHandle eid, int argc, struct MprVar** 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);
39 net = NetContext(credentials);
42 static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
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;
51 ejsSetErrorMsg(eid, "talloc_new() failed");
54 ev = event_context_find(event_mem_ctx);
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
60 talloc_steal(ctx, event_mem_ctx);
62 if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
64 create the default credentials
66 creds = cli_credentials_init(ctx);
68 ejsSetErrorMsg(eid, "cli_credential_init() failed");
72 cli_credentials_set_conf(creds);
73 cli_credentials_set_anonymous(creds);
75 mprCreds = mprCredentials(creds);
77 } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
79 get credential values from credentials object
81 mprCreds = *(argv[0]);
82 creds = mprGetPtr(&mprCreds, "creds");
84 ejsSetErrorMsg(eid, "invalid credentials parameter");
90 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
95 /* setup libnet_context credentials */
98 /* create the NetContext object */
99 obj = mprObject("NetContext");
101 /* add internal libnet_context pointer to the NetContext object */
102 mprSetPtrChild(&obj, "ctx", ctx);
104 /* add properties publicly available from js code */
105 mprCreateProperty(&obj, "credentials", &mprCreds);
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);
113 /* return the object */
114 mpr_Return(eid, obj);
120 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
123 struct libnet_context *ctx;
124 struct libnet_Join *join;
126 ctx = mprGetThisPtr(eid, "ctx");
127 mem_ctx = talloc_new(mprMemCtx());
129 join = talloc(mem_ctx, struct libnet_Join);
131 talloc_free(mem_ctx);
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;
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);
147 join->in.netbios_name = mprToString(netbios_name);
150 join->in.domain_name = mprToString(domain_name);
153 join->in.join_type = mprToInt(join_type);
157 if (!join->in.domain_name) {
158 ejsSetErrorMsg(eid, "a domain must be specified for to join");
159 talloc_free(mem_ctx);
163 /* do the domain join */
164 status = libnet_Join(ctx, join, join);
166 if (!NT_STATUS_IS_OK(status)) {
167 MprVar error_string = mprString(join->out.error_string);
169 mprSetPropertyValue(argv[0], "error_string", error_string);
170 mpr_Return(eid, mprCreateBoolVar(False));
172 mpr_Return(eid, mprCreateBoolVar(True));
174 talloc_free(mem_ctx);
179 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
182 struct libnet_context *ctx;
183 struct libnet_samsync_ldb *samsync;
185 ctx = mprGetThisPtr(eid, "ctx");
186 mem_ctx = talloc_new(mprMemCtx());
188 samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
190 talloc_free(mem_ctx);
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;
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);
204 samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
207 samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
211 /* do the domain samsync */
212 status = libnet_samsync_ldb(ctx, samsync, samsync);
214 if (!NT_STATUS_IS_OK(status)) {
215 MprVar error_string = mprString(samsync->out.error_string);
217 mprSetPropertyValue(argv[0], "error_string", error_string);
218 mpr_Return(eid, mprCreateBoolVar(False));
220 mpr_Return(eid, mprCreateBoolVar(True));
222 talloc_free(mem_ctx);
227 NTSTATUS smb_setup_ejs_net(void)
229 ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);