2c7717a508b5f93003ed78465430725816e9e1a3
[sfrench/samba-autobuild/.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.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_EQUAL(status, NT_STATUS_NOT_FOUND)) {
56                 errno = ENOATTR;
57                 return -1;
58         }
59
60         if (!NT_STATUS_IS_OK(status)) {
61                 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
62                            nt_errstr(status)));
63                 errno = EINVAL;
64                 return -1;
65         }
66
67         if (blob.length > size) {
68                 errno = ERANGE;
69                 goto fail;
70         }
71
72         memcpy(value, blob.data, blob.length);
73         result = blob.length;
74
75  fail:
76         return result;
77 }
78
79 static ssize_t posix_eadb_getxattr(struct vfs_handle_struct *handle,
80                                   const char *path, const char *name,
81                                   void *value, size_t size)
82 {
83         struct tdb_wrap *db;
84
85         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
86
87         return posix_eadb_getattr(db, path, -1, name, value, size);
88 }
89
90 static ssize_t posix_eadb_fgetxattr(struct vfs_handle_struct *handle,
91                                    struct files_struct *fsp,
92                                    const char *name, void *value, size_t size)
93 {
94         struct tdb_wrap *db;
95
96         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
97
98         return posix_eadb_getattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name, value, size);
99 }
100
101 /*
102  * Worker routine for setxattr and fsetxattr
103  */
104
105 static int posix_eadb_setattr(struct tdb_wrap *db_ctx,
106                              const char *fname, int fd, const char *name,
107                              const void *value, size_t size, int flags)
108 {
109         NTSTATUS status;
110         DATA_BLOB data = data_blob_const(value, size);
111
112         DEBUG(10, ("posix_eadb_setattr called for file %s/fd %d, name %s\n",
113                    fname, fd, name));
114
115         status = push_xattr_blob_tdb_raw(db_ctx, name, fname, fd, &data);
116
117         if (!NT_STATUS_IS_OK(status)) {
118                 DEBUG(10, ("push_xattr_blob_tdb_raw failed: %s\n",
119                            nt_errstr(status)));
120                 return -1;
121         }
122
123         return 0;
124 }
125
126 static int posix_eadb_setxattr(struct vfs_handle_struct *handle,
127                               const char *path, const char *name,
128                               const void *value, size_t size, int flags)
129 {
130         struct tdb_wrap *db;
131
132         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
133
134         return posix_eadb_setattr(db, path, -1, name, value, size, flags);
135 }
136
137 static int posix_eadb_fsetxattr(struct vfs_handle_struct *handle,
138                                struct files_struct *fsp,
139                                const char *name, const void *value,
140                                size_t size, int flags)
141 {
142         struct tdb_wrap *db;
143
144         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
145
146         return posix_eadb_setattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name, value, size, flags);
147 }
148
149 /*
150  * Worker routine for listxattr and flistxattr
151  */
152
153 static ssize_t posix_eadb_listattr(struct tdb_wrap *db_ctx,
154                                   const char *fname, int fd, char *list,
155                                   size_t size)
156 {
157         DATA_BLOB blob;
158         NTSTATUS status;
159
160         status = list_posix_eadb_raw(db_ctx, talloc_tos(), fname, fd, &blob);
161
162         if (!NT_STATUS_IS_OK(status)) {
163                 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
164                            nt_errstr(status)));
165                 errno = EINVAL;
166                 return -1;
167         }
168
169         if (blob.length > size) {
170                 errno = ERANGE;
171                 TALLOC_FREE(blob.data);
172                 return -1;
173         }
174
175         memcpy(list, blob.data, blob.length);
176
177         TALLOC_FREE(blob.data);
178         return blob.length;
179 }
180
181 static ssize_t posix_eadb_listxattr(struct vfs_handle_struct *handle,
182                                    const char *path, char *list, size_t size)
183 {
184         struct tdb_wrap *db;
185
186         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
187
188         return posix_eadb_listattr(db, path, -1, list, size);
189 }
190
191 static ssize_t posix_eadb_flistxattr(struct vfs_handle_struct *handle,
192                                     struct files_struct *fsp, char *list,
193                                     size_t size)
194 {
195         struct tdb_wrap *db;
196
197         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
198
199         return posix_eadb_listattr(db, fsp->fsp_name->base_name, fsp->fh->fd, list, size);
200 }
201
202 /*
203  * Worker routine for removexattr and fremovexattr
204  */
205
206 static int posix_eadb_removeattr(struct tdb_wrap *db_ctx,
207                                 const char *fname, int fd, const char *name)
208 {
209         NTSTATUS status;
210
211         status = delete_posix_eadb_raw(db_ctx, name, fname, fd);
212
213         if (!NT_STATUS_IS_OK(status)) {
214                 DEBUG(10, ("delete_posix_eadb_raw failed: %s\n",
215                            nt_errstr(status)));
216                 return -1;
217         }
218         return 0;
219 }
220
221 static int posix_eadb_removexattr(struct vfs_handle_struct *handle,
222                                  const char *path, const char *name)
223 {
224         struct tdb_wrap *db;
225
226         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
227
228         return posix_eadb_removeattr(db, path, -1, name);
229 }
230
231 static int posix_eadb_fremovexattr(struct vfs_handle_struct *handle,
232                                   struct files_struct *fsp, const char *name)
233 {
234         struct tdb_wrap *db;
235
236         SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
237
238         return posix_eadb_removeattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name);
239 }
240
241 /*
242  * Open the tdb file upon VFS_CONNECT
243  */
244
245 static bool posix_eadb_init(int snum, struct tdb_wrap **p_db)
246 {
247         struct tdb_wrap *db;
248         struct loadparm_context *lp_ctx;
249         const char *eadb = lp_parm_const_string(snum, "posix", "eadb", NULL);
250
251         if (!eadb) {
252                 DEBUG(0, ("Can not use vfs_posix_eadb without posix:eadb set\n"));
253                 return false;
254         }
255
256         lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
257
258         become_root();
259         db = tdb_wrap_open(NULL, eadb, 50000,
260                            lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT),
261                            O_RDWR|O_CREAT, 0600);
262
263         unbecome_root();
264         talloc_unlink(NULL, lp_ctx);
265         /* now we know dbname is not NULL */
266
267         if (db == NULL) {
268 #if defined(ENOTSUP)
269                 errno = ENOTSUP;
270 #else
271                 errno = ENOSYS;
272 #endif
273                 return false;
274         }
275
276         *p_db = db;
277         return true;
278 }
279
280 /*
281  * On unlink we need to delete the tdb record
282  */
283 static int posix_eadb_unlink(vfs_handle_struct *handle,
284                             const struct smb_filename *smb_fname)
285 {
286         struct smb_filename *smb_fname_tmp = NULL;
287         int ret = -1;
288
289         struct tdb_wrap *ea_tdb;
290
291         SMB_VFS_HANDLE_GET_DATA(handle, ea_tdb, struct tdb_wrap, return -1);
292
293         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
294         if (smb_fname_tmp == NULL) {
295                 errno = ENOMEM;
296                 return -1;
297         }
298
299         if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
300                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
301         } else {
302                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
303         }
304         if (ret == -1) {
305                 goto out;
306         }
307
308         if (smb_fname_tmp->st.st_ex_nlink == 1) {
309                 NTSTATUS status;
310
311                 /* Only remove record on last link to file. */
312
313                 if (tdb_transaction_start(ea_tdb->tdb) != 0) {
314                         ret = -1;
315                         goto out;
316                 }
317
318                 status = unlink_posix_eadb_raw(ea_tdb, smb_fname->base_name, -1);
319                 if (!NT_STATUS_IS_OK(status)) {
320                         tdb_transaction_cancel(ea_tdb->tdb);
321                         ret = -1;
322                         goto out;
323                 }
324         }
325
326         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
327
328         if (ret == -1) {
329                 tdb_transaction_cancel(ea_tdb->tdb);
330                 goto out;
331         } else {
332                 if (tdb_transaction_commit(ea_tdb->tdb) != 0) {
333                         ret = -1;
334                         goto out;
335                 }
336         }
337
338 out:
339         TALLOC_FREE(smb_fname_tmp);
340         return ret;
341 }
342
343 /*
344  * On rmdir we need to delete the tdb record
345  */
346 static int posix_eadb_rmdir(vfs_handle_struct *handle,
347                         const struct smb_filename *smb_fname)
348 {
349         NTSTATUS status;
350         struct tdb_wrap *ea_tdb;
351         int ret;
352         const char *path = smb_fname->base_name;
353
354         SMB_VFS_HANDLE_GET_DATA(handle, ea_tdb, struct tdb_wrap, return -1);
355
356         if (tdb_transaction_start(ea_tdb->tdb) != 0) {
357                 return -1;
358         }
359
360         status = unlink_posix_eadb_raw(ea_tdb, path, -1);
361         if (!NT_STATUS_IS_OK(status)) {
362                 tdb_transaction_cancel(ea_tdb->tdb);
363         }
364
365         ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
366
367         if (ret == -1) {
368                 tdb_transaction_cancel(ea_tdb->tdb);
369         } else {
370                 if (tdb_transaction_commit(ea_tdb->tdb) != 0) {
371                         return -1;
372                 }
373         }
374
375         return ret;
376 }
377
378 /*
379  * Destructor for the VFS private data
380  */
381
382 static void close_xattr_db(void **data)
383 {
384         struct tdb_wrap **p_db = (struct tdb_wrap **)data;
385         TALLOC_FREE(*p_db);
386 }
387
388 static int posix_eadb_connect(vfs_handle_struct *handle, const char *service,
389                           const char *user)
390 {
391         char *sname = NULL;
392         int res, snum;
393         struct tdb_wrap *db;
394
395         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
396         if (res < 0) {
397                 return res;
398         }
399
400         snum = find_service(talloc_tos(), service, &sname);
401         if (snum == -1 || sname == NULL) {
402                 /*
403                  * Should not happen, but we should not fail just *here*.
404                  */
405                 return 0;
406         }
407
408         if (!posix_eadb_init(snum, &db)) {
409                 DEBUG(5, ("Could not init xattr tdb\n"));
410                 lp_do_parameter(snum, "ea support", "False");
411                 return 0;
412         }
413
414         lp_do_parameter(snum, "ea support", "True");
415
416         SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
417                                 struct tdb_wrap, return -1);
418
419         return 0;
420 }
421
422 static struct vfs_fn_pointers vfs_posix_eadb_fns = {
423         .getxattr_fn = posix_eadb_getxattr,
424         .fgetxattr_fn = posix_eadb_fgetxattr,
425         .setxattr_fn = posix_eadb_setxattr,
426         .fsetxattr_fn = posix_eadb_fsetxattr,
427         .listxattr_fn = posix_eadb_listxattr,
428         .flistxattr_fn = posix_eadb_flistxattr,
429         .removexattr_fn = posix_eadb_removexattr,
430         .fremovexattr_fn = posix_eadb_fremovexattr,
431         .unlink_fn = posix_eadb_unlink,
432         .rmdir_fn = posix_eadb_rmdir,
433         .connect_fn = posix_eadb_connect,
434 };
435
436 static_decl_vfs;
437 NTSTATUS vfs_posix_eadb_init(void)
438 {
439         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posix_eadb",
440                                 &vfs_posix_eadb_fns);
441 }