1da6c28aaSamw /* 2da6c28aaSamw * CDDL HEADER START 3da6c28aaSamw * 4da6c28aaSamw * The contents of this file are subject to the terms of the 5da6c28aaSamw * Common Development and Distribution License (the "License"). 6da6c28aaSamw * You may not use this file except in compliance with the License. 7da6c28aaSamw * 8da6c28aaSamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9da6c28aaSamw * or http://www.opensolaris.org/os/licensing. 10da6c28aaSamw * See the License for the specific language governing permissions 11da6c28aaSamw * and limitations under the License. 12da6c28aaSamw * 13da6c28aaSamw * When distributing Covered Code, include this CDDL HEADER in each 14da6c28aaSamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15da6c28aaSamw * If applicable, add the following below this CDDL HEADER, with the 16da6c28aaSamw * fields enclosed by brackets "[]" replaced with your own identifying 17da6c28aaSamw * information: Portions Copyright [yyyy] [name of copyright owner] 18da6c28aaSamw * 19da6c28aaSamw * CDDL HEADER END 20da6c28aaSamw */ 21da6c28aaSamw /* 22cb174861Sjoyce mcintosh * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 235fd03bc0SGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 24da6c28aaSamw */ 25da6c28aaSamw 26da6c28aaSamw #include <sys/synch.h> 27bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h> 28da6c28aaSamw #include <smbsrv/smb_fsops.h> 29dc20a302Sas200622 #include <sys/nbmlock.h> 30da6c28aaSamw 31b1352070SAlan Wright /* 32b1352070SAlan Wright * NT_RENAME InformationLevels: 33b1352070SAlan Wright * 34b1352070SAlan Wright * SMB_NT_RENAME_MOVE_CLUSTER_INFO Server returns invalid parameter. 35b1352070SAlan Wright * SMB_NT_RENAME_SET_LINK_INFO Create a hard link to a file. 36b1352070SAlan Wright * SMB_NT_RENAME_RENAME_FILE In-place rename of a file. 37b1352070SAlan Wright * SMB_NT_RENAME_MOVE_FILE Move (rename) a file. 38b1352070SAlan Wright */ 39b1352070SAlan Wright #define SMB_NT_RENAME_MOVE_CLUSTER_INFO 0x0102 40b1352070SAlan Wright #define SMB_NT_RENAME_SET_LINK_INFO 0x0103 41b1352070SAlan Wright #define SMB_NT_RENAME_RENAME_FILE 0x0104 42b1352070SAlan Wright #define SMB_NT_RENAME_MOVE_FILE 0x0105 43b1352070SAlan Wright 44bbf6f00cSJordan Brown /* 45da6c28aaSamw * smb_com_rename 46da6c28aaSamw * 47da6c28aaSamw * Rename a file. Files OldFileName must exist and NewFileName must not. 48da6c28aaSamw * Both pathnames must be relative to the Tid specified in the request. 49da6c28aaSamw * Open files may be renamed. 50da6c28aaSamw * 51da6c28aaSamw * Multiple files may be renamed in response to a single request as Rename 52da6c28aaSamw * File supports wildcards in the file name (last component of the path). 53da6c28aaSamw * NOTE: we don't support rename with wildcards. 54da6c28aaSamw * 55da6c28aaSamw * SearchAttributes indicates the attributes that the target file(s) must 56da6c28aaSamw * have. If SearchAttributes is zero then only normal files are renamed. 57da6c28aaSamw * If the system file or hidden attributes are specified then the rename 58da6c28aaSamw * is inclusive - both the specified type(s) of files and normal files are 59bbf6f00cSJordan Brown * renamed. 60da6c28aaSamw */ 617b59d02dSjb150015 smb_sdrc_t 62faa1795aSjb150015 smb_pre_rename(smb_request_t *sr) 63faa1795aSjb150015 { 64b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_fqi_t *src_fqi = &sr->arg.dirop.fqi; 65b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_fqi_t *dst_fqi = &sr->arg.dirop.dst_fqi; 66faa1795aSjb150015 int rc; 67faa1795aSjb150015 68eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States if ((rc = smbsr_decode_vwv(sr, "w", &src_fqi->fq_sattr)) == 0) { 69eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States rc = smbsr_decode_data(sr, "%SS", sr, &src_fqi->fq_path.pn_path, 70eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States &dst_fqi->fq_path.pn_path); 71faa1795aSjb150015 72eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States dst_fqi->fq_sattr = 0; 73faa1795aSjb150015 } 74faa1795aSjb150015 75faa1795aSjb150015 DTRACE_SMB_2(op__Rename__start, smb_request_t *, sr, 76faa1795aSjb150015 struct dirop *, &sr->arg.dirop); 77faa1795aSjb150015 78faa1795aSjb150015 return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); 79faa1795aSjb150015 } 80faa1795aSjb150015 81faa1795aSjb150015 void 82faa1795aSjb150015 smb_post_rename(smb_request_t *sr) 83faa1795aSjb150015 { 84faa1795aSjb150015 DTRACE_SMB_1(op__Rename__done, smb_request_t *, sr); 85faa1795aSjb150015 } 86faa1795aSjb150015 87faa1795aSjb150015 smb_sdrc_t 88faa1795aSjb150015 smb_com_rename(smb_request_t *sr) 89da6c28aaSamw { 90b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_fqi_t *src_fqi = &sr->arg.dirop.fqi; 91b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_fqi_t *dst_fqi = &sr->arg.dirop.dst_fqi; 929fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_t *src_pn = &src_fqi->fq_path; 939fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_t *dst_pn = &dst_fqi->fq_path; 94*a90cf9f2SGordon Ross uint32_t status; 95da6c28aaSamw 96da6c28aaSamw if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) { 97dc20a302Sas200622 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 98da6c28aaSamw ERRDOS, ERROR_ACCESS_DENIED); 99faa1795aSjb150015 return (SDRC_ERROR); 100da6c28aaSamw } 101da6c28aaSamw 1029fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_init(sr, src_pn, src_pn->pn_path); 1039fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_init(sr, dst_pn, dst_pn->pn_path); 1049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (!smb_pathname_validate(sr, src_pn) || 1059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States !smb_pathname_validate(sr, dst_pn)) { 1069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (SDRC_ERROR); 1079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States } 1089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 109*a90cf9f2SGordon Ross status = smb_common_rename(sr, src_fqi, dst_fqi); 110*a90cf9f2SGordon Ross if (status != 0) { 111*a90cf9f2SGordon Ross smbsr_error(sr, status, 0, 0); 112faa1795aSjb150015 return (SDRC_ERROR); 113da6c28aaSamw } 114da6c28aaSamw 115*a90cf9f2SGordon Ross (void) smbsr_encode_empty_result(sr); 116*a90cf9f2SGordon Ross return (SDRC_SUCCESS); 117da6c28aaSamw } 118da6c28aaSamw 119da6c28aaSamw /* 120b1352070SAlan Wright * smb_com_nt_rename 121b1352070SAlan Wright * 122b1352070SAlan Wright * Rename a file. Files OldFileName must exist and NewFileName must not. 123b1352070SAlan Wright * Both pathnames must be relative to the Tid specified in the request. 124b1352070SAlan Wright * Open files may be renamed. 125b1352070SAlan Wright * 126b1352070SAlan Wright * SearchAttributes indicates the attributes that the target file(s) must 127b1352070SAlan Wright * have. If SearchAttributes is zero then only normal files are renamed. 128b1352070SAlan Wright * If the system file or hidden attributes are specified then the rename 129b1352070SAlan Wright * is inclusive - both the specified type(s) of files and normal files are 130bbf6f00cSJordan Brown * renamed. 131b1352070SAlan Wright */ 132b1352070SAlan Wright smb_sdrc_t 133b1352070SAlan Wright smb_pre_nt_rename(smb_request_t *sr) 134b1352070SAlan Wright { 135b1352070SAlan Wright smb_fqi_t *src_fqi = &sr->arg.dirop.fqi; 136b1352070SAlan Wright smb_fqi_t *dst_fqi = &sr->arg.dirop.dst_fqi; 137b1352070SAlan Wright uint32_t clusters; 138b1352070SAlan Wright int rc; 139dc20a302Sas200622 140b1352070SAlan Wright rc = smbsr_decode_vwv(sr, "wwl", &src_fqi->fq_sattr, 141b1352070SAlan Wright &sr->arg.dirop.info_level, &clusters); 142b1352070SAlan Wright if (rc == 0) { 143b1352070SAlan Wright rc = smbsr_decode_data(sr, "%SS", sr, 144b1352070SAlan Wright &src_fqi->fq_path.pn_path, &dst_fqi->fq_path.pn_path); 145dc20a302Sas200622 146b1352070SAlan Wright dst_fqi->fq_sattr = 0; 147b1352070SAlan Wright } 148b1352070SAlan Wright 149b1352070SAlan Wright DTRACE_SMB_2(op__NtRename__start, smb_request_t *, sr, 150b1352070SAlan Wright struct dirop *, &sr->arg.dirop); 151b1352070SAlan Wright 152b1352070SAlan Wright return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); 153b1352070SAlan Wright } 154b1352070SAlan Wright 155b1352070SAlan Wright void 156b1352070SAlan Wright smb_post_nt_rename(smb_request_t *sr) 157b1352070SAlan Wright { 158b1352070SAlan Wright DTRACE_SMB_1(op__NtRename__done, smb_request_t *, sr); 159b1352070SAlan Wright } 160b1352070SAlan Wright 161b1352070SAlan Wright smb_sdrc_t 162b1352070SAlan Wright smb_com_nt_rename(smb_request_t *sr) 163b1352070SAlan Wright { 164b1352070SAlan Wright smb_fqi_t *src_fqi = &sr->arg.dirop.fqi; 165b1352070SAlan Wright smb_fqi_t *dst_fqi = &sr->arg.dirop.dst_fqi; 1669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_t *src_pn = &src_fqi->fq_path; 1679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_t *dst_pn = &dst_fqi->fq_path; 168*a90cf9f2SGordon Ross uint32_t status; 169b1352070SAlan Wright 170b1352070SAlan Wright if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) { 171b1352070SAlan Wright smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 172b1352070SAlan Wright ERRDOS, ERROR_ACCESS_DENIED); 173b1352070SAlan Wright return (SDRC_ERROR); 174b1352070SAlan Wright } 175b1352070SAlan Wright 1769fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_init(sr, src_pn, src_pn->pn_path); 1779fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_pathname_init(sr, dst_pn, dst_pn->pn_path); 1789fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (!smb_pathname_validate(sr, src_pn) || 1799fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States !smb_pathname_validate(sr, dst_pn)) { 1809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (SDRC_ERROR); 1819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States } 1829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 1839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (smb_contains_wildcards(src_pn->pn_path)) { 184b1352070SAlan Wright smbsr_error(sr, NT_STATUS_OBJECT_PATH_SYNTAX_BAD, 185b1352070SAlan Wright ERRDOS, ERROR_BAD_PATHNAME); 186b1352070SAlan Wright return (SDRC_ERROR); 187b1352070SAlan Wright } 188b1352070SAlan Wright 189b1352070SAlan Wright switch (sr->arg.dirop.info_level) { 190b1352070SAlan Wright case SMB_NT_RENAME_SET_LINK_INFO: 191*a90cf9f2SGordon Ross status = smb_make_link(sr, src_fqi, dst_fqi); 192b1352070SAlan Wright break; 193b1352070SAlan Wright case SMB_NT_RENAME_RENAME_FILE: 194b1352070SAlan Wright case SMB_NT_RENAME_MOVE_FILE: 195*a90cf9f2SGordon Ross status = smb_common_rename(sr, src_fqi, dst_fqi); 196b1352070SAlan Wright break; 197b1352070SAlan Wright case SMB_NT_RENAME_MOVE_CLUSTER_INFO: 198*a90cf9f2SGordon Ross status = NT_STATUS_INVALID_PARAMETER; 199b1352070SAlan Wright break; 200b1352070SAlan Wright default: 201*a90cf9f2SGordon Ross status = NT_STATUS_ACCESS_DENIED; 202b1352070SAlan Wright break; 203b1352070SAlan Wright } 204b1352070SAlan Wright 205*a90cf9f2SGordon Ross if (status != 0) { 206*a90cf9f2SGordon Ross smbsr_error(sr, status, 0, 0); 207b1352070SAlan Wright return (SDRC_ERROR); 208b1352070SAlan Wright } 209b1352070SAlan Wright 210*a90cf9f2SGordon Ross (void) smbsr_encode_empty_result(sr); 211*a90cf9f2SGordon Ross return (SDRC_SUCCESS); 212b1352070SAlan Wright } 213b1352070SAlan Wright 214b1352070SAlan Wright /* 215bbf6f00cSJordan Brown * smb_nt_transact_rename 216bbf6f00cSJordan Brown * 217bbf6f00cSJordan Brown * Windows servers return SUCCESS without renaming file. 218bbf6f00cSJordan Brown * The only check required is to check that the handle (fid) is valid. 219bbf6f00cSJordan Brown */ 220bbf6f00cSJordan Brown smb_sdrc_t 221bbf6f00cSJordan Brown smb_nt_transact_rename(smb_request_t *sr, smb_xa_t *xa) 222bbf6f00cSJordan Brown { 223bbf6f00cSJordan Brown if (smb_mbc_decodef(&xa->req_param_mb, "w", &sr->smb_fid) != 0) 224bbf6f00cSJordan Brown return (SDRC_ERROR); 225bbf6f00cSJordan Brown 226bbf6f00cSJordan Brown smbsr_lookup_file(sr); 227bbf6f00cSJordan Brown if (sr->fid_ofile == NULL) { 228bbf6f00cSJordan Brown smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid); 229bbf6f00cSJordan Brown return (SDRC_ERROR); 230bbf6f00cSJordan Brown } 231bbf6f00cSJordan Brown smbsr_release_file(sr); 232bbf6f00cSJordan Brown 233bbf6f00cSJordan Brown return (SDRC_SUCCESS); 234bbf6f00cSJordan Brown } 235