s3-security: use shared SECINFO_DACL define.
[kai/samba.git] / source3 / smbd / open.c
index e366c5fadfe95b2dfcc0e4272f885bbb4fca8982..4fc464d8abdb119c28ea55057b92cf706813d2f8 100644 (file)
 
 #include "includes.h"
 #include "smbd/globals.h"
+#include "librpc/gen_ndr/messaging.h"
+#include "../librpc/gen_ndr/ndr_security.h"
 
 extern const struct generic_mapping file_generic_mapping;
 
 struct deferred_open_record {
-       bool delayed_for_oplocks;
-       struct file_id id;
+        bool delayed_for_oplocks;
+        struct file_id id;
 };
 
 static NTSTATUS create_file_unixpath(connection_struct *conn,
@@ -58,7 +60,7 @@ NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
 {
        *access_granted = 0;
 
-       if (conn->server_info->utok.uid == 0 || conn->admin_user) {
+       if (get_current_uid(conn) == (uid_t)0) {
                /* I'm sorry sir, I didn't know you were root... */
                *access_granted = access_desired;
                if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
@@ -87,9 +89,9 @@ NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
        struct security_descriptor *sd = NULL;
 
        status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
-                       (OWNER_SECURITY_INFORMATION |
-                       GROUP_SECURITY_INFORMATION |
-                       DACL_SECURITY_INFORMATION),&sd);
+                       (SECINFO_OWNER |
+                       SECINFO_GROUP |
+                       SECINFO_DACL),&sd);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("smbd_check_open_rights: Could not get acl "
@@ -615,7 +617,7 @@ static NTSTATUS open_file(files_struct *fsp,
                fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
                        True : False;
        }
-       fsp->print_file = False;
+       fsp->print_file = NULL;
        fsp->modified = False;
        fsp->sent_oplock_break = NO_BREAK_SENT;
        fsp->is_directory = False;
@@ -890,7 +892,7 @@ static bool is_delete_request(files_struct *fsp) {
 
 static NTSTATUS send_break_message(files_struct *fsp,
                                        struct share_mode_entry *exclusive,
-                                       uint16 mid,
+                                       uint64_t mid,
                                        int oplock_request)
 {
        NTSTATUS status;
@@ -907,7 +909,8 @@ static NTSTATUS send_break_message(files_struct *fsp,
           don't want this set in the share mode struct pointed to by lck. */
 
        if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
-               SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
+               SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
+                       exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
        }
 
        status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
@@ -934,7 +937,7 @@ static NTSTATUS send_break_message(files_struct *fsp,
 
 static bool delay_for_oplocks(struct share_mode_lock *lck,
                              files_struct *fsp,
-                             uint16 mid,
+                             uint64_t mid,
                              int pass_number,
                              int oplock_request)
 {
@@ -1069,7 +1072,8 @@ static void defer_open(struct share_mode_lock *lck,
 
                if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
                        DEBUG(0, ("Trying to defer an already deferred "
-                                 "request: mid=%d, exiting\n", req->mid));
+                               "request: mid=%llu, exiting\n",
+                               (unsigned long long)req->mid));
                        exit_server("attempt to defer a deferred request");
                }
        }
@@ -1077,14 +1081,14 @@ static void defer_open(struct share_mode_lock *lck,
        /* End paranoia check */
 
        DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
-                 "open entry for mid %u\n",
+                 "open entry for mid %llu\n",
                  (unsigned int)request_time.tv_sec,
                  (unsigned int)request_time.tv_usec,
-                 (unsigned int)req->mid));
+                 (unsigned long long)req->mid));
 
-       if (!push_deferred_smb_message(req, request_time, timeout,
-                                      (char *)state, sizeof(*state))) {
-               exit_server("push_deferred_smb_message failed");
+       if (!push_deferred_open_message_smb(req, request_time, timeout,
+                                      state->id, (char *)state, sizeof(*state))) {
+               exit_server("push_deferred_open_message_smb failed");
        }
        add_deferred_open(lck, req->mid, request_time, state->id);
 }
@@ -1409,9 +1413,9 @@ static NTSTATUS calculate_access_mask(connection_struct *conn,
                        uint32_t access_granted = 0;
 
                        status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
-                                       (OWNER_SECURITY_INFORMATION |
-                                       GROUP_SECURITY_INFORMATION |
-                                       DACL_SECURITY_INFORMATION),&sd);
+                                       (SECINFO_OWNER |
+                                       SECINFO_GROUP |
+                                       SECINFO_DACL),&sd);
 
                        if (!NT_STATUS_IS_OK(status)) {
                                DEBUG(10, ("calculate_access_mask: Could not get acl "
@@ -1446,6 +1450,22 @@ static NTSTATUS calculate_access_mask(connection_struct *conn,
        return NT_STATUS_OK;
 }
 
+/****************************************************************************
+ Remove the deferred open entry under lock.
+****************************************************************************/
+
+void remove_deferred_open_entry(struct file_id id, uint64_t mid)
+{
+       struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
+                       NULL, NULL, NULL);
+       if (lck == NULL) {
+               DEBUG(0, ("could not get share mode lock\n"));
+       } else {
+               del_deferred_open_entry(lck, mid);
+               TALLOC_FREE(lck);
+       }
+}
+
 /****************************************************************************
  Open a file with a share mode. Passed in an already created files_struct *.
 ****************************************************************************/
@@ -1477,7 +1497,6 @@ static NTSTATUS open_file_ntcreate(connection_struct *conn,
        mode_t unx_mode = (mode_t)0;
        int info;
        uint32 existing_dos_attributes = 0;
-       struct pending_message_list *pml = NULL;
        struct timeval request_time = timeval_zero();
        struct share_mode_lock *lck = NULL;
        uint32 open_access_mask = access_mask;
@@ -1543,29 +1562,23 @@ static NTSTATUS open_file_ntcreate(connection_struct *conn,
         * Only non-internal opens can be deferred at all
         */
 
-       if ((req != NULL)
-           && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
-               struct deferred_open_record *state =
-                       (struct deferred_open_record *)pml->private_data.data;
+       if (req) {
+               void *ptr;
+               if (get_deferred_open_message_state(req,
+                               &request_time,
+                               &ptr)) {
 
-               /* Remember the absolute time of the original
-                  request with this mid. We'll use it later to
-                  see if this has timed out. */
+                       struct deferred_open_record *state = (struct deferred_open_record *)ptr;
+                       /* Remember the absolute time of the original
+                          request with this mid. We'll use it later to
+                          see if this has timed out. */
 
-               request_time = pml->request_time;
+                       /* Remove the deferred open entry under lock. */
+                       remove_deferred_open_entry(state->id, req->mid);
 
-               /* Remove the deferred open entry under lock. */
-               lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
-                                         NULL);
-               if (lck == NULL) {
-                       DEBUG(0, ("could not get share mode lock\n"));
-               } else {
-                       del_deferred_open_entry(lck, req->mid);
-                       TALLOC_FREE(lck);
+                       /* Ensure we don't reprocess this message. */
+                       remove_deferred_open_message_smb(req->mid);
                }
-
-               /* Ensure we don't reprocess this message. */
-               remove_deferred_open_smb_message(req->mid);
        }
 
        status = check_name(conn, smb_fname->base_name);
@@ -1581,7 +1594,7 @@ static NTSTATUS open_file_ntcreate(connection_struct *conn,
        }
 
        /* ignore any oplock requests if oplocks are disabled */
-       if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
+       if (!lp_oplocks(SNUM(conn)) ||
            IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
                /* Mask off everything except the private Samba bits. */
                oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
@@ -2176,7 +2189,7 @@ static NTSTATUS open_file_ntcreate(connection_struct *conn,
                new_file_created = True;
        }
 
-       set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
+       set_share_mode(lck, fsp, get_current_uid(conn), 0,
                       fsp->oplock_type);
 
        /* Handle strange delete on close create semantics. */
@@ -2609,7 +2622,7 @@ static NTSTATUS open_directory(connection_struct *conn,
         * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
         */
        fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
-       fsp->print_file = False;
+       fsp->print_file = NULL;
        fsp->modified = False;
        fsp->oplock_type = NO_OPLOCK;
        fsp->sent_oplock_break = NO_BREAK_SENT;
@@ -2641,7 +2654,7 @@ static NTSTATUS open_directory(connection_struct *conn,
                return status;
        }
 
-       set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
+       set_share_mode(lck, fsp, get_current_uid(conn), 0, NO_OPLOCK);
 
        /* For directories the delete on close bit at open time seems
           always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
@@ -3196,10 +3209,10 @@ static NTSTATUS create_file_unixpath(connection_struct *conn,
                security_acl_map_generic(sd->dacl, &file_generic_mapping);
                security_acl_map_generic(sd->sacl, &file_generic_mapping);
 
-               if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
-                                       GROUP_SECURITY_INFORMATION|
-                                       DACL_SECURITY_INFORMATION|
-                                       SACL_SECURITY_INFORMATION)) {
+               if (sec_info_sent & (SECINFO_OWNER|
+                                       SECINFO_GROUP|
+                                       SECINFO_DACL|
+                                       SECINFO_SACL)) {
                        status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
                }
 
@@ -3407,6 +3420,7 @@ NTSTATUS create_file_default(connection_struct *conn,
        int info = FILE_WAS_OPENED;
        files_struct *fsp = NULL;
        NTSTATUS status;
+       bool stream_name = false;
 
        DEBUG(10,("create_file: access_mask = 0x%x "
                  "file_attributes = 0x%x, share_access = 0x%x, "
@@ -3441,7 +3455,8 @@ NTSTATUS create_file_default(connection_struct *conn,
         * Check to see if this is a mac fork of some kind.
         */
 
-       if (is_ntfs_stream_smb_fname(smb_fname)) {
+       stream_name = is_ntfs_stream_smb_fname(smb_fname);
+       if (stream_name) {
                enum FAKE_FILE_TYPE fake_file_type;
 
                fake_file_type = is_fake_file(smb_fname);
@@ -3483,6 +3498,26 @@ NTSTATUS create_file_default(connection_struct *conn,
                goto fail;
        }
 
+       if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
+               int ret;
+               smb_fname->stream_name = NULL;
+               /* We have to handle this error here. */
+               if (create_options & FILE_DIRECTORY_FILE) {
+                       status = NT_STATUS_NOT_A_DIRECTORY;
+                       goto fail;
+               }
+               if (lp_posix_pathnames()) {
+                       ret = SMB_VFS_LSTAT(conn, smb_fname);
+               } else {
+                       ret = SMB_VFS_STAT(conn, smb_fname);
+               }
+
+               if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
+                       status = NT_STATUS_FILE_IS_A_DIRECTORY;
+                       goto fail;
+               }
+       }
+
        status = create_file_unixpath(
                conn, req, smb_fname, access_mask, share_access,
                create_disposition, create_options, file_attributes,