77a18cad3790d7cf209a2d7e79e147137df3ddce
[kamenim/samba-autobuild/.git] / source3 / modules / vfs_worm.c
1 /*
2  * VFS module to disallow writes for older files
3  *
4  * Copyright (C) 2013, Volker Lendecke
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 "system/filesys.h"
23 #include "libcli/security/security.h"
24
25 static NTSTATUS vfs_worm_create_file(vfs_handle_struct *handle,
26                                      struct smb_request *req,
27                                      uint16_t root_dir_fid,
28                                      struct smb_filename *smb_fname,
29                                      uint32_t access_mask,
30                                      uint32_t share_access,
31                                      uint32_t create_disposition,
32                                      uint32_t create_options,
33                                      uint32_t file_attributes,
34                                      uint32_t oplock_request,
35                                      uint64_t allocation_size,
36                                      uint32_t private_flags,
37                                      struct security_descriptor *sd,
38                                      struct ea_list *ea_list,
39                                      files_struct **result,
40                                      int *pinfo)
41 {
42         bool readonly = false;
43         const uint32_t write_access_flags =
44                 FILE_WRITE_DATA | FILE_APPEND_DATA |
45                 FILE_WRITE_ATTRIBUTES | DELETE_ACCESS |
46                 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS;
47         NTSTATUS status;
48
49         if (VALID_STAT(smb_fname->st)) {
50                 double age;
51                 age = timespec_elapsed(&smb_fname->st.st_ex_ctime);
52                 if (age > lp_parm_int(SNUM(handle->conn), "worm",
53                                       "grace_period", 3600)) {
54                         readonly = true;
55                 }
56         }
57
58         if (readonly && (access_mask & write_access_flags)) {
59                 return NT_STATUS_ACCESS_DENIED;
60         }
61
62         status = SMB_VFS_NEXT_CREATE_FILE(
63                 handle, req, root_dir_fid, smb_fname, access_mask,
64                 share_access, create_disposition, create_options,
65                 file_attributes, oplock_request, allocation_size,
66                 private_flags, sd, ea_list, result, pinfo);
67         if (!NT_STATUS_IS_OK(status)) {
68                 return status;
69         }
70
71         /*
72          * Access via MAXIMUM_ALLOWED_ACCESS?
73          */
74         if (readonly && ((*result)->access_mask & write_access_flags)) {
75                 close_file(req, *result, NORMAL_CLOSE);
76                 return NT_STATUS_ACCESS_DENIED;
77         }
78         return NT_STATUS_OK;
79 }
80
81 static struct vfs_fn_pointers vfs_worm_fns = {
82         .create_file_fn = vfs_worm_create_file,
83 };
84
85 NTSTATUS vfs_worm_init(void);
86 NTSTATUS vfs_worm_init(void)
87 {
88         NTSTATUS ret;
89
90         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "worm",
91                                &vfs_worm_fns);
92         if (!NT_STATUS_IS_OK(ret)) {
93                 return ret;
94         }
95
96         return ret;
97 }