s3-vfs: Put vfs_aixacl_util.c helper functions into a header file
[kai/samba.git] / source3 / modules / vfs_posix_eadb.c
1 /*
2  * Store posix-level xattrs in a tdb (posix:eadb format)
3  *
4  * Copyright (C) Andrew Bartlett, 2011
5  *
6  * Based on vfs_xattr_tdb by
7  * Copyright (C) Volker Lendecke, 2007
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "librpc/gen_ndr/xattr.h"
27 #include "librpc/gen_ndr/ndr_xattr.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "tdb_compat.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "ntvfs/posix/posix_eadb.h"
32 #include "param/param.h"
33 #include "lib/param/loadparm.h"
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_VFS
37
38 /*
39  * Worker routine for getxattr and fgetxattr
40  */
41
42 static ssize_t posix_eadb_getattr(struct tdb_wrap *db_ctx,
43                                  const char *fname, int fd,
44                                  const char *name, void *value, size_t size)
45 {
46         ssize_t result = -1;
47         NTSTATUS status;
48         DATA_BLOB blob;
49
50         DEBUG(10, ("posix_eadb_getattr called for file %s/fd %d, name %s\n",
51                    fname, fd, name));
52
53         status = pull_xattr_blob_tdb_raw(db_ctx, talloc_tos(), name, fname, fd, size, &blob);
54
55         if (!NT_STATUS_IS_OK(status)) {
56                 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
57                            nt_errstr(status)));
58                 errno = EINVAL;
59                 return -1;
60         }
61
62         if (blob.length > size) {
63                 errno = ERANGE;
64                 goto fail;
65         }
66
67         memcpy(value, blob.data, blob.length);
68         result = blob.length;
69
70  fail:
71         return result;
72 }
73
74 static ssize_t posix_eadb_getxattr(struct vfs_handle_struct *handle,
75                                   const char *path, const char *name,
76                                   void *value, size_t size)
77 {
78         struct tdb_wrap *db;
79
80         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
81
82         return posix_eadb_getattr(db, path, -1, name, value, size);
83 }
84
85 static ssize_t posix_eadb_fgetxattr(struct vfs_handle_struct *handle,
86                                    struct files_struct *fsp,
87                                    const char *name, void *value, size_t size)
88 {
89         struct tdb_wrap *db;
90
91         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
92
93         return posix_eadb_getattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name, value, size);
94 }
95
96 /*
97  * Worker routine for setxattr and fsetxattr
98  */
99
100 static int posix_eadb_setattr(struct tdb_wrap *db_ctx,
101                              const char *fname, int fd, const char *name,
102                              const void *value, size_t size, int flags)
103 {
104         NTSTATUS status;
105         DATA_BLOB data = data_blob_const(value, size);
106
107         DEBUG(10, ("posix_eadb_setattr called for file %s/fd %d, name %s\n",
108                    fname, fd, name));
109
110         status = push_xattr_blob_tdb_raw(db_ctx, name, fname, fd, &data);
111
112         if (!NT_STATUS_IS_OK(status)) {
113                 DEBUG(10, ("push_xattr_blob_tdb_raw failed: %s\n",
114                            nt_errstr(status)));
115                 return -1;
116         }
117
118         return 0;
119 }
120
121 static int posix_eadb_setxattr(struct vfs_handle_struct *handle,
122                               const char *path, const char *name,
123                               const void *value, size_t size, int flags)
124 {
125         struct tdb_wrap *db;
126
127         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
128
129         return posix_eadb_setattr(db, path, -1, name, value, size, flags);
130 }
131
132 static int posix_eadb_fsetxattr(struct vfs_handle_struct *handle,
133                                struct files_struct *fsp,
134                                const char *name, const void *value,
135                                size_t size, int flags)
136 {
137         struct tdb_wrap *db;
138
139         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
140
141         return posix_eadb_setattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name, value, size, flags);
142 }
143
144 /*
145  * Worker routine for listxattr and flistxattr
146  */
147
148 static ssize_t posix_eadb_listattr(struct tdb_wrap *db_ctx,
149                                   const char *fname, int fd, char *list,
150                                   size_t size)
151 {
152         DATA_BLOB blob;
153         NTSTATUS status;
154
155         status = list_posix_eadb_raw(db_ctx, talloc_tos(), fname, fd, &blob);
156
157         if (!NT_STATUS_IS_OK(status)) {
158                 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
159                            nt_errstr(status)));
160                 errno = EINVAL;
161                 return -1;
162         }
163
164         if (blob.length > size) {
165                 errno = ERANGE;
166                 TALLOC_FREE(blob.data);
167                 return -1;
168         }
169
170         memcpy(list, blob.data, blob.length);
171
172         TALLOC_FREE(blob.data);
173         return blob.length;
174 }
175
176 static ssize_t posix_eadb_listxattr(struct vfs_handle_struct *handle,
177                                    const char *path, char *list, size_t size)
178 {
179         struct tdb_wrap *db;
180
181         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
182
183         return posix_eadb_listattr(db, path, -1, list, size);
184 }
185
186 static ssize_t posix_eadb_flistxattr(struct vfs_handle_struct *handle,
187                                     struct files_struct *fsp, char *list,
188                                     size_t size)
189 {
190         struct tdb_wrap *db;
191
192         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
193
194         return posix_eadb_listattr(db, fsp->fsp_name->base_name, fsp->fh->fd, list, size);
195 }
196
197 /*
198  * Worker routine for removexattr and fremovexattr
199  */
200
201 static int posix_eadb_removeattr(struct tdb_wrap *db_ctx,
202                                 const char *fname, int fd, const char *name)
203 {
204         NTSTATUS status;
205
206         status = delete_posix_eadb_raw(db_ctx, name, fname, fd);
207
208         if (!NT_STATUS_IS_OK(status)) {
209                 DEBUG(10, ("delete_posix_eadb_raw failed: %s\n",
210                            nt_errstr(status)));
211                 return -1;
212         }
213         return 0;
214 }
215
216 static int posix_eadb_removexattr(struct vfs_handle_struct *handle,
217                                  const char *path, const char *name)
218 {
219         struct tdb_wrap *db;
220
221         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
222
223         return posix_eadb_removeattr(db, path, -1, name);
224 }
225
226 static int posix_eadb_fremovexattr(struct vfs_handle_struct *handle,
227                                   struct files_struct *fsp, const char *name)
228 {
229         struct tdb_wrap *db;
230
231         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
232
233         return posix_eadb_removeattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name);
234 }
235
236 /*
237  * Open the tdb file upon VFS_CONNECT
238  */
239
240 static bool posix_eadb_init(int snum, struct tdb_wrap **p_db)
241 {
242         struct tdb_wrap *db;
243         struct loadparm_context *lp_ctx;
244         const char *eadb = lp_parm_const_string(snum, "posix", "eadb", NULL);
245
246         if (!eadb) {
247                 DEBUG(0, ("Can not use vfs_posix_eadb without posix:eadb set\n"));
248                 return false;
249         }
250
251         lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
252
253         become_root();
254         db = tdb_wrap_open(NULL, eadb, 50000,
255                            TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
256                            lp_ctx);
257
258         unbecome_root();
259         talloc_unlink(NULL, lp_ctx);
260         /* now we know dbname is not NULL */
261
262         if (db == NULL) {
263 #if defined(ENOTSUP)
264                 errno = ENOTSUP;
265 #else
266                 errno = ENOSYS;
267 #endif
268                 return false;
269         }
270
271         *p_db = db;
272         return true;
273 }
274
275 /*
276  * On unlink we need to delete the tdb record
277  */
278 static int posix_eadb_unlink(vfs_handle_struct *handle,
279                             const struct smb_filename *smb_fname)
280 {
281         struct smb_filename *smb_fname_tmp = NULL;
282         NTSTATUS status;
283         int ret = -1;
284
285         struct tdb_wrap *ea_tdb;
286
287         SMB_VFS_HANDLE_GET_DATA(handle, ea_tdb, struct tdb_wrap, return -1);
288
289         status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
290         if (!NT_STATUS_IS_OK(status)) {
291                 errno = map_errno_from_nt_status(status);
292                 return -1;
293         }
294
295         if (lp_posix_pathnames()) {
296                 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
297         } else {
298                 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
299         }
300         if (ret == -1) {
301                 goto out;
302         }
303
304         if (smb_fname_tmp->st.st_ex_nlink == 1) {
305                 /* Only remove record on last link to file. */
306
307                 if (tdb_transaction_start(ea_tdb->tdb) != 0) {
308                         ret = -1;
309                         goto out;
310                 }
311
312                 status = unlink_posix_eadb_raw(ea_tdb, smb_fname->base_name, -1);
313                 if (!NT_STATUS_IS_OK(status)) {
314                         tdb_transaction_cancel(ea_tdb->tdb);
315                         ret = -1;
316                         goto out;
317                 }
318         }
319
320         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
321
322         if (ret == -1) {
323                 tdb_transaction_cancel(ea_tdb->tdb);
324                 goto out;
325         } else {
326                 if (tdb_transaction_commit(ea_tdb->tdb) != 0) {
327                         ret = -1;
328                         goto out;
329                 }
330         }
331
332 out:
333         TALLOC_FREE(smb_fname_tmp);
334         return ret;
335 }
336
337 /*
338  * On rmdir we need to delete the tdb record
339  */
340 static int posix_eadb_rmdir(vfs_handle_struct *handle, const char *path)
341 {
342         NTSTATUS status;
343         struct tdb_wrap *ea_tdb;
344         int ret;
345
346         SMB_VFS_HANDLE_GET_DATA(handle, ea_tdb, struct tdb_wrap, return -1);
347
348         if (tdb_transaction_start(ea_tdb->tdb) != 0) {
349                 return -1;
350         }
351
352         status = unlink_posix_eadb_raw(ea_tdb, path, -1);
353         if (!NT_STATUS_IS_OK(status)) {
354                 tdb_transaction_cancel(ea_tdb->tdb);
355         }
356
357         ret = SMB_VFS_NEXT_RMDIR(handle, path);
358
359         if (ret == -1) {
360                 tdb_transaction_cancel(ea_tdb->tdb);
361         } else {
362                 if (tdb_transaction_commit(ea_tdb->tdb) != 0) {
363                         return -1;
364                 }
365         }
366
367         return ret;
368 }
369
370 /*
371  * Destructor for the VFS private data
372  */
373
374 static void close_xattr_db(void **data)
375 {
376         struct tdb_wrap **p_db = (struct tdb_wrap **)data;
377         TALLOC_FREE(*p_db);
378 }
379
380 static int posix_eadb_connect(vfs_handle_struct *handle, const char *service,
381                           const char *user)
382 {
383         char *sname = NULL;
384         int res, snum;
385         struct tdb_wrap *db;
386
387         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
388         if (res < 0) {
389                 return res;
390         }
391
392         snum = find_service(talloc_tos(), service, &sname);
393         if (snum == -1 || sname == NULL) {
394                 /*
395                  * Should not happen, but we should not fail just *here*.
396                  */
397                 return 0;
398         }
399
400         if (!posix_eadb_init(snum, &db)) {
401                 DEBUG(5, ("Could not init xattr tdb\n"));
402                 lp_do_parameter(snum, "ea support", "False");
403                 return 0;
404         }
405
406         lp_do_parameter(snum, "ea support", "True");
407
408         SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
409                                 struct tdb_wrap, return -1);
410
411         return 0;
412 }
413
414 static struct vfs_fn_pointers vfs_posix_eadb_fns = {
415         .getxattr_fn = posix_eadb_getxattr,
416         .fgetxattr_fn = posix_eadb_fgetxattr,
417         .setxattr_fn = posix_eadb_setxattr,
418         .fsetxattr_fn = posix_eadb_fsetxattr,
419         .listxattr_fn = posix_eadb_listxattr,
420         .flistxattr_fn = posix_eadb_flistxattr,
421         .removexattr_fn = posix_eadb_removexattr,
422         .fremovexattr_fn = posix_eadb_fremovexattr,
423         .unlink_fn = posix_eadb_unlink,
424         .rmdir_fn = posix_eadb_rmdir,
425         .connect_fn = posix_eadb_connect,
426 };
427
428 NTSTATUS vfs_posix_eadb_init(void);
429 NTSTATUS vfs_posix_eadb_init(void)
430 {
431         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posix_eadb",
432                                 &vfs_posix_eadb_fns);
433 }