Fix the build breakage by #including modules/vfs_acl_common.c
[samba.git] / source3 / modules / vfs_acl_xattr.c
1 /*
2  * Store Windows ACLs in xattrs.
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  * Copyright (C) Jeremy Allison, 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /* NOTE: This is an experimental module, not yet finished. JRA. */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "../lib/crypto/crypto.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
30
31 /* Pull in the common functions. */
32 #include "modules/vfs_acl_common.c"
33
34 /*******************************************************************
35  Pull a security descriptor into a DATA_BLOB from a xattr.
36 *******************************************************************/
37
38 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
39                         vfs_handle_struct *handle,
40                         files_struct *fsp,
41                         const char *name,
42                         DATA_BLOB *pblob)
43 {
44         size_t size = 1024;
45         uint8_t *val = NULL;
46         uint8_t *tmp;
47         ssize_t sizeret;
48         int saved_errno = 0;
49
50         ZERO_STRUCTP(pblob);
51
52   again:
53
54         tmp = TALLOC_REALLOC_ARRAY(ctx, val, uint8_t, size);
55         if (tmp == NULL) {
56                 TALLOC_FREE(val);
57                 return NT_STATUS_NO_MEMORY;
58         }
59         val = tmp;
60
61         become_root();
62         if (fsp && fsp->fh->fd != -1) {
63                 sizeret = SMB_VFS_FGETXATTR(fsp, XATTR_NTACL_NAME, val, size);
64         } else {
65                 sizeret = SMB_VFS_GETXATTR(handle->conn, name,
66                                         XATTR_NTACL_NAME, val, size);
67         }
68         if (sizeret == -1) {
69                 saved_errno = errno;
70         }
71         unbecome_root();
72
73         /* Max ACL size is 65536 bytes. */
74         if (sizeret == -1) {
75                 errno = saved_errno;
76                 if ((errno == ERANGE) && (size != 65536)) {
77                         /* Too small, try again. */
78                         size = 65536;
79                         goto again;
80                 }
81
82                 /* Real error - exit here. */
83                 TALLOC_FREE(val);
84                 return map_nt_error_from_unix(errno);
85         }
86
87         pblob->data = val;
88         pblob->length = sizeret;
89         return NT_STATUS_OK;
90 }
91
92 /*******************************************************************
93  Store a DATA_BLOB into an xattr given an fsp pointer.
94 *******************************************************************/
95
96 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
97                                 files_struct *fsp,
98                                 DATA_BLOB *pblob)
99 {
100         int ret;
101         int saved_errno = 0;
102
103         DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
104                   (unsigned int)pblob->length, fsp_str_dbg(fsp)));
105
106         become_root();
107         if (fsp->fh->fd != -1) {
108                 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
109                         pblob->data, pblob->length, 0);
110         } else {
111                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name->base_name,
112                                 XATTR_NTACL_NAME,
113                                 pblob->data, pblob->length, 0);
114         }
115         if (ret) {
116                 saved_errno = errno;
117         }
118         unbecome_root();
119         if (ret) {
120                 errno = saved_errno;
121                 DEBUG(5, ("store_acl_blob_fsp: setting attr failed for file %s"
122                         "with error %s\n",
123                         fsp_str_dbg(fsp),
124                         strerror(errno) ));
125                 return map_nt_error_from_unix(errno);
126         }
127         return NT_STATUS_OK;
128 }
129
130 /*******************************************************************
131  Store a DATA_BLOB into an xattr given a pathname.
132 *******************************************************************/
133
134 static NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
135                                         const char *fname,
136                                         DATA_BLOB *pblob)
137 {
138         connection_struct *conn = handle->conn;
139         int ret;
140         int saved_errno = 0;
141
142         DEBUG(10,("store_acl_blob_pathname: storing blob "
143                         "length %u on file %s\n",
144                         (unsigned int)pblob->length, fname));
145
146         become_root();
147         ret = SMB_VFS_SETXATTR(conn, fname,
148                                 XATTR_NTACL_NAME,
149                                 pblob->data, pblob->length, 0);
150         if (ret) {
151                 saved_errno = errno;
152         }
153         unbecome_root();
154         if (ret) {
155                 errno = saved_errno;
156                 DEBUG(5, ("store_acl_blob_pathname: setting attr failed "
157                         "for file %s with error %s\n",
158                         fname,
159                         strerror(errno) ));
160                 return map_nt_error_from_unix(errno);
161         }
162         return NT_STATUS_OK;
163 }
164
165 /*********************************************************************
166  Remove a Windows ACL - we're setting the underlying POSIX ACL.
167 *********************************************************************/
168
169 static int sys_acl_set_file_xattr(vfs_handle_struct *handle,
170                               const char *name,
171                               SMB_ACL_TYPE_T type,
172                               SMB_ACL_T theacl)
173 {
174         int ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
175                                                 name,
176                                                 type,
177                                                 theacl);
178         if (ret == -1) {
179                 return -1;
180         }
181
182         become_root();
183         SMB_VFS_REMOVEXATTR(handle->conn, name, XATTR_NTACL_NAME);
184         unbecome_root();
185
186         return ret;
187 }
188
189 /*********************************************************************
190  Remove a Windows ACL - we're setting the underlying POSIX ACL.
191 *********************************************************************/
192
193 static int sys_acl_set_fd_xattr(vfs_handle_struct *handle,
194                             files_struct *fsp,
195                             SMB_ACL_T theacl)
196 {
197         int ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
198                                                 fsp,
199                                                 theacl);
200         if (ret == -1) {
201                 return -1;
202         }
203
204         become_root();
205         SMB_VFS_FREMOVEXATTR(fsp, XATTR_NTACL_NAME);
206         unbecome_root();
207
208         return ret;
209 }
210
211
212 static struct vfs_fn_pointers vfs_acl_xattr_fns = {
213         .mkdir = mkdir_acl_common,
214         .open = open_acl_common,
215         .fget_nt_acl = fget_nt_acl_common,
216         .get_nt_acl = get_nt_acl_common,
217         .fset_nt_acl = fset_nt_acl_common,
218         .sys_acl_set_file = sys_acl_set_file_xattr,
219         .sys_acl_set_fd = sys_acl_set_fd_xattr
220 };
221
222 NTSTATUS vfs_acl_xattr_init(void)
223 {
224         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr",
225                                 &vfs_acl_xattr_fns);
226 }