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) {
236 if (loop_count > 200) {
237 DEBUG(0,("Error: Looping in FIND_NEXT??\n"));
242 setup = TRANSACT2_FINDFIRST;
243 SSVAL(param,0,attribute); /* attribute */
244 SSVAL(param,2,max_matches); /* max count */
245 SSVAL(param,4,(FLAG_TRANS2_FIND_REQUIRE_RESUME|FLAG_TRANS2_FIND_CLOSE_IF_END)); /* resume required + close on end */
246 SSVAL(param,6,info_level);
249 p += clistr_push(cli, param+12, mask, sizeof(param)-12,
252 setup = TRANSACT2_FINDNEXT;
253 SSVAL(param,0,ff_dir_handle);
254 SSVAL(param,2,max_matches); /* max count */
255 SSVAL(param,4,info_level);
256 /* For W2K servers serving out FAT filesystems we *must* set the
257 resume key. If it's not FAT then it's returned as zero. */
258 SIVAL(param,6,resume_key); /* ff_resume_key */
259 /* NB. *DON'T* use continue here. If you do it seems that W2K and bretheren
260 can miss filenames. Use last filename continue instead. JRA */
261 SSVAL(param,10,(FLAG_TRANS2_FIND_REQUIRE_RESUME|FLAG_TRANS2_FIND_CLOSE_IF_END)); /* resume required + close on end */
263 if (last_name_raw.length && (last_name_raw.length < (sizeof(param)-12))) {
264 memcpy(p, last_name_raw.data, last_name_raw.length);
265 p += last_name_raw.length;
267 p += clistr_push(cli, param+12, mask, sizeof(param)-12, STR_TERMINATE);
271 param_len = PTR_DIFF(p, param);
273 if (!cli_send_trans(cli, SMBtrans2,
275 -1, 0, /* fid, flags */
276 &setup, 1, 0, /* setup, length, max */
277 param, param_len, 10, /* param, length, max */
281 MIN(16384,cli->max_xmit) /* data, length, max. */
283 cli->max_xmit /* data, length, max. */
289 if (!cli_receive_trans(cli, SMBtrans2,
291 &rdata, &data_len) &&
292 cli_is_dos_error(cli)) {
293 /* we need to work around a Win95 bug - sometimes
294 it gives ERRSRV/ERRerror temprarily */
301 cli_dos_error(cli, &eclass, &ecode);
302 if (eclass != ERRSRV || ecode != ERRerror)
308 if (cli_is_error(cli) || !rdata || !rparam) {
314 if (total_received == -1)
317 /* parse out some important return info */
320 ff_dir_handle = SVAL(p,0);
321 ff_searchcount = SVAL(p,2);
324 ff_searchcount = SVAL(p,0);
328 if (ff_searchcount == 0) {
334 /* point to the data bytes */
336 rdata_end = rdata + data_len;
338 /* we might need the lastname for continuations */
339 for (p2=p,i=0;i<ff_searchcount && p2 < rdata_end;i++) {
340 if ((info_level == 260) && (i == ff_searchcount-1)) {
341 /* Last entry - fixup the last offset length. */
342 SIVAL(p2,0,PTR_DIFF((rdata + data_len),p2));
344 p2 += interpret_long_filename(cli,
352 if (!First && *mask && strcsequal(finfo.name, mask)) {
353 DEBUG(0,("Error: Looping in FIND_NEXT as name %s has already been seen?\n",
361 if (ff_searchcount > 0) {
362 mask = SMB_STRDUP(finfo.name);
364 mask = SMB_STRDUP("");
372 /* grab the data for later use */
373 /* and add them to the dirlist pool */
374 dirlist = (char *)SMB_REALLOC(dirlist,dirlist_len + data_len);
377 DEBUG(0,("cli_list_new: Failed to expand dirlist\n"));
383 memcpy(dirlist+dirlist_len,p,data_len);
384 dirlist_len += data_len;
386 total_received += ff_searchcount;
391 DEBUG(3,("received %d entries (eos=%d)\n",
392 ff_searchcount,ff_eos));
394 if (ff_searchcount > 0)
400 mnt = cli_cm_get_mntpoint( cli );
402 /* see if the server disconnected or the connection otherwise failed */
403 if (cli_is_error(cli)) {
406 /* no connection problem. let user function add each entry */
407 rdata_end = dirlist + dirlist_len;
408 for (p=dirlist,i=0;i<total_received;i++) {
409 p += interpret_long_filename(cli,
416 fn( mnt,&finfo, Mask, state );
420 /* free up the dirlist buffer and last name raw blob */
422 data_blob_free(&last_name_raw);
424 return(total_received);
427 /****************************************************************************
428 Interpret a short filename structure.
429 The length of the structure is returned.
430 ****************************************************************************/
432 static int interpret_short_filename(struct cli_state *cli, char *p,file_info *finfo)
438 finfo->mode = CVAL(p,21);
440 /* this date is converted to GMT by make_unix_date */
441 finfo->ctime_ts.tv_sec = cli_make_unix_date(cli, p+22);
442 finfo->ctime_ts.tv_nsec = 0;
443 finfo->mtime_ts.tv_sec = finfo->atime_ts.tv_sec = finfo->ctime_ts.tv_sec;
444 finfo->mtime_ts.tv_nsec = finfo->atime_ts.tv_nsec = 0;
445 finfo->size = IVAL(p,26);
446 clistr_pull(cli, finfo->name, p+30, sizeof(finfo->name), 12, STR_ASCII);
447 if (strcmp(finfo->name, "..") && strcmp(finfo->name, ".")) {
448 strncpy(finfo->short_name,finfo->name, sizeof(finfo->short_name)-1);
449 finfo->short_name[sizeof(finfo->short_name)-1] = '\0';
452 return(DIR_STRUCT_SIZE);
456 /****************************************************************************
457 Do a directory listing, calling fn on each file found.
458 this uses the old SMBsearch interface. It is needed for testing Samba,
459 but should otherwise not be used.
460 ****************************************************************************/
462 int cli_list_old(struct cli_state *cli,const char *Mask,uint16 attribute,
463 void (*fn)(const char *, file_info *, const char *, void *), void *state)
469 int num_asked = (cli->max_xmit - 100)/DIR_STRUCT_SIZE;
470 int num_received = 0;
472 char *dirlist = NULL;
477 mask = SMB_STRDUP(Mask);
483 memset(cli->outbuf,'\0',smb_size);
484 memset(cli->inbuf,'\0',smb_size);
486 set_message(cli->outbuf,2,0,True);
488 SCVAL(cli->outbuf,smb_com,SMBsearch);
490 SSVAL(cli->outbuf,smb_tid,cli->cnum);
491 cli_setup_packet(cli);
493 SSVAL(cli->outbuf,smb_vwv0,num_asked);
494 SSVAL(cli->outbuf,smb_vwv1,attribute);
496 p = smb_buf(cli->outbuf);
499 p += clistr_push(cli, p, first?mask:"", -1, STR_TERMINATE);
511 cli_setup_bcc(cli, p);
513 if (!cli_receive_smb(cli)) break;
515 received = SVAL(cli->inbuf,smb_vwv0);
516 if (received <= 0) break;
520 dirlist = (char *)SMB_REALLOC(
521 dirlist,(num_received + received)*DIR_STRUCT_SIZE);
523 DEBUG(0,("cli_list_old: failed to expand dirlist"));
528 p = smb_buf(cli->inbuf) + 3;
530 memcpy(dirlist+num_received*DIR_STRUCT_SIZE,
531 p,received*DIR_STRUCT_SIZE);
533 memcpy(status,p + ((received-1)*DIR_STRUCT_SIZE),21);
535 num_received += received;
537 if (cli_is_error(cli)) break;
541 memset(cli->outbuf,'\0',smb_size);
542 memset(cli->inbuf,'\0',smb_size);
544 set_message(cli->outbuf,2,0,True);
545 SCVAL(cli->outbuf,smb_com,SMBfclose);
546 SSVAL(cli->outbuf,smb_tid,cli->cnum);
547 cli_setup_packet(cli);
549 SSVAL(cli->outbuf, smb_vwv0, 0); /* find count? */
550 SSVAL(cli->outbuf, smb_vwv1, attribute);
552 p = smb_buf(cli->outbuf);
562 cli_setup_bcc(cli, p);
564 if (!cli_receive_smb(cli)) {
565 DEBUG(0,("Error closing search: %s\n",cli_errstr(cli)));
569 for (p=dirlist,i=0;i<num_received;i++) {
571 p += interpret_short_filename(cli, p,&finfo);
572 fn("\\", &finfo, Mask, state);
577 return(num_received);
580 /****************************************************************************
581 Do a directory listing, calling fn on each file found.
582 This auto-switches between old and new style.
583 ****************************************************************************/
585 int cli_list(struct cli_state *cli,const char *Mask,uint16 attribute,
586 void (*fn)(const char *, file_info *, const char *, void *), void *state)
588 if (cli->protocol <= PROTOCOL_LANMAN1)
589 return cli_list_old(cli, Mask, attribute, fn, state);
590 return cli_list_new(cli, Mask, attribute, fn, state);