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