r25554: Convert last instances of BOOL, True and False to the standard types.
[bbaumbach/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
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"
28
29
30 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar** argv);
31 int ejs_net_hostman(MprVarHandle eid, int argc, struct MprVar** argv);
32
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);
35
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, mprCreds;
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
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
58          * make it a child */
59         talloc_steal(ctx, event_mem_ctx);
60
61         if (argc == 0 || (argc == 1 && argv[0]->type == MPR_TYPE_NULL)) {
62                 /* 
63                    create the default credentials
64                 */
65                 creds = cli_credentials_init(ctx);
66                 if (creds == NULL) {
67                         ejsSetErrorMsg(eid, "cli_credential_init() failed");
68                         talloc_free(ctx);
69                         return -1;
70                 }
71                 cli_credentials_set_conf(creds, global_loadparm);
72                 cli_credentials_set_anonymous(creds);
73
74                 mprCreds = mprCredentials(creds);
75
76         } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) {
77                 /*
78                   get credential values from credentials object
79                 */
80                 mprCreds = *(argv[0]);
81                 creds = (struct cli_credentials *)mprGetPtr(&mprCreds, "creds");
82                 if (creds == NULL) {
83                         ejsSetErrorMsg(eid, "invalid credentials parameter");
84                         talloc_free(ctx);
85                         return -1;
86                 }
87
88         } else {
89                 ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object.");
90                 talloc_free(ctx);
91                 return -1;
92         }
93         
94         /* setup libnet_context credentials */
95         ctx->cred = creds;
96
97         /* create the NetContext object */
98         obj = mprObject("NetContext");
99
100         /* add internal libnet_context pointer to the NetContext object */
101         mprSetPtrChild(&obj, "ctx", ctx);
102
103         /* add properties publicly available from js code */
104         mprCreateProperty(&obj, "credentials", &mprCreds);
105         
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);
111
112         /* return the object */
113         mpr_Return(eid, obj);
114
115         return 0;
116 }
117
118
119 static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv)
120 {
121         TALLOC_CTX *mem_ctx;
122         struct libnet_context *ctx;
123         struct libnet_Join *join;
124         NTSTATUS status;
125         ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
126         mem_ctx = talloc_new(mprMemCtx());
127
128         join = talloc(mem_ctx, struct libnet_Join);
129         if (!join) {
130                 talloc_free(mem_ctx);
131                 return -1;
132         }
133
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;
140
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);
145                 if (netbios_name) {
146                         join->in.netbios_name = mprToString(netbios_name);
147                 }
148                 if (domain_name) {
149                         join->in.domain_name = mprToString(domain_name);
150                 }
151                 if (join_type) {
152                         join->in.join_type = mprToInt(join_type);
153                 }
154         }
155
156         if (!join->in.domain_name) {
157                 ejsSetErrorMsg(eid, "a domain must be specified for to join");
158                 talloc_free(mem_ctx);
159                 return -1;
160         }
161
162         /* do the domain join */
163         status = libnet_Join(ctx, join, join);
164         
165         if (!NT_STATUS_IS_OK(status)) {
166                 MprVar error_string = mprString(join->out.error_string);
167                 
168                 mprSetPropertyValue(argv[0], "error_string", error_string);
169                 mpr_Return(eid, mprCreateBoolVar(false));
170         } else {
171                 mpr_Return(eid, mprCreateBoolVar(true));
172         }
173         talloc_free(mem_ctx);
174         return 0;
175 }
176
177
178 static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv)
179 {
180         TALLOC_CTX *mem_ctx;
181         struct libnet_context *ctx;
182         struct libnet_samsync_ldb *samsync;
183         NTSTATUS status;
184         ctx = (struct libnet_context *)mprGetThisPtr(eid, "ctx");
185         mem_ctx = talloc_new(mprMemCtx());
186
187         samsync = talloc(mem_ctx, struct libnet_samsync_ldb);
188         if (!samsync) {
189                 talloc_free(mem_ctx);
190                 return -1;
191         }
192
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;
198
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);
202                 if (credentials) {
203                         samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials);
204                 }
205                 if (session_info) {
206                         samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info);
207                 }
208         }
209
210         /* do the domain samsync */
211         status = libnet_samsync_ldb(ctx, samsync, samsync);
212         
213         if (!NT_STATUS_IS_OK(status)) {
214                 MprVar error_string = mprString(samsync->out.error_string);
215                 
216                 mprSetPropertyValue(argv[0], "error_string", error_string);
217                 mpr_Return(eid, mprCreateBoolVar(false));
218         } else {
219                 mpr_Return(eid, mprCreateBoolVar(true));
220         }
221         talloc_free(mem_ctx);
222         return 0;
223 }
224
225
226 NTSTATUS smb_setup_ejs_net(void)
227 {
228         ejsDefineCFunction(-1, "NetContext", ejs_net_context, NULL, MPR_VAR_SCRIPT_HANDLE);
229         return NT_STATUS_OK;
230 }