Fix more SMB2 locking. We still crash but this won't last :-).
[kai/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 #if defined(WITH_AIO)
22 struct aio_extra;
23 extern struct aio_extra *aio_list_head;
24 extern struct tevent_signal *aio_signal_event;
25 extern int aio_pending_size;
26 extern int outstanding_aio_calls;
27 #endif
28
29 #ifdef USE_DMAPI
30 struct smbd_dmapi_context;
31 extern struct smbd_dmapi_context *dmapi_ctx;
32 #endif
33
34 extern bool dfree_broken;
35
36 /* how many write cache buffers have been allocated */
37 extern unsigned int allocated_write_caches;
38
39 extern int real_max_open_files;
40 extern struct bitmap *file_bmap;
41 extern files_struct *Files;
42 extern int files_used;
43 /* A singleton cache to speed up searching by dev/inode. */
44 struct fsp_singleton_cache {
45         files_struct *fsp;
46         struct file_id id;
47 };
48 extern struct fsp_singleton_cache fsp_fi_cache;
49 extern unsigned long file_gen_counter;
50 extern int first_file;
51
52 extern const struct mangle_fns *mangle_fns;
53
54 extern unsigned char *chartest;
55 extern TDB_CONTEXT *tdb_mangled_cache;
56
57 /* these tables are used to provide fast tests for characters */
58 extern unsigned char char_flags[256];
59 /*
60   this determines how many characters are used from the original filename
61   in the 8.3 mangled name. A larger value leads to a weaker hash and more collisions.
62   The largest possible value is 6.
63 */
64 extern unsigned mangle_prefix;
65 extern unsigned char base_reverse[256];
66
67 extern char *last_from;
68 extern char *last_to;
69
70 struct msg_state;
71 extern struct msg_state *smbd_msg_state;
72
73 extern bool logged_ioctl_message;
74
75 extern int trans_num;
76
77 extern pid_t mypid;
78 extern time_t last_smb_conf_reload_time;
79 extern time_t last_printer_reload_time;
80 /****************************************************************************
81  structure to hold a linked list of queued messages.
82  for processing.
83 ****************************************************************************/
84 struct pending_message_list;
85 extern struct pending_message_list *deferred_open_queue;
86 extern uint32_t common_flags2;
87
88 struct smb_srv_trans_enc_ctx;
89 extern struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx;
90 extern struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx;
91
92 struct sec_ctx {
93         UNIX_USER_TOKEN ut;
94         NT_USER_TOKEN *token;
95 };
96 /* A stack of security contexts.  We include the current context as being
97    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
98 extern struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
99 extern int sec_ctx_stack_ndx;
100 extern bool become_uid_done;
101 extern bool become_gid_done;
102
103 extern connection_struct *last_conn;
104 extern uint16_t last_flags;
105
106 extern uint32_t global_client_caps;
107
108 extern uint16_t fnf_handle;
109
110 struct conn_ctx {
111         connection_struct *conn;
112         uint16 vuid;
113 };
114 /* A stack of current_user connection contexts. */
115 extern struct conn_ctx conn_ctx_stack[MAX_SEC_CTX_DEPTH];
116 extern int conn_ctx_stack_ndx;
117
118 struct vfs_init_function_entry;
119 extern struct vfs_init_function_entry *backends;
120 extern char *sparse_buf;
121
122 /* Current number of oplocks we have outstanding. */
123 extern int32_t exclusive_oplocks_open;
124 extern int32_t level_II_oplocks_open;
125 extern bool global_client_failed_oplock_break;
126 extern struct kernel_oplocks *koplocks;
127
128 extern int am_parent;
129 extern int server_fd;
130 extern struct event_context *smbd_event_ctx;
131 extern struct messaging_context *smbd_msg_ctx;
132 extern struct memcache *smbd_memcache_ctx;
133 extern bool exit_firsttime;
134 struct child_pid;
135 extern struct child_pid *children;
136 extern int num_children;
137
138 struct tstream_context;
139 struct smbd_smb2_request;
140 struct smbd_smb2_session;
141 struct smbd_smb2_tcon;
142
143 DATA_BLOB negprot_spnego(void);
144
145 void smbd_lock_socket(struct smbd_server_connection *sconn);
146 void smbd_unlock_socket(struct smbd_server_connection *sconn);
147
148 NTSTATUS smb2_signing_sign_pdu(DATA_BLOB session_key,
149                                struct iovec *vector,
150                                int count);
151 NTSTATUS smb2_signing_check_pdu(DATA_BLOB session_key,
152                                 const struct iovec *vector,
153                                 int count);
154
155 NTSTATUS smbd_do_locking(struct smb_request *req,
156                          files_struct *fsp,
157                          uint8_t type,
158                          int32_t timeout,
159                          uint16_t num_ulocks,
160                          struct smbd_lock_element *ulocks,
161                          uint16_t num_locks,
162                          struct smbd_lock_element *locks,
163                          bool *async);
164
165 NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
166                                TALLOC_CTX *mem_ctx,
167                                uint16_t info_level,
168                                files_struct *fsp,
169                                struct smb_filename *smb_fname,
170                                bool delete_pending,
171                                struct timespec write_time_ts,
172                                struct ea_list *ea_list,
173                                int lock_data_count,
174                                char *lock_data,
175                                uint16_t flags2,
176                                unsigned int max_data_bytes,
177                                char **ppdata,
178                                unsigned int *pdata_size);
179
180 NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
181                                 struct smb_request *req,
182                                 TALLOC_CTX *mem_ctx,
183                                 uint16_t info_level,
184                                 files_struct *fsp,
185                                 struct smb_filename *smb_fname,
186                                 char **ppdata, int total_data,
187                                 int *ret_data_size);
188
189 NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
190                          TALLOC_CTX *mem_ctx,
191                          uint16_t info_level,
192                          uint16_t flags2,
193                          unsigned int max_data_bytes,
194                          char **ppdata,
195                          int *ret_data_len);
196
197 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
198                            struct dptr_struct *dirptr,
199                            const char *mask,
200                            uint32_t dirtype,
201                            bool dont_descend,
202                            bool ask_sharemode,
203                            bool (*match_fn)(TALLOC_CTX *ctx,
204                                             void *private_data,
205                                             const char *dname,
206                                             const char *mask,
207                                             char **_fname),
208                            bool (*mode_fn)(TALLOC_CTX *ctx,
209                                            void *private_data,
210                                            struct smb_filename *smb_fname,
211                                            uint32_t *_mode),
212                            void *private_data,
213                            char **_fname,
214                            struct smb_filename **_smb_fname,
215                            uint32_t *_mode,
216                            long *_prev_offset);
217
218 bool smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
219                                connection_struct *conn,
220                                struct dptr_struct *dirptr,
221                                uint16 flags2,
222                                const char *path_mask,
223                                uint32 dirtype,
224                                int info_level,
225                                int requires_resume_key,
226                                bool dont_descend,
227                                bool ask_sharemode,
228                                uint8_t align,
229                                bool do_pad,
230                                char **ppdata,
231                                char *base_data,
232                                char *end_data,
233                                int space_remaining,
234                                bool *out_of_space,
235                                bool *got_exact_match,
236                                int *_last_entry_off,
237                                struct ea_list *name_list);
238
239 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
240                                 const struct smb_filename *smb_fname,
241                                 uint32_t access_mask,
242                                 uint32_t *access_granted);
243
244 void smbd_notify_cancel_by_smbreq(struct smbd_server_connection *sconn,
245                                   const struct smb_request *smbreq);
246
247 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
248                                          const char *reason,
249                                          const char *location);
250 #define smbd_server_connection_terminate(sconn, reason) \
251         smbd_server_connection_terminate_ex(sconn, reason, __location__)
252
253 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size);
254
255 void reply_smb2002(struct smb_request *req, uint16_t choice);
256 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
257                              const uint8_t *inbuf, size_t size);
258
259 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
260                                     NTSTATUS status,
261                                     DATA_BLOB *info,
262                                     const char *location);
263 #define smbd_smb2_request_error(req, status) \
264         smbd_smb2_request_error_ex(req, status, NULL, __location__)
265 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
266                                    NTSTATUS status,
267                                    DATA_BLOB body, DATA_BLOB *dyn,
268                                    const char *location);
269 #define smbd_smb2_request_done(req, body, dyn) \
270         smbd_smb2_request_done_ex(req, NT_STATUS_OK, body, dyn, __location__)
271
272 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
273                                      uint64_t file_id_volatile,
274                                      uint8_t oplock_level);
275
276 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
277                                          struct tevent_req *subreq);
278
279 NTSTATUS smbd_smb2_request_check_session(struct smbd_smb2_request *req);
280 NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req);
281
282 struct smb_request *smbd_smb2_fake_smb_request(struct smbd_smb2_request *req);
283
284 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req);
285 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *req);
286 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req);
287 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req);
288 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req);
289 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *req);
290 NTSTATUS smbd_smb2_request_process_close(struct smbd_smb2_request *req);
291 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req);
292 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req);
293 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req);
294 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req);
295 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req);
296 NTSTATUS smbd_smb2_request_process_keepalive(struct smbd_smb2_request *req);
297 NTSTATUS smbd_smb2_request_process_find(struct smbd_smb2_request *req);
298 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req);
299 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req);
300 NTSTATUS smbd_smb2_request_process_setinfo(struct smbd_smb2_request *req);
301 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req);
302 NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req);
303 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
304                                 struct tevent_immediate *im,
305                                 void *private_data);
306
307 /* SMB1 -> SMB2 glue. */
308 void send_break_message_smb2(files_struct *fsp, int level);
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                                 uint32_t lock_pid,
316                                 enum brl_type lock_type,
317                                 enum brl_flavour lock_flav,
318                                 uint64_t offset,
319                                 uint64_t count,
320                                 uint32_t blocking_pid);
321 void process_blocking_lock_queue_smb2(struct timeval tv_curr);
322 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
323                         struct byte_range_lock *br_lck);
324 /* From smbd/smb2_create.c */
325 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level);
326 uint8_t map_samba_oplock_levels_to_smb2(int oplock_type);
327 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
328                         struct timeval *p_request_time,
329                         void **pp_state);
330 bool open_was_deferred_smb2(uint64_t mid);
331 void remove_deferred_open_message_smb2(uint64_t mid);
332 void schedule_deferred_open_message_smb2(uint64_t mid);
333 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
334                         struct timeval request_time,
335                         struct timeval timeout,
336                         struct file_id id,
337                         char *private_data,
338                         size_t priv_len);
339
340 struct smbd_smb2_request {
341         struct smbd_smb2_request *prev, *next;
342
343         TALLOC_CTX *mem_pool;
344         struct smbd_smb2_request **parent;
345
346         struct smbd_server_connection *sconn;
347
348         /* the session the request operates on, maybe NULL */
349         struct smbd_smb2_session *session;
350
351         /* the tcon the request operates on, maybe NULL */
352         struct smbd_smb2_tcon *tcon;
353
354         int current_idx;
355         bool do_signing;
356         bool async;
357         bool cancelled;
358
359         struct files_struct *compat_chain_fsp;
360
361         NTSTATUS next_status;
362
363         /*
364          * The sub request for async backend calls.
365          * This is used for SMB2 Cancel.
366          */
367         struct tevent_req *subreq;
368
369         struct {
370                 /* the NBT header is not allocated */
371                 uint8_t nbt_hdr[4];
372                 /*
373                  * vector[0] NBT
374                  * .
375                  * vector[1] SMB2
376                  * vector[2] fixed body
377                  * vector[3] dynamic body
378                  * .
379                  * .
380                  * .
381                  * vector[4] SMB2
382                  * vector[5] fixed body
383                  * vector[6] dynamic body
384                  * .
385                  * .
386                  * .
387                  */
388                 struct iovec *vector;
389                 int vector_count;
390         } in;
391         struct {
392                 /* the NBT header is not allocated */
393                 uint8_t nbt_hdr[4];
394                 /*
395                  * vector[0] NBT
396                  * .
397                  * vector[1] SMB2
398                  * vector[2] fixed body
399                  * vector[3] dynamic body
400                  * .
401                  * .
402                  * .
403                  * vector[4] SMB2
404                  * vector[5] fixed body
405                  * vector[6] dynamic body
406                  * .
407                  * .
408                  * .
409                  */
410                 struct iovec *vector;
411                 int vector_count;
412         } out;
413 };
414
415 struct smbd_server_connection;
416
417 struct smbd_smb2_session {
418         struct smbd_smb2_session *prev, *next;
419         struct smbd_server_connection *sconn;
420         NTSTATUS status;
421         uint64_t vuid;
422         AUTH_NTLMSSP_STATE *auth_ntlmssp_state;
423         struct auth_serversupplied_info *server_info;
424         DATA_BLOB session_key;
425         bool do_signing;
426
427         user_struct *compat_vuser;
428
429         struct {
430                 /* an id tree used to allocate tids */
431                 struct idr_context *idtree;
432
433                 /* this is the limit of tid values for this connection */
434                 uint32_t limit;
435
436                 struct smbd_smb2_tcon *list;
437         } tcons;
438 };
439
440 struct smbd_smb2_tcon {
441         struct smbd_smb2_tcon *prev, *next;
442         struct smbd_smb2_session *session;
443         uint32_t tid;
444         int snum;
445         connection_struct *compat_conn;
446 };
447
448 struct pending_auth_data;
449
450 struct smbd_server_connection {
451         const struct tsocket_address *local_address;
452         const struct tsocket_address *remote_address;
453         struct {
454                 bool got_session;
455         } nbt;
456         bool allow_smb2;
457         struct {
458                 struct fd_event *fde;
459
460                 struct {
461                         /*
462                          * fd for the fcntl lock mutexing access to smbd_server_fd
463                          */
464                         int socket_lock_fd;
465
466                         /*
467                          * fd for the trusted pipe from
468                          * echo handler child
469                          */
470                         int trusted_fd;
471
472                         /*
473                          * fde for the trusted_fd
474                          */
475                         struct fd_event *trusted_fde;
476
477                         /*
478                          * Reference count for the fcntl lock to
479                          * allow recursive locks.
480                          */
481                         int ref_count;
482                 } echo_handler;
483
484                 uint64_t num_requests;
485                 struct {
486                         bool encrypted_passwords;
487                         bool spnego;
488                         struct auth_context *auth_context;
489                         bool done;
490                         /*
491                          * Size of the data we can receive. Set by us.
492                          * Can be modified by the max xmit parameter.
493                          */
494                         int max_recv;
495                 } negprot;
496
497                 struct {
498                         bool done_sesssetup;
499                         /*
500                          * Size of data we can send to client. Set
501                          *  by the client for all protocols above CORE.
502                          *  Set by us for CORE protocol.
503                          */
504                         int max_send;
505                         uint16_t last_session_tag;
506
507                         /* users from session setup */
508                         char *session_userlist;
509                         /* workgroup from session setup. */
510                         char *session_workgroup;
511                         /*
512                          * this holds info on user ids that are already
513                          * validated for this VC
514                          */
515                         user_struct *validated_users;
516                         uint16_t next_vuid;
517                         int num_validated_vuids;
518 #ifdef HAVE_NETGROUP
519                         char *my_yp_domain;
520 #endif
521                 } sessions;
522                 struct {
523                         connection_struct *Connections;
524                         /* number of open connections */
525                         struct bitmap *bmap;
526                         int num_open;
527                 } tcons;
528                 struct smb_signing_state *signing_state;
529                 /* List to store partial SPNEGO auth fragments. */
530                 struct pending_auth_data *pd_list;
531
532                 struct notify_mid_map *notify_mid_maps;
533
534                 struct {
535                         struct bitmap *dptr_bmap;
536                         struct dptr_struct *dirptrs;
537                         int dirhandles_open;
538                 } searches;
539                 struct {
540                         /* dlink list we store pending lock records on. */
541                         struct blocking_lock_record *blocking_lock_queue;
542                         /* dlink list we move cancelled lock records onto. */
543                         struct blocking_lock_record *blocking_lock_cancelled_queue;
544
545                         /* The event that makes us process our blocking lock queue */
546                         struct timed_event *brl_timeout;
547
548                         bool blocking_lock_unlock_state;
549                         bool blocking_lock_cancel_state;
550                 } locks;
551         } smb1;
552         struct {
553                 struct tevent_context *event_ctx;
554                 struct tevent_queue *recv_queue;
555                 struct tevent_queue *send_queue;
556                 struct tstream_context *stream;
557                 struct {
558                         /* an id tree used to allocate vuids */
559                         /* this holds info on session vuids that are already
560                          * validated for this VC */
561                         struct idr_context *idtree;
562
563                         /* this is the limit of vuid values for this connection */
564                         uint64_t limit;
565
566                         struct smbd_smb2_session *list;
567                 } sessions;
568                 struct {
569                         /* The event that makes us process our blocking lock queue */
570                         struct timed_event *brl_timeout;
571                         bool blocking_lock_unlock_state;
572                 } locks;
573                 struct smbd_smb2_request *requests;
574         } smb2;
575 };
576
577 extern struct smbd_server_connection *smbd_server_conn;
578
579 void smbd_init_globals(void);