1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Directory handling routines
|
---|
4 | Copyright (C) Andrew Tridgell 1992-1998
|
---|
5 | Copyright (C) Jeremy Allison 2007
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | /*
|
---|
24 | This module implements directory related functions for Samba.
|
---|
25 | */
|
---|
26 |
|
---|
27 | extern struct current_user current_user;
|
---|
28 |
|
---|
29 | /* "Special" directory offsets. */
|
---|
30 | #define END_OF_DIRECTORY_OFFSET ((long)-1)
|
---|
31 | #define START_OF_DIRECTORY_OFFSET ((long)0)
|
---|
32 | #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
|
---|
33 |
|
---|
34 | /* Make directory handle internals available. */
|
---|
35 |
|
---|
36 | struct name_cache_entry {
|
---|
37 | char *name;
|
---|
38 | long offset;
|
---|
39 | };
|
---|
40 |
|
---|
41 | struct smb_Dir {
|
---|
42 | connection_struct *conn;
|
---|
43 | SMB_STRUCT_DIR *dir;
|
---|
44 | long offset;
|
---|
45 | char *dir_path;
|
---|
46 | size_t name_cache_size;
|
---|
47 | struct name_cache_entry *name_cache;
|
---|
48 | unsigned int name_cache_index;
|
---|
49 | unsigned int file_number;
|
---|
50 | };
|
---|
51 |
|
---|
52 | struct dptr_struct {
|
---|
53 | struct dptr_struct *next, *prev;
|
---|
54 | int dnum;
|
---|
55 | uint16 spid;
|
---|
56 | struct connection_struct *conn;
|
---|
57 | struct smb_Dir *dir_hnd;
|
---|
58 | bool expect_close;
|
---|
59 | char *wcard;
|
---|
60 | uint32 attr;
|
---|
61 | char *path;
|
---|
62 | bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
|
---|
63 | bool did_stat; /* Optimisation for non-wcard searches. */
|
---|
64 | };
|
---|
65 |
|
---|
66 | static struct bitmap *dptr_bmap;
|
---|
67 | static struct dptr_struct *dirptrs;
|
---|
68 | static int dirhandles_open = 0;
|
---|
69 |
|
---|
70 | #define INVALID_DPTR_KEY (-3)
|
---|
71 |
|
---|
72 | /****************************************************************************
|
---|
73 | Make a dir struct.
|
---|
74 | ****************************************************************************/
|
---|
75 |
|
---|
76 | bool make_dir_struct(TALLOC_CTX *ctx,
|
---|
77 | char *buf,
|
---|
78 | const char *mask,
|
---|
79 | const char *fname,
|
---|
80 | SMB_OFF_T size,
|
---|
81 | uint32 mode,
|
---|
82 | time_t date,
|
---|
83 | bool uc)
|
---|
84 | {
|
---|
85 | char *p;
|
---|
86 | char *mask2 = talloc_strdup(ctx, mask);
|
---|
87 |
|
---|
88 | if (!mask2) {
|
---|
89 | return False;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if ((mode & aDIR) != 0) {
|
---|
93 | size = 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | memset(buf+1,' ',11);
|
---|
97 | if ((p = strchr_m(mask2,'.')) != NULL) {
|
---|
98 | *p = 0;
|
---|
99 | push_ascii(buf+1,mask2,8, 0);
|
---|
100 | push_ascii(buf+9,p+1,3, 0);
|
---|
101 | *p = '.';
|
---|
102 | } else {
|
---|
103 | push_ascii(buf+1,mask2,11, 0);
|
---|
104 | }
|
---|
105 |
|
---|
106 | memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
|
---|
107 | SCVAL(buf,21,mode);
|
---|
108 | srv_put_dos_date(buf,22,date);
|
---|
109 | SSVAL(buf,26,size & 0xFFFF);
|
---|
110 | SSVAL(buf,28,(size >> 16)&0xFFFF);
|
---|
111 | /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
|
---|
112 | Strange, but verified on W2K3. Needed for OS/2. JRA. */
|
---|
113 | push_ascii(buf+30,fname,12, uc ? STR_UPPER : 0);
|
---|
114 | DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
|
---|
115 | return True;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /****************************************************************************
|
---|
119 | Initialise the dir bitmap.
|
---|
120 | ****************************************************************************/
|
---|
121 |
|
---|
122 | void init_dptrs(void)
|
---|
123 | {
|
---|
124 | static bool dptrs_init=False;
|
---|
125 |
|
---|
126 | if (dptrs_init)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
|
---|
130 |
|
---|
131 | if (!dptr_bmap)
|
---|
132 | exit_server("out of memory in init_dptrs");
|
---|
133 |
|
---|
134 | dptrs_init = True;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /****************************************************************************
|
---|
138 | Idle a dptr - the directory is closed but the control info is kept.
|
---|
139 | ****************************************************************************/
|
---|
140 |
|
---|
141 | static void dptr_idle(struct dptr_struct *dptr)
|
---|
142 | {
|
---|
143 | if (dptr->dir_hnd) {
|
---|
144 | DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
|
---|
145 | TALLOC_FREE(dptr->dir_hnd);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /****************************************************************************
|
---|
150 | Idle the oldest dptr.
|
---|
151 | ****************************************************************************/
|
---|
152 |
|
---|
153 | static void dptr_idleoldest(void)
|
---|
154 | {
|
---|
155 | struct dptr_struct *dptr;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Go to the end of the list.
|
---|
159 | */
|
---|
160 | for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
|
---|
161 | ;
|
---|
162 |
|
---|
163 | if(!dptr) {
|
---|
164 | DEBUG(0,("No dptrs available to idle ?\n"));
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Idle the oldest pointer.
|
---|
170 | */
|
---|
171 |
|
---|
172 | for(; dptr; dptr = dptr->prev) {
|
---|
173 | if (dptr->dir_hnd) {
|
---|
174 | dptr_idle(dptr);
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /****************************************************************************
|
---|
181 | Get the struct dptr_struct for a dir index.
|
---|
182 | ****************************************************************************/
|
---|
183 |
|
---|
184 | static struct dptr_struct *dptr_get(int key, bool forclose)
|
---|
185 | {
|
---|
186 | struct dptr_struct *dptr;
|
---|
187 |
|
---|
188 | for(dptr = dirptrs; dptr; dptr = dptr->next) {
|
---|
189 | if(dptr->dnum == key) {
|
---|
190 | if (!forclose && !dptr->dir_hnd) {
|
---|
191 | if (dirhandles_open >= MAX_OPEN_DIRECTORIES)
|
---|
192 | dptr_idleoldest();
|
---|
193 | DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
|
---|
194 | if (!(dptr->dir_hnd = OpenDir(
|
---|
195 | NULL, dptr->conn, dptr->path,
|
---|
196 | dptr->wcard, dptr->attr))) {
|
---|
197 | DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
|
---|
198 | strerror(errno)));
|
---|
199 | return False;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | DLIST_PROMOTE(dirptrs,dptr);
|
---|
203 | return dptr;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | return(NULL);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /****************************************************************************
|
---|
210 | Get the dir path for a dir index.
|
---|
211 | ****************************************************************************/
|
---|
212 |
|
---|
213 | char *dptr_path(int key)
|
---|
214 | {
|
---|
215 | struct dptr_struct *dptr = dptr_get(key, False);
|
---|
216 | if (dptr)
|
---|
217 | return(dptr->path);
|
---|
218 | return(NULL);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /****************************************************************************
|
---|
222 | Get the dir wcard for a dir index.
|
---|
223 | ****************************************************************************/
|
---|
224 |
|
---|
225 | char *dptr_wcard(int key)
|
---|
226 | {
|
---|
227 | struct dptr_struct *dptr = dptr_get(key, False);
|
---|
228 | if (dptr)
|
---|
229 | return(dptr->wcard);
|
---|
230 | return(NULL);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /****************************************************************************
|
---|
234 | Get the dir attrib for a dir index.
|
---|
235 | ****************************************************************************/
|
---|
236 |
|
---|
237 | uint16 dptr_attr(int key)
|
---|
238 | {
|
---|
239 | struct dptr_struct *dptr = dptr_get(key, False);
|
---|
240 | if (dptr)
|
---|
241 | return(dptr->attr);
|
---|
242 | return(0);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /****************************************************************************
|
---|
246 | Close a dptr (internal func).
|
---|
247 | ****************************************************************************/
|
---|
248 |
|
---|
249 | static void dptr_close_internal(struct dptr_struct *dptr)
|
---|
250 | {
|
---|
251 | DEBUG(4,("closing dptr key %d\n",dptr->dnum));
|
---|
252 |
|
---|
253 | DLIST_REMOVE(dirptrs, dptr);
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Free the dnum in the bitmap. Remember the dnum value is always
|
---|
257 | * biased by one with respect to the bitmap.
|
---|
258 | */
|
---|
259 |
|
---|
260 | if(bitmap_query( dptr_bmap, dptr->dnum - 1) != True) {
|
---|
261 | DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
|
---|
262 | dptr->dnum ));
|
---|
263 | }
|
---|
264 |
|
---|
265 | bitmap_clear(dptr_bmap, dptr->dnum - 1);
|
---|
266 |
|
---|
267 | TALLOC_FREE(dptr->dir_hnd);
|
---|
268 |
|
---|
269 | /* Lanman 2 specific code */
|
---|
270 | SAFE_FREE(dptr->wcard);
|
---|
271 | string_set(&dptr->path,"");
|
---|
272 | SAFE_FREE(dptr);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /****************************************************************************
|
---|
276 | Close a dptr given a key.
|
---|
277 | ****************************************************************************/
|
---|
278 |
|
---|
279 | void dptr_close(int *key)
|
---|
280 | {
|
---|
281 | struct dptr_struct *dptr;
|
---|
282 |
|
---|
283 | if(*key == INVALID_DPTR_KEY)
|
---|
284 | return;
|
---|
285 |
|
---|
286 | /* OS/2 seems to use -1 to indicate "close all directories" */
|
---|
287 | if (*key == -1) {
|
---|
288 | struct dptr_struct *next;
|
---|
289 | for(dptr = dirptrs; dptr; dptr = next) {
|
---|
290 | next = dptr->next;
|
---|
291 | dptr_close_internal(dptr);
|
---|
292 | }
|
---|
293 | *key = INVALID_DPTR_KEY;
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | dptr = dptr_get(*key, True);
|
---|
298 |
|
---|
299 | if (!dptr) {
|
---|
300 | DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
|
---|
301 | return;
|
---|
302 | }
|
---|
303 |
|
---|
304 | dptr_close_internal(dptr);
|
---|
305 |
|
---|
306 | *key = INVALID_DPTR_KEY;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /****************************************************************************
|
---|
310 | Close all dptrs for a cnum.
|
---|
311 | ****************************************************************************/
|
---|
312 |
|
---|
313 | void dptr_closecnum(connection_struct *conn)
|
---|
314 | {
|
---|
315 | struct dptr_struct *dptr, *next;
|
---|
316 | for(dptr = dirptrs; dptr; dptr = next) {
|
---|
317 | next = dptr->next;
|
---|
318 | if (dptr->conn == conn)
|
---|
319 | dptr_close_internal(dptr);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | /****************************************************************************
|
---|
324 | Idle all dptrs for a cnum.
|
---|
325 | ****************************************************************************/
|
---|
326 |
|
---|
327 | void dptr_idlecnum(connection_struct *conn)
|
---|
328 | {
|
---|
329 | struct dptr_struct *dptr;
|
---|
330 | for(dptr = dirptrs; dptr; dptr = dptr->next) {
|
---|
331 | if (dptr->conn == conn && dptr->dir_hnd)
|
---|
332 | dptr_idle(dptr);
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | /****************************************************************************
|
---|
337 | Close a dptr that matches a given path, only if it matches the spid also.
|
---|
338 | ****************************************************************************/
|
---|
339 |
|
---|
340 | void dptr_closepath(char *path,uint16 spid)
|
---|
341 | {
|
---|
342 | struct dptr_struct *dptr, *next;
|
---|
343 | for(dptr = dirptrs; dptr; dptr = next) {
|
---|
344 | next = dptr->next;
|
---|
345 | if (spid == dptr->spid && strequal(dptr->path,path))
|
---|
346 | dptr_close_internal(dptr);
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /****************************************************************************
|
---|
351 | Try and close the oldest handle not marked for
|
---|
352 | expect close in the hope that the client has
|
---|
353 | finished with that one.
|
---|
354 | ****************************************************************************/
|
---|
355 |
|
---|
356 | static void dptr_close_oldest(bool old)
|
---|
357 | {
|
---|
358 | struct dptr_struct *dptr;
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Go to the end of the list.
|
---|
362 | */
|
---|
363 | for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
|
---|
364 | ;
|
---|
365 |
|
---|
366 | if(!dptr) {
|
---|
367 | DEBUG(0,("No old dptrs available to close oldest ?\n"));
|
---|
368 | return;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
|
---|
373 | * does not have expect_close set. If 'old' is false, close
|
---|
374 | * one of the new dnum handles.
|
---|
375 | */
|
---|
376 |
|
---|
377 | for(; dptr; dptr = dptr->prev) {
|
---|
378 | if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
|
---|
379 | (!old && (dptr->dnum > 255))) {
|
---|
380 | dptr_close_internal(dptr);
|
---|
381 | return;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | /****************************************************************************
|
---|
387 | Create a new dir ptr. If the flag old_handle is true then we must allocate
|
---|
388 | from the bitmap range 0 - 255 as old SMBsearch directory handles are only
|
---|
389 | one byte long. If old_handle is false we allocate from the range
|
---|
390 | 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
|
---|
391 | a directory handle is never zero.
|
---|
392 | wcard must not be zero.
|
---|
393 | ****************************************************************************/
|
---|
394 |
|
---|
395 | NTSTATUS dptr_create(connection_struct *conn, const char *path, bool old_handle, bool expect_close,uint16 spid,
|
---|
396 | const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
|
---|
397 | {
|
---|
398 | struct dptr_struct *dptr = NULL;
|
---|
399 | struct smb_Dir *dir_hnd;
|
---|
400 | NTSTATUS status;
|
---|
401 |
|
---|
402 | DEBUG(5,("dptr_create dir=%s\n", path));
|
---|
403 |
|
---|
404 | if (!wcard) {
|
---|
405 | return NT_STATUS_INVALID_PARAMETER;
|
---|
406 | }
|
---|
407 |
|
---|
408 | status = check_name(conn,path);
|
---|
409 | if (!NT_STATUS_IS_OK(status)) {
|
---|
410 | return status;
|
---|
411 | }
|
---|
412 |
|
---|
413 | dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
|
---|
414 | if (!dir_hnd) {
|
---|
415 | return map_nt_error_from_unix(errno);
|
---|
416 | }
|
---|
417 |
|
---|
418 | string_set(&conn->dirpath,path);
|
---|
419 |
|
---|
420 | if (dirhandles_open >= MAX_OPEN_DIRECTORIES) {
|
---|
421 | dptr_idleoldest();
|
---|
422 | }
|
---|
423 |
|
---|
424 | dptr = SMB_MALLOC_P(struct dptr_struct);
|
---|
425 | if(!dptr) {
|
---|
426 | DEBUG(0,("malloc fail in dptr_create.\n"));
|
---|
427 | TALLOC_FREE(dir_hnd);
|
---|
428 | return NT_STATUS_NO_MEMORY;
|
---|
429 | }
|
---|
430 |
|
---|
431 | ZERO_STRUCTP(dptr);
|
---|
432 |
|
---|
433 | if(old_handle) {
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * This is an old-style SMBsearch request. Ensure the
|
---|
437 | * value we return will fit in the range 1-255.
|
---|
438 | */
|
---|
439 |
|
---|
440 | dptr->dnum = bitmap_find(dptr_bmap, 0);
|
---|
441 |
|
---|
442 | if(dptr->dnum == -1 || dptr->dnum > 254) {
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * Try and close the oldest handle not marked for
|
---|
446 | * expect close in the hope that the client has
|
---|
447 | * finished with that one.
|
---|
448 | */
|
---|
449 |
|
---|
450 | dptr_close_oldest(True);
|
---|
451 |
|
---|
452 | /* Now try again... */
|
---|
453 | dptr->dnum = bitmap_find(dptr_bmap, 0);
|
---|
454 | if(dptr->dnum == -1 || dptr->dnum > 254) {
|
---|
455 | DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
|
---|
456 | SAFE_FREE(dptr);
|
---|
457 | TALLOC_FREE(dir_hnd);
|
---|
458 | return NT_STATUS_TOO_MANY_OPENED_FILES;
|
---|
459 | }
|
---|
460 | }
|
---|
461 | } else {
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * This is a new-style trans2 request. Allocate from
|
---|
465 | * a range that will return 256 - MAX_DIRECTORY_HANDLES.
|
---|
466 | */
|
---|
467 |
|
---|
468 | dptr->dnum = bitmap_find(dptr_bmap, 255);
|
---|
469 |
|
---|
470 | if(dptr->dnum == -1 || dptr->dnum < 255) {
|
---|
471 |
|
---|
472 | /*
|
---|
473 | * Try and close the oldest handle close in the hope that
|
---|
474 | * the client has finished with that one. This will only
|
---|
475 | * happen in the case of the Win98 client bug where it leaks
|
---|
476 | * directory handles.
|
---|
477 | */
|
---|
478 |
|
---|
479 | dptr_close_oldest(False);
|
---|
480 |
|
---|
481 | /* Now try again... */
|
---|
482 | dptr->dnum = bitmap_find(dptr_bmap, 255);
|
---|
483 |
|
---|
484 | if(dptr->dnum == -1 || dptr->dnum < 255) {
|
---|
485 | DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
|
---|
486 | SAFE_FREE(dptr);
|
---|
487 | TALLOC_FREE(dir_hnd);
|
---|
488 | return NT_STATUS_TOO_MANY_OPENED_FILES;
|
---|
489 | }
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | bitmap_set(dptr_bmap, dptr->dnum);
|
---|
494 |
|
---|
495 | dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
|
---|
496 |
|
---|
497 | string_set(&dptr->path,path);
|
---|
498 | dptr->conn = conn;
|
---|
499 | dptr->dir_hnd = dir_hnd;
|
---|
500 | dptr->spid = spid;
|
---|
501 | dptr->expect_close = expect_close;
|
---|
502 | dptr->wcard = SMB_STRDUP(wcard);
|
---|
503 | if (!dptr->wcard) {
|
---|
504 | bitmap_clear(dptr_bmap, dptr->dnum - 1);
|
---|
505 | SAFE_FREE(dptr);
|
---|
506 | TALLOC_FREE(dir_hnd);
|
---|
507 | return NT_STATUS_NO_MEMORY;
|
---|
508 | }
|
---|
509 | if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
|
---|
510 | dptr->has_wild = True;
|
---|
511 | } else {
|
---|
512 | dptr->has_wild = wcard_has_wild;
|
---|
513 | }
|
---|
514 |
|
---|
515 | dptr->attr = attr;
|
---|
516 |
|
---|
517 | DLIST_ADD(dirptrs, dptr);
|
---|
518 |
|
---|
519 | DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
|
---|
520 | dptr->dnum,path,expect_close));
|
---|
521 |
|
---|
522 | *dptr_ret = dptr;
|
---|
523 |
|
---|
524 | return NT_STATUS_OK;
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | /****************************************************************************
|
---|
529 | Wrapper functions to access the lower level directory handles.
|
---|
530 | ****************************************************************************/
|
---|
531 |
|
---|
532 | int dptr_CloseDir(struct dptr_struct *dptr)
|
---|
533 | {
|
---|
534 | DLIST_REMOVE(dirptrs, dptr);
|
---|
535 | TALLOC_FREE(dptr->dir_hnd);
|
---|
536 | return 0;
|
---|
537 | }
|
---|
538 |
|
---|
539 | void dptr_SeekDir(struct dptr_struct *dptr, long offset)
|
---|
540 | {
|
---|
541 | SeekDir(dptr->dir_hnd, offset);
|
---|
542 | }
|
---|
543 |
|
---|
544 | long dptr_TellDir(struct dptr_struct *dptr)
|
---|
545 | {
|
---|
546 | return TellDir(dptr->dir_hnd);
|
---|
547 | }
|
---|
548 |
|
---|
549 | bool dptr_has_wild(struct dptr_struct *dptr)
|
---|
550 | {
|
---|
551 | return dptr->has_wild;
|
---|
552 | }
|
---|
553 |
|
---|
554 | int dptr_dnum(struct dptr_struct *dptr)
|
---|
555 | {
|
---|
556 | return dptr->dnum;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /****************************************************************************
|
---|
560 | Return the next visible file name, skipping veto'd and invisible files.
|
---|
561 | ****************************************************************************/
|
---|
562 |
|
---|
563 | static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr, long *poffset, SMB_STRUCT_STAT *pst)
|
---|
564 | {
|
---|
565 | /* Normal search for the next file. */
|
---|
566 | const char *name;
|
---|
567 | while ((name = ReadDirName(dptr->dir_hnd, poffset)) != NULL) {
|
---|
568 | if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
|
---|
569 | return name;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | return NULL;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /****************************************************************************
|
---|
576 | Return the next visible file name, skipping veto'd and invisible files.
|
---|
577 | ****************************************************************************/
|
---|
578 |
|
---|
579 | const char *dptr_ReadDirName(TALLOC_CTX *ctx,
|
---|
580 | struct dptr_struct *dptr,
|
---|
581 | long *poffset,
|
---|
582 | SMB_STRUCT_STAT *pst)
|
---|
583 | {
|
---|
584 | SET_STAT_INVALID(*pst);
|
---|
585 |
|
---|
586 | if (dptr->has_wild) {
|
---|
587 | return dptr_normal_ReadDirName(dptr, poffset, pst);
|
---|
588 | }
|
---|
589 |
|
---|
590 | /* If poffset is -1 then we know we returned this name before and we have
|
---|
591 | no wildcards. We're at the end of the directory. */
|
---|
592 | if (*poffset == END_OF_DIRECTORY_OFFSET) {
|
---|
593 | return NULL;
|
---|
594 | }
|
---|
595 |
|
---|
596 | if (!dptr->did_stat) {
|
---|
597 | char *pathreal = NULL;
|
---|
598 |
|
---|
599 | /* We know the stored wcard contains no wildcard characters. See if we can match
|
---|
600 | with a stat call. If we can't, then set did_stat to true to
|
---|
601 | ensure we only do this once and keep searching. */
|
---|
602 |
|
---|
603 | dptr->did_stat = True;
|
---|
604 |
|
---|
605 | /* First check if it should be visible. */
|
---|
606 | if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard, pst, True)) {
|
---|
607 | /* This only returns False if the file was found, but
|
---|
608 | is explicitly not visible. Set us to end of directory,
|
---|
609 | but return NULL as we know we can't ever find it. */
|
---|
610 | dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
|
---|
611 | return NULL;
|
---|
612 | }
|
---|
613 |
|
---|
614 | if (VALID_STAT(*pst)) {
|
---|
615 | /* We need to set the underlying dir_hnd offset to -1 also as
|
---|
616 | this function is usually called with the output from TellDir. */
|
---|
617 | dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
|
---|
618 | return dptr->wcard;
|
---|
619 | }
|
---|
620 |
|
---|
621 | pathreal = talloc_asprintf(ctx,
|
---|
622 | "%s/%s",
|
---|
623 | dptr->path,
|
---|
624 | dptr->wcard);
|
---|
625 | if (!pathreal) {
|
---|
626 | return NULL;
|
---|
627 | }
|
---|
628 |
|
---|
629 | if (SMB_VFS_STAT(dptr->conn,pathreal,pst) == 0) {
|
---|
630 | /* We need to set the underlying dir_hnd offset to -1 also as
|
---|
631 | this function is usually called with the output from TellDir. */
|
---|
632 | dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
|
---|
633 | TALLOC_FREE(pathreal);
|
---|
634 | return dptr->wcard;
|
---|
635 | } else {
|
---|
636 | /* If we get any other error than ENOENT or ENOTDIR
|
---|
637 | then the file exists we just can't stat it. */
|
---|
638 | if (errno == ENOTDIR)
|
---|
639 | DEBUG( 0, ( "PS - ENOTDIR2\n" ) );
|
---|
640 |
|
---|
641 | if (errno != ENOENT && errno != ENOTDIR) {
|
---|
642 | /* We need to set the underlying dir_hdn offset to -1 also as
|
---|
643 | this function is usually called with the output from TellDir. */
|
---|
644 | dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
|
---|
645 | TALLOC_FREE(pathreal);
|
---|
646 | return dptr->wcard;
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | TALLOC_FREE(pathreal);
|
---|
651 |
|
---|
652 | /* Stat failed. We know this is authoratiative if we are
|
---|
653 | * providing case sensitive semantics or the underlying
|
---|
654 | * filesystem is case sensitive.
|
---|
655 | */
|
---|
656 |
|
---|
657 | if (dptr->conn->case_sensitive ||
|
---|
658 | !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
|
---|
659 | /* We need to set the underlying dir_hnd offset to -1 also as
|
---|
660 | this function is usually called with the output from TellDir. */
|
---|
661 | dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
|
---|
662 | return NULL;
|
---|
663 | }
|
---|
664 | }
|
---|
665 | return dptr_normal_ReadDirName(dptr, poffset, pst);
|
---|
666 | }
|
---|
667 |
|
---|
668 | /****************************************************************************
|
---|
669 | Search for a file by name, skipping veto'ed and not visible files.
|
---|
670 | ****************************************************************************/
|
---|
671 |
|
---|
672 | bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
|
---|
673 | {
|
---|
674 | SET_STAT_INVALID(*pst);
|
---|
675 |
|
---|
676 | if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
|
---|
677 | /* This is a singleton directory and we're already at the end. */
|
---|
678 | *poffset = END_OF_DIRECTORY_OFFSET;
|
---|
679 | return False;
|
---|
680 | }
|
---|
681 |
|
---|
682 | return SearchDir(dptr->dir_hnd, name, poffset);
|
---|
683 | }
|
---|
684 |
|
---|
685 | /****************************************************************************
|
---|
686 | Add the name we're returning into the underlying cache.
|
---|
687 | ****************************************************************************/
|
---|
688 |
|
---|
689 | void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
|
---|
690 | {
|
---|
691 | DirCacheAdd(dptr->dir_hnd, name, offset);
|
---|
692 | }
|
---|
693 |
|
---|
694 | /****************************************************************************
|
---|
695 | Fill the 5 byte server reserved dptr field.
|
---|
696 | ****************************************************************************/
|
---|
697 |
|
---|
698 | bool dptr_fill(char *buf1,unsigned int key)
|
---|
699 | {
|
---|
700 | unsigned char *buf = (unsigned char *)buf1;
|
---|
701 | struct dptr_struct *dptr = dptr_get(key, False);
|
---|
702 | uint32 offset;
|
---|
703 | if (!dptr) {
|
---|
704 | DEBUG(1,("filling null dirptr %d\n",key));
|
---|
705 | return(False);
|
---|
706 | }
|
---|
707 | offset = (uint32)TellDir(dptr->dir_hnd);
|
---|
708 | DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
|
---|
709 | (long)dptr->dir_hnd,(int)offset));
|
---|
710 | buf[0] = key;
|
---|
711 | SIVAL(buf,1,offset);
|
---|
712 | return(True);
|
---|
713 | }
|
---|
714 |
|
---|
715 | /****************************************************************************
|
---|
716 | Fetch the dir ptr and seek it given the 5 byte server field.
|
---|
717 | ****************************************************************************/
|
---|
718 |
|
---|
719 | struct dptr_struct *dptr_fetch(char *buf,int *num)
|
---|
720 | {
|
---|
721 | unsigned int key = *(unsigned char *)buf;
|
---|
722 | struct dptr_struct *dptr = dptr_get(key, False);
|
---|
723 | uint32 offset;
|
---|
724 | long seekoff;
|
---|
725 |
|
---|
726 | if (!dptr) {
|
---|
727 | DEBUG(3,("fetched null dirptr %d\n",key));
|
---|
728 | return(NULL);
|
---|
729 | }
|
---|
730 | *num = key;
|
---|
731 | offset = IVAL(buf,1);
|
---|
732 | if (offset == (uint32)-1) {
|
---|
733 | seekoff = END_OF_DIRECTORY_OFFSET;
|
---|
734 | } else {
|
---|
735 | seekoff = (long)offset;
|
---|
736 | }
|
---|
737 | SeekDir(dptr->dir_hnd,seekoff);
|
---|
738 | DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
|
---|
739 | key,dptr_path(key),(int)seekoff));
|
---|
740 | return(dptr);
|
---|
741 | }
|
---|
742 |
|
---|
743 | /****************************************************************************
|
---|
744 | Fetch the dir ptr.
|
---|
745 | ****************************************************************************/
|
---|
746 |
|
---|
747 | struct dptr_struct *dptr_fetch_lanman2(int dptr_num)
|
---|
748 | {
|
---|
749 | struct dptr_struct *dptr = dptr_get(dptr_num, False);
|
---|
750 |
|
---|
751 | if (!dptr) {
|
---|
752 | DEBUG(3,("fetched null dirptr %d\n",dptr_num));
|
---|
753 | return(NULL);
|
---|
754 | }
|
---|
755 | DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
|
---|
756 | return(dptr);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /****************************************************************************
|
---|
760 | Check that a file matches a particular file type.
|
---|
761 | ****************************************************************************/
|
---|
762 |
|
---|
763 | bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
|
---|
764 | {
|
---|
765 | uint32 mask;
|
---|
766 |
|
---|
767 | /* Check the "may have" search bits. */
|
---|
768 | if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
|
---|
769 | return False;
|
---|
770 |
|
---|
771 | /* Check the "must have" bits, which are the may have bits shifted eight */
|
---|
772 | /* If must have bit is set, the file/dir can not be returned in search unless the matching
|
---|
773 | file attribute is set */
|
---|
774 | mask = ((dirtype >> 8) & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM)); /* & 0x37 */
|
---|
775 | if(mask) {
|
---|
776 | if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask) /* check if matching attribute present */
|
---|
777 | return True;
|
---|
778 | else
|
---|
779 | return False;
|
---|
780 | }
|
---|
781 |
|
---|
782 | return True;
|
---|
783 | }
|
---|
784 |
|
---|
785 | static bool mangle_mask_match(connection_struct *conn,
|
---|
786 | const char *filename,
|
---|
787 | const char *mask)
|
---|
788 | {
|
---|
789 | char mname[13];
|
---|
790 |
|
---|
791 | if (!name_to_8_3(filename,mname,False,conn->params)) {
|
---|
792 | return False;
|
---|
793 | }
|
---|
794 | return mask_match_search(mname,mask,False);
|
---|
795 | }
|
---|
796 |
|
---|
797 | /****************************************************************************
|
---|
798 | Get an 8.3 directory entry.
|
---|
799 | ****************************************************************************/
|
---|
800 |
|
---|
801 | bool get_dir_entry(TALLOC_CTX *ctx,
|
---|
802 | connection_struct *conn,
|
---|
803 | const char *mask,
|
---|
804 | uint32 dirtype,
|
---|
805 | char **pp_fname_out,
|
---|
806 | SMB_OFF_T *size,
|
---|
807 | uint32 *mode,
|
---|
808 | time_t *date,
|
---|
809 | bool check_descend,
|
---|
810 | bool ask_sharemode)
|
---|
811 | {
|
---|
812 | const char *dname = NULL;
|
---|
813 | bool found = False;
|
---|
814 | SMB_STRUCT_STAT sbuf;
|
---|
815 | char *pathreal = NULL;
|
---|
816 | const char *filename = NULL;
|
---|
817 | bool needslash;
|
---|
818 |
|
---|
819 | *pp_fname_out = NULL;
|
---|
820 |
|
---|
821 | needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
|
---|
822 |
|
---|
823 | if (!conn->dirptr) {
|
---|
824 | return(False);
|
---|
825 | }
|
---|
826 |
|
---|
827 | while (!found) {
|
---|
828 | long curoff = dptr_TellDir(conn->dirptr);
|
---|
829 | dname = dptr_ReadDirName(ctx, conn->dirptr, &curoff, &sbuf);
|
---|
830 |
|
---|
831 | DEBUG(6,("readdir on dirptr 0x%lx now at offset %ld\n",
|
---|
832 | (long)conn->dirptr,TellDir(conn->dirptr->dir_hnd)));
|
---|
833 |
|
---|
834 | if (dname == NULL) {
|
---|
835 | return(False);
|
---|
836 | }
|
---|
837 |
|
---|
838 | filename = dname;
|
---|
839 |
|
---|
840 | /* notice the special *.* handling. This appears to be the only difference
|
---|
841 | between the wildcard handling in this routine and in the trans2 routines.
|
---|
842 | see masktest for a demo
|
---|
843 | */
|
---|
844 | if ((strcmp(mask,"*.*") == 0) ||
|
---|
845 | mask_match_search(filename,mask,False) ||
|
---|
846 | mangle_mask_match(conn,filename,mask)) {
|
---|
847 | char mname[13];
|
---|
848 |
|
---|
849 | if (!mangle_is_8_3(filename, False, conn->params)) {
|
---|
850 | if (!name_to_8_3(filename,mname,False,
|
---|
851 | conn->params)) {
|
---|
852 | continue;
|
---|
853 | }
|
---|
854 | filename = mname;
|
---|
855 | }
|
---|
856 |
|
---|
857 | if (needslash) {
|
---|
858 | pathreal = talloc_asprintf(ctx,
|
---|
859 | "%s/%s",
|
---|
860 | conn->dirpath,
|
---|
861 | dname);
|
---|
862 | } else {
|
---|
863 | pathreal = talloc_asprintf(ctx,
|
---|
864 | "%s%s",
|
---|
865 | conn->dirpath,
|
---|
866 | dname);
|
---|
867 | }
|
---|
868 | if (!pathreal) {
|
---|
869 | return False;
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn, pathreal, &sbuf)) != 0) {
|
---|
873 | DEBUG(5,("Couldn't stat 1 [%s]. Error = %s\n",
|
---|
874 | pathreal, strerror(errno) ));
|
---|
875 | TALLOC_FREE(pathreal);
|
---|
876 | continue;
|
---|
877 | }
|
---|
878 |
|
---|
879 | *mode = dos_mode(conn,pathreal,&sbuf);
|
---|
880 |
|
---|
881 | if (!dir_check_ftype(conn,*mode,dirtype)) {
|
---|
882 | DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",filename,(unsigned int)*mode,(unsigned int)dirtype));
|
---|
883 | TALLOC_FREE(pathreal);
|
---|
884 | continue;
|
---|
885 | }
|
---|
886 |
|
---|
887 | *size = sbuf.st_size;
|
---|
888 | *date = sbuf.st_mtime;
|
---|
889 |
|
---|
890 | if (ask_sharemode) {
|
---|
891 | struct timespec write_time_ts;
|
---|
892 | struct file_id fileid;
|
---|
893 |
|
---|
894 | fileid = vfs_file_id_from_sbuf(conn, &sbuf);
|
---|
895 | get_file_infos(fileid, NULL, &write_time_ts);
|
---|
896 | if (!null_timespec(write_time_ts)) {
|
---|
897 | *date = convert_timespec_to_time_t(write_time_ts);
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | DEBUG(3,("get_dir_entry mask=[%s] found %s "
|
---|
902 | "fname=%s (%s)\n",
|
---|
903 | mask,
|
---|
904 | pathreal,
|
---|
905 | dname,
|
---|
906 | filename));
|
---|
907 |
|
---|
908 | found = True;
|
---|
909 |
|
---|
910 | *pp_fname_out = talloc_strdup(ctx, filename);
|
---|
911 | if (!*pp_fname_out) {
|
---|
912 | return False;
|
---|
913 | }
|
---|
914 |
|
---|
915 | DirCacheAdd(conn->dirptr->dir_hnd, dname, curoff);
|
---|
916 | TALLOC_FREE(pathreal);
|
---|
917 | }
|
---|
918 | }
|
---|
919 |
|
---|
920 | return(found);
|
---|
921 | }
|
---|
922 |
|
---|
923 | /*******************************************************************
|
---|
924 | Check to see if a user can read a file. This is only approximate,
|
---|
925 | it is used as part of the "hide unreadable" option. Don't
|
---|
926 | use it for anything security sensitive.
|
---|
927 | ********************************************************************/
|
---|
928 |
|
---|
929 | static bool user_can_read_file(connection_struct *conn, char *name, SMB_STRUCT_STAT *pst)
|
---|
930 | {
|
---|
931 | SEC_DESC *psd = NULL;
|
---|
932 | files_struct *fsp;
|
---|
933 | NTSTATUS status;
|
---|
934 | uint32 access_granted;
|
---|
935 |
|
---|
936 | /*
|
---|
937 | * If user is a member of the Admin group
|
---|
938 | * we never hide files from them.
|
---|
939 | */
|
---|
940 |
|
---|
941 | if (conn->admin_user) {
|
---|
942 | return True;
|
---|
943 | }
|
---|
944 |
|
---|
945 | SMB_ASSERT(VALID_STAT(*pst));
|
---|
946 |
|
---|
947 | /* Pseudo-open the file (note - no fd's created). */
|
---|
948 |
|
---|
949 | if(S_ISDIR(pst->st_mode)) {
|
---|
950 | status = open_directory(conn, NULL, name, pst,
|
---|
951 | READ_CONTROL_ACCESS,
|
---|
952 | FILE_SHARE_READ|FILE_SHARE_WRITE,
|
---|
953 | FILE_OPEN,
|
---|
954 | 0, /* no create options. */
|
---|
955 | FILE_ATTRIBUTE_DIRECTORY,
|
---|
956 | NULL, &fsp);
|
---|
957 | } else {
|
---|
958 | status = open_file_stat(conn, NULL, name, pst, &fsp);
|
---|
959 | }
|
---|
960 |
|
---|
961 | if (!NT_STATUS_IS_OK(status)) {
|
---|
962 | return False;
|
---|
963 | }
|
---|
964 |
|
---|
965 | /* Get NT ACL -allocated in main loop talloc context. No free needed here. */
|
---|
966 | status = SMB_VFS_FGET_NT_ACL(fsp,
|
---|
967 | (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION), &psd);
|
---|
968 | close_file(fsp, NORMAL_CLOSE);
|
---|
969 |
|
---|
970 | /* No access if SD get failed. */
|
---|
971 | if (!NT_STATUS_IS_OK(status)) {
|
---|
972 | return False;
|
---|
973 | }
|
---|
974 |
|
---|
975 | return se_access_check(psd, current_user.nt_user_token, FILE_READ_DATA,
|
---|
976 | &access_granted, &status);
|
---|
977 | }
|
---|
978 |
|
---|
979 | /*******************************************************************
|
---|
980 | Check to see if a user can write a file (and only files, we do not
|
---|
981 | check dirs on this one). This is only approximate,
|
---|
982 | it is used as part of the "hide unwriteable" option. Don't
|
---|
983 | use it for anything security sensitive.
|
---|
984 | ********************************************************************/
|
---|
985 |
|
---|
986 | static bool user_can_write_file(connection_struct *conn, char *name, SMB_STRUCT_STAT *pst)
|
---|
987 | {
|
---|
988 | SEC_DESC *psd = NULL;
|
---|
989 | files_struct *fsp;
|
---|
990 | int info;
|
---|
991 | NTSTATUS status;
|
---|
992 | uint32 access_granted;
|
---|
993 |
|
---|
994 | /*
|
---|
995 | * If user is a member of the Admin group
|
---|
996 | * we never hide files from them.
|
---|
997 | */
|
---|
998 |
|
---|
999 | if (conn->admin_user) {
|
---|
1000 | return True;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | SMB_ASSERT(VALID_STAT(*pst));
|
---|
1004 |
|
---|
1005 | /* Pseudo-open the file */
|
---|
1006 |
|
---|
1007 | if(S_ISDIR(pst->st_mode)) {
|
---|
1008 | return True;
|
---|
1009 | } else {
|
---|
1010 | status = open_file_ntcreate(conn, NULL, name, pst,
|
---|
1011 | FILE_WRITE_ATTRIBUTES,
|
---|
1012 | FILE_SHARE_READ|FILE_SHARE_WRITE,
|
---|
1013 | FILE_OPEN,
|
---|
1014 | 0,
|
---|
1015 | FILE_ATTRIBUTE_NORMAL,
|
---|
1016 | INTERNAL_OPEN_ONLY,
|
---|
1017 | &info, &fsp);
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1021 | return False;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /* Get NT ACL -allocated in main loop talloc context. No free needed here. */
|
---|
1025 | status = SMB_VFS_FGET_NT_ACL(fsp,
|
---|
1026 | (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION), &psd);
|
---|
1027 | close_file(fsp, NORMAL_CLOSE);
|
---|
1028 |
|
---|
1029 | /* No access if SD get failed. */
|
---|
1030 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1031 | return False;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | return se_access_check(psd, current_user.nt_user_token, FILE_WRITE_DATA,
|
---|
1035 | &access_granted, &status);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | /*******************************************************************
|
---|
1039 | Is a file a "special" type ?
|
---|
1040 | ********************************************************************/
|
---|
1041 |
|
---|
1042 | static bool file_is_special(connection_struct *conn, char *name, SMB_STRUCT_STAT *pst)
|
---|
1043 | {
|
---|
1044 | /*
|
---|
1045 | * If user is a member of the Admin group
|
---|
1046 | * we never hide files from them.
|
---|
1047 | */
|
---|
1048 |
|
---|
1049 | if (conn->admin_user)
|
---|
1050 | return False;
|
---|
1051 |
|
---|
1052 | SMB_ASSERT(VALID_STAT(*pst));
|
---|
1053 |
|
---|
1054 | if (S_ISREG(pst->st_mode) || S_ISDIR(pst->st_mode) || S_ISLNK(pst->st_mode))
|
---|
1055 | return False;
|
---|
1056 |
|
---|
1057 | return True;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | /*******************************************************************
|
---|
1061 | Should the file be seen by the client ? NOTE: A successful return
|
---|
1062 | is no guarantee of the file's existence ... you also have to check
|
---|
1063 | whether pst is valid.
|
---|
1064 | ********************************************************************/
|
---|
1065 |
|
---|
1066 | bool is_visible_file(connection_struct *conn, const char *dir_path, const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
|
---|
1067 | {
|
---|
1068 | bool hide_unreadable = lp_hideunreadable(SNUM(conn));
|
---|
1069 | bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
|
---|
1070 | bool hide_special = lp_hide_special_files(SNUM(conn));
|
---|
1071 |
|
---|
1072 | SET_STAT_INVALID(*pst);
|
---|
1073 |
|
---|
1074 | if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
|
---|
1075 | return True; /* . and .. are always visible. */
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | /* If it's a vetoed file, pretend it doesn't even exist */
|
---|
1079 | if (use_veto && IS_VETO_PATH(conn, name)) {
|
---|
1080 | DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
|
---|
1081 | return False;
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | if (hide_unreadable || hide_unwriteable || hide_special) {
|
---|
1085 | char *entry = NULL;
|
---|
1086 |
|
---|
1087 | if (asprintf(&entry, "%s/%s", dir_path, name) == -1) {
|
---|
1088 | return False;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /* If it's a dfs symlink, ignore _hide xxxx_ options */
|
---|
1092 | if (lp_host_msdfs() &&
|
---|
1093 | lp_msdfs_root(SNUM(conn)) &&
|
---|
1094 | is_msdfs_link(conn, entry, NULL)) {
|
---|
1095 | SAFE_FREE(entry);
|
---|
1096 | return True;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | /* If the file name does not exist, there's no point checking
|
---|
1100 | * the configuration options. We succeed, on the basis that the
|
---|
1101 | * checks *might* have passed if the file was present.
|
---|
1102 | */
|
---|
1103 | if (SMB_VFS_STAT(conn, entry, pst) != 0) {
|
---|
1104 | SAFE_FREE(entry);
|
---|
1105 | return True;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /* Honour _hide unreadable_ option */
|
---|
1109 | if (hide_unreadable && !user_can_read_file(conn, entry, pst)) {
|
---|
1110 | DEBUG(10,("is_visible_file: file %s is unreadable.\n", entry ));
|
---|
1111 | SAFE_FREE(entry);
|
---|
1112 | return False;
|
---|
1113 | }
|
---|
1114 | /* Honour _hide unwriteable_ option */
|
---|
1115 | if (hide_unwriteable && !user_can_write_file(conn, entry, pst)) {
|
---|
1116 | DEBUG(10,("is_visible_file: file %s is unwritable.\n", entry ));
|
---|
1117 | SAFE_FREE(entry);
|
---|
1118 | return False;
|
---|
1119 | }
|
---|
1120 | /* Honour _hide_special_ option */
|
---|
1121 | if (hide_special && file_is_special(conn, entry, pst)) {
|
---|
1122 | DEBUG(10,("is_visible_file: file %s is special.\n", entry ));
|
---|
1123 | SAFE_FREE(entry);
|
---|
1124 | return False;
|
---|
1125 | }
|
---|
1126 | SAFE_FREE(entry);
|
---|
1127 | }
|
---|
1128 | return True;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | static int smb_Dir_destructor(struct smb_Dir *dirp)
|
---|
1132 | {
|
---|
1133 | if (dirp->dir) {
|
---|
1134 | SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
|
---|
1135 | }
|
---|
1136 | dirhandles_open--;
|
---|
1137 | return 0;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /*******************************************************************
|
---|
1141 | Open a directory.
|
---|
1142 | ********************************************************************/
|
---|
1143 |
|
---|
1144 | struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
|
---|
1145 | const char *name, const char *mask, uint32 attr)
|
---|
1146 | {
|
---|
1147 | struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
|
---|
1148 |
|
---|
1149 | if (!dirp) {
|
---|
1150 | return NULL;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | dirp->conn = conn;
|
---|
1154 | dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
|
---|
1155 |
|
---|
1156 | dirp->dir_path = talloc_strdup(dirp, name);
|
---|
1157 | if (!dirp->dir_path) {
|
---|
1158 | errno = ENOMEM;
|
---|
1159 | goto fail;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | dirhandles_open++;
|
---|
1163 | talloc_set_destructor(dirp, smb_Dir_destructor);
|
---|
1164 |
|
---|
1165 | dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
|
---|
1166 | if (!dirp->dir) {
|
---|
1167 | DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
|
---|
1168 | strerror(errno) ));
|
---|
1169 | goto fail;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | return dirp;
|
---|
1173 |
|
---|
1174 | fail:
|
---|
1175 | TALLOC_FREE(dirp);
|
---|
1176 | return NULL;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /*******************************************************************
|
---|
1180 | Read from a directory. Also return current offset.
|
---|
1181 | Don't check for veto or invisible files.
|
---|
1182 | ********************************************************************/
|
---|
1183 |
|
---|
1184 | const char *ReadDirName(struct smb_Dir *dirp, long *poffset)
|
---|
1185 | {
|
---|
1186 | const char *n;
|
---|
1187 | connection_struct *conn = dirp->conn;
|
---|
1188 |
|
---|
1189 | /* Cheat to allow . and .. to be the first entries returned. */
|
---|
1190 | if (((*poffset == START_OF_DIRECTORY_OFFSET) || (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2)) {
|
---|
1191 | if (dirp->file_number == 0) {
|
---|
1192 | n = ".";
|
---|
1193 | *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
|
---|
1194 | } else {
|
---|
1195 | *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
|
---|
1196 | n = "..";
|
---|
1197 | }
|
---|
1198 | dirp->file_number++;
|
---|
1199 | return n;
|
---|
1200 | } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
|
---|
1201 | *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
|
---|
1202 | return NULL;
|
---|
1203 | } else {
|
---|
1204 | /* A real offset, seek to it. */
|
---|
1205 | SeekDir(dirp, *poffset);
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | while ((n = vfs_readdirname(conn, dirp->dir))) {
|
---|
1209 | /* Ignore . and .. - we've already returned them. */
|
---|
1210 | if (*n == '.') {
|
---|
1211 | if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
|
---|
1212 | continue;
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 | *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
|
---|
1216 | dirp->file_number++;
|
---|
1217 | return n;
|
---|
1218 | }
|
---|
1219 | *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
|
---|
1220 | return NULL;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | /*******************************************************************
|
---|
1224 | Rewind to the start.
|
---|
1225 | ********************************************************************/
|
---|
1226 |
|
---|
1227 | void RewindDir(struct smb_Dir *dirp, long *poffset)
|
---|
1228 | {
|
---|
1229 | SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
|
---|
1230 | dirp->file_number = 0;
|
---|
1231 | dirp->offset = START_OF_DIRECTORY_OFFSET;
|
---|
1232 | *poffset = START_OF_DIRECTORY_OFFSET;
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | /*******************************************************************
|
---|
1236 | Seek a dir.
|
---|
1237 | ********************************************************************/
|
---|
1238 |
|
---|
1239 | void SeekDir(struct smb_Dir *dirp, long offset)
|
---|
1240 | {
|
---|
1241 | if (offset != dirp->offset) {
|
---|
1242 | if (offset == START_OF_DIRECTORY_OFFSET) {
|
---|
1243 | RewindDir(dirp, &offset);
|
---|
1244 | /*
|
---|
1245 | * Ok we should really set the file number here
|
---|
1246 | * to 1 to enable ".." to be returned next. Trouble
|
---|
1247 | * is I'm worried about callers using SeekDir(dirp,0)
|
---|
1248 | * as equivalent to RewindDir(). So leave this alone
|
---|
1249 | * for now.
|
---|
1250 | */
|
---|
1251 | } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
|
---|
1252 | RewindDir(dirp, &offset);
|
---|
1253 | /*
|
---|
1254 | * Set the file number to 2 - we want to get the first
|
---|
1255 | * real file entry (the one we return after "..")
|
---|
1256 | * on the next ReadDir.
|
---|
1257 | */
|
---|
1258 | dirp->file_number = 2;
|
---|
1259 | } else if (offset == END_OF_DIRECTORY_OFFSET) {
|
---|
1260 | ; /* Don't seek in this case. */
|
---|
1261 | } else {
|
---|
1262 | SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
|
---|
1263 | }
|
---|
1264 | dirp->offset = offset;
|
---|
1265 | }
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | /*******************************************************************
|
---|
1269 | Tell a dir position.
|
---|
1270 | ********************************************************************/
|
---|
1271 |
|
---|
1272 | long TellDir(struct smb_Dir *dirp)
|
---|
1273 | {
|
---|
1274 | return(dirp->offset);
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | /*******************************************************************
|
---|
1278 | Add an entry into the dcache.
|
---|
1279 | ********************************************************************/
|
---|
1280 |
|
---|
1281 | void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
|
---|
1282 | {
|
---|
1283 | struct name_cache_entry *e;
|
---|
1284 |
|
---|
1285 | if (dirp->name_cache_size == 0) {
|
---|
1286 | return;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | if (dirp->name_cache == NULL) {
|
---|
1290 | dirp->name_cache = TALLOC_ZERO_ARRAY(
|
---|
1291 | dirp, struct name_cache_entry, dirp->name_cache_size);
|
---|
1292 |
|
---|
1293 | if (dirp->name_cache == NULL) {
|
---|
1294 | return;
|
---|
1295 | }
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | dirp->name_cache_index = (dirp->name_cache_index+1) %
|
---|
1299 | dirp->name_cache_size;
|
---|
1300 | e = &dirp->name_cache[dirp->name_cache_index];
|
---|
1301 | TALLOC_FREE(e->name);
|
---|
1302 | e->name = talloc_strdup(dirp, name);
|
---|
1303 | e->offset = offset;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | /*******************************************************************
|
---|
1307 | Find an entry by name. Leave us at the offset after it.
|
---|
1308 | Don't check for veto or invisible files.
|
---|
1309 | ********************************************************************/
|
---|
1310 |
|
---|
1311 | bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
|
---|
1312 | {
|
---|
1313 | int i;
|
---|
1314 | const char *entry;
|
---|
1315 | connection_struct *conn = dirp->conn;
|
---|
1316 |
|
---|
1317 | /* Search back in the name cache. */
|
---|
1318 | if (dirp->name_cache_size && dirp->name_cache) {
|
---|
1319 | for (i = dirp->name_cache_index; i >= 0; i--) {
|
---|
1320 | struct name_cache_entry *e = &dirp->name_cache[i];
|
---|
1321 | if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
|
---|
1322 | *poffset = e->offset;
|
---|
1323 | SeekDir(dirp, e->offset);
|
---|
1324 | return True;
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
|
---|
1328 | struct name_cache_entry *e = &dirp->name_cache[i];
|
---|
1329 | if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
|
---|
1330 | *poffset = e->offset;
|
---|
1331 | SeekDir(dirp, e->offset);
|
---|
1332 | return True;
|
---|
1333 | }
|
---|
1334 | }
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | /* Not found in the name cache. Rewind directory and start from scratch. */
|
---|
1338 | SMB_VFS_REWINDDIR(conn, dirp->dir);
|
---|
1339 | dirp->file_number = 0;
|
---|
1340 | *poffset = START_OF_DIRECTORY_OFFSET;
|
---|
1341 | while ((entry = ReadDirName(dirp, poffset))) {
|
---|
1342 | if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
|
---|
1343 | return True;
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 | return False;
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | /*****************************************************************
|
---|
1350 | Is this directory empty ?
|
---|
1351 | *****************************************************************/
|
---|
1352 |
|
---|
1353 | NTSTATUS can_delete_directory(struct connection_struct *conn,
|
---|
1354 | const char *dirname)
|
---|
1355 | {
|
---|
1356 | NTSTATUS status = NT_STATUS_OK;
|
---|
1357 | long dirpos = 0;
|
---|
1358 | const char *dname;
|
---|
1359 | struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, dirname,
|
---|
1360 | NULL, 0);
|
---|
1361 |
|
---|
1362 | if (!dir_hnd) {
|
---|
1363 | return map_nt_error_from_unix(errno);
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | while ((dname = ReadDirName(dir_hnd,&dirpos))) {
|
---|
1367 | SMB_STRUCT_STAT st;
|
---|
1368 |
|
---|
1369 | /* Quick check for "." and ".." */
|
---|
1370 | if (dname[0] == '.') {
|
---|
1371 | if (!dname[1] || (dname[1] == '.' && !dname[2])) {
|
---|
1372 | continue;
|
---|
1373 | }
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | if (!is_visible_file(conn, dirname, dname, &st, True)) {
|
---|
1377 | continue;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | DEBUG(10,("can_delete_directory: got name %s - can't delete\n", dname ));
|
---|
1381 | status = NT_STATUS_DIRECTORY_NOT_EMPTY;
|
---|
1382 | break;
|
---|
1383 | }
|
---|
1384 | TALLOC_FREE(dir_hnd);
|
---|
1385 |
|
---|
1386 | return status;
|
---|
1387 | }
|
---|