xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_trans2_find.c (revision b819cea2f73f98c5662230cc9affc8cc84f77fcf)
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 /*
229fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23da6c28aaSamw  * Use is subject to license terms.
24*b819cea2SGordon Ross  *
25*b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
26da6c28aaSamw  */
27da6c28aaSamw 
28da6c28aaSamw 
29da6c28aaSamw /*
30da6c28aaSamw  * This module provides functions for TRANS2_FIND_FIRST2 and
31da6c28aaSamw  * TRANS2_FIND_NEXT2 requests. The requests allow the client to search
32da6c28aaSamw  * for the file(s) which match the file specification.  The search is
33da6c28aaSamw  * started with TRANS2_FIND_FIRST2 and can be continued if necessary with
34da6c28aaSamw  * TRANS2_FIND_NEXT2. There are numerous levels of information which may be
35da6c28aaSamw  * obtained for the returned files, the desired level is specified in the
36da6c28aaSamw  * InformationLevel field of the requests.
37da6c28aaSamw  *
38da6c28aaSamw  *  InformationLevel Name              Value
39da6c28aaSamw  *  =================================  ================
40da6c28aaSamw  *
41da6c28aaSamw  *  SMB_INFO_STANDARD                  1
42da6c28aaSamw  *  SMB_INFO_QUERY_EA_SIZE             2
43da6c28aaSamw  *  SMB_INFO_QUERY_EAS_FROM_LIST       3
44da6c28aaSamw  *  SMB_FIND_FILE_DIRECTORY_INFO       0x101
45da6c28aaSamw  *  SMB_FIND_FILE_FULL_DIRECTORY_INFO  0x102
46da6c28aaSamw  *  SMB_FIND_FILE_NAMES_INFO           0x103
47da6c28aaSamw  *  SMB_FIND_FILE_BOTH_DIRECTORY_INFO  0x104
48b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO  0x105
49b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO  0x106
50da6c28aaSamw  *
51da6c28aaSamw  * The following sections detail the data returned for each
52da6c28aaSamw  * InformationLevel. The requested information is placed in the Data
53da6c28aaSamw  * portion of the transaction response. Note: a client which does not
54da6c28aaSamw  * support long names can only request SMB_INFO_STANDARD.
55da6c28aaSamw  *
56da6c28aaSamw  * A four-byte resume key precedes each data item (described below) if bit
57da6c28aaSamw  * 2 in the Flags field is set, i.e. if the request indicates the server
58da6c28aaSamw  * should return resume keys. Note: it is not always the case. If the
59da6c28aaSamw  * data item already includes the resume key, the resume key should not be
60da6c28aaSamw  * added again.
61da6c28aaSamw  *
62da6c28aaSamw  * 4.3.4.1   SMB_INFO_STANDARD
63da6c28aaSamw  *
64da6c28aaSamw  *  Response Field                    Description
65da6c28aaSamw  *  ================================  ==================================
66da6c28aaSamw  *
67da6c28aaSamw  *  SMB_DATE CreationDate;            Date when file was created
68da6c28aaSamw  *  SMB_TIME CreationTime;            Time when file was created
69da6c28aaSamw  *  SMB_DATE LastAccessDate;          Date of last file access
70da6c28aaSamw  *  SMB_TIME LastAccessTime;          Time of last file access
71da6c28aaSamw  *  SMB_DATE LastWriteDate;           Date of last write to the file
72da6c28aaSamw  *  SMB_TIME LastWriteTime;           Time of last write to the file
73da6c28aaSamw  *  ULONG  DataSize;                  File Size
74da6c28aaSamw  *  ULONG AllocationSize;             Size of filesystem allocation unit
75da6c28aaSamw  *  USHORT Attributes;                File Attributes
76da6c28aaSamw  *  UCHAR FileNameLength;             Length of filename in bytes
77da6c28aaSamw  *  STRING FileName;                  Name of found file
78da6c28aaSamw  *
79da6c28aaSamw  * 4.3.4.2   SMB_INFO_QUERY_EA_SIZE
80da6c28aaSamw  *
81da6c28aaSamw  *  Response Field                     Description
82da6c28aaSamw  *  =================================  ==================================
83da6c28aaSamw  *
84da6c28aaSamw  *   SMB_DATE CreationDate;            Date when file was created
85da6c28aaSamw  *   SMB_TIME CreationTime;            Time when file was created
86da6c28aaSamw  *   SMB_DATE LastAccessDate;          Date of last file access
87da6c28aaSamw  *   SMB_TIME LastAccessTime;          Time of last file access
88da6c28aaSamw  *   SMB_DATE LastWriteDate;           Date of last write to the file
89da6c28aaSamw  *   SMB_TIME LastWriteTime;           Time of last write to the file
90da6c28aaSamw  *   ULONG DataSize;                   File Size
91da6c28aaSamw  *   ULONG AllocationSize;             Size of filesystem allocation unit
92da6c28aaSamw  *   USHORT Attributes;                File Attributes
93da6c28aaSamw  *   ULONG EaSize;                     Size of file's EA information
94da6c28aaSamw  *   UCHAR FileNameLength;             Length of filename in bytes
95da6c28aaSamw  *   STRING FileName;                  Name of found file
96da6c28aaSamw  *
97da6c28aaSamw  * 4.3.4.3   SMB_INFO_QUERY_EAS_FROM_LIST
98da6c28aaSamw  *
99da6c28aaSamw  * This request returns the same information as SMB_INFO_QUERY_EA_SIZE, but
100da6c28aaSamw  * only for files which have an EA list which match the EA information in
101da6c28aaSamw  * the Data part of the request.
102da6c28aaSamw  *
103da6c28aaSamw  * 4.3.4.4   SMB_FIND_FILE_DIRECTORY_INFO
104da6c28aaSamw  *
105da6c28aaSamw  *  Response Field                     Description
106da6c28aaSamw  *  =================================  ==================================
107da6c28aaSamw  *
108da6c28aaSamw  *  ULONG NextEntryOffset;             Offset from this structure to
109da6c28aaSamw  *					beginning of next one
110da6c28aaSamw  *  ULONG FileIndex;
111da6c28aaSamw  *  LARGE_INTEGER CreationTime;        file creation time
112da6c28aaSamw  *  LARGE_INTEGER LastAccessTime;      last access time
113da6c28aaSamw  *  LARGE_INTEGER LastWriteTime;       last write time
114da6c28aaSamw  *  LARGE_INTEGER ChangeTime;          last attribute change time
115da6c28aaSamw  *  LARGE_INTEGER EndOfFile;           file size
116da6c28aaSamw  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
117da6c28aaSamw  *  ULONG ExtFileAttributes;           Extended file attributes
118da6c28aaSamw  *					(see section 3.11)
119da6c28aaSamw  *  ULONG FileNameLength;              Length of filename in bytes
120da6c28aaSamw  *  STRING FileName;                   Name of the file
121da6c28aaSamw  *
122da6c28aaSamw  * 4.3.4.5   SMB_FIND_FILE_FULL_DIRECTORY_INFO
123da6c28aaSamw  *
124da6c28aaSamw  *  Response Field                     Description
125da6c28aaSamw  *  =================================  ==================================
126da6c28aaSamw  *
127da6c28aaSamw  *  ULONG NextEntryOffset;             Offset from this structure to
128da6c28aaSamw  *					beginning of next one
129da6c28aaSamw  *  ULONG FileIndex;
130da6c28aaSamw  *  LARGE_INTEGER CreationTime;        file creation time
131da6c28aaSamw  *  LARGE_INTEGER LastAccessTime;      last access time
132da6c28aaSamw  *  LARGE_INTEGER LastWriteTime;       last write time
133da6c28aaSamw  *  LARGE_INTEGER ChangeTime;          last attribute change time
134da6c28aaSamw  *  LARGE_INTEGER EndOfFile;           file size
135da6c28aaSamw  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
136da6c28aaSamw  *  ULONG ExtFileAttributes;           Extended file attributes
137da6c28aaSamw  *					(see section 3.11)
138da6c28aaSamw  *  ULONG FileNameLength;              Length of filename in bytes
139da6c28aaSamw  *  ULONG EaSize;                      Size of file's extended attributes
140da6c28aaSamw  *  STRING FileName;                   Name of the file
141da6c28aaSamw  *
142b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
143b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO
144b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
145b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  This is the same as SMB_FIND_FILE_FULL_DIRECTORY_INFO but with
146b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  FileId inserted after EaSize. FileId is preceded by a 4 byte
147b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  alignment padding.
148b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
149b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  Response Field                     Description
150b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  =================================  ==================================
151b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  ...
152b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  ULONG EaSize;                      Size of file's extended attributes
153b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  UCHAR Reserved[4]
154b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  LARGE_INTEGER FileId               Internal file system unique id.
155b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  STRING FileName;                   Name of the file
156b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
157da6c28aaSamw  * 4.3.4.6   SMB_FIND_FILE_BOTH_DIRECTORY_INFO
158da6c28aaSamw  *
159da6c28aaSamw  *  Response Field                     Description
160da6c28aaSamw  *  =================================  ==================================
161da6c28aaSamw  *
162da6c28aaSamw  *  ULONG NextEntryOffset;             Offset from this structure to
163da6c28aaSamw  *					beginning of next one
164da6c28aaSamw  *  ULONG FileIndex;
165da6c28aaSamw  *  LARGE_INTEGER CreationTime;        file creation time
166da6c28aaSamw  *  LARGE_INTEGER LastAccessTime;      last access time
167da6c28aaSamw  *  LARGE_INTEGER LastWriteTime;       last write time
168da6c28aaSamw  *  LARGE_INTEGER ChangeTime;          last attribute change time
169da6c28aaSamw  *  LARGE_INTEGER EndOfFile;           file size
170da6c28aaSamw  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
171da6c28aaSamw  *  ULONG ExtFileAttributes;           Extended file attributes
172da6c28aaSamw  *					(see section 3.11)
173da6c28aaSamw  *  ULONG FileNameLength;              Length of FileName in bytes
174da6c28aaSamw  *  ULONG EaSize;                      Size of file's extended attributes
175da6c28aaSamw  *  UCHAR ShortNameLength;             Length of file's short name in bytes
176da6c28aaSamw  *  UCHAR Reserved
177da6c28aaSamw  *  WCHAR ShortName[12];               File's 8.3 conformant name in Unicode
178da6c28aaSamw  *  STRING FileName;                   Files full length name
179da6c28aaSamw  *
180b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
181b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO
182b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
183b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  This is the same as SMB_FIND_FILE_BOTH_DIRECTORY_INFO but with
184b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  FileId inserted after ShortName. FileId is preceded by a 2 byte
185b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  alignment pad.
186b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
187b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  Response Field                     Description
188b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  =================================  ==================================
189b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  ...
190b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  WCHAR ShortName[12];               File's 8.3 conformant name in Unicode
191b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  UCHAR Reserved[2]
192b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  LARGE_INTEGER FileId               Internal file system unique id.
193b89a8333Snatalie li - Sun Microsystems - Irvine United States  *  STRING FileName;                   Files full length name
194b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
195da6c28aaSamw  * 4.3.4.7   SMB_FIND_FILE_NAMES_INFO
196da6c28aaSamw  *
197da6c28aaSamw  *  Response Field                     Description
198da6c28aaSamw  *  =================================  ==================================
199da6c28aaSamw  *
200da6c28aaSamw  *  ULONG NextEntryOffset;             Offset from this structure to
201da6c28aaSamw  *                                     beginning of next one
202da6c28aaSamw  *  ULONG FileIndex;
203da6c28aaSamw  *  ULONG FileNameLength;              Length of FileName in bytes
204da6c28aaSamw  *  STRING FileName;                   Files full length name
205da6c28aaSamw  */
206da6c28aaSamw 
207bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
208da6c28aaSamw #include <smbsrv/msgbuf.h>
209da6c28aaSamw #include <smbsrv/smb_fsops.h>
210da6c28aaSamw 
211bfbce3c1SGordon Ross /*
212bfbce3c1SGordon Ross  * Args (and other state) that we carry around among the
213bfbce3c1SGordon Ross  * various functions involved in FindFirst, FindNext.
214bfbce3c1SGordon Ross  */
2157f667e74Sjose borrego typedef struct smb_find_args {
216bfbce3c1SGordon Ross 	uint32_t fa_maxdata;
2177f667e74Sjose borrego 	uint16_t fa_infolev;
2187f667e74Sjose borrego 	uint16_t fa_maxcount;
2197f667e74Sjose borrego 	uint16_t fa_fflag;
220bfbce3c1SGordon Ross 	uint16_t fa_eos;	/* End Of Search */
221bfbce3c1SGordon Ross 	uint16_t fa_lno;	/* Last Name Offset */
222bfbce3c1SGordon Ross 	uint32_t fa_lastkey;	/* Last resume key */
223bfbce3c1SGordon Ross 	char fa_lastname[MAXNAMELEN]; /* and name */
2247f667e74Sjose borrego } smb_find_args_t;
2257f667e74Sjose borrego 
2267f667e74Sjose borrego static int smb_trans2_find_entries(smb_request_t *, smb_xa_t *,
227bfbce3c1SGordon Ross     smb_odir_t *, smb_find_args_t *);
228dc20a302Sas200622 static int smb_trans2_find_get_maxdata(smb_request_t *, uint16_t, uint16_t);
2297f667e74Sjose borrego static int smb_trans2_find_mbc_encode(smb_request_t *, smb_xa_t *,
2307f667e74Sjose borrego     smb_fileinfo_t *, smb_find_args_t *);
231da6c28aaSamw 
232da6c28aaSamw /*
233dc20a302Sas200622  * Tunable parameter to limit the maximum
234dc20a302Sas200622  * number of entries to be returned.
235da6c28aaSamw  */
236dc20a302Sas200622 uint16_t smb_trans2_find_max = 128;
237da6c28aaSamw 
238da6c28aaSamw /*
239da6c28aaSamw  * smb_com_trans2_find_first2
240da6c28aaSamw  *
241da6c28aaSamw  *  Client Request                Value
242da6c28aaSamw  *  ============================  ==================================
243da6c28aaSamw  *
244da6c28aaSamw  *  UCHAR  WordCount              15
245da6c28aaSamw  *  UCHAR  TotalDataCount         Total size of extended attribute list
246da6c28aaSamw  *  UCHAR  SetupCount             1
247da6c28aaSamw  *  UCHAR  Setup[0]               TRANS2_FIND_FIRST2
248da6c28aaSamw  *
249da6c28aaSamw  *  Parameter Block Encoding      Description
250da6c28aaSamw  *  ============================  ==================================
251da6c28aaSamw  *  USHORT SearchAttributes;
252da6c28aaSamw  *  USHORT SearchCount;           Maximum number of entries to return
253da6c28aaSamw  *  USHORT Flags;                 Additional information:
254da6c28aaSamw  *                                Bit 0 - close search after this request
255da6c28aaSamw  *                                Bit 1 - close search if end of search
256da6c28aaSamw  *                                reached
257da6c28aaSamw  *                                Bit 2 - return resume keys for each
258da6c28aaSamw  *                                entry found
259da6c28aaSamw  *                                Bit 3 - continue search from previous
260da6c28aaSamw  *                                ending place
261da6c28aaSamw  *                                Bit 4 - find with backup intent
262da6c28aaSamw  *  USHORT InformationLevel;      See below
263da6c28aaSamw  *  ULONG SearchStorageType;
264da6c28aaSamw  *  STRING FileName;              Pattern for the search
265da6c28aaSamw  *  UCHAR Data[ TotalDataCount ]  FEAList if InformationLevel is
266da6c28aaSamw  *                                QUERY_EAS_FROM_LIST
267da6c28aaSamw  *
268da6c28aaSamw  *  Response Parameter Block      Description
269da6c28aaSamw  *  ============================  ==================================
270da6c28aaSamw  *
271da6c28aaSamw  *  USHORT Sid;                   Search handle
272da6c28aaSamw  *  USHORT SearchCount;           Number of entries returned
273da6c28aaSamw  *  USHORT EndOfSearch;           Was last entry returned?
274da6c28aaSamw  *  USHORT EaErrorOffset;         Offset into EA list if EA error
275da6c28aaSamw  *  USHORT LastNameOffset;        Offset into data to file name of last
276da6c28aaSamw  *                                entry, if server needs it to resume
277da6c28aaSamw  *                                search; else 0
278da6c28aaSamw  *  UCHAR Data[ TotalDataCount ]  Level dependent info about the matches
279da6c28aaSamw  *                                found in the search
280da6c28aaSamw  */
2817b59d02dSjb150015 smb_sdrc_t
282dc20a302Sas200622 smb_com_trans2_find_first2(smb_request_t *sr, smb_xa_t *xa)
283da6c28aaSamw {
2847f667e74Sjose borrego 	int		count;
2857f667e74Sjose borrego 	uint16_t	sattr, odid;
2869fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_pathname_t	*pn;
2877f667e74Sjose borrego 	smb_odir_t	*od;
2887f667e74Sjose borrego 	smb_find_args_t	args;
289eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	uint32_t	odir_flags = 0;
2907f667e74Sjose borrego 
2917f667e74Sjose borrego 	bzero(&args, sizeof (smb_find_args_t));
292da6c28aaSamw 
293da6c28aaSamw 	if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) {
294dc20a302Sas200622 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
295da6c28aaSamw 		    ERRDOS, ERROR_ACCESS_DENIED);
296faa1795aSjb150015 		return (SDRC_ERROR);
297da6c28aaSamw 	}
298da6c28aaSamw 
2999fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	pn = &sr->arg.dirop.fqi.fq_path;
3009fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
3017f667e74Sjose borrego 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwww4.u", sr, &sattr,
3029fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    &args.fa_maxcount, &args.fa_fflag, &args.fa_infolev,
3039fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    &pn->pn_path) != 0) {
304faa1795aSjb150015 		return (SDRC_ERROR);
305da6c28aaSamw 	}
306da6c28aaSamw 
3079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_pathname_init(sr, pn, pn->pn_path);
3089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (!smb_pathname_validate(sr, pn))
3099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
3109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
3119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_is_stream_name(pn->pn_path)) {
312b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
313b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    ERRDOS, ERROR_INVALID_NAME);
314b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (SDRC_ERROR);
315b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
316b89a8333Snatalie li - Sun Microsystems - Irvine United States 
317eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (args.fa_fflag & SMB_FIND_WITH_BACKUP_INTENT) {
318b89a8333Snatalie li - Sun Microsystems - Irvine United States 		sr->user_cr = smb_user_getprivcred(sr->uid_user);
319eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		odir_flags = SMB_ODIR_OPENF_BACKUP_INTENT;
320eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
321b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3227f667e74Sjose borrego 	args.fa_maxdata =
3237f667e74Sjose borrego 	    smb_trans2_find_get_maxdata(sr, args.fa_infolev, args.fa_fflag);
3247f667e74Sjose borrego 	if (args.fa_maxdata == 0)
325faa1795aSjb150015 		return (SDRC_ERROR);
326dc20a302Sas200622 
3279fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	odid = smb_odir_open(sr, pn->pn_path, sattr, odir_flags);
3289fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (odid == 0) {
3299fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (sr->smb_error.status == NT_STATUS_OBJECT_PATH_NOT_FOUND) {
3309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
3319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			    ERRDOS, ERROR_FILE_NOT_FOUND);
3329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		}
333faa1795aSjb150015 		return (SDRC_ERROR);
3349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	}
335da6c28aaSamw 
3363b13a1efSThomas Keiser 	od = smb_tree_lookup_odir(sr, odid);
3377f667e74Sjose borrego 	if (od == NULL)
3387f667e74Sjose borrego 		return (SDRC_ERROR);
339bfbce3c1SGordon Ross 
340bfbce3c1SGordon Ross 	count = smb_trans2_find_entries(sr, xa, od, &args);
341dc20a302Sas200622 
3427f667e74Sjose borrego 	if (count == -1) {
3437f667e74Sjose borrego 		smb_odir_close(od);
344a1511e6bSjoyce mcintosh 		smb_odir_release(od);
345faa1795aSjb150015 		return (SDRC_ERROR);
346da6c28aaSamw 	}
347da6c28aaSamw 
3487f667e74Sjose borrego 	if (count == 0) {
3497f667e74Sjose borrego 		smb_odir_close(od);
350a1511e6bSjoyce mcintosh 		smb_odir_release(od);
3517f667e74Sjose borrego 		smbsr_errno(sr, ENOENT);
3527f667e74Sjose borrego 		return (SDRC_ERROR);
353da6c28aaSamw 	}
354da6c28aaSamw 
3557f667e74Sjose borrego 	if ((args.fa_fflag & SMB_FIND_CLOSE_AFTER_REQUEST) ||
356bfbce3c1SGordon Ross 	    (args.fa_eos && (args.fa_fflag & SMB_FIND_CLOSE_AT_EOS))) {
3577f667e74Sjose borrego 		smb_odir_close(od);
3587f667e74Sjose borrego 	} /* else leave odir open for trans2_find_next2 */
3597f667e74Sjose borrego 
360a1511e6bSjoyce mcintosh 	smb_odir_release(od);
361a1511e6bSjoyce mcintosh 
3623db3f65cSamw 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwwww",
363bfbce3c1SGordon Ross 	    odid,	/* Search ID */
364bfbce3c1SGordon Ross 	    count,	/* Search Count */
365bfbce3c1SGordon Ross 	    args.fa_eos, /* End Of Search */
366bfbce3c1SGordon Ross 	    0,		/* EA Error Offset */
367bfbce3c1SGordon Ross 	    args.fa_lno); /* Last Name Offset */
368da6c28aaSamw 
369faa1795aSjb150015 	return (SDRC_SUCCESS);
370da6c28aaSamw }
371da6c28aaSamw 
372da6c28aaSamw /*
373da6c28aaSamw  * smb_com_trans2_find_next2
374da6c28aaSamw  *
375da6c28aaSamw  *  Client Request                     Value
376da6c28aaSamw  *  ================================== =================================
377da6c28aaSamw  *
378da6c28aaSamw  *  WordCount                          15
379da6c28aaSamw  *  SetupCount                         1
380da6c28aaSamw  *  Setup[0]                           TRANS2_FIND_NEXT2
381da6c28aaSamw  *
382da6c28aaSamw  *  Parameter Block Encoding           Description
383da6c28aaSamw  *  ================================== =================================
384da6c28aaSamw  *
385da6c28aaSamw  *  USHORT Sid;                        Search handle
386da6c28aaSamw  *  USHORT SearchCount;                Maximum number of entries to
387da6c28aaSamw  *                                      return
388da6c28aaSamw  *  USHORT InformationLevel;           Levels described in
389da6c28aaSamw  *                                      TRANS2_FIND_FIRST2 request
390da6c28aaSamw  *  ULONG ResumeKey;                   Value returned by previous find2
391da6c28aaSamw  *                                      call
392da6c28aaSamw  *  USHORT Flags;                      Additional information: bit set-
393da6c28aaSamw  *                                      0 - close search after this
394da6c28aaSamw  *                                      request
395da6c28aaSamw  *                                      1 - close search if end of search
396da6c28aaSamw  *                                      reached
397da6c28aaSamw  *                                      2 - return resume keys for each
398da6c28aaSamw  *                                      entry found
399da6c28aaSamw  *                                      3 - resume/continue from previous
400da6c28aaSamw  *                                      ending place
401da6c28aaSamw  *                                      4 - find with backup intent
402da6c28aaSamw  *  STRING FileName;                   Resume file name
403da6c28aaSamw  *
404da6c28aaSamw  * Sid is the value returned by a previous successful TRANS2_FIND_FIRST2
405da6c28aaSamw  * call.  If Bit3 of Flags is set, then FileName may be the NULL string,
406da6c28aaSamw  * since the search is continued from the previous TRANS2_FIND request.
407da6c28aaSamw  * Otherwise, FileName must not be more than 256 characters long.
408da6c28aaSamw  *
409da6c28aaSamw  *  Response Field                     Description
410da6c28aaSamw  *  ================================== =================================
411da6c28aaSamw  *
412da6c28aaSamw  *  USHORT SearchCount;                Number of entries returned
413da6c28aaSamw  *  USHORT EndOfSearch;                Was last entry returned?
414da6c28aaSamw  *  USHORT EaErrorOffset;              Offset into EA list if EA error
415da6c28aaSamw  *  USHORT LastNameOffset;             Offset into data to file name of
416da6c28aaSamw  *                                      last entry, if server needs it to
417da6c28aaSamw  *                                      resume search; else 0
418da6c28aaSamw  *  UCHAR Data[TotalDataCount]         Level dependent info about the
419da6c28aaSamw  *                                      matches found in the search
420b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
421b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
422b89a8333Snatalie li - Sun Microsystems - Irvine United States  * The last parameter in the request is a filename, which is a
423b89a8333Snatalie li - Sun Microsystems - Irvine United States  * null-terminated unicode string.
424b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
425b89a8333Snatalie li - Sun Microsystems - Irvine United States  * smb_mbc_decodef(&xa->req_param_mb, "%www lwu", sr,
4267f667e74Sjose borrego  *    &odid, &fa_maxcount, &fa_infolev, &cookie, &fa_fflag, &fname)
427b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
4287f667e74Sjose borrego  * The filename parameter is not currently decoded because we
4297f667e74Sjose borrego  * expect a 2-byte null but Mac OS 10 clients send a 1-byte null,
430b89a8333Snatalie li - Sun Microsystems - Irvine United States  * which leads to a decode error.
431b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Thus, we do not support resume by filename.  We treat a request
432b89a8333Snatalie li - Sun Microsystems - Irvine United States  * to resume by filename as SMB_FIND_CONTINUE_FROM_LAST.
433da6c28aaSamw  */
4347b59d02dSjb150015 smb_sdrc_t
435dc20a302Sas200622 smb_com_trans2_find_next2(smb_request_t *sr, smb_xa_t *xa)
436da6c28aaSamw {
4377f667e74Sjose borrego 	int			count;
4387f667e74Sjose borrego 	uint16_t		odid;
4397f667e74Sjose borrego 	smb_odir_t		*od;
4407f667e74Sjose borrego 	smb_find_args_t		args;
4417f667e74Sjose borrego 	smb_odir_resume_t	odir_resume;
442da6c28aaSamw 
443bfbce3c1SGordon Ross 	bzero(&args, sizeof (args));
444bfbce3c1SGordon Ross 	bzero(&odir_resume, sizeof (odir_resume));
4457f667e74Sjose borrego 
446bfbce3c1SGordon Ross 	if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) {
447bfbce3c1SGordon Ross 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
448bfbce3c1SGordon Ross 		    ERRDOS, ERROR_ACCESS_DENIED);
449faa1795aSjb150015 		return (SDRC_ERROR);
450da6c28aaSamw 	}
451da6c28aaSamw 
452bfbce3c1SGordon Ross 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwwlwu", sr,
453bfbce3c1SGordon Ross 	    &odid, &args.fa_maxcount, &args.fa_infolev,
454bfbce3c1SGordon Ross 	    &odir_resume.or_cookie, &args.fa_fflag,
455bfbce3c1SGordon Ross 	    &odir_resume.or_fname) != 0) {
456bfbce3c1SGordon Ross 		return (SDRC_ERROR);
4577f667e74Sjose borrego 	}
458b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4597f667e74Sjose borrego 	if (args.fa_fflag & SMB_FIND_WITH_BACKUP_INTENT)
460b89a8333Snatalie li - Sun Microsystems - Irvine United States 		sr->user_cr = smb_user_getprivcred(sr->uid_user);
461b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4627f667e74Sjose borrego 	args.fa_maxdata =
4637f667e74Sjose borrego 	    smb_trans2_find_get_maxdata(sr, args.fa_infolev, args.fa_fflag);
4647f667e74Sjose borrego 	if (args.fa_maxdata == 0)
4657f667e74Sjose borrego 		return (SDRC_ERROR);
4667f667e74Sjose borrego 
4673b13a1efSThomas Keiser 	od = smb_tree_lookup_odir(sr, odid);
4687f667e74Sjose borrego 	if (od == NULL) {
4697f667e74Sjose borrego 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
4707f667e74Sjose borrego 		    ERRDOS, ERROR_INVALID_HANDLE);
4717f667e74Sjose borrego 		return (SDRC_ERROR);
4727f667e74Sjose borrego 	}
4737f667e74Sjose borrego 
474bfbce3c1SGordon Ross 	/*
475bfbce3c1SGordon Ross 	 * Set the correct position in the directory.
476bfbce3c1SGordon Ross 	 *
477bfbce3c1SGordon Ross 	 * "Continue from last" is easy, but due to a history of
478bfbce3c1SGordon Ross 	 * buggy server implementations, most clients don't use
479bfbce3c1SGordon Ross 	 * that method.  The most widely used (and reliable) is
480bfbce3c1SGordon Ross 	 * resume by file name.  Unfortunately, that can't really
481bfbce3c1SGordon Ross 	 * be fully supported unless your file system stores all
482bfbce3c1SGordon Ross 	 * directory entries in some sorted order (like NTFS).
483bfbce3c1SGordon Ross 	 * We can partially support resume by name, where the only
484bfbce3c1SGordon Ross 	 * name we're ever asked to resume on is the same as the
485bfbce3c1SGordon Ross 	 * most recent we returned.  That's always what the client
486bfbce3c1SGordon Ross 	 * gives us as the resume name, so we can simply remember
487bfbce3c1SGordon Ross 	 * the last name/offset pair and use that to position on
488bfbce3c1SGordon Ross 	 * the following FindNext call.  In the unlikely event
489bfbce3c1SGordon Ross 	 * that the client asks to resume somewhere else, we'll
490bfbce3c1SGordon Ross 	 * use the numeric resume key, and hope the client gives
491bfbce3c1SGordon Ross 	 * correctly uses one of the resume keys we provided.
492bfbce3c1SGordon Ross 	 */
493bfbce3c1SGordon Ross 	if (args.fa_fflag & SMB_FIND_CONTINUE_FROM_LAST) {
494bfbce3c1SGordon Ross 		odir_resume.or_type = SMB_ODIR_RESUME_CONT;
495bfbce3c1SGordon Ross 	} else {
496bfbce3c1SGordon Ross 		odir_resume.or_type = SMB_ODIR_RESUME_FNAME;
497bfbce3c1SGordon Ross 	}
498bfbce3c1SGordon Ross 	smb_odir_resume_at(od, &odir_resume);
499bfbce3c1SGordon Ross 
500bfbce3c1SGordon Ross 	count = smb_trans2_find_entries(sr, xa, od, &args);
5017f667e74Sjose borrego 	if (count == -1) {
5027f667e74Sjose borrego 		smb_odir_close(od);
503a1511e6bSjoyce mcintosh 		smb_odir_release(od);
504faa1795aSjb150015 		return (SDRC_ERROR);
505da6c28aaSamw 	}
506da6c28aaSamw 
5077f667e74Sjose borrego 	if ((args.fa_fflag & SMB_FIND_CLOSE_AFTER_REQUEST) ||
508bfbce3c1SGordon Ross 	    (args.fa_eos && (args.fa_fflag & SMB_FIND_CLOSE_AT_EOS))) {
5097f667e74Sjose borrego 		smb_odir_close(od);
5107f667e74Sjose borrego 	} /* else leave odir open for trans2_find_next2 */
5117f667e74Sjose borrego 
512a1511e6bSjoyce mcintosh 	smb_odir_release(od);
513bfbce3c1SGordon Ross 
5147f667e74Sjose borrego 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwww",
515bfbce3c1SGordon Ross 	    count,	/* Search Count */
516bfbce3c1SGordon Ross 	    args.fa_eos, /* End Of Search */
517bfbce3c1SGordon Ross 	    0,		/* EA Error Offset */
518bfbce3c1SGordon Ross 	    args.fa_lno); /* Last Name Offset */
5197f667e74Sjose borrego 
5207f667e74Sjose borrego 	return (SDRC_SUCCESS);
521da6c28aaSamw }
522da6c28aaSamw 
5237f667e74Sjose borrego 
524da6c28aaSamw /*
5257f667e74Sjose borrego  * smb_trans2_find_entries
5267f667e74Sjose borrego  *
5277f667e74Sjose borrego  * Find and encode up to args->fa_maxcount directory entries.
5287f667e74Sjose borrego  * For compatibilty with Windows, if args->fa_maxcount is zero treat it as 1.
5297f667e74Sjose borrego  *
5307f667e74Sjose borrego  * Returns:
5317f667e74Sjose borrego  *   count - count of entries encoded
5327f667e74Sjose borrego  *           *eos = B_TRUE if no more directory entries
5337f667e74Sjose borrego  *      -1 - error
534dc20a302Sas200622  */
5357f667e74Sjose borrego static int
5367f667e74Sjose borrego smb_trans2_find_entries(smb_request_t *sr, smb_xa_t *xa, smb_odir_t *od,
537bfbce3c1SGordon Ross     smb_find_args_t *args)
5387f667e74Sjose borrego {
5397f667e74Sjose borrego 	smb_fileinfo_t	fileinfo;
540bfbce3c1SGordon Ross 	smb_odir_resume_t odir_resume;
541*b819cea2SGordon Ross 	uint16_t	count, maxcount;
542*b819cea2SGordon Ross 	int		rc = -1;
5437f667e74Sjose borrego 
5447f667e74Sjose borrego 	if ((maxcount = args->fa_maxcount) == 0)
545dc20a302Sas200622 		maxcount = 1;
546dc20a302Sas200622 
547dc20a302Sas200622 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
548dc20a302Sas200622 		maxcount = smb_trans2_find_max;
549dc20a302Sas200622 
5507f667e74Sjose borrego 	count = 0;
5517f667e74Sjose borrego 	while (count < maxcount) {
552bfbce3c1SGordon Ross 		if (smb_odir_read_fileinfo(sr, od, &fileinfo, &args->fa_eos)
553bfbce3c1SGordon Ross 		    != 0)
5547f667e74Sjose borrego 			return (-1);
555bfbce3c1SGordon Ross 		if (args->fa_eos != 0)
5567f667e74Sjose borrego 			break;
5577f667e74Sjose borrego 
5587f667e74Sjose borrego 		rc = smb_trans2_find_mbc_encode(sr, xa, &fileinfo, args);
5597f667e74Sjose borrego 		if (rc == -1)
5607f667e74Sjose borrego 			return (-1);
5617f667e74Sjose borrego 		if (rc == 1)
5627f667e74Sjose borrego 			break;
5637f667e74Sjose borrego 
564bfbce3c1SGordon Ross 		/*
565bfbce3c1SGordon Ross 		 * Save the info about the last file returned.
566bfbce3c1SGordon Ross 		 */
567bfbce3c1SGordon Ross 		args->fa_lastkey = fileinfo.fi_cookie;
568bfbce3c1SGordon Ross 		bcopy(fileinfo.fi_name, args->fa_lastname, MAXNAMELEN);
569bfbce3c1SGordon Ross 
5707f667e74Sjose borrego 		++count;
571da6c28aaSamw 	}
572da6c28aaSamw 
5737f667e74Sjose borrego 	/* save the last cookie returned to client */
5747f667e74Sjose borrego 	if (count != 0)
575bfbce3c1SGordon Ross 		smb_odir_save_fname(od, args->fa_lastkey, args->fa_lastname);
576da6c28aaSamw 
5771fcced4cSJordan Brown 	/*
5781fcced4cSJordan Brown 	 * If all retrieved entries have been successfully encoded
5791fcced4cSJordan Brown 	 * and eos has not already been detected, check if there are
5801fcced4cSJordan Brown 	 * any more entries. eos will be set if there are no more.
5811fcced4cSJordan Brown 	 */
582bfbce3c1SGordon Ross 	if ((rc == 0) && (args->fa_eos == 0))
583bfbce3c1SGordon Ross 		(void) smb_odir_read_fileinfo(sr, od, &fileinfo, &args->fa_eos);
584bfbce3c1SGordon Ross 
585bfbce3c1SGordon Ross 	/*
586bfbce3c1SGordon Ross 	 * When the last entry we read from the directory did not
587bfbce3c1SGordon Ross 	 * fit in the return buffer, we will have read one entry
588bfbce3c1SGordon Ross 	 * that will not be returned in this call.  That, and the
589bfbce3c1SGordon Ross 	 * check for EOS just above both can leave the directory
590bfbce3c1SGordon Ross 	 * position incorrect for the next call.  Fix that now.
591bfbce3c1SGordon Ross 	 */
592bfbce3c1SGordon Ross 	bzero(&odir_resume, sizeof (odir_resume));
593bfbce3c1SGordon Ross 	odir_resume.or_type = SMB_ODIR_RESUME_COOKIE;
594bfbce3c1SGordon Ross 	odir_resume.or_cookie = args->fa_lastkey;
595bfbce3c1SGordon Ross 	smb_odir_resume_at(od, &odir_resume);
596da6c28aaSamw 
5977f667e74Sjose borrego 	return (count);
598da6c28aaSamw }
599da6c28aaSamw 
600da6c28aaSamw /*
601da6c28aaSamw  * smb_trans2_find_get_maxdata
602da6c28aaSamw  *
603dc20a302Sas200622  * Calculate the minimum response space required for the specified
604dc20a302Sas200622  * information level.
605da6c28aaSamw  *
606dc20a302Sas200622  * A non-zero return value provides the minimum space required.
607dc20a302Sas200622  * A return value of zero indicates an unknown information level.
608da6c28aaSamw  */
609dc20a302Sas200622 static int
610dc20a302Sas200622 smb_trans2_find_get_maxdata(smb_request_t *sr, uint16_t infolev, uint16_t fflag)
611da6c28aaSamw {
612da6c28aaSamw 	int maxdata;
613da6c28aaSamw 
614da6c28aaSamw 	maxdata = smb_ascii_or_unicode_null_len(sr);
615da6c28aaSamw 
616da6c28aaSamw 	switch (infolev) {
617da6c28aaSamw 	case SMB_INFO_STANDARD :
618da6c28aaSamw 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
619da6c28aaSamw 			maxdata += sizeof (int32_t);
620da6c28aaSamw 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 1;
621da6c28aaSamw 		break;
622da6c28aaSamw 
623da6c28aaSamw 	case SMB_INFO_QUERY_EA_SIZE:
624da6c28aaSamw 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
625da6c28aaSamw 			maxdata += sizeof (int32_t);
626da6c28aaSamw 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 4 + 1;
627da6c28aaSamw 		break;
628da6c28aaSamw 
629da6c28aaSamw 	case SMB_FIND_FILE_DIRECTORY_INFO:
630da6c28aaSamw 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4;
631da6c28aaSamw 		break;
632da6c28aaSamw 
633b89a8333Snatalie li - Sun Microsystems - Irvine United States 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
634b89a8333Snatalie li - Sun Microsystems - Irvine United States 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4;
635b89a8333Snatalie li - Sun Microsystems - Irvine United States 		break;
636b89a8333Snatalie li - Sun Microsystems - Irvine United States 
637b89a8333Snatalie li - Sun Microsystems - Irvine United States 	case SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
638b89a8333Snatalie li - Sun Microsystems - Irvine United States 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 4 + 8;
639b89a8333Snatalie li - Sun Microsystems - Irvine United States 		break;
640b89a8333Snatalie li - Sun Microsystems - Irvine United States 
641da6c28aaSamw 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
642da6c28aaSamw 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24;
643da6c28aaSamw 		break;
644da6c28aaSamw 
645b89a8333Snatalie li - Sun Microsystems - Irvine United States 	case SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
646b89a8333Snatalie li - Sun Microsystems - Irvine United States 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24
647b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    + 2 + 8;
648b89a8333Snatalie li - Sun Microsystems - Irvine United States 		break;
649b89a8333Snatalie li - Sun Microsystems - Irvine United States 
650da6c28aaSamw 	case SMB_FIND_FILE_NAMES_INFO:
651da6c28aaSamw 		maxdata += 4 + 4 + 4;
652da6c28aaSamw 		break;
653da6c28aaSamw 
654da6c28aaSamw 	case SMB_MAC_FIND_BOTH_HFS_INFO:
655da6c28aaSamw 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 1 + 1 + 2 +
656da6c28aaSamw 		    4 + 32 + 4 + 1 + 1 + 24 + 4;
657da6c28aaSamw 		break;
658da6c28aaSamw 
659da6c28aaSamw 	default:
660da6c28aaSamw 		maxdata = 0;
6617f667e74Sjose borrego 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
6627f667e74Sjose borrego 		    ERRDOS, ERROR_INVALID_LEVEL);
663da6c28aaSamw 	}
664da6c28aaSamw 
665da6c28aaSamw 	return (maxdata);
666da6c28aaSamw }
667da6c28aaSamw 
668da6c28aaSamw /*
669bfbce3c1SGordon Ross  * This is an experimental feature that allows us to return zero
670bfbce3c1SGordon Ross  * for all numeric resume keys, to match Windows behavior with an
671bfbce3c1SGordon Ross  * NTFS share.  Setting this variable to zero does that.
672bfbce3c1SGordon Ross  *
673bfbce3c1SGordon Ross  * It's possible we could remove this variable and always set
674bfbce3c1SGordon Ross  * numeric resume keys to zero, but that would leave us unable
675bfbce3c1SGordon Ross  * to handle a FindNext call with an arbitrary start position.
676bfbce3c1SGordon Ross  * In practice we never see these, but in theory we could.
677bfbce3c1SGordon Ross  *
678bfbce3c1SGordon Ross  * See the long comment above smb_com_trans2_find_next2() for
679bfbce3c1SGordon Ross  * more details about resume key / resume name handling.
680bfbce3c1SGordon Ross  */
681bfbce3c1SGordon Ross int smbd_use_resume_keys = 1;
682bfbce3c1SGordon Ross 
683bfbce3c1SGordon Ross /*
6847f667e74Sjose borrego  * smb_trans2_mbc_encode
685da6c28aaSamw  *
686da6c28aaSamw  * This function encodes the mbc for one directory entry.
687da6c28aaSamw  *
688da6c28aaSamw  * The function returns -1 when the max data requested by client
689da6c28aaSamw  * is reached. If the entry is valid and successful encoded, 0
690da6c28aaSamw  * will be returned; otherwise, 1 will be returned.
691b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
692b89a8333Snatalie li - Sun Microsystems - Irvine United States  * We always null terminate the filename. The space for the null
693b89a8333Snatalie li - Sun Microsystems - Irvine United States  * is included in the maxdata calculation and is therefore included
694b89a8333Snatalie li - Sun Microsystems - Irvine United States  * in the next_entry_offset. namelen is the unterminated length of
695b89a8333Snatalie li - Sun Microsystems - Irvine United States  * the filename. For levels except STANDARD and EA_SIZE, if the
696b89a8333Snatalie li - Sun Microsystems - Irvine United States  * filename is ascii the name length returned to the client should
697b89a8333Snatalie li - Sun Microsystems - Irvine United States  * include the null terminator. Otherwise the length returned to
698b89a8333Snatalie li - Sun Microsystems - Irvine United States  * the client should not include the terminator.
6997f667e74Sjose borrego  *
7007f667e74Sjose borrego  * Returns: 0 - data successfully encoded
7017f667e74Sjose borrego  *          1 - client request's maxdata limit reached
7027f667e74Sjose borrego  *	   -1 - error
703da6c28aaSamw  */
7047f667e74Sjose borrego static int
7057f667e74Sjose borrego smb_trans2_find_mbc_encode(smb_request_t *sr, smb_xa_t *xa,
7067f667e74Sjose borrego     smb_fileinfo_t *fileinfo, smb_find_args_t *args)
707da6c28aaSamw {
708bfbce3c1SGordon Ross 	int		namelen, shortlen;
709dc20a302Sas200622 	uint32_t	next_entry_offset;
7107f667e74Sjose borrego 	uint32_t	dsize32, asize32;
7117f667e74Sjose borrego 	uint32_t	mb_flags = 0;
712bfbce3c1SGordon Ross 	uint32_t	resume_key;
713da6c28aaSamw 	char		buf83[26];
714da6c28aaSamw 	smb_msgbuf_t	mb;
715da6c28aaSamw 
7167f667e74Sjose borrego 	namelen = smb_ascii_or_unicode_strlen(sr, fileinfo->fi_name);
717b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (namelen == -1)
718da6c28aaSamw 		return (-1);
719da6c28aaSamw 
720b89a8333Snatalie li - Sun Microsystems - Irvine United States 	/*
721b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 * If ascii the filename length returned to the client should
722b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 * include the null terminator for levels except STANDARD and
723b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 * EASIZE.
724b89a8333Snatalie li - Sun Microsystems - Irvine United States 	 */
725b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (!(sr->smb_flg2 & SMB_FLAGS2_UNICODE)) {
7267f667e74Sjose borrego 		if ((args->fa_infolev != SMB_INFO_STANDARD) &&
7277f667e74Sjose borrego 		    (args->fa_infolev != SMB_INFO_QUERY_EA_SIZE))
728b89a8333Snatalie li - Sun Microsystems - Irvine United States 			namelen += 1;
729b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
730b89a8333Snatalie li - Sun Microsystems - Irvine United States 
731bfbce3c1SGordon Ross 	next_entry_offset = args->fa_maxdata + namelen;
732bfbce3c1SGordon Ross 
733bfbce3c1SGordon Ross 	if (MBC_ROOM_FOR(&xa->rep_data_mb, (args->fa_maxdata + namelen)) == 0)
734bfbce3c1SGordon Ross 		return (1);
735bfbce3c1SGordon Ross 
7367f667e74Sjose borrego 	mb_flags = (sr->smb_flg2 & SMB_FLAGS2_UNICODE) ? SMB_MSGBUF_UNICODE : 0;
7377f667e74Sjose borrego 	dsize32 = (fileinfo->fi_size > UINT_MAX) ?
7387f667e74Sjose borrego 	    UINT_MAX : (uint32_t)fileinfo->fi_size;
7397f667e74Sjose borrego 	asize32 = (fileinfo->fi_alloc_size > UINT_MAX) ?
7407f667e74Sjose borrego 	    UINT_MAX : (uint32_t)fileinfo->fi_alloc_size;
741da6c28aaSamw 
742bfbce3c1SGordon Ross 	resume_key = fileinfo->fi_cookie;
743bfbce3c1SGordon Ross 	if (smbd_use_resume_keys == 0)
744bfbce3c1SGordon Ross 		resume_key = 0;
745bfbce3c1SGordon Ross 
746bfbce3c1SGordon Ross 	/*
747bfbce3c1SGordon Ross 	 * This switch handles all the "information levels" (formats)
748bfbce3c1SGordon Ross 	 * that we support.  Note that all formats have the file name
749bfbce3c1SGordon Ross 	 * placed after some fixed-size data, and the code to write
750bfbce3c1SGordon Ross 	 * the file name is factored out at the end of this switch.
751bfbce3c1SGordon Ross 	 */
7527f667e74Sjose borrego 	switch (args->fa_infolev) {
753da6c28aaSamw 	case SMB_INFO_STANDARD:
7547f667e74Sjose borrego 		if (args->fa_fflag & SMB_FIND_RETURN_RESUME_KEYS)
7553db3f65cSamw 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
756bfbce3c1SGordon Ross 			    resume_key);
757da6c28aaSamw 
758bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwb", sr,
759e3f2c991SKeyur Desai 		    smb_time_gmt_to_local(sr, fileinfo->fi_crtime.tv_sec),
760e3f2c991SKeyur Desai 		    smb_time_gmt_to_local(sr, fileinfo->fi_atime.tv_sec),
761e3f2c991SKeyur Desai 		    smb_time_gmt_to_local(sr, fileinfo->fi_mtime.tv_sec),
7626537f381Sas200622 		    dsize32,
7636537f381Sas200622 		    asize32,
7647f667e74Sjose borrego 		    fileinfo->fi_dosattr,
765bfbce3c1SGordon Ross 		    namelen);
766da6c28aaSamw 		break;
767da6c28aaSamw 
768da6c28aaSamw 	case SMB_INFO_QUERY_EA_SIZE:
7697f667e74Sjose borrego 		if (args->fa_fflag & SMB_FIND_RETURN_RESUME_KEYS)
7703db3f65cSamw 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
771bfbce3c1SGordon Ross 			    resume_key);
772da6c28aaSamw 
773bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwlb", sr,
774e3f2c991SKeyur Desai 		    smb_time_gmt_to_local(sr, fileinfo->fi_crtime.tv_sec),
775e3f2c991SKeyur Desai 		    smb_time_gmt_to_local(sr, fileinfo->fi_atime.tv_sec),
776e3f2c991SKeyur Desai 		    smb_time_gmt_to_local(sr, fileinfo->fi_mtime.tv_sec),
7776537f381Sas200622 		    dsize32,
7786537f381Sas200622 		    asize32,
7797f667e74Sjose borrego 		    fileinfo->fi_dosattr,
780da6c28aaSamw 		    0L,		/* EA Size */
781bfbce3c1SGordon Ross 		    namelen);
782da6c28aaSamw 		break;
783da6c28aaSamw 
784da6c28aaSamw 	case SMB_FIND_FILE_DIRECTORY_INFO:
785bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqll", sr,
786dc20a302Sas200622 		    next_entry_offset,
787bfbce3c1SGordon Ross 		    resume_key,
7887f667e74Sjose borrego 		    &fileinfo->fi_crtime,
7897f667e74Sjose borrego 		    &fileinfo->fi_atime,
7907f667e74Sjose borrego 		    &fileinfo->fi_mtime,
7917f667e74Sjose borrego 		    &fileinfo->fi_ctime,
7927f667e74Sjose borrego 		    fileinfo->fi_size,
7937f667e74Sjose borrego 		    fileinfo->fi_alloc_size,
7947f667e74Sjose borrego 		    fileinfo->fi_dosattr,
795bfbce3c1SGordon Ross 		    namelen);
796b89a8333Snatalie li - Sun Microsystems - Irvine United States 		break;
797b89a8333Snatalie li - Sun Microsystems - Irvine United States 
798b89a8333Snatalie li - Sun Microsystems - Irvine United States 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
799bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlll", sr,
800b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    next_entry_offset,
801bfbce3c1SGordon Ross 		    resume_key,
8027f667e74Sjose borrego 		    &fileinfo->fi_crtime,
8037f667e74Sjose borrego 		    &fileinfo->fi_atime,
8047f667e74Sjose borrego 		    &fileinfo->fi_mtime,
8057f667e74Sjose borrego 		    &fileinfo->fi_ctime,
8067f667e74Sjose borrego 		    fileinfo->fi_size,
8077f667e74Sjose borrego 		    fileinfo->fi_alloc_size,
8087f667e74Sjose borrego 		    fileinfo->fi_dosattr,
809b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    namelen,
810bfbce3c1SGordon Ross 		    0L);
811b89a8333Snatalie li - Sun Microsystems - Irvine United States 		break;
812b89a8333Snatalie li - Sun Microsystems - Irvine United States 
813b89a8333Snatalie li - Sun Microsystems - Irvine United States 	case SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
814bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlll4.q", sr,
815b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    next_entry_offset,
816bfbce3c1SGordon Ross 		    resume_key,
8177f667e74Sjose borrego 		    &fileinfo->fi_crtime,
8187f667e74Sjose borrego 		    &fileinfo->fi_atime,
8197f667e74Sjose borrego 		    &fileinfo->fi_mtime,
8207f667e74Sjose borrego 		    &fileinfo->fi_ctime,
8217f667e74Sjose borrego 		    fileinfo->fi_size,
8227f667e74Sjose borrego 		    fileinfo->fi_alloc_size,
8237f667e74Sjose borrego 		    fileinfo->fi_dosattr,
824b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    namelen,
825b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    0L,
826bfbce3c1SGordon Ross 		    fileinfo->fi_nodeid);
827da6c28aaSamw 		break;
828da6c28aaSamw 
829da6c28aaSamw 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
830da6c28aaSamw 		bzero(buf83, sizeof (buf83));
831dc20a302Sas200622 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
832da6c28aaSamw 		    mb_flags);
8337f667e74Sjose borrego 		if (smb_msgbuf_encode(&mb, "U", fileinfo->fi_shortname) < 0) {
834da6c28aaSamw 			smb_msgbuf_term(&mb);
835da6c28aaSamw 			return (-1);
836da6c28aaSamw 		}
837bbf6f00cSJordan Brown 		shortlen = smb_wcequiv_strlen(fileinfo->fi_shortname);
838da6c28aaSamw 
839bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlllb.24c",
840dc20a302Sas200622 		    sr,
841dc20a302Sas200622 		    next_entry_offset,
842bfbce3c1SGordon Ross 		    resume_key,
8437f667e74Sjose borrego 		    &fileinfo->fi_crtime,
8447f667e74Sjose borrego 		    &fileinfo->fi_atime,
8457f667e74Sjose borrego 		    &fileinfo->fi_mtime,
8467f667e74Sjose borrego 		    &fileinfo->fi_ctime,
8477f667e74Sjose borrego 		    fileinfo->fi_size,
8487f667e74Sjose borrego 		    fileinfo->fi_alloc_size,
8497f667e74Sjose borrego 		    fileinfo->fi_dosattr,
850b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    namelen,
851da6c28aaSamw 		    0L,
852dc20a302Sas200622 		    shortlen,
853bfbce3c1SGordon Ross 		    buf83);
854da6c28aaSamw 
855da6c28aaSamw 		smb_msgbuf_term(&mb);
856da6c28aaSamw 		break;
857da6c28aaSamw 
858b89a8333Snatalie li - Sun Microsystems - Irvine United States 	case SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
859b89a8333Snatalie li - Sun Microsystems - Irvine United States 		bzero(buf83, sizeof (buf83));
860b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
861b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    mb_flags);
8627f667e74Sjose borrego 		if (smb_msgbuf_encode(&mb, "u", fileinfo->fi_shortname) < 0) {
863b89a8333Snatalie li - Sun Microsystems - Irvine United States 			smb_msgbuf_term(&mb);
864b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (-1);
865b89a8333Snatalie li - Sun Microsystems - Irvine United States 		}
8667f667e74Sjose borrego 		shortlen = smb_ascii_or_unicode_strlen(sr,
8677f667e74Sjose borrego 		    fileinfo->fi_shortname);
868b89a8333Snatalie li - Sun Microsystems - Irvine United States 
869b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) smb_mbc_encodef(&xa->rep_data_mb,
870bfbce3c1SGordon Ross 		    "%llTTTTqqlllb.24c2.q",
871b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    sr,
872b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    next_entry_offset,
873bfbce3c1SGordon Ross 		    resume_key,
8747f667e74Sjose borrego 		    &fileinfo->fi_crtime,
8757f667e74Sjose borrego 		    &fileinfo->fi_atime,
8767f667e74Sjose borrego 		    &fileinfo->fi_mtime,
8777f667e74Sjose borrego 		    &fileinfo->fi_ctime,
8787f667e74Sjose borrego 		    fileinfo->fi_size,
8797f667e74Sjose borrego 		    fileinfo->fi_alloc_size,
8807f667e74Sjose borrego 		    fileinfo->fi_dosattr,
881b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    namelen,
882b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    0L,
883b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    shortlen,
884b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    buf83,
885bfbce3c1SGordon Ross 		    fileinfo->fi_nodeid);
886b89a8333Snatalie li - Sun Microsystems - Irvine United States 
887b89a8333Snatalie li - Sun Microsystems - Irvine United States 		smb_msgbuf_term(&mb);
888b89a8333Snatalie li - Sun Microsystems - Irvine United States 		break;
889b89a8333Snatalie li - Sun Microsystems - Irvine United States 
890da6c28aaSamw 	case SMB_FIND_FILE_NAMES_INFO:
891bfbce3c1SGordon Ross 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%lll", sr,
892dc20a302Sas200622 		    next_entry_offset,
893bfbce3c1SGordon Ross 		    resume_key,
894bfbce3c1SGordon Ross 		    namelen);
895da6c28aaSamw 		break;
896bfbce3c1SGordon Ross 
897bfbce3c1SGordon Ross 	default:
898bfbce3c1SGordon Ross 		/* invalid info. level */
899bfbce3c1SGordon Ross 		return (-1);
900da6c28aaSamw 	}
901da6c28aaSamw 
902bfbce3c1SGordon Ross 	/*
903bfbce3c1SGordon Ross 	 * At this point we have written all the fixed-size data
904bfbce3c1SGordon Ross 	 * for the specified info. level, and we're about to put
905bfbce3c1SGordon Ross 	 * the file name string in the message.  We may later
906bfbce3c1SGordon Ross 	 * need the offset in the trans2 data where this string
907bfbce3c1SGordon Ross 	 * is placed, so save the message position now.  Note:
908bfbce3c1SGordon Ross 	 * We also need to account for the alignment padding
909bfbce3c1SGordon Ross 	 * that may precede the unicode string.
910bfbce3c1SGordon Ross 	 */
911bfbce3c1SGordon Ross 	args->fa_lno = xa->rep_data_mb.chain_offset;
912bfbce3c1SGordon Ross 	if ((sr->smb_flg2 & SMB_FLAGS2_UNICODE) != 0 &&
913bfbce3c1SGordon Ross 	    (args->fa_lno & 1) != 0)
914bfbce3c1SGordon Ross 		args->fa_lno++;
915bfbce3c1SGordon Ross 
916bfbce3c1SGordon Ross 	(void) smb_mbc_encodef(&xa->rep_data_mb, "%u", sr,
917bfbce3c1SGordon Ross 	    fileinfo->fi_name);
918bfbce3c1SGordon Ross 
919da6c28aaSamw 	return (0);
920da6c28aaSamw }
921da6c28aaSamw 
922da6c28aaSamw /*
923da6c28aaSamw  * Close a search started by a Trans2FindFirst2 request.
924da6c28aaSamw  */
9257b59d02dSjb150015 smb_sdrc_t
926faa1795aSjb150015 smb_pre_find_close2(smb_request_t *sr)
927faa1795aSjb150015 {
928faa1795aSjb150015 	DTRACE_SMB_1(op__FindClose2__start, smb_request_t *, sr);
9297f667e74Sjose borrego 	return (SDRC_SUCCESS);
930faa1795aSjb150015 }
931faa1795aSjb150015 
932faa1795aSjb150015 void
933faa1795aSjb150015 smb_post_find_close2(smb_request_t *sr)
934faa1795aSjb150015 {
935faa1795aSjb150015 	DTRACE_SMB_1(op__FindClose2__done, smb_request_t *, sr);
936faa1795aSjb150015 }
937faa1795aSjb150015 
938faa1795aSjb150015 smb_sdrc_t
939dc20a302Sas200622 smb_com_find_close2(smb_request_t *sr)
940da6c28aaSamw {
9417f667e74Sjose borrego 	uint16_t	odid;
9427f667e74Sjose borrego 	smb_odir_t	*od;
9437f667e74Sjose borrego 
9447f667e74Sjose borrego 	if (smbsr_decode_vwv(sr, "w", &odid) != 0)
9457f667e74Sjose borrego 		return (SDRC_ERROR);
9467f667e74Sjose borrego 
9473b13a1efSThomas Keiser 	od = smb_tree_lookup_odir(sr, odid);
9487f667e74Sjose borrego 	if (od == NULL) {
9497f667e74Sjose borrego 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
9507f667e74Sjose borrego 		    ERRDOS, ERROR_INVALID_HANDLE);
951faa1795aSjb150015 		return (SDRC_ERROR);
952da6c28aaSamw 	}
953da6c28aaSamw 
9547f667e74Sjose borrego 	smb_odir_close(od);
9557f667e74Sjose borrego 	smb_odir_release(od);
9567b59d02dSjb150015 
9577b59d02dSjb150015 	if (smbsr_encode_empty_result(sr))
958faa1795aSjb150015 		return (SDRC_ERROR);
9597b59d02dSjb150015 
960faa1795aSjb150015 	return (SDRC_SUCCESS);
961da6c28aaSamw }
962