s3:smbd/globals: change 'int am_parent' into 'struct smbd_parent_context *am_parent'
[kai/samba.git] / source3 / smbd / globals.h
index 1017ff100689ddff352e33f5e91a8775dbde95f6..020e1bcddeb090d7d77b20537143180733f70594 100644 (file)
@@ -2,6 +2,7 @@
    Unix SMB/Netbios implementation.
    smbd globals
    Copyright (C) Stefan Metzmacher 2009
+   Copyright (C) Jeremy Allison 2010
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -17,6 +18,8 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "system/select.h"
+
 #if defined(WITH_AIO)
 struct aio_extra;
 extern struct aio_extra *aio_list_head;
@@ -25,18 +28,6 @@ extern int aio_pending_size;
 extern int outstanding_aio_calls;
 #endif
 
-/* dlink list we store pending lock records on. */
-extern struct blocking_lock_record *blocking_lock_queue;
-
-/* dlink list we move cancelled lock records onto. */
-extern struct blocking_lock_record *blocking_lock_cancelled_queue;
-
-/* The event that makes us process our blocking lock queue */
-extern struct timed_event *brl_timeout;
-
-extern bool blocking_lock_unlock_state;
-extern bool blocking_lock_cancel_state;
-
 #ifdef USE_DMAPI
 struct smbd_dmapi_context;
 extern struct smbd_dmapi_context *dmapi_ctx;
@@ -47,36 +38,24 @@ extern bool dfree_broken;
 /* how many write cache buffers have been allocated */
 extern unsigned int allocated_write_caches;
 
-extern int real_max_open_files;
-extern struct bitmap *file_bmap;
-extern files_struct *Files;
-extern int files_used;
 /* A singleton cache to speed up searching by dev/inode. */
 struct fsp_singleton_cache {
        files_struct *fsp;
        struct file_id id;
 };
-extern struct fsp_singleton_cache fsp_fi_cache;
-extern unsigned long file_gen_counter;
-extern int first_file;
 
 extern const struct mangle_fns *mangle_fns;
 
 extern unsigned char *chartest;
-extern TDB_CONTEXT *tdb_mangled_cache;
+struct tdb_context;
+extern struct tdb_context *tdb_mangled_cache;
 
-/* these tables are used to provide fast tests for characters */
-extern unsigned char char_flags[256];
 /*
   this determines how many characters are used from the original filename
   in the 8.3 mangled name. A larger value leads to a weaker hash and more collisions.
   The largest possible value is 6.
 */
 extern unsigned mangle_prefix;
-extern unsigned char base_reverse[256];
-
-extern char *last_from;
-extern char *last_to;
 
 struct msg_state;
 extern struct msg_state *smbd_msg_state;
@@ -85,24 +64,22 @@ extern bool logged_ioctl_message;
 
 extern int trans_num;
 
-extern pid_t mypid;
 extern time_t last_smb_conf_reload_time;
 extern time_t last_printer_reload_time;
+extern pid_t background_lpq_updater_pid;
+
 /****************************************************************************
  structure to hold a linked list of queued messages.
  for processing.
 ****************************************************************************/
-struct pending_message_list;
-extern struct pending_message_list *deferred_open_queue;
 extern uint32_t common_flags2;
 
-struct smb_srv_trans_enc_ctx;
-extern struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx;
-extern struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx;
+extern struct smb_trans_enc_state *partial_srv_trans_enc_ctx;
+extern struct smb_trans_enc_state *srv_trans_enc_ctx;
 
 struct sec_ctx {
-       UNIX_USER_TOKEN ut;
-       NT_USER_TOKEN *token;
+       struct security_unix_token ut;
+       struct security_token *token;
 };
 /* A stack of security contexts.  We include the current context as being
    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
@@ -114,8 +91,6 @@ extern bool become_gid_done;
 extern connection_struct *last_conn;
 extern uint16_t last_flags;
 
-extern struct db_context *session_db_ctx_ptr;
-
 extern uint32_t global_client_caps;
 
 extern uint16_t fnf_handle;
@@ -136,13 +111,10 @@ extern char *LastDir;
 /* Current number of oplocks we have outstanding. */
 extern int32_t exclusive_oplocks_open;
 extern int32_t level_II_oplocks_open;
