source: vendor/3.6.0/source3/smbd/files.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 17.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Files[] structure handling
4 Copyright (C) Andrew Tridgell 1998
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21#include "smbd/smbd.h"
22#include "smbd/globals.h"
23#include "libcli/security/security.h"
24#include "util_tdb.h"
25
26#define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
27
28#define FILE_HANDLE_OFFSET 0x1000
29
30/****************************************************************************
31 Return a unique number identifying this fsp over the life of this pid.
32****************************************************************************/
33
34static unsigned long get_gen_count(struct smbd_server_connection *sconn)
35{
36 sconn->file_gen_counter += 1;
37 if (sconn->file_gen_counter == 0) {
38 sconn->file_gen_counter += 1;
39 }
40 return sconn->file_gen_counter;
41}
42
43/****************************************************************************
44 Find first available file slot.
45****************************************************************************/
46
47NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
48 files_struct **result)
49{
50 struct smbd_server_connection *sconn = conn->sconn;
51 int i;
52 files_struct *fsp;
53 NTSTATUS status;
54
55 /* we want to give out file handles differently on each new
56 connection because of a common bug in MS clients where they try to
57 reuse a file descriptor from an earlier smb connection. This code
58 increases the chance that the errant client will get an error rather
59 than causing corruption */
60 if (sconn->first_file == 0) {
61 sconn->first_file = (sys_getpid() ^ (int)time(NULL));
62 sconn->first_file %= sconn->real_max_open_files;
63 }
64
65 /* TODO: Port the id-tree implementation from Samba4 */
66
67 i = bitmap_find(sconn->file_bmap, sconn->first_file);
68 if (i == -1) {
69 DEBUG(0,("ERROR! Out of file structures\n"));
70 /* TODO: We have to unconditionally return a DOS error here,
71 * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
72 * NTSTATUS negotiated */
73 return NT_STATUS_TOO_MANY_OPENED_FILES;
74 }
75
76 /*
77 * Make a child of the connection_struct as an fsp can't exist
78 * independent of a connection.
79 */
80 fsp = talloc_zero(conn, struct files_struct);
81 if (!fsp) {
82 return NT_STATUS_NO_MEMORY;
83 }
84
85 /*
86 * This can't be a child of fsp because the file_handle can be ref'd
87 * when doing a dos/fcb open, which will then share the file_handle
88 * across multiple fsps.
89 */
90 fsp->fh = talloc_zero(conn, struct fd_handle);
91 if (!fsp->fh) {
92 TALLOC_FREE(fsp);
93 return NT_STATUS_NO_MEMORY;
94 }
95
96 fsp->fh->ref_count = 1;
97 fsp->fh->fd = -1;
98
99 fsp->conn = conn;
100 fsp->fh->gen_id = get_gen_count(sconn);
101 GetTimeOfDay(&fsp->open_time);
102
103 sconn->first_file = (i+1) % (sconn->real_max_open_files);
104
105 bitmap_set(sconn->file_bmap, i);
106 sconn->files_used += 1;
107
108 fsp->fnum = i + FILE_HANDLE_OFFSET;
109 SMB_ASSERT(fsp->fnum < 65536);
110
111 /*
112 * Create an smb_filename with "" for the base_name. There are very
113 * few NULL checks, so make sure it's initialized with something. to
114 * be safe until an audit can be done.
115 */
116 status = create_synthetic_smb_fname(fsp, "", NULL, NULL,
117 &fsp->fsp_name);
118 if (!NT_STATUS_IS_OK(status)) {
119 TALLOC_FREE(fsp);
120 TALLOC_FREE(fsp->fh);
121 }
122
123 DLIST_ADD(sconn->files, fsp);
124
125 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
126 i, fsp->fnum, sconn->files_used));
127
128 if (req != NULL) {
129 req->chain_fsp = fsp;
130 }
131
132 /* A new fsp invalidates the positive and
133 negative fsp_fi_cache as the new fsp is pushed
134 at the start of the list and we search from
135 a cache hit to the *end* of the list. */
136
137 ZERO_STRUCT(sconn->fsp_fi_cache);
138
139 conn->num_files_open++;
140
141 *result = fsp;
142 return NT_STATUS_OK;
143}
144
145/****************************************************************************
146 Close all open files for a connection.
147****************************************************************************/
148
149void file_close_conn(connection_struct *conn)
150{
151 files_struct *fsp, *next;
152
153 for (fsp=conn->sconn->files; fsp; fsp=next) {
154 next = fsp->next;
155 if (fsp->conn == conn) {
156 close_file(NULL, fsp, SHUTDOWN_CLOSE);
157 }
158 }
159}
160
161/****************************************************************************
162 Close all open files for a pid and a vuid.
163****************************************************************************/
164
165void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
166 int vuid)
167{
168 files_struct *fsp, *next;
169
170 for (fsp=sconn->files;fsp;fsp=next) {
171 next = fsp->next;
172 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
173 close_file(NULL, fsp, SHUTDOWN_CLOSE);
174 }
175 }
176}
177
178/****************************************************************************
179 Initialise file structures.
180****************************************************************************/
181
182bool file_init(struct smbd_server_connection *sconn)
183{
184 int request_max_open_files = lp_max_open_files();
185 int real_lim;
186
187 /*
188 * Set the max_open files to be the requested
189 * max plus a fudgefactor to allow for the extra
190 * fd's we need such as log files etc...
191 */
192 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
193
194 sconn->real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
195
196 if (sconn->real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES
197 > 65536)
198 sconn->real_max_open_files =
199 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
200
201 if(sconn->real_max_open_files != request_max_open_files) {
202 DEBUG(1, ("file_init: Information only: requested %d "
203 "open files, %d are available.\n",
204 request_max_open_files, sconn->real_max_open_files));
205 }
206
207 SMB_ASSERT(sconn->real_max_open_files > 100);
208
209 sconn->file_bmap = bitmap_talloc(sconn, sconn->real_max_open_files);
210
211 if (!sconn->file_bmap) {
212 return false;
213 }
214 return true;
215}
216
217/****************************************************************************
218 Close files open by a specified vuid.
219****************************************************************************/
220
221void file_close_user(struct smbd_server_connection *sconn, int vuid)
222{
223 files_struct *fsp, *next;
224
225 for (fsp=sconn->files; fsp; fsp=next) {
226 next=fsp->next;
227 if (fsp->vuid == vuid) {
228 close_file(NULL, fsp, SHUTDOWN_CLOSE);
229 }
230 }
231}
232
233/*
234 * Walk the files table until "fn" returns non-NULL
235 */
236
237struct files_struct *files_forall(
238 struct smbd_server_connection *sconn,
239 struct files_struct *(*fn)(struct files_struct *fsp,
240 void *private_data),
241 void *private_data)
242{
243 struct files_struct *fsp, *next;
244
245 for (fsp = sconn->files; fsp; fsp = next) {
246 struct files_struct *ret;
247 next = fsp->next;
248 ret = fn(fsp, private_data);
249 if (ret != NULL) {
250 return ret;
251 }
252 }
253 return NULL;
254}
255
256/****************************************************************************
257 Find a fsp given a file descriptor.
258****************************************************************************/
259
260files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
261{
262 int count=0;
263 files_struct *fsp;
264
265 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
266 if (fsp->fh->fd == fd) {
267 if (count > 10) {
268 DLIST_PROMOTE(sconn->files, fsp);
269 }
270 return fsp;
271 }
272 }
273
274 return NULL;
275}
276
277/****************************************************************************
278 Find a fsp given a device, inode and file_id.
279****************************************************************************/
280
281files_struct *file_find_dif(struct smbd_server_connection *sconn,
282 struct file_id id, unsigned long gen_id)
283{
284 int count=0;
285 files_struct *fsp;
286
287 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
288 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
289 if (file_id_equal(&fsp->file_id, &id) &&
290 fsp->fh->gen_id == gen_id ) {
291 if (count > 10) {
292 DLIST_PROMOTE(sconn->files, fsp);
293 }
294 /* Paranoia check. */
295 if ((fsp->fh->fd == -1) &&
296 (fsp->oplock_type != NO_OPLOCK) &&
297 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
298 DEBUG(0,("file_find_dif: file %s file_id = "
299 "%s, gen = %u oplock_type = %u is a "
300 "stat open with oplock type !\n",
301 fsp_str_dbg(fsp),
302 file_id_string_tos(&fsp->file_id),
303 (unsigned int)fsp->fh->gen_id,
304 (unsigned int)fsp->oplock_type ));
305 smb_panic("file_find_dif");
306 }
307 return fsp;
308 }
309 }
310
311 return NULL;
312}
313
314/****************************************************************************
315 Find the first fsp given a device and inode.
316 We use a singleton cache here to speed up searching from getfilepathinfo
317 calls.
318****************************************************************************/
319
320files_struct *file_find_di_first(struct smbd_server_connection *sconn,
321 struct file_id id)
322{
323 files_struct *fsp;
324
325 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
326 /* Positive or negative cache hit. */
327 return sconn->fsp_fi_cache.fsp;
328 }
329
330 sconn->fsp_fi_cache.id = id;
331
332 for (fsp=sconn->files;fsp;fsp=fsp->next) {
333 if (file_id_equal(&fsp->file_id, &id)) {
334 /* Setup positive cache. */
335 sconn->fsp_fi_cache.fsp = fsp;
336 return fsp;
337 }
338 }
339
340 /* Setup negative cache. */
341 sconn->fsp_fi_cache.fsp = NULL;
342 return NULL;
343}
344
345/****************************************************************************
346 Find the next fsp having the same device and inode.
347****************************************************************************/
348
349files_struct *file_find_di_next(files_struct *start_fsp)
350{
351 files_struct *fsp;
352
353 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
354 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
355 return fsp;
356 }
357 }
358
359 return NULL;
360}
361
362/****************************************************************************
363 Find any fsp open with a pathname below that of an already open path.
364****************************************************************************/
365
366bool file_find_subpath(files_struct *dir_fsp)
367{
368 files_struct *fsp;
369 size_t dlen;
370 char *d_fullname = NULL;
371
372 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
373 dir_fsp->conn->connectpath,
374 dir_fsp->fsp_name->base_name);
375
376 if (!d_fullname) {
377 return false;
378 }
379
380 dlen = strlen(d_fullname);
381
382 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
383 char *d1_fullname;
384
385 if (fsp == dir_fsp) {
386 continue;
387 }
388
389 d1_fullname = talloc_asprintf(talloc_tos(),
390 "%s/%s",
391 fsp->conn->connectpath,
392 fsp->fsp_name->base_name);
393
394 /*
395 * If the open file has a path that is a longer
396 * component, then it's a subpath.
397 */
398 if (strnequal(d_fullname, d1_fullname, dlen) &&
399 (d1_fullname[dlen] == '/')) {
400 TALLOC_FREE(d1_fullname);
401 TALLOC_FREE(d_fullname);
402 return true;
403 }
404 TALLOC_FREE(d1_fullname);
405 }
406
407 TALLOC_FREE(d_fullname);
408 return false;
409}
410
411/****************************************************************************
412 Sync open files on a connection.
413****************************************************************************/
414
415void file_sync_all(connection_struct *conn)
416{
417 files_struct *fsp, *next;
418
419 for (fsp=conn->sconn->files; fsp; fsp=next) {
420 next=fsp->next;
421 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
422 sync_file(conn, fsp, True /* write through */);
423 }
424 }
425}
426
427/****************************************************************************
428 Free up a fsp.
429****************************************************************************/
430
431void file_free(struct smb_request *req, files_struct *fsp)
432{
433 struct smbd_server_connection *sconn = fsp->conn->sconn;
434
435 DLIST_REMOVE(sconn->files, fsp);
436
437 TALLOC_FREE(fsp->fake_file_handle);
438
439 if (fsp->fh->ref_count == 1) {
440 TALLOC_FREE(fsp->fh);
441 } else {
442 fsp->fh->ref_count--;
443 }
444
445 if (fsp->notify) {
446 if (fsp->is_directory) {
447 notify_remove_onelevel(fsp->conn->notify_ctx,
448 &fsp->file_id, fsp);
449 }
450 notify_remove(fsp->conn->notify_ctx, fsp);
451 TALLOC_FREE(fsp->notify);
452 }
453
454 /* Ensure this event will never fire. */
455 TALLOC_FREE(fsp->oplock_timeout);
456
457 /* Ensure this event will never fire. */
458 TALLOC_FREE(fsp->update_write_time_event);
459
460 bitmap_clear(sconn->file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
461 sconn->files_used--;
462
463 DEBUG(5,("freed files structure %d (%d used)\n",
464 fsp->fnum, sconn->files_used));
465
466 fsp->conn->num_files_open--;
467
468 if ((req != NULL) && (fsp == req->chain_fsp)) {
469 req->chain_fsp = NULL;
470 }
471
472 /*
473 * Clear all possible chained fsp
474 * pointers in the SMB2 request queue.
475 */
476 if (req != NULL && req->smb2req) {
477 remove_smb2_chained_fsp(fsp);
478 }
479
480 /* Closing a file can invalidate the positive cache. */
481 if (fsp == sconn->fsp_fi_cache.fsp) {
482 ZERO_STRUCT(sconn->fsp_fi_cache);
483 }
484
485 /* Drop all remaining extensions. */
486 while (fsp->vfs_extension) {
487 vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp);
488 }
489
490 /* this is paranoia, just in case someone tries to reuse the
491 information */
492 ZERO_STRUCTP(fsp);
493
494 /* fsp->fsp_name is a talloc child and is free'd automatically. */
495 TALLOC_FREE(fsp);
496}
497
498/****************************************************************************
499 Get an fsp from a 16 bit fnum.
500****************************************************************************/
501
502static struct files_struct *file_fnum(struct smbd_server_connection *sconn,
503 uint16 fnum)
504{
505 files_struct *fsp;
506 int count=0;
507
508 for (fsp=sconn->files; fsp; fsp=fsp->next, count++) {
509 if (fsp->fnum == fnum) {
510 if (count > 10) {
511 DLIST_PROMOTE(sconn->files, fsp);
512 }
513 return fsp;
514 }
515 }
516 return NULL;
517}
518
519/****************************************************************************
520 Get an fsp from a packet given a 16 bit fnum.
521****************************************************************************/
522
523files_struct *file_fsp(struct smb_request *req, uint16 fid)
524{
525 files_struct *fsp;
526
527 if (req == NULL) {
528 /*
529 * We should never get here. req==NULL could in theory
530 * only happen from internal opens with a non-zero
531 * root_dir_fid. Internal opens just don't do that, at
532 * least they are not supposed to do so. And if they
533 * start to do so, they better fake up a smb_request
534 * from which we get the right smbd_server_conn. While
535 * this should never happen, let's return NULL here.
536 */
537 return NULL;
538 }
539
540 if (req->chain_fsp != NULL) {
541 return req->chain_fsp;
542 }
543
544 fsp = file_fnum(req->sconn, fid);
545 if (fsp != NULL) {
546 req->chain_fsp = fsp;
547 }
548 return fsp;
549}
550
551/****************************************************************************
552 Duplicate the file handle part for a DOS or FCB open.
553****************************************************************************/
554
555NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
556 uint32 access_mask, uint32 share_access,
557 uint32 create_options, files_struct *to)
558{
559 TALLOC_FREE(to->fh);
560
561 to->fh = from->fh;
562 to->fh->ref_count++;
563
564 to->file_id = from->file_id;
565 to->initial_allocation_size = from->initial_allocation_size;
566 to->mode = from->mode;
567 to->file_pid = from->file_pid;
568 to->vuid = from->vuid;
569 to->open_time = from->open_time;
570 to->access_mask = access_mask;
571 to->share_access = share_access;
572 to->oplock_type = from->oplock_type;
573 to->can_lock = from->can_lock;
574 to->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
575 if (!CAN_WRITE(from->conn)) {
576 to->can_write = False;
577 } else {
578 to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
579 }
580 to->modified = from->modified;
581 to->is_directory = from->is_directory;
582 to->aio_write_behind = from->aio_write_behind;
583
584 if (from->print_file) {
585 to->print_file = talloc(to, struct print_file_data);
586 if (!to->print_file) return NT_STATUS_NO_MEMORY;
587 to->print_file->rap_jobid = from->print_file->rap_jobid;
588 } else {
589 to->print_file = NULL;
590 }
591
592 return fsp_set_smb_fname(to, from->fsp_name);
593}
594
595/**
596 * Return a jenkins hash of a pathname on a connection.
597 */
598
599NTSTATUS file_name_hash(connection_struct *conn,
600 const char *name, uint32_t *p_name_hash)
601{
602 TDB_DATA key;
603 char *fullpath = NULL;
604
605 /* Set the hash of the full pathname. */
606 fullpath = talloc_asprintf(talloc_tos(),
607 "%s/%s",
608 conn->connectpath,
609 name);
610 if (!fullpath) {
611 return NT_STATUS_NO_MEMORY;
612 }
613 key = string_term_tdb_data(fullpath);
614 *p_name_hash = tdb_jenkins_hash(&key);
615
616 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
617 fullpath,
618 (unsigned int)*p_name_hash ));
619
620 TALLOC_FREE(fullpath);
621 return NT_STATUS_OK;
622}
623
624/**
625 * The only way that the fsp->fsp_name field should ever be set.
626 */
627NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
628 const struct smb_filename *smb_fname_in)
629{
630 NTSTATUS status;
631 struct smb_filename *smb_fname_new;
632
633 status = copy_smb_filename(fsp, smb_fname_in, &smb_fname_new);
634 if (!NT_STATUS_IS_OK(status)) {
635 return status;
636 }
637
638 TALLOC_FREE(fsp->fsp_name);
639 fsp->fsp_name = smb_fname_new;
640
641 return file_name_hash(fsp->conn,
642 smb_fname_str_dbg(fsp->fsp_name),
643 &fsp->name_hash);
644}
Note: See TracBrowser for help on using the repository browser.