summaryrefslogtreecommitdiffstats
path: root/source/rpc_server/srv_pipe_srv.c
blob: 51efb9dd2043907a76976b5d3b2a4af60247761e (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

/* 
 *  Unix SMB/Netbios implementation.
 *  Version 1.9.
 *  RPC Pipe client / server routines
 *  Copyright (C) Andrew Tridgell              1992-2000
 *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
 *  Copyright (C) Paul Ashton                  1997-2000.
 *  
 *  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"
#include "rpc_parse.h"
#include "nterr.h"

extern int DEBUGLEVEL;

/*******************************************************************
turns a DCE/RPC response stream into a DCE/RPC reply
********************************************************************/
static BOOL create_rpc_reply(rpcsrv_struct *l, uint32 data_start,
				prs_struct *resp)
{
	BOOL ret;

	if (l->auth != NULL)
	{
		ret = l->auth->api_create_pdu(l, data_start, resp);
	}
	else
	{
		ret = False;
	}
	if ((!ret) || IS_BITS_SET_ALL(l->hdr.flags, RPC_FLG_LAST))
	{
		DEBUG(10,("create_rpc_reply: finished sending\n"));
		prs_free_data(&l->rdata);
	}
	return ret;
}


struct api_cmd
{
  char * pipe_clnt_name;
  char * pipe_srv_name;
  BOOL (*fn) (rpcsrv_struct *);
};

static struct api_cmd **api_fd_commands = NULL;
uint32 num_cmds = 0;

static void api_cmd_free(struct api_cmd *item)
{
	if (item != NULL)
	{
		if (item->pipe_clnt_name != NULL)
		{
			free(item->pipe_clnt_name);
		}
		if (item->pipe_srv_name != NULL)
		{
			free(item->pipe_srv_name);
		}
		free(item);
	}
}

static struct api_cmd *api_cmd_dup(const struct api_cmd *from)
{
	struct api_cmd *copy = NULL;
	if (from == NULL)
	{
		return NULL;
	}
	copy = (struct api_cmd *) malloc(sizeof(struct api_cmd));
	if (copy != NULL)
	{
		ZERO_STRUCTP(copy);
		if (from->pipe_clnt_name != NULL)
		{
			copy->pipe_clnt_name  = strdup(from->pipe_clnt_name );
		}
		if (from->pipe_srv_name != NULL)
		{
			copy->pipe_srv_name = strdup(from->pipe_srv_name);
		}
		if (from->fn != NULL)
		{
			copy->fn    = from->fn;
		}
	}
	return copy;
}

static void free_api_cmd_array(uint32 num_entries, struct api_cmd **entries)
{
	void(*fn)(void*) = (void(*)(void*))&api_cmd_free;
	free_void_array(num_entries, (void**)entries, *fn);
}

static struct api_cmd* add_api_cmd_to_array(uint32 *len,
				struct api_cmd ***array,
				const struct api_cmd *name)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&api_cmd_dup;
	return (struct api_cmd*)add_copy_to_array(len,
	                     (void***)array, (const void*)name, *fn, False);
}


void close_msrpc_command_processor(void)
{
	free_api_cmd_array(num_cmds, api_fd_commands);
}

void add_msrpc_command_processor(char* pipe_name,
				char* process_name,
				BOOL (*fn) (rpcsrv_struct *))
{
	struct api_cmd cmd;
	cmd.pipe_clnt_name = pipe_name;
	cmd.pipe_srv_name = process_name;
	cmd.fn = fn;

	add_api_cmd_to_array(&num_cmds, &api_fd_commands, &cmd);
}

static BOOL api_pipe_fault_resp(rpcsrv_struct *l, uint32 status,
				prs_struct *resp)
{
	prs_struct rhdr;
	prs_struct rfault;
	RPC_HDR_FAULT hdr_fault;
	RPC_HDR_RESP  hdr_resp;

	DEBUG(5,("api_pipe_fault_resp: make response\n"));

	if (l->auth_validated == False)
	{
		l->faulted_once_before = True;
	}

	prs_init(&(rhdr     ), 0, 4, False);
	prs_init(&(rfault   ), 0, 4, False);

	/***/
	/*** set up the header, response header and fault status ***/
	/***/

	hdr_fault.status   = status;
	hdr_fault.reserved = 0x0;

	hdr_resp.alloc_hint   = 0x0;
	hdr_resp.cancel_count = 0x0;
	hdr_resp.context_id   = 0x0;
	hdr_resp.reserved     = 0x0;

	make_rpc_hdr(&l->hdr, RPC_FAULT, RPC_FLG_NOCALL | RPC_FLG_FIRST | RPC_FLG_LAST,
	             l->hdr.call_id,
	             0x20,
	             0);

	smb_io_rpc_hdr      ("hdr"  , &(l->hdr      ), &(rhdr), 0);
	smb_io_rpc_hdr_resp ("resp" , &(hdr_resp ), &(rhdr), 0);
	smb_io_rpc_hdr_fault("fault", &(hdr_fault), &(rfault), 0);
	prs_realloc_data(&rhdr  , rhdr.offset  );
	prs_realloc_data(&rfault, rfault.offset);

	/***/
	/*** link rpc header and fault together ***/
	/***/

	prs_link(NULL    , &rhdr  , &rfault);
	prs_link(&rhdr, &rfault, NULL      );

	prs_init(resp, 0, 4, False);
	if (!prs_copy(resp, &rhdr)) return False;
	prs_free_data(&rfault);
	prs_free_data(&rhdr);

	return True;
}

