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