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