summaryrefslogtreecommitdiff
path: root/source3/rpc_parse/parse_epmapper.c
blob: 7a5f147c507dd16d51ae4c9757a2fbc33c11d471 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* 
   Unix SMB/CIFS implementation.
   Samba end point mapper functions
   Copyright (C) Jim McDonough (jmcd@us.ibm.com)     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"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_PARSE

static uint32 internal_referent_id = 0;


/*******************************************************************
 Reads or writes a handle.
********************************************************************/
BOOL epm_io_handle(const char *desc, EPM_HANDLE *handle, prs_struct *ps,
		   int depth)
{
	if (!prs_align(ps))
		return False;

	if (!prs_uint8s(False, "data", ps, depth, handle->data, 
			sizeof(handle->data)))
		return False;

	return True;
}

/*******************************************************************
 inits an EPM_FLOOR structure.
********************************************************************/
NTSTATUS init_epm_floor(EPM_FLOOR *floor, uint8 protocol)
{
	/* handle lhs */
	floor->lhs.protocol = protocol;
	floor->lhs.length = sizeof(floor->lhs.protocol);

	switch(floor->lhs.protocol) {
	case EPM_FLOOR_UUID:
		floor->lhs.length += sizeof(floor->lhs.uuid.uuid);
		floor->lhs.length += sizeof(floor->lhs.uuid.version);
		break;
	default:
		break;
	}

	/* handle rhs */
	switch(floor->lhs.protocol) {
	case EPM_FLOOR_RPC:
	case EPM_FLOOR_UUID:
		floor->rhs.length = sizeof(floor->rhs.unknown);
		break;
	case EPM_FLOOR_TCP:
		floor->rhs.length = sizeof(floor->rhs.tcp.port);
		break;
	case EPM_FLOOR_IP:
		floor->rhs.length = sizeof(floor->rhs.ip.addr);
		break;
	case EPM_FLOOR_NMPIPES:
	case EPM_FLOOR_LRPC:
	case EPM_FLOOR_NETBIOS:
		floor->rhs.length = strlen(floor->rhs.string) + 1;
		break;
	default:
		break;
	}

	return NT_STATUS_OK;
}
	
/*******************************************************************
 inits an EPM_FLOOR structure with a UUID
********************************************************************/
NTSTATUS init_epm_floor_uuid(EPM_FLOOR *floor,
			     const RPC_UUID *uuid, uint16 version)
{
	memcpy(&floor->lhs.uuid.uuid, uuid, sizeof(*uuid));
	floor->lhs.uuid.version = version;
	floor->rhs.unknown = 0;
	return init_epm_floor(floor, EPM_FLOOR_UUID);
}

/*******************************************************************
 inits an EPM_FLOOR structure for RPC
********************************************************************/
NTSTATUS init_epm_floor_rpc(EPM_FLOOR *floor)
{
	floor->rhs.unknown = 0;
	return init_epm_floor(floor, EPM_FLOOR_RPC);
}

/*******************************************************************
 inits an EPM_FLOOR structure for TCP
********************************************************************/
NTSTATUS init_epm_floor_tcp(EPM_FLOOR *floor, uint16 port)
{
	floor->rhs.tcp.port = htons(port);
	return init_epm_floor(floor, EPM_FLOOR_TCP);
}

/*******************************************************************
 inits an EPM_FLOOR structure for IP
********************************************************************/
NTSTATUS init_epm_floor_ip(EPM_FLOOR *floor, uint8 addr[4])
{
	memcpy(&floor->rhs.ip.addr, addr, sizeof(addr));
	return init_epm_floor(floor, EPM_FLOOR_IP);
}

/*******************************************************************
 inits an EPM_FLOOR structure for named pipe
********************************************************************/
NTSTATUS init_epm_floor_np(EPM_FLOOR *floor, const char *pipe_name)
{
	safe_strcpy(floor->rhs.string, pipe_name, sizeof(floor->rhs.string)-1);
	return init_epm_floor(floor, EPM_FLOOR_NMPIPES);
}

