summaryrefslogtreecommitdiff
path: root/source4/smb_server/smb_server.h
blob: d38543bb67ac1a5897f5c6902275829d645be13b (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
/* 
   Unix SMB/CIFS implementation.
   
   Copyright (C) Andrew Tridgell              2003
   Copyright (C) James J Myers 		      2003 <myersjj@samba.org>
   Copyright (C) Stefan Metzmacher            2004-2005
   
   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 "libcli/raw/request.h"
#include "libcli/raw/interfaces.h"
#include "lib/events/events.h"
#include "lib/socket/socket.h"

/*
  this header declares the core context structures associated with smb
  sockets, tree connects, requests etc

  the idea is that we will eventually get rid of all our global
  variables and instead store our state from structures hanging off
  these basic elements
*/

struct smbsrv_tcons_context {
	/* an id tree used to allocate tids */
	struct idr_context *idtree_tid;

	/* this is the limit of vuid values for this connection */
	uint32_t idtree_limit;

	/* list of open tree connects */
	struct smbsrv_tcon *list;
};

struct smbsrv_sessions_context {
	/* an id tree used to allocate vuids */
	/* this holds info on session vuids that are already
	 * validated for this VC */
	struct idr_context *idtree_vuid;

	/* this is the limit of vuid values for this connection */
	uint64_t idtree_limit;

	/* also kept as a link list so it can be enumerated by
	   the management code */
	struct smbsrv_session *list;
};

struct smbsrv_handles_context {
	/* an id tree used to allocate file handles */
	struct idr_context *idtree_hid;

	/* this is the limit of handle values for this context */
	uint64_t idtree_limit;

	/* also kept as a link list so it can be enumerated by
	   the management code */
	struct smbsrv_handle *list;
};

/* the current user context for a request */
struct smbsrv_session {
	struct smbsrv_session *prev, *next;

	struct smbsrv_connection *smb_conn;

	/*
	 * in SMB2 tcons belong to just one session
	 * and not to the whole connection
	 */
	struct smbsrv_tcons_context smb2_tcons;

	/*
	 * the open file handles for this session,
	 * used for SMBexit, SMBulogoff and SMB2 SessionLogoff
	 */
	struct smbsrv_handle_session_item *handles;

	/* 
	 * an index passed over the wire:
	 * - 16 bit for smb
	 * - 64 bit for smb2
	 */
	uint64_t vuid;

	struct gensec_security *gensec_ctx;

	struct auth_session_info *session_info;

	/* some statistics for the management tools */
	struct {
		/* the time when the session setup started */
		struct timeval connect_time;
		/* the time when the session setup was finished */
		struct timeval auth_time;
		/* the time when the last request comes in */
		struct timeval last_request_time;
	} statistics;
};

/* we need a forward declaration of the ntvfs_ops strucutre to prevent
   include recursion */
struct ntvfs_context;

struct smbsrv_tcon {
	struct smbsrv_tcon *next, *prev;

	/* the server context that this was created on */
	struct smbsrv_connection *smb_conn;

	/* the open file handles on this tcon */
	struct smbsrv_handles_context handles;

	/* 
	 * an index passed over the wire:
	 * - 16 bit for smb
	 * - 32 bit for smb2
	 */
	uint32_t tid; /* an index passed over the wire (the TID) */

	/* the share name */
	const char *share_name;

	/* the NTVFS context - see source/ntvfs/ for details */
	struct ntvfs_context *ntvfs;

	/* some stuff to support share level security */
	struct {
		/* in share level security we need to fake up a session */
		struct smbsrv_session *session;
	} sec_share;

	/* some stuff to support share level security */
	struct {
		/* in SMB2 a tcon always belongs to one session */
		struct smbsrv_session *session;
	} smb2;

	/* some statistics for the management tools */
	struct {
		/* the time when the tree connect started */
		struct timeval connect_time;
		/* the time when the last request comes in */
		struct timeval last_request_time;
	} statistics;
};

struct smbsrv_handle {
	struct smbsrv_handle *next, *prev;

	/* the tcon the handle belongs to */
	struct smbsrv_tcon *tcon;

	/* the session the handle was opened on */
	struct smbsrv_session *session;

	/* the smbpid used on the open, used for SMBexit */
	uint16_t smbpid;

	/*
	 * this is for adding the handle into a linked list
	 * on the smbsrv_session, we can't use *next,*prev
	 * for this because they're used for the linked list on the 
	 * smbsrv_tcon
	 */
	struct smbsrv_handle_session_item {
		struct smbsrv_handle_session_item *prev, *next;
		struct smbsrv_handle *handle;
	} session_item;

	/*
	 * the value passed over the wire
	 * - 16 bit for smb
	 * - 64 bit for smb2
	 *   Note: for SMB2 handles are 128 bit
	 *         we'll fill the 2nd 64 bit with:
	 *         - 32 bit TID
	 *         - 32 bit 0xFFFFFFFF
	 */
	uint64_t hid;

	/*
	 * the ntvfs handle passed to the ntvfs backend
	 */
	struct ntvfs_handle *ntvfs;

	/* some statistics for the management tools */
	struct {
		/* the time when the tree connect started */
		struct timeval open_time;
		/* the time when the last request comes in */
		struct timeval last_use_time;
	} statistics;
};

/* a set of flags to control handling of request structures */
#define SMBSRV_REQ_CONTROL_LARGE     (1<<1) /* allow replies larger than max_xmit */

#define SMBSRV_REQ_DEFAULT_STR_FLAGS(req) (((req)->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII)

/* the context for a single SMB request. This is passed to any request-context 
   functions */
struct smbsrv_request {
	/* the smbsrv_connection needs a list of requests queued for send */
	struct smbsrv_request *next, *prev;

	/* the server_context contains all context specific to this SMB socket */
	struct smbsrv_connection *smb_conn;

	/* conn is only set for operations that have a valid TID */
	struct smbsrv_tcon *tcon;

	/* the session context is derived from the vuid */
	struct smbsrv_session *session;

	/* a set of flags to control usage of the request. See SMBSRV_REQ_CONTROL_* */
	uint32_t control_flags;

	/* the system time when the request arrived */
	struct timeval request_time;

	/* a pointer to the per request union smb_* io structure */
	void *io_ptr;

	/* the ntvfs_request */
	struct ntvfs_request *ntvfs;

	/* Now the SMB specific stuff */

	/* the flags from the SMB request, in raw form (host byte order) */
	uint16_t flags2;

	/* this can contain a fnum from an earlier part of a chained
	 * message (such as an SMBOpenX), or -1 */
	int chained_fnum;

	/* how far through the chain of SMB commands have we gone? */
	unsigned chain_count;

	/* the sequence number for signing */
	uint64_t seq_num;

	struct request_buffer in;
	struct request_buffer out;
};

/* this contains variables that should be used in % substitutions for
 * smb.conf parameters */
struct substitute_context {
	char *remote_arch;

	/* our local netbios name, as give to us by the client */
	char *local_machine;

	/* the remote netbios name, as give to us by the client */
	char *remote_machine;

	/* the select remote protocol */
	char *remote_proto;	

	/* the name of the client as should be displayed in
	 * smbstatus. Can be an IP or a netbios name */
	char *client_name; 

	/* the username for %U */
	char *user_name;
};

/* Remote architectures we know about. */
enum remote_arch_types {RA_UNKNOWN, RA_WFWG, RA_OS2, RA_WIN95, RA_WINNT, RA_WIN2K, RA_WINXP, RA_SAMBA};

enum security_types {SEC_SHARE,SEC_USER};

/* smb server context structure. This should contain all the state
 * information associated with a SMB server connection 
 */
struct smbsrv_connection {
	/* context that has been negotiated between the client and server */
	struct {
		/* have we already done the NBT session establishment? */
		BOOL done_nbt_session;
	
		/* only one negprot per connection is allowed */
		BOOL done_negprot;
	
		/* multiple session setups are allowed, but some parameters are
		   ignored in any but the first */
		BOOL done_sesssetup;
		
		/* 
		 * Size of data we can send to client. Set
		 *  by the client for all protocols above CORE.
		 *  Set by us for CORE protocol.
		 */
		unsigned max_send; /* init to BUFFER_SIZE */
	
		/*
		 * Size of the data we can receive. Set by us.
		 * Can be modified by the max xmit parameter.
		 */
		unsigned max_recv; /* init to BUFFER_SIZE */
	
		/* a guess at the remote architecture. Try not to rely on this - in almost
		   all cases using these values is the wrong thing to do */
		enum remote_arch_types ra_type;
	
		/* the negotiatiated protocol */
		enum protocol_types protocol;
	
		/* authentication context for multi-part negprot */
		struct auth_context *auth_context;
	
		/* reference to the kerberos keytab, or machine trust account */
		struct cli_credentials *server_credentials;
	
		/* did we tell the client we support encrypted passwords? */
		BOOL encrypted_passwords;
	
		/* Did we choose SPNEGO, or perhaps raw NTLMSSP, or even no extended security at all? */
		const char *oid;
	
		/* client capabilities */
		uint32_t client_caps;
	
		/* the timezone we sent to the client */
		int zone_offset;

		/* NBT names only set when done_nbt_session is true */
		struct nbt_name *called_name;
		struct nbt_name *calling_name;
	} negotiate;

	/* the context associated with open tree connects on a smb socket, not for SMB2 */
	struct smbsrv_tcons_context smb_tcons;

	/* context associated with currently valid session setups */
	struct smbsrv_sessions_context sessions;

	/* the server_context holds a linked list of pending requests,
	 * this is used for blocking locks and requests blocked due to oplock
	 * break requests */
	struct _smbsrv_pending_request {
		struct _smbsrv_pending_request *next, *prev;
	
		/* the request itself - needs to be freed */
		struct smbsrv_request *request;
	} *requests;

	struct smb_signing_context signing;

	struct stream_connection *connection;

	/* this holds a partially received request */
	struct packet_context *packet;

	/* a list of partially received transaction requests */
	struct smbsrv_trans_partial {
		struct smbsrv_trans_partial *next, *prev;
		struct smbsrv_request *req;
		struct smb_trans2 *trans;
		uint8_t command;
	} *trans_partial;

	/* configuration parameters */
	struct {
		enum security_types security;
		BOOL nt_status_support;
	} config;

	/* some statictics for the management tools */
	struct {
		/* the time when the client connects */
		struct timeval connect_time;
		/* the time when the last request comes in */
		struct timeval last_request_time;
	} statistics;
};

#include "smb_server/smb_server_proto.h"
#include "smb_server/smb/smb_proto.h"

/* useful way of catching wct errors with file and line number */
#define SMBSRV_CHECK_WCT(req, wcount) do { \
	if ((req)->in.wct != (wcount)) { \
		DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", \
			 (req)->in.wct, __FILE__, __LINE__, wcount)); \
		smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror)); \
		return; \
	} \
} while (0)
	
/* useful wrapper for talloc with NO_MEMORY reply */
#define SMBSRV_TALLOC_IO_PTR(ptr, type) do { \
	ptr = talloc(req, type); \
	if (!ptr) { \
		smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
		return; \
	} \
	req->io_ptr = ptr; \
} while (0)

#define SMBSRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \
	req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \
					  req->session->session_info,\
					  SVAL(req->in.hdr,HDR_PID), \
					  SVAL(req->in.hdr,HDR_MID), \
					  req->request_time, \
					  req, send_fn, state); \
	if (!req->ntvfs) { \
		smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
		return; \
	} \
	(void)talloc_steal(req->tcon->ntvfs, req); \
	req->ntvfs->frontend_data.private_data = req; \
} while (0)