static BOOL srv_pipe_bind_and_alt_req(rpcsrv_struct *l, 
				const char* ack_pipe_name,
				prs_struct *resp,
				enum RPC_PKT_TYPE pkt_type)
{
	BOOL ret;

	prs_struct rhdr;
	uint32 assoc_gid = l->key.pid;

	l->auth = NULL;

	/* decode the bind request */
	smb_io_rpc_hdr_rb("", &l->hdr_rb, &l->data_i, 0);

	if (l->data_i.offset == 0) return False;

	assoc_gid = l->hdr_rb.bba.assoc_gid;

	if (assoc_gid != 0)
	{
		l->key.pid = assoc_gid;
	}

	if (l->hdr.auth_len != 0)
	{
		RPC_HDR_AUTH  auth_info;
		BOOL found = False;
		int i;

		/* decode the authentication verifier */
		smb_io_rpc_hdr_auth    ("", &auth_info    , &l->data_i, 0);
		if (l->data_i.offset == 0) return False;

		for (i = 0; i < l->num_auths && !found; i++)
		{
			if (l->auth_fns[i]->api_is_auth(&auth_info,
			                                &l->auth_info))
			{
				l->auth = l->auth_fns[i];
				found = True;
			}
		}
		if (!found)
		{
			return False;
		}
	}
	else
	{
		extern srv_auth_fns noauth_fns;

		l->auth = &noauth_fns;
		l->auth_info = NULL;
		
		assoc_gid = l->hdr_rb.bba.assoc_gid;
		if (assoc_gid != 0)
		{
			l->key.pid = assoc_gid;
		}
	}

	if (l->auth != NULL)
	{
		if (!l->auth->api_auth_chk(l, pkt_type))
		{
			if (l->auth_info != NULL)
			{
				free(l->auth_info);
			}
			l->auth_info = NULL;
			return False;
		}
	}
	DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));

	prs_init(&(l->rdata), 0, 4, False);
	prs_init(&(rhdr    ), 0, 4, False);

	/***/
	/*** do the bind ack first ***/
	/***/

	make_rpc_hdr_ba(&l->hdr_ba,
	                l->hdr_rb.bba.max_tsize,
	                l->hdr_rb.bba.max_rsize,
	                assoc_gid,
	                ack_pipe_name,
	                0x1, 0x0, 0x0,
	                &(l->hdr_rb.transfer));

	smb_io_rpc_hdr_ba("", &l->hdr_ba, &l->rdata, 0);
	prs_realloc_data(&l->rdata, l->rdata.offset);

	if (l->auth != NULL)
	{
		/***/
		/*** now the authentication ***/
		/***/

		ret = l->auth->api_auth_gen(l, resp, pkt_type);

		if (!ret)
		{
			free(l->auth_info);
			l->auth_info = NULL;
			prs_free_data(&l->rdata);
			return False;
		}
	}
	else
	{
		return False;
	}

	return ret;
}

static BOOL api_pipe_bind_and_alt_req(rpcsrv_struct *l, 
				const char* name,
				prs_struct *resp,
				enum RPC_PKT_TYPE pkt_type)
{
	fstring ack_pipe_name;
	fstring pipe_srv_name;
	int i = 0;

	DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__));

	for (i = 0; i < num_cmds; i++)
	{
		if (strequal(api_fd_commands[i]->pipe_clnt_name, name) &&
		    api_fd_commands[i]->fn != NULL)
		{
			DEBUG(3,("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
			           api_fd_commands[i]->pipe_clnt_name,
			           api_fd_commands[i]->pipe_srv_name));
			fstrcpy(pipe_srv_name, api_fd_commands[i]->pipe_srv_name);
			break;
		}
	}

	if (api_fd_commands[i]->fn == NULL) return False;

	switch (pkt_type)
	{
		case RPC_BINDACK:
		{
			/* name has to be \PIPE\xxxxx */
			fstrcpy(ack_pipe_name, "\\PIPE\\");
			fstrcat(ack_pipe_name, pipe_srv_name);
			break;
		}
		case RPC_ALTCONTRESP:
		{
			/* secondary address CAN be NULL
			 * as the specs says it's ignored.
			 * It MUST NULL to have the spoolss working.
			 */
			fstrcpy(ack_pipe_name, "");
			break;
		}
		default:
		{
			return False;
		}
	}
	return srv_pipe_bind_and_alt_req(l, ack_pipe_name, resp, pkt_type);
}

/*
 * The RPC Alter-Context call is used only by the spoolss pipe
 * simply because there is a bug (?) in the MS unmarshalling code
 * or in the marshalling code. If it's in the later, then Samba
 * have the same bug.
 */
static BOOL api_pipe_bind_req(rpcsrv_struct *l, const char* name, prs_struct *resp)
{
	return api_pipe_bind_and_alt_req(l, name, resp, RPC_BINDACK);
}

static BOOL api_pipe_alt_req(rpcsrv_struct *l, const char* name, prs_struct *resp)
{
	return api_pipe_bind_and_alt_req(l, name, resp, RPC_ALTCONTRESP);
}

static BOOL api_pipe_request(rpcsrv_struct *l, const char* name,
				prs_struct *resp)
{
	int i = 0;

	if (l->auth != NULL && l->auth_validated)
	{
		DEBUG(10,("api_pipe_request: validated auth\n"));
		if (!l->auth->api_decode_pdu(l)) return False;
	}

	for (i = 0; i < num_cmds; i++)
	{
		if (strequal(api_fd_commands[i]->pipe_clnt_name, name) &&
		    api_fd_commands[i]->fn != NULL)
		{
			DEBUG(3,("Doing \\PIPE\\%s\n", api_fd_commands[i]->pipe_clnt_name));
			if (!api_fd_commands[i]->fn(l))
			{
				return False;
			}
			l->rdata_offset = 0;

			/* create the rpc pdu */
			return create_rpc_reply(l, 0, resp);

		}
	}
	return False;
}

static BOOL rpc_redir_local(rpcsrv_struct *l, prs_struct *req, prs_struct *resp,
				const char* name)
{
	BOOL reply = False;
	BOOL last;
	BOOL first;

	if (req->data == NULL || req->data_size == 0)
	{
		if (l->rdata.data == NULL)
		{
			return False;
		}
		/* hmm, must need some more data.
		 * create, flatten and return data in a single pdu
		 */
		if (!create_rpc_reply(l, l->rdata_offset, resp)) return False;

		return True;
	}

	if (req->data == NULL) return False;

	/* lkclXXXX still assume that the first complete PDU is always
	   in a single request!!!
	 */
	/* process the rpc header */
	req->offset = 0x0;
	req->io = True;
	smb_io_rpc_hdr("hdr", &l->hdr, req, 0);

	if (req->offset == 0) return False;

	last  = IS_BITS_SET_ALL(l->hdr.flags, RPC_FLG_LAST);
	first = IS_BITS_SET_ALL(l->hdr.flags, RPC_FLG_FIRST);

	if (l->hdr.pkt_type == RPC_BIND ||
	    l->hdr.pkt_type == RPC_BINDRESP)
	{
		last = True;
		first = True;
	}

	if (first)
	{
		prs_init(&l->data_i, 0, 4, True);
	}
	if (last)
	{
		prs_append_data(&l->data_i,
		                prs_data(req, req->offset),
		                req->data_size - req->offset);
	}
	else
	{
		prs_init(resp, 0, 4, False);
		return True;
	}

	/* previous authentication failure.  don't give a monkey's what
	 * is sent to us, we reject it, outright
	 */

	if (l->faulted_once_before)
	{
		DEBUG(10,("rpc_redir_local: faulted before (so do it again)\n"));
		prs_free_data(&l->data_i);		
		return api_pipe_fault_resp(l, 0x1c010002, resp);
	}

	switch (l->hdr.pkt_type)
	{
		case RPC_BIND   :
		{
			reply = api_pipe_bind_req(l, name, resp);
			break;
		}
		case RPC_ALTCONT:
		{
			reply = api_pipe_alt_req(l, name, resp);
 			break;
 		}
		case RPC_REQUEST:
		{
			if (l->auth != NULL && !l->auth_validated)
			{
				/* authentication _was_ requested
				   and it failed.  sorry, no deal!
				 */
				reply = False;
			}
			else
			{
				/* read the rpc header */
				reply = smb_io_rpc_hdr_req("req", &(l->hdr_req), &l->data_i, 0);
				if (reply)
				{
					if (l->hdr_req.context_id != 0)
					{
						l->key.vuid = l->hdr_req.context_id;
					}
					reply = become_vuser(&l->key) ||
					        become_guest();

				}
				if (reply)
				{	
					reply = api_pipe_request(l, name, resp);
				}
			}
			break;
		}
		case RPC_BINDRESP: /* not the real name! */
		{
			if (l->auth != NULL)
			{
				reply = l->auth->api_auth_chk(l,
				                  l->hdr.pkt_type);
			}
			if (!reply)
			{
				l->auth = NULL;
				if (l->auth_info != NULL)
				{
					free(l->auth_info);
					l->auth_info = NULL;
				}
				l->auth_validated = False;
			}
			break;
		}
	}

	if (!reply)
	{
		reply = api_pipe_fault_resp(l, 0x1c010002, resp);
	}
	
	if (reply)
	{
		/* flatten the data into a single pdu */
		DEBUG(200,("rpc_redir_local: %d\n", __LINE__));
		prs_debug_out(resp    , "redir_local resp", 200);

		return True;
	}

	/* delete intermediate data used to set up the pdu.  leave
	   rdata alone because that's got the rest of the data in it */
	prs_free_data(&l->data_i);		

	return reply;
}

