Fix the build breakage by #including modules/vfs_acl_common.c
[samba.git] / source3 / modules / vfs_acl_tdb.c
1 /*
2  * Store Windows ACLs in a tdb.
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  * Copyright (C) Jeremy Allison, 2008
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 /* NOTE: This is an experimental module, not yet finished. JRA. */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "../lib/crypto/crypto.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
30
31 #include "modules/vfs_acl_common.c"
32
33 static unsigned int ref_count;
34 static struct db_context *acl_db;
35
36 /*******************************************************************
37  Open acl_db if not already open, increment ref count.
38 *******************************************************************/
39
40 static bool acl_tdb_init(struct db_context **pp_db)
41 {
42         char *dbname;
43
44         if (acl_db) {
45                 *pp_db = acl_db;
46                 ref_count++;
47                 return true;
48         }
49
50         dbname = state_path("file_ntacls.tdb");
51
52         if (dbname == NULL) {
53                 errno = ENOSYS;
54                 return false;
55         }
56
57         become_root();
58         *pp_db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
59         unbecome_root();
60
61         if (*pp_db == NULL) {
62 #if defined(ENOTSUP)
63                 errno = ENOTSUP;
64 #else
65                 errno = ENOSYS;
66 #endif
67                 TALLOC_FREE(dbname);
68                 return false;
69         }
70
71         ref_count++;
72         TALLOC_FREE(dbname);
73         return true;
74 }
75
76 /*******************************************************************
77  Lower ref count and close acl_db if zero.
78 *******************************************************************/
79
80 static void free_acl_tdb_data(void **pptr)
81 {
82         struct db_context **pp_db = (struct db_context **)pptr;
83
84         ref_count--;
85         if (ref_count == 0) {
86                 TALLOC_FREE(*pp_db);
87                 acl_db = NULL;
88         }
89 }
90
91 /*******************************************************************
92  Fetch_lock the tdb acl record for a file
93 *******************************************************************/
94
95 static struct db_record *acl_tdb_lock(TALLOC_CTX *mem_ctx,
96                                         struct db_context *db,
97                                         const struct file_id *id)
98 {
99         uint8 id_buf[16];
100
101         /* For backwards compatibility only store the dev/inode. */
102         push_file_id_16((char *)id_buf, id);
103         return db->fetch_locked(db,
104                                 mem_ctx,
105                                 make_tdb_data(id_buf,
106                                         sizeof(id_buf)));
107 }
108
109 /*******************************************************************
110  Delete the tdb acl record for a file
111 *******************************************************************/
112
113 static NTSTATUS acl_tdb_delete(vfs_handle_struct *handle,
114                                 struct db_context *db,
115                                 SMB_STRUCT_STAT *psbuf)
116 {
117         NTSTATUS status;
118         struct file_id id = vfs_file_id_from_sbuf(handle->conn, psbuf);
119         struct db_record *rec = acl_tdb_lock(talloc_tos(), db, &id);
120
121         /*
122          * If rec == NULL there's not much we can do about it
123          */
124
125         if (rec == NULL) {
126                 DEBUG(10,("acl_tdb_delete: rec == NULL\n"));
127                 TALLOC_FREE(rec);
128                 return NT_STATUS_OK;
129         }
130
131         status = rec->delete_rec(rec);
132         TALLOC_FREE(rec);
133         return status;
134 }
135
136 /*******************************************************************
137  Pull a security descriptor into a DATA_BLOB from a tdb store.
138 *******************************************************************/
139
140 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
141                         vfs_handle_struct *handle,
142                         files_struct *fsp,
143                         const char *name,
144                         DATA_BLOB *pblob)
145 {
146         uint8 id_buf[16];
147         TDB_DATA data;
148         struct file_id id;
149         struct db_context *db;
150         int ret = -1;
151         SMB_STRUCT_STAT sbuf;
152
153         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
154                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
155
156         if (fsp && fsp->fh->fd != -1) {
157                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
158         } else {
159                 if (fsp && fsp->posix_open) {
160                         ret = vfs_lstat_smb_fname(handle->conn, name, &sbuf);
161                 } else {
162                         ret = vfs_stat_smb_fname(handle->conn, name, &sbuf);
163                 }
164         }
165
166         if (ret == -1) {
167                 return map_nt_error_from_unix(errno);
168         }
169
170         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
171
172         /* For backwards compatibility only store the dev/inode. */
173         push_file_id_16((char *)id_buf, &id);
174
175         if (db->fetch(db,
176                         ctx,
177                         make_tdb_data(id_buf, sizeof(id_buf)),
178                         &data) == -1) {
179                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
180         }
181
182         pblob->data = data.dptr;
183         pblob->length = data.dsize;
184
185         DEBUG(10,("get_acl_blob: returned %u bytes from file %s\n",
186                 (unsigned int)data.dsize, name ));
187
188         if (pblob->length == 0 || pblob->data == NULL) {
189                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
190         }
191         return NT_STATUS_OK;
192 }
193
194 /*******************************************************************
195  Store a DATA_BLOB into a tdb record given an fsp pointer.
196 *******************************************************************/
197
198 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
199                                 files_struct *fsp,
200                                 DATA_BLOB *pblob)
201 {
202         uint8 id_buf[16];
203         struct file_id id;
204         TDB_DATA data;
205         struct db_context *db;
206         struct db_record *rec;
207         int ret = -1;
208
209         DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
210                   (unsigned int)pblob->length, fsp_str_dbg(fsp)));
211
212         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
213                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
214
215         if (fsp->fh->fd != -1) {
216                 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
217         } else {
218                 if (fsp->posix_open) {
219                         ret = SMB_VFS_LSTAT(handle->conn, fsp->fsp_name);
220                 } else {
221                         ret = SMB_VFS_STAT(handle->conn, fsp->fsp_name);
222                 }
223         }
224
225         if (ret == -1) {
226                 return map_nt_error_from_unix(errno);
227         }
228
229         id = vfs_file_id_from_sbuf(handle->conn, &fsp->fsp_name->st);
230
231         /* For backwards compatibility only store the dev/inode. */
232         push_file_id_16((char *)id_buf, &id);
233         rec = db->fetch_locked(db, talloc_tos(),
234                                 make_tdb_data(id_buf,
235                                         sizeof(id_buf)));
236         if (rec == NULL) {
237                 DEBUG(0, ("store_acl_blob_fsp_tdb: fetch_lock failed\n"));
238                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
239         }
240         data.dptr = pblob->data;
241         data.dsize = pblob->length;
242         return rec->store(rec, data, 0);
243 }
244
245 /*******************************************************************
246  Store a DATA_BLOB into a tdb record given a pathname.
247 *******************************************************************/
248
249 static NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
250                                         const char *fname,
251                                         DATA_BLOB *pblob)
252 {
253         uint8 id_buf[16];
254         struct file_id id;
255         TDB_DATA data;
256         SMB_STRUCT_STAT sbuf;
257         struct db_context *db;
258         struct db_record *rec;
259         int ret = -1;
260
261         DEBUG(10,("store_acl_blob_pathname: storing blob "
262                         "length %u on file %s\n",
263                         (unsigned int)pblob->length, fname));
264
265         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
266                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
267
268         if (lp_posix_pathnames()) {
269                 ret = vfs_lstat_smb_fname(handle->conn, fname, &sbuf);
270         } else {
271                 ret = vfs_stat_smb_fname(handle->conn, fname, &sbuf);
272         }
273
274         if (ret == -1) {
275                 return map_nt_error_from_unix(errno);
276         }
277
278         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
279
280         /* For backwards compatibility only store the dev/inode. */
281         push_file_id_16((char *)id_buf, &id);
282
283         rec = db->fetch_locked(db, talloc_tos(),
284                                 make_tdb_data(id_buf,
285                                         sizeof(id_buf)));
286         if (rec == NULL) {
287                 DEBUG(0, ("store_acl_blob_pathname_tdb: fetch_lock failed\n"));
288                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
289         }
290         data.dptr = pblob->data;
291         data.dsize = pblob->length;
292         return rec->store(rec, data, 0);
293 }
294
295 /*********************************************************************
296  On unlink we need to delete the tdb record (if using tdb).
297 *********************************************************************/
298
299 static int unlink_acl_tdb(vfs_handle_struct *handle,
300                           const struct smb_filename *smb_fname)
301 {
302         struct smb_filename *smb_fname_tmp = NULL;
303         struct db_context *db;
304         NTSTATUS status;
305         int ret = -1;
306
307         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
308
309         status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
310         if (!NT_STATUS_IS_OK(status)) {
311                 errno = map_errno_from_nt_status(status);
312                 goto out;
313         }
314
315         if (lp_posix_pathnames()) {
316                 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
317         } else {
318                 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
319         }
320
321         if (ret == -1) {
322                 goto out;
323         }
324
325         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
326
327         if (ret == -1) {
328                 goto out;
329         }
330
331         acl_tdb_delete(handle, db, &smb_fname_tmp->st);
332  out:
333         return ret;
334 }
335
336 /*********************************************************************
337  On rmdir we need to delete the tdb record (if using tdb).
338 *********************************************************************/
339
340 static int rmdir_acl_tdb(vfs_handle_struct *handle, const char *path)
341 {
342
343         SMB_STRUCT_STAT sbuf;
344         struct db_context *db;
345         int ret = -1;
346
347         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
348
349         if (lp_posix_pathnames()) {
350                 ret = vfs_lstat_smb_fname(handle->conn, path, &sbuf);
351         } else {
352                 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
353         }
354
355         if (ret == -1) {
356                 return -1;
357         }
358
359         ret = SMB_VFS_NEXT_RMDIR(handle, path);
360         if (ret == -1) {
361                 return -1;
362         }
363
364         acl_tdb_delete(handle, db, &sbuf);
365         return 0;
366 }
367
368 /*******************************************************************
369  Handle opening the storage tdb if so configured.
370 *******************************************************************/
371
372 static int connect_acl_tdb(struct vfs_handle_struct *handle,
373                                 const char *service,
374                                 const char *user)
375 {
376         struct db_context *db;
377         int res;
378
379         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
380         if (res < 0) {
381                 return res;
382         }
383
384         if (!acl_tdb_init(&db)) {
385                 SMB_VFS_NEXT_DISCONNECT(handle);
386                 return -1;
387         }
388
389         SMB_VFS_HANDLE_SET_DATA(handle, db, free_acl_tdb_data,
390                                 struct db_context, return -1);
391
392         return 0;
393 }
394
395 /*********************************************************************
396  Remove a Windows ACL - we're setting the underlying POSIX ACL.
397 *********************************************************************/
398
399 static int sys_acl_set_file_tdb(vfs_handle_struct *handle,
400                               const char *path,
401                               SMB_ACL_TYPE_T type,
402                               SMB_ACL_T theacl)
403 {
404         SMB_STRUCT_STAT sbuf;
405         struct db_context *db;
406         int ret = -1;
407
408         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
409
410         if (lp_posix_pathnames()) {
411                 ret = vfs_lstat_smb_fname(handle->conn, path, &sbuf);
412         } else {
413                 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
414         }
415
416         if (ret == -1) {
417                 return -1;
418         }
419
420         ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
421                                                 path,
422                                                 type,
423                                                 theacl);
424         if (ret == -1) {
425                 return -1;
426         }
427
428         acl_tdb_delete(handle, db, &sbuf);
429         return 0;
430 }
431
432 /*********************************************************************
433  Remove a Windows ACL - we're setting the underlying POSIX ACL.
434 *********************************************************************/
435
436 static int sys_acl_set_fd_tdb(vfs_handle_struct *handle,
437                             files_struct *fsp,
438                             SMB_ACL_T theacl)
439 {
440         struct db_context *db;
441         int ret;
442
443         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
444
445         if (fsp->is_directory || fsp->fh->fd == -1) {
446                 if (fsp->posix_open) {
447                         ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
448                 } else {
449                         ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
450                 }
451         } else {
452                 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
453         }
454         if (ret == -1) {
455                 return -1;
456         }
457
458         ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
459                                                 fsp,
460                                                 theacl);
461         if (ret == -1) {
462                 return -1;
463         }
464
465         acl_tdb_delete(handle, db, &fsp->fsp_name->st);
466         return 0;
467 }
468
469 static struct vfs_fn_pointers vfs_acl_tdb_fns = {
470         .connect_fn = connect_acl_tdb,
471         .mkdir = mkdir_acl_common,
472         .open = open_acl_common,
473         .unlink = unlink_acl_tdb,
474         .rmdir = rmdir_acl_tdb,
475         .fget_nt_acl = fget_nt_acl_common,
476         .get_nt_acl = get_nt_acl_common,
477         .fset_nt_acl = fset_nt_acl_common,
478         .sys_acl_set_file = sys_acl_set_file_tdb,
479         .sys_acl_set_fd = sys_acl_set_fd_tdb
480 };
481
482 NTSTATUS vfs_acl_tdb_init(void)
483 {
484         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_tdb",
485                                 &vfs_acl_tdb_fns);
486 }