summaryrefslogtreecommitdiff
path: root/source4/scripting/ejs/ejsnet/net_host.c
blob: 478da19db3c7f984799401dc076e1aa80354df2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 
   Unix SMB/CIFS implementation.

   provides interfaces to libnet calls from ejs scripts

   Copyright (C) Rafal Szczesniak  2005-2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "includes.h"
#include "lib/appweb/ejs/ejs.h"
#include "libnet/libnet.h"
#include "scripting/ejs/ejsnet/proto.h"
#include "scripting/ejs/smbcalls.h"
#include "events/events.h"
#include "auth/credentials/credentials.h"


static int ejs_net_domainlist(MprVarHandle eid, int argc, char **argv);


/*
  Usage:
  hostCtx = net.HostMgr(hostname = <default from credentials>)
*/
int ejs_net_hostman(MprVarHandle eid, int argc, struct MprVar** argv)
{
	struct libnet_context *ctx;
	const char *hostname;
	struct MprVar obj;

	/* libnet context */
	ctx = mprGetThisPtr(eid, "ctx");
	if (ctx == NULL) {
		ejsSetErrorMsg(eid, "ctx property returns null pointer");
		return 0;
	}

	/* fetch the arguments: host name */
	if (argc == 0) {
		/* default host (machine) name is supplied in credentials */
		hostname = cli_credentials_get_workstation(ctx->cred);

	} else if (argc == 1 && mprVarIsString(argv[0]->type)) {
		/* host name has been specified */
		hostname = mprToString(argv[0]);

	} else {
		ejsSetErrorMsg(eid, "too many arguments");
		return 0;
	}

	/* create the NetHostCtx object */
	obj = mprObject("NetHostCtx");
	
	/* create a copy of the string for the object */
	hostname = talloc_strdup(ctx, hostname);

	/* add internal libnet_context pointer to the NetHostCtx object */
	mprSetPtrChild(&obj, "ctx", ctx);
	mprSetPtrChild(&obj, "hostname", hostname);
	
	/* add methods to the object */
	mprSetStringCFunction(&obj, "DomainList", ejs_net_domainlist);

	/* set the object returned by this function */
	mpr_Return(eid, obj);

	return 0;
}


static int ejs_net_domainlist(MprVarHandle eid, int argc, char **argv)
{
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	const char* hostname;
	struct libnet_context *ctx;
	struct libnet_DomainList req;
	struct MprVar mprDomains;

	mem_ctx = talloc_new(mprMemCtx());
	if (mem_ctx == NULL) {
		ejsSetErrorMsg(eid, "could not create memory context - out of memory");
		goto done;
	}

	/* libnet context */
	ctx = mprGetThisPtr(eid, "ctx");
	if (ctx == NULL) {
		ejsSetErrorMsg(eid, "ctx property returns null pointer");
		goto done;
	}

	hostname = mprGetThisPtr(eid, "hostname");
	if (hostname == NULL) {
		ejsSetErrorMsg(eid, "hostname property returns null pointer");
		goto done;
	}

	/* call the libnet function */
	req.in.hostname = hostname;
	
	status = libnet_DomainList(ctx, mem_ctx, &req);
	mprDomains = mprDomainsList(mem_ctx, &req, status);

done:
	talloc_free(mem_ctx);
	mpr_Return(eid, mprDomains);
	return 0;
}