summaryrefslogtreecommitdiff
path: root/source4/librpc/rpc/dcerpc_util.c
blob: 335761e10fcb90dadebcb77bc0e29b230c5ceee7 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* 
   Unix SMB/CIFS implementation.

   dcerpc utility functions

   Copyright (C) Andrew Tridgell 2003
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

/*
  this ndr_size_* stuff should really be auto-generated ....
*/

static size_t ndr_size_epm_floor(struct epm_floor *fl)
{
	size_t ret = 5;
	if (fl->lhs.protocol == EPM_PROTOCOL_UUID) {
		ret += 18;
	} else {
		ret += fl->lhs.info.lhs_data.length;
	}
	ret += fl->rhs.rhs_data.length;
	return ret;
}

size_t ndr_size_epm_towers(struct epm_towers *towers)
{
	size_t ret = 2;
	int i;
	for (i=0;i<towers->num_floors;i++) {
		ret += ndr_size_epm_floor(&towers->floors[i]);
	}
	return ret;
}

/*
  work out what TCP port to use for a given interface on a given host
*/
NTSTATUS dcerpc_epm_map_tcp_port(const char *server, 
				 const char *uuid, unsigned version,
				 uint32 *port)
{
	struct dcerpc_pipe *p;
	NTSTATUS status;
	struct epm_Map r;
	struct policy_handle handle;
	GUID guid;
	struct epm_twr_t twr, *twr_r;

	if (strcasecmp(uuid, DCERPC_EPMAPPER_UUID) == 0 ||
	    strcasecmp(uuid, DCERPC_MGMT_UUID) == 0) {
		/* don't lookup epmapper via epmapper! */
		*port = EPMAPPER_PORT;
		return NT_STATUS_OK;
	}

	status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* we can use the pipes memory context here as we will have a short
	   lived connection */
	status = dcerpc_bind_byuuid(p, p->mem_ctx, 
				    DCERPC_EPMAPPER_UUID,
				    DCERPC_EPMAPPER_VERSION);
	if (!NT_STATUS_IS_OK(status)) {
		dcerpc_pipe_close(p);
		return status;
	}

	ZERO_STRUCT(handle);
	ZERO_STRUCT(guid);

	twr.towers.num_floors = 5;
	twr.towers.floors = talloc(p->mem_ctx, sizeof(twr.towers.floors[0]) * 5);

	/* what I'd like for christmas ... */

	/* an RPC interface ... */
	twr.towers.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
	GUID_from_string(uuid, &twr.towers.floors[0].lhs.info.uuid.uuid);
	twr.towers.floors[0].lhs.info.uuid.version = version;
	twr.towers.floors[0].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);

	/* encoded with NDR ... */
	twr.towers.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
	GUID_from_string(NDR_GUID, &twr.towers.floors[1].lhs.info.uuid.uuid);
	twr.towers.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
	twr.towers.floors[1].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);

	/* on an RPC connection ... */
	twr.towers.floors[2].lhs.protocol = EPM_PROTOCOL_RPC_C;
	twr.towers.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
	twr.towers.floors[2].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);

	/* on a TCP port ... */
	twr.towers.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
	twr.towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
	twr.towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);

	/* on an IP link ... */
	twr.towers.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
	twr.towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
	twr.towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 4);

	/* with some nice pretty paper around it of course */
	r.in.object = &guid;
	r.in.map_tower = &twr;
	r.in.entry_handle = &handle;
	r.in.max_towers = 1;
	r.out.entry_handle = &handle;

	status = dcerpc_epm_Map(p, p->mem_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		dcerpc_pipe_close(p);
		return status;
	}
	if (r.out.status != 0 || r.out.num_towers != 1) {
		dcerpc_pipe_close(p);
		return NT_STATUS_PORT_UNREACHABLE;
	}

	twr_r = r.out.towers[0].twr;
	if (!twr_r) {
		dcerpc_pipe_close(p);
		return NT_STATUS_PORT_UNREACHABLE;
	}

	if (twr_r->towers.num_floors != 5 ||
	    twr_r->towers.floors[3].lhs.protocol != twr.towers.floors[3].lhs.protocol ||
	    twr_r->towers.floors[3].rhs.rhs_data.length != 2) {
		dcerpc_pipe_close(p);
		return NT_STATUS_PORT_UNREACHABLE;
	}

	*port = RSVAL(twr_r->towers.floors[3].rhs.rhs_data.data, 0);

	dcerpc_pipe_close(p);

	return NT_STATUS_OK;
}


/*
  find the pipe name for a local IDL interface
*/
const char *idl_pipe_name(const char *uuid, uint32 if_version)
{
	int i;
	for (i=0;dcerpc_pipes[i];i++) {
		if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
		    dcerpc_pipes[i]->if_version == if_version) {
			return dcerpc_pipes[i]->name;
		}
	}
	return "UNKNOWN";
}

/*
  find the number of calls defined by local IDL
*/
int idl_num_calls(const char *uuid, uint32 if_version)
{
	int i;
	for (i=0;dcerpc_pipes[i];i++) {
		if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
		    dcerpc_pipes[i]->if_version == if_version) {
			return dcerpc_pipes[i]->num_calls;
		}
	}
	return -1;
}


/*
  find a dcerpc interface by name
*/
const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
{
	int i;
	for (i=0;dcerpc_pipes[i];i++) {
		if (strcasecmp(dcerpc_pipes[i]->name, name) == 0) {
			return dcerpc_pipes[i];
		}
	}
	return NULL;
}



/* 
   push a dcerpc_packet into a blob, potentially with auth info
*/
NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
			  struct dcerpc_packet *pkt,
			  struct dcerpc_auth *auth_info)
{
	NTSTATUS status;
	struct ndr_push *ndr;

	ndr = ndr_push_init_ctx(mem_ctx);
	if (!ndr) {
		return NT_STATUS_NO_MEMORY;
	}

	if (auth_info) {
		pkt->auth_length = auth_info->credentials.length;
	} else {
		pkt->auth_length = 0;
	}

	status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (auth_info) {
		status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
	}

	*blob = ndr_push_blob(ndr);

	/* fill in the frag length */
	SSVAL(blob->data, DCERPC_FRAG_LEN_OFFSET, blob->length);

	return NT_STATUS_OK;
}