/*******************************************************************
 receives a netlogon pipe and responds.
 ********************************************************************/
static BOOL api_rpc_command(rpcsrv_struct *l, const char *rpc_name,
			    const struct api_struct *api_rpc_cmds)
{
	int fn_num;
	DEBUG(4,("api_rpc_command: %s op 0x%x - ", rpc_name, l->hdr_req.opnum));

	for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++)
	{
		if (api_rpc_cmds[fn_num].opnum == l->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL)
		{
			DEBUG(3,("api_rpc_command: %s\n", api_rpc_cmds[fn_num].name));
			break;
		}
	}

	if (api_rpc_cmds[fn_num].name == NULL)
	{
		DEBUG(4, ("unknown\n"));
		return False;
	}

	prs_init(&l->rdata, 0, 4, False);

	/* do the actual command */
	api_rpc_cmds[fn_num].fn(l, &l->data_i, &(l->rdata));

	if (l->rdata.data == NULL || l->rdata.offset == 0)
	{
		prs_free_data(&l->rdata);
		return False;
	}

	prs_realloc_data(&l->rdata, l->rdata.offset);

	DEBUG(10,("called %s\n", rpc_name));

	return True;
}


/*******************************************************************
 receives a netlogon pipe and responds.
 ********************************************************************/
BOOL api_rpcTNP(rpcsrv_struct *l, const char *rpc_name,
		const struct api_struct *api_rpc_cmds)
{
	if (l == NULL)
	{
		DEBUG(1, ("NULL rpcsrv_struct\n"));
		return False;
	}
	if (l->data_i.data == NULL)
	{
		DEBUG(2,("%s: NULL data received\n", rpc_name));
		return False;
	}

	/* interpret the command */
	if (!api_rpc_command(l, rpc_name, api_rpc_cmds))
	{
		return False;
	}

	return True;
}

/*******************************************************************
 entry point from msrpc to smb.  adds data received to pdu; checks
 pdu; hands pdu off to msrpc, which gets a pdu back (except in the
 case of the RPC_BINDCONT pdu).
 ********************************************************************/
BOOL rpc_local(rpcsrv_struct *l, char *data, int len, const char *name)
{
	BOOL reply = False;

	DEBUG(10,("rpc_local: len %d\n", len));

	if (len != 0)
	{
		reply = prs_add_data(&l->smb_pdu, data, len);

		if (reply && is_complete_pdu(&l->smb_pdu))
		{
			l->smb_pdu.offset = l->smb_pdu.data_size;
			prs_link(NULL, &l->smb_pdu, NULL);
			reply = rpc_redir_local(l, &l->smb_pdu, &l->rsmb_pdu, name);
			prs_free_data(&l->smb_pdu);
			prs_init(&l->smb_pdu, 0, 4, True);
		}
	}
	else
	{
		if (l->rdata.data == NULL || l->rdata.data_size == 0)
		{
			DEBUG(10,("rpc_local: no data to send\n"));
			return True;
		}
		prs_free_data(&l->smb_pdu);
		prs_init(&l->smb_pdu, 0, 4, True);
		reply = rpc_redir_local(l, &l->smb_pdu, &l->rsmb_pdu, name);
	}
	return reply;
}