#define SMBSRV_CHECK_FILE_HANDLE(handle) do { \
	if (!handle) { \
		smbsrv_send_error(req, NT_STATUS_INVALID_HANDLE); \
		return; \
	} \
} while (0)

#define SMBSRV_CHECK_FILE_HANDLE_ERROR(handle, _status) do { \
	if (!handle) { \
		smbsrv_send_error(req, _status); \
		return; \
	} \
} while (0)

#define SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(handle) do { \
	if (!handle) { \
		return NT_STATUS_INVALID_HANDLE; \
	} \
} while (0)

/* 
   check if the backend wants to handle the request asynchronously.
   if it wants it handled synchronously then call the send function
   immediately
*/
#define SMBSRV_CALL_NTVFS_BACKEND(cmd) do { \
	req->ntvfs->async_states->status = cmd; \
	if (!(req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC)) { \
		req->ntvfs->async_states->send_fn(req->ntvfs); \
	} \
} while (0)

/* check req->ntvfs->async_states->status and if not OK then send an error reply */
#define SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \
	req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
	if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
		smbsrv_send_error(req, ntvfs->async_states->status); \
		return; \
	} \
} while (0)
#define SMBSRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \
	SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \
	ptr = talloc_get_type(req->io_ptr, type); \
} while (0)
#define SMBSRV_CHECK_ASYNC_STATUS_SIMPLE do { \
	req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
	if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
		smbsrv_send_error(req, ntvfs->async_states->status); \
		return; \
	} \
} while (0)
#define SMBSRV_CHECK_ASYNC_STATUS(ptr, type) do { \
	SMBSRV_CHECK_ASYNC_STATUS_SIMPLE; \
	ptr = talloc_get_type(req->io_ptr, type); \
} while (0)

/* zero out some reserved fields in a reply */
#define SMBSRV_VWV_RESERVED(start, count) memset(req->out.vwv + VWV(start), 0, (count)*2)