/*******************************************************************
 inits an EPM_FLOOR structure for named pipe
********************************************************************/
NTSTATUS init_epm_floor_lrpc(EPM_FLOOR *floor, const char *pipe_name)
{
	safe_strcpy(floor->rhs.string, pipe_name, sizeof(floor->rhs.string)-1);
	return init_epm_floor(floor, EPM_FLOOR_LRPC);
}

/*******************************************************************
 inits an EPM_FLOOR structure for named pipe
********************************************************************/
NTSTATUS init_epm_floor_nb(EPM_FLOOR *floor, char *host_name)
{
	safe_strcpy(floor->rhs.string, host_name, sizeof(floor->rhs.string)-1);
	return init_epm_floor(floor, EPM_FLOOR_NETBIOS);
}

/*******************************************************************
 reads and writes EPM_FLOOR.
********************************************************************/
BOOL epm_io_floor(const char *desc, EPM_FLOOR *floor,
		  prs_struct *ps, int depth)
{
	prs_debug(ps, depth, desc, "epm_io_floor");
	depth++;

	if (!prs_uint16("lhs_length", ps, depth, &floor->lhs.length))
		return False;
	if (!prs_uint8("protocol", ps, depth, &floor->lhs.protocol))
		return False;

	switch (floor->lhs.protocol) {
	case EPM_FLOOR_UUID:
		if (!smb_io_rpc_uuid("uuid", &floor->lhs.uuid.uuid, ps, depth))
			return False;
		if (!prs_uint16("version", ps, depth, 
				&floor->lhs.uuid.version))
			return False;
		break;
	}

	if (!prs_uint16("rhs_length", ps, depth, &floor->rhs.length))
		return False;

	switch (floor->lhs.protocol) {
	case EPM_FLOOR_UUID:
	case EPM_FLOOR_RPC:
		if (!prs_uint16("unknown", ps, depth, &floor->rhs.unknown))
			return False;
		break;
	case EPM_FLOOR_TCP:
		if (!prs_uint16("tcp_port", ps, depth, &floor->rhs.tcp.port))
			return False;
		break;
	case EPM_FLOOR_IP:
		if (!prs_uint8s(False, "ip_addr", ps, depth, 
				floor->rhs.ip.addr,
				sizeof(floor->rhs.ip.addr)))
			return False;
		break;
	case EPM_FLOOR_NMPIPES:
	case EPM_FLOOR_LRPC:
	case EPM_FLOOR_NETBIOS:
		if (!prs_uint8s(False, "string", ps, depth,
				floor->rhs.string,
				floor->rhs.length))
			return False;
		break;
	default:
		break;
	}

	return True;
}

/*******************************************************************
 Inits a EPM_TOWER structure.
********************************************************************/
NTSTATUS init_epm_tower(TALLOC_CTX *ctx, EPM_TOWER *tower, 
			const EPM_FLOOR *floors, int num_floors)
{
	int size = 0;
	int i;

	DEBUG(5, ("init_epm_tower\n"));

	size += sizeof(uint16); /* number of floors is in tower length */
	for (i = 0; i < num_floors; i++) {
		size += (sizeof(uint16) * 2);
		size += floors[i].lhs.length;
		size += floors[i].rhs.length;
	}

	tower->max_length = tower->length = size;
	tower->num_floors = num_floors;
	tower->floors = talloc(ctx, sizeof(EPM_FLOOR) * num_floors);
	if (!tower->floors) {
		return NT_STATUS_NO_MEMORY;
	}
	memcpy(tower->floors, floors, sizeof(EPM_FLOOR) * num_floors);
	tower->unknown = 0x7e;

	return NT_STATUS_OK;
}

