source: trunk/server/source3/smbd/files.c

Last change on this file was 920, checked in by Silvan Scherrer, 9 years ago

Samba Server: apply latest security patches to trunk

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