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 /*
222c2961f8Sjose borrego  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da6c28aaSamw  * Use is subject to license terms.
24*93bc28dbSGordon Ross  *
25*93bc28dbSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26da6c28aaSamw  */
27da6c28aaSamw 
28da6c28aaSamw /*
29da6c28aaSamw  * The seek message is sent to set the current file pointer for FID.
30da6c28aaSamw  * This request should generally only be used by clients wishing to
31da6c28aaSamw  * find the size of a file, since all read and write requests include
32da6c28aaSamw  * the read or write file position as part of the SMB. This request
33da6c28aaSamw  * is inappropriate for large files, as the offsets specified are only
34da6c28aaSamw  * 32 bits.
35da6c28aaSamw  *
36da6c28aaSamw  * The CIFS/1.0 (1996) spec contains the following incomplete statement:
37da6c28aaSamw  *
38da6c28aaSamw  * "A seek which results in an Offset which can not be expressed
39da6c28aaSamw  *  in 32 bits returns the least significant."
40da6c28aaSamw  *
41da6c28aaSamw  * It would probably be a mistake to make an assumption about what this
42da6c28aaSamw  * statement means. So, for now, we return an error if the resultant
43da6c28aaSamw  * file offset is beyond the 32-bit limit.
44da6c28aaSamw  */
45da6c28aaSamw 
46bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
47da6c28aaSamw 
48da6c28aaSamw 
49da6c28aaSamw /*
50da6c28aaSamw  * smb_com_seek
51da6c28aaSamw  *
52da6c28aaSamw  * Client Request                     Description
53da6c28aaSamw  * ================================== =================================
54da6c28aaSamw  * UCHAR WordCount;                   Count of parameter words = 4
55da6c28aaSamw  * USHORT Fid;                        File handle
56da6c28aaSamw  * USHORT Mode;                       Seek mode: 0, 1 or 2
57da6c28aaSamw  * LONG Offset;                       Relative offset
58da6c28aaSamw  * USHORT ByteCount;                  Count of data bytes = 0
59da6c28aaSamw  *
60da6c28aaSamw  * The starting point of the seek is set by Mode:
61da6c28aaSamw  *
62da6c28aaSamw  *      0  seek from start of file
63da6c28aaSamw  *      1  seek from current current position
64da6c28aaSamw  *      2  seek from end of file
65da6c28aaSamw  *
66da6c28aaSamw  * The "current position" reflects the offset plus data length specified in
67da6c28aaSamw  * the previous read, write or seek request, and the pointer set by this
68da6c28aaSamw  * command will be replaced by the offset specified in the next read, write
69da6c28aaSamw  * or seek command.
70da6c28aaSamw  *
71da6c28aaSamw  * Server Response                    Description
72da6c28aaSamw  * ================================== =================================
73da6c28aaSamw  * UCHAR WordCount;                   Count of parameter words = 2
74da6c28aaSamw  * ULONG Offset;                      Offset from start of file
75da6c28aaSamw  * USHORT ByteCount;                  Count of data bytes = 0
76da6c28aaSamw  *
77da6c28aaSamw  * The response returns the new file pointer in Offset, which is expressed
78da6c28aaSamw  * as the offset from the start of the file, and may be beyond the current
79da6c28aaSamw  * end of file. An attempt to seek before the start of the file sets the
80da6c28aaSamw  * current file pointer to the start of the file.
81da6c28aaSamw  */
827b59d02dSjb smb_sdrc_t
smb_pre_seek(smb_request_t * sr)83faa1795aSjb smb_pre_seek(smb_request_t *sr)
84faa1795aSjb {
85*93bc28dbSGordon Ross 	DTRACE_SMB_START(op__Seek, smb_request_t *, sr);
86faa1795aSjb 	return (SDRC_SUCCESS);
87faa1795aSjb }
88faa1795aSjb 
89faa1795aSjb void
smb_post_seek(smb_request_t * sr)90faa1795aSjb smb_post_seek(smb_request_t *sr)
91faa1795aSjb {
92*93bc28dbSGordon Ross 	DTRACE_SMB_DONE(op__Seek, smb_request_t *, sr);
93faa1795aSjb }
94faa1795aSjb 
95faa1795aSjb smb_sdrc_t
smb_com_seek(smb_request_t * sr)96faa1795aSjb smb_com_seek(smb_request_t *sr)
97da6c28aaSamw {
98da6c28aaSamw 	ushort_t	mode;
99da6c28aaSamw 	int32_t		off;
100da6c28aaSamw 	uint32_t	off_ret;
101da6c28aaSamw 	int		rc;
102da6c28aaSamw 
1037b59d02dSjb 	if (smbsr_decode_vwv(sr, "wwl", &sr->smb_fid, &mode, &off) != 0)
104faa1795aSjb 		return (SDRC_ERROR);
105da6c28aaSamw 
1062c2961f8Sjose borrego 	smbsr_lookup_file(sr);
107da6c28aaSamw 	if (sr->fid_ofile == NULL) {
108dc20a302Sas 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
109faa1795aSjb 		return (SDRC_ERROR);
110da6c28aaSamw 	}
111da6c28aaSamw 
112b89a8333Snatalie li - Sun Microsystems - Irvine United States 	sr->user_cr = smb_ofile_getcred(sr->fid_ofile);
113b89a8333Snatalie li - Sun Microsystems - Irvine United States 
114dc20a302Sas 	if ((rc = smb_ofile_seek(sr->fid_ofile, mode, off, &off_ret)) != 0) {
115dc20a302Sas 		if (rc == EINVAL) {
116dc20a302Sas 			smbsr_error(sr, 0, ERRDOS, ERRbadfunc);
117faa1795aSjb 			return (SDRC_ERROR);
118dc20a302Sas 		} else {
119dc20a302Sas 			smbsr_error(sr, 0, ERRSRV, ERRerror);
120faa1795aSjb 			return (SDRC_ERROR);
121dc20a302Sas 		}
122da6c28aaSamw 	}
123da6c28aaSamw 
1247b59d02dSjb 	if (smbsr_encode_result(sr, 2, 0, "blw", 2, off_ret, 0))
125faa1795aSjb 		return (SDRC_ERROR);
1267b59d02dSjb 
127faa1795aSjb 	return (SDRC_SUCCESS);
128da6c28aaSamw }
129