VERSION: Re-enable git snapshots.
[Samba.git] / source4 / smb_server / smb_server.h
blobab555445a3371276ae5834c66a87609c1fe77450
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 "libds/common/roles.h"
26 #include "../lib/util/dlinklist.h"
27 #include "../librpc/gen_ndr/nbt.h"
29 struct tevent_context;
32 this header declares the core context structures associated with smb
33 sockets, tree connects, requests etc
35 the idea is that we will eventually get rid of all our global
36 variables and instead store our state from structures hanging off
37 these basic elements
40 struct smbsrv_tcons_context {
41 /* an id tree used to allocate tids */
42 struct idr_context *idtree_tid;
44 /* this is the limit of vuid values for this connection */
45 uint32_t idtree_limit;
47 /* list of open tree connects */
48 struct smbsrv_tcon *list;
51 struct smbsrv_sessions_context {
52 /* an id tree used to allocate vuids */
53 /* this holds info on session vuids that are already
54 * validated for this VC */
55 struct idr_context *idtree_vuid;
57 /* this is the limit of vuid values for this connection */
58 uint64_t idtree_limit;
60 /* also kept as a link list so it can be enumerated by
61 the management code */
62 struct smbsrv_session *list;
65 struct smbsrv_handles_context {
66 /* an id tree used to allocate file handles */
67 struct idr_context *idtree_hid;
69 /* this is the limit of handle values for this context */
70 uint64_t idtree_limit;
72 /* also kept as a link list so it can be enumerated by
73 the management code */
74 struct smbsrv_handle *list;
77 /* the current user context for a request */
78 struct smbsrv_session {
79 struct smbsrv_session *prev, *next;
81 struct smbsrv_connection *smb_conn;
84 * in SMB2 tcons belong to just one session
85 * and not to the whole connection
87 struct smbsrv_tcons_context smb2_tcons;
90 * the open file handles for this session,
91 * used for SMBexit, SMBulogoff and SMB2 SessionLogoff
93 struct smbsrv_handle_session_item *handles;
95 /*
96 * an index passed over the wire:
97 * - 16 bit for smb
98 * - 64 bit for smb2
100 uint64_t vuid;
102 struct gensec_security *gensec_ctx;
104 struct auth_session_info *session_info;
106 struct {
107 bool required;
108 bool active;
109 } smb2_signing;
111 /* some statistics for the management tools */
112 struct {
113 /* the time when the session setup started */
114 struct timeval connect_time;
115 /* the time when the session setup was finished */
116 struct timeval auth_time;
117 /* the time when the last request comes in */
118 struct timeval last_request_time;
119 } statistics;
122 /* we need a forward declaration of the ntvfs_ops strucutre to prevent
123 include recursion */
124 struct ntvfs_context;
126 struct smbsrv_tcon {
127 struct smbsrv_tcon *next, *prev;
129 /* the server context that this was created on */
130 struct smbsrv_connection *smb_conn;
132 /* the open file handles on this tcon */
133 struct smbsrv_handles_context handles;
136 * an index passed over the wire:
137 * - 16 bit for smb
138 * - 32 bit for smb2
140 uint32_t tid; /* an index passed over the wire (the TID) */
142 /* the share name */
143 const char *share_name;
145 /* the NTVFS context - see source/ntvfs/ for details */
146 struct ntvfs_context *ntvfs;
148 /* some stuff to support share level security */
149 struct {
150 /* in share level security we need to fake up a session */
151 struct smbsrv_session *session;
152 } sec_share;
154 /* some stuff to support share level security */
155 struct {
156 /* in SMB2 a tcon always belongs to one session */
157 struct smbsrv_session *session;
158 } smb2;
160 /* some statistics for the management tools */
161 struct {
162 /* the time when the tree connect started */
163 struct timeval connect_time;
164 /* the time when the last request comes in */
165 struct timeval last_request_time;
166 } statistics;
169 struct smbsrv_handle {
170 struct smbsrv_handle *next, *prev;
172 /* the tcon the handle belongs to */
173 struct smbsrv_tcon *tcon;
175 /* the session the handle was opened on */
176 struct smbsrv_session *session;
178 /* the smbpid used on the open, used for SMBexit */
179 uint16_t smbpid;
182 * this is for adding the handle into a linked list
183 * on the smbsrv_session, we can't use *next,*prev
184 * for this because they're used for the linked list on the
185 * smbsrv_tcon
187 struct smbsrv_handle_session_item {
188 struct smbsrv_handle_session_item *prev, *next;
189 struct smbsrv_handle *handle;
190 } session_item;
193 * the value passed over the wire
194 * - 16 bit for smb
195 * - 32 bit for smb2
196 * Note: for SMB2 handles are 128 bit
197 * we'll fill them with
198 * - 32 bit HID
199 * - 32 bit TID
200 * - 64 bit VUID
202 uint32_t hid;
205 * the ntvfs handle passed to the ntvfs backend
207 struct ntvfs_handle *ntvfs;
209 /* some statistics for the management tools */
210 struct {
211 /* the time when the tree connect started */
212 struct timeval open_time;
213 /* the time when the last request comes in */
214 struct timeval last_use_time;
215 } statistics;
218 /* a set of flags to control handling of request structures */
219 #define SMBSRV_REQ_CONTROL_LARGE (1<<1) /* allow replies larger than max_xmit */
221 #define SMBSRV_REQ_DEFAULT_STR_FLAGS(req) (((req)->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII)
223 /* the context for a single SMB request. This is passed to any request-context
224 functions */
225 struct smbsrv_request {
226 /* the smbsrv_connection needs a list of requests queued for send */
227 struct smbsrv_request *next, *prev;
229 /* the server_context contains all context specific to this SMB socket */
230 struct smbsrv_connection *smb_conn;
232 /* conn is only set for operations that have a valid TID */
233 struct smbsrv_tcon *tcon;
235 /* the session context is derived from the vuid */
236 struct smbsrv_session *session;
238 /* a set of flags to control usage of the request. See SMBSRV_REQ_CONTROL_* */
239 uint32_t control_flags;
241 /* the system time when the request arrived */
242 struct timeval request_time;
244 /* a pointer to the per request union smb_* io structure */
245 void *io_ptr;
247 /* the ntvfs_request */
248 struct ntvfs_request *ntvfs;
250 /* Now the SMB specific stuff */
252 /* the flags from the SMB request, in raw form (host byte order) */
253 uint16_t flags2;
255 /* this can contain a fnum from an earlier part of a chained
256 * message (such as an SMBOpenX), or -1 */
257 int chained_fnum;
259 /* how far through the chain of SMB commands have we gone? */
260 unsigned chain_count;
262 /* the sequence number for signing */
263 uint64_t seq_num;
265 struct smb_request_buffer in;
266 struct smb_request_buffer out;
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 auth4_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 bool nt_status_support;
374 } config;
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;
384 struct share_context *share_context;
386 struct loadparm_context *lp_ctx;
388 bool smb2_signing_required;
390 uint64_t highest_smb2_seqnum;
393 struct model_ops;
394 struct loadparm_context;
396 NTSTATUS smbsrv_add_socket(TALLOC_CTX *mem_ctx,
397 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"