summaryrefslogtreecommitdiff
path: root/source4/build/pidl/server.pm
blob: 20a8c383ea339a16d98c451574c55bc709e2755d (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
###################################################
# server boilerplate generator
# Copyright tridge@samba.org 2003
# Copyright metze@samba.org 2004
# released under the GNU GPL

package IdlServer;

use strict;

my($res);

sub pidl($)
{
	$res .= shift;
}

#####################################################################
# produce boilerplate code for a interface
sub Boilerplate_Iface($)
{
	my($interface) = shift;
	my($data) = $interface->{DATA};
	my $count = 0;
	my $name = $interface->{NAME};
	my $uname = uc $name;

	foreach my $d (@{$data}) {
		if ($d->{TYPE} eq "FUNCTION") { $count++; }
	}

	if ($count == 0) {
		return;
	}

	pidl "static const dcesrv_dispatch_fn_t $name\_dispatch_table[] = {\n";
	foreach my $d (@{$data}) {
		if ($d->{TYPE} eq "FUNCTION") {
			pidl "\t(dcesrv_dispatch_fn_t)$d->{NAME},\n";
		}
	}
	pidl "\tNULL};\n\n";

	pidl "
static NTSTATUS $name\_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
{
	return NT_STATUS_OK;	
}

static void $name\_op_unbind(struct dcesrv_connection *dce_conn, const struct dcesrv_interface *iface)
{
	return;	
}

static NTSTATUS $name\_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
{
	uint16 opnum = dce_call->pkt.u.request.opnum;

	return $name\_dispatch_table[opnum](dce_call, mem_ctx, r);	
}

static const struct dcesrv_interface $name\_interface = {
	&dcerpc_table_$name,
	$name\_op_bind,
	$name\_op_unbind,
	$name\_op_dispatch
};

";
}

#####################################################################
# produce boilerplate code for an endpoint server
sub Boilerplate_Ep_Server($)
{
	my($interface) = shift;
	my($data) = $interface->{DATA};
	my $count = 0;
	my $name = $interface->{NAME};
	my $uname = uc $name;

	foreach my $d (@{$data}) {
		if ($d->{TYPE} eq "FUNCTION") { $count++; }
	}

	if ($count == 0) {
		return;
	}

	pidl "
static NTSTATUS $name\_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
{
	int i;

	for (i=0;i<$name\_interface.ndr->endpoints->count;i++) {
		NTSTATUS ret;
		const char *name = $name\_interface.ndr->endpoints->names[i];

		ret = dcesrv_interface_register(dce_ctx, name, &$name\_interface, NULL);
		if (!NT_STATUS_IS_OK(ret)) {
			DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
			return ret;
		}
	}

	return NT_STATUS_OK;
}

static BOOL $name\_op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32 if_version)
{
	if ($name\_interface.ndr->if_version == if_version &&
		strcmp($name\_interface.ndr->uuid, uuid)==0) {
		memcpy(iface,&$name\_interface, sizeof(*iface));
		return True;
	}

	return False;
}

static BOOL $name\_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
{
	if (strcmp($name\_interface.ndr->name, name)==0) {
		memcpy(iface,&$name\_interface, sizeof(*iface));
		return True;
	}

	return False;	
}
	
NTSTATUS dcerpc_$name\_init(void)
{
	NTSTATUS ret;
	struct dcesrv_endpoint_server ep_server;

	/* fill in our name */
	ep_server.name = \"$name\";

	/* fill in all the operations */
	ep_server.init_server = $name\_op_init_server;

	ep_server.interface_by_uuid = $name\_op_interface_by_uuid;
	ep_server.interface_by_name = $name\_op_interface_by_name;

	/* register ourselves with the NTVFS subsystem. */
	ret = register_backend(\"dcerpc\", &ep_server);

	if (!NT_STATUS_IS_OK(ret)) {
		DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
		return ret;
	}

	return ret;
}

";
}


#####################################################################
# parse a parsed IDL structure back into an IDL file
sub Parse($)
{
	my($idl) = shift;
	$res = "/* dcerpc server boilerplate generated by pidl */\n\n";
	foreach my $x (@{$idl}) {
		if ($x->{TYPE} eq "INTERFACE") { 
			Boilerplate_Iface($x);
			Boilerplate_Ep_Server($x);
		}
	}

	return $res;
}

1;