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