VFS: Remove SMB_VFS_LISTXATTR, no longer used
[samba.git] / source3 / modules / vfs_xattr_tdb.c
1 /*
2  * Store posix-level xattrs in a tdb
3  *
4  * Copyright (C) Volker Lendecke, 2007
5  * Copyright (C) Andrew Bartlett, 2012
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 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "dbwrap/dbwrap.h"
25 #include "dbwrap/dbwrap_open.h"
26 #include "source3/lib/xattr_tdb.h"
27 #include "lib/util/tevent_unix.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
31
32 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db);
33
34 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
35                                 const char *path, struct file_id *id)
36 {
37         int ret;
38         TALLOC_CTX *frame = talloc_stackframe();
39         struct smb_filename *smb_fname;
40
41         smb_fname = synthetic_smb_fname(frame,
42                                         path,
43                                         NULL,
44                                         NULL,
45                                         0,
46                                         0);
47         if (smb_fname == NULL) {
48                 TALLOC_FREE(frame);
49                 errno = ENOMEM;
50                 return -1;
51         }
52
53         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
54
55         if (ret == -1) {
56                 TALLOC_FREE(frame); 
57                 return -1;
58         }
59
60         *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
61         TALLOC_FREE(frame);
62         return 0;
63 }
64
65 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
66                                 const struct smb_filename *smb_fname,
67                                 const char *name,
68                                 void *value,
69                                 size_t size)
70 {
71         struct file_id id;
72         struct db_context *db;
73         ssize_t xattr_size;
74         int ret;
75         DATA_BLOB blob;
76         TALLOC_CTX *frame = talloc_stackframe();
77
78         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
79                                 if (!xattr_tdb_init(-1, frame, &db))
80                                 {
81                                         TALLOC_FREE(frame); return -1;
82                                 });
83
84         ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
85         if (ret == -1) {
86                 TALLOC_FREE(frame);
87                 return -1;
88         }
89
90         xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
91         if (xattr_size < 0) {
92                 errno = ENOATTR;
93                 TALLOC_FREE(frame);
94                 return -1;
95         }
96
97         if (size == 0) {
98                 TALLOC_FREE(frame);
99                 return xattr_size;
100         }
101
102         if (blob.length > size) {
103                 TALLOC_FREE(frame);
104                 errno = ERANGE;
105                 return -1;
106         }
107         memcpy(value, blob.data, xattr_size);
108         TALLOC_FREE(frame);
109         return xattr_size;
110 }
111
112 struct xattr_tdb_getxattrat_state {
113         struct vfs_aio_state vfs_aio_state;
114         ssize_t xattr_size;
115         uint8_t *xattr_value;
116 };
117
118 static struct tevent_req *xattr_tdb_getxattrat_send(
119                         TALLOC_CTX *mem_ctx,
120                         struct tevent_context *ev,
121                         struct vfs_handle_struct *handle,
122                         files_struct *dir_fsp,
123                         const struct smb_filename *smb_fname,
124                         const char *xattr_name,
125                         size_t alloc_hint)
126 {
127         struct tevent_req *req = NULL;
128         struct xattr_tdb_getxattrat_state *state = NULL;
129         struct smb_filename *cwd = NULL;
130         struct db_context *db = NULL;
131         struct file_id id;
132         int ret;
133         int error;
134         int cwd_ret;
135         DATA_BLOB xattr_blob;
136
137         req = tevent_req_create(mem_ctx, &state,
138                                 struct xattr_tdb_getxattrat_state);
139         if (req == NULL) {
140                 return NULL;
141         }
142         state->xattr_size = -1;
143
144         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
145                                 if (!xattr_tdb_init(-1, state, &db)) {
146                                         tevent_req_error(req, EIO);
147                                         return tevent_req_post(req, ev);
148                                 });
149
150         cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
151         if (tevent_req_nomem(cwd, req)) {
152                 return tevent_req_post(req, ev);
153         }
154
155         ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
156         if (ret != 0) {
157                 tevent_req_error(req, errno);
158                 return tevent_req_post(req, ev);
159         }
160
161         ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
162         error = errno;
163
164         cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
165         SMB_ASSERT(cwd_ret == 0);
166
167         if (ret == -1) {
168                 tevent_req_error(req, error);
169                 return tevent_req_post(req, ev);
170         }
171
172         state->xattr_size = xattr_tdb_getattr(db,
173                                               state,
174                                               &id,
175                                               xattr_name,
176                                               &xattr_blob);
177         if (state->xattr_size == -1) {
178                 tevent_req_error(req, errno);
179                 return tevent_req_post(req, ev);
180         }
181
182         if (alloc_hint == 0) {
183                 /*
184                  * The caller only wants to know the size.
185                  */
186                 tevent_req_done(req);
187                 return tevent_req_post(req, ev);
188         }
189
190         if (state->xattr_size == 0) {
191                 /*
192                  * There's no data.
193                  */
194                 tevent_req_done(req);
195                 return tevent_req_post(req, ev);
196         }
197
198         if (xattr_blob.length > alloc_hint) {
199                 /*
200                  * The data doesn't fit.
201                  */
202                 state->xattr_size = -1;
203                 tevent_req_error(req, ERANGE);
204                 return tevent_req_post(req, ev);
205         }
206
207         /*
208          * take the whole blob.
209          */
210         state->xattr_value = xattr_blob.data;
211
212         tevent_req_done(req);
213         return tevent_req_post(req, ev);
214 }
215
216 static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
217                                          struct vfs_aio_state *aio_state,
218                                          TALLOC_CTX *mem_ctx,
219                                          uint8_t **xattr_value)
220 {
221         struct xattr_tdb_getxattrat_state *state = tevent_req_data(
222                 req, struct xattr_tdb_getxattrat_state);
223         ssize_t xattr_size;
224
225         if (tevent_req_is_unix_error(req, &aio_state->error)) {
226                 tevent_req_received(req);
227                 return -1;
228         }
229
230         *aio_state = state->vfs_aio_state;
231         xattr_size = state->xattr_size;
232         if (xattr_value != NULL) {
233                 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
234         }
235
236         tevent_req_received(req);
237         return xattr_size;
238 }
239
240 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
241                                    struct files_struct *fsp,
242                                    const char *name, void *value, size_t size)
243 {
244         SMB_STRUCT_STAT sbuf;
245         struct file_id id;
246         struct db_context *db;
247         ssize_t xattr_size;
248         DATA_BLOB blob;
249         TALLOC_CTX *frame = talloc_stackframe();
250
251         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
252                                 if (!xattr_tdb_init(-1, frame, &db))
253                                 {
254                                         TALLOC_FREE(frame); return -1;
255                                 });
256
257         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
258                 TALLOC_FREE(frame);
259                 return -1;
260         }
261
262         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
263
264         xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
265         if (xattr_size < 0) {
266                 errno = ENOATTR;
267                 TALLOC_FREE(frame);
268                 return -1;
269         }
270
271         if (size == 0) {
272                 TALLOC_FREE(frame);
273                 return xattr_size;
274         }
275
276         if (blob.length > size) {
277                 TALLOC_FREE(frame);
278                 errno = ERANGE;
279                 return -1;
280         }
281         memcpy(value, blob.data, xattr_size);
282         TALLOC_FREE(frame);
283         return xattr_size;
284 }
285
286 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
287                                 const struct smb_filename *smb_fname,
288                                 const char *name,
289                                 const void *value,
290                                 size_t size,
291                                 int flags)
292 {
293         struct file_id id;
294         struct db_context *db;
295         int ret;
296         TALLOC_CTX *frame = talloc_stackframe();
297
298         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
299                                 if (!xattr_tdb_init(-1, frame, &db))
300                                 {
301                                         TALLOC_FREE(frame); return -1;
302                                 });
303
304         ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
305         if (ret == -1) {
306                 TALLOC_FREE(frame);
307                 return -1;
308         }
309
310         ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
311         TALLOC_FREE(frame);
312         return ret;
313 }
314
315 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
316                                struct files_struct *fsp,
317                                const char *name, const void *value,
318                                size_t size, int flags)
319 {
320         SMB_STRUCT_STAT sbuf;
321         struct file_id id;
322         struct db_context *db;
323         int ret;
324         TALLOC_CTX *frame = talloc_stackframe();
325
326         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
327                                 if (!xattr_tdb_init(-1, frame, &db))
328                                 {
329                                         TALLOC_FREE(frame); return -1;
330                                 });
331
332         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
333                 TALLOC_FREE(frame);
334                 return -1;
335         }
336
337         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
338
339         ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
340         TALLOC_FREE(frame);
341         return ret;
342
343 }
344
345 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
346                                     struct files_struct *fsp, char *list,
347                                     size_t size)
348 {
349         SMB_STRUCT_STAT sbuf;
350         struct file_id id;
351         struct db_context *db;
352         int ret;
353         TALLOC_CTX *frame = talloc_stackframe();
354
355         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
356                                 if (!xattr_tdb_init(-1, frame, &db))
357                                 {
358                                         TALLOC_FREE(frame); return -1;
359                                 });
360
361         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
362                 TALLOC_FREE(frame);
363                 return -1;
364         }
365
366         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
367
368         ret = xattr_tdb_listattr(db, &id, list, size);
369         TALLOC_FREE(frame);
370         return ret;
371 }
372
373 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
374                                 const struct smb_filename *smb_fname,
375                                 const char *name)
376 {
377         struct file_id id;
378         struct db_context *db;
379         int ret;
380         TALLOC_CTX *frame = talloc_stackframe();
381
382         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
383                                 if (!xattr_tdb_init(-1, frame, &db))
384                                 {
385                                         TALLOC_FREE(frame); return -1;
386                                 });
387
388         ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
389         if (ret == -1) {
390                 TALLOC_FREE(frame);
391                 return ret;
392         }
393
394         
395         ret = xattr_tdb_removeattr(db, &id, name);
396         TALLOC_FREE(frame);
397         return ret;
398 }
399
400 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
401                                   struct files_struct *fsp, const char *name)
402 {
403         SMB_STRUCT_STAT sbuf;
404         struct file_id id;
405         struct db_context *db;
406         int ret;
407         TALLOC_CTX *frame = talloc_stackframe();
408
409         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
410                                 if (!xattr_tdb_init(-1, frame, &db))
411                                 {
412                                         TALLOC_FREE(frame); return -1;
413                                 });
414
415         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
416                 TALLOC_FREE(frame);
417                 return -1;
418         }
419
420         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
421
422         ret = xattr_tdb_removeattr(db, &id, name);
423         TALLOC_FREE(frame);
424         return ret;
425 }
426
427 /*
428  * Open the tdb file upon VFS_CONNECT
429  */
430
431 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
432 {
433         struct db_context *db;
434         const char *dbname;
435         char *def_dbname;
436
437         def_dbname = state_path(talloc_tos(), "xattr.tdb");
438         if (def_dbname == NULL) {
439                 errno = ENOSYS;
440                 return false;
441         }
442
443         dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
444
445         /* now we know dbname is not NULL */
446
447         become_root();
448         db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
449                      DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
450         unbecome_root();
451
452         if (db == NULL) {
453 #if defined(ENOTSUP)
454                 errno = ENOTSUP;
455 #else
456                 errno = ENOSYS;
457 #endif
458                 TALLOC_FREE(def_dbname);
459                 return false;
460         }
461
462         *p_db = db;
463         TALLOC_FREE(def_dbname);
464         return true;
465 }
466
467 static int xattr_tdb_openat(struct vfs_handle_struct *handle,
468                             const struct files_struct *dirfsp,
469                             const struct smb_filename *smb_fname,
470                             struct files_struct *fsp,
471                             int flags,
472                             mode_t mode)
473 {
474         struct db_context *db = NULL;
475         TALLOC_CTX *frame = NULL;
476         SMB_STRUCT_STAT sbuf;
477         int fd;
478         int ret;
479
480         fd = SMB_VFS_NEXT_OPENAT(handle,
481                                  dirfsp,
482                                  smb_fname,
483                                  fsp,
484                                  flags,
485                                  mode);
486
487         if (fd == -1) {
488                 return -1;
489         }
490
491         if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
492                 return fd;
493         }
494
495         /*
496          * We know we used O_CREAT|O_EXCL and it worked.
497          * We must have created the file.
498          */
499
500         fsp_set_fd(fsp, fd);
501         ret = SMB_VFS_FSTAT(fsp, &sbuf);
502         fsp_set_fd(fsp, -1);
503         if (ret == -1) {
504                 /* Can't happen... */
505                 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
506                             smb_fname_str_dbg(smb_fname),
507                             strerror(errno));
508                 return -1;
509         }
510
511         fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &sbuf);
512
513         frame = talloc_stackframe();
514
515         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
516                                 if (!xattr_tdb_init(-1, frame, &db))
517                                 {
518                                         TALLOC_FREE(frame); return -1;
519                                 });
520
521         xattr_tdb_remove_all_attrs(db, &fsp->file_id);
522
523         TALLOC_FREE(frame);
524         return fd;
525 }
526
527 static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
528                 struct files_struct *dirfsp,
529                 const struct smb_filename *smb_fname,
530                 mode_t mode)
531 {
532         struct db_context *db = NULL;
533         TALLOC_CTX *frame = NULL;
534         struct file_id fileid;
535         int ret;
536         struct smb_filename *full_fname = NULL;
537
538         ret = SMB_VFS_NEXT_MKDIRAT(handle,
539                                 dirfsp,
540                                 smb_fname,
541                                 mode);
542         if (ret < 0) {
543                 return ret;
544         }
545
546         frame = talloc_stackframe();
547
548         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
549                                                   dirfsp,
550                                                   smb_fname);
551         if (full_fname == NULL) {
552                 errno = ENOMEM;
553                 return -1;
554         }
555
556         /* Always use LSTAT here - we just created the directory. */
557         ret = SMB_VFS_LSTAT(handle->conn, full_fname);
558         if (ret == -1) {
559                 /* Rename race. Let upper level take care of it. */
560                 TALLOC_FREE(frame);
561                 return -1;
562         }
563         if (!S_ISDIR(full_fname->st.st_ex_mode)) {
564                 /* Rename race. Let upper level take care of it. */
565                 TALLOC_FREE(frame);
566                 return -1;
567         }
568
569         fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &full_fname->st);
570
571         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
572                                 if (!xattr_tdb_init(-1, frame, &db))
573                                 {
574                                         TALLOC_FREE(frame); return -1;
575                                 });
576
577         xattr_tdb_remove_all_attrs(db, &fileid);
578         TALLOC_FREE(frame);
579         return 0;
580 }
581
582 /*
583  * On unlink we need to delete the tdb record
584  */
585 static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
586                         struct files_struct *dirfsp,
587                         const struct smb_filename *smb_fname,
588                         int flags)
589 {
590         struct smb_filename *smb_fname_tmp = NULL;
591         struct smb_filename *full_fname = NULL;
592         struct file_id id;
593         struct db_context *db;
594         int ret = -1;
595         bool remove_record = false;
596         TALLOC_CTX *frame = talloc_stackframe();
597
598         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
599                                 if (!xattr_tdb_init(-1, frame, &db))
600                                 {
601                                         TALLOC_FREE(frame); return -1;
602                                 });
603
604         smb_fname_tmp = cp_smb_filename(frame, smb_fname);
605         if (smb_fname_tmp == NULL) {
606                 TALLOC_FREE(frame);
607                 errno = ENOMEM;
608                 return -1;
609         }
610
611         /*
612          * TODO: use SMB_VFS_STATX() once we have that
613          */
614
615         full_fname = full_path_from_dirfsp_atname(frame,
616                                                   dirfsp,
617                                                   smb_fname);
618         if (full_fname == NULL) {
619                 goto out;
620         }
621
622         if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
623                 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
624         } else {
625                 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
626         }
627         if (ret == -1) {
628                 goto out;
629         }
630         smb_fname_tmp->st = full_fname->st;
631
632         if (flags & AT_REMOVEDIR) {
633                 /* Always remove record when removing a directory succeeds. */
634                 remove_record = true;
635         } else {
636                 if (smb_fname_tmp->st.st_ex_nlink == 1) {
637                         /* Only remove record on last link to file. */
638                         remove_record = true;
639                 }
640         }
641
642         ret = SMB_VFS_NEXT_UNLINKAT(handle,
643                                 dirfsp,
644                                 smb_fname_tmp,
645                                 flags);
646
647         if (ret == -1) {
648                 goto out;
649         }
650
651         if (!remove_record) {
652                 goto out;
653         }
654
655         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
656
657         xattr_tdb_remove_all_attrs(db, &id);
658
659  out:
660         TALLOC_FREE(frame);
661         return ret;
662 }
663
664 /*
665  * Destructor for the VFS private data
666  */
667
668 static void close_xattr_db(void **data)
669 {
670         struct db_context **p_db = (struct db_context **)data;
671         TALLOC_FREE(*p_db);
672 }
673
674 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
675                           const char *user)
676 {
677         char *sname = NULL;
678         int res, snum;
679         struct db_context *db;
680
681         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
682         if (res < 0) {
683                 return res;
684         }
685
686         snum = find_service(talloc_tos(), service, &sname);
687         if (snum == -1 || sname == NULL) {
688                 /*
689                  * Should not happen, but we should not fail just *here*.
690                  */
691                 return 0;
692         }
693
694         if (!xattr_tdb_init(snum, NULL, &db)) {
695                 DEBUG(5, ("Could not init xattr tdb\n"));
696                 lp_do_parameter(snum, "ea support", "False");
697                 return 0;
698         }
699
700         lp_do_parameter(snum, "ea support", "True");
701
702         SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
703                                 struct db_context, return -1);
704
705         return 0;
706 }
707
708 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
709         .getxattr_fn = xattr_tdb_getxattr,
710         .getxattrat_send_fn = xattr_tdb_getxattrat_send,
711         .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
712         .fgetxattr_fn = xattr_tdb_fgetxattr,
713         .setxattr_fn = xattr_tdb_setxattr,
714         .fsetxattr_fn = xattr_tdb_fsetxattr,
715         .flistxattr_fn = xattr_tdb_flistxattr,
716         .removexattr_fn = xattr_tdb_removexattr,
717         .fremovexattr_fn = xattr_tdb_fremovexattr,
718         .openat_fn = xattr_tdb_openat,
719         .mkdirat_fn = xattr_tdb_mkdirat,
720         .unlinkat_fn = xattr_tdb_unlinkat,
721         .connect_fn = xattr_tdb_connect,
722 };
723
724 static_decl_vfs;
725 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
726 {
727         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
728                                 &vfs_xattr_tdb_fns);
729 }