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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern file_info def_finfo;
25 /****************************************************************************
26 Interpret a long filename structure - this is mostly guesses at the moment.
27 The length of the structure is returned
28 The structure of a long filename depends on the info level. 260 is used
29 by NT and 2 is used by OS/2
30 ****************************************************************************/
32 static size_t interpret_long_filename(struct cli_state *cli, int level,char *p,file_info *finfo,
33 uint32 *p_resume_key, DATA_BLOB *p_last_name_raw, uint32 *p_last_name_raw_len)
46 memcpy(finfo,&def_finfo,sizeof(*finfo));
49 case 1: /* OS/2 understands this */
50 /* these dates are converted to GMT by
52 finfo->ctime = cli_make_unix_date2(cli, p+4);
53 finfo->atime = cli_make_unix_date2(cli, p+8);
54 finfo->mtime = cli_make_unix_date2(cli, p+12);
55 finfo->size = IVAL(p,16);
56 finfo->mode = CVAL(p,24);
59 p += clistr_align_in(cli, p, 0);
60 /* the len+2 below looks strange but it is
61 important to cope with the differences
62 between win2000 and win9x for this call
64 p += clistr_pull(cli, finfo->name, p,
68 return PTR_DIFF(p, base);
70 case 2: /* this is what OS/2 uses mostly */
71 /* these dates are converted to GMT by
73 finfo->ctime = cli_make_unix_date2(cli, p+4);
74 finfo->atime = cli_make_unix_date2(cli, p+8);
75 finfo->mtime = cli_make_unix_date2(cli, p+12);
76 finfo->size = IVAL(p,16);
77 finfo->mode = CVAL(p,24);
80 /* check for unisys! */
81 p += clistr_pull(cli, finfo->name, p,
85 return PTR_DIFF(p, base) + 1;
87 case 260: /* NT uses this, but also accepts 2 */
90 p += 4; /* next entry offset */
93 *p_resume_key = IVAL(p,0);
95 p += 4; /* fileindex */
97 /* these dates appear to arrive in a
98 weird way. It seems to be localtime
99 plus the serverzone given in the
100 initial connect. This is GMT when
101 DST is not in effect and one hour
102 from GMT otherwise. Can this really
105 I suppose this could be called
106 kludge-GMT. Is is the GMT you get
107 by using the current DST setting on
108 a different localtime. It will be
109 cheap to calculate, I suppose, as
110 no DST tables will be needed */
112 finfo->ctime = interpret_long_date(p);
114 finfo->atime = interpret_long_date(p);
116 finfo->mtime = interpret_long_date(p);
119 finfo->size = IVAL2_TO_SMB_BIG_UINT(p,0);
121 p += 8; /* alloc size */
122 finfo->mode = CVAL(p,0);
126 p += 4; /* EA size */
130 /* stupid NT bugs. grr */
132 if (p[1] == 0 && namelen > 1) flags |= STR_UNICODE;
133 clistr_pull(cli, finfo->short_name, p,
134 sizeof(finfo->short_name),
137 p += 24; /* short name? */
138 clistr_pull(cli, finfo->name, p,
142 /* To be robust in the face of unicode conversion failures
143 we need to copy the raw bytes of the last name seen here.
144 Namelen doesn't include the terminating unicode null, so
147 if (p_last_name_raw && p_last_name_raw_len) {
148 if (namelen + 2 > p_last_name_raw->length) {
149 memset(p_last_name_raw->data, '\0', sizeof(p_last_name_raw->length));
150 *p_last_name_raw_len = 0;
152 memcpy(p_last_name_raw->data, p, namelen);
153 SSVAL(p_last_name_raw->data, namelen, 0);
154 *p_last_name_raw_len = namelen + 2;
157 return (size_t)IVAL(base, 0);
161 DEBUG(1,("Unknown long filename format %d\n",level));
162 return (size_t)IVAL(base,0);
165 /****************************************************************************
166 Do a directory listing, calling fn on each file found.
167 ****************************************************************************/
169 int cli_list_new(struct cli_state *cli,const char *Mask,uint16 attribute,
170 void (*fn)(const char *, file_info *, const char *, void *), void *state)
172 int max_matches = 1366;
178 char *tdl, *dirlist = NULL;
180 int total_received = -1;
182 int ff_searchcount=0;
187 char *rparam=NULL, *rdata=NULL;
188 unsigned int param_len, data_len;
192 uint32 resume_key = 0;
193 uint32 last_name_raw_len = 0;
194 DATA_BLOB last_name_raw = data_blob(NULL, 2*sizeof(pstring));
196 /* NT uses 260, OS/2 uses 2. Both accept 1. */
197 info_level = (cli->capabilities&CAP_NT_SMBS)?260:1;
199 /* when getting a directory listing from a 2k dfs root share,
200 we have to include the full path (\server\share\mask) here */
203 pstr_sprintf( mask, "\\%s\\%s\\%s", cli->desthost, cli->share, Mask );
207 while (ff_eos == 0) {
209 if (loop_count > 200) {
210 DEBUG(0,("Error: Looping in FIND_NEXT??\n"));
215 setup = TRANSACT2_FINDFIRST;
216 SSVAL(param,0,attribute); /* attribute */
217 SSVAL(param,2,max_matches); /* max count */
218 SSVAL(param,4,(FLAG_TRANS2_FIND_REQUIRE_RESUME|FLAG_TRANS2_FIND_CLOSE_IF_END)); /* resume required + close on end */
219 SSVAL(param,6,info_level);
222 p += clistr_push(cli, param+12, mask, sizeof(param)-12,
225 setup = TRANSACT2_FINDNEXT;
226 SSVAL(param,0,ff_dir_handle);
227 SSVAL(param,2,max_matches); /* max count */
228 SSVAL(param,4,info_level);
229 /* For W2K servers serving out FAT filesystems we *must* set the
230 resume key. If it's not FAT then it's returned as zero. */
231 SIVAL(param,6,resume_key); /* ff_resume_key */
232 /* NB. *DON'T* use continue here. If you do it seems that W2K and bretheren
233 can miss filenames. Use last filename continue instead. JRA */
234 SSVAL(param,10,(FLAG_TRANS2_FIND_REQUIRE_RESUME|FLAG_TRANS2_FIND_CLOSE_IF_END)); /* resume required + close on end */
236 if (last_name_raw_len && (last_name_raw_len < (sizeof(param)-12))) {
237 memcpy(p, last_name_raw.data, last_name_raw_len);
238 p += last_name_raw_len;
240 p += clistr_push(cli, param+12, mask, sizeof(param)-12, STR_TERMINATE);
244 param_len = PTR_DIFF(p, param);
246 if (!cli_send_trans(cli, SMBtrans2,
248 -1, 0, /* fid, flags */
249 &setup, 1, 0, /* setup, length, max */
250 param, param_len, 10, /* param, length, max */
254 MIN(16384,cli->max_xmit) /* data, length, max. */
256 cli->max_xmit /* data, length, max. */
262 if (!cli_receive_trans(cli, SMBtrans2,
264 &rdata, &data_len) &&
265 cli_is_dos_error(cli)) {
266 /* we need to work around a Win95 bug - sometimes
267 it gives ERRSRV/ERRerror temprarily */
274 cli_dos_error(cli, &eclass, &ecode);
275 if (eclass != ERRSRV || ecode != ERRerror)
281 if (cli_is_error(cli) || !rdata || !rparam) {
287 if (total_received == -1)
290 /* parse out some important return info */
293 ff_dir_handle = SVAL(p,0);
294 ff_searchcount = SVAL(p,2);
296 ff_lastname = SVAL(p,8);
298 ff_searchcount = SVAL(p,0);
300 ff_lastname = SVAL(p,6);
303 if (ff_searchcount == 0) {
309 /* point to the data bytes */
312 /* we might need the lastname for continuations */
313 for (p2=p,i=0;i<ff_searchcount;i++) {
314 if ((info_level == 260) && (i == ff_searchcount-1)) {
315 /* Last entry - fixup the last offset length. */
316 SIVAL(p2,0,PTR_DIFF((rdata + data_len),p2));
318 p2 += interpret_long_filename(cli,info_level,p2,&finfo,
319 &resume_key,&last_name_raw,&last_name_raw_len);
321 if (!First && *mask && strcsequal(finfo.name, mask)) {
322 DEBUG(0,("Error: Looping in FIND_NEXT as name %s has already been seen?\n",
329 if (ff_lastname > 0) {
330 pstrcpy(mask, finfo.name);
335 /* grab the data for later use */
336 /* and add them to the dirlist pool */
337 tdl = SMB_REALLOC(dirlist,dirlist_len + data_len);
340 DEBUG(0,("cli_list_new: Failed to expand dirlist\n"));
348 memcpy(dirlist+dirlist_len,p,data_len);
349 dirlist_len += data_len;
351 total_received += ff_searchcount;
356 DEBUG(3,("received %d entries (eos=%d)\n",
357 ff_searchcount,ff_eos));
359 if (ff_searchcount > 0)
365 mnt = cli_cm_get_mntpoint( cli );
367 for (p=dirlist,i=0;i<total_received;i++) {
368 p += interpret_long_filename(cli,info_level,p,&finfo,NULL,NULL,NULL);
369 fn( mnt,&finfo, Mask, state );
372 /* free up the dirlist buffer and last name raw blob */
374 data_blob_free(&last_name_raw);
375 return(total_received);
378 /****************************************************************************
379 Interpret a short filename structure.
380 The length of the structure is returned.
381 ****************************************************************************/
383 static int interpret_short_filename(struct cli_state *cli, char *p,file_info *finfo)
388 finfo->mode = CVAL(p,21);
390 /* this date is converted to GMT by make_unix_date */
391 finfo->ctime = cli_make_unix_date(cli, p+22);
392 finfo->mtime = finfo->atime = finfo->ctime;
393 finfo->size = IVAL(p,26);
394 clistr_pull(cli, finfo->name, p+30, sizeof(finfo->name), 12, STR_ASCII);
395 if (strcmp(finfo->name, "..") && strcmp(finfo->name, ".")) {
396 strncpy(finfo->short_name,finfo->name, sizeof(finfo->short_name)-1);
397 finfo->short_name[sizeof(finfo->short_name)-1] = '\0';
400 return(DIR_STRUCT_SIZE);
404 /****************************************************************************
405 Do a directory listing, calling fn on each file found.
406 this uses the old SMBsearch interface. It is needed for testing Samba,
407 but should otherwise not be used.
408 ****************************************************************************/
410 int cli_list_old(struct cli_state *cli,const char *Mask,uint16 attribute,
411 void (*fn)(const char *, file_info *, const char *, void *), void *state)
417 int num_asked = (cli->max_xmit - 100)/DIR_STRUCT_SIZE;
418 int num_received = 0;
420 char *tdl, *dirlist = NULL;
428 memset(cli->outbuf,'\0',smb_size);
429 memset(cli->inbuf,'\0',smb_size);
431 set_message(cli->outbuf,2,0,True);
433 SCVAL(cli->outbuf,smb_com,SMBsearch);
435 SSVAL(cli->outbuf,smb_tid,cli->cnum);
436 cli_setup_packet(cli);
438 SSVAL(cli->outbuf,smb_vwv0,num_asked);
439 SSVAL(cli->outbuf,smb_vwv1,attribute);
441 p = smb_buf(cli->outbuf);
444 p += clistr_push(cli, p, first?mask:"", -1, STR_TERMINATE);
456 cli_setup_bcc(cli, p);
458 if (!cli_receive_smb(cli)) break;
460 received = SVAL(cli->inbuf,smb_vwv0);
461 if (received <= 0) break;
465 tdl = SMB_REALLOC(dirlist,(num_received + received)*DIR_STRUCT_SIZE);
468 DEBUG(0,("cli_list_old: failed to expand dirlist"));
474 p = smb_buf(cli->inbuf) + 3;
476 memcpy(dirlist+num_received*DIR_STRUCT_SIZE,
477 p,received*DIR_STRUCT_SIZE);
479 memcpy(status,p + ((received-1)*DIR_STRUCT_SIZE),21);
481 num_received += received;
483 if (cli_is_error(cli)) break;
487 memset(cli->outbuf,'\0',smb_size);
488 memset(cli->inbuf,'\0',smb_size);
490 set_message(cli->outbuf,2,0,True);
491 SCVAL(cli->outbuf,smb_com,SMBfclose);
492 SSVAL(cli->outbuf,smb_tid,cli->cnum);
493 cli_setup_packet(cli);
495 SSVAL(cli->outbuf, smb_vwv0, 0); /* find count? */
496 SSVAL(cli->outbuf, smb_vwv1, attribute);
498 p = smb_buf(cli->outbuf);
508 cli_setup_bcc(cli, p);
510 if (!cli_receive_smb(cli)) {
511 DEBUG(0,("Error closing search: %s\n",cli_errstr(cli)));
515 for (p=dirlist,i=0;i<num_received;i++) {
517 p += interpret_short_filename(cli, p,&finfo);
518 fn("\\", &finfo, Mask, state);
522 return(num_received);
525 /****************************************************************************
526 Do a directory listing, calling fn on each file found.
527 This auto-switches between old and new style.
528 ****************************************************************************/
530 int cli_list(struct cli_state *cli,const char *Mask,uint16 attribute,
531 void (*fn)(const char *, file_info *, const char *, void *), void *state)
533 if (cli->protocol <= PROTOCOL_LANMAN1)
534 return cli_list_old(cli, Mask, attribute, fn, state);
535 return cli_list_new(cli, Mask, attribute, fn, state);