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