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