s3:libsmb: allow store_cldap_reply() to work with a ipv6 response
[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 struct xattr_tdb_config {
33         struct db_context *db;
34         bool ignore_user_xattr;
35 };
36
37 static bool xattr_tdb_init(struct vfs_handle_struct *handle,
38                            struct xattr_tdb_config **_config);
39
40 static bool is_user_xattr(const char *xattr_name)
41 {
42         int match;
43
44         match = strncmp(xattr_name, "user.", strlen("user."));
45         return (match == 0);
46 }
47
48 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
49                                 const char *path, struct file_id *id)
50 {
51         int ret;
52         TALLOC_CTX *frame = talloc_stackframe();
53         struct smb_filename *smb_fname;
54
55         smb_fname = synthetic_smb_fname(frame,
56                                         path,
57                                         NULL,
58                                         NULL,
59                                         0,
60                                         0);
61         if (smb_fname == NULL) {
62                 TALLOC_FREE(frame);
63                 errno = ENOMEM;
64                 return -1;
65         }
66
67         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
68
69         if (ret == -1) {
70                 TALLOC_FREE(frame); 
71                 return -1;
72         }
73
74         *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
75         TALLOC_FREE(frame);
76         return 0;
77 }
78
79 struct xattr_tdb_getxattrat_state {
80         struct vfs_aio_state vfs_aio_state;
81         ssize_t xattr_size;
82         uint8_t *xattr_value;
83 };
84
85 static void xattr_tdb_getxattrat_done(struct tevent_req *subreq);
86
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)
95 {
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;
106
107         if (!xattr_tdb_init(handle, &config)) {
108                 return NULL;
109         }
110
111         req = tevent_req_create(mem_ctx, &state,
112                                 struct xattr_tdb_getxattrat_state);
113         if (req == NULL) {
114                 return NULL;
115         }
116         state->xattr_size = -1;
117
118         if (config->ignore_user_xattr && is_user_xattr(xattr_name)) {
119                 subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
120                                                       ev,
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);
128                 }
129                 tevent_req_set_callback(subreq, xattr_tdb_getxattrat_done, req);
130                 return req;
131         }
132
133         cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
134         if (tevent_req_nomem(cwd, req)) {
135                 return tevent_req_post(req, ev);
136         }
137
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);
142         }
143
144         ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
145         error = errno;
146
147         cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
148         SMB_ASSERT(cwd_ret == 0);
149
150         if (ret == -1) {
151                 tevent_req_error(req, error);
152                 return tevent_req_post(req, ev);
153         }
154
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);
163         }
164
165         if (alloc_hint == 0) {
166                 /*
167                  * The caller only wants to know the size.
168                  */
169                 tevent_req_done(req);
170                 return tevent_req_post(req, ev);
171         }
172
173         if (state->xattr_size == 0) {
174                 /*
175                  * There's no data.
176                  */
177                 tevent_req_done(req);
178                 return tevent_req_post(req, ev);
179         }
180
181         if (xattr_blob.length > alloc_hint) {
182                 /*
183                  * The data doesn't fit.
184                  */
185                 state->xattr_size = -1;
186                 tevent_req_error(req, ERANGE);
187                 return tevent_req_post(req, ev);
188         }
189
190         /*
191          * take the whole blob.
192          */
193         state->xattr_value = xattr_blob.data;
194
195         tevent_req_done(req);
196         return tevent_req_post(req, ev);
197 }
198
199 static void xattr_tdb_getxattrat_done(struct tevent_req *subreq)
200 {
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);
205
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;
214         }
215
216         tevent_req_done(req);
217 }
218
219
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)
224 {
225         struct xattr_tdb_getxattrat_state *state = tevent_req_data(
226                 req, struct xattr_tdb_getxattrat_state);
227         ssize_t xattr_size;
228
229         if (tevent_req_is_unix_error(req, &aio_state->error)) {
230                 tevent_req_received(req);
231                 return -1;
232         }
233
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);
238         }
239
240         tevent_req_received(req);
241         return xattr_size;
242 }
243
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)
247 {
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;
254
255         if (!xattr_tdb_init(handle, &config)) {
256                 return -1;
257         }
258
259         if (config->ignore_user_xattr && is_user_xattr(name)) {
260                 return SMB_VFS_NEXT_FGETXATTR(
261                         handle, fsp, name, value, size);
262         }
263
264         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
265                 return -1;
266         }
267
268         frame = talloc_stackframe();
269
270         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
271
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;
277         }
278
279         if (size == 0) {
280                 TALLOC_FREE(frame);
281                 return xattr_size;
282         }
283
284         if (blob.length > size) {
285                 TALLOC_FREE(frame);
286                 errno = ERANGE;
287                 return -1;
288         }
289         memcpy(value, blob.data, xattr_size);
290         TALLOC_FREE(frame);
291         return xattr_size;
292 }
293
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)
298 {
299         struct xattr_tdb_config *config = NULL;
300         SMB_STRUCT_STAT sbuf;
301         struct file_id id;
302         int ret;
303
304         if (!xattr_tdb_init(handle, &config)) {
305                 return -1;
306         }
307
308         if (config->ignore_user_xattr && is_user_xattr(name)) {
309                 return SMB_VFS_NEXT_FSETXATTR(
310                         handle, fsp, name, value, size, flags);
311         }
312
313         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
314                 return -1;
315         }
316
317         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
318
319         ret = xattr_tdb_setattr(config->db, &id, name, value, size, flags);
320         return ret;
321
322 }
323
324 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
325                                     struct files_struct *fsp, char *list,
326                                     size_t size)
327 {
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;
333
334         if (!xattr_tdb_init(handle, &config)) {
335                 return -1;
336         }
337
338         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
339                 return -1;
340         }
341
342         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
343
344         ret = xattr_tdb_listattr(config->db, &id, list, size);
345         if (ret == -1) {
346                 return -1;
347         }
348         if (ret == size) {
349                 return ret;
350         }
351         if (!config->ignore_user_xattr) {
352                 return ret;
353         }
354         SMB_ASSERT(ret < size);
355
356         backend_size = SMB_VFS_NEXT_FLISTXATTR(
357                 handle, fsp, list + ret, size - ret);
358         if (backend_size == -1) {
359                 return -1;
360         }
361
362         return ret + backend_size;
363 }
364
365 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
366                                   struct files_struct *fsp, const char *name)
367 {
368         struct xattr_tdb_config *config = NULL;
369         SMB_STRUCT_STAT sbuf;
370         struct file_id id;
371
372         if (!xattr_tdb_init(handle, &config)) {
373                 return -1;
374         }
375
376         if (config->ignore_user_xattr && is_user_xattr(name)) {
377                 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
378         }
379
380         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
381                 return -1;
382         }
383
384         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
385
386         return xattr_tdb_removeattr(config->db, &id, name);
387 }
388
389 /*
390  * Destructor for the VFS private data
391  */
392
393 static void config_destructor(void **data)
394 {
395         struct xattr_tdb_config **config = (struct xattr_tdb_config **)data;
396         TALLOC_FREE((*config)->db);
397 }
398
399 /*
400  * Open the tdb file upon VFS_CONNECT
401  */
402
403 static bool xattr_tdb_init(struct vfs_handle_struct *handle,
404                            struct xattr_tdb_config **_config)
405 {
406         struct xattr_tdb_config *config = NULL;
407         const char *dbname;
408         char *def_dbname;
409
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;
415                 }
416                 return true;
417         }
418
419         config = talloc_zero(handle->conn, struct xattr_tdb_config);
420         if (config == NULL) {
421                 errno = ENOMEM;
422                 goto error;
423         }
424
425         def_dbname = state_path(talloc_tos(), "xattr.tdb");
426         if (def_dbname == NULL) {
427                 errno = ENOSYS;
428                 goto error;
429         }
430
431         dbname = lp_parm_const_string(SNUM(handle->conn),
432                                       "xattr_tdb",
433                                       "file",
434                                       def_dbname);
435
436         /* now we know dbname is not NULL */
437
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();
442
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;
451         }
452         TALLOC_FREE(def_dbname);
453
454         config->ignore_user_xattr = lp_parm_bool(
455                 SNUM(handle->conn), "xattr_tdb", "ignore_user_xattr", false);
456
457         SMB_VFS_HANDLE_SET_DATA(handle, config, config_destructor,
458                                 struct xattr_tdb_config, return false);
459
460         if (_config != NULL) {
461                 *_config = config;
462         }
463         return true;
464
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;
469 }
470
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)
476 {
477         struct xattr_tdb_config *config = NULL;
478         SMB_STRUCT_STAT sbuf;
479         int fd;
480         int ret;
481
482         if (!xattr_tdb_init(handle, &config)) {
483                 return -1;
484         }
485
486         fd = SMB_VFS_NEXT_OPENAT(handle,
487                                  dirfsp,
488                                  smb_fname,
489                                  fsp,
490                                  how);
491         if (fd == -1) {
492                 return -1;
493         }
494
495         if ((how->flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
496                 return fd;
497         }
498
499         /*
500          * We know we used O_CREAT|O_EXCL and it worked.
501          * We must have created the file.
502          */
503
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;
513         }
514
515         fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &sbuf);
516
517         xattr_tdb_remove_all_attrs(config->db, &fsp->file_id);
518
519         return fd;
520 }
521
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)
526 {
527         struct xattr_tdb_config *config = NULL;
528         struct file_id fileid;
529         struct stat_ex sbuf = { .st_ex_nlink = 0, };
530         int ret;
531
532         if (!xattr_tdb_init(handle, &config)) {
533                 return -1;
534         }
535
536         ret = SMB_VFS_NEXT_MKDIRAT(handle,
537                                 dirfsp,
538                                 smb_fname,
539                                 mode);
540         if (ret < 0) {
541                 return ret;
542         }
543
544         ret = SMB_VFS_NEXT_FSTATAT(
545                 handle, dirfsp, smb_fname, &sbuf, AT_SYMLINK_NOFOLLOW);
546
547         if (ret == -1) {
548                 /* Rename race. Let upper level take care of it. */
549                 return -1;
550         }
551         if (!S_ISDIR(sbuf.st_ex_mode)) {
552                 /* Rename race. Let upper level take care of it. */
553                 return -1;
554         }
555
556         fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
557
558         xattr_tdb_remove_all_attrs(config->db, &fileid);
559         return 0;
560 }
561
562 /*
563  * On unlink we need to delete the tdb record
564  */
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)
569 {
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;
577
578         if (!xattr_tdb_init(handle, &config)) {
579                 return -1;
580         }
581
582         frame = talloc_stackframe();
583
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;
589         }
590
591         /*
592          * TODO: use SMB_VFS_STATX() once we have that
593          */
594
595         full_fname = full_path_from_dirfsp_atname(frame,
596                                                   dirfsp,
597                                                   smb_fname);
598         if (full_fname == NULL) {
599                 goto out;
600         }
601
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)) {
609                                 /*
610                                  * Original name was a link - Could be
611                                  * trying to remove a dangling symlink.
612                                  */
613                                 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
614                         }
615                 }
616         }
617         if (ret == -1) {
618                 goto out;
619         }
620         smb_fname_tmp->st = full_fname->st;
621
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;
629                 }
630         }
631
632         ret = SMB_VFS_NEXT_UNLINKAT(handle,
633                                 dirfsp,
634                                 smb_fname_tmp,
635                                 flags);
636
637         if (ret == -1) {
638                 goto out;
639         }
640
641         if (!remove_record) {
642                 goto out;
643         }
644
645         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
646
647         xattr_tdb_remove_all_attrs(config->db, &id);
648
649  out:
650         TALLOC_FREE(frame);
651         return ret;
652 }
653
654 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
655                           const char *user)
656 {
657         char *sname = NULL;
658         int res, snum;
659
660         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
661         if (res < 0) {
662                 return res;
663         }
664
665         snum = find_service(talloc_tos(), service, &sname);
666         if (snum == -1 || sname == NULL) {
667                 /*
668                  * Should not happen, but we should not fail just *here*.
669                  */
670                 return 0;
671         }
672
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;
677         }
678
679         lp_do_parameter(snum, "ea support", "True");
680
681         return 0;
682 }
683
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,
695 };
696
697 static_decl_vfs;
698 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
699 {
700         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
701                                 &vfs_xattr_tdb_fns);
702 }