6ae571844178f530393d6bc48de19e38b773fbf5
[samba.git] / source3 / smbd / globals.h
1 /*
2    Unix SMB/Netbios implementation.
3    smbd globals
4    Copyright (C) Stefan Metzmacher 2009
5    Copyright (C) Jeremy Allison 2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "system/select.h"
22 #include "librpc/gen_ndr/smbXsrv.h"
23 #include "smbprofile.h"
24
25 #ifdef USE_DMAPI
26 struct smbd_dmapi_context;
27 extern struct smbd_dmapi_context *dmapi_ctx;
28 #endif
29
30 /* how many write cache buffers have been allocated */
31 extern unsigned int allocated_write_caches;
32
33 /* A singleton cache to speed up searching by dev/inode. */
34 struct fsp_singleton_cache {
35         files_struct *fsp;
36         struct file_id id;
37 };
38
39 extern const struct mangle_fns *mangle_fns;
40
41 extern unsigned char *chartest;
42 struct tdb_context;
43 extern struct tdb_context *tdb_mangled_cache;
44
45 /*
46   this determines how many characters are used from the original filename
47   in the 8.3 mangled name. A larger value leads to a weaker hash and more collisions.
48   The largest possible value is 6.
49 */
50 extern unsigned mangle_prefix;
51
52 struct msg_state;
53
54 extern bool logged_ioctl_message;
55
56 extern int trans_num;
57
58 extern time_t last_smb_conf_reload_time;
59 extern time_t last_printer_reload_time;
60 extern pid_t background_lpq_updater_pid;
61
62 /****************************************************************************
63  structure to hold a linked list of queued messages.
64  for processing.
65 ****************************************************************************/
66 extern uint32_t common_flags2;
67
68 extern struct smb_trans_enc_state *partial_srv_trans_enc_ctx;
69 extern struct smb_trans_enc_state *srv_trans_enc_ctx;
70
71 struct sec_ctx {
72         struct security_unix_token ut;
73         struct security_token *token;
74 };
75 /* A stack of security contexts.  We include the current context as being
76    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
77 extern struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
78 extern int sec_ctx_stack_ndx;
79 extern bool become_uid_done;
80 extern bool become_gid_done;
81
82 extern connection_struct *last_conn;
83 extern uint16_t last_flags;
84
85 extern uint32_t global_client_caps;
86
87 extern uint16_t fnf_handle;
88
89 struct conn_ctx {
90         connection_struct *conn;
91         uint64_t vuid;
92         bool need_chdir;
93         bool done_chdir;
94         userdom_struct user_info;
95 };
96 /* A stack of current_user connection contexts. */
97 extern struct conn_ctx conn_ctx_stack[MAX_SEC_CTX_DEPTH];
98 extern int conn_ctx_stack_ndx;
99
100 struct vfs_init_function_entry;
101 extern struct vfs_init_function_entry *backends;
102 extern char *sparse_buf;
103 extern char *LastDir;
104
105 struct smbd_parent_context;
106 extern struct smbd_parent_context *am_parent;
107 extern struct memcache *smbd_memcache_ctx;
108 extern bool exit_firsttime;
109
110 struct tstream_context;
111 struct smbd_smb2_request;
112
113 DATA_BLOB negprot_spnego(TALLOC_CTX *ctx, struct smbXsrv_connection *xconn);
114
115 void smbd_lock_socket(struct smbXsrv_connection *xconn);
116 void smbd_unlock_socket(struct smbXsrv_connection *xconn);
117
118 NTSTATUS smbd_do_locking(struct smb_request *req,
119                          files_struct *fsp,
120                          uint8_t type,
121                          int32_t timeout,
122                          uint16_t num_locks,
123                          struct smbd_lock_element *locks,
124                          bool *async);
125 NTSTATUS smbd_do_unlocking(struct smb_request *req,
126                            files_struct *fsp,
127                            uint16_t num_ulocks,
128                            struct smbd_lock_element *ulocks);
129
130 NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
131                                TALLOC_CTX *mem_ctx,
132                                uint16_t info_level,
133                                files_struct *fsp,
134                                struct smb_filename *smb_fname,
135                                bool delete_pending,
136                                struct timespec write_time_ts,
137                                struct ea_list *ea_list,
138                                int lock_data_count,
139                                char *lock_data,
140                                uint16_t flags2,
141                                unsigned int max_data_bytes,
142                                size_t *fixed_portion,
143                                char **ppdata,
144                                unsigned int *pdata_size);
145
146 NTSTATUS smbd_do_setfsinfo(connection_struct *conn,
147                                 struct smb_request *req,
148                                 TALLOC_CTX *mem_ctx,
149                                 uint16_t info_level,
150                                 files_struct *fsp,
151                                 const DATA_BLOB *pdata);
152
153 NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
154                                 struct smb_request *req,
155                                 TALLOC_CTX *mem_ctx,
156                                 uint16_t info_level,
157                                 files_struct *fsp,
158                                 struct smb_filename *smb_fname,
159                                 char **ppdata, int total_data,
160                                 int *ret_data_size);
161
162 NTSTATUS smbd_do_qfsinfo(struct smbXsrv_connection *xconn,
163                          connection_struct *conn,
164                          TALLOC_CTX *mem_ctx,
165                          uint16_t info_level,
166                          uint16_t flags2,
167                          unsigned int max_data_bytes,
168                          size_t *fixed_portion,
169                          struct smb_filename *smb_fname,
170                          char **ppdata,
171                          int *ret_data_len);
172
173 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
174                            struct dptr_struct *dirptr,
175                            const char *mask,
176                            uint32_t dirtype,
177                            bool dont_descend,
178                            bool ask_sharemode,
179                            bool get_dosmode,
180                            bool (*match_fn)(TALLOC_CTX *ctx,
181                                             void *private_data,
182                                             const char *dname,
183                                             const char *mask,
184                                             char **_fname),
185                            bool (*mode_fn)(TALLOC_CTX *ctx,
186                                            void *private_data,
187                                            struct smb_filename *smb_fname,
188                                            bool get_dosmode,
189                                            uint32_t *_mode),
190                            void *private_data,
191                            char **_fname,
192                            struct smb_filename **_smb_fname,
193                            uint32_t *_mode,
194                            long *_prev_offset);
195
196 NTSTATUS smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
197                                connection_struct *conn,
198                                struct dptr_struct *dirptr,
199                                uint16_t flags2,
200                                const char *path_mask,
201                                uint32_t dirtype,
202                                int info_level,
203                                int requires_resume_key,
204                                bool dont_descend,
205                                bool ask_sharemode,
206                                bool get_dosmode,
207                                uint8_t align,
208                                bool do_pad,
209                                char **ppdata,
210                                char *base_data,
211                                char *end_data,
212                                int space_remaining,
213                                struct smb_filename **smb_fname,
214                                bool *got_exact_match,
215                                int *_last_entry_off,
216                                struct ea_list *name_list,
217                                struct file_id *file_id);
218
219 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
220                                     const struct smb_filename *smb_fname,
221                                     bool use_privs,
222                                     uint32_t access_mask,
223                                     uint32_t *access_mask_out);
224
225 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq);
226
227 void smbd_server_connection_terminate_ex(struct smbXsrv_connection *xconn,
228                                          const char *reason,
229                                          const char *location);
230 #define smbd_server_connection_terminate(xconn, reason) \
231         smbd_server_connection_terminate_ex(xconn, reason, __location__)
232
233 const char *smb2_opcode_name(uint16_t opcode);
234 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size);
235 bool smbd_smb2_is_compound(const struct smbd_smb2_request *req);
236
237 NTSTATUS smbd_add_connection(struct smbXsrv_client *client, int sock_fd,
238                              struct smbXsrv_connection **_xconn);
239
240 void reply_smb2002(struct smb_request *req, uint16_t choice);
241 void reply_smb20ff(struct smb_request *req, uint16_t choice);
242 void smbd_smb2_process_negprot(struct smbXsrv_connection *xconn,
243                                uint64_t expected_seq_low,
244                                const uint8_t *inpdu, size_t size);
245
246 DATA_BLOB smbd_smb2_generate_outbody(struct smbd_smb2_request *req, size_t size);
247
248 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
249                                     NTSTATUS status,
250                                     DATA_BLOB *info,
251                                     const char *location);
252 #define smbd_smb2_request_error(req, status) \
253         smbd_smb2_request_error_ex(req, status, NULL, __location__)
254 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
255                                    NTSTATUS status,
256                                    DATA_BLOB body, DATA_BLOB *dyn,
257                                    const char *location);
258 #define smbd_smb2_request_done(req, body, dyn) \
259         smbd_smb2_request_done_ex(req, NT_STATUS_OK, body, dyn, __location__)
260
261 NTSTATUS smbd_smb2_send_oplock_break(struct smbXsrv_connection *xconn,
262                                      struct smbXsrv_session *session,
263                                      struct smbXsrv_tcon *tcon,
264                                      struct smbXsrv_open *op,
265                                      uint8_t oplock_level);
266 NTSTATUS smbd_smb2_send_lease_break(struct smbXsrv_connection *xconn,
267                                     uint16_t new_epoch,
268                                     uint32_t lease_flags,
269                                     struct smb2_lease_key *lease_key,
270                                     uint32_t current_lease_state,
271                                     uint32_t new_lease_state);
272
273 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
274                                          struct tevent_req *subreq,
275                                          uint32_t defer_time);
276
277 struct smb_request *smbd_smb2_fake_smb_request(struct smbd_smb2_request *req);
278 size_t smbd_smb2_unread_bytes(struct smbd_smb2_request *req);
279 void remove_smb2_chained_fsp(files_struct *fsp);
280
281 NTSTATUS smbd_smb2_request_verify_creditcharge(struct smbd_smb2_request *req,
282                                                uint32_t data_length);
283
284 NTSTATUS smbd_smb2_request_verify_sizes(struct smbd_smb2_request *req,
285                                         size_t expected_body_size);
286
287 void smb2_request_set_async_internal(struct smbd_smb2_request *req,
288                                      bool async_internal);
289
290 enum protocol_types smbd_smb2_protocol_dialect_match(const uint8_t *indyn,
291                                                      const int dialect_count,
292                                                      uint16_t *dialect);
293 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req);
294 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *req);
295 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req);
296 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req);
297 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req);
298 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *req);
299 NTSTATUS smbd_smb2_request_process_close(struct smbd_smb2_request *req);
300 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req);
301 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req);
302 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err);
303 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req);
304 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err);
305 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
306                                     int err);
307 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req);
308 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req);
309 NTSTATUS smbd_smb2_request_process_keepalive(struct smbd_smb2_request *req);
310 NTSTATUS smbd_smb2_request_process_query_directory(struct smbd_smb2_request *req);
311 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req);
312 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req);
313 NTSTATUS smbd_smb2_request_process_setinfo(struct smbd_smb2_request *req);
314 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req);
315 NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req);
316 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
317                                 struct tevent_immediate *im,
318                                 void *private_data);
319
320 struct deferred_open_record;
321
322 /* SMB1 -> SMB2 glue. */
323 void send_break_message_smb2(files_struct *fsp,
324                              uint32_t break_from,
325                              uint32_t break_to);
326 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req);
327 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
328                                 struct smb_request *req,
329                                 files_struct *fsp,
330                                 int lock_timeout,
331                                 int lock_num,
332                                 uint64_t smblctx,
333                                 enum brl_type lock_type,
334                                 enum brl_flavour lock_flav,
335                                 uint64_t offset,
336                                 uint64_t count,
337                                 uint64_t blocking_smblctx);
338 void process_blocking_lock_queue_smb2(
339         struct smbd_server_connection *sconn, struct timeval tv_curr);
340 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
341                         struct byte_range_lock *br_lck,
342                         enum file_close_type close_type);
343 /* From smbd/smb2_create.c */
344 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level);
345 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
346                         struct timeval *p_request_time,
347                         struct deferred_open_record **open_rec);
348 bool open_was_deferred_smb2(
349         struct smbXsrv_connection *xconn, uint64_t mid);
350 void remove_deferred_open_message_smb2(
351         struct smbXsrv_connection *xconn, uint64_t mid);
352 bool schedule_deferred_open_message_smb2(
353         struct smbXsrv_connection *xconn, uint64_t mid);
354 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
355                                 struct timeval request_time,
356                                 struct timeval timeout,
357                                 struct file_id id,
358                                 struct deferred_open_record *open_rec);
359
360 struct smbXsrv_client;
361
362 struct smbXsrv_preauth {
363         uint8_t sha512_value[64];
364 };
365
366 struct smbXsrv_connection {
367         struct smbXsrv_connection *prev, *next;
368
369         struct smbXsrv_client *client;
370
371         const struct tsocket_address *local_address;
372         const struct tsocket_address *remote_address;
373         const char *remote_hostname;
374
375         enum protocol_types protocol;
376
377         struct {
378                 NTSTATUS status;
379                 int sock;
380                 struct tevent_fd *fde;
381
382                 struct {
383                         bool got_session;
384                 } nbt;
385         } transport;
386
387         struct {
388                 struct {
389                         /*
390                          * fd for the fcntl lock and process shared
391                          * robust mutex to coordinate access to the
392                          * client socket. When the system supports
393                          * process shared robust mutexes, those are
394                          * used. If not, then the fcntl lock will be
395                          * used.
396                          */
397                         int socket_lock_fd;
398 #ifdef HAVE_ROBUST_MUTEXES
399                         pthread_mutex_t *socket_mutex;
400 #endif
401
402                         /*
403                          * fd for the trusted pipe from
404                          * echo handler child
405                          */
406                         int trusted_fd;
407
408                         /*
409                          * fde for the trusted_fd
410                          */
411                         struct tevent_fd *trusted_fde;
412
413                         /*
414                          * Reference count for the fcntl lock to
415                          * allow recursive locks.
416                          */
417                         int ref_count;
418                 } echo_handler;
419
420                 struct {
421                         bool encrypted_passwords;
422                         bool spnego;
423                         struct auth4_context *auth_context;
424                         bool done;
425                         /*
426                          * Size of the data we can receive. Set by us.
427                          * Can be modified by the max xmit parameter.
428                          */
429                         int max_recv;
430                 } negprot;
431
432                 struct {
433                         bool done_sesssetup;
434                         /*
435                          * Size of data we can send to client. Set
436                          *  by the client for all protocols above CORE.
437                          *  Set by us for CORE protocol.
438                          */
439                         int max_send;
440                 } sessions;
441                 struct smb_signing_state *signing_state;
442
443                 struct {
444                         uint16_t client_major;
445                         uint16_t client_minor;
446                         uint32_t client_cap_low;
447                         uint32_t client_cap_high;
448                 } unix_info;
449
450                 struct msg_state *msg_state;
451         } smb1;
452         struct {
453                 struct smbd_smb2_request_read_state {
454                         struct smbd_smb2_request *req;
455                         struct {
456                                 uint8_t nbt[NBT_HDR_SIZE];
457                                 bool done;
458                         } hdr;
459                         struct iovec vector;
460                         bool doing_receivefile;
461                         size_t min_recv_size;
462                         size_t pktfull;
463                         size_t pktlen;
464                         uint8_t *pktbuf;
465                 } request_read_state;
466                 struct smbd_smb2_send_queue *send_queue;
467                 size_t send_queue_len;
468
469                 struct {
470                         /*
471                          * seq_low is the lowest sequence number
472                          * we will accept.
473                          */
474                         uint64_t seq_low;
475                         /*
476                          * seq_range is the range of credits we have
477                          * granted from the sequence windows starting
478                          * at seq_low.
479                          *
480                          * This gets incremented when new credits are
481                          * granted and gets decremented when the
482                          * lowest sequence number is consumed
483                          * (when seq_low gets incremented).
484                          */
485                         uint16_t seq_range;
486                         /*
487                          * The number of credits we have currently granted
488                          * to the client.
489                          *
490                          * This gets incremented when new credits are
491                          * granted and gets decremented when any credit
492                          * is comsumed.
493                          *
494                          * Note: the decrementing is different compared
495                          *       to seq_range.
496                          */
497                         uint16_t granted;
498                         /*
499                          * The maximum number of credits we will ever
500                          * grant to the client.
501                          *
502                          * Typically we will only grant 1/16th of
503                          * max_credits.
504                          *
505                          * This is the "server max credits" parameter.
506                          */
507                         uint16_t max;
508                         /*
509                          * a bitmap of size max_credits
510                          */
511                         struct bitmap *bitmap;
512                         bool multicredit;
513                 } credits;
514
515                 bool allow_2ff;
516                 struct {
517                         uint32_t capabilities;
518                         struct GUID guid;
519                         bool guid_verified;
520                         uint16_t security_mode;
521                         uint16_t num_dialects;
522                         uint16_t *dialects;
523                 } client;
524                 struct {
525                         uint32_t capabilities;
526                         struct GUID guid;
527                         uint16_t security_mode;
528                         uint16_t dialect;
529                         uint32_t max_trans;
530                         uint32_t max_read;
531                         uint32_t max_write;
532                         uint16_t cipher;
533                 } server;
534
535                 struct smbXsrv_preauth preauth;
536
537                 struct smbd_smb2_request *requests;
538         } smb2;
539 };
540
541 const char *smbXsrv_connection_dbg(const struct smbXsrv_connection *xconn);
542
543 NTSTATUS smbXsrv_version_global_init(const struct server_id *server_id);
544 uint32_t smbXsrv_version_global_current(void);
545
546 struct smbXsrv_client_table;
547 NTSTATUS smbXsrv_client_global_init(void);
548 NTSTATUS smbXsrv_client_create(TALLOC_CTX *mem_ctx,
549                                struct tevent_context *ev_ctx,
550                                struct messaging_context *msg_ctx,
551                                NTTIME now,
552                                struct smbXsrv_client **_client);
553 NTSTATUS smbXsrv_client_update(struct smbXsrv_client *client);
554 NTSTATUS smbXsrv_client_remove(struct smbXsrv_client *client);
555 NTSTATUS smb2srv_client_lookup_global(struct smbXsrv_client *client,
556                                       struct GUID client_guid,
557                                       TALLOC_CTX *mem_ctx,
558                                       struct smbXsrv_client_global0 **_pass);
559 NTSTATUS smb2srv_client_connection_pass(struct smbd_smb2_request *smb2req,
560                                         struct smbXsrv_client_global0 *global);
561
562 NTSTATUS smbXsrv_connection_init_tables(struct smbXsrv_connection *conn,
563                                         enum protocol_types protocol);
564
565 NTSTATUS smbXsrv_session_global_init(struct messaging_context *msg_ctx);
566 NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
567                                 NTTIME now,
568                                 struct smbXsrv_session **_session);
569 NTSTATUS smbXsrv_session_add_channel(struct smbXsrv_session *session,
570                                      struct smbXsrv_connection *conn,
571                                      struct smbXsrv_channel_global0 **_c);
572 NTSTATUS smbXsrv_session_update(struct smbXsrv_session *session);
573 struct smbXsrv_channel_global0;
574 NTSTATUS smbXsrv_session_find_channel(const struct smbXsrv_session *session,
575                                       const struct smbXsrv_connection *conn,
576                                       struct smbXsrv_channel_global0 **_c);
577 NTSTATUS smbXsrv_session_find_auth(const struct smbXsrv_session *session,
578                                    const struct smbXsrv_connection *conn,
579                                    NTTIME now,
580                                    struct smbXsrv_session_auth0 **_a);
581 NTSTATUS smbXsrv_session_create_auth(struct smbXsrv_session *session,
582                                      struct smbXsrv_connection *conn,
583                                      NTTIME now,
584                                      uint8_t in_flags,
585                                      uint8_t in_security_mode,
586                                      struct smbXsrv_session_auth0 **_a);
587 struct tevent_req *smb2srv_session_shutdown_send(TALLOC_CTX *mem_ctx,
588                                         struct tevent_context *ev,
589                                         struct smbXsrv_session *session,
590                                         struct smbd_smb2_request *current_req);
591 NTSTATUS smb2srv_session_shutdown_recv(struct tevent_req *req);
592 NTSTATUS smbXsrv_session_logoff(struct smbXsrv_session *session);
593 NTSTATUS smbXsrv_session_logoff_all(struct smbXsrv_client *client);
594 NTSTATUS smb1srv_session_table_init(struct smbXsrv_connection *conn);
595 NTSTATUS smb1srv_session_lookup(struct smbXsrv_connection *conn,
596                                 uint16_t vuid, NTTIME now,
597                                 struct smbXsrv_session **session);
598 NTSTATUS smb2srv_session_table_init(struct smbXsrv_connection *conn);
599 NTSTATUS smb2srv_session_lookup_conn(struct smbXsrv_connection *conn,
600                                      uint64_t session_id, NTTIME now,
601                                      struct smbXsrv_session **session);
602 NTSTATUS smb2srv_session_lookup_client(struct smbXsrv_client *client,
603                                        uint64_t session_id, NTTIME now,
604                                        struct smbXsrv_session **session);
605 struct smbXsrv_session_global0;
606 NTSTATUS smbXsrv_session_global_traverse(
607                         int (*fn)(struct smbXsrv_session_global0 *, void *),
608                         void *private_data);
609 struct tevent_req *smb2srv_session_close_previous_send(TALLOC_CTX *mem_ctx,
610                                         struct tevent_context *ev,
611                                         struct smbXsrv_connection *conn,
612                                         struct auth_session_info *session_info,
613                                         uint64_t previous_session_id,
614                                         uint64_t current_session_id);
615 NTSTATUS smb2srv_session_close_previous_recv(struct tevent_req *req);
616
617 NTSTATUS smbXsrv_tcon_global_init(void);
618 NTSTATUS smbXsrv_tcon_update(struct smbXsrv_tcon *tcon);
619 NTSTATUS smbXsrv_tcon_disconnect(struct smbXsrv_tcon *tcon, uint64_t vuid);
620 NTSTATUS smb1srv_tcon_table_init(struct smbXsrv_connection *conn);
621 NTSTATUS smb1srv_tcon_create(struct smbXsrv_connection *conn,
622                              NTTIME now,
623                              struct smbXsrv_tcon **_tcon);
624 NTSTATUS smb1srv_tcon_lookup(struct smbXsrv_connection *conn,
625                              uint16_t tree_id, NTTIME now,
626                              struct smbXsrv_tcon **tcon);
627 NTSTATUS smb1srv_tcon_disconnect_all(struct smbXsrv_client *client);
628 NTSTATUS smb2srv_tcon_table_init(struct smbXsrv_session *session);
629 NTSTATUS smb2srv_tcon_create(struct smbXsrv_session *session,
630                              NTTIME now,
631                              struct smbXsrv_tcon **_tcon);
632 NTSTATUS smb2srv_tcon_lookup(struct smbXsrv_session *session,
633                              uint32_t tree_id, NTTIME now,
634                              struct smbXsrv_tcon **tcon);
635 NTSTATUS smb2srv_tcon_disconnect_all(struct smbXsrv_session *session);
636 struct smbXsrv_tcon_global0;
637 NTSTATUS smbXsrv_tcon_global_traverse(
638                         int (*fn)(struct smbXsrv_tcon_global0 *, void *),
639                         void *private_data);
640
641 NTSTATUS smbXsrv_open_global_init(void);
642 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
643                              struct auth_session_info *session_info,
644                              NTTIME now,
645                              struct smbXsrv_open **_open);
646 uint32_t smbXsrv_open_hash(struct smbXsrv_open *_open);
647 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *_open);
648 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now);
649 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn);
650 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
651                              uint16_t fnum, NTTIME now,
652                              struct smbXsrv_open **_open);
653 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn);
654 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
655                              uint64_t persistent_id,
656                              uint64_t volatile_id,
657                              NTTIME now,
658                              struct smbXsrv_open **_open);
659 NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
660                                           const struct GUID *create_guid,
661                                           NTTIME now,
662                                           struct smbXsrv_open **_open);
663 NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
664                                struct auth_session_info *session_info,
665                                uint64_t persistent_id,
666                                const struct GUID *create_guid,
667                                NTTIME now,
668                                struct smbXsrv_open **_open);
669 struct smbXsrv_open_global0;
670 NTSTATUS smbXsrv_open_global_traverse(
671         int (*fn)(struct smbXsrv_open_global0 *, void *),
672         void *private_data);
673
674 NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id);
675 bool smbXsrv_is_encrypted(uint8_t encryption_flags);
676 bool smbXsrv_is_partially_encrypted(uint8_t encryption_flags);
677 bool smbXsrv_set_crypto_flag(uint8_t *flags, uint8_t flag);
678 bool smbXsrv_is_signed(uint8_t signing_flags);
679 bool smbXsrv_is_partially_signed(uint8_t signing_flags);
680
681 struct smbd_smb2_send_queue {
682         struct smbd_smb2_send_queue *prev, *next;
683
684         DATA_BLOB *sendfile_header;
685         NTSTATUS *sendfile_status;
686         struct iovec *vector;
687         int count;
688
689         TALLOC_CTX *mem_ctx;
690 };
691
692 struct smbd_smb2_request {
693         struct smbd_smb2_request *prev, *next;
694
695         struct smbd_server_connection *sconn;
696         struct smbXsrv_connection *xconn;
697
698         struct smbd_smb2_send_queue queue_entry;
699
700         /* the session the request operates on, maybe NULL */
701         struct smbXsrv_session *session;
702         uint64_t last_session_id;
703
704         /* the tcon the request operates on, maybe NULL */
705         struct smbXsrv_tcon *tcon;
706         uint32_t last_tid;
707
708         /* the tevent_context (wrapper) the request operates on */
709         struct tevent_context *ev_ctx;
710
711         int current_idx;
712         bool do_signing;
713         /* Was the request encrypted? */
714         bool was_encrypted;
715         /* Should we encrypt? */
716         bool do_encryption;
717         struct tevent_timer *async_te;
718         bool compound_related;
719
720         /*
721          * Give the implementation of an SMB2 req a way to tell the SMB2 request
722          * processing engine that the internal request is going async, while
723          * preserving synchronous SMB2 behaviour.
724          */
725         bool async_internal;
726
727         /*
728          * the encryption key for the whole
729          * compound chain
730          */
731         DATA_BLOB first_key;
732         /*
733          * the signing key for the last
734          * request/response of a compound chain
735          */
736         DATA_BLOB last_key;
737         struct smbXsrv_preauth *preauth;
738
739         struct timeval request_time;
740
741         SMBPROFILE_IOBYTES_ASYNC_STATE(profile);
742
743         /* fake smb1 request. */
744         struct smb_request *smb1req;
745         struct files_struct *compat_chain_fsp;
746
747         /*
748          * Keep track of whether the outstanding request counters
749          * had been updated in dispatch, so that they need to be
750          * adapted again in reply.
751          */
752         bool request_counters_updated;
753         uint64_t channel_generation;
754
755         /*
756          * The sub request for async backend calls.
757          * This is used for SMB2 Cancel.
758          */
759         struct tevent_req *subreq;
760
761 #define SMBD_SMB2_TF_IOV_OFS 0
762 #define SMBD_SMB2_HDR_IOV_OFS 1
763 #define SMBD_SMB2_BODY_IOV_OFS 2
764 #define SMBD_SMB2_DYN_IOV_OFS 3
765
766 #define SMBD_SMB2_NUM_IOV_PER_REQ 4
767
768 #define SMBD_SMB2_IOV_IDX_OFS(req,dir,idx,ofs) \
769         (&req->dir.vector[(idx)+(ofs)])
770
771 #define SMBD_SMB2_IDX_TF_IOV(req,dir,idx) \
772         SMBD_SMB2_IOV_IDX_OFS(req,dir,idx,SMBD_SMB2_TF_IOV_OFS)
773 #define SMBD_SMB2_IDX_HDR_IOV(req,dir,idx) \
774         SMBD_SMB2_IOV_IDX_OFS(req,dir,idx,SMBD_SMB2_HDR_IOV_OFS)
775 #define SMBD_SMB2_IDX_BODY_IOV(req,dir,idx) \
776         SMBD_SMB2_IOV_IDX_OFS(req,dir,idx,SMBD_SMB2_BODY_IOV_OFS)
777 #define SMBD_SMB2_IDX_DYN_IOV(req,dir,idx) \
778         SMBD_SMB2_IOV_IDX_OFS(req,dir,idx,SMBD_SMB2_DYN_IOV_OFS)
779
780 #define SMBD_SMB2_IN_TF_IOV(req)    SMBD_SMB2_IDX_TF_IOV(req,in,req->current_idx)
781 #define SMBD_SMB2_IN_TF_PTR(req)    (uint8_t *)(SMBD_SMB2_IN_TF_IOV(req)->iov_base)
782 #define SMBD_SMB2_IN_HDR_IOV(req)    SMBD_SMB2_IDX_HDR_IOV(req,in,req->current_idx)
783 #define SMBD_SMB2_IN_HDR_PTR(req)    (uint8_t *)(SMBD_SMB2_IN_HDR_IOV(req)->iov_base)
784 #define SMBD_SMB2_IN_BODY_IOV(req)   SMBD_SMB2_IDX_BODY_IOV(req,in,req->current_idx)
785 #define SMBD_SMB2_IN_BODY_PTR(req)   (uint8_t *)(SMBD_SMB2_IN_BODY_IOV(req)->iov_base)
786 #define SMBD_SMB2_IN_BODY_LEN(req)   (SMBD_SMB2_IN_BODY_IOV(req)->iov_len)
787 #define SMBD_SMB2_IN_DYN_IOV(req)    SMBD_SMB2_IDX_DYN_IOV(req,in,req->current_idx)
788 #define SMBD_SMB2_IN_DYN_PTR(req)    (uint8_t *)(SMBD_SMB2_IN_DYN_IOV(req)->iov_base)
789 #define SMBD_SMB2_IN_DYN_LEN(req)    (SMBD_SMB2_IN_DYN_IOV(req)->iov_len)
790
791 #define SMBD_SMB2_OUT_TF_IOV(req)   SMBD_SMB2_IDX_TF_IOV(req,out,req->current_idx)
792 #define SMBD_SMB2_OUT_TF_PTR(req)   (uint8_t *)(SMBD_SMB2_OUT_TF_IOV(req)->iov_base)
793 #define SMBD_SMB2_OUT_HDR_IOV(req)   SMBD_SMB2_IDX_HDR_IOV(req,out,req->current_idx)
794 #define SMBD_SMB2_OUT_HDR_PTR(req)   (uint8_t *)(SMBD_SMB2_OUT_HDR_IOV(req)->iov_base)
795 #define SMBD_SMB2_OUT_BODY_IOV(req)  SMBD_SMB2_IDX_BODY_IOV(req,out,req->current_idx)
796 #define SMBD_SMB2_OUT_BODY_PTR(req)  (uint8_t *)(SMBD_SMB2_OUT_BODY_IOV(req)->iov_base)
797 #define SMBD_SMB2_OUT_BODY_LEN(req)  (SMBD_SMB2_OUT_BODY_IOV(req)->iov_len)
798 #define SMBD_SMB2_OUT_DYN_IOV(req)   SMBD_SMB2_IDX_DYN_IOV(req,out,req->current_idx)
799 #define SMBD_SMB2_OUT_DYN_PTR(req)   (uint8_t *)(SMBD_SMB2_OUT_DYN_IOV(req)->iov_base)
800 #define SMBD_SMB2_OUT_DYN_LEN(req)   (SMBD_SMB2_OUT_DYN_IOV(req)->iov_len)
801
802 #define SMBD_SMB2_SHORT_RECEIVEFILE_WRITE_LEN (SMB2_HDR_BODY + 0x30)
803
804         struct {
805                 /*
806                  * vector[0] TRANSPORT HEADER (empty)
807                  * .
808                  * vector[1] SMB2_TRANSFORM (optional)
809                  * vector[2] SMB2
810                  * vector[3] fixed body
811                  * vector[4] dynamic body
812                  * .
813                  * .
814                  * .
815                  * vector[5] SMB2_TRANSFORM (optional)
816                  * vector[6] SMB2
817                  * vector[7] fixed body
818                  * vector[8] dynamic body
819                  * .
820                  * .
821                  * .
822                  */
823                 struct iovec *vector;
824                 int vector_count;
825                 struct iovec _vector[1 + SMBD_SMB2_NUM_IOV_PER_REQ];
826         } in;
827         struct {
828                 /* the NBT header is not allocated */
829                 uint8_t nbt_hdr[4];
830                 /*
831                  * vector[0] TRANSPORT HEADER
832                  * .
833                  * vector[1] SMB2_TRANSFORM (optional)
834                  * vector[2] SMB2
835                  * vector[3] fixed body
836                  * vector[4] dynamic body
837                  * .
838                  * .
839                  * .
840                  * vector[5] SMB2_TRANSFORM (empty)
841                  * vector[6] SMB2
842                  * vector[7] fixed body
843                  * vector[8] dynamic body
844                  * .
845                  * .
846                  * .
847                  */
848                 struct iovec *vector;
849                 int vector_count;
850                 struct iovec _vector[1 + SMBD_SMB2_NUM_IOV_PER_REQ];
851 #define OUTVEC_ALLOC_SIZE (SMB2_HDR_BODY + 9)
852                 uint8_t _hdr[OUTVEC_ALLOC_SIZE];
853                 uint8_t _body[0x58];
854         } out;
855 };
856
857 struct smbd_server_connection;
858 struct user_struct;
859
860 struct pending_message_list;
861 struct pending_auth_data;
862
863 struct user_struct {
864         struct user_struct *next, *prev;
865         uint64_t vuid; /* Tag for this entry. */
866
867         char *session_keystr; /* used by utmp and pam session code.
868                                  TDB key string */
869         int homes_snum;
870
871         struct auth_session_info *session_info;
872
873         struct smbXsrv_session *session;
874 };
875
876 struct pthreadpool_tevent;
877
878 struct smbd_server_connection {
879         const struct tsocket_address *local_address;
880         const struct tsocket_address *remote_address;
881         const char *remote_hostname;
882         struct tevent_context *raw_ev_ctx;
883         struct tevent_context *root_ev_ctx;
884         struct tevent_context *guest_ev_ctx;
885         struct messaging_context *msg_ctx;
886         struct notify_context *notify_ctx;
887         bool using_smb2;
888         bool aapl_zero_file_id; /* Apple-specific */
889         int trans_num;
890
891         size_t num_users;
892         struct user_struct *users;
893
894         size_t num_connections;
895         struct connection_struct *connections;
896
897         size_t num_files;
898         struct files_struct *files;
899
900         int real_max_open_files;
901         struct fsp_singleton_cache fsp_fi_cache;
902
903         struct pending_message_list *deferred_open_queue;
904
905
906         /* open directory handles. */
907         struct {
908                 struct bitmap *dptr_bmap;
909                 struct dptr_struct *dirptrs;
910                 int dirhandles_open;
911         } searches;
912
913         uint64_t num_requests;
914
915         /* Current number of oplocks we have outstanding. */
916         struct {
917                 int32_t exclusive_open;
918                 int32_t level_II_open;
919                 struct kernel_oplocks *kernel_ops;
920         } oplocks;
921
922         struct {
923                 struct notify_mid_map *notify_mid_maps;
924
925                 struct {
926                         /* dlink list we store pending lock records on. */
927                         struct blocking_lock_record *blocking_lock_queue;
928                         /* dlink list we move cancelled lock records onto. */
929                         struct blocking_lock_record *blocking_lock_cancelled_queue;
930
931                         /* The event that makes us process our blocking lock queue */
932                         struct tevent_timer *brl_timeout;
933
934                         bool blocking_lock_unlock_state;
935                         bool blocking_lock_cancel_state;
936                 } locks;
937         } smb1;
938         struct {
939                 struct {
940                         /* The event that makes us process our blocking lock queue */
941                         struct tevent_timer *brl_timeout;
942                         bool blocking_lock_unlock_state;
943                 } locks;
944         } smb2;
945
946         struct pthreadpool_tevent *sync_thread_pool;
947         struct pthreadpool_tevent *raw_thread_pool;
948
949         struct smbXsrv_client *client;
950 };
951
952 extern struct smbXsrv_client *global_smbXsrv_client;
953
954 void smbd_init_globals(void);