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
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
;
94 * an index passed over the wire:
100 struct gensec_security
*gensec_ctx
;
102 struct auth_session_info
*session_info
;
109 /* some statistics for the management tools */
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
;
120 /* we need a forward declaration of the ntvfs_ops strucutre to prevent
122 struct ntvfs_context
;
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:
138 uint32_t tid
; /* an index passed over the wire (the TID) */
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 */
148 /* in share level security we need to fake up a session */
149 struct smbsrv_session
*session
;
152 /* some stuff to support share level security */
154 /* in SMB2 a tcon always belongs to one session */
155 struct smbsrv_session
*session
;
158 /* some statistics for the management tools */
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
;
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 */
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
185 struct smbsrv_handle_session_item
{
186 struct smbsrv_handle_session_item
*prev
, *next
;
187 struct smbsrv_handle
*handle
;
191 * the value passed over the wire
194 * Note: for SMB2 handles are 128 bit
195 * we'll fill them with
203 * the ntvfs handle passed to the ntvfs backend
205 struct ntvfs_handle
*ntvfs
;
207 /* some statistics for the management tools */
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
;
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
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 */
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) */
253 /* this can contain a fnum from an earlier part of a chained
254 * message (such as an SMBOpenX), or -1 */
257 /* how far through the chain of SMB commands have we gone? */
258 unsigned chain_count
;
260 /* the sequence number for signing */
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 */
275 /* have we already done the NBT session establishment? */
276 bool done_nbt_session
;
278 /* only one negprot per connection is allowed */
281 /* multiple session setups are allowed, but some parameters are
282 ignored in any but the first */
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? */
313 /* client capabilities */
314 uint32_t client_caps
;
316 /* the timezone we sent to the client */
319 /* NBT names only set when done_nbt_session is true */
320 struct nbt_name
*called_name
;
321 struct nbt_name
*calling_name
;
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
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
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
;
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
;
366 struct smb_trans2
*trans
;
367 struct smb_nttrans
*nttrans
;
371 /* configuration parameters */
373 enum security_types security
;
374 bool nt_status_support
;
377 /* some statictics for the management tools */
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
;
385 struct share_context
*share_context
;
387 struct loadparm_context
*lp_ctx
;
389 bool smb2_signing_required
;
391 uint64_t highest_smb2_seqnum
;
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)); \
417 /* useful wrapper for talloc with NO_MEMORY reply */
418 #define SMBSRV_TALLOC_IO_PTR(ptr, type) do { \
419 ptr = talloc(req, type); \
421 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
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), \
432 req, send_fn, state); \
434 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
437 (void)talloc_steal(req->tcon->ntvfs, req); \
438 req->ntvfs->frontend_data.private_data = req; \
441 #define SMBSRV_CHECK_FILE_HANDLE(handle) do { \
443 smbsrv_send_error(req, NT_STATUS_INVALID_HANDLE); \
448 #define SMBSRV_CHECK_FILE_HANDLE_ERROR(handle, _status) do { \
450 smbsrv_send_error(req, _status); \
455 #define SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(handle) do { \
457 return NT_STATUS_INVALID_HANDLE; \
461 #define SMBSRV_CHECK(cmd) do {\
464 if (!NT_STATUS_IS_OK(_status)) { \
465 smbsrv_send_error(req, _status); \
471 check if the backend wants to handle the request asynchronously.
472 if it wants it handled synchronously then call the send function
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 *); \
480 req->ntvfs->async_states->send_fn(req->ntvfs); \
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)); \
492 if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
493 smbsrv_send_error(req, ntvfs->async_states->status); \
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); \
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)); \
508 if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
509 smbsrv_send_error(req, ntvfs->async_states->status); \
513 #define SMBSRV_CHECK_ASYNC_STATUS(ptr, type) do { \
514 SMBSRV_CHECK_ASYNC_STATUS_SIMPLE; \
515 ptr = talloc_get_type(req->io_ptr, type); \
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"