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/events/events.h"
25 #include "lib/socket/socket.h"
26 #include "lib/util/dlinklist.h"
29 this header declares the core context structures associated with smb
30 sockets, tree connects, requests etc
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
37 struct smbsrv_tcons_context {
38 /* an id tree used to allocate tids */
39 struct idr_context *idtree_tid;
41 /* this is the limit of vuid values for this connection */
42 uint32_t idtree_limit;
44 /* list of open tree connects */
45 struct smbsrv_tcon *list;
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;
54 /* this is the limit of vuid values for this connection */
55 uint64_t idtree_limit;
57 /* also kept as a link list so it can be enumerated by
58 the management code */
59 struct smbsrv_session *list;
62 struct smbsrv_handles_context {
63 /* an id tree used to allocate file handles */
64 struct idr_context *idtree_hid;
66 /* this is the limit of handle values for this context */
67 uint64_t idtree_limit;
69 /* also kept as a link list so it can be enumerated by
70 the management code */
71 struct smbsrv_handle *list;
74 /* the current user context for a request */
75 struct smbsrv_session {
76 struct smbsrv_session *prev, *next;
78 struct smbsrv_connection *smb_conn;
81 * in SMB2 tcons belong to just one session
82 * and not to the whole connection
84 struct smbsrv_tcons_context smb2_tcons;
87 * the open file handles for this session,
88 * used for SMBexit, SMBulogoff and SMB2 SessionLogoff
90 struct smbsrv_handle_session_item *handles;
93 * an index passed over the wire:
99 struct gensec_security *gensec_ctx;
101 struct auth_session_info *session_info;
103 /* some statistics for the management tools */
105 /* the time when the session setup started */
106 struct timeval connect_time;
107 /* the time when the session setup was finished */
108 struct timeval auth_time;
109 /* the time when the last request comes in */
110 struct timeval last_request_time;
114 /* we need a forward declaration of the ntvfs_ops strucutre to prevent
116 struct ntvfs_context;
119 struct smbsrv_tcon *next, *prev;
121 /* the server context that this was created on */
122 struct smbsrv_connection *smb_conn;
124 /* the open file handles on this tcon */
125 struct smbsrv_handles_context handles;
128 * an index passed over the wire:
132 uint32_t tid; /* an index passed over the wire (the TID) */
135 const char *share_name;
137 /* the NTVFS context - see source/ntvfs/ for details */
138 struct ntvfs_context *ntvfs;
140 /* some stuff to support share level security */
142 /* in share level security we need to fake up a session */
143 struct smbsrv_session *session;
146 /* some stuff to support share level security */
148 /* in SMB2 a tcon always belongs to one session */
149 struct smbsrv_session *session;
152 /* some statistics for the management tools */
154 /* the time when the tree connect started */
155 struct timeval connect_time;
156 /* the time when the last request comes in */
157 struct timeval last_request_time;
161 struct smbsrv_handle {
162 struct smbsrv_handle *next, *prev;
164 /* the tcon the handle belongs to */
165 struct smbsrv_tcon *tcon;
167 /* the session the handle was opened on */
168 struct smbsrv_session *session;
170 /* the smbpid used on the open, used for SMBexit */
174 * this is for adding the handle into a linked list
175 * on the smbsrv_session, we can't use *next,*prev
176 * for this because they're used for the linked list on the
179 struct smbsrv_handle_session_item {
180 struct smbsrv_handle_session_item *prev, *next;
181 struct smbsrv_handle *handle;
185 * the value passed over the wire
188 * Note: for SMB2 handles are 128 bit
189 * we'll fill them with
197 * the ntvfs handle passed to the ntvfs backend
199 struct ntvfs_handle *ntvfs;
201 /* some statistics for the management tools */
203 /* the time when the tree connect started */
204 struct timeval open_time;
205 /* the time when the last request comes in */
206 struct timeval last_use_time;
210 /* a set of flags to control handling of request structures */
211 #define SMBSRV_REQ_CONTROL_LARGE (1<<1) /* allow replies larger than max_xmit */
213 #define SMBSRV_REQ_DEFAULT_STR_FLAGS(req) (((req)->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII)
215 /* the context for a single SMB request. This is passed to any request-context
217 struct smbsrv_request {
218 /* the smbsrv_connection needs a list of requests queued for send */
219 struct smbsrv_request *next, *prev;
221 /* the server_context contains all context specific to this SMB socket */
222 struct smbsrv_connection *smb_conn;
224 /* conn is only set for operations that have a valid TID */
225 struct smbsrv_tcon *tcon;
227 /* the session context is derived from the vuid */
228 struct smbsrv_session *session;
230 /* a set of flags to control usage of the request. See SMBSRV_REQ_CONTROL_* */
231 uint32_t control_flags;
233 /* the system time when the request arrived */
234 struct timeval request_time;
236 /* a pointer to the per request union smb_* io structure */
239 /* the ntvfs_request */
240 struct ntvfs_request *ntvfs;
242 /* Now the SMB specific stuff */
244 /* the flags from the SMB request, in raw form (host byte order) */
247 /* this can contain a fnum from an earlier part of a chained
248 * message (such as an SMBOpenX), or -1 */
251 /* how far through the chain of SMB commands have we gone? */
252 unsigned chain_count;
254 /* the sequence number for signing */
257 struct smb_request_buffer in;
258 struct smb_request_buffer out;
261 enum security_types {SEC_SHARE,SEC_USER};
263 /* smb server context structure. This should contain all the state
264 * information associated with a SMB server connection
266 struct smbsrv_connection {
267 /* context that has been negotiated between the client and server */
269 /* have we already done the NBT session establishment? */
270 bool done_nbt_session;
272 /* only one negprot per connection is allowed */
275 /* multiple session setups are allowed, but some parameters are
276 ignored in any but the first */
280 * Size of data we can send to client. Set
281 * by the client for all protocols above CORE.
282 * Set by us for CORE protocol.
284 unsigned max_send; /* init to BUFFER_SIZE */
287 * Size of the data we can receive. Set by us.
288 * Can be modified by the max xmit parameter.
290 unsigned max_recv; /* init to BUFFER_SIZE */
292 /* the negotiatiated protocol */
293 enum protocol_types protocol;
295 /* authentication context for multi-part negprot */
296 struct auth_context *auth_context;
298 /* reference to the kerberos keytab, or machine trust account */
299 struct cli_credentials *server_credentials;
301 /* did we tell the client we support encrypted passwords? */
302 bool encrypted_passwords;
304 /* Did we choose SPNEGO, or perhaps raw NTLMSSP, or even no extended security at all? */
307 /* client capabilities */
308 uint32_t client_caps;
310 /* the timezone we sent to the client */
313 /* NBT names only set when done_nbt_session is true */
314 struct nbt_name *called_name;
315 struct nbt_name *calling_name;
318 /* the context associated with open tree connects on a smb socket, not for SMB2 */
319 struct smbsrv_tcons_context smb_tcons;
321 /* context associated with currently valid session setups */
322 struct smbsrv_sessions_context sessions;
325 * the server_context holds a linked list of pending requests,
326 * this is used for finding the request structures on ntcancel requests
329 struct smbsrv_request *requests;
332 * the server_context holds a linked list of pending requests,
333 * and an idtree for finding the request structures on SMB2 Cancel
337 /* an id tree used to allocate ids */
338 struct idr_context *idtree_req;
340 /* this is the limit of pending requests values for this connection */
341 uint32_t idtree_limit;
343 /* list of open tree connects */
344 struct smb2srv_request *list;
347 struct smb_signing_context signing;
349 struct stream_connection *connection;
351 /* this holds a partially received request */
352 struct packet_context *packet;
354 /* a list of partially received transaction requests */
355 struct smbsrv_trans_partial {
356 struct smbsrv_trans_partial *next, *prev;
357 struct smbsrv_request *req;
358 struct smb_trans2 *trans;
362 /* configuration parameters */
364 enum security_types security;
365 bool nt_status_support;
368 /* some statictics for the management tools */
370 /* the time when the client connects */
371 struct timeval connect_time;
372 /* the time when the last request comes in */
373 struct timeval last_request_time;
376 struct share_context *share_context;
378 struct loadparm_context *lp_ctx;
384 struct loadparm_context;
386 NTSTATUS smbsrv_add_socket(struct event_context *event_context,
387 struct loadparm_context *lp_ctx,
388 const struct model_ops *model_ops,
389 const char *address);
391 struct loadparm_context;
393 #include "smb_server/smb_server_proto.h"
394 #include "smb_server/smb/smb_proto.h"
396 /* useful way of catching wct errors with file and line number */
397 #define SMBSRV_CHECK_WCT(req, wcount) do { \
398 if ((req)->in.wct != (wcount)) { \
399 DEBUG(1,("Unexpected WCT %u at %s(%d) - expected %d\n", \
400 (req)->in.wct, __FILE__, __LINE__, wcount)); \
401 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror)); \
406 /* useful wrapper for talloc with NO_MEMORY reply */
407 #define SMBSRV_TALLOC_IO_PTR(ptr, type) do { \
408 ptr = talloc(req, type); \
410 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
416 #define SMBSRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \
417 req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \
418 req->session->session_info,\
419 SVAL(req->in.hdr,HDR_PID), \
421 req, send_fn, state); \
423 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
426 (void)talloc_steal(req->tcon->ntvfs, req); \
427 req->ntvfs->frontend_data.private_data = req; \
430 #define SMBSRV_CHECK_FILE_HANDLE(handle) do { \
432 smbsrv_send_error(req, NT_STATUS_INVALID_HANDLE); \
437 #define SMBSRV_CHECK_FILE_HANDLE_ERROR(handle, _status) do { \
439 smbsrv_send_error(req, _status); \
444 #define SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(handle) do { \
446 return NT_STATUS_INVALID_HANDLE; \
450 #define SMBSRV_CHECK(cmd) do {\
453 if (!NT_STATUS_IS_OK(_status)) { \
454 smbsrv_send_error(req, _status); \
460 check if the backend wants to handle the request asynchronously.
461 if it wants it handled synchronously then call the send function
464 #define SMBSRV_CALL_NTVFS_BACKEND(cmd) do { \
465 req->ntvfs->async_states->status = cmd; \
466 if (req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC) { \
467 DLIST_ADD_END(req->smb_conn->requests, req, struct smbsrv_request *); \
469 req->ntvfs->async_states->send_fn(req->ntvfs); \
473 /* check req->ntvfs->async_states->status and if not OK then send an error reply */
474 #define SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \
475 req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
476 if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
477 smbsrv_send_error(req, ntvfs->async_states->status); \
481 #define SMBSRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \
482 SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \
483 ptr = talloc_get_type(req->io_ptr, type); \
485 #define SMBSRV_CHECK_ASYNC_STATUS_SIMPLE do { \
486 req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
487 if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
488 smbsrv_send_error(req, ntvfs->async_states->status); \
492 #define SMBSRV_CHECK_ASYNC_STATUS(ptr, type) do { \
493 SMBSRV_CHECK_ASYNC_STATUS_SIMPLE; \
494 ptr = talloc_get_type(req->io_ptr, type); \
497 /* zero out some reserved fields in a reply */
498 #define SMBSRV_VWV_RESERVED(start, count) memset(req->out.vwv + VWV(start), 0, (count)*2)