s3-torture: introduce test_cli_read()
[Samba/gebeck_regimport.git] / source4 / smb_server / smb_server.h
blob6fcd9787bb705876fac37983d59afc73744fa40d
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 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(TALLOC_CTX *mem_ctx,
398 struct tevent_context *event_context,
399 struct loadparm_context *lp_ctx,
400 const struct model_ops *model_ops,
401 const char *address);
403 struct loadparm_context;
405 #include "smb_server/smb_server_proto.h"
406 #include "smb_server/smb/smb_proto.h"
408 /* useful way of catching wct errors with file and line number */
409 #define SMBSRV_CHECK_WCT(req, wcount) do { \
410 if ((req)->in.wct != (wcount)) { \
411 DEBUG(1,("Unexpected WCT %u at %s(%d) - expected %d\n", \
412 (req)->in.wct, __FILE__, __LINE__, wcount)); \
413 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror)); \
414 return; \
416 } while (0)
418 /* useful wrapper for talloc with NO_MEMORY reply */
419 #define SMBSRV_TALLOC_IO_PTR(ptr, type) do { \
420 ptr = talloc(req, type); \
421 if (!ptr) { \
422 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
423 return; \
425 req->io_ptr = ptr; \
426 } while (0)
428 #define SMBSRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \
429 req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \
430 req->session->session_info,\
431 SVAL(req->in.hdr,HDR_PID), \
432 req->request_time, \
433 req, send_fn, state); \
434 if (!req->ntvfs) { \
435 smbsrv_send_error(req, NT_STATUS_NO_MEMORY); \
436 return; \
438 (void)talloc_steal(req->tcon->ntvfs, req); \
439 req->ntvfs->frontend_data.private_data = req; \
440 } while (0)
442 #define SMBSRV_CHECK_FILE_HANDLE(handle) do { \
443 if (!handle) { \
444 smbsrv_send_error(req, NT_STATUS_INVALID_HANDLE); \
445 return; \
447 } while (0)
449 #define SMBSRV_CHECK_FILE_HANDLE_ERROR(handle, _status) do { \
450 if (!handle) { \
451 smbsrv_send_error(req, _status); \
452 return; \
454 } while (0)
456 #define SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(handle) do { \
457 if (!handle) { \
458 return NT_STATUS_INVALID_HANDLE; \
460 } while (0)
462 #define SMBSRV_CHECK(cmd) do {\
463 NTSTATUS _status; \
464 _status = cmd; \
465 if (!NT_STATUS_IS_OK(_status)) { \
466 smbsrv_send_error(req, _status); \
467 return; \
469 } while (0)
472 check if the backend wants to handle the request asynchronously.
473 if it wants it handled synchronously then call the send function
474 immediately
476 #define SMBSRV_CALL_NTVFS_BACKEND(cmd) do { \
477 req->ntvfs->async_states->status = cmd; \
478 if (req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC) { \
479 DLIST_ADD_END(req->smb_conn->requests, req, struct smbsrv_request *); \
480 } else { \
481 req->ntvfs->async_states->send_fn(req->ntvfs); \
483 } while (0)
485 /* check req->ntvfs->async_states->status and if not OK then send an error reply */
486 #define SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \
487 req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
488 if (ntvfs->async_states->state & NTVFS_ASYNC_STATE_CLOSE || NT_STATUS_EQUAL(ntvfs->async_states->status, NT_STATUS_NET_WRITE_FAULT)) { \
489 smbsrv_terminate_connection(req->smb_conn, get_friendly_nt_error_msg (ntvfs->async_states->status)); \
490 talloc_free(req); \
491 return; \
493 if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
494 smbsrv_send_error(req, ntvfs->async_states->status); \
495 return; \
497 } while (0)
498 #define SMBSRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \
499 SMBSRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \
500 ptr = talloc_get_type(req->io_ptr, type); \
501 } while (0)
502 #define SMBSRV_CHECK_ASYNC_STATUS_SIMPLE do { \
503 req = talloc_get_type(ntvfs->async_states->private_data, struct smbsrv_request); \
504 if (ntvfs->async_states->state & NTVFS_ASYNC_STATE_CLOSE || NT_STATUS_EQUAL(ntvfs->async_states->status, NT_STATUS_NET_WRITE_FAULT)) { \
505 smbsrv_terminate_connection(req->smb_conn, get_friendly_nt_error_msg (ntvfs->async_states->status)); \
506 talloc_free(req); \
507 return; \
509 if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
510 smbsrv_send_error(req, ntvfs->async_states->status); \
511 return; \
513 } while (0)
514 #define SMBSRV_CHECK_ASYNC_STATUS(ptr, type) do { \
515 SMBSRV_CHECK_ASYNC_STATUS_SIMPLE; \
516 ptr = talloc_get_type(req->io_ptr, type); \
517 } while (0)
519 /* zero out some reserved fields in a reply */
520 #define SMBSRV_VWV_RESERVED(start, count) memset(req->out.vwv + VWV(start), 0, (count)*2)
522 #include "smb_server/service_smb_proto.h"