Factor out common code into vfs_acl_common.c.
[kai/samba-autobuild/.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 /*******************************************************************
32  Pull a security descriptor into a DATA_BLOB from a xattr.
33 *******************************************************************/
34
35 NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
36                         vfs_handle_struct *handle,
37                         files_struct *fsp,
38                         const char *name,
39                         DATA_BLOB *pblob)
40 {
41         size_t size = 1024;
42         uint8_t *val = NULL;
43         uint8_t *tmp;
44         ssize_t sizeret;
45         int saved_errno = 0;
46
47         ZERO_STRUCTP(pblob);
48
49   again:
50
51         tmp = TALLOC_REALLOC_ARRAY(ctx, val, uint8_t, size);
52         if (tmp == NULL) {
53                 TALLOC_FREE(val);
54                 return NT_STATUS_NO_MEMORY;
55         }
56         val = tmp;
57
58         become_root();
59         if (fsp && fsp->fh->fd != -1) {
60                 sizeret = SMB_VFS_FGETXATTR(fsp, XATTR_NTACL_NAME, val, size);
61         } else {
62                 sizeret = SMB_VFS_GETXATTR(handle->conn, name,
63                                         XATTR_NTACL_NAME, val, size);
64         }
65         if (sizeret == -1) {
66                 saved_errno = errno;
67         }
68         unbecome_root();
69
70         /* Max ACL size is 65536 bytes. */
71         if (sizeret == -1) {
72                 errno = saved_errno;
73                 if ((errno == ERANGE) && (size != 65536)) {
74                         /* Too small, try again. */
75                         size = 65536;
76                         goto again;
77                 }
78
79                 /* Real error - exit here. */
80                 TALLOC_FREE(val);
81                 return map_nt_error_from_unix(errno);
82         }
83
84         pblob->data = val;
85         pblob->length = sizeret;
86         return NT_STATUS_OK;
87 }
88
89 /*******************************************************************
90  Store a DATA_BLOB into an xattr given an fsp pointer.
91 *******************************************************************/
92
93 NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
94                                 files_struct *fsp,
95                                 DATA_BLOB *pblob)
96 {
97         int ret;
98         int saved_errno = 0;
99
100         DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
101                   (unsigned int)pblob->length, fsp_str_dbg(fsp)));
102
103         become_root();
104         if (fsp->fh->fd != -1) {
105                 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
106                         pblob->data, pblob->length, 0);
107         } else {
108                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name->base_name,
109                                 XATTR_NTACL_NAME,
110                                 pblob->data, pblob->length, 0);
111         }
112         if (ret) {
113                 saved_errno = errno;
114         }
115         unbecome_root();
116         if (ret) {
117                 errno = saved_errno;
118                 DEBUG(5, ("store_acl_blob_fsp: setting attr failed for file %s"
119                         "with error %s\n",
120                         fsp_str_dbg(fsp),
121                         strerror(errno) ));
122                 return map_nt_error_from_unix(errno);
123         }
124         return NT_STATUS_OK;
125 }
126
127 /*******************************************************************
128  Store a DATA_BLOB into an xattr given a pathname.
129 *******************************************************************/
130
131 NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
132                                         const char *fname,
133                                         DATA_BLOB *pblob)
134 {
135         connection_struct *conn = handle->conn;
136         int ret;
137         int saved_errno = 0;
138
139         DEBUG(10,("store_acl_blob_pathname: storing blob "
140                         "length %u on file %s\n",
141                         (unsigned int)pblob->length, fname));
142
143         become_root();
144         ret = SMB_VFS_SETXATTR(conn, fname,
145                                 XATTR_NTACL_NAME,
146                                 pblob->data, pblob->length, 0);
147         if (ret) {
148                 saved_errno = errno;
149         }
150         unbecome_root();
151         if (ret) {
152                 errno = saved_errno;
153                 DEBUG(5, ("store_acl_blob_pathname: setting attr failed "
154                         "for file %s with error %s\n",
155                         fname,
156                         strerror(errno) ));
157                 return map_nt_error_from_unix(errno);
158         }
159         return NT_STATUS_OK;
160 }
161
162 /*********************************************************************
163  Remove a Windows ACL - we're setting the underlying POSIX ACL.
164 *********************************************************************/
165
166 static int sys_acl_set_file_xattr(vfs_handle_struct *handle,
167                               const char *name,
168                               SMB_ACL_TYPE_T type,
169                               SMB_ACL_T theacl)
170 {
171         int ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
172                                                 name,
173                                                 type,
174                                                 theacl);
175         if (ret == -1) {
176                 return -1;
177         }
178
179         become_root();
180         SMB_VFS_REMOVEXATTR(handle->conn, name, XATTR_NTACL_NAME);
181         unbecome_root();
182
183         return ret;
184 }
185
186 /*********************************************************************
187  Remove a Windows ACL - we're setting the underlying POSIX ACL.
188 *********************************************************************/
189
190 static int sys_acl_set_fd_xattr(vfs_handle_struct *handle,
191                             files_struct *fsp,
192                             SMB_ACL_T theacl)
193 {
194         int ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
195                                                 fsp,
196                                                 theacl);
197         if (ret == -1) {
198                 return -1;
199         }
200
201         become_root();
202         SMB_VFS_FREMOVEXATTR(fsp, XATTR_NTACL_NAME);
203         unbecome_root();
204
205         return ret;
206 }
207
208
209 static struct vfs_fn_pointers vfs_acl_xattr_fns = {
210         .mkdir = mkdir_acl_common,
211         .open = open_acl_common,
212         .fget_nt_acl = fget_nt_acl_common,
213         .get_nt_acl = get_nt_acl_common,
214         .fset_nt_acl = fset_nt_acl_common,
215         .sys_acl_set_file = sys_acl_set_file_xattr,
216         .sys_acl_set_fd = sys_acl_set_fd_xattr
217 };
218
219 NTSTATUS vfs_acl_xattr_init(void)
220 {
221         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr",
222                                 &vfs_acl_xattr_fns);
223 }