s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / source3 / modules / vfs_xattr_tdb.c
blob447d868924d1042d7b93f45a271b3c2984ff81a5
1 /*
2 * Store posix-level xattrs in a tdb
4 * Copyright (C) Volker Lendecke, 2007
5 * Copyright (C) Andrew Bartlett, 2012
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.
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.
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/>.
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"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 struct xattr_tdb_config {
33 struct db_context *db;
34 bool ignore_user_xattr;
37 static bool xattr_tdb_init(struct vfs_handle_struct *handle,
38 struct xattr_tdb_config **_config);
40 static bool is_user_xattr(const char *xattr_name)
42 int match;
44 match = strncmp(xattr_name, "user.", strlen("user."));
45 return (match == 0);
48 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
49 const char *path, struct file_id *id)
51 int ret;
52 TALLOC_CTX *frame = talloc_stackframe();
53 struct smb_filename *smb_fname;
55 smb_fname = synthetic_smb_fname(frame,
56 path,
57 NULL,
58 NULL,
60 0);
61 if (smb_fname == NULL) {
62 TALLOC_FREE(frame);
63 errno = ENOMEM;
64 return -1;
67 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
69 if (ret == -1) {
70 TALLOC_FREE(frame);
71 return -1;
74 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
75 TALLOC_FREE(frame);
76 return 0;
79 struct xattr_tdb_getxattrat_state {
80 struct vfs_aio_state vfs_aio_state;
81 ssize_t xattr_size;
82 uint8_t *xattr_value;
85 static void xattr_tdb_getxattrat_done(struct tevent_req *subreq);
87 static struct tevent_req *xattr_tdb_getxattrat_send(
88 TALLOC_CTX *mem_ctx,
89 struct tevent_context *ev,
90 struct vfs_handle_struct *handle,
91 files_struct *dir_fsp,
92 const struct smb_filename *smb_fname,
93 const char *xattr_name,
94 size_t alloc_hint)
96 struct xattr_tdb_config *config = NULL;
97 struct tevent_req *req = NULL;
98 struct tevent_req *subreq = NULL;
99 struct xattr_tdb_getxattrat_state *state = NULL;
100 struct smb_filename *cwd = NULL;
101 struct file_id id;
102 int ret;
103 int error;
104 int cwd_ret;
105 DATA_BLOB xattr_blob;
107 if (!xattr_tdb_init(handle, &config)) {
108 return NULL;
111 req = tevent_req_create(mem_ctx, &state,
112 struct xattr_tdb_getxattrat_state);
113 if (req == NULL) {
114 return NULL;
116 state->xattr_size = -1;
118 if (config->ignore_user_xattr && is_user_xattr(xattr_name)) {
119 subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
121 handle,
122 dir_fsp,
123 smb_fname,
124 xattr_name,
125 alloc_hint);
126 if (tevent_req_nomem(subreq, req)) {
127 return tevent_req_post(req, ev);
129 tevent_req_set_callback(subreq, xattr_tdb_getxattrat_done, req);
130 return req;
133 cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
134 if (tevent_req_nomem(cwd, req)) {
135 return tevent_req_post(req, ev);
138 ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
139 if (ret != 0) {
140 tevent_req_error(req, errno);
141 return tevent_req_post(req, ev);
144 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
145 error = errno;
147 cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
148 SMB_ASSERT(cwd_ret == 0);
150 if (ret == -1) {
151 tevent_req_error(req, error);
152 return tevent_req_post(req, ev);
155 state->xattr_size = xattr_tdb_getattr(config->db,
156 state,
157 &id,
158 xattr_name,
159 &xattr_blob);
160 if (state->xattr_size == -1) {
161 tevent_req_error(req, errno);
162 return tevent_req_post(req, ev);
165 if (alloc_hint == 0) {
167 * The caller only wants to know the size.
169 tevent_req_done(req);
170 return tevent_req_post(req, ev);
173 if (state->xattr_size == 0) {
175 * There's no data.
177 tevent_req_done(req);
178 return tevent_req_post(req, ev);
181 if (xattr_blob.length > alloc_hint) {
183 * The data doesn't fit.
185 state->xattr_size = -1;
186 tevent_req_error(req, ERANGE);
187 return tevent_req_post(req, ev);
191 * take the whole blob.
193 state->xattr_value = xattr_blob.data;
195 tevent_req_done(req);
196 return tevent_req_post(req, ev);
199 static void xattr_tdb_getxattrat_done(struct tevent_req *subreq)
201 struct tevent_req *req = tevent_req_callback_data(
202 subreq, struct tevent_req);
203 struct xattr_tdb_getxattrat_state *state = tevent_req_data(
204 req, struct xattr_tdb_getxattrat_state);
206 state->xattr_size = SMB_VFS_NEXT_GETXATTRAT_RECV(subreq,
207 &state->vfs_aio_state,
208 state,
209 &state->xattr_value);
210 TALLOC_FREE(subreq);
211 if (state->xattr_size == -1) {
212 tevent_req_error(req, state->vfs_aio_state.error);
213 return;
216 tevent_req_done(req);
220 static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
221 struct vfs_aio_state *aio_state,
222 TALLOC_CTX *mem_ctx,
223 uint8_t **xattr_value)
225 struct xattr_tdb_getxattrat_state *state = tevent_req_data(
226 req, struct xattr_tdb_getxattrat_state);
227 ssize_t xattr_size;
229 if (tevent_req_is_unix_error(req, &aio_state->error)) {
230 tevent_req_received(req);
231 return -1;
234 *aio_state = state->vfs_aio_state;
235 xattr_size = state->xattr_size;
236 if (xattr_value != NULL) {
237 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
240 tevent_req_received(req);
241 return xattr_size;
244 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
245 struct files_struct *fsp,
246 const char *name, void *value, size_t size)
248 struct xattr_tdb_config *config = NULL;
249 SMB_STRUCT_STAT sbuf;
250 struct file_id id;
251 ssize_t xattr_size;
252 DATA_BLOB blob;
253 TALLOC_CTX *frame = NULL;
255 if (!xattr_tdb_init(handle, &config)) {
256 return -1;
259 if (config->ignore_user_xattr && is_user_xattr(name)) {
260 return SMB_VFS_NEXT_FGETXATTR(
261 handle, fsp, name, value, size);
264 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
265 return -1;
268 frame = talloc_stackframe();
270 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
272 xattr_size = xattr_tdb_getattr(config->db, frame, &id, name, &blob);
273 if (xattr_size < 0) {
274 errno = ENOATTR;
275 TALLOC_FREE(frame);
276 return -1;
279 if (size == 0) {
280 TALLOC_FREE(frame);
281 return xattr_size;
284 if (blob.length > size) {
285 TALLOC_FREE(frame);
286 errno = ERANGE;
287 return -1;
289 memcpy(value, blob.data, xattr_size);
290 TALLOC_FREE(frame);
291 return xattr_size;
294 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
295 struct files_struct *fsp,
296 const char *name, const void *value,
297 size_t size, int flags)
299 struct xattr_tdb_config *config = NULL;
300 SMB_STRUCT_STAT sbuf;
301 struct file_id id;
302 int ret;
304 if (!xattr_tdb_init(handle, &config)) {
305 return -1;
308 if (config->ignore_user_xattr && is_user_xattr(name)) {
309 return SMB_VFS_NEXT_FSETXATTR(
310 handle, fsp, name, value, size, flags);
313 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
314 return -1;
317 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
319 ret = xattr_tdb_setattr(config->db, &id, name, value, size, flags);
320 return ret;
324 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
325 struct files_struct *fsp, char *list,
326 size_t size)
328 struct xattr_tdb_config *config = NULL;
329 SMB_STRUCT_STAT sbuf;
330 struct file_id id;
331 ssize_t backend_size;
332 ssize_t ret;
334 if (!xattr_tdb_init(handle, &config)) {
335 return -1;
338 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
339 return -1;
342 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
344 ret = xattr_tdb_listattr(config->db, &id, list, size);
345 if (ret == -1) {
346 return -1;
348 if (ret == size) {
349 return ret;
351 if (!config->ignore_user_xattr) {
352 return ret;
354 SMB_ASSERT(ret < size);
356 backend_size = SMB_VFS_NEXT_FLISTXATTR(
357 handle, fsp, list + ret, size - ret);
358 if (backend_size == -1) {
359 return -1;
362 return ret + backend_size;
365 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
366 struct files_struct *fsp, const char *name)
368 struct xattr_tdb_config *config = NULL;
369 SMB_STRUCT_STAT sbuf;
370 struct file_id id;
372 if (!xattr_tdb_init(handle, &config)) {
373 return -1;
376 if (config->ignore_user_xattr && is_user_xattr(name)) {
377 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
380 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
381 return -1;
384 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
386 return xattr_tdb_removeattr(config->db, &id, name);
390 * Destructor for the VFS private data
393 static void config_destructor(void **data)
395 struct xattr_tdb_config **config = (struct xattr_tdb_config **)data;
396 TALLOC_FREE((*config)->db);
400 * Open the tdb file upon VFS_CONNECT
403 static bool xattr_tdb_init(struct vfs_handle_struct *handle,
404 struct xattr_tdb_config **_config)
406 struct xattr_tdb_config *config = NULL;
407 const char *dbname;
408 char *def_dbname;
410 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
411 SMB_VFS_HANDLE_GET_DATA(handle, config, struct xattr_tdb_config,
412 return false);
413 if (_config != NULL) {
414 *_config = config;
416 return true;
419 config = talloc_zero(handle->conn, struct xattr_tdb_config);
420 if (config == NULL) {
421 errno = ENOMEM;
422 goto error;
425 def_dbname = state_path(talloc_tos(), "xattr.tdb");
426 if (def_dbname == NULL) {
427 errno = ENOSYS;
428 goto error;
431 dbname = lp_parm_const_string(SNUM(handle->conn),
432 "xattr_tdb",
433 "file",
434 def_dbname);
436 /* now we know dbname is not NULL */
438 become_root();
439 config->db = db_open(handle, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
440 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
441 unbecome_root();
443 if (config->db == NULL) {
444 #if defined(ENOTSUP)
445 errno = ENOTSUP;
446 #else
447 errno = ENOSYS;
448 #endif
449 TALLOC_FREE(def_dbname);
450 goto error;
452 TALLOC_FREE(def_dbname);
454 config->ignore_user_xattr = lp_parm_bool(
455 SNUM(handle->conn), "xattr_tdb", "ignore_user_xattr", false);
457 SMB_VFS_HANDLE_SET_DATA(handle, config, config_destructor,
458 struct xattr_tdb_config, return false);
460 if (_config != NULL) {
461 *_config = config;
463 return true;
465 error:
466 DBG_WARNING("Failed to initialize config: %s\n", strerror(errno));
467 lp_do_parameter(SNUM(handle->conn), "ea support", "False");
468 return false;
471 static int xattr_tdb_openat(struct vfs_handle_struct *handle,
472 const struct files_struct *dirfsp,
473 const struct smb_filename *smb_fname,
474 struct files_struct *fsp,
475 const struct vfs_open_how *how)
477 struct xattr_tdb_config *config = NULL;
478 SMB_STRUCT_STAT sbuf;
479 int fd;
480 int ret;
482 if (!xattr_tdb_init(handle, &config)) {
483 return -1;
486 fd = SMB_VFS_NEXT_OPENAT(handle,
487 dirfsp,
488 smb_fname,
489 fsp,
490 how);
491 if (fd == -1) {
492 return -1;
495 if ((how->flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
496 return fd;
500 * We know we used O_CREAT|O_EXCL and it worked.
501 * We must have created the file.
504 fsp_set_fd(fsp, fd);
505 ret = SMB_VFS_FSTAT(fsp, &sbuf);
506 fsp_set_fd(fsp, -1);
507 if (ret == -1) {
508 /* Can't happen... */
509 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
510 smb_fname_str_dbg(smb_fname),
511 strerror(errno));
512 return -1;
515 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &sbuf);
517 xattr_tdb_remove_all_attrs(config->db, &fsp->file_id);
519 return fd;
522 static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
523 struct files_struct *dirfsp,
524 const struct smb_filename *smb_fname,
525 mode_t mode)
527 struct xattr_tdb_config *config = NULL;
528 struct file_id fileid;
529 struct stat_ex sbuf = { .st_ex_nlink = 0, };
530 int ret;
532 if (!xattr_tdb_init(handle, &config)) {
533 return -1;
536 ret = SMB_VFS_NEXT_MKDIRAT(handle,
537 dirfsp,
538 smb_fname,
539 mode);
540 if (ret < 0) {
541 return ret;
544 ret = SMB_VFS_NEXT_FSTATAT(
545 handle, dirfsp, smb_fname, &sbuf, AT_SYMLINK_NOFOLLOW);
547 if (ret == -1) {
548 /* Rename race. Let upper level take care of it. */
549 return -1;
551 if (!S_ISDIR(sbuf.st_ex_mode)) {
552 /* Rename race. Let upper level take care of it. */
553 return -1;
556 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
558 xattr_tdb_remove_all_attrs(config->db, &fileid);
559 return 0;
563 * On unlink we need to delete the tdb record
565 static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
566 struct files_struct *dirfsp,
567 const struct smb_filename *smb_fname,
568 int flags)
570 struct xattr_tdb_config *config = NULL;
571 struct smb_filename *smb_fname_tmp = NULL;
572 struct smb_filename *full_fname = NULL;
573 struct file_id id;
574 int ret = -1;
575 bool remove_record = false;
576 TALLOC_CTX *frame = NULL;
578 if (!xattr_tdb_init(handle, &config)) {
579 return -1;
582 frame = talloc_stackframe();
584 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
585 if (smb_fname_tmp == NULL) {
586 TALLOC_FREE(frame);
587 errno = ENOMEM;
588 return -1;
592 * TODO: use SMB_VFS_STATX() once we have that
595 full_fname = full_path_from_dirfsp_atname(frame,
596 dirfsp,
597 smb_fname);
598 if (full_fname == NULL) {
599 goto out;
602 if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
603 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
604 } else {
605 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
606 if (ret == -1 && (errno == ENOENT || errno == ELOOP)) {
607 if (VALID_STAT(smb_fname->st) &&
608 S_ISLNK(smb_fname->st.st_ex_mode)) {
610 * Original name was a link - Could be
611 * trying to remove a dangling symlink.
613 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
617 if (ret == -1) {
618 goto out;
620 smb_fname_tmp->st = full_fname->st;
622 if (flags & AT_REMOVEDIR) {
623 /* Always remove record when removing a directory succeeds. */
624 remove_record = true;
625 } else {
626 if (smb_fname_tmp->st.st_ex_nlink == 1) {
627 /* Only remove record on last link to file. */
628 remove_record = true;
632 ret = SMB_VFS_NEXT_UNLINKAT(handle,
633 dirfsp,
634 smb_fname_tmp,
635 flags);
637 if (ret == -1) {
638 goto out;
641 if (!remove_record) {
642 goto out;
645 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
647 xattr_tdb_remove_all_attrs(config->db, &id);
649 out:
650 TALLOC_FREE(frame);
651 return ret;
654 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
655 const char *user)
657 char *sname = NULL;
658 int res, snum;
660 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
661 if (res < 0) {
662 return res;
665 snum = find_service(talloc_tos(), service, &sname);
666 if (snum == -1 || sname == NULL) {
668 * Should not happen, but we should not fail just *here*.
670 return 0;
673 if (!xattr_tdb_init(handle, NULL)) {
674 DEBUG(5, ("Could not init xattr tdb\n"));
675 lp_do_parameter(snum, "ea support", "False");
676 return 0;
679 lp_do_parameter(snum, "ea support", "True");
681 return 0;
684 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
685 .getxattrat_send_fn = xattr_tdb_getxattrat_send,
686 .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
687 .fgetxattr_fn = xattr_tdb_fgetxattr,
688 .fsetxattr_fn = xattr_tdb_fsetxattr,
689 .flistxattr_fn = xattr_tdb_flistxattr,
690 .fremovexattr_fn = xattr_tdb_fremovexattr,
691 .openat_fn = xattr_tdb_openat,
692 .mkdirat_fn = xattr_tdb_mkdirat,
693 .unlinkat_fn = xattr_tdb_unlinkat,
694 .connect_fn = xattr_tdb_connect,
697 static_decl_vfs;
698 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
700 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
701 &vfs_xattr_tdb_fns);