/*******************************************************************
 Reads or writes an EPM_TOWER structure.
********************************************************************/
BOOL epm_io_tower(const char *desc, EPM_TOWER *tower,
		  prs_struct *ps, int depth)
{
	int i;

	prs_debug(ps, depth, desc, "epm_io_tower");
	depth++;

	if (!prs_align(ps))
		return False;

	if (!prs_uint32("max_length", ps, depth, &tower->max_length))
		return False;
	if (!prs_uint32("length", ps, depth, &tower->length))
		return False;
	if (!prs_uint16("num_floors", ps, depth, &tower->num_floors))
		return False;

	if (UNMARSHALLING(ps)) {
		tower->floors = talloc(ps->mem_ctx,
				       sizeof(EPM_FLOOR) * tower->num_floors);
		if (!tower->floors)
			return False;
	}

	for (i = 0; i < tower->num_floors; i++) {
		if (!epm_io_floor("floor", tower->floors + i, ps, depth))
			return False;
	}

	return True;
}

/*******************************************************************
 Initialize an EPM_TOWER_ARRAY structure
********************************************************************/
NTSTATUS init_epm_tower_array(TALLOC_CTX *ctx, EPM_TOWER_ARRAY *array,
			      const EPM_TOWER *towers, int num_towers)
{
	int i;

	array->max_count = num_towers;
	array->offset = 0;
	array->count = num_towers;
	array->tower_ref_ids = talloc(ctx, sizeof(uint32) * num_towers);
	if (!array->tower_ref_ids) {
		return NT_STATUS_NO_MEMORY;
	}
	for (i=0;i<num_towers;i++)
		array->tower_ref_ids[i] = ++internal_referent_id;

	array->towers = talloc(ctx, sizeof(EPM_TOWER) * num_towers);
	if (!array->towers) {
		return NT_STATUS_NO_MEMORY;
	}
	memcpy(array->towers, towers, sizeof(EPM_TOWER) * num_towers);

	return NT_STATUS_OK;
}

/*******************************************************************
 Reads or writes an EPM_TOWER_ARRAY structure.
********************************************************************/
BOOL epm_io_tower_array(const char *desc, EPM_TOWER_ARRAY *array,
			prs_struct *ps, int depth)
{
	int i;

	prs_debug(ps, depth, desc, "epm_io_tower_array");
	depth++;

	if (!prs_uint32("max_count", ps, depth, &array->max_count))
		return False;
	if (!prs_uint32("offset", ps, depth, &array->offset))
		return False;
	if (!prs_uint32("count", ps, depth, &array->count))
		return False;


	if (UNMARSHALLING(ps)) {
		array->tower_ref_ids = talloc(ps->mem_ctx,
					      sizeof(uint32) * array->count);
		if (!array->tower_ref_ids) {
			return False;
		}
	}
	for (i=0; i < array->count; i++) {
		if (!prs_uint32("ref_id", ps, depth, &array->tower_ref_ids[i])) {
			return False;
		} else {
			if (array->tower_ref_ids[i] > internal_referent_id) {
				internal_referent_id = array->tower_ref_ids[i];
			}
		}
	}
			
			

	if (!prs_set_offset(ps, prs_offset(ps) + array->offset))
		return False;

	if (UNMARSHALLING(ps)) {
		array->towers = talloc(ps->mem_ctx,
				       sizeof(EPM_TOWER) * array->count);
		if (!array->towers) {
			return False;
		}
	}

	for (i = 0; i < array->count; i++) {
		if (!epm_io_tower("tower", &array->towers[i], ps, depth))
			return False;
	}

	return True;
}

/*******************************************************************
  Initialize EPM_R_MAP structure
******************************************************************/
NTSTATUS init_epm_r_map(TALLOC_CTX *ctx, EPM_R_MAP *r_map, 
			const EPM_HANDLE *handle, const EPM_TOWER_ARRAY *array,
			int num_elements, uint32 status)
{
	memcpy(&r_map->handle, handle, sizeof(*handle));
	r_map->num_results = num_elements;
	r_map->results = talloc(ctx, sizeof(EPM_TOWER_ARRAY) * num_elements);
	if (!r_map->results) {
		return NT_STATUS_NO_MEMORY;
	}
	memcpy(r_map->results, array, sizeof(EPM_TOWER_ARRAY) * num_elements);
	r_map->status = status;
	return NT_STATUS_OK;
}

