2 Unix SMB/CIFS implementation.
3 client directory list routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 extern file_info def_finfo
;
24 /****************************************************************************
25 Calculate a safe next_entry_offset.
26 ****************************************************************************/
28 static size_t calc_next_entry_offset(const char *base
, const char *pdata_end
)
30 size_t next_entry_offset
= (size_t)IVAL(base
,0);
32 if (next_entry_offset
== 0 ||
33 base
+ next_entry_offset
< base
||
34 base
+ next_entry_offset
> pdata_end
) {
35 next_entry_offset
= pdata_end
- base
;
37 return next_entry_offset
;
40 /****************************************************************************
41 Interpret a long filename structure - this is mostly guesses at the moment.
42 The length of the structure is returned
43 The structure of a long filename depends on the info level. 260 is used
44 by NT and 2 is used by OS/2
45 ****************************************************************************/
47 static size_t interpret_long_filename(struct cli_state
*cli
,
50 const char *pdata_end
,
53 DATA_BLOB
*p_last_name_raw
)
59 data_blob_free(p_last_name_raw
);
68 memcpy(finfo
,&def_finfo
,sizeof(*finfo
));
72 case 1: /* OS/2 understands this */
73 /* these dates are converted to GMT by
75 if (pdata_end
- base
< 27) {
76 return pdata_end
- base
;
78 finfo
->ctime_ts
= convert_time_t_to_timespec(cli_make_unix_date2(cli
, p
+4));
79 finfo
->atime_ts
= convert_time_t_to_timespec(cli_make_unix_date2(cli
, p
+8));
80 finfo
->mtime_ts
= convert_time_t_to_timespec(cli_make_unix_date2(cli
, p
+12));
81 finfo
->size
= IVAL(p
,16);
82 finfo
->mode
= CVAL(p
,24);
85 p
+= clistr_align_in(cli
, p
, 0);
86 if (p
+ len
+ 2 > pdata_end
) {
87 return pdata_end
- base
;
89 /* the len+2 below looks strange but it is
90 important to cope with the differences
91 between win2000 and win9x for this call
93 p
+= clistr_pull(cli
, finfo
->name
, p
,
97 return PTR_DIFF(p
, base
);
99 case 2: /* this is what OS/2 uses mostly */
100 /* these dates are converted to GMT by
102 if (pdata_end
- base
< 31) {
103 return pdata_end
- base
;
105 finfo
->ctime_ts
= convert_time_t_to_timespec(cli_make_unix_date2(cli
, p
+4));
106 finfo
->atime_ts
= convert_time_t_to_timespec(cli_make_unix_date2(cli
, p
+8));
107 finfo
->mtime_ts
= convert_time_t_to_timespec(cli_make_unix_date2(cli
, p
+12));
108 finfo
->size
= IVAL(p
,16);
109 finfo
->mode
= CVAL(p
,24);
112 /* check for unisys! */
113 if (p
+ len
+ 1 > pdata_end
) {
114 return pdata_end
- base
;
116 p
+= clistr_pull(cli
, finfo
->name
, p
,
120 return PTR_DIFF(p
, base
) + 1;
122 case 260: /* NT uses this, but also accepts 2 */
124 size_t namelen
, slen
;
126 if (pdata_end
- base
< 94) {
127 return pdata_end
- base
;
130 p
+= 4; /* next entry offset */
133 *p_resume_key
= IVAL(p
,0);
135 p
+= 4; /* fileindex */
137 /* Offset zero is "create time", not "change time". */
139 finfo
->atime_ts
= interpret_long_date(p
);
141 finfo
->mtime_ts
= interpret_long_date(p
);
143 finfo
->ctime_ts
= interpret_long_date(p
);
145 finfo
->size
= IVAL2_TO_SMB_BIG_UINT(p
,0);
147 p
+= 8; /* alloc size */
148 finfo
->mode
= CVAL(p
,0);
152 p
+= 4; /* EA size */
155 /* Bad short name length. */
156 return pdata_end
- base
;
160 /* stupid NT bugs. grr */
162 if (p
[1] == 0 && namelen
> 1) flags
|= STR_UNICODE
;
163 clistr_pull(cli
, finfo
->short_name
, p
,
164 sizeof(finfo
->short_name
),
167 p
+= 24; /* short name? */
168 if (p
+ namelen
< p
|| p
+ namelen
> pdata_end
) {
169 return pdata_end
- base
;
171 clistr_pull(cli
, finfo
->name
, p
,
175 /* To be robust in the face of unicode conversion failures
176 we need to copy the raw bytes of the last name seen here.
177 Namelen doesn't include the terminating unicode null, so
180 if (p_last_name_raw
) {
181 *p_last_name_raw
= data_blob(NULL
, namelen
+2);
182 memcpy(p_last_name_raw
->data
, p
, namelen
);
183 SSVAL(p_last_name_raw
->data
, namelen
, 0);
185 return calc_next_entry_offset(base
, pdata_end
);
189 DEBUG(1,("Unknown long filename format %d\n",level
));
190 return calc_next_entry_offset(base
, pdata_end
);
193 /****************************************************************************
194 Do a directory listing, calling fn on each file found.
195 ****************************************************************************/
197 int cli_list_new(struct cli_state
*cli
,const char *Mask
,uint16 attribute
,
198 void (*fn
)(const char *, file_info
*, const char *, void *), void *state
)
201 int max_matches
= 1366; /* Match W2k - was 512. */
203 int max_matches
= 512;
206 char *p
, *p2
, *rdata_end
;
210 char *dirlist
= NULL
;
212 int total_received
= -1;
214 int ff_searchcount
=0;
218 char *rparam
=NULL
, *rdata
=NULL
;
219 unsigned int param_len
, data_len
;
223 uint32 resume_key
= 0;
224 DATA_BLOB last_name_raw
= data_blob(NULL
, 0);
226 /* NT uses 260, OS/2 uses 2. Both accept 1. */
227 info_level
= (cli
->capabilities
&CAP_NT_SMBS
)?260:1;
229 mask
= SMB_STRDUP(Mask
);
234 while (ff_eos
== 0) {
235 size_t nlen
= 2*(strlen(mask
)+1);
238 if (loop_count
> 200) {
239 DEBUG(0,("Error: Looping in FIND_NEXT??\n"));
243 param
= SMB_MALLOC(12+nlen
+last_name_raw
.length
+2);
249 setup
= TRANSACT2_FINDFIRST
;
250 SSVAL(param
,0,attribute
); /* attribute */
251 SSVAL(param
,2,max_matches
); /* max count */
252 SSVAL(param
,4,(FLAG_TRANS2_FIND_REQUIRE_RESUME
|FLAG_TRANS2_FIND_CLOSE_IF_END
)); /* resume required + close on end */
253 SSVAL(param
,6,info_level
);
256 p
+= clistr_push(cli
, param
+12, mask
,
257 nlen
, STR_TERMINATE
);
259 setup
= TRANSACT2_FINDNEXT
;
260 SSVAL(param
,0,ff_dir_handle
);
261 SSVAL(param
,2,max_matches
); /* max count */
262 SSVAL(param
,4,info_level
);
263 /* For W2K servers serving out FAT filesystems we *must* set the
264 resume key. If it's not FAT then it's returned as zero. */
265 SIVAL(param
,6,resume_key
); /* ff_resume_key */
266 /* NB. *DON'T* use continue here. If you do it seems that W2K and bretheren
267 can miss filenames. Use last filename continue instead. JRA */
268 SSVAL(param
,10,(FLAG_TRANS2_FIND_REQUIRE_RESUME
|FLAG_TRANS2_FIND_CLOSE_IF_END
)); /* resume required + close on end */
270 if (last_name_raw
.length
) {
271 memcpy(p
, last_name_raw
.data
, last_name_raw
.length
);
272 p
+= last_name_raw
.length
;
274 p
+= clistr_push(cli
, param
+12, mask
,
275 nlen
, STR_TERMINATE
);
279 param_len
= PTR_DIFF(p
, param
);
281 if (!cli_send_trans(cli
, SMBtrans2
,
283 -1, 0, /* fid, flags */
284 &setup
, 1, 0, /* setup, length, max */
285 param
, param_len
, 10, /* param, length, max */
289 MIN(16384,cli
->max_xmit
) /* data, length, max. */
291 cli
->max_xmit
/* data, length, max. */
300 if (!cli_receive_trans(cli
, SMBtrans2
,
302 &rdata
, &data_len
) &&
303 cli_is_dos_error(cli
)) {
304 /* we need to work around a Win95 bug - sometimes
305 it gives ERRSRV/ERRerror temprarily */
312 cli_dos_error(cli
, &eclass
, &ecode
);
313 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
)
319 if (cli_is_error(cli
) || !rdata
|| !rparam
) {
325 if (total_received
== -1)
328 /* parse out some important return info */
331 ff_dir_handle
= SVAL(p
,0);
332 ff_searchcount
= SVAL(p
,2);
335 ff_searchcount
= SVAL(p
,0);
339 if (ff_searchcount
== 0) {
345 /* point to the data bytes */
347 rdata_end
= rdata
+ data_len
;
349 /* we might need the lastname for continuations */
350 for (p2
=p
,i
=0;i
<ff_searchcount
&& p2
< rdata_end
;i
++) {
351 if ((info_level
== 260) && (i
== ff_searchcount
-1)) {
352 /* Last entry - fixup the last offset length. */
353 SIVAL(p2
,0,PTR_DIFF((rdata
+ data_len
),p2
));
355 p2
+= interpret_long_filename(cli
,
363 if (!First
&& *mask
&& strcsequal(finfo
.name
, mask
)) {
364 DEBUG(0,("Error: Looping in FIND_NEXT as name %s has already been seen?\n",
372 if (ff_searchcount
> 0) {
373 mask
= SMB_STRDUP(finfo
.name
);
375 mask
= SMB_STRDUP("");
383 /* grab the data for later use */
384 /* and add them to the dirlist pool */
385 dirlist
= (char *)SMB_REALLOC(dirlist
,dirlist_len
+ data_len
);
388 DEBUG(0,("cli_list_new: Failed to expand dirlist\n"));
394 memcpy(dirlist
+dirlist_len
,p
,data_len
);
395 dirlist_len
+= data_len
;
397 total_received
+= ff_searchcount
;
402 DEBUG(3,("received %d entries (eos=%d)\n",
403 ff_searchcount
,ff_eos
));
405 if (ff_searchcount
> 0)
411 mnt
= cli_cm_get_mntpoint( cli
);
413 /* see if the server disconnected or the connection otherwise failed */
414 if (cli_is_error(cli
)) {
417 /* no connection problem. let user function add each entry */
418 rdata_end
= dirlist
+ dirlist_len
;
419 for (p
=dirlist
,i
=0;i
<total_received
;i
++) {
420 p
+= interpret_long_filename(cli
,
427 fn( mnt
,&finfo
, Mask
, state
);
431 /* free up the dirlist buffer and last name raw blob */
433 data_blob_free(&last_name_raw
);
435 return(total_received
);
438 /****************************************************************************
439 Interpret a short filename structure.
440 The length of the structure is returned.
441 ****************************************************************************/
443 static int interpret_short_filename(struct cli_state
*cli
, char *p
,file_info
*finfo
)
449 finfo
->mode
= CVAL(p
,21);
451 /* this date is converted to GMT by make_unix_date */
452 finfo
->ctime_ts
.tv_sec
= cli_make_unix_date(cli
, p
+22);
453 finfo
->ctime_ts
.tv_nsec
= 0;
454 finfo
->mtime_ts
.tv_sec
= finfo
->atime_ts
.tv_sec
= finfo
->ctime_ts
.tv_sec
;
455 finfo
->mtime_ts
.tv_nsec
= finfo
->atime_ts
.tv_nsec
= 0;
456 finfo
->size
= IVAL(p
,26);
457 clistr_pull(cli
, finfo
->name
, p
+30, sizeof(finfo
->name
), 12, STR_ASCII
);
458 if (strcmp(finfo
->name
, "..") && strcmp(finfo
->name
, ".")) {
459 strncpy(finfo
->short_name
,finfo
->name
, sizeof(finfo
->short_name
)-1);
460 finfo
->short_name
[sizeof(finfo
->short_name
)-1] = '\0';
463 return(DIR_STRUCT_SIZE
);
467 /****************************************************************************
468 Do a directory listing, calling fn on each file found.
469 this uses the old SMBsearch interface. It is needed for testing Samba,
470 but should otherwise not be used.
471 ****************************************************************************/
473 int cli_list_old(struct cli_state
*cli
,const char *Mask
,uint16 attribute
,
474 void (*fn
)(const char *, file_info
*, const char *, void *), void *state
)
480 int num_asked
= (cli
->max_xmit
- 100)/DIR_STRUCT_SIZE
;
481 int num_received
= 0;
483 char *dirlist
= NULL
;
488 mask
= SMB_STRDUP(Mask
);
494 memset(cli
->outbuf
,'\0',smb_size
);
495 memset(cli
->inbuf
,'\0',smb_size
);
497 set_message(cli
->outbuf
,2,0,True
);
499 SCVAL(cli
->outbuf
,smb_com
,SMBsearch
);
501 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
502 cli_setup_packet(cli
);
504 SSVAL(cli
->outbuf
,smb_vwv0
,num_asked
);
505 SSVAL(cli
->outbuf
,smb_vwv1
,attribute
);
507 p
= smb_buf(cli
->outbuf
);
510 p
+= clistr_push(cli
, p
, first
?mask
:"", -1, STR_TERMINATE
);
522 cli_setup_bcc(cli
, p
);
524 if (!cli_receive_smb(cli
)) break;
526 received
= SVAL(cli
->inbuf
,smb_vwv0
);
527 if (received
<= 0) break;
531 dirlist
= (char *)SMB_REALLOC(
532 dirlist
,(num_received
+ received
)*DIR_STRUCT_SIZE
);
534 DEBUG(0,("cli_list_old: failed to expand dirlist"));
539 p
= smb_buf(cli
->inbuf
) + 3;
541 memcpy(dirlist
+num_received
*DIR_STRUCT_SIZE
,
542 p
,received
*DIR_STRUCT_SIZE
);
544 memcpy(status
,p
+ ((received
-1)*DIR_STRUCT_SIZE
),21);
546 num_received
+= received
;
548 if (cli_is_error(cli
)) break;
552 memset(cli
->outbuf
,'\0',smb_size
);
553 memset(cli
->inbuf
,'\0',smb_size
);
555 set_message(cli
->outbuf
,2,0,True
);
556 SCVAL(cli
->outbuf
,smb_com
,SMBfclose
);
557 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
558 cli_setup_packet(cli
);
560 SSVAL(cli
->outbuf
, smb_vwv0
, 0); /* find count? */
561 SSVAL(cli
->outbuf
, smb_vwv1
, attribute
);
563 p
= smb_buf(cli
->outbuf
);
573 cli_setup_bcc(cli
, p
);
575 if (!cli_receive_smb(cli
)) {
576 DEBUG(0,("Error closing search: %s\n",cli_errstr(cli
)));
580 for (p
=dirlist
,i
=0;i
<num_received
;i
++) {
582 p
+= interpret_short_filename(cli
, p
,&finfo
);
583 fn("\\", &finfo
, Mask
, state
);
588 return(num_received
);
591 /****************************************************************************
592 Do a directory listing, calling fn on each file found.
593 This auto-switches between old and new style.
594 ****************************************************************************/
596 int cli_list(struct cli_state
*cli
,const char *Mask
,uint16 attribute
,
597 void (*fn
)(const char *, file_info
*, const char *, void *), void *state
)
599 if (cli
->protocol
<= PROTOCOL_LANMAN1
)
600 return cli_list_old(cli
, Mask
, attribute
, fn
, state
);
601 return cli_list_new(cli
, Mask
, attribute
, fn
, state
);