-extern bool global_client_failed_oplock_break;
 extern struct kernel_oplocks *koplocks;
 
-extern int am_parent;
-extern int server_fd;
-extern struct event_context *smbd_event_ctx;
-extern struct messaging_context *smbd_msg_ctx;
+struct smbd_parent_context;
+extern struct smbd_parent_context *am_parent;
 extern struct memcache *smbd_memcache_ctx;
 extern bool exit_firsttime;
 struct child_pid;
@@ -154,21 +126,10 @@ struct smbd_smb2_request;
 struct smbd_smb2_session;
 struct smbd_smb2_tcon;
 
-DATA_BLOB negprot_spnego(void);
-
-NTSTATUS smb2_signing_sign_pdu(DATA_BLOB session_key,
-                              struct iovec *vector,
-                              int count);
-NTSTATUS smb2_signing_check_pdu(DATA_BLOB session_key,
-                               const struct iovec *vector,
-                               int count);
+DATA_BLOB negprot_spnego(TALLOC_CTX *ctx, struct smbd_server_connection *sconn);
 
-struct smbd_lock_element {
-       uint32_t smbpid;
-       enum brl_type brltype;
-       uint64_t offset;
-       uint64_t count;
-};
+void smbd_lock_socket(struct smbd_server_connection *sconn);
+void smbd_unlock_socket(struct smbd_server_connection *sconn);
 
 NTSTATUS smbd_do_locking(struct smb_request *req,
                         files_struct *fsp,
@@ -187,7 +148,6 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                               struct smb_filename *smb_fname,
                               bool delete_pending,
                               struct timespec write_time_ts,
-                              bool ms_dfs_link,
                               struct ea_list *ea_list,
                               int lock_data_count,
                               char *lock_data,
@@ -255,13 +215,13 @@ bool smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
                               int *_last_entry_off,
                               struct ea_list *name_list);
 
-NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
-                               const struct smb_filename *smb_fname,
-                               uint32_t access_mask,
-                               uint32_t *access_granted);
+NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
+                                   const struct smb_filename *smb_fname,
+                                   bool file_existed,
+                                   uint32_t access_mask,
+                                   uint32_t *access_mask_out);
 
-void smbd_notify_cancel_by_smbreq(struct smbd_server_connection *sconn,
-                                 const struct smb_request *smbreq);
+void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq);
 
 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
                                         const char *reason,
@@ -269,9 +229,13 @@ void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
 #define smbd_server_connection_terminate(sconn, reason) \
        smbd_server_connection_terminate_ex(sconn, reason, __location__)
 
+struct server_id sconn_server_id(const struct smbd_server_connection *sconn);
+
+const char *smb2_opcode_name(uint16_t opcode);
 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size);
 
 void reply_smb2002(struct smb_request *req, uint16_t choice);
+void reply_smb20ff(struct smb_request *req, uint16_t choice);
 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
                             const uint8_t *inbuf, size_t size);
 
@@ -294,12 +258,14 @@ NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
                                     uint8_t oplock_level);
 
 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
-                                        struct tevent_req *subreq);
-
-NTSTATUS smbd_smb2_request_check_session(struct smbd_smb2_request *req);
-NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req);
+                                        struct tevent_req *subreq,
+                                        uint32_t defer_time);
 
 struct smb_request *smbd_smb2_fake_smb_request(struct smbd_smb2_request *req);
+void remove_smb2_chained_fsp(files_struct *fsp);
+
+NTSTATUS smbd_smb2_request_verify_sizes(struct smbd_smb2_request *req,
+                                       size_t expected_body_size);
 
 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *req);
@@ -310,7 +276,9 @@ NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_close(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req);
+NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err);
 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req);
+NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err);
 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_keepalive(struct smbd_smb2_request *req);
@@ -319,6 +287,47 @@ NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_getinfo(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_setinfo(struct smbd_smb2_request *req);
 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req);
+NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req);
+void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
+                               struct tevent_immediate *im,
+                               void *private_data);
+
+/* SMB1 -> SMB2 glue. */
+void send_break_message_smb2(files_struct *fsp, int level);
+struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req);
+bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
+                               struct smb_request *req,
+                               files_struct *fsp,
+                               int lock_timeout,
+                               int lock_num,
+                               uint64_t smblctx,
+                               enum brl_type lock_type,
+                               enum brl_flavour lock_flav,
+                               uint64_t offset,
+                               uint64_t count,
+                               uint64_t blocking_smblctx);
+void process_blocking_lock_queue_smb2(
+       struct smbd_server_connection *sconn, struct timeval tv_curr);
+void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
+                       struct byte_range_lock *br_lck,
+                       enum file_close_type close_type);
+/* From smbd/smb2_create.c */
+int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level);
+bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
+                       struct timeval *p_request_time,
+                       void **pp_state);
+bool open_was_deferred_smb2(struct smbd_server_connection *sconn,
+                           uint64_t mid);
+void remove_deferred_open_message_smb2(
+       struct smbd_server_connection *sconn, uint64_t mid);
+void schedule_deferred_open_message_smb2(
+       struct smbd_server_connection *sconn, uint64_t mid);
+bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
+                       struct timeval request_time,
+                       struct timeval timeout,
+                       struct file_id id,
+                       char *private_data,
+                       size_t priv_len);
 
 struct smbd_smb2_request {
        struct smbd_smb2_request *prev, *next;
@@ -330,13 +339,20 @@ struct smbd_smb2_request {
 
        /* the session the request operates on, maybe NULL */
        struct smbd_smb2_session *session;
+       uint64_t last_session_id;
 
        /* the tcon the request operates on, maybe NULL */
        struct smbd_smb2_tcon *tcon;
+       uint32_t last_tid;
 
        int current_idx;
        bool do_signing;
+       struct tevent_timer *async_te;
+       bool cancelled;
+       bool compound_related;
 
+       /* fake smb1 request. */
+       struct smb_request *smb1req;
        struct files_struct *compat_chain_fsp;
 
        NTSTATUS next_status;
@@ -400,8 +416,8 @@ struct smbd_smb2_session {
        struct smbd_server_connection *sconn;
        NTSTATUS status;
        uint64_t vuid;
-       AUTH_NTLMSSP_STATE *auth_ntlmssp_state;
-       struct auth_serversupplied_info *server_info;
+       struct auth_ntlmssp_state *auth_ntlmssp_state;
+       struct auth_session_info *session_info;
        DATA_BLOB session_key;
        bool do_signing;
 
@@ -426,16 +442,81 @@ struct smbd_smb2_tcon {
        connection_struct *compat_conn;
 };
 
+struct pending_message_list;
 struct pending_auth_data;
 
 struct smbd_server_connection {
+       int sock;
+       const struct tsocket_address *local_address;
+       const struct tsocket_address *remote_address;
+       const char *remote_hostname;
+       struct tevent_context *ev_ctx;
+       struct messaging_context *msg_ctx;
        struct {
                bool got_session;
        } nbt;
-       bool allow_smb2;
+       bool using_smb2;
+       int trans_num;
+
+       /*
+        * Cache for calling poll(2) to avoid allocations in our
+        * central event loop
+        */
+       struct pollfd *pfds;
+
+       struct files_struct *files;
+       struct bitmap *file_bmap;
+       int real_max_open_files;
+       int files_used;
+       struct fsp_singleton_cache fsp_fi_cache;
+       unsigned long file_gen_counter;
+       int first_file;
+
+       /* Try GENSEC hook */
+       bool use_gensec_hook;
+
+       /* number of open connections (tcons) */
+       int num_tcons_open;
+
+       struct pending_message_list *deferred_open_queue;
+
+
+       /* open directory handles. */
+       struct {
+               struct bitmap *dptr_bmap;
+               struct dptr_struct *dirptrs;
+               int dirhandles_open;
+       } searches;
+
+       uint64_t num_requests;
+
        struct {
                struct fd_event *fde;
-               uint64_t num_requests;
+
+               struct {
+                       /*
+                        * fd for the fcntl lock mutexing access to our sock
+                        */
+                       int socket_lock_fd;
+
+                       /*
+                        * fd for the trusted pipe from
+                        * echo handler child
+                        */
+                       int trusted_fd;
+
+                       /*
+                        * fde for the trusted_fd
+                        */
+                       struct fd_event *trusted_fde;
+
+                       /*
+                        * Reference count for the fcntl lock to
+                        * allow recursive locks.
+                        */
+                       int ref_count;
+               } echo_handler;
+
                struct {
                        bool encrypted_passwords;
                        bool spnego;
@@ -446,9 +527,15 @@ struct smbd_server_connection {
                         * Can be modified by the max xmit parameter.
                         */
                        int max_recv;
-                       enum protocol_types protocol;
                } negprot;
 
+               struct {
+                       uint16_t client_major;
+                       uint16_t client_minor;
+                       uint32_t client_cap_low;
+                       uint32_t client_cap_high;
+               } unix_info;
+
                struct {
                        bool done_sesssetup;
                        /*
@@ -470,15 +557,11 @@ struct smbd_server_connection {
                        user_struct *validated_users;
                        uint16_t next_vuid;
                        int num_validated_vuids;
-#ifdef HAVE_NETGROUP
-                       char *my_yp_domain;
-#endif
                } sessions;
                struct {
                        connection_struct *Connections;
                        /* number of open connections */
                        struct bitmap *bmap;
-                       int num_open;
                } tcons;
                struct smb_signing_state *signing_state;
                /* List to store partial SPNEGO auth fragments. */
@@ -487,16 +570,23 @@ struct smbd_server_connection {
                struct notify_mid_map *notify_mid_maps;
 
                struct {
-                       struct bitmap *dptr_bmap;
-                       struct dptr_struct *dirptrs;
-                       int dirhandles_open;
-               } searches;
+                       /* dlink list we store pending lock records on. */
+                       struct blocking_lock_record *blocking_lock_queue;
+                       /* dlink list we move cancelled lock records onto. */
+                       struct blocking_lock_record *blocking_lock_cancelled_queue;
+
+                       /* The event that makes us process our blocking lock queue */
+                       struct timed_event *brl_timeout;
+
+                       bool blocking_lock_unlock_state;
+                       bool blocking_lock_cancel_state;
+               } locks;
        } smb1;
        struct {
-               struct tevent_context *event_ctx;
                struct tevent_queue *recv_queue;
                struct tevent_queue *send_queue;
                struct tstream_context *stream;
+               bool negprot_2ff;
                struct {
                        /* an id tree used to allocate vuids */
                        /* this holds info on session vuids that are already
@@ -508,10 +598,25 @@ struct smbd_server_connection {
 
                        struct smbd_smb2_session *list;
                } sessions;
+               struct {
+                       /* The event that makes us process our blocking lock queue */
+                       struct timed_event *brl_timeout;
+                       bool blocking_lock_unlock_state;
+               } locks;
                struct smbd_smb2_request *requests;
+               uint64_t seqnum_low;
+               uint32_t credits_granted;
+               uint32_t max_credits;
+               uint32_t max_trans;
+               uint32_t max_read;
+               uint32_t max_write;
+               struct bitmap *credits_bitmap;
+               bool compound_related_in_progress;
        } smb2;
 };
 
 extern struct smbd_server_connection *smbd_server_conn;
 
+struct smbd_server_connection *msg_ctx_to_sconn(struct messaging_context *msg_ctx);
+
 void smbd_init_globals(void);