s3-vfs: Continue to make vfs_xattr_tdb non-recursive
[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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "source3/lib/xattr_tdb.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_VFS
29
30 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
31                                 const char *path, struct file_id *id)
32 {
33         int ret;
34         TALLOC_CTX *frame = talloc_stackframe();
35         struct smb_filename *smb_fname = NULL;
36         NTSTATUS status = create_synthetic_smb_fname_split(frame, path, NULL,
37                                                   &smb_fname);
38         if (!NT_STATUS_IS_OK(status)) {
39                 errno = map_errno_from_nt_status(status);
40                 TALLOC_FREE(frame); 
41                 return -1;
42         }
43
44         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
45
46         if (ret == -1) {
47                 TALLOC_FREE(frame); 
48                 return -1;
49         }
50
51         *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
52         TALLOC_FREE(frame);
53         return 0;
54 }
55
56 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
57                                   const char *path, const char *name,
58                                   void *value, size_t size)
59 {
60         struct file_id id;
61         struct db_context *db;
62         ssize_t xattr_size;
63         int ret;
64         DATA_BLOB blob;
65         TALLOC_CTX *frame = talloc_stackframe();
66
67         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
68                                 TALLOC_FREE(frame); return -1);
69
70         ret = xattr_tdb_get_file_id(handle, path, &id);
71         if (ret == -1) {
72                 TALLOC_FREE(frame);
73                 return -1;
74         }
75
76         xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
77         if (xattr_size < 0) {
78                 errno = ENOATTR;
79                 TALLOC_FREE(frame);
80                 return -1;
81         }
82         if (blob.length > size) {
83                 TALLOC_FREE(frame);
84                 errno = ERANGE;
85                 return -1;
86         }
87         memcpy(value, blob.data, xattr_size);
88         TALLOC_FREE(frame);
89         return xattr_size;
90 }
91
92 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
93                                    struct files_struct *fsp,
94                                    const char *name, void *value, size_t size)
95 {
96         SMB_STRUCT_STAT sbuf;
97         struct file_id id;
98         struct db_context *db;
99         ssize_t xattr_size;
100         DATA_BLOB blob;
101         TALLOC_CTX *frame = talloc_stackframe();
102
103         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, TALLOC_FREE(frame); return -1);
104
105         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
106                 TALLOC_FREE(frame);
107                 return -1;
108         }
109
110         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
111
112         xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
113         if (xattr_size < 0) {
114                 errno = ENOATTR;
115                 TALLOC_FREE(frame);
116                 return -1;
117         }
118         if (blob.length > size) {
119                 TALLOC_FREE(frame);
120                 errno = ERANGE;
121                 return -1;
122         }
123         memcpy(value, blob.data, xattr_size);
124         TALLOC_FREE(frame);
125         return xattr_size;
126 }
127
128 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
129                               const char *path, const char *name,
130                               const void *value, size_t size, int flags)
131 {
132         struct file_id id;
133         struct db_context *db;
134         int ret;
135
136         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
137
138         ret = xattr_tdb_get_file_id(handle, path, &id);
139         if (ret == -1) {
140                 return -1;
141         }
142
143         return xattr_tdb_setattr(db, &id, name, value, size, flags);
144 }
145
146 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
147                                struct files_struct *fsp,
148                                const char *name, const void *value,
149                                size_t size, int flags)
150 {
151         SMB_STRUCT_STAT sbuf;
152         struct file_id id;
153         struct db_context *db;
154
155         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
156
157         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
158                 return -1;
159         }
160
161         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
162
163         return xattr_tdb_setattr(db, &id, name, value, size, flags);
164 }
165
166 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
167                                    const char *path, char *list, size_t size)
168 {
169         struct file_id id;
170         struct db_context *db;
171         int ret;
172
173         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
174
175         ret = xattr_tdb_get_file_id(handle, path, &id);
176         if (ret == -1) {
177                 return -1;
178         }
179
180         return xattr_tdb_listattr(db, &id, list, size);
181 }
182
183 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
184                                     struct files_struct *fsp, char *list,
185                                     size_t size)
186 {
187         SMB_STRUCT_STAT sbuf;
188         struct file_id id;
189         struct db_context *db;
190
191         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
192
193         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
194                 return -1;
195         }
196
197         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
198
199         return xattr_tdb_listattr(db, &id, list, size);
200 }
201
202 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
203                                  const char *path, const char *name)
204 {
205         struct file_id id;
206         struct db_context *db;
207         int ret;
208
209         TALLOC_CTX *frame = talloc_stackframe();
210
211         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, TALLOC_FREE(frame); return -1);
212
213         ret = xattr_tdb_get_file_id(handle, path, &id);
214         if (ret == -1) {
215                 return ret;
216         }
217
218         TALLOC_FREE(frame); 
219         
220         return xattr_tdb_removeattr(db, &id, name);
221 }
222
223 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
224                                   struct files_struct *fsp, const char *name)
225 {
226         SMB_STRUCT_STAT sbuf;
227         struct file_id id;
228         struct db_context *db;
229
230         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
231
232         if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
233                 return -1;
234         }
235
236         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
237
238         return xattr_tdb_removeattr(db, &id, name);
239 }
240
241 /*
242  * Open the tdb file upon VFS_CONNECT
243  */
244
245 static bool xattr_tdb_init(int snum, struct db_context **p_db)
246 {
247         struct db_context *db;
248         const char *dbname;
249         char *def_dbname;
250
251         def_dbname = state_path("xattr.tdb");
252         if (def_dbname == NULL) {
253                 errno = ENOSYS;
254                 return false;
255         }
256
257         dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
258
259         /* now we know dbname is not NULL */
260
261         become_root();
262         db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
263                      DBWRAP_LOCK_ORDER_2);
264         unbecome_root();
265
266         if (db == NULL) {
267 #if defined(ENOTSUP)
268                 errno = ENOTSUP;
269 #else
270                 errno = ENOSYS;
271 #endif
272                 TALLOC_FREE(def_dbname);
273                 return false;
274         }
275
276         *p_db = db;
277         TALLOC_FREE(def_dbname);
278         return true;
279 }
280
281 /*
282  * On unlink we need to delete the tdb record
283  */
284 static int xattr_tdb_unlink(vfs_handle_struct *handle,
285                             const struct smb_filename *smb_fname)
286 {
287         struct smb_filename *smb_fname_tmp = NULL;
288         struct file_id id;
289         struct db_context *db;
290         NTSTATUS status;
291         int ret = -1;
292         bool remove_record = false;
293
294         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
295
296         status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
297         if (!NT_STATUS_IS_OK(status)) {
298                 errno = map_errno_from_nt_status(status);
299                 return -1;
300         }
301
302         if (lp_posix_pathnames()) {
303                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
304         } else {
305                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
306         }
307         if (ret == -1) {
308                 goto out;
309         }
310
311         if (smb_fname_tmp->st.st_ex_nlink == 1) {
312                 /* Only remove record on last link to file. */
313                 remove_record = true;
314         }
315
316         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
317
318         if (ret == -1) {
319                 goto out;
320         }
321
322         if (!remove_record) {
323                 goto out;
324         }
325
326         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
327
328         xattr_tdb_remove_all_attrs(db, &id);
329
330  out:
331         TALLOC_FREE(smb_fname_tmp);
332         return ret;
333 }
334
335 /*
336  * On rmdir we need to delete the tdb record
337  */
338 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
339 {
340         SMB_STRUCT_STAT sbuf;
341         struct file_id id;
342         struct db_context *db;
343         int ret;
344
345         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
346
347         if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
348                 return -1;
349         }
350
351         ret = SMB_VFS_NEXT_RMDIR(handle, path);
352
353         if (ret == -1) {
354                 return -1;
355         }
356
357         id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
358
359         xattr_tdb_remove_all_attrs(db, &id);
360
361         return 0;
362 }
363
364 /*
365  * Destructor for the VFS private data
366  */
367
368 static void close_xattr_db(void **data)
369 {
370         struct db_context **p_db = (struct db_context **)data;
371         TALLOC_FREE(*p_db);
372 }
373
374 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
375                           const char *user)
376 {
377         char *sname = NULL;
378         int res, snum;
379         struct db_context *db;
380
381         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
382         if (res < 0) {
383                 return res;
384         }
385
386         snum = find_service(talloc_tos(), service, &sname);
387         if (snum == -1 || sname == NULL) {
388                 /*
389                  * Should not happen, but we should not fail just *here*.
390                  */
391                 return 0;
392         }
393
394         if (!xattr_tdb_init(snum, &db)) {
395                 DEBUG(5, ("Could not init xattr tdb\n"));
396                 lp_do_parameter(snum, "ea support", "False");
397                 return 0;
398         }
399
400         lp_do_parameter(snum, "ea support", "True");
401
402         SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
403                                 struct db_context, return -1);
404
405         return 0;
406 }
407
408 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
409         .getxattr_fn = xattr_tdb_getxattr,
410         .fgetxattr_fn = xattr_tdb_fgetxattr,
411         .setxattr_fn = xattr_tdb_setxattr,
412         .fsetxattr_fn = xattr_tdb_fsetxattr,
413         .listxattr_fn = xattr_tdb_listxattr,
414         .flistxattr_fn = xattr_tdb_flistxattr,
415         .removexattr_fn = xattr_tdb_removexattr,
416         .fremovexattr_fn = xattr_tdb_fremovexattr,
417         .unlink_fn = xattr_tdb_unlink,
418         .rmdir_fn = xattr_tdb_rmdir,
419         .connect_fn = xattr_tdb_connect,
420 };
421
422 NTSTATUS vfs_xattr_tdb_init(void);
423 NTSTATUS vfs_xattr_tdb_init(void)
424 {
425         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
426                                 &vfs_xattr_tdb_fns);
427 }