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