83a3f29ce5ff7c869aefe9bc0f10a088c72b1cf3
[metze/samba/wip.git] / source4 / smb_server / smb_server.h
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Andrew Tridgell              2003
5    Copyright (C) James J Myers                2003 <myersjj@samba.org>
6    Copyright (C) Stefan Metzmacher            2004-2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "libcli/raw/request.h"
24 #include "libcli/raw/interfaces.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "lib/util/dlinklist.h"
28
29 /*
30   this header declares the core context structures associated with smb
31   sockets, tree connects, requests etc
32
33   the idea is that we will eventually get rid of all our global
34   variables and instead store our state from structures hanging off
35   these basic elements
36 */
37
38 struct smbsrv_tcons_context {
39         /* an id tree used to allocate tids */
40         struct idr_context *idtree_tid;
41
42         /* this is the limit of vuid values for this connection */
43         uint32_t idtree_limit;
44
45         /* list of open tree connects */
46         struct smbsrv_tcon *list;
47 };
48
49 struct smbsrv_sessions_context {
50         /* an id tree used to allocate vuids */
51         /* this holds info on session vuids that are already
52          * validated for this VC */
53         struct idr_context *idtree_vuid;
54
55         /* this is the limit of vuid values for this connection */
56         uint64_t idtree_limit;
57
58         /* also kept as a link list so it can be enumerated by
59            the management code */
60         struct smbsrv_session *list;
61 };
62
63 struct smbsrv_handles_context {
64         /* an id tree used to allocate file handles */
65         struct idr_context *idtree_hid;
66
67         /* this is the limit of handle values for this context */
68         uint64_t idtree_limit;
69
70         /* also kept as a link list so it can be enumerated by
71            the management code */
72         struct smbsrv_handle *list;
73 };
74
75 /* the current user context for a request */
76 struct smbsrv_session {
77         struct smbsrv_session *prev, *next;
78
79         struct smbsrv_connection *smb_conn;
80
81         /*
82          * in SMB2 tcons belong to just one session
83          * and not to the whole connection
84          */
85         struct smbsrv_tcons_context smb2_tcons;
86
87         /*
88          * the open file handles for this session,
89          * used for SMBexit, SMBulogoff and SMB2 SessionLogoff
90          */
91         struct smbsrv_handle_session_item *handles;
92
93         /* 
94          * an index passed over the wire:
95          * - 16 bit for smb
96          * - 64 bit for smb2
97          */
98         uint64_t vuid;
99
100         struct gensec_security *gensec_ctx;
101
102         struct auth_session_info *session_info;
103
104         /* some statistics for the management tools */
105         struct {
106                 /* the time when the session setup started */
107                 struct timeval connect_time;
108                 /* the time when the session setup was finished */
109                 struct timeval auth_time;
110                 /* the time when the last request comes in */
111                 struct timeval last_request_time;
112         } statistics;
113 };
114
115 /* we need a forward declaration of the ntvfs_ops strucutre to prevent
116    include recursion */
117 struct ntvfs_context;
118
119 struct smbsrv_tcon {
120         struct smbsrv_tcon *next, *prev;
121
122         /* the server context that this was created on */
123         struct smbsrv_connection *smb_conn;
124
125         /* the open file handles on this tcon */
126         struct smbsrv_handles_context handles;
127
128         /* 
129          * an index passed over the wire:
130          * - 16 bit for smb
131          * - 32 bit for smb2
132          */
133         uint32_t tid; /* an index passed over the wire (the TID) */
134
135         /* the share name */
136         const char *share_name;
137
138         /* the NTVFS context - see source/ntvfs/ for details */
139         struct ntvfs_context *ntvfs;
140
141         /* some stuff to support share level security */
142         struct {
143                 /* in share level security we need to fake up a session */
144                 struct smbsrv_session *session;
145         } sec_share;
146
147         /* some stuff to support share level security */
148         struct {
149                 /* in SMB2 a tcon always belongs to one session */
150                 struct smbsrv_session *session;
151         } smb2;
152
153         /* some statistics for the management tools */
154         struct {
155                 /* the time when the tree connect started */
156                 struct timeval connect_time;
157                 /* the time when the last request comes in */
158                 struct timeval last_request_time;
159         } statistics;
160 };
161
162 struct smbsrv_handle {
163         struct smbsrv_handle *next, *prev;
164
165         /* the tcon the handle belongs to */
166         struct smbsrv_tcon *tcon;
167
168         /* the session the handle was opened on */
169         struct smbsrv_session *session;
170
171         /* the smbpid used on the open, used for SMBexit */
172         uint16_t smbpid;
173
174         /*
175          * this is for adding the handle into a linked list
176          * on the smbsrv_session, we can't use *next,*prev
177          * for this because they're used for the linked list on the 
178          * smbsrv_tcon
179          */
180         struct smbsrv_handle_session_item {
181                 struct smbsrv_handle_session_item *prev, *next;
182                 struct smbsrv_handle *handle;
183         } session_item;
184
185         /*
186          * the value passed over the wire
187          * - 16 bit for smb
188          * - 32 bit for smb2
189          *   Note: for SMB2 handles are 128 bit
190          *         we'll fill them with
191          *         - 32 bit HID
192          *         - 32 bit TID
193          *         - 64 bit VUID
194          */
195         uint32_t hid;
196
197         /*
198          * the ntvfs handle passed to the ntvfs backend
199          */
200         struct ntvfs_handle *ntvfs;
201
202         /* some statistics for the management tools */
203         struct {
204                 /* the time when the tree connect started */
205                 struct timeval open_time;
206                 /* the time when the last request comes in */
207                 struct timeval last_use_time;
208         } statistics;
209 };
210
211 /* a set of flags to control handling of request structures */
212 #define SMBSRV_REQ_CONTROL_LARGE     (1<<1) /* allow replies larger than max_xmit */
213
214 #define SMBSRV_REQ_DEFAULT_STR_FLAGS(req) (((req)->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII)
215
216 /* the context for a single SMB request. This is passed to any request-context 
217    functions */
218 struct smbsrv_request {
219         /* the smbsrv_connection needs a list of requests queued for send */
220         struct smbsrv_request *next, *prev;
221
222         /* the server_context contains all context specific to this SMB socket */
223         struct smbsrv_connection *smb_conn;
224
225         /* conn is only set for operations that have a valid TID */
226         struct smbsrv_tcon *tcon;
227
228         /* the session context is derived from the vuid */
229         struct smbsrv_session *session;
230
231         /* a set of flags to control usage of the request. See SMBSRV_REQ_CONTROL_* */
232         uint32_t control_flags;
233
234         /* the system time when the request arrived */
235         struct timeval request_time;
236
237         /* a pointer to the per request union smb_* io structure */
238         void *io_ptr;
239
240         /* the ntvfs_request */
241         struct ntvfs_request *ntvfs;
242
243         /* Now the SMB specific stuff */
244
245         /* the flags from the SMB request, in raw form (host byte order) */
246         uint16_t flags2;
247
248         /* this can contain a fnum from an earlier part of a chained
249          * message (such as an SMBOpenX), or -1 */
250         int chained_fnum;
251
252         /* how far through the chain of SMB commands have we gone? */
253         unsigned chain_count;
254
255         /* the sequence number for signing */
256         uint64_t seq_num;
257
258         struct request_buffer in;
259         struct request_buffer out;
260 };
261
262 enum security_types {SEC_SHARE,SEC_USER};
263
264 /* smb server context structure. This should contain all the state
265  * information associated with a SMB server connection 
266  */
267 struct smbsrv_connection {
268         /* context that has been negotiated between the client and server */
269         struct {
270                 /* have we already done the NBT session establishment? */
271                 BOOL done_nbt_session;
272         
273                 /* only one negprot per connection is allowed */
274                 BOOL done_negprot;
275         
276                 /* multiple session setups are allowed, but some parameters are
277                    ignored in any but the first */
278                 BOOL done_sesssetup;
279                 
280                 /* 
281                  * Size of data we can send to client. Set
282                  *  by the client for all protocols above CORE.
283                  *  Set by us for CORE protocol.
284                  */
285                 unsigned max_send; /* init to BUFFER_SIZE */
286         
287                 /*
288                  * Size of the data we can receive. Set by us.
289                  * Can be modified by the max xmit parameter.
290                  */
291                 unsigned max_recv; /* init to BUFFER_SIZE */
292         
293                 /* the negotiatiated protocol */
294                 enum protocol_types protocol;
295         
296                 /* authentication context for multi-part negprot */
297                 struct auth_context *auth_context;
298         
299                 /* reference to the kerberos keytab, or machine trust account */
300                 struct cli_credentials *server_credentials;
301         
302                 /* did we tell the client we support encrypted passwords? */
303                 BOOL encrypted_passwords;
304         
305                 /* Did we choose SPNEGO, or perhaps raw NTLMSSP, or even no extended security at all? */
306                 const char *oid;
307         
308                 /* client capabilities */
309                 uint32_t client_caps;
310         
311                 /* the timezone we sent to the client */
312                 int zone_offset;
313
314                 /* NBT names only set when done_nbt_session is true */
315                 struct nbt_name *called_name;
316                 struct nbt_name *calling_name;
317         } negotiate;
318
319         /* the context associated with open tree connects on a smb socket, not for SMB2 */
320         struct smbsrv_tcons_context smb_tcons;
321
322         /* context associated with currently valid session setups */
323         struct smbsrv_sessions_context sessions;
324
325         /*
326          * the server_context holds a linked list of pending requests,
327          * this is used for finding the request structures on ntcancel requests
328          * For SMB only
329          */
330         struct smbsrv_request *requests;
331
332         /*
333          * the server_context holds a linked list of pending requests,
334          * and an idtree for finding the request structures on SMB2 Cancel
335          * For SMB2 only
336          */
337         struct {
338                 /* an id tree used to allocate ids */
339                 struct idr_context *idtree_req;
340
341                 /* this is the limit of pending requests values for this connection */
342                 uint32_t idtree_limit;
343
344                 /* list of open tree connects */
345                 struct smb2srv_request *list;
346         } requests2;
347
348         struct smb_signing_context signing;
349
350         struct stream_connection *connection;
351
352         /* this holds a partially received request */
353         struct packet_context *packet;
354
355         /* a list of partially received transaction requests */
356         struct smbsrv_trans_partial {
357                 struct smbsrv_trans_partial *next, *prev;
358                 struct smbsrv_request *req;
359                 struct smb_trans2 *trans;
360                 uint8_t command;
361         } *trans_partial;
362
363         /* configuration parameters */
364         struct {
365                 enum security_types security;
366                 BOOL nt_status_support;
367         } config;
368
369         /* some statictics for the management tools */
370         struct {
371                 /* the time when the client connects */
372                 struct timeval connect_time;
373                 /* the time when the last request comes in */
374                 struct timeval last_request_time;
375         } statistics;
376
377         struct share_context *share_context;
378 };
379
380 #include "smb_server/smb_server_proto.h"
381 #include "smb_server/smb/smb_proto.h"
382
383 /* useful way of catching wct errors with file and line number */
384 #define SMBSRV_CHECK_WCT(req, wcount) do { \
385         if ((req)->in.wct != (wcount)) { \
386                 DEBUG(1,("Unexpected WCT %u at %s(%d) - expected %d\n", \
387                          (req)->in.wct, __FILE__, __LINE__, wcount)); \
388                 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror)); \
389                 return; \
390         } \
391 } while (0)
392         
393 /* useful wrapper for talloc with NO_MEMORY reply */
394 #define SMBSRV_TALLOC_IO_PTR(ptr, type) do { \
395         ptr = talloc(req, type); \
396         if (!ptr) { \
397                 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
398                 return; \
399         } \
400         req->io_ptr = ptr; \
401 } while (0)
402
403 #define SMBSRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \
404         req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \
405                                           req->session->session_info,\
406                                           SVAL(req->in.hdr,HDR_PID), \
407                                           req->request_time, \
408                                           req, send_fn, state); \
409         if (!req->ntvfs) { \
410                 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
411                 return; \
412         } \
413         (void)talloc_steal(req->tcon->ntvfs, req); \
414         req->ntvfs->frontend_data.private_data = req; \
415 } while (0)
416
417 #define SMBSRV_CHECK_FILE_HANDLE(handle) do { \
418         if (!handle) { \
419                 smbsrv_send_error(req, NT_STATUS_INVALID_HANDLE); \
420                 return; \
421         } \
422 } while (0)
423
424 #define SMBSRV_CHECK_FILE_HANDLE_ERROR(handle, _status) do { \
425         if (!handle) { \
426                 smbsrv_send_error(req, _status); \
427                 return; \
428         } \
429 } while (0)
430
431 #define SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(handle) do { \
432         if (!handle) { \
433                 return NT_STATUS_INVALID_HANDLE; \
434         } \
435 } while (0)
436
437 #define SMBSRV_CHECK(cmd) do {\
438         NTSTATUS _status; \
439         _status = cmd; \
440         if (!NT_STATUS_IS_OK(_status)) { \
441                 smbsrv_send_error(req,  _status); \
442                 return; \
443         } \
444 } while (0)
445
446 /* 
447    check if the backend wants to handle the request asynchronously.
448    if it wants it handled synchronously then call the send function
449    immediately
450 */
451 #define SMBSRV_CALL_NTVFS_BACKEND(cmd) do { \
452         req->ntvfs->async_states->status = cmd; \
453         if (req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC) { \
454                 DLIST_ADD_END(req->smb_conn->requests, req, struct smbsrv_request *); \
455         } else { \
456                 req->ntvfs->async_states->send_fn(req->ntvfs); \
457         } \
458 } while (0)
459
460 /* check req->ntvfs->async_states->status and if not OK then send an error reply */
461 #define SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \
462         req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
463         if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
464                 smbsrv_send_error(req, ntvfs->async_states->status); \
465                 return; \
466         } \
467 } while (0)
468 #define SMBSRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \
469         SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \
470         ptr = talloc_get_type(req->io_ptr, type); \
471 } while (0)
472 #define SMBSRV_CHECK_ASYNC_STATUS_SIMPLE do { \
473         req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
474         if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
475                 smbsrv_send_error(req, ntvfs->async_states->status); \
476                 return; \
477         } \
478 } while (0)
479 #define SMBSRV_CHECK_ASYNC_STATUS(ptr, type) do { \
480         SMBSRV_CHECK_ASYNC_STATUS_SIMPLE; \
481         ptr = talloc_get_type(req->io_ptr, type); \
482 } while (0)
483
484 /* zero out some reserved fields in a reply */
485 #define SMBSRV_VWV_RESERVED(start, count) memset(req->out.vwv + VWV(start), 0, (count)*2)