s3: winbindd: On new client connect, prune idle or hung connections older than "winbi...
[Samba.git] / source3 / smbd / files.c
blob2a0f6cebc0c560079888214bec1bc4460b2c7c76
1 /*
2 Unix SMB/CIFS implementation.
3 Files[] structure handling
4 Copyright (C) Andrew Tridgell 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/>.
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 #include <ccan/hash/hash.h>
26 #include "lib/util/bitmap.h"
28 #define FILE_HANDLE_OFFSET 0x1000
30 /**
31 * create new fsp to be used for file_new or a durable handle reconnect
33 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
34 files_struct **result)
36 NTSTATUS status = NT_STATUS_NO_MEMORY;
37 files_struct *fsp = NULL;
38 struct smbd_server_connection *sconn = conn->sconn;
40 fsp = talloc_zero(mem_ctx, struct files_struct);
41 if (fsp == NULL) {
42 goto fail;
46 * This can't be a child of fsp because the file_handle can be ref'd
47 * when doing a dos/fcb open, which will then share the file_handle
48 * across multiple fsps.
50 fsp->fh = talloc_zero(mem_ctx, struct fd_handle);
51 if (fsp->fh == NULL) {
52 goto fail;
55 fsp->fh->ref_count = 1;
56 fsp->fh->fd = -1;
58 fsp->fnum = FNUM_FIELD_INVALID;
59 fsp->conn = conn;
61 DLIST_ADD(sconn->files, fsp);
62 sconn->num_files += 1;
64 conn->num_files_open++;
66 *result = fsp;
67 return NT_STATUS_OK;
69 fail:
70 if (fsp != NULL) {
71 TALLOC_FREE(fsp->fh);
73 TALLOC_FREE(fsp);
75 return status;
78 /****************************************************************************
79 Find first available file slot.
80 ****************************************************************************/
82 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
83 files_struct **result)
85 struct smbd_server_connection *sconn = conn->sconn;
86 files_struct *fsp;
87 NTSTATUS status;
89 status = fsp_new(conn, conn, &fsp);
90 if (!NT_STATUS_IS_OK(status)) {
91 return status;
94 GetTimeOfDay(&fsp->open_time);
96 if (req) {
97 struct smbXsrv_open *op = NULL;
98 NTTIME now = timeval_to_nttime(&fsp->open_time);
100 status = smbXsrv_open_create(sconn->conn,
101 conn->session_info,
102 now, &op);
103 if (!NT_STATUS_IS_OK(status)) {
104 file_free(NULL, fsp);
105 return status;
107 fsp->op = op;
108 op->compat = fsp;
109 fsp->fnum = op->local_id;
110 fsp->fh->gen_id = smbXsrv_open_hash(op);
111 } else {
112 DEBUG(10, ("%s: req==NULL, INTERNAL_OPEN_ONLY, smbXsrv_open "
113 "allocated\n", __func__));
117 * Create an smb_filename with "" for the base_name. There are very
118 * few NULL checks, so make sure it's initialized with something. to
119 * be safe until an audit can be done.
121 fsp->fsp_name = synthetic_smb_fname(fsp, "", NULL, NULL);
122 if (fsp->fsp_name == NULL) {
123 file_free(NULL, fsp);
124 return NT_STATUS_NO_MEMORY;
127 DEBUG(5,("allocated file structure %s (%u used)\n",
128 fsp_fnum_dbg(fsp), (unsigned int)sconn->num_files));
130 if (req != NULL) {
131 fsp->mid = req->mid;
132 req->chain_fsp = fsp;
135 /* A new fsp invalidates the positive and
136 negative fsp_fi_cache as the new fsp is pushed
137 at the start of the list and we search from
138 a cache hit to the *end* of the list. */
140 ZERO_STRUCT(sconn->fsp_fi_cache);
142 *result = fsp;
143 return NT_STATUS_OK;
146 /****************************************************************************
147 Close all open files for a connection.
148 ****************************************************************************/
150 void file_close_conn(connection_struct *conn)
152 files_struct *fsp, *next;
154 for (fsp=conn->sconn->files; fsp; fsp=next) {
155 next = fsp->next;
156 if (fsp->conn != conn) {
157 continue;
159 if (fsp->op != NULL && fsp->op->global->durable) {
161 * A tree disconnect closes a durable handle
163 fsp->op->global->durable = false;
165 close_file(NULL, fsp, SHUTDOWN_CLOSE);
169 /****************************************************************************
170 Close all open files for a pid and a vuid.
171 ****************************************************************************/
173 void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
174 uint64_t vuid)
176 files_struct *fsp, *next;
178 for (fsp=sconn->files;fsp;fsp=next) {
179 next = fsp->next;
180 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
181 close_file(NULL, fsp, SHUTDOWN_CLOSE);
186 /****************************************************************************
187 Initialise file structures.
188 ****************************************************************************/
190 static int files_max_open_fds;
192 bool file_init_global(void)
194 int request_max = lp_max_open_files();
195 int real_lim;
196 int real_max;
198 if (files_max_open_fds != 0) {
199 return true;
203 * Set the max_open files to be the requested
204 * max plus a fudgefactor to allow for the extra
205 * fd's we need such as log files etc...
207 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
209 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
211 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
212 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
215 if (real_max != request_max) {
216 DEBUG(1, ("file_init_global: Information only: requested %d "
217 "open files, %d are available.\n",
218 request_max, real_max));
221 SMB_ASSERT(real_max > 100);
223 files_max_open_fds = real_max;
224 return true;
227 bool file_init(struct smbd_server_connection *sconn)
229 bool ok;
231 ok = file_init_global();
232 if (!ok) {
233 return false;
236 sconn->real_max_open_files = files_max_open_fds;
238 return true;
241 /****************************************************************************
242 Close files open by a specified vuid.
243 ****************************************************************************/
245 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
247 files_struct *fsp, *next;
249 for (fsp=sconn->files; fsp; fsp=next) {
250 next=fsp->next;
251 if (fsp->vuid == vuid) {
252 close_file(NULL, fsp, SHUTDOWN_CLOSE);
258 * Walk the files table until "fn" returns non-NULL
261 struct files_struct *files_forall(
262 struct smbd_server_connection *sconn,
263 struct files_struct *(*fn)(struct files_struct *fsp,
264 void *private_data),
265 void *private_data)
267 struct files_struct *fsp, *next;
269 for (fsp = sconn->files; fsp; fsp = next) {
270 struct files_struct *ret;
271 next = fsp->next;
272 ret = fn(fsp, private_data);
273 if (ret != NULL) {
274 return ret;
277 return NULL;
280 /****************************************************************************
281 Find a fsp given a file descriptor.
282 ****************************************************************************/
284 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
286 int count=0;
287 files_struct *fsp;
289 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
290 if (fsp->fh->fd == fd) {
291 if (count > 10) {
292 DLIST_PROMOTE(sconn->files, fsp);
294 return fsp;
298 return NULL;
301 /****************************************************************************
302 Find a fsp given a device, inode and file_id.
303 ****************************************************************************/
305 files_struct *file_find_dif(struct smbd_server_connection *sconn,
306 struct file_id id, unsigned long gen_id)
308 int count=0;
309 files_struct *fsp;
311 if (gen_id == 0) {
312 return NULL;
315 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
316 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
317 if (file_id_equal(&fsp->file_id, &id) &&
318 fsp->fh->gen_id == gen_id ) {
319 if (count > 10) {
320 DLIST_PROMOTE(sconn->files, fsp);
322 /* Paranoia check. */
323 if ((fsp->fh->fd == -1) &&
324 (fsp->oplock_type != NO_OPLOCK)) {
325 DEBUG(0,("file_find_dif: file %s file_id = "
326 "%s, gen = %u oplock_type = %u is a "
327 "stat open with oplock type !\n",
328 fsp_str_dbg(fsp),
329 file_id_string_tos(&fsp->file_id),
330 (unsigned int)fsp->fh->gen_id,
331 (unsigned int)fsp->oplock_type ));
332 smb_panic("file_find_dif");
334 return fsp;
338 return NULL;
341 /****************************************************************************
342 Find the first fsp given a device and inode.
343 We use a singleton cache here to speed up searching from getfilepathinfo
344 calls.
345 ****************************************************************************/
347 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
348 struct file_id id)
350 files_struct *fsp;
352 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
353 /* Positive or negative cache hit. */
354 return sconn->fsp_fi_cache.fsp;
357 sconn->fsp_fi_cache.id = id;
359 for (fsp=sconn->files;fsp;fsp=fsp->next) {
360 if (file_id_equal(&fsp->file_id, &id)) {
361 /* Setup positive cache. */
362 sconn->fsp_fi_cache.fsp = fsp;
363 return fsp;
367 /* Setup negative cache. */
368 sconn->fsp_fi_cache.fsp = NULL;
369 return NULL;
372 /****************************************************************************
373 Find the next fsp having the same device and inode.
374 ****************************************************************************/
376 files_struct *file_find_di_next(files_struct *start_fsp)
378 files_struct *fsp;
380 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
381 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
382 return fsp;
386 return NULL;
389 /****************************************************************************
390 Find any fsp open with a pathname below that of an already open path.
391 ****************************************************************************/
393 bool file_find_subpath(files_struct *dir_fsp)
395 files_struct *fsp;
396 size_t dlen;
397 char *d_fullname = NULL;
399 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
400 dir_fsp->conn->connectpath,
401 dir_fsp->fsp_name->base_name);
403 if (!d_fullname) {
404 return false;
407 dlen = strlen(d_fullname);
409 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
410 char *d1_fullname;
412 if (fsp == dir_fsp) {
413 continue;
416 d1_fullname = talloc_asprintf(talloc_tos(),
417 "%s/%s",
418 fsp->conn->connectpath,
419 fsp->fsp_name->base_name);
422 * If the open file has a path that is a longer
423 * component, then it's a subpath.
425 if (strnequal(d_fullname, d1_fullname, dlen) &&
426 (d1_fullname[dlen] == '/')) {
427 TALLOC_FREE(d1_fullname);
428 TALLOC_FREE(d_fullname);
429 return true;
431 TALLOC_FREE(d1_fullname);
434 TALLOC_FREE(d_fullname);
435 return false;
438 /****************************************************************************
439 Sync open files on a connection.
440 ****************************************************************************/
442 void file_sync_all(connection_struct *conn)
444 files_struct *fsp, *next;
446 for (fsp=conn->sconn->files; fsp; fsp=next) {
447 next=fsp->next;
448 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
449 sync_file(conn, fsp, True /* write through */);
454 /****************************************************************************
455 Free up a fsp.
456 ****************************************************************************/
458 void fsp_free(files_struct *fsp)
460 struct smbd_server_connection *sconn = fsp->conn->sconn;
462 DLIST_REMOVE(sconn->files, fsp);
463 SMB_ASSERT(sconn->num_files > 0);
464 sconn->num_files--;
466 TALLOC_FREE(fsp->fake_file_handle);
468 if (fsp->fh->ref_count == 1) {
469 TALLOC_FREE(fsp->fh);
470 } else {
471 fsp->fh->ref_count--;
474 fsp->conn->num_files_open--;
476 /* this is paranoia, just in case someone tries to reuse the
477 information */
478 ZERO_STRUCTP(fsp);
480 /* fsp->fsp_name is a talloc child and is free'd automatically. */
481 TALLOC_FREE(fsp);
484 void file_free(struct smb_request *req, files_struct *fsp)
486 struct smbd_server_connection *sconn = fsp->conn->sconn;
487 uint64_t fnum = fsp->fnum;
489 if (fsp->notify) {
490 struct notify_context *notify_ctx =
491 fsp->conn->sconn->notify_ctx;
492 notify_remove(notify_ctx, fsp);
493 TALLOC_FREE(fsp->notify);
496 /* Ensure this event will never fire. */
497 TALLOC_FREE(fsp->update_write_time_event);
499 if (fsp->op != NULL) {
500 fsp->op->compat = NULL;
502 TALLOC_FREE(fsp->op);
504 if ((req != NULL) && (fsp == req->chain_fsp)) {
505 req->chain_fsp = NULL;
509 * Clear all possible chained fsp
510 * pointers in the SMB2 request queue.
512 if (req != NULL && req->smb2req) {
513 remove_smb2_chained_fsp(fsp);
516 /* Closing a file can invalidate the positive cache. */
517 if (fsp == sconn->fsp_fi_cache.fsp) {
518 ZERO_STRUCT(sconn->fsp_fi_cache);
521 /* Drop all remaining extensions. */
522 vfs_remove_all_fsp_extensions(fsp);
524 fsp_free(fsp);
526 DEBUG(5,("freed files structure %llu (%u used)\n",
527 (unsigned long long)fnum, (unsigned int)sconn->num_files));
530 /****************************************************************************
531 Get an fsp from a packet given a 16 bit fnum.
532 ****************************************************************************/
534 files_struct *file_fsp(struct smb_request *req, uint16 fid)
536 struct smbXsrv_open *op;
537 NTSTATUS status;
538 NTTIME now = 0;
539 files_struct *fsp;
541 if (req == NULL) {
543 * We should never get here. req==NULL could in theory
544 * only happen from internal opens with a non-zero
545 * root_dir_fid. Internal opens just don't do that, at
546 * least they are not supposed to do so. And if they
547 * start to do so, they better fake up a smb_request
548 * from which we get the right smbd_server_conn. While
549 * this should never happen, let's return NULL here.
551 return NULL;
554 if (req->chain_fsp != NULL) {
555 if (req->chain_fsp->deferred_close) {
556 return NULL;
558 return req->chain_fsp;
561 if (req->sconn->conn == NULL) {
562 return NULL;
565 now = timeval_to_nttime(&req->request_time);
567 status = smb1srv_open_lookup(req->sconn->conn,
568 fid, now, &op);
569 if (!NT_STATUS_IS_OK(status)) {
570 return NULL;
573 fsp = op->compat;
574 if (fsp == NULL) {
575 return NULL;
578 if (fsp->deferred_close) {
579 return NULL;
582 req->chain_fsp = fsp;
583 return fsp;
586 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
587 uint64_t persistent_id,
588 uint64_t volatile_id)
590 struct smbXsrv_open *op;
591 NTSTATUS status;
592 NTTIME now = 0;
593 struct files_struct *fsp;
595 now = timeval_to_nttime(&smb2req->request_time);
597 status = smb2srv_open_lookup(smb2req->sconn->conn,
598 persistent_id, volatile_id,
599 now, &op);
600 if (!NT_STATUS_IS_OK(status)) {
601 return NULL;
604 fsp = op->compat;
605 if (fsp == NULL) {
606 return NULL;
609 if (smb2req->tcon == NULL) {
610 return NULL;
613 if (smb2req->tcon->compat != fsp->conn) {
614 return NULL;
617 if (smb2req->session == NULL) {
618 return NULL;
621 if (smb2req->session->compat == NULL) {
622 return NULL;
625 if (smb2req->session->compat->vuid != fsp->vuid) {
626 return NULL;
629 if (fsp->deferred_close) {
630 return NULL;
633 return fsp;
636 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
637 uint64_t persistent_id,
638 uint64_t volatile_id)
640 struct files_struct *fsp;
642 if (smb2req->compat_chain_fsp != NULL) {
643 if (smb2req->compat_chain_fsp->deferred_close) {
644 return NULL;
646 return smb2req->compat_chain_fsp;
649 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
650 if (fsp == NULL) {
651 return NULL;
654 smb2req->compat_chain_fsp = fsp;
655 return fsp;
658 /****************************************************************************
659 Duplicate the file handle part for a DOS or FCB open.
660 ****************************************************************************/
662 NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
663 uint32 access_mask, uint32 share_access,
664 uint32 create_options, files_struct *to)
666 /* this can never happen for print files */
667 SMB_ASSERT(from->print_file == NULL);
669 TALLOC_FREE(to->fh);
671 to->fh = from->fh;
672 to->fh->ref_count++;
674 to->file_id = from->file_id;
675 to->initial_allocation_size = from->initial_allocation_size;
676 to->file_pid = from->file_pid;
677 to->vuid = from->vuid;
678 to->open_time = from->open_time;
679 to->access_mask = access_mask;
680 to->share_access = share_access;
681 to->oplock_type = from->oplock_type;
682 to->can_lock = from->can_lock;
683 to->can_read = ((access_mask & FILE_READ_DATA) != 0);
684 to->can_write =
685 CAN_WRITE(from->conn) &&
686 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
687 to->modified = from->modified;
688 to->is_directory = from->is_directory;
689 to->aio_write_behind = from->aio_write_behind;
691 return fsp_set_smb_fname(to, from->fsp_name);
695 * Return a jenkins hash of a pathname on a connection.
698 NTSTATUS file_name_hash(connection_struct *conn,
699 const char *name, uint32_t *p_name_hash)
701 char tmpbuf[PATH_MAX];
702 char *fullpath, *to_free;
703 size_t len;
705 /* Set the hash of the full pathname. */
707 len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
708 &fullpath, &to_free);
709 if (len == -1) {
710 return NT_STATUS_NO_MEMORY;
712 *p_name_hash = hash(fullpath, len+1, 0);
714 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
715 fullpath,
716 (unsigned int)*p_name_hash ));
718 TALLOC_FREE(to_free);
719 return NT_STATUS_OK;
723 * The only way that the fsp->fsp_name field should ever be set.
725 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
726 const struct smb_filename *smb_fname_in)
728 struct smb_filename *smb_fname_new;
730 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
731 if (smb_fname_new == NULL) {
732 return NT_STATUS_NO_MEMORY;
735 TALLOC_FREE(fsp->fsp_name);
736 fsp->fsp_name = smb_fname_new;
738 return file_name_hash(fsp->conn,
739 smb_fname_str_dbg(fsp->fsp_name),
740 &fsp->name_hash);