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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "lib/appweb/ejs/ejs.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "libnet/libnet.h"
26 #include "events/events.h"
27 #include "auth/credentials/credentials.h"
30 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar** argv);
31 int ejs_net_hostman(MprVarHandle eid, int argc, struct MprVar** argv);
33 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv);
34 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv);
38 net = NetContext(credentials);
41 static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv)
43 TALLOC_CTX *event_mem_ctx = talloc_new(mprMemCtx());
44 struct cli_credentials *creds;
45 struct libnet_context *ctx;
46 struct MprVar obj, mprCreds;
47 struct event_context *ev;
50 ejsSetErrorMsg(eid, "talloc_new() failed");
53 ev = event_context_find(event_mem_ctx);
55 ctx = libnet_context_init(ev);
56 /* IF we generated a new event context, it will be under here,
57 * and we need it to last as long as the libnet context, so
59 talloc_steal(ctx, event_mem_ctx);
61 if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
63 create the default credentials
65 creds = cli_credentials_init(ctx);
67 ejsSetErrorMsg(eid, "cli_credential_init() failed");
71 cli_credentials_set_conf(creds, global_loadparm);
72 cli_credentials_set_anonymous(creds);
74 mprCreds = mprCredentials(creds);
76 } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
78 get credential values from credentials object
80 mprCreds = *(argv[0]);
81 creds = (struct cli_credentials *)mprGetPtr(&mprCreds, "creds");
83 ejsSetErrorMsg(eid, "invalid credentials parameter");
89 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
94 /* setup libnet_context credentials */
97 /* create the NetContext object */
98 obj = mprObject("NetContext");
100 /* add internal libnet_context pointer to the NetContext object */
101 mprSetPtrChild(&obj, "ctx", ctx);
103 /* add properties publicly available from js code */
104 mprCreateProperty(&obj, "credentials", &mprCreds);
106 /* add methods to the object */
107 mprSetCFunction(&obj, "UserMgr", ejs_net_userman);
108 mprSetCFunction(&obj, "HostMgr", ejs_net_hostman);
109 mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain);
110 mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb);
112 /* return the object */
113 mpr_Return(eid, obj);
119 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
122 struct libnet_context *ctx;
123 struct libnet_Join *join;
125 ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
126 mem_ctx = talloc_new(mprMemCtx());
128 join = talloc(mem_ctx, struct libnet_Join);
130 talloc_free(mem_ctx);
134 /* prepare parameters for the join */
135 join->in.netbios_name = NULL;
136 join->in.join_type = SEC_CHAN_WKSTA;
137 join->in.domain_name = cli_credentials_get_domain(ctx->cred);
138 join->in.level = LIBNET_JOIN_AUTOMATIC;
139 join->out.error_string = NULL;
141 if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
142 MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL);
143 MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL);
144 MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL);
146 join->in.netbios_name = mprToString(netbios_name);
149 join->in.domain_name = mprToString(domain_name);
152 join->in.join_type = mprToInt(join_type);
156 if (!join->in.domain_name) {
157 ejsSetErrorMsg(eid, "a domain must be specified for to join");
158 talloc_free(mem_ctx);
162 /* do the domain join */
163 status = libnet_Join(ctx, join, join);
165 if (!NT_STATUS_IS_OK(status)) {
166 MprVar error_string = mprString(join->out.error_string);
168 mprSetPropertyValue(argv[0], "error_string", error_string);
169 mpr_Return(eid, mprCreateBoolVar(false));
171 mpr_Return(eid, mprCreateBoolVar(true));
173 talloc_free(mem_ctx);
178 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
181 struct libnet_context *ctx;
182 struct libnet_samsync_ldb *samsync;
184 ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
185 mem_ctx = talloc_new(mprMemCtx());
187 samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
189 talloc_free(mem_ctx);
193 /* prepare parameters for the samsync */
194 samsync->in.machine_account = NULL;
195 samsync->in.session_info = NULL;
196 samsync->in.binding_string = NULL;
197 samsync->out.error_string = NULL;
199 if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
200 MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL);
201 MprVar *session_info = mprGetProperty(argv[0], "session_info", NULL);
203 samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
206 samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
210 /* do the domain samsync */
211 status = libnet_samsync_ldb(ctx, samsync, samsync);
213 if (!NT_STATUS_IS_OK(status)) {
214 MprVar error_string = mprString(samsync->out.error_string);
216 mprSetPropertyValue(argv[0], "error_string", error_string);
217 mpr_Return(eid, mprCreateBoolVar(false));
219 mpr_Return(eid, mprCreateBoolVar(true));
221 talloc_free(mem_ctx);
226 NTSTATUS smb_setup_ejs_net(void)
228 ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);