WHATSNEW: Update release notes.
[Samba/bb.git] / source4 / smb_server / smb_server.h
blobd238e03a750c5c2b6df8d07bf8f64babf7775953
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) James J Myers 2003 <myersjj@samba.org>
6 Copyright (C) Stefan Metzmacher 2004-2005
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.
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.
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/>.
22 #include "libcli/raw/request.h"
23 #include "libcli/raw/interfaces.h"
24 #include "lib/socket/socket.h"
25 #include "../lib/util/dlinklist.h"
27 struct tevent_context;
30 this header declares the core context structures associated with smb
31 sockets, tree connects, requests etc
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
38 struct smbsrv_tcons_context {
39 /* an id tree used to allocate tids */
40 struct idr_context *idtree_tid;
42 /* this is the limit of vuid values for this connection */
43 uint32_t idtree_limit;
45 /* list of open tree connects */
46 struct smbsrv_tcon *list;
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;
55 /* this is the limit of vuid values for this connection */
56 uint64_t idtree_limit;
58 /* also kept as a link list so it can be enumerated by
59 the management code */
60 struct smbsrv_session *list;
63 struct smbsrv_handles_context {
64 /* an id tree used to allocate file handles */
65 struct idr_context *idtree_hid;
67 /* this is the limit of handle values for this context */
68 uint64_t idtree_limit;
70 /* also kept as a link list so it can be enumerated by
71 the management code */
72 struct smbsrv_handle *list;
75 /* the current user context for a request */
76 struct smbsrv_session {
77 struct smbsrv_session *prev, *next;
79 struct smbsrv_connection *smb_conn;
82 * in SMB2 tcons belong to just one session
83 * and not to the whole connection
85 struct smbsrv_tcons_context smb2_tcons;
88 * the open file handles for this session,
89 * used for SMBexit, SMBulogoff and SMB2 SessionLogoff
91 struct smbsrv_handle_session_item *handles;
93 /*
94 * an index passed over the wire:
95 * - 16 bit for smb
96 * - 64 bit for smb2
98 uint64_t vuid;
100 struct gensec_security *gensec_ctx;
102 struct auth_session_info *session_info;
104 struct {
105 bool required;
106 bool active;
107 } smb2_signing;
109 /* some statistics for the management tools */
110 struct {
111 /* the time when the session setup started */
112 struct timeval connect_time;
113 /* the time when the session setup was finished */
114 struct timeval auth_time;
115 /* the time when the last request comes in */
116 struct timeval last_request_time;
117 } statistics;
120 /* we need a forward declaration of the ntvfs_ops strucutre to prevent
121 include recursion */
122 struct ntvfs_context;
124 struct smbsrv_tcon {
125 struct smbsrv_tcon *next, *prev;
127 /* the server context that this was created on */
128 struct smbsrv_connection *smb_conn;
130 /* the open file handles on this tcon */
131 struct smbsrv_handles_context handles;
134 * an index passed over the wire:
135 * - 16 bit for smb
136 * - 32 bit for smb2
138 uint32_t tid; /* an index passed over the wire (the TID) */
140 /* the share name */
141 const char *share_name;
143 /* the NTVFS context - see source/ntvfs/ for details */
144 struct ntvfs_context *ntvfs;
146 /* some stuff to support share level security */
147 struct {
148 /* in share level security we need to fake up a session */
149 struct smbsrv_session *session;
150 } sec_share;
152 /* some stuff to support share level security */
153 struct {
154 /* in SMB2 a tcon always belongs to one session */
155 struct smbsrv_session *session;
156 } smb2;
158 /* some statistics for the management tools */
159 struct {
160 /* the time when the tree connect started */
161 struct timeval connect_time;
162 /* the time when the last request comes in */
163 struct timeval last_request_time;
164 } statistics;
167 struct smbsrv_handle {
168 struct smbsrv_handle *next, *prev;
170 /* the tcon the handle belongs to */
171 struct smbsrv_tcon *tcon;
173 /* the session the handle was opened on */
174 struct smbsrv_session *session;
176 /* the smbpid used on the open, used for SMBexit */
177 uint16_t smbpid;
180 * this is for adding the handle into a linked list
181 * on the smbsrv_session, we can't use *next,*prev
182 * for this because they're used for the linked list on the
183 * smbsrv_tcon
185 struct smbsrv_handle_session_item {
186 struct smbsrv_handle_session_item *prev, *next;
187 struct smbsrv_handle *handle;
188 } session_item;
191 * the value passed over the wire
192 * - 16 bit for smb
193 * - 32 bit for smb2
194 * Note: for SMB2 handles are 128 bit
195 * we'll fill them with
196 * - 32 bit HID
197 * - 32 bit TID
198 * - 64 bit VUID
200 uint32_t hid;
203 * the ntvfs handle passed to the ntvfs backend
205 struct ntvfs_handle *ntvfs;
207 /* some statistics for the management tools */
208 struct {
209 /* the time when the tree connect started */
210 struct timeval open_time;
211 /* the time when the last request comes in */
212 struct timeval last_use_time;
213 } statistics;
216 /* a set of flags to control handling of request structures */
217 #define SMBSRV_REQ_CONTROL_LARGE (1<<1) /* allow replies larger than max_xmit */
219 #define SMBSRV_REQ_DEFAULT_STR_FLAGS(req) (((req)->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII)
221 /* the context for a single SMB request. This is passed to any request-context
222 functions */
223 struct smbsrv_request {
224 /* the smbsrv_connection needs a list of requests queued for send */
225 struct smbsrv_request *next, *prev;
227 /* the server_context contains all context specific to this SMB socket */
228 struct smbsrv_connection *smb_conn;
230 /* conn is only set for operations that have a valid TID */
231 struct smbsrv_tcon *tcon;
233 /* the session context is derived from the vuid */
234 struct smbsrv_session *session;
236 /* a set of flags to control usage of the request. See SMBSRV_REQ_CONTROL_* */
237 uint32_t control_flags;
239 /* the system time when the request arrived */
240 struct timeval request_time;
242 /* a pointer to the per request union smb_* io structure */
243 void *io_ptr;
245 /* the ntvfs_request */
246 struct ntvfs_request *ntvfs;
248 /* Now the SMB specific stuff */
250 /* the flags from the SMB request, in raw form (host byte order) */
251 uint16_t flags2;
253 /* this can contain a fnum from an earlier part of a chained
254 * message (such as an SMBOpenX), or -1 */
255 int chained_fnum;
257 /* how far through the chain of SMB commands have we gone? */
258 unsigned chain_count;
260 /* the sequence number for signing */
261 uint64_t seq_num;
263 struct smb_request_buffer in;
264 struct smb_request_buffer out;
267 enum security_types {SEC_SHARE,SEC_USER};
269 /* smb server context structure. This should contain all the state
270 * information associated with a SMB server connection
272 struct smbsrv_connection {
273 /* context that has been negotiated between the client and server */
274 struct {
275 /* have we already done the NBT session establishment? */
276 bool done_nbt_session;
278 /* only one negprot per connection is allowed */
279 bool done_negprot;
281 /* multiple session setups are allowed, but some parameters are
282 ignored in any but the first */
283 bool done_sesssetup;
286 * Size of data we can send to client. Set
287 * by the client for all protocols above CORE.
288 * Set by us for CORE protocol.
290 unsigned max_send; /* init to BUFFER_SIZE */
293 * Size of the data we can receive. Set by us.
294 * Can be modified by the max xmit parameter.
296 unsigned max_recv; /* init to BUFFER_SIZE */
298 /* the negotiatiated protocol */
299 enum protocol_types protocol;
301 /* authentication context for multi-part negprot */
302 struct auth_context *auth_context;
304 /* reference to the kerberos keytab, or machine trust account */
305 struct cli_credentials *server_credentials;
307 /* did we tell the client we support encrypted passwords? */
308 bool encrypted_passwords;
310 /* Did we choose SPNEGO, or perhaps raw NTLMSSP, or even no extended security at all? */
311 const char *oid;
313 /* client capabilities */
314 uint32_t client_caps;
316 /* the timezone we sent to the client */
317 int zone_offset;
319 /* NBT names only set when done_nbt_session is true */
320 struct nbt_name *called_name;
321 struct nbt_name *calling_name;
322 } negotiate;
324 /* the context associated with open tree connects on a smb socket, not for SMB2 */
325 struct smbsrv_tcons_context smb_tcons;
327 /* context associated with currently valid session setups */
328 struct smbsrv_sessions_context sessions;
331 * the server_context holds a linked list of pending requests,
332 * this is used for finding the request structures on ntcancel requests
333 * For SMB only
335 struct smbsrv_request *requests;
338 * the server_context holds a linked list of pending requests,
339 * and an idtree for finding the request structures on SMB2 Cancel
340 * For SMB2 only
342 struct {
343 /* an id tree used to allocate ids */
344 struct idr_context *idtree_req;
346 /* this is the limit of pending requests values for this connection */
347 uint32_t idtree_limit;
349 /* list of open tree connects */
350 struct smb2srv_request *list;
351 } requests2;
353 struct smb_signing_context signing;
355 struct stream_connection *connection;
357 /* this holds a partially received request */
358 struct packet_context *packet;
360 /* a list of partially received transaction requests */
361 struct smbsrv_trans_partial {
362 struct smbsrv_trans_partial *next, *prev;
363 struct smbsrv_request *req;
364 uint8_t command;
365 union {
366 struct smb_trans2 *trans;
367 struct smb_nttrans *nttrans;
368 } u;
369 } *trans_partial;
371 /* configuration parameters */
372 struct {
373 enum security_types security;
374 bool nt_status_support;
375 } config;
377 /* some statictics for the management tools */
378 struct {
379 /* the time when the client connects */
380 struct timeval connect_time;
381 /* the time when the last request comes in */
382 struct timeval last_request_time;
383 } statistics;
385 struct share_context *share_context;
387 struct loadparm_context *lp_ctx;
389 bool smb2_signing_required;
391 uint64_t highest_smb2_seqnum;
394 struct model_ops;
395 struct loadparm_context;
397 NTSTATUS smbsrv_add_socket(struct tevent_context *event_context,
398 struct loadparm_context *lp_ctx,
399 const struct model_ops *model_ops,
400 const char *address);
402 struct loadparm_context;
404 #include "smb_server/smb_server_proto.h"
405 #include "smb_server/smb/smb_proto.h"
407 /* useful way of catching wct errors with file and line number */
408 #define SMBSRV_CHECK_WCT(req, wcount) do { \
409 if ((req)->in.wct != (wcount)) { \
410 DEBUG(1,("Unexpected WCT %u at %s(%d) - expected %d\n", \
411 (req)->in.wct, __FILE__, __LINE__, wcount)); \
412 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror)); \
413 return; \
415 } while (0)
417 /* useful wrapper for talloc with NO_MEMORY reply */
418 #define SMBSRV_TALLOC_IO_PTR(ptr, type) do { \
419 ptr = talloc(req, type); \
420 if (!ptr) { \
421 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
422 return; \
424 req->io_ptr = ptr; \
425 } while (0)
427 #define SMBSRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \
428 req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \
429 req->session->session_info,\
430 SVAL(req->in.hdr,HDR_PID), \
431 req->request_time, \
432 req, send_fn, state); \
433 if (!req->ntvfs) { \
434 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
435 return; \
437 (void)talloc_steal(req->tcon->ntvfs, req); \
438 req->ntvfs->frontend_data.private_data = req; \
439 } while (0)
441 #define SMBSRV_CHECK_FILE_HANDLE(handle) do { \
442 if (!handle) { \
443 smbsrv_send_error(req, NT_STATUS_INVALID_HANDLE); \
444 return; \
446 } while (0)
448 #define SMBSRV_CHECK_FILE_HANDLE_ERROR(handle, _status) do { \
449 if (!handle) { \
450 smbsrv_send_error(req, _status); \
451 return; \
453 } while (0)
455 #define SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(handle) do { \
456 if (!handle) { \
457 return NT_STATUS_INVALID_HANDLE; \
459 } while (0)
461 #define SMBSRV_CHECK(cmd) do {\
462 NTSTATUS _status; \
463 _status = cmd; \
464 if (!NT_STATUS_IS_OK(_status)) { \
465 smbsrv_send_error(req, _status); \
466 return; \
468 } while (0)
471 check if the backend wants to handle the request asynchronously.
472 if it wants it handled synchronously then call the send function
473 immediately
475 #define SMBSRV_CALL_NTVFS_BACKEND(cmd) do { \
476 req->ntvfs->async_states->status = cmd; \
477 if (req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC) { \
478 DLIST_ADD_END(req->smb_conn->requests, req, struct smbsrv_request *); \
479 } else { \
480 req->ntvfs->async_states->send_fn(req->ntvfs); \
482 } while (0)
484 /* check req->ntvfs->async_states->status and if not OK then send an error reply */
485 #define SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \
486 req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
487 if (ntvfs->async_states->state & NTVFS_ASYNC_STATE_CLOSE || NT_STATUS_EQUAL(ntvfs->async_states->status, NT_STATUS_NET_WRITE_FAULT)) { \
488 smbsrv_terminate_connection(req->smb_conn, get_friendly_nt_error_msg (ntvfs->async_states->status)); \
489 talloc_free(req); \
490 return; \
492 if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
493 smbsrv_send_error(req, ntvfs->async_states->status); \
494 return; \
496 } while (0)
497 #define SMBSRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \
498 SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \
499 ptr = talloc_get_type(req->io_ptr, type); \
500 } while (0)
501 #define SMBSRV_CHECK_ASYNC_STATUS_SIMPLE do { \
502 req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
503 if (ntvfs->async_states->state & NTVFS_ASYNC_STATE_CLOSE || NT_STATUS_EQUAL(ntvfs->async_states->status, NT_STATUS_NET_WRITE_FAULT)) { \
504 smbsrv_terminate_connection(req->smb_conn, get_friendly_nt_error_msg (ntvfs->async_states->status)); \
505 talloc_free(req); \
506 return; \
508 if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
509 smbsrv_send_error(req, ntvfs->async_states->status); \
510 return; \
512 } while (0)
513 #define SMBSRV_CHECK_ASYNC_STATUS(ptr, type) do { \
514 SMBSRV_CHECK_ASYNC_STATUS_SIMPLE; \
515 ptr = talloc_get_type(req->io_ptr, type); \
516 } while (0)
518 /* zero out some reserved fields in a reply */
519 #define SMBSRV_VWV_RESERVED(start, count) memset(req->out.vwv + VWV(start), 0, (count)*2)
521 #include "smb_server/service_smb_proto.h"