/*************************************************************************
 Inits a EPM_Q_MAP structure.
**************************************************************************
* We attempt to hide the ugliness of the wire format by taking a EPM_TOWER
* array with a defined size 
**************************************************************************/
NTSTATUS init_epm_q_map(TALLOC_CTX *ctx, EPM_Q_MAP *q_map,
			const EPM_TOWER *towers, int num_towers)
{
	static uint32 handle = 1;

	ZERO_STRUCTP(q_map);

	DEBUG(5, ("init_epm_q_map\n"));
	q_map->handle.data[0] = (handle >>  0) & 0xFF;
	q_map->handle.data[1] = (handle >>  8) & 0xFF;
	q_map->handle.data[2] = (handle >> 16) & 0xFF;
	q_map->handle.data[3] = (handle >> 24) & 0xFF;

	q_map->tower = talloc(ctx, sizeof(EPM_TOWER) * (num_towers + 1));
	if (!q_map->tower) {
		return NT_STATUS_NO_MEMORY;
	}

	memcpy(q_map->tower, towers, sizeof(EPM_TOWER) * num_towers);

	ZERO_STRUCT(q_map->tower[num_towers]);

	/* For now let's not take more than 4 towers per result */
	q_map->max_towers = num_towers * 4;

	q_map->tower_ref_id = ++internal_referent_id;

	handle++;

	return NT_STATUS_OK;
}

/*****************************************************************
  epm_io_q_map - read or write EPM_Q_MAP structure
******************************************************************/
BOOL epm_io_q_map(const char *desc, EPM_Q_MAP *io_map, prs_struct *ps, 
		  int depth)
{
	prs_debug(ps, depth, desc, "epm_io_q_map");
	depth++;
	
	if (!epm_io_handle("handle", &io_map->handle, ps, depth))
		return False;

	if (!prs_uint32("referent_id", ps, 0, &io_map->tower_ref_id))
		return False;
	if (io_map->tower_ref_id > internal_referent_id)
		internal_referent_id = io_map->tower_ref_id;

	/* HACK: We need a more elegant way of doing this */
	if (UNMARSHALLING(ps)) {
		io_map->tower = talloc(ps->mem_ctx, sizeof(EPM_TOWER));
		if (!io_map->tower)
			return False;
	}		
	if (!epm_io_tower("tower", io_map->tower, ps, depth))
		return False;
	if (!epm_io_handle("term_handle", &io_map->term_handle, ps, depth))
		return False;

	if (!prs_uint32("max_towers", ps, 0, &io_map->max_towers))
		return False;

	return True;
}

/*******************************************************************
  epm_io_r_map - Read/Write EPM_R_MAP structure
******************************************************************/
BOOL epm_io_r_map(const char *desc, EPM_R_MAP *io_map,
		  prs_struct *ps, int depth)
{
	prs_debug(ps, depth, desc, "epm_io_r_map");
	depth++;

	if (!epm_io_handle("handle", &io_map->handle, ps, depth))
		return False;
	if (!prs_uint32("num_results", ps, depth, &io_map->num_results))
		return False;

	if (UNMARSHALLING(ps)) {
		io_map->results = talloc(ps->mem_ctx,
					 sizeof(EPM_TOWER_ARRAY) * 
					 io_map->num_results);
		if (!io_map->results)
			return False;
	}
	if (!epm_io_tower_array("results", io_map->results, ps, depth))
			return False;

	if (!prs_align(ps))
		return False;

	if (!prs_uint32("status", ps, depth, &io_map->status))
		return False;

	return True;
}