Same fix as bug 8989 - Samba 3.5.x (and probably all other versions of Samba) does...
[samba.git] / source3 / smbd / trans2.c
index 3b4a4b70d39afcd46f118cb0f239ed92bc90f973..602280d9c18c2809a1126e73ded06ce982c45928 100644 (file)
@@ -57,6 +57,23 @@ uint64_t smb_roundup(connection_struct *conn, uint64_t val)
        return val;
 }
 
+/********************************************************************
+ Create a 64 bit FileIndex. If the file is on the same device as
+ the root of the share, just return the 64-bit inode. If it isn't,
+ mangle as we used to do.
+********************************************************************/
+
+uint64_t get_FileIndex(connection_struct *conn, const SMB_STRUCT_STAT *psbuf)
+{
+       uint64_t file_index;
+       if (conn->base_share_dev == psbuf->st_ex_dev) {
+               return (uint64_t)psbuf->st_ex_ino;
+       }
+       file_index = ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
+       file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
+       return file_index;
+}
+
 /****************************************************************************
  Utility functions for dealing with extended attributes.
 ****************************************************************************/
@@ -832,12 +849,6 @@ void send_trans2_replies(connection_struct *conn,
                reply_outbuf(req, 10, total_sent_thistime + alignment_offset
                             + data_alignment_offset);
 
-               /*
-                * We might have SMBtrans2s in req which was transferred to
-                * the outbuf, fix that.
-                */
-               SCVAL(req->outbuf, smb_com, SMBtrans2);
-
                /* Set total params and data to be sent */
                SSVAL(req->outbuf,smb_tprcnt,paramsize);
                SSVAL(req->outbuf,smb_tdrcnt,datasize);
@@ -1012,7 +1023,7 @@ static void call_trans2open(connection_struct *conn,
        pname = &params[28];
 
        if (IS_IPC(conn)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
                goto out;
        }
 
@@ -1051,21 +1062,17 @@ static void call_trans2open(connection_struct *conn,
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(smb_fname, deny_mode, open_ofun,
+       if (!map_open_params_to_ntcreate(smb_fname->base_name, deny_mode,
+                                        open_ofun,
                                         &access_mask, &share_mode,
                                         &create_disposition,
                                         &create_options)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
        /* Any data in this call is an EA list. */
-       if (total_data && (total_data != 4) && !lp_ea_support(SNUM(conn))) {
-               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
-               goto out;
-       }
-
-       if (total_data != 4) {
+       if (total_data && (total_data != 4)) {
                if (total_data < 10) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
@@ -1084,9 +1091,11 @@ static void call_trans2open(connection_struct *conn,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
                }
-       } else if (IVAL(pdata,0) != 4) {
-               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               goto out;
+
+               if (!lp_ea_support(SNUM(conn))) {
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
+                       goto out;
+               }
        }
 
        status = SMB_VFS_CREATE_FILE(
@@ -1121,7 +1130,7 @@ static void call_trans2open(connection_struct *conn,
        inode = smb_fname->st.st_ex_ino;
        if (fattr & aDIR) {
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS,ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -1477,6 +1486,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        uint32_t reskey=0;
        uint64_t file_size = 0;
        uint64_t allocation_size = 0;
+       uint64_t file_index = 0;
        uint32_t len;
        struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
        time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
@@ -1484,7 +1494,6 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        char *nameptr;
        char *last_entry_ptr;
        bool was_8_3;
-       uint32_t nt_extmode; /* Used for NT connections instead of mode */
        off_t off;
        off_t pad = 0;
 
@@ -1500,6 +1509,8 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        }
        allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
 
+       file_index = get_FileIndex(conn, &smb_fname->st);
+
        mdate_ts = smb_fname->st.st_ex_mtime;
        adate_ts = smb_fname->st.st_ex_atime;
        create_date_ts = get_create_timespec(conn, NULL, smb_fname);
@@ -1535,8 +1546,6 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        pad = 0;
        off = 0;
 
-       nt_extmode = mode ? mode : FILE_ATTRIBUTE_NORMAL;
-
        switch (info_level) {
        case SMB_FIND_INFO_STANDARD:
                DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_INFO_STANDARD\n"));
@@ -1684,7 +1693,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length. */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1750,7 +1759,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                len = srvstr_push(base_data, flags2,
                                  p + 4, fname, PTR_DIFF(end_data, p+4),
                                  STR_TERMINATE_ASCII);
@@ -1786,7 +1795,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length. */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1861,7 +1870,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length. */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1870,8 +1879,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                        p +=4;
                }
                SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
-               SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
-               SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
+               SBVAL(p,0,file_index); p += 8;
                len = srvstr_push(base_data, flags2, p,
                                  fname, PTR_DIFF(end_data, p),
                                  STR_TERMINATE_ASCII);
@@ -1908,7 +1916,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1941,8 +1949,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                }
                p += 26;
                SSVAL(p,0,0); p += 2; /* Reserved ? */
-               SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
-               SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
+               SBVAL(p,0,file_index); p += 8;
                len = srvstr_push(base_data, flags2, p,
                                  fname, PTR_DIFF(end_data, p),
                                  STR_TERMINATE_ASCII);
@@ -2220,6 +2227,7 @@ static void call_trans2findfirst(connection_struct *conn,
        TALLOC_CTX *ctx = talloc_tos();
        struct dptr_struct *dirptr = NULL;
        struct smbd_server_connection *sconn = smbd_server_conn;
+       uint32_t ucf_flags = (UCF_SAVE_LCOMP | UCF_ALWAYS_ALLOW_WCARD_LCOMP);
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2263,6 +2271,7 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                goto out;
                        }
+                       ucf_flags |= UCF_UNIX_NAME_LOOKUP;
                        break;
                default:
                        reply_nterror(req, NT_STATUS_INVALID_LEVEL);
@@ -2280,8 +2289,7 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
        ntstatus = filename_convert(ctx, conn,
                                    req->flags2 & FLAGS2_DFS_PATHNAMES,
                                    directory,
-                                   (UCF_SAVE_LCOMP |
-                                       UCF_ALWAYS_ALLOW_WCARD_LCOMP),
+                                   ucf_flags,
                                    &mask_contains_wcard,
                                    &smb_dname);
        if (!NT_STATUS_IS_OK(ntstatus)) {
@@ -2337,7 +2345,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                }
 
                if (!lp_ea_support(SNUM(conn))) {
-                       reply_doserror(req, ERRDOS, ERReasnotsupported);
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                        goto out;
                }
 
@@ -2465,7 +2473,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        if(numentries == 0) {
                dptr_close(sconn, &dptr_num);
                if (get_Protocol() < PROTOCOL_NT1) {
-                       reply_doserror(req, ERRDOS, ERRnofiles);
+                       reply_force_doserror(req, ERRDOS, ERRnofiles);
                        goto out;
                } else {
                        reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
@@ -2652,7 +2660,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                }
 
                if (!lp_ea_support(SNUM(conn))) {
-                       reply_doserror(req, ERRDOS, ERReasnotsupported);
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                        return;
                }
 
@@ -2685,7 +2693,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        /* Check that the dptr is valid */
        if(!(dirptr = dptr_fetch_lanman2(sconn, dptr_num))) {
-               reply_doserror(req, ERRDOS, ERRnofiles);
+               reply_nterror(req, STATUS_NO_MORE_FILES);
                return;
        }
 
@@ -2694,7 +2702,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Get the wildcard mask from the dptr */
        if((p = dptr_wcard(sconn, dptr_num))== NULL) {
                DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
-               reply_doserror(req, ERRDOS, ERRnofiles);
+               reply_nterror(req, STATUS_NO_MORE_FILES);
                return;
        }
 
@@ -3144,7 +3152,8 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        fsp.fnum = -1;
 
                        /* access check */
-                       if (conn->server_info->utok.uid != sec_initial_uid()) {
+                       if (conn->server_info->utok.uid != sec_initial_uid() &&
+                                       !conn->admin_user) {
                                DEBUG(0,("set_user_quota: access_denied "
                                         "service [%s] user [%s]\n",
                                         lp_servicename(SNUM(conn)),
@@ -3631,7 +3640,7 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                ZERO_STRUCT(quotas);
 
                                /* access check */
-                               if ((conn->server_info->utok.uid != sec_initial_uid())
+                               if (((conn->server_info->utok.uid != sec_initial_uid()) && !conn->admin_user)
                                    ||!CAN_WRITE(conn)) {
                                        DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
                                                 lp_servicename(SNUM(conn)),
@@ -3856,6 +3865,8 @@ static char *store_file_unix_basic(connection_struct *conn,
                                files_struct *fsp,
                                const SMB_STRUCT_STAT *psbuf)
 {
+       uint64_t file_index = get_FileIndex(conn, psbuf);
+
        DEBUG(10,("store_file_unix_basic: SMB_QUERY_FILE_UNIX_BASIC\n"));
        DEBUG(4,("store_file_unix_basic: st_mode=%o\n",(int)psbuf->st_ex_mode));
 
@@ -3889,7 +3900,7 @@ static char *store_file_unix_basic(connection_struct *conn,
        SIVAL(pdata,4,0);
        pdata += 8;
 
-       SINO_T_VAL(pdata,0,(SMB_INO_T)psbuf->st_ex_ino);   /* inode number */
+       SINO_T_VAL(pdata,0,(SMB_INO_T)file_index);   /* inode number */
        pdata += 8;
 
        SIVAL(pdata,0, unix_perms_to_wire(psbuf->st_ex_mode));     /* Standard UNIX file permissions */
@@ -4187,8 +4198,6 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
        } else {
                mode = dos_mode(conn, smb_fname);
        }
-       if (!mode)
-               mode = FILE_ATTRIBUTE_NORMAL;
 
        nlink = psbuf->st_ex_nlink;
 
@@ -4293,8 +4302,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
           I think this causes us to fail the IFSKIT
           BasicFileInformationTest. -tpot */
-       file_index =  ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
-       file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
+       file_index = get_FileIndex(conn, psbuf);
 
        switch (info_level) {
                case SMB_INFO_STANDARD:
@@ -5076,6 +5084,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
 
        } else {
                char *fname = NULL;
+               uint32_t ucf_flags = 0;
 
                /* qpathinfo */
                if (total_params < 7) {
@@ -5087,9 +5096,16 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
 
                DEBUG(3,("call_trans2qfilepathinfo: TRANSACT2_QPATHINFO: level = %d\n", info_level));
 
-               if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
-                       reply_nterror(req, NT_STATUS_INVALID_LEVEL);
-                       return;
+               if (INFO_LEVEL_IS_UNIX(info_level)) {
+                       if (!lp_unix_extensions()) {
+                               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
+                               return;
+                       }
+                       if (info_level == SMB_QUERY_FILE_UNIX_BASIC ||
+                                       info_level == SMB_QUERY_FILE_UNIX_INFO2 ||
+                                       info_level == SMB_QUERY_FILE_UNIX_LINK) {
+                               ucf_flags |= UCF_UNIX_NAME_LOOKUP;
+                       }
                }
 
                srvstr_get_path(req, params, req->flags2, &fname, &params[6],
@@ -5104,7 +5120,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                        conn,
                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
                                        fname,
-                                       0,
+                                       ucf_flags,
                                        NULL,
                                        &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
@@ -5236,8 +5252,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        }
 
                        if (!lp_ea_support(SNUM(conn))) {
-                               reply_doserror(req, ERRDOS,
-                                              ERReasnotsupported);
+                               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                                return;
                        }
 
@@ -5737,10 +5752,11 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
                (unsigned int)dosmode,
                (unsigned int)delete_on_close ));
 
-       status = can_set_delete_on_close(fsp, delete_on_close, dosmode);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (delete_on_close) {
+               status = can_set_delete_on_close(fsp, dosmode);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
        }
 
        /* The set is across all open files on this dev/inode pair. */
@@ -5820,7 +5836,6 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
 {
        char *link_target = NULL;
        const char *newname = smb_fname->base_name;
-       NTSTATUS status = NT_STATUS_OK;
        TALLOC_CTX *ctx = talloc_tos();
 
        /* Set a symbolic link. */
@@ -5841,42 +5856,6 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* !widelinks forces the target path to be within the share. */
-       /* This means we can interpret the target as a pathname. */
-       if (!lp_widelinks(SNUM(conn))) {
-               char *rel_name = NULL;
-               char *last_dirp = NULL;
-
-               if (*link_target == '/') {
-                       /* No absolute paths allowed. */
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-               rel_name = talloc_strdup(ctx,newname);
-               if (!rel_name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-               last_dirp = strrchr_m(rel_name, '/');
-               if (last_dirp) {
-                       last_dirp[1] = '\0';
-               } else {
-                       rel_name = talloc_strdup(ctx,"./");
-                       if (!rel_name) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-               rel_name = talloc_asprintf_append(rel_name,
-                               "%s",
-                               link_target);
-               if (!rel_name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               status = check_name(conn, rel_name);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       }
-
        DEBUG(10,("smb_set_file_unix_link: SMB_SET_FILE_UNIX_LINK doing symlink %s -> %s\n",
                        newname, link_target ));
 
@@ -5975,6 +5954,7 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
        status = resolve_dfspath_wcard(ctx, conn,
                                       req->flags2 & FLAGS2_DFS_PATHNAMES,
                                       newname,
+                                      true,
                                       &newname,
                                       &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
@@ -6619,6 +6599,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        files_struct *all_fsps = NULL;
        bool modify_mtime = true;
        struct file_id id;
+       struct smb_filename *smb_fname_tmp = NULL;
        SMB_STRUCT_STAT sbuf;
 
        ZERO_STRUCT(ft);
@@ -6671,7 +6652,6 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        sbuf = smb_fname->st;
 
        if (!VALID_STAT(sbuf)) {
-               struct smb_filename *smb_fname_tmp = NULL;
                /*
                 * The only valid use of this is to create character and block
                 * devices, and named pipes. This is deprecated (IMHO) and 
@@ -6700,7 +6680,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
                }
 
                sbuf = smb_fname_tmp->st;
-               TALLOC_FREE(smb_fname_tmp);
+               smb_fname = smb_fname_tmp;
 
                /* Ensure we don't try and change anything else. */
                raw_unixmode = SMB_MODE_NO_CHANGE;
@@ -7069,18 +7049,55 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
 
        wire_open_mode &= ~SMB_ACCMODE;
 
-       if((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL)) == (SMB_O_CREAT | SMB_O_EXCL)) {
-               create_disp = FILE_CREATE;
-       } else if((wire_open_mode & (SMB_O_CREAT | SMB_O_TRUNC)) == (SMB_O_CREAT | SMB_O_TRUNC)) {
-               create_disp = FILE_OVERWRITE_IF;
-       } else if((wire_open_mode & SMB_O_CREAT) == SMB_O_CREAT) {
-               create_disp = FILE_OPEN_IF;
-       } else if ((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL | SMB_O_TRUNC)) == 0) {
-               create_disp = FILE_OPEN;
-       } else {
-               DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
-                       (unsigned int)wire_open_mode ));
-               return NT_STATUS_INVALID_PARAMETER;
+       /* First take care of O_CREAT|O_EXCL interactions. */
+       switch (wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL)) {
+               case (SMB_O_CREAT | SMB_O_EXCL):
+                       /* File exists fail. File not exist create. */
+                       create_disp = FILE_CREATE;
+                       break;
+               case SMB_O_CREAT:
+                       /* File exists open. File not exist create. */
+                       create_disp = FILE_OPEN_IF;
+                       break;
+               case 0:
+                       /* File exists open. File not exist fail. */
+                       create_disp = FILE_OPEN;
+                       break;
+               case SMB_O_EXCL:
+                       /* O_EXCL on its own without O_CREAT is undefined. */
+               default:
+                       DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
+                               (unsigned int)wire_open_mode ));
+                       return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       /* Next factor in the effects of O_TRUNC. */
+       wire_open_mode &= ~(SMB_O_CREAT | SMB_O_EXCL);
+
+       if (wire_open_mode & SMB_O_TRUNC) {
+               switch (create_disp) {
+                       case FILE_CREATE:
+                               /* (SMB_O_CREAT | SMB_O_EXCL | O_TRUNC) */
+                               /* Leave create_disp alone as
+                                  (O_CREAT|O_EXCL|O_TRUNC) == (O_CREAT|O_EXCL)
+                               */
+                               /* File exists fail. File not exist create. */
+                               break;
+                       case FILE_OPEN_IF:
+                               /* SMB_O_CREAT | SMB_O_TRUNC */
+                               /* File exists overwrite. File not exist create. */
+                               create_disp = FILE_OVERWRITE_IF;
+                               break;
+                       case FILE_OPEN:
+                               /* SMB_O_TRUNC */
+                               /* File exists overwrite. File not exist fail. */
+                               create_disp = FILE_OVERWRITE;
+                               break;
+                       default:
+                               /* Cannot get here. */
+                               smb_panic("smb_posix_open: logic error");
+                               return NT_STATUS_INVALID_PARAMETER;
+               }
        }
 
        raw_unixmode = IVAL(pdata,8);
@@ -7668,7 +7685,8 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                                    max_data_bytes);
                                return;
                        } else {
-                               reply_doserror(req, ERRDOS, ERRbadpath);
+                               reply_nterror(req,
+                                       NT_STATUS_OBJECT_PATH_NOT_FOUND);
                                return;
                        }
                } else {
@@ -7689,6 +7707,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                }
        } else {
                char *fname = NULL;
+               uint32_t ucf_flags = 0;
 
                /* set path info */
                if (total_params < 7) {
@@ -7705,10 +7724,17 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        return;
                }
 
+               if (info_level == SMB_SET_FILE_UNIX_BASIC ||
+                               info_level == SMB_SET_FILE_UNIX_INFO2 ||
+                               info_level == SMB_FILE_RENAME_INFORMATION ||
+                               info_level == SMB_POSIX_PATH_UNLINK) {
+                       ucf_flags |= UCF_UNIX_NAME_LOOKUP;
+               }
+
                status = filename_convert(req, conn,
                                         req->flags2 & FLAGS2_DFS_PATHNAMES,
                                         fname,
-                                        0,
+                                        ucf_flags,
                                         NULL,
                                         &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
@@ -7808,7 +7834,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
        TALLOC_CTX *ctx = talloc_tos();
 
        if (!CAN_WRITE(conn)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -7846,19 +7872,14 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                return;
         }
 
-       /* Any data in this call is an EA list. */
-       if (total_data && (total_data != 4) && !lp_ea_support(SNUM(conn))) {
-               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
-               goto out;
-       }
-
        /*
         * OS/2 workplace shell seems to send SET_EA requests of "null"
         * length (4 bytes containing IVAL 4).
         * They seem to have no effect. Bug #3212. JRA.
         */
 
-       if (total_data != 4) {
+       if (total_data && (total_data != 4)) {
+               /* Any data in this call is an EA list. */
                if (total_data < 10) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
@@ -7877,6 +7898,11 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
                }
+
+               if (!lp_ea_support(SNUM(conn))) {
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
+                       goto out;
+               }
        }
        /* If total_data == 4 Windows doesn't care what values
         * are placed in that field, it just ignores them.
@@ -8027,7 +8053,7 @@ static void call_trans2getdfsreferral(connection_struct *conn,
        max_referral_level = SVAL(params,0);
 
        if(!lp_host_msdfs()) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
                return;
        }
 
@@ -8069,7 +8095,7 @@ static void call_trans2ioctl(connection_struct *conn,
        /* check for an invalid fid before proceeding */
 
        if (!fsp) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
@@ -8098,7 +8124,7 @@ static void call_trans2ioctl(connection_struct *conn,
        }
 
        DEBUG(2,("Unknown TRANS2_IOCTL\n"));
-       reply_doserror(req, ERRSRV, ERRerror);
+       reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
 }
 
 /****************************************************************************
@@ -8324,7 +8350,7 @@ static void handle_trans2(connection_struct *conn, struct smb_request *req,
        default:
                /* Error in request */
                DEBUG(2,("Unknown request %d in trans2 call\n", state->call));
-               reply_doserror(req, ERRSRV,ERRerror);
+               reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
        }
 }
 
@@ -8376,7 +8402,7 @@ void reply_trans2(struct smb_request *req)
                case TRANSACT2_SETFSINFO:
                        break;
                default:
-                       reply_doserror(req, ERRSRV, ERRaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBtrans2);
                        return;
                }
@@ -8530,6 +8556,15 @@ void reply_transs2(struct smb_request *req)
 
        show_msg((char *)req->inbuf);
 
+       /* Windows clients expect all replies to
+          a transact secondary (SMBtranss2 0x33)
+          to have a command code of transact
+          (SMBtrans2 0x32). See bug #8989
+          and also [MS-CIFS] section 2.2.4.47.2
+          for details.
+       */
+       req->cmd = SMBtrans2;
+
        if (req->wct < 8) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                END_PROFILE(SMBtranss2);