summaryrefslogtreecommitdiff
path: root/source/msrpc/msrpcd_process.c
blob: f8dda0b5281fdff9cef2a8fb79afd03582ff3491 (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
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   process incoming packets - main loop
   Copyright (C) Andrew Tridgell 1992-1998
   
   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"

extern int DEBUGLEVEL;

/* 
 * Size of data we can send to client. Set
 *  by the client for all protocols above CORE.
 *  Set by us for CORE protocol.
 */
int max_send = BUFFER_SIZE;
/*
 * Size of the data we can receive. Set by us.
 * Can be modified by the max xmit parameter.
 */
int max_recv = BUFFER_SIZE;

extern int last_message;
extern pstring sesssetup_user;
extern int smb_read_error;
extern BOOL reload_after_sighup;
extern int max_send;


/****************************************************************************
  Do a select on an two fd's - with timeout. 

  If a local udp message has been pushed onto the
  queue (this can only happen during oplock break
  processing) return this first.

  If a pending smb message has been pushed onto the
  queue (this can only happen during oplock break
  processing) return this next.

  If the first smbfd is ready then read an smb from it.
  if the second (loopback UDP) fd is ready then read a message
  from it and setup the buffer header to identify the length
  and from address.
  Returns False on timeout or error.
  Else returns True.

The timeout is in milli seconds
****************************************************************************/

static BOOL receive_message_or_msrpc(int c, prs_struct * ps,
				     int timeout, BOOL *got_msrpc)
{
	fd_set fds;
	int selrtn;
	struct timeval to;
	int maxfd;

	smb_read_error = 0;

	*got_msrpc = False;

	/*
	 * Check to see if we already have a message on the smb queue.
	 * If so - copy and return it.
	 */

	/*
	 * Setup the select read fd set.
	 */

	FD_ZERO(&fds);
	FD_SET(c, &fds);
	maxfd = 0;

	to.tv_sec = timeout / 1000;
	to.tv_usec = (timeout % 1000) * 1000;

	selrtn =
		sys_select(MAX(maxfd, c) + 1, &fds, NULL,
			   timeout > 0 ? &to : NULL);

	/* Check if error */
	if (selrtn == -1)
	{
		/* something is wrong. Maybe the socket is dead? */
		smb_read_error = READ_ERROR;
		return False;
	}

	/* Did we timeout ? */
	if (selrtn == 0)
	{
		smb_read_error = READ_TIMEOUT;
		return False;
	}

	if (FD_ISSET(c, &fds))
	{
		*got_msrpc = True;
		return receive_msrpc(c, ps, 0);
	}
	return False;
}


/*
These flags determine some of the permissions required to do an operation 

Note that I don't set NEED_WRITE on some write operations because they
are used by some brain-dead clients when printing, and I don't want to
force write permissions on print services.
*/
#define AS_USER (1<<0)
#define NEED_WRITE (1<<1)
#define TIME_INIT (1<<2)
#define CAN_IPC (1<<3)
#define AS_GUEST (1<<5)
#define QUEUE_IN_OPLOCK (1<<6)

/* 
   define a list of possible SMB messages and their corresponding
   functions. Any message that has a NULL function is unimplemented -
   please feel free to contribute implementations!
*/


/****************************************************************************
  process an smb from the client - split out from the process() code so
  it can be used by the oplock break code.
****************************************************************************/
static void process_msrpc(rpcsrv_struct * l, const char *name,
			  prs_struct * pdu)
{
	static int trans_num;
	int32 len = prs_buf_len(pdu);

	DEBUG(6, ("got message of len 0x%x\n", len));

	dump_data(10, pdu->data, len);

#ifdef WITH_VTP
	if (trans_num == 1 && VT_Check(pdu->data))
	{
		VT_Process();
		return;
	}
#endif

	if (rpc_local(l, pdu->data, len, name) &&
	    msrpc_send(l->c, &l->rsmb_pdu))
	{
		prs_free_data(&l->rsmb_pdu);

		while (rpc_local(l, NULL, 0, name))
		{
			fd_set fds;
			int selrtn;
			struct timeval to;
			int maxfd;
			int timeout = SMBD_SELECT_LOOP * 1000;

			smb_read_error = 0;

			FD_ZERO(&fds);
			FD_SET(l->c, &fds);
			maxfd = 0;

			to.tv_sec = timeout / 1000;
			to.tv_usec = (timeout % 1000) * 1000;

			selrtn =
				sys_select(MAX(maxfd, l->c) + 1, NULL, &fds,
					   timeout > 0 ? &to : NULL);

			/* Check if error */
			if (selrtn == -1)
			{
				smb_read_error = READ_ERROR;
				return;
			}

			/* Did we timeout ? */
			if (selrtn == 0)
			{
				smb_read_error = READ_TIMEOUT;
				return;
			}

			if (FD_ISSET(l->c, &fds))
			{
				if (!msrpc_send(l->c, &l->rsmb_pdu))
					prs_free_data(&l->rsmb_pdu);
				break;
			}
			prs_free_data(&l->rsmb_pdu);
		}
	}
	trans_num++;
}

/****************************************************************************
 reads user credentials from the socket
****************************************************************************/
BOOL get_user_creds(int c, vuser_key * uk)
{
	pstring buf;
	int rl;
	uint32 len;
	BOOL new_con = False;
	uint32 status;

	CREDS_CMD cmd;
	prs_struct ps;

	ZERO_STRUCT(cmd);

	DEBUG(10, ("get_user_creds: first request\n"));

	rl = read(c, &buf, sizeof(len));

	if (rl != sizeof(len))
	{
		DEBUG(0, ("Unable to read length\n"));
		dump_data(0, buf, sizeof(len));
		return False;
	}

	len = IVAL(buf, 0);

	if (len > sizeof(buf))
	{
		DEBUG(0, ("length %d too long\n", len));
		return False;
	}

	rl = read(c, buf, len);

	if (rl < 0)
	{
		DEBUG(0, ("Unable to read from connection\n"));
		return False;
	}

#ifdef DEBUG_PASSWORD
	dump_data(100, buf, rl);
#endif

	/* make a static data parsing structure from the api_fd_reply data */
	prs_init(&ps, 0, 4, True);
	prs_add_data(&ps, buf, len);

	if (!creds_io_cmd("creds", &cmd, &ps, 0))
	{
		DEBUG(0, ("Unable to parse credentials\n"));
		prs_free_data(&ps);
		return False;
	}

	if (ps.offset != rl)
	{
		DEBUG(0, ("Buffer size %d %d!\n", ps.offset, rl));
		prs_free_data(&ps);
		return False;
	}

	prs_free_data(&ps);

	switch (cmd.command)
	{
		case AGENT_CMD_CON:
		case AGENT_CMD_CON_ANON:
		{
			new_con = True;
			break;
		}
		case AGENT_CMD_CON_REUSE:
		{
			new_con = True;
			break;
		}
		default:
		{
			DEBUG(0, ("unknown command %d\n", cmd.command));
			return False;
		}
	}

	/* obtain the remote process id and vuid */
	(*uk) = cmd.key;

	status = new_con ? 0x0 : 0x1;

	if (write(c, &status, sizeof(status)) != sizeof(status))
	{
		return False;
	}

	return new_con;
}

static void free_srv_auth_fns_array(uint32 num_entries,
				    srv_auth_fns ** entries)
{
	free_void_array(num_entries, (void **)entries, NULL);
}

static srv_auth_fns *add_srv_auth_fns_to_array(uint32 * len,
					       srv_auth_fns *** array,
					       srv_auth_fns * name)
{
	return (srv_auth_fns *) add_item_to_array(len,
						  (void ***)array,
						  (void *)name);
}

void close_srv_auth_array(rpcsrv_struct * l)
{
	free_srv_auth_fns_array(l->num_auths, l->auth_fns);
}

void add_srv_auth_fn(rpcsrv_struct * l, srv_auth_fns * fn)
{
	add_srv_auth_fns_to_array(&l->num_auths, &l->auth_fns, fn);
	DEBUG(10, ("add_srv_auth_fn: %d\n", l->num_auths));
}

/****************************************************************************
  initialise from pipe
****************************************************************************/
BOOL msrpcd_init(int c, rpcsrv_struct ** l)
{
	vuser_key uk;
	user_struct *vuser = NULL;

	if (!get_user_creds(c, &uk))
	{
		DEBUG(0, ("authentication failed\n"));
		return False;
	}

	if (uk.vuid != UID_FIELD_INVALID)
	{
		if (!become_vuser(&uk))
		{
			return False;
		}

		vuser = get_valid_user_struct(&uk);
		if (vuser == NULL)
		{
			return False;
		}
	}
	else
	{
		if (!become_guest())
		{
			return False;
		}
	}

	(*l) = malloc(sizeof(*(*l)));
	if ((*l) == NULL)
	{
		vuid_free_user_struct(vuser);
		return False;
	}

	ZERO_STRUCTP((*l));

	(*l)->key = uk;
	(*l)->c = c;

	if (vuser != NULL && !vuser->guest)
	{
		char *user = vuser->name;
		if (!strequal(user, lp_guestaccount(-1)) &&
		    lp_servicenumber(user) < 0)
		{
			int homes = lp_servicenumber(HOMES_NAME);
			char *home = get_unixhome_dir(user);
			if (homes >= 0 && home)
			{
				pstring home_dir;
				fstrcpy(home_dir, home);
				lp_add_home(user, homes, home_dir);
			}
		}
	}

	vuid_free_user_struct(vuser);

	return True;
}

/****************************************************************************
  process commands from the client
****************************************************************************/
void msrpcd_process(msrpc_service_fns * fn, rpcsrv_struct * l,
		    const char *name)
{
	extern fstring remote_machine;
	extern fstring local_machine;
	extern pstring global_myname;

	max_recv = MIN(lp_maxxmit(), BUFFER_SIZE);

	/* re-initialise the timezone */
	TimeInit();

	fstrcpy(remote_machine, name);
	fstrcpy(local_machine, global_myname);
	local_machine[15] = 0;
	strlower(local_machine);

	DEBUG(2, ("msrpc_process: client_name: %s my_name: %s\n",
		  remote_machine, local_machine));

	fn->reload_services(True);
	reopen_logs();

	while (True)
	{
		int counter;
		int service_load_counter = 0;
		BOOL got_msrpc = False;
		prs_struct pdu;

		errno = 0;

		for (counter = SMBD_SELECT_LOOP;
		     !receive_message_or_msrpc(l->c, &pdu,
					       SMBD_SELECT_LOOP * 1000,
					       &got_msrpc);
		     counter += SMBD_SELECT_LOOP)
		{
			time_t t;

			if (counter > 365 * 3600)	/* big number of seconds. */
			{
				counter = 0;
				service_load_counter = 0;
			}

			if (smb_read_error == READ_EOF)
			{
				DEBUG(3, ("end of file from client\n"));
				if (fn->idle != NULL)
				{
					fn->idle();
				}
				return;
			}

			if (smb_read_error == READ_ERROR)
			{
				DEBUG(3, ("receive error (%s) exiting\n",
					  strerror(errno)));
				if (fn->idle != NULL)
				{
					fn->idle();
				}
				return;
			}

			t = time(NULL);

			/* check for smb.conf reload */
			if (counter >=
			    service_load_counter + SMBD_RELOAD_CHECK)
			{
				service_load_counter = counter;

				/* reload services, if files have changed. */
				fn->reload_services(True);
			}

			/*
			 * If reload_after_sighup == True then we got a SIGHUP
			 * and are being asked to reload. Fix from <branko.cibej@hermes.si>
			 */

			if (reload_after_sighup)
			{
				DEBUG(0,
				      ("Reloading services after SIGHUP\n"));
				fn->reload_services(False);
				reload_after_sighup = False;
				/*
				 * Use this as an excuse to print some stats.
				 */
			}

			/* automatic timeout if all connections are closed */
			if (counter >= IDLE_CLOSED_TIMEOUT)
			{
				DEBUG(2, ("Closing idle connection\n"));
				if (fn->idle != NULL)
				{
					fn->idle();
				}
				return;
			}

		}

		if (got_msrpc)
		{
			process_msrpc(l, name, &pdu);
		}
		prs_free_data(&pdu);
	}
}