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