r23792: convert Samba4 to GPLv3
[jelmer/samba4-debian.git] / source / scripting / ejs / ejsnet / net_host.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
23 #include "includes.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "libnet/libnet.h"
26 #include "scripting/ejs/ejsnet/proto.h"
27 #include "scripting/ejs/smbcalls.h"
28 #include "events/events.h"
29 #include "auth/credentials/credentials.h"
30
31
32 static int ejs_net_domainlist(MprVarHandle eid, int argc, char **argv);
33
34
35 /*
36   Usage:
37   hostCtx = net.HostMgr(hostname = <default from credentials>)
38 */
39 int ejs_net_hostman(MprVarHandle eid, int argc, struct MprVar** argv)
40 {
41         struct libnet_context *ctx;
42         const char *hostname;
43         struct MprVar obj;
44
45         /* libnet context */
46         ctx = mprGetThisPtr(eid, "ctx");
47         if (ctx == NULL) {
48                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
49                 return 0;
50         }
51
52         /* fetch the arguments: host name */
53         if (argc == 0) {
54                 /* default host (machine) name is supplied in credentials */
55                 hostname = cli_credentials_get_workstation(ctx->cred);
56
57         } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
58                 /* host name has been specified */
59                 hostname = mprToString(argv[0]);
60
61         } else {
62                 ejsSetErrorMsg(eid, "too many arguments");
63                 return 0;
64         }
65
66         /* create the NetHostCtx object */
67         obj = mprObject("NetHostCtx");
68         
69         /* create a copy of the string for the object */
70         hostname = talloc_strdup(ctx, hostname);
71
72         /* add internal libnet_context pointer to the NetHostCtx object */
73         mprSetPtrChild(&obj, "ctx", ctx);
74         mprSetPtrChild(&obj, "hostname", hostname);
75         
76         /* add methods to the object */
77         mprSetStringCFunction(&obj, "DomainList", ejs_net_domainlist);
78
79         /* set the object returned by this function */
80         mpr_Return(eid, obj);
81
82         return 0;
83 }
84
85
86 static int ejs_net_domainlist(MprVarHandle eid, int argc, char **argv)
87 {
88         NTSTATUS status;
89         TALLOC_CTX *mem_ctx;
90         const char* hostname;
91         struct libnet_context *ctx;
92         struct libnet_DomainList req;
93         struct MprVar mprDomains;
94
95         mem_ctx = talloc_new(mprMemCtx());
96         if (mem_ctx == NULL) {
97                 ejsSetErrorMsg(eid, "could not create memory context - out of memory");
98                 goto done;
99         }
100
101         /* libnet context */
102         ctx = mprGetThisPtr(eid, "ctx");
103         if (ctx == NULL) {
104                 ejsSetErrorMsg(eid, "ctx property returns null pointer");
105                 goto done;
106         }
107
108         hostname = mprGetThisPtr(eid, "hostname");
109         if (hostname == NULL) {
110                 ejsSetErrorMsg(eid, "hostname property returns null pointer");
111                 goto done;
112         }
113
114         /* call the libnet function */
115         req.in.hostname = hostname;
116         
117         status = libnet_DomainList(ctx, mem_ctx, &req);
118         mprDomains = mprDomainsList(mem_ctx, &req, status);
119
120 done:
121         talloc_free(mem_ctx);
122         mpr_Return(eid, mprDomains);
123         return 0;
124 }