d29064b00d1dd7eabb547e8173169afa33b4d1f9
[kamenim/samba-autobuild/.git] / source3 / modules / vfs_full_audit.c
1 /* 
2  * Auditing VFS module for samba.  Log selected file operations to syslog
3  * facility.
4  *
5  * Copyright (C) Tim Potter, 1999-2000
6  * Copyright (C) Alexander Bokovoy, 2002
7  * Copyright (C) John H Terpstra, 2003
8  * Copyright (C) Stefan (metze) Metzmacher, 2003
9  * Copyright (C) Volker Lendecke, 2004
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *  
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *  
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, see <http://www.gnu.org/licenses/>.
23  */
24
25 /*
26  * This module implements parseable logging for all Samba VFS operations.
27  *
28  * You use it as follows:
29  *
30  * [tmp]
31  * path = /tmp
32  * vfs objects = full_audit
33  * full_audit:prefix = %u|%I
34  * full_audit:success = open opendir
35  * full_audit:failure = all
36  *
37  * vfs op can be "all" which means log all operations.
38  * vfs op can be "none" which means no logging.
39  *
40  * This leads to syslog entries of the form:
41  * smbd_audit: nobody|192.168.234.1|opendir|ok|.
42  * smbd_audit: nobody|192.168.234.1|open|fail (File not found)|r|x.txt
43  *
44  * where "nobody" is the connected username and "192.168.234.1" is the
45  * client's IP address. 
46  *
47  * Options:
48  *
49  * prefix: A macro expansion template prepended to the syslog entry.
50  *
51  * success: A list of VFS operations for which a successful completion should
52  * be logged. Defaults to no logging at all. The special operation "all" logs
53  * - you guessed it - everything.
54  *
55  * failure: A list of VFS operations for which failure to complete should be
56  * logged. Defaults to logging everything.
57  */
58
59
60 #include "includes.h"
61 #include "system/filesys.h"
62 #include "system/syslog.h"
63 #include "smbd/smbd.h"
64 #include "../librpc/gen_ndr/ndr_netlogon.h"
65 #include "auth.h"
66 #include "ntioctl.h"
67 #include "lib/param/loadparm.h"
68 #include "lib/util/bitmap.h"
69 #include "lib/util/tevent_unix.h"
70 #include "libcli/security/sddl.h"
71 #include "passdb/machine_sid.h"
72
73 static int vfs_full_audit_debug_level = DBGC_VFS;
74
75 struct vfs_full_audit_private_data {
76         struct bitmap *success_ops;
77         struct bitmap *failure_ops;
78         int syslog_facility;
79         int syslog_priority;
80         bool log_secdesc;
81         bool do_syslog;
82 };
83
84 #undef DBGC_CLASS
85 #define DBGC_CLASS vfs_full_audit_debug_level
86
87 typedef enum _vfs_op_type {
88         SMB_VFS_OP_NOOP = -1,
89
90         /* Disk operations */
91
92         SMB_VFS_OP_CONNECT = 0,
93         SMB_VFS_OP_DISCONNECT,
94         SMB_VFS_OP_DISK_FREE,
95         SMB_VFS_OP_GET_QUOTA,
96         SMB_VFS_OP_SET_QUOTA,
97         SMB_VFS_OP_GET_SHADOW_COPY_DATA,
98         SMB_VFS_OP_STATVFS,
99         SMB_VFS_OP_FS_CAPABILITIES,
100         SMB_VFS_OP_SNAP_CHECK_PATH,
101         SMB_VFS_OP_SNAP_CREATE,
102         SMB_VFS_OP_SNAP_DELETE,
103
104         /* Directory operations */
105
106         SMB_VFS_OP_OPENDIR,
107         SMB_VFS_OP_FDOPENDIR,
108         SMB_VFS_OP_READDIR,
109         SMB_VFS_OP_SEEKDIR,
110         SMB_VFS_OP_TELLDIR,
111         SMB_VFS_OP_REWINDDIR,
112         SMB_VFS_OP_MKDIR,
113         SMB_VFS_OP_RMDIR,
114         SMB_VFS_OP_CLOSEDIR,
115         SMB_VFS_OP_INIT_SEARCH_OP,
116
117         /* File operations */
118
119         SMB_VFS_OP_OPEN,
120         SMB_VFS_OP_CREATE_FILE,
121         SMB_VFS_OP_CLOSE,
122         SMB_VFS_OP_READ,
123         SMB_VFS_OP_PREAD,
124         SMB_VFS_OP_PREAD_SEND,
125         SMB_VFS_OP_PREAD_RECV,
126         SMB_VFS_OP_WRITE,
127         SMB_VFS_OP_PWRITE,
128         SMB_VFS_OP_PWRITE_SEND,
129         SMB_VFS_OP_PWRITE_RECV,
130         SMB_VFS_OP_LSEEK,
131         SMB_VFS_OP_SENDFILE,
132         SMB_VFS_OP_RECVFILE,
133         SMB_VFS_OP_RENAME,
134         SMB_VFS_OP_FSYNC,
135         SMB_VFS_OP_FSYNC_SEND,
136         SMB_VFS_OP_FSYNC_RECV,
137         SMB_VFS_OP_STAT,
138         SMB_VFS_OP_FSTAT,
139         SMB_VFS_OP_LSTAT,
140         SMB_VFS_OP_GET_ALLOC_SIZE,
141         SMB_VFS_OP_UNLINK,
142         SMB_VFS_OP_CHMOD,
143         SMB_VFS_OP_FCHMOD,
144         SMB_VFS_OP_CHOWN,
145         SMB_VFS_OP_FCHOWN,
146         SMB_VFS_OP_LCHOWN,
147         SMB_VFS_OP_CHDIR,
148         SMB_VFS_OP_GETWD,
149         SMB_VFS_OP_NTIMES,
150         SMB_VFS_OP_FTRUNCATE,
151         SMB_VFS_OP_FALLOCATE,
152         SMB_VFS_OP_LOCK,
153         SMB_VFS_OP_KERNEL_FLOCK,
154         SMB_VFS_OP_LINUX_SETLEASE,
155         SMB_VFS_OP_GETLOCK,
156         SMB_VFS_OP_SYMLINK,
157         SMB_VFS_OP_READLINK,
158         SMB_VFS_OP_LINK,
159         SMB_VFS_OP_MKNOD,
160         SMB_VFS_OP_REALPATH,
161         SMB_VFS_OP_CHFLAGS,
162         SMB_VFS_OP_FILE_ID_CREATE,
163         SMB_VFS_OP_STREAMINFO,
164         SMB_VFS_OP_GET_REAL_FILENAME,
165         SMB_VFS_OP_CONNECTPATH,
166         SMB_VFS_OP_BRL_LOCK_WINDOWS,
167         SMB_VFS_OP_BRL_UNLOCK_WINDOWS,
168         SMB_VFS_OP_BRL_CANCEL_WINDOWS,
169         SMB_VFS_OP_STRICT_LOCK,
170         SMB_VFS_OP_STRICT_UNLOCK,
171         SMB_VFS_OP_TRANSLATE_NAME,
172         SMB_VFS_OP_COPY_CHUNK_SEND,
173         SMB_VFS_OP_COPY_CHUNK_RECV,
174         SMB_VFS_OP_GET_COMPRESSION,
175         SMB_VFS_OP_SET_COMPRESSION,
176         SMB_VFS_OP_READDIR_ATTR,
177
178         /* NT ACL operations. */
179
180         SMB_VFS_OP_FGET_NT_ACL,
181         SMB_VFS_OP_GET_NT_ACL,
182         SMB_VFS_OP_FSET_NT_ACL,
183
184         /* POSIX ACL operations. */
185
186         SMB_VFS_OP_CHMOD_ACL,
187         SMB_VFS_OP_FCHMOD_ACL,
188
189         SMB_VFS_OP_SYS_ACL_GET_FILE,
190         SMB_VFS_OP_SYS_ACL_GET_FD,
191         SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE,
192         SMB_VFS_OP_SYS_ACL_BLOB_GET_FD,
193         SMB_VFS_OP_SYS_ACL_SET_FILE,
194         SMB_VFS_OP_SYS_ACL_SET_FD,
195         SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
196
197         /* EA operations. */
198         SMB_VFS_OP_GETXATTR,
199         SMB_VFS_OP_FGETXATTR,
200         SMB_VFS_OP_LISTXATTR,
201         SMB_VFS_OP_FLISTXATTR,
202         SMB_VFS_OP_REMOVEXATTR,
203         SMB_VFS_OP_FREMOVEXATTR,
204         SMB_VFS_OP_SETXATTR,
205         SMB_VFS_OP_FSETXATTR,
206
207         /* aio operations */
208         SMB_VFS_OP_AIO_FORCE,
209
210         /* offline operations */
211         SMB_VFS_OP_IS_OFFLINE,
212         SMB_VFS_OP_SET_OFFLINE,
213
214         /* This should always be last enum value */
215
216         SMB_VFS_OP_LAST
217 } vfs_op_type;
218
219 /* The following array *must* be in the same order as defined in vfs_op_type */
220
221 static struct {
222         vfs_op_type type;
223         const char *name;
224 } vfs_op_names[] = {
225         { SMB_VFS_OP_CONNECT,   "connect" },
226         { SMB_VFS_OP_DISCONNECT,        "disconnect" },
227         { SMB_VFS_OP_DISK_FREE, "disk_free" },
228         { SMB_VFS_OP_GET_QUOTA, "get_quota" },
229         { SMB_VFS_OP_SET_QUOTA, "set_quota" },
230         { SMB_VFS_OP_GET_SHADOW_COPY_DATA,      "get_shadow_copy_data" },
231         { SMB_VFS_OP_STATVFS,   "statvfs" },
232         { SMB_VFS_OP_FS_CAPABILITIES,   "fs_capabilities" },
233         { SMB_VFS_OP_SNAP_CHECK_PATH, "snap_check_path" },
234         { SMB_VFS_OP_SNAP_CREATE, "snap_create" },
235         { SMB_VFS_OP_SNAP_DELETE, "snap_delete" },
236         { SMB_VFS_OP_OPENDIR,   "opendir" },
237         { SMB_VFS_OP_FDOPENDIR, "fdopendir" },
238         { SMB_VFS_OP_READDIR,   "readdir" },
239         { SMB_VFS_OP_SEEKDIR,   "seekdir" },
240         { SMB_VFS_OP_TELLDIR,   "telldir" },
241         { SMB_VFS_OP_REWINDDIR, "rewinddir" },
242         { SMB_VFS_OP_MKDIR,     "mkdir" },
243         { SMB_VFS_OP_RMDIR,     "rmdir" },
244         { SMB_VFS_OP_CLOSEDIR,  "closedir" },
245         { SMB_VFS_OP_INIT_SEARCH_OP, "init_search_op" },
246         { SMB_VFS_OP_OPEN,      "open" },
247         { SMB_VFS_OP_CREATE_FILE, "create_file" },
248         { SMB_VFS_OP_CLOSE,     "close" },
249         { SMB_VFS_OP_READ,      "read" },
250         { SMB_VFS_OP_PREAD,     "pread" },
251         { SMB_VFS_OP_PREAD_SEND,        "pread_send" },
252         { SMB_VFS_OP_PREAD_RECV,        "pread_recv" },
253         { SMB_VFS_OP_WRITE,     "write" },
254         { SMB_VFS_OP_PWRITE,    "pwrite" },
255         { SMB_VFS_OP_PWRITE_SEND,       "pwrite_send" },
256         { SMB_VFS_OP_PWRITE_RECV,       "pwrite_recv" },
257         { SMB_VFS_OP_LSEEK,     "lseek" },
258         { SMB_VFS_OP_SENDFILE,  "sendfile" },
259         { SMB_VFS_OP_RECVFILE,  "recvfile" },
260         { SMB_VFS_OP_RENAME,    "rename" },
261         { SMB_VFS_OP_FSYNC,     "fsync" },
262         { SMB_VFS_OP_FSYNC_SEND,        "fsync_send" },
263         { SMB_VFS_OP_FSYNC_RECV,        "fsync_recv" },
264         { SMB_VFS_OP_STAT,      "stat" },
265         { SMB_VFS_OP_FSTAT,     "fstat" },
266         { SMB_VFS_OP_LSTAT,     "lstat" },
267         { SMB_VFS_OP_GET_ALLOC_SIZE,    "get_alloc_size" },
268         { SMB_VFS_OP_UNLINK,    "unlink" },
269         { SMB_VFS_OP_CHMOD,     "chmod" },
270         { SMB_VFS_OP_FCHMOD,    "fchmod" },
271         { SMB_VFS_OP_CHOWN,     "chown" },
272         { SMB_VFS_OP_FCHOWN,    "fchown" },
273         { SMB_VFS_OP_LCHOWN,    "lchown" },
274         { SMB_VFS_OP_CHDIR,     "chdir" },
275         { SMB_VFS_OP_GETWD,     "getwd" },
276         { SMB_VFS_OP_NTIMES,    "ntimes" },
277         { SMB_VFS_OP_FTRUNCATE, "ftruncate" },
278         { SMB_VFS_OP_FALLOCATE,"fallocate" },
279         { SMB_VFS_OP_LOCK,      "lock" },
280         { SMB_VFS_OP_KERNEL_FLOCK,      "kernel_flock" },
281         { SMB_VFS_OP_LINUX_SETLEASE, "linux_setlease" },
282         { SMB_VFS_OP_GETLOCK,   "getlock" },
283         { SMB_VFS_OP_SYMLINK,   "symlink" },
284         { SMB_VFS_OP_READLINK,  "readlink" },
285         { SMB_VFS_OP_LINK,      "link" },
286         { SMB_VFS_OP_MKNOD,     "mknod" },
287         { SMB_VFS_OP_REALPATH,  "realpath" },
288         { SMB_VFS_OP_CHFLAGS,   "chflags" },
289         { SMB_VFS_OP_FILE_ID_CREATE,    "file_id_create" },
290         { SMB_VFS_OP_STREAMINFO,        "streaminfo" },
291         { SMB_VFS_OP_GET_REAL_FILENAME, "get_real_filename" },
292         { SMB_VFS_OP_CONNECTPATH,       "connectpath" },
293         { SMB_VFS_OP_BRL_LOCK_WINDOWS,  "brl_lock_windows" },
294         { SMB_VFS_OP_BRL_UNLOCK_WINDOWS, "brl_unlock_windows" },
295         { SMB_VFS_OP_BRL_CANCEL_WINDOWS, "brl_cancel_windows" },
296         { SMB_VFS_OP_STRICT_LOCK, "strict_lock" },
297         { SMB_VFS_OP_STRICT_UNLOCK, "strict_unlock" },
298         { SMB_VFS_OP_TRANSLATE_NAME,    "translate_name" },
299         { SMB_VFS_OP_COPY_CHUNK_SEND,   "copy_chunk_send" },
300         { SMB_VFS_OP_COPY_CHUNK_RECV,   "copy_chunk_recv" },
301         { SMB_VFS_OP_GET_COMPRESSION,   "get_compression" },
302         { SMB_VFS_OP_SET_COMPRESSION,   "set_compression" },
303         { SMB_VFS_OP_READDIR_ATTR,      "readdir_attr" },
304         { SMB_VFS_OP_FGET_NT_ACL,       "fget_nt_acl" },
305         { SMB_VFS_OP_GET_NT_ACL,        "get_nt_acl" },
306         { SMB_VFS_OP_FSET_NT_ACL,       "fset_nt_acl" },
307         { SMB_VFS_OP_CHMOD_ACL, "chmod_acl" },
308         { SMB_VFS_OP_FCHMOD_ACL,        "fchmod_acl" },
309         { SMB_VFS_OP_SYS_ACL_GET_FILE,  "sys_acl_get_file" },
310         { SMB_VFS_OP_SYS_ACL_GET_FD,    "sys_acl_get_fd" },
311         { SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE,     "sys_acl_blob_get_file" },
312         { SMB_VFS_OP_SYS_ACL_BLOB_GET_FD,       "sys_acl_blob_get_fd" },
313         { SMB_VFS_OP_SYS_ACL_SET_FILE,  "sys_acl_set_file" },
314         { SMB_VFS_OP_SYS_ACL_SET_FD,    "sys_acl_set_fd" },
315         { SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,   "sys_acl_delete_def_file" },
316         { SMB_VFS_OP_GETXATTR,  "getxattr" },
317         { SMB_VFS_OP_FGETXATTR, "fgetxattr" },
318         { SMB_VFS_OP_LISTXATTR, "listxattr" },
319         { SMB_VFS_OP_FLISTXATTR,        "flistxattr" },
320         { SMB_VFS_OP_REMOVEXATTR,       "removexattr" },
321         { SMB_VFS_OP_FREMOVEXATTR,      "fremovexattr" },
322         { SMB_VFS_OP_SETXATTR,  "setxattr" },
323         { SMB_VFS_OP_FSETXATTR, "fsetxattr" },
324         { SMB_VFS_OP_AIO_FORCE, "aio_force" },
325         { SMB_VFS_OP_IS_OFFLINE, "is_offline" },
326         { SMB_VFS_OP_SET_OFFLINE, "set_offline" },
327         { SMB_VFS_OP_LAST, NULL }
328 };
329
330 static int audit_syslog_facility(vfs_handle_struct *handle)
331 {
332         static const struct enum_list enum_log_facilities[] = {
333                 { LOG_USER, "USER" },
334                 { LOG_LOCAL0, "LOCAL0" },
335                 { LOG_LOCAL1, "LOCAL1" },
336                 { LOG_LOCAL2, "LOCAL2" },
337                 { LOG_LOCAL3, "LOCAL3" },
338                 { LOG_LOCAL4, "LOCAL4" },
339                 { LOG_LOCAL5, "LOCAL5" },
340                 { LOG_LOCAL6, "LOCAL6" },
341                 { LOG_LOCAL7, "LOCAL7" },
342                 { -1, NULL}
343         };
344
345         int facility;
346
347         facility = lp_parm_enum(SNUM(handle->conn), "full_audit", "facility", enum_log_facilities, LOG_USER);
348
349         return facility;
350 }
351
352 static int audit_syslog_priority(vfs_handle_struct *handle)
353 {
354         static const struct enum_list enum_log_priorities[] = {
355                 { LOG_EMERG, "EMERG" },
356                 { LOG_ALERT, "ALERT" },
357                 { LOG_CRIT, "CRIT" },
358                 { LOG_ERR, "ERR" },
359                 { LOG_WARNING, "WARNING" },
360                 { LOG_NOTICE, "NOTICE" },
361                 { LOG_INFO, "INFO" },
362                 { LOG_DEBUG, "DEBUG" },
363                 { -1, NULL}
364         };
365
366         int priority;
367
368         priority = lp_parm_enum(SNUM(handle->conn), "full_audit", "priority",
369                                 enum_log_priorities, LOG_NOTICE);
370         if (priority == -1) {
371                 priority = LOG_WARNING;
372         }
373
374         return priority;
375 }
376
377 static char *audit_prefix(TALLOC_CTX *ctx, connection_struct *conn)
378 {
379         char *prefix = NULL;
380         char *result;
381
382         prefix = talloc_strdup(ctx,
383                         lp_parm_const_string(SNUM(conn), "full_audit",
384                                              "prefix", "%u|%I"));
385         if (!prefix) {
386                 return NULL;
387         }
388         result = talloc_sub_advanced(ctx,
389                         lp_servicename(talloc_tos(), SNUM(conn)),
390                         conn->session_info->unix_info->unix_name,
391                         conn->connectpath,
392                         conn->session_info->unix_token->gid,
393                         conn->session_info->unix_info->sanitized_username,
394                         conn->session_info->info->domain_name,
395                         prefix);
396         TALLOC_FREE(prefix);
397         return result;
398 }
399
400 static bool log_success(struct vfs_full_audit_private_data *pd, vfs_op_type op)
401 {
402         if (pd->success_ops == NULL) {
403                 return True;
404         }
405
406         return bitmap_query(pd->success_ops, op);
407 }
408
409 static bool log_failure(struct vfs_full_audit_private_data *pd, vfs_op_type op)
410 {
411         if (pd->failure_ops == NULL)
412                 return True;
413
414         return bitmap_query(pd->failure_ops, op);
415 }
416
417 static struct bitmap *init_bitmap(TALLOC_CTX *mem_ctx, const char **ops)
418 {
419         struct bitmap *bm;
420
421         if (ops == NULL) {
422                 return NULL;
423         }
424
425         bm = bitmap_talloc(mem_ctx, SMB_VFS_OP_LAST);
426         if (bm == NULL) {
427                 DEBUG(0, ("Could not alloc bitmap -- "
428                           "defaulting to logging everything\n"));
429                 return NULL;
430         }
431
432         for (; *ops != NULL; ops += 1) {
433                 int i;
434                 bool neg = false;
435                 const char *op;
436
437                 if (strequal(*ops, "all")) {
438                         for (i=0; i<SMB_VFS_OP_LAST; i++) {
439                                 bitmap_set(bm, i);
440                         }
441                         continue;
442                 }
443
444                 if (strequal(*ops, "none")) {
445                         break;
446                 }
447
448                 op = ops[0];
449                 if (op[0] == '!') {
450                         neg = true;
451                         op += 1;
452                 }
453
454                 for (i=0; i<SMB_VFS_OP_LAST; i++) {
455                         if ((vfs_op_names[i].name == NULL)
456                          || (vfs_op_names[i].type != i)) {
457                                 smb_panic("vfs_full_audit.c: name table not "
458                                           "in sync with vfs_op_type enums\n");
459                         }
460                         if (strequal(op, vfs_op_names[i].name)) {
461                                 if (neg) {
462                                         bitmap_clear(bm, i);
463                                 } else {
464                                         bitmap_set(bm, i);
465                                 }
466                                 break;
467                         }
468                 }
469                 if (i == SMB_VFS_OP_LAST) {
470                         DEBUG(0, ("Could not find opname %s, logging all\n",
471                                   *ops));
472                         TALLOC_FREE(bm);
473                         return NULL;
474                 }
475         }
476         return bm;
477 }
478
479 static const char *audit_opname(vfs_op_type op)
480 {
481         if (op >= SMB_VFS_OP_LAST)
482                 return "INVALID VFS OP";
483         return vfs_op_names[op].name;
484 }
485
486 static TALLOC_CTX *tmp_do_log_ctx;
487 /*
488  * Get us a temporary talloc context usable just for DEBUG arguments
489  */
490 static TALLOC_CTX *do_log_ctx(void)
491 {
492         if (tmp_do_log_ctx == NULL) {
493                 tmp_do_log_ctx = talloc_named_const(NULL, 0, "do_log_ctx");
494         }
495         return tmp_do_log_ctx;
496 }
497
498 static void do_log(vfs_op_type op, bool success, vfs_handle_struct *handle,
499                    const char *format, ...)
500 {
501         struct vfs_full_audit_private_data *pd;
502         fstring err_msg;
503         char *audit_pre = NULL;
504         va_list ap;
505         char *op_msg = NULL;
506
507         SMB_VFS_HANDLE_GET_DATA(handle, pd,
508                                 struct vfs_full_audit_private_data,
509                                 return;);
510
511         if (success && (!log_success(pd, op)))
512                 goto out;
513
514         if (!success && (!log_failure(pd, op)))
515                 goto out;
516
517         if (success)
518                 fstrcpy(err_msg, "ok");
519         else
520                 fstr_sprintf(err_msg, "fail (%s)", strerror(errno));
521
522         va_start(ap, format);
523         op_msg = talloc_vasprintf(talloc_tos(), format, ap);
524         va_end(ap);
525
526         if (!op_msg) {
527                 goto out;
528         }
529
530         audit_pre = audit_prefix(talloc_tos(), handle->conn);
531
532         if (pd->do_syslog) {
533                 int priority;
534
535                 /*
536                  * Specify the facility to interoperate with other syslog
537                  * callers (smbd for example).
538                  */
539                 priority = pd->syslog_priority | pd->syslog_facility;
540
541                 syslog(priority, "%s|%s|%s|%s\n",
542                        audit_pre ? audit_pre : "",
543                        audit_opname(op), err_msg, op_msg);
544         } else {
545                 DEBUG(1, ("%s|%s|%s|%s\n",
546                           audit_pre ? audit_pre : "",
547                           audit_opname(op), err_msg, op_msg));
548         }
549  out:
550         TALLOC_FREE(audit_pre);
551         TALLOC_FREE(op_msg);
552         TALLOC_FREE(tmp_do_log_ctx);
553 }
554
555 /**
556  * Return a string using the do_log_ctx()
557  */
558 static const char *smb_fname_str_do_log(const struct smb_filename *smb_fname)
559 {
560         char *fname = NULL;
561         NTSTATUS status;
562
563         if (smb_fname == NULL) {
564                 return "";
565         }
566         status = get_full_smb_filename(do_log_ctx(), smb_fname, &fname);
567         if (!NT_STATUS_IS_OK(status)) {
568                 return "";
569         }
570         return fname;
571 }
572
573 /**
574  * Return an fsp debug string using the do_log_ctx()
575  */
576 static const char *fsp_str_do_log(const struct files_struct *fsp)
577 {
578         return smb_fname_str_do_log(fsp->fsp_name);
579 }
580
581 /* Implementation of vfs_ops.  Pass everything on to the default
582    operation but log event first. */
583
584 static int smb_full_audit_connect(vfs_handle_struct *handle,
585                          const char *svc, const char *user)
586 {
587         int result;
588         struct vfs_full_audit_private_data *pd = NULL;
589
590         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
591         if (result < 0) {
592                 return result;
593         }
594
595         pd = talloc_zero(handle, struct vfs_full_audit_private_data);
596         if (!pd) {
597                 SMB_VFS_NEXT_DISCONNECT(handle);
598                 return -1;
599         }
600
601         pd->syslog_facility = audit_syslog_facility(handle);
602         if (pd->syslog_facility == -1) {
603                 DEBUG(1, ("%s: Unknown facility %s\n", __func__,
604                           lp_parm_const_string(SNUM(handle->conn),
605                                                "full_audit", "facility",
606                                                "USER")));
607                 SMB_VFS_NEXT_DISCONNECT(handle);
608                 return -1;
609         }
610
611         pd->syslog_priority = audit_syslog_priority(handle);
612
613         pd->log_secdesc = lp_parm_bool(SNUM(handle->conn),
614                                        "full_audit", "log_secdesc", false);
615
616         pd->do_syslog = lp_parm_bool(SNUM(handle->conn),
617                                      "full_audit", "syslog", true);
618
619 #ifdef WITH_SYSLOG
620         if (pd->do_syslog) {
621                 openlog("smbd_audit", 0, pd->syslog_facility);
622         }
623 #endif
624
625         pd->success_ops = init_bitmap(
626                 pd, lp_parm_string_list(SNUM(handle->conn), "full_audit",
627                                         "success", NULL));
628         pd->failure_ops = init_bitmap(
629                 pd, lp_parm_string_list(SNUM(handle->conn), "full_audit",
630                                         "failure", NULL));
631
632         /* Store the private data. */
633         SMB_VFS_HANDLE_SET_DATA(handle, pd, NULL,
634                                 struct vfs_full_audit_private_data, return -1);
635
636         do_log(SMB_VFS_OP_CONNECT, True, handle,
637                "%s", svc);
638
639         return 0;
640 }
641
642 static void smb_full_audit_disconnect(vfs_handle_struct *handle)
643 {
644         SMB_VFS_NEXT_DISCONNECT(handle);
645
646         do_log(SMB_VFS_OP_DISCONNECT, True, handle,
647                "%s", lp_servicename(talloc_tos(), SNUM(handle->conn)));
648
649         /* The bitmaps will be disconnected when the private
650            data is deleted. */
651 }
652
653 static uint64_t smb_full_audit_disk_free(vfs_handle_struct *handle,
654                                     const char *path, uint64_t *bsize,
655                                     uint64_t *dfree, uint64_t *dsize)
656 {
657         uint64_t result;
658
659         result = SMB_VFS_NEXT_DISK_FREE(handle, path, bsize, dfree, dsize);
660
661         /* Don't have a reasonable notion of failure here */
662
663         do_log(SMB_VFS_OP_DISK_FREE, True, handle, "%s", path);
664
665         return result;
666 }
667
668 static int smb_full_audit_get_quota(struct vfs_handle_struct *handle,
669                                     const char *path, enum SMB_QUOTA_TYPE qtype,
670                                     unid_t id, SMB_DISK_QUOTA *qt)
671 {
672         int result;
673
674         result = SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, qt);
675
676         do_log(SMB_VFS_OP_GET_QUOTA, (result >= 0), handle, "%s", path);
677
678         return result;
679 }
680
681 static int smb_full_audit_set_quota(struct vfs_handle_struct *handle,
682                            enum SMB_QUOTA_TYPE qtype, unid_t id,
683                            SMB_DISK_QUOTA *qt)
684 {
685         int result;
686
687         result = SMB_VFS_NEXT_SET_QUOTA(handle, qtype, id, qt);
688
689         do_log(SMB_VFS_OP_SET_QUOTA, (result >= 0), handle, "");
690
691         return result;
692 }
693
694 static int smb_full_audit_get_shadow_copy_data(struct vfs_handle_struct *handle,
695                                 struct files_struct *fsp,
696                                 struct shadow_copy_data *shadow_copy_data,
697                                 bool labels)
698 {
699         int result;
700
701         result = SMB_VFS_NEXT_GET_SHADOW_COPY_DATA(handle, fsp, shadow_copy_data, labels);
702
703         do_log(SMB_VFS_OP_GET_SHADOW_COPY_DATA, (result >= 0), handle, "");
704
705         return result;
706 }
707
708 static int smb_full_audit_statvfs(struct vfs_handle_struct *handle,
709                                 const char *path,
710                                 struct vfs_statvfs_struct *statbuf)
711 {
712         int result;
713
714         result = SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
715
716         do_log(SMB_VFS_OP_STATVFS, (result >= 0), handle, "");
717
718         return result;
719 }
720
721 static uint32_t smb_full_audit_fs_capabilities(struct vfs_handle_struct *handle, enum timestamp_set_resolution *p_ts_res)
722 {
723         int result;
724
725         result = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
726
727         do_log(SMB_VFS_OP_FS_CAPABILITIES, true, handle, "");
728
729         return result;
730 }
731
732 static NTSTATUS smb_full_audit_snap_check_path(struct vfs_handle_struct *handle,
733                                                TALLOC_CTX *mem_ctx,
734                                                const char *service_path,
735                                                char **base_volume)
736 {
737         NTSTATUS status;
738
739         status = SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx, service_path,
740                                               base_volume);
741         do_log(SMB_VFS_OP_SNAP_CHECK_PATH, NT_STATUS_IS_OK(status),
742                handle, "");
743
744         return status;
745 }
746
747 static NTSTATUS smb_full_audit_snap_create(struct vfs_handle_struct *handle,
748                                            TALLOC_CTX *mem_ctx,
749                                            const char *base_volume,
750                                            time_t *tstamp,
751                                            bool rw,
752                                            char **base_path,
753                                            char **snap_path)
754 {
755         NTSTATUS status;
756
757         status = SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume, tstamp,
758                                           rw, base_path, snap_path);
759         do_log(SMB_VFS_OP_SNAP_CREATE, NT_STATUS_IS_OK(status), handle, "");
760
761         return status;
762 }
763
764 static NTSTATUS smb_full_audit_snap_delete(struct vfs_handle_struct *handle,
765                                            TALLOC_CTX *mem_ctx,
766                                            char *base_path,
767                                            char *snap_path)
768 {
769         NTSTATUS status;
770
771         status = SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx, base_path,
772                                           snap_path);
773         do_log(SMB_VFS_OP_SNAP_DELETE, NT_STATUS_IS_OK(status), handle, "");
774
775         return status;
776 }
777
778 static DIR *smb_full_audit_opendir(vfs_handle_struct *handle,
779                         const struct smb_filename *smb_fname,
780                         const char *mask,
781                         uint32_t attr)
782 {
783         DIR *result;
784
785         result = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
786
787         do_log(SMB_VFS_OP_OPENDIR, (result != NULL), handle, "%s",
788                 smb_fname->base_name);
789
790         return result;
791 }
792
793 static DIR *smb_full_audit_fdopendir(vfs_handle_struct *handle,
794                           files_struct *fsp, const char *mask, uint32_t attr)
795 {
796         DIR *result;
797
798         result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
799
800         do_log(SMB_VFS_OP_FDOPENDIR, (result != NULL), handle, "%s",
801                         fsp_str_do_log(fsp));
802
803         return result;
804 }
805
806 static struct dirent *smb_full_audit_readdir(vfs_handle_struct *handle,
807                                     DIR *dirp, SMB_STRUCT_STAT *sbuf)
808 {
809         struct dirent *result;
810
811         result = SMB_VFS_NEXT_READDIR(handle, dirp, sbuf);
812
813         /* This operation has no reasonable error condition
814          * (End of dir is also failure), so always succeed.
815          */
816         do_log(SMB_VFS_OP_READDIR, True, handle, "");
817
818         return result;
819 }
820
821 static void smb_full_audit_seekdir(vfs_handle_struct *handle,
822                         DIR *dirp, long offset)
823 {
824         SMB_VFS_NEXT_SEEKDIR(handle, dirp, offset);
825
826         do_log(SMB_VFS_OP_SEEKDIR, True, handle, "");
827 }
828
829 static long smb_full_audit_telldir(vfs_handle_struct *handle,
830                         DIR *dirp)
831 {
832         long result;
833
834         result = SMB_VFS_NEXT_TELLDIR(handle, dirp);
835
836         do_log(SMB_VFS_OP_TELLDIR, True, handle, "");
837
838         return result;
839 }
840
841 static void smb_full_audit_rewinddir(vfs_handle_struct *handle,
842                         DIR *dirp)
843 {
844         SMB_VFS_NEXT_REWINDDIR(handle, dirp);
845
846         do_log(SMB_VFS_OP_REWINDDIR, True, handle, "");
847 }
848
849 static int smb_full_audit_mkdir(vfs_handle_struct *handle,
850                        const struct smb_filename *smb_fname, mode_t mode)
851 {
852         int result;
853         
854         result = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
855         
856         do_log(SMB_VFS_OP_MKDIR, (result >= 0), handle, "%s",
857                 smb_fname->base_name);
858
859         return result;
860 }
861
862 static int smb_full_audit_rmdir(vfs_handle_struct *handle,
863                        const struct smb_filename *smb_fname)
864 {
865         int result;
866         
867         result = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
868
869         do_log(SMB_VFS_OP_RMDIR, (result >= 0), handle, "%s",
870                 smb_fname->base_name);
871
872         return result;
873 }
874
875 static int smb_full_audit_closedir(vfs_handle_struct *handle,
876                           DIR *dirp)
877 {
878         int result;
879
880         result = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
881         
882         do_log(SMB_VFS_OP_CLOSEDIR, (result >= 0), handle, "");
883
884         return result;
885 }
886
887 static void smb_full_audit_init_search_op(vfs_handle_struct *handle,
888                         DIR *dirp)
889 {
890         SMB_VFS_NEXT_INIT_SEARCH_OP(handle, dirp);
891
892         do_log(SMB_VFS_OP_INIT_SEARCH_OP, True, handle, "");
893 }
894
895 static int smb_full_audit_open(vfs_handle_struct *handle,
896                                struct smb_filename *smb_fname,
897                                files_struct *fsp, int flags, mode_t mode)
898 {
899         int result;
900         
901         result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
902
903         do_log(SMB_VFS_OP_OPEN, (result >= 0), handle, "%s|%s",
904                ((flags & O_WRONLY) || (flags & O_RDWR))?"w":"r",
905                smb_fname_str_do_log(smb_fname));
906
907         return result;
908 }
909
910 static NTSTATUS smb_full_audit_create_file(vfs_handle_struct *handle,
911                                       struct smb_request *req,
912                                       uint16_t root_dir_fid,
913                                       struct smb_filename *smb_fname,
914                                       uint32_t access_mask,
915                                       uint32_t share_access,
916                                       uint32_t create_disposition,
917                                       uint32_t create_options,
918                                       uint32_t file_attributes,
919                                       uint32_t oplock_request,
920                                       struct smb2_lease *lease,
921                                       uint64_t allocation_size,
922                                       uint32_t private_flags,
923                                       struct security_descriptor *sd,
924                                       struct ea_list *ea_list,
925                                       files_struct **result_fsp,
926                                       int *pinfo,
927                                       const struct smb2_create_blobs *in_context_blobs,
928                                       struct smb2_create_blobs *out_context_blobs)
929 {
930         NTSTATUS result;
931         const char* str_create_disposition;
932
933         switch (create_disposition) {
934         case FILE_SUPERSEDE:
935                 str_create_disposition = "supersede";
936                 break;
937         case FILE_OVERWRITE_IF:
938                 str_create_disposition = "overwrite_if";
939                 break;
940         case FILE_OPEN:
941                 str_create_disposition = "open";
942                 break;
943         case FILE_OVERWRITE:
944                 str_create_disposition = "overwrite";
945                 break;
946         case FILE_CREATE:
947                 str_create_disposition = "create";
948                 break;
949         case FILE_OPEN_IF:
950                 str_create_disposition = "open_if";
951                 break;
952         default:
953                 str_create_disposition = "unknown";
954         }
955
956         result = SMB_VFS_NEXT_CREATE_FILE(
957                 handle,                                 /* handle */
958                 req,                                    /* req */
959                 root_dir_fid,                           /* root_dir_fid */
960                 smb_fname,                              /* fname */
961                 access_mask,                            /* access_mask */
962                 share_access,                           /* share_access */
963                 create_disposition,                     /* create_disposition*/
964                 create_options,                         /* create_options */
965                 file_attributes,                        /* file_attributes */
966                 oplock_request,                         /* oplock_request */
967                 lease,                                  /* lease */
968                 allocation_size,                        /* allocation_size */
969                 private_flags,
970                 sd,                                     /* sd */
971                 ea_list,                                /* ea_list */
972                 result_fsp,                             /* result */
973                 pinfo,                                  /* pinfo */
974                 in_context_blobs, out_context_blobs);   /* create context */
975
976         do_log(SMB_VFS_OP_CREATE_FILE, (NT_STATUS_IS_OK(result)), handle,
977                "0x%x|%s|%s|%s", access_mask,
978                create_options & FILE_DIRECTORY_FILE ? "dir" : "file",
979                str_create_disposition, smb_fname_str_do_log(smb_fname));
980
981         return result;
982 }
983
984 static int smb_full_audit_close(vfs_handle_struct *handle, files_struct *fsp)
985 {
986         int result;
987         
988         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
989
990         do_log(SMB_VFS_OP_CLOSE, (result >= 0), handle, "%s",
991                fsp_str_do_log(fsp));
992
993         return result;
994 }
995
996 static ssize_t smb_full_audit_read(vfs_handle_struct *handle, files_struct *fsp,
997                           void *data, size_t n)
998 {
999         ssize_t result;
1000
1001         result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
1002
1003         do_log(SMB_VFS_OP_READ, (result >= 0), handle, "%s",
1004                fsp_str_do_log(fsp));
1005
1006         return result;
1007 }
1008
1009 static ssize_t smb_full_audit_pread(vfs_handle_struct *handle, files_struct *fsp,
1010                            void *data, size_t n, off_t offset)
1011 {
1012         ssize_t result;
1013
1014         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1015
1016         do_log(SMB_VFS_OP_PREAD, (result >= 0), handle, "%s",
1017                fsp_str_do_log(fsp));
1018
1019         return result;
1020 }
1021
1022 struct smb_full_audit_pread_state {
1023         vfs_handle_struct *handle;
1024         files_struct *fsp;
1025         ssize_t ret;
1026         struct vfs_aio_state vfs_aio_state;
1027 };
1028
1029 static void smb_full_audit_pread_done(struct tevent_req *subreq);
1030
1031 static struct tevent_req *smb_full_audit_pread_send(
1032         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1033         struct tevent_context *ev, struct files_struct *fsp,
1034         void *data, size_t n, off_t offset)
1035 {
1036         struct tevent_req *req, *subreq;
1037         struct smb_full_audit_pread_state *state;
1038
1039         req = tevent_req_create(mem_ctx, &state,
1040                                 struct smb_full_audit_pread_state);
1041         if (req == NULL) {
1042                 do_log(SMB_VFS_OP_PREAD_SEND, false, handle, "%s",
1043                        fsp_str_do_log(fsp));
1044                 return NULL;
1045         }
1046         state->handle = handle;
1047         state->fsp = fsp;
1048
1049         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
1050                                          n, offset);
1051         if (tevent_req_nomem(subreq, req)) {
1052                 do_log(SMB_VFS_OP_PREAD_SEND, false, handle, "%s",
1053                        fsp_str_do_log(fsp));
1054                 return tevent_req_post(req, ev);
1055         }
1056         tevent_req_set_callback(subreq, smb_full_audit_pread_done, req);
1057
1058         do_log(SMB_VFS_OP_PREAD_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1059         return req;
1060 }
1061
1062 static void smb_full_audit_pread_done(struct tevent_req *subreq)
1063 {
1064         struct tevent_req *req = tevent_req_callback_data(
1065                 subreq, struct tevent_req);
1066         struct smb_full_audit_pread_state *state = tevent_req_data(
1067                 req, struct smb_full_audit_pread_state);
1068
1069         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1070         TALLOC_FREE(subreq);
1071         tevent_req_done(req);
1072 }
1073
1074 static ssize_t smb_full_audit_pread_recv(struct tevent_req *req,
1075                                          struct vfs_aio_state *vfs_aio_state)
1076 {
1077         struct smb_full_audit_pread_state *state = tevent_req_data(
1078                 req, struct smb_full_audit_pread_state);
1079
1080         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1081                 do_log(SMB_VFS_OP_PREAD_RECV, false, state->handle, "%s",
1082                        fsp_str_do_log(state->fsp));
1083                 return -1;
1084         }
1085
1086         do_log(SMB_VFS_OP_PREAD_RECV, (state->ret >= 0), state->handle, "%s",
1087                fsp_str_do_log(state->fsp));
1088
1089         *vfs_aio_state = state->vfs_aio_state;
1090         return state->ret;
1091 }
1092
1093 static ssize_t smb_full_audit_write(vfs_handle_struct *handle, files_struct *fsp,
1094                            const void *data, size_t n)
1095 {
1096         ssize_t result;
1097
1098         result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
1099
1100         do_log(SMB_VFS_OP_WRITE, (result >= 0), handle, "%s",
1101                fsp_str_do_log(fsp));
1102
1103         return result;
1104 }
1105
1106 static ssize_t smb_full_audit_pwrite(vfs_handle_struct *handle, files_struct *fsp,
1107                             const void *data, size_t n,
1108                             off_t offset)
1109 {
1110         ssize_t result;
1111
1112         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
1113
1114         do_log(SMB_VFS_OP_PWRITE, (result >= 0), handle, "%s",
1115                fsp_str_do_log(fsp));
1116
1117         return result;
1118 }
1119
1120 struct smb_full_audit_pwrite_state {
1121         vfs_handle_struct *handle;
1122         files_struct *fsp;
1123         ssize_t ret;
1124         struct vfs_aio_state vfs_aio_state;
1125 };
1126
1127 static void smb_full_audit_pwrite_done(struct tevent_req *subreq);
1128
1129 static struct tevent_req *smb_full_audit_pwrite_send(
1130         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1131         struct tevent_context *ev, struct files_struct *fsp,
1132         const void *data, size_t n, off_t offset)
1133 {
1134         struct tevent_req *req, *subreq;
1135         struct smb_full_audit_pwrite_state *state;
1136
1137         req = tevent_req_create(mem_ctx, &state,
1138                                 struct smb_full_audit_pwrite_state);
1139         if (req == NULL) {
1140                 do_log(SMB_VFS_OP_PWRITE_SEND, false, handle, "%s",
1141                        fsp_str_do_log(fsp));
1142                 return NULL;
1143         }
1144         state->handle = handle;
1145         state->fsp = fsp;
1146
1147         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
1148                                          n, offset);
1149         if (tevent_req_nomem(subreq, req)) {
1150                 do_log(SMB_VFS_OP_PWRITE_SEND, false, handle, "%s",
1151                        fsp_str_do_log(fsp));
1152                 return tevent_req_post(req, ev);
1153         }
1154         tevent_req_set_callback(subreq, smb_full_audit_pwrite_done, req);
1155
1156         do_log(SMB_VFS_OP_PWRITE_SEND, true, handle, "%s",
1157                fsp_str_do_log(fsp));
1158         return req;
1159 }
1160
1161 static void smb_full_audit_pwrite_done(struct tevent_req *subreq)
1162 {
1163         struct tevent_req *req = tevent_req_callback_data(
1164                 subreq, struct tevent_req);
1165         struct smb_full_audit_pwrite_state *state = tevent_req_data(
1166                 req, struct smb_full_audit_pwrite_state);
1167
1168         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1169         TALLOC_FREE(subreq);
1170         tevent_req_done(req);
1171 }
1172
1173 static ssize_t smb_full_audit_pwrite_recv(struct tevent_req *req,
1174                                           struct vfs_aio_state *vfs_aio_state)
1175 {
1176         struct smb_full_audit_pwrite_state *state = tevent_req_data(
1177                 req, struct smb_full_audit_pwrite_state);
1178
1179         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1180                 do_log(SMB_VFS_OP_PWRITE_RECV, false, state->handle, "%s",
1181                        fsp_str_do_log(state->fsp));
1182                 return -1;
1183         }
1184
1185         do_log(SMB_VFS_OP_PWRITE_RECV, (state->ret >= 0), state->handle, "%s",
1186                fsp_str_do_log(state->fsp));
1187
1188         *vfs_aio_state = state->vfs_aio_state;
1189         return state->ret;
1190 }
1191
1192 static off_t smb_full_audit_lseek(vfs_handle_struct *handle, files_struct *fsp,
1193                              off_t offset, int whence)
1194 {
1195         ssize_t result;
1196
1197         result = SMB_VFS_NEXT_LSEEK(handle, fsp, offset, whence);
1198
1199         do_log(SMB_VFS_OP_LSEEK, (result != (ssize_t)-1), handle,
1200                "%s", fsp_str_do_log(fsp));
1201
1202         return result;
1203 }
1204
1205 static ssize_t smb_full_audit_sendfile(vfs_handle_struct *handle, int tofd,
1206                               files_struct *fromfsp,
1207                               const DATA_BLOB *hdr, off_t offset,
1208                               size_t n)
1209 {
1210         ssize_t result;
1211
1212         result = SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp, hdr, offset, n);
1213
1214         do_log(SMB_VFS_OP_SENDFILE, (result >= 0), handle,
1215                "%s", fsp_str_do_log(fromfsp));
1216
1217         return result;
1218 }
1219
1220 static ssize_t smb_full_audit_recvfile(vfs_handle_struct *handle, int fromfd,
1221                       files_struct *tofsp,
1222                               off_t offset,
1223                               size_t n)
1224 {
1225         ssize_t result;
1226
1227         result = SMB_VFS_NEXT_RECVFILE(handle, fromfd, tofsp, offset, n);
1228
1229         do_log(SMB_VFS_OP_RECVFILE, (result >= 0), handle,
1230                "%s", fsp_str_do_log(tofsp));
1231
1232         return result;
1233 }
1234
1235 static int smb_full_audit_rename(vfs_handle_struct *handle,
1236                                  const struct smb_filename *smb_fname_src,
1237                                  const struct smb_filename *smb_fname_dst)
1238 {
1239         int result;
1240         
1241         result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1242
1243         do_log(SMB_VFS_OP_RENAME, (result >= 0), handle, "%s|%s",
1244                smb_fname_str_do_log(smb_fname_src),
1245                smb_fname_str_do_log(smb_fname_dst));
1246
1247         return result;    
1248 }
1249
1250 static int smb_full_audit_fsync(vfs_handle_struct *handle, files_struct *fsp)
1251 {
1252         int result;
1253         
1254         result = SMB_VFS_NEXT_FSYNC(handle, fsp);
1255
1256         do_log(SMB_VFS_OP_FSYNC, (result >= 0), handle, "%s",
1257                fsp_str_do_log(fsp));
1258
1259         return result;    
1260 }
1261
1262 struct smb_full_audit_fsync_state {
1263         vfs_handle_struct *handle;
1264         files_struct *fsp;
1265         int ret;
1266         struct vfs_aio_state vfs_aio_state;
1267 };
1268
1269 static void smb_full_audit_fsync_done(struct tevent_req *subreq);
1270
1271 static struct tevent_req *smb_full_audit_fsync_send(
1272         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1273         struct tevent_context *ev, struct files_struct *fsp)
1274 {
1275         struct tevent_req *req, *subreq;
1276         struct smb_full_audit_fsync_state *state;
1277
1278         req = tevent_req_create(mem_ctx, &state,
1279                                 struct smb_full_audit_fsync_state);
1280         if (req == NULL) {
1281                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1282                        fsp_str_do_log(fsp));
1283                 return NULL;
1284         }
1285         state->handle = handle;
1286         state->fsp = fsp;
1287
1288         subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1289         if (tevent_req_nomem(subreq, req)) {
1290                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1291                        fsp_str_do_log(fsp));
1292                 return tevent_req_post(req, ev);
1293         }
1294         tevent_req_set_callback(subreq, smb_full_audit_fsync_done, req);
1295
1296         do_log(SMB_VFS_OP_FSYNC_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1297         return req;
1298 }
1299
1300 static void smb_full_audit_fsync_done(struct tevent_req *subreq)
1301 {
1302         struct tevent_req *req = tevent_req_callback_data(
1303                 subreq, struct tevent_req);
1304         struct smb_full_audit_fsync_state *state = tevent_req_data(
1305                 req, struct smb_full_audit_fsync_state);
1306
1307         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1308         TALLOC_FREE(subreq);
1309         tevent_req_done(req);
1310 }
1311
1312 static int smb_full_audit_fsync_recv(struct tevent_req *req,
1313                                      struct vfs_aio_state *vfs_aio_state)
1314 {
1315         struct smb_full_audit_fsync_state *state = tevent_req_data(
1316                 req, struct smb_full_audit_fsync_state);
1317
1318         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1319                 do_log(SMB_VFS_OP_FSYNC_RECV, false, state->handle, "%s",
1320                        fsp_str_do_log(state->fsp));
1321                 return -1;
1322         }
1323
1324         do_log(SMB_VFS_OP_FSYNC_RECV, (state->ret >= 0), state->handle, "%s",
1325                fsp_str_do_log(state->fsp));
1326
1327         *vfs_aio_state = state->vfs_aio_state;
1328         return state->ret;
1329 }
1330
1331 static int smb_full_audit_stat(vfs_handle_struct *handle,
1332                                struct smb_filename *smb_fname)
1333 {
1334         int result;
1335         
1336         result = SMB_VFS_NEXT_STAT(handle, smb_fname);
1337
1338         do_log(SMB_VFS_OP_STAT, (result >= 0), handle, "%s",
1339                smb_fname_str_do_log(smb_fname));
1340
1341         return result;    
1342 }
1343
1344 static int smb_full_audit_fstat(vfs_handle_struct *handle, files_struct *fsp,
1345                        SMB_STRUCT_STAT *sbuf)
1346 {
1347         int result;
1348         
1349         result = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1350
1351         do_log(SMB_VFS_OP_FSTAT, (result >= 0), handle, "%s",
1352                fsp_str_do_log(fsp));
1353
1354         return result;
1355 }
1356
1357 static int smb_full_audit_lstat(vfs_handle_struct *handle,
1358                                 struct smb_filename *smb_fname)
1359 {
1360         int result;
1361         
1362         result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1363
1364         do_log(SMB_VFS_OP_LSTAT, (result >= 0), handle, "%s",
1365                smb_fname_str_do_log(smb_fname));
1366
1367         return result;    
1368 }
1369
1370 static uint64_t smb_full_audit_get_alloc_size(vfs_handle_struct *handle,
1371                        files_struct *fsp, const SMB_STRUCT_STAT *sbuf)
1372 {
1373         uint64_t result;
1374
1375         result = SMB_VFS_NEXT_GET_ALLOC_SIZE(handle, fsp, sbuf);
1376
1377         do_log(SMB_VFS_OP_GET_ALLOC_SIZE, (result != (uint64_t)-1), handle,
1378                         "%llu", result);
1379
1380         return result;
1381 }
1382
1383 static int smb_full_audit_unlink(vfs_handle_struct *handle,
1384                                  const struct smb_filename *smb_fname)
1385 {
1386         int result;
1387         
1388         result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1389
1390         do_log(SMB_VFS_OP_UNLINK, (result >= 0), handle, "%s",
1391                smb_fname_str_do_log(smb_fname));
1392
1393         return result;
1394 }
1395
1396 static int smb_full_audit_chmod(vfs_handle_struct *handle,
1397                                 const struct smb_filename *smb_fname,
1398                                 mode_t mode)
1399 {
1400         int result;
1401
1402         result = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1403
1404         do_log(SMB_VFS_OP_CHMOD, (result >= 0), handle, "%s|%o",
1405                 smb_fname->base_name,
1406                 mode);
1407
1408         return result;
1409 }
1410
1411 static int smb_full_audit_fchmod(vfs_handle_struct *handle, files_struct *fsp,
1412                         mode_t mode)
1413 {
1414         int result;
1415         
1416         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1417
1418         do_log(SMB_VFS_OP_FCHMOD, (result >= 0), handle,
1419                "%s|%o", fsp_str_do_log(fsp), mode);
1420
1421         return result;
1422 }
1423
1424 static int smb_full_audit_chown(vfs_handle_struct *handle,
1425                         const struct smb_filename *smb_fname,
1426                         uid_t uid,
1427                         gid_t gid)
1428 {
1429         int result;
1430
1431         result = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1432
1433         do_log(SMB_VFS_OP_CHOWN, (result >= 0), handle, "%s|%ld|%ld",
1434                smb_fname->base_name, (long int)uid, (long int)gid);
1435
1436         return result;
1437 }
1438
1439 static int smb_full_audit_fchown(vfs_handle_struct *handle, files_struct *fsp,
1440                         uid_t uid, gid_t gid)
1441 {
1442         int result;
1443
1444         result = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1445
1446         do_log(SMB_VFS_OP_FCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1447                fsp_str_do_log(fsp), (long int)uid, (long int)gid);
1448
1449         return result;
1450 }
1451
1452 static int smb_full_audit_lchown(vfs_handle_struct *handle,
1453                         const struct smb_filename *smb_fname,
1454                         uid_t uid,
1455                         gid_t gid)
1456 {
1457         int result;
1458
1459         result = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1460
1461         do_log(SMB_VFS_OP_LCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1462                smb_fname->base_name, (long int)uid, (long int)gid);
1463
1464         return result;
1465 }
1466
1467 static int smb_full_audit_chdir(vfs_handle_struct *handle,
1468                        const char *path)
1469 {
1470         int result;
1471
1472         result = SMB_VFS_NEXT_CHDIR(handle, path);
1473
1474         do_log(SMB_VFS_OP_CHDIR, (result >= 0), handle, "chdir|%s", path);
1475
1476         return result;
1477 }
1478
1479 static char *smb_full_audit_getwd(vfs_handle_struct *handle)
1480 {
1481         char *result;
1482
1483         result = SMB_VFS_NEXT_GETWD(handle);
1484         
1485         do_log(SMB_VFS_OP_GETWD, (result != NULL), handle, "%s",
1486                 result == NULL? "" : result);
1487
1488         return result;
1489 }
1490
1491 static int smb_full_audit_ntimes(vfs_handle_struct *handle,
1492                                  const struct smb_filename *smb_fname,
1493                                  struct smb_file_time *ft)
1494 {
1495         int result;
1496
1497         result = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1498
1499         do_log(SMB_VFS_OP_NTIMES, (result >= 0), handle, "%s",
1500                smb_fname_str_do_log(smb_fname));
1501
1502         return result;
1503 }
1504
1505 static int smb_full_audit_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1506                            off_t len)
1507 {
1508         int result;
1509
1510         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1511
1512         do_log(SMB_VFS_OP_FTRUNCATE, (result >= 0), handle,
1513                "%s", fsp_str_do_log(fsp));
1514
1515         return result;
1516 }
1517
1518 static int smb_full_audit_fallocate(vfs_handle_struct *handle, files_struct *fsp,
1519                            uint32_t mode,
1520                            off_t offset,
1521                            off_t len)
1522 {
1523         int result;
1524
1525         result = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1526
1527         do_log(SMB_VFS_OP_FALLOCATE, (result >= 0), handle,
1528                "%s", fsp_str_do_log(fsp));
1529
1530         return result;
1531 }
1532
1533 static bool smb_full_audit_lock(vfs_handle_struct *handle, files_struct *fsp,
1534                        int op, off_t offset, off_t count, int type)
1535 {
1536         bool result;
1537
1538         result = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1539
1540         do_log(SMB_VFS_OP_LOCK, result, handle, "%s", fsp_str_do_log(fsp));
1541
1542         return result;
1543 }
1544
1545 static int smb_full_audit_kernel_flock(struct vfs_handle_struct *handle,
1546                                        struct files_struct *fsp,
1547                                        uint32_t share_mode, uint32_t access_mask)
1548 {
1549         int result;
1550
1551         result = SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp, share_mode, access_mask);
1552
1553         do_log(SMB_VFS_OP_KERNEL_FLOCK, (result >= 0), handle, "%s",
1554                fsp_str_do_log(fsp));
1555
1556         return result;
1557 }
1558
1559 static int smb_full_audit_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1560                                  int leasetype)
1561 {
1562         int result;
1563
1564         result = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1565
1566         do_log(SMB_VFS_OP_LINUX_SETLEASE, (result >= 0), handle, "%s",
1567                fsp_str_do_log(fsp));
1568
1569         return result;
1570 }
1571
1572 static bool smb_full_audit_getlock(vfs_handle_struct *handle, files_struct *fsp,
1573                        off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1574 {
1575         bool result;
1576
1577         result = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype, ppid);
1578
1579         do_log(SMB_VFS_OP_GETLOCK, result, handle, "%s", fsp_str_do_log(fsp));
1580
1581         return result;
1582 }
1583
1584 static int smb_full_audit_symlink(vfs_handle_struct *handle,
1585                          const char *oldpath, const char *newpath)
1586 {
1587         int result;
1588
1589         result = SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
1590
1591         do_log(SMB_VFS_OP_SYMLINK, (result >= 0), handle,
1592                "%s|%s", oldpath, newpath);
1593
1594         return result;
1595 }
1596
1597 static int smb_full_audit_readlink(vfs_handle_struct *handle,
1598                           const char *path, char *buf, size_t bufsiz)
1599 {
1600         int result;
1601
1602         result = SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
1603
1604         do_log(SMB_VFS_OP_READLINK, (result >= 0), handle, "%s", path);
1605
1606         return result;
1607 }
1608
1609 static int smb_full_audit_link(vfs_handle_struct *handle,
1610                       const char *oldpath, const char *newpath)
1611 {
1612         int result;
1613
1614         result = SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
1615
1616         do_log(SMB_VFS_OP_LINK, (result >= 0), handle,
1617                "%s|%s", oldpath, newpath);
1618
1619         return result;
1620 }
1621
1622 static int smb_full_audit_mknod(vfs_handle_struct *handle,
1623                        const char *pathname, mode_t mode, SMB_DEV_T dev)
1624 {
1625         int result;
1626
1627         result = SMB_VFS_NEXT_MKNOD(handle, pathname, mode, dev);
1628
1629         do_log(SMB_VFS_OP_MKNOD, (result >= 0), handle, "%s", pathname);
1630
1631         return result;
1632 }
1633
1634 static char *smb_full_audit_realpath(vfs_handle_struct *handle,
1635                             const char *path)
1636 {
1637         char *result;
1638
1639         result = SMB_VFS_NEXT_REALPATH(handle, path);
1640
1641         do_log(SMB_VFS_OP_REALPATH, (result != NULL), handle, "%s", path);
1642
1643         return result;
1644 }
1645
1646 static int smb_full_audit_chflags(vfs_handle_struct *handle,
1647                             const char *path, unsigned int flags)
1648 {
1649         int result;
1650
1651         result = SMB_VFS_NEXT_CHFLAGS(handle, path, flags);
1652
1653         do_log(SMB_VFS_OP_CHFLAGS, (result != 0), handle, "%s", path);
1654
1655         return result;
1656 }
1657
1658 static struct file_id smb_full_audit_file_id_create(struct vfs_handle_struct *handle,
1659                                                     const SMB_STRUCT_STAT *sbuf)
1660 {
1661         struct file_id id_zero;
1662         struct file_id result;
1663
1664         ZERO_STRUCT(id_zero);
1665
1666         result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
1667
1668         do_log(SMB_VFS_OP_FILE_ID_CREATE,
1669                !file_id_equal(&id_zero, &result),
1670                handle, "%s", file_id_string_tos(&result));
1671
1672         return result;
1673 }
1674
1675 static NTSTATUS smb_full_audit_streaminfo(vfs_handle_struct *handle,
1676                                           struct files_struct *fsp,
1677                                           const char *fname,
1678                                           TALLOC_CTX *mem_ctx,
1679                                           unsigned int *pnum_streams,
1680                                           struct stream_struct **pstreams)
1681 {
1682         NTSTATUS result;
1683
1684         result = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
1685                                          pnum_streams, pstreams);
1686
1687         do_log(SMB_VFS_OP_STREAMINFO, NT_STATUS_IS_OK(result), handle,
1688                "%s", fname);
1689
1690         return result;
1691 }
1692
1693 static int smb_full_audit_get_real_filename(struct vfs_handle_struct *handle,
1694                                             const char *path,
1695                                             const char *name,
1696                                             TALLOC_CTX *mem_ctx,
1697                                             char **found_name)
1698 {
1699         int result;
1700
1701         result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
1702                                                 found_name);
1703
1704         do_log(SMB_VFS_OP_GET_REAL_FILENAME, (result == 0), handle,
1705                "%s/%s->%s", path, name, (result == 0) ? "" : *found_name);
1706
1707         return result;
1708 }
1709
1710 static const char *smb_full_audit_connectpath(vfs_handle_struct *handle,
1711                                               const char *fname)
1712 {
1713         const char *result;
1714
1715         result = SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1716
1717         do_log(SMB_VFS_OP_CONNECTPATH, result != NULL, handle,
1718                "%s", fname);
1719
1720         return result;
1721 }
1722
1723 static NTSTATUS smb_full_audit_brl_lock_windows(struct vfs_handle_struct *handle,
1724                                                 struct byte_range_lock *br_lck,
1725                                                 struct lock_struct *plock,
1726                                                 bool blocking_lock)
1727 {
1728         NTSTATUS result;
1729
1730         result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock,
1731                                                blocking_lock);
1732
1733         do_log(SMB_VFS_OP_BRL_LOCK_WINDOWS, NT_STATUS_IS_OK(result), handle,
1734             "%s:%llu-%llu. type=%d. blocking=%d",
1735                fsp_str_do_log(brl_fsp(br_lck)),
1736             plock->start, plock->size, plock->lock_type, blocking_lock);
1737
1738         return result;
1739 }
1740
1741 static bool smb_full_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
1742                                               struct messaging_context *msg_ctx,
1743                                               struct byte_range_lock *br_lck,
1744                                               const struct lock_struct *plock)
1745 {
1746         bool result;
1747
1748         result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, msg_ctx, br_lck,
1749             plock);
1750
1751         do_log(SMB_VFS_OP_BRL_UNLOCK_WINDOWS, (result == 0), handle,
1752                "%s:%llu-%llu:%d", fsp_str_do_log(brl_fsp(br_lck)),
1753                plock->start,
1754             plock->size, plock->lock_type);
1755
1756         return result;
1757 }
1758
1759 static bool smb_full_audit_brl_cancel_windows(struct vfs_handle_struct *handle,
1760                                               struct byte_range_lock *br_lck,
1761                                               struct lock_struct *plock)
1762 {
1763         bool result;
1764
1765         result = SMB_VFS_NEXT_BRL_CANCEL_WINDOWS(handle, br_lck, plock);
1766
1767         do_log(SMB_VFS_OP_BRL_CANCEL_WINDOWS, (result == 0), handle,
1768                "%s:%llu-%llu:%d", fsp_str_do_log(brl_fsp(br_lck)),
1769                plock->start,
1770             plock->size, plock->lock_type);
1771
1772         return result;
1773 }
1774
1775 static bool smb_full_audit_strict_lock(struct vfs_handle_struct *handle,
1776                                        struct files_struct *fsp,
1777                                        struct lock_struct *plock)
1778 {
1779         bool result;
1780
1781         result = SMB_VFS_NEXT_STRICT_LOCK(handle, fsp, plock);
1782
1783         do_log(SMB_VFS_OP_STRICT_LOCK, result, handle,
1784             "%s:%llu-%llu:%d", fsp_str_do_log(fsp), plock->start,
1785             plock->size, plock->lock_type);
1786
1787         return result;
1788 }
1789
1790 static void smb_full_audit_strict_unlock(struct vfs_handle_struct *handle,
1791                                          struct files_struct *fsp,
1792                                          struct lock_struct *plock)
1793 {
1794         SMB_VFS_NEXT_STRICT_UNLOCK(handle, fsp, plock);
1795
1796         do_log(SMB_VFS_OP_STRICT_UNLOCK, true, handle,
1797             "%s:%llu-%llu:%d", fsp_str_do_log(fsp), plock->start,
1798             plock->size, plock->lock_type);
1799 }
1800
1801 static NTSTATUS smb_full_audit_translate_name(struct vfs_handle_struct *handle,
1802                                               const char *name,
1803                                               enum vfs_translate_direction direction,
1804                                               TALLOC_CTX *mem_ctx,
1805                                               char **mapped_name)
1806 {
1807         NTSTATUS result;
1808
1809         result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
1810                                              mapped_name);
1811
1812         do_log(SMB_VFS_OP_TRANSLATE_NAME, NT_STATUS_IS_OK(result), handle, "");
1813
1814         return result;
1815 }
1816
1817 static struct tevent_req *smb_full_audit_copy_chunk_send(struct vfs_handle_struct *handle,
1818                                                          TALLOC_CTX *mem_ctx,
1819                                                          struct tevent_context *ev,
1820                                                          struct files_struct *src_fsp,
1821                                                          off_t src_off,
1822                                                          struct files_struct *dest_fsp,
1823                                                          off_t dest_off,
1824                                                          off_t num)
1825 {
1826         struct tevent_req *req;
1827
1828         req = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle, mem_ctx, ev, src_fsp,
1829                                            src_off, dest_fsp, dest_off, num);
1830
1831         do_log(SMB_VFS_OP_COPY_CHUNK_SEND, req, handle, "");
1832
1833         return req;
1834 }
1835
1836 static NTSTATUS smb_full_audit_copy_chunk_recv(struct vfs_handle_struct *handle,
1837                                                struct tevent_req *req,
1838                                                off_t *copied)
1839 {
1840         NTSTATUS result;
1841
1842         result = SMB_VFS_NEXT_COPY_CHUNK_RECV(handle, req, copied);
1843
1844         do_log(SMB_VFS_OP_COPY_CHUNK_RECV, NT_STATUS_IS_OK(result), handle, "");
1845
1846         return result;
1847 }
1848
1849 static NTSTATUS smb_full_audit_get_compression(vfs_handle_struct *handle,
1850                                                TALLOC_CTX *mem_ctx,
1851                                                struct files_struct *fsp,
1852                                                struct smb_filename *smb_fname,
1853                                                uint16_t *_compression_fmt)
1854 {
1855         NTSTATUS result;
1856
1857         result = SMB_VFS_NEXT_GET_COMPRESSION(handle, mem_ctx, fsp, smb_fname,
1858                                               _compression_fmt);
1859
1860         do_log(SMB_VFS_OP_GET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
1861                "%s",
1862                (fsp ? fsp_str_do_log(fsp) : smb_fname_str_do_log(smb_fname)));
1863
1864         return result;
1865 }
1866
1867 static NTSTATUS smb_full_audit_set_compression(vfs_handle_struct *handle,
1868                                                TALLOC_CTX *mem_ctx,
1869                                                struct files_struct *fsp,
1870                                                uint16_t compression_fmt)
1871 {
1872         NTSTATUS result;
1873
1874         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
1875                                               compression_fmt);
1876
1877         do_log(SMB_VFS_OP_SET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
1878                "%s", fsp_str_do_log(fsp));
1879
1880         return result;
1881 }
1882
1883 static NTSTATUS smb_full_audit_readdir_attr(struct vfs_handle_struct *handle,
1884                                             const struct smb_filename *fname,
1885                                             TALLOC_CTX *mem_ctx,
1886                                             struct readdir_attr_data **pattr_data)
1887 {
1888         NTSTATUS status;
1889
1890         status = SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
1891
1892         do_log(SMB_VFS_OP_READDIR_ATTR, NT_STATUS_IS_OK(status), handle, "%s",
1893                smb_fname_str_do_log(fname));
1894
1895         return status;
1896 }
1897
1898 static NTSTATUS smb_full_audit_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
1899                                            uint32_t security_info,
1900                                            TALLOC_CTX *mem_ctx,
1901                                            struct security_descriptor **ppdesc)
1902 {
1903         NTSTATUS result;
1904
1905         result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1906                                           mem_ctx, ppdesc);
1907
1908         do_log(SMB_VFS_OP_FGET_NT_ACL, NT_STATUS_IS_OK(result), handle,
1909                "%s", fsp_str_do_log(fsp));
1910
1911         return result;
1912 }
1913
1914 static NTSTATUS smb_full_audit_get_nt_acl(vfs_handle_struct *handle,
1915                                           const struct smb_filename *smb_fname,
1916                                           uint32_t security_info,
1917                                           TALLOC_CTX *mem_ctx,
1918                                           struct security_descriptor **ppdesc)
1919 {
1920         NTSTATUS result;
1921
1922         result = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1923                                          mem_ctx, ppdesc);
1924
1925         do_log(SMB_VFS_OP_GET_NT_ACL, NT_STATUS_IS_OK(result), handle,
1926                "%s", smb_fname_str_do_log(smb_fname));
1927
1928         return result;
1929 }
1930
1931 static NTSTATUS smb_full_audit_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
1932                               uint32_t security_info_sent,
1933                               const struct security_descriptor *psd)
1934 {
1935         struct vfs_full_audit_private_data *pd;
1936         NTSTATUS result;
1937         char *sd = NULL;
1938
1939         SMB_VFS_HANDLE_GET_DATA(handle, pd,
1940                                 struct vfs_full_audit_private_data,
1941                                 return NT_STATUS_INTERNAL_ERROR);
1942
1943         if (pd->log_secdesc) {
1944                 sd = sddl_encode(talloc_tos(), psd, get_global_sam_sid());
1945         }
1946
1947         result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1948
1949         do_log(SMB_VFS_OP_FSET_NT_ACL, NT_STATUS_IS_OK(result), handle,
1950                "%s [%s]", fsp_str_do_log(fsp), sd ? sd : "");
1951
1952         TALLOC_FREE(sd);
1953
1954         return result;
1955 }
1956
1957 static int smb_full_audit_chmod_acl(vfs_handle_struct *handle,
1958                                 const struct smb_filename *smb_fname,
1959                                 mode_t mode)
1960 {
1961         int result;
1962         
1963         result = SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1964
1965         do_log(SMB_VFS_OP_CHMOD_ACL, (result >= 0), handle,
1966                "%s|%o", smb_fname->base_name, mode);
1967
1968         return result;
1969 }
1970
1971 static int smb_full_audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp,
1972                                      mode_t mode)
1973 {
1974         int result;
1975         
1976         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1977
1978         do_log(SMB_VFS_OP_FCHMOD_ACL, (result >= 0), handle,
1979                "%s|%o", fsp_str_do_log(fsp), mode);
1980
1981         return result;
1982 }
1983
1984 static SMB_ACL_T smb_full_audit_sys_acl_get_file(vfs_handle_struct *handle,
1985                                         const char *path_p,
1986                                                  SMB_ACL_TYPE_T type,
1987                                                  TALLOC_CTX *mem_ctx)
1988 {
1989         SMB_ACL_T result;
1990
1991         result = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p, type, mem_ctx);
1992
1993         do_log(SMB_VFS_OP_SYS_ACL_GET_FILE, (result != NULL), handle,
1994                "%s", path_p);
1995
1996         return result;
1997 }
1998
1999 static SMB_ACL_T smb_full_audit_sys_acl_get_fd(vfs_handle_struct *handle,
2000                                                files_struct *fsp, TALLOC_CTX *mem_ctx)
2001 {
2002         SMB_ACL_T result;
2003
2004         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
2005
2006         do_log(SMB_VFS_OP_SYS_ACL_GET_FD, (result != NULL), handle,
2007                "%s", fsp_str_do_log(fsp));
2008
2009         return result;
2010 }
2011
2012 static int smb_full_audit_sys_acl_blob_get_file(vfs_handle_struct *handle,
2013                                                 const char *path_p,
2014                                                 TALLOC_CTX *mem_ctx,
2015                                                 char **blob_description,
2016                                                 DATA_BLOB *blob)
2017 {
2018         int result;
2019
2020         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, path_p, mem_ctx, blob_description, blob);
2021
2022         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FILE, (result >= 0), handle,
2023                "%s", path_p);
2024
2025         return result;
2026 }
2027
2028 static int smb_full_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
2029                                               files_struct *fsp,
2030                                               TALLOC_CTX *mem_ctx,
2031                                               char **blob_description,
2032                                               DATA_BLOB *blob)
2033 {
2034         int result;
2035
2036         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
2037
2038         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FD, (result >= 0), handle,
2039                "%s", fsp_str_do_log(fsp));
2040
2041         return result;
2042 }
2043
2044 static int smb_full_audit_sys_acl_set_file(vfs_handle_struct *handle,
2045
2046                                   const char *name, SMB_ACL_TYPE_T acltype,
2047                                   SMB_ACL_T theacl)
2048 {
2049         int result;
2050
2051         result = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, acltype,
2052                                                theacl);
2053
2054         do_log(SMB_VFS_OP_SYS_ACL_SET_FILE, (result >= 0), handle,
2055                "%s", name);
2056
2057         return result;
2058 }
2059
2060 static int smb_full_audit_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp,
2061                                 SMB_ACL_T theacl)
2062 {
2063         int result;
2064
2065         result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
2066
2067         do_log(SMB_VFS_OP_SYS_ACL_SET_FD, (result >= 0), handle,
2068                "%s", fsp_str_do_log(fsp));
2069
2070         return result;
2071 }
2072
2073 static int smb_full_audit_sys_acl_delete_def_file(vfs_handle_struct *handle,
2074
2075                                          const char *path)
2076 {
2077         int result;
2078
2079         result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
2080
2081         do_log(SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE, (result >= 0), handle,
2082                "%s", path);
2083
2084         return result;
2085 }
2086
2087 static ssize_t smb_full_audit_getxattr(struct vfs_handle_struct *handle,
2088                               const char *path,
2089                               const char *name, void *value, size_t size)
2090 {
2091         ssize_t result;
2092
2093         result = SMB_VFS_NEXT_GETXATTR(handle, path, name, value, size);
2094
2095         do_log(SMB_VFS_OP_GETXATTR, (result >= 0), handle,
2096                "%s|%s", path, name);
2097
2098         return result;
2099 }
2100
2101 static ssize_t smb_full_audit_fgetxattr(struct vfs_handle_struct *handle,
2102                                struct files_struct *fsp,
2103                                const char *name, void *value, size_t size)
2104 {
2105         ssize_t result;
2106
2107         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
2108
2109         do_log(SMB_VFS_OP_FGETXATTR, (result >= 0), handle,
2110                "%s|%s", fsp_str_do_log(fsp), name);
2111
2112         return result;
2113 }
2114
2115 static ssize_t smb_full_audit_listxattr(struct vfs_handle_struct *handle,
2116                                const char *path, char *list, size_t size)
2117 {
2118         ssize_t result;
2119
2120         result = SMB_VFS_NEXT_LISTXATTR(handle, path, list, size);
2121
2122         do_log(SMB_VFS_OP_LISTXATTR, (result >= 0), handle, "%s", path);
2123
2124         return result;
2125 }
2126
2127 static ssize_t smb_full_audit_flistxattr(struct vfs_handle_struct *handle,
2128                                 struct files_struct *fsp, char *list,
2129                                 size_t size)
2130 {
2131         ssize_t result;
2132
2133         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
2134
2135         do_log(SMB_VFS_OP_FLISTXATTR, (result >= 0), handle,
2136                "%s", fsp_str_do_log(fsp));
2137
2138         return result;
2139 }
2140
2141 static int smb_full_audit_removexattr(struct vfs_handle_struct *handle,
2142                              const char *path,
2143                              const char *name)
2144 {
2145         int result;
2146
2147         result = SMB_VFS_NEXT_REMOVEXATTR(handle, path, name);
2148
2149         do_log(SMB_VFS_OP_REMOVEXATTR, (result >= 0), handle,
2150                "%s|%s", path, name);
2151
2152         return result;
2153 }
2154
2155 static int smb_full_audit_fremovexattr(struct vfs_handle_struct *handle,
2156                               struct files_struct *fsp,
2157                               const char *name)
2158 {
2159         int result;
2160
2161         result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2162
2163         do_log(SMB_VFS_OP_FREMOVEXATTR, (result >= 0), handle,
2164                "%s|%s", fsp_str_do_log(fsp), name);
2165
2166         return result;
2167 }
2168
2169 static int smb_full_audit_setxattr(struct vfs_handle_struct *handle,
2170                           const char *path,
2171                           const char *name, const void *value, size_t size,
2172                           int flags)
2173 {
2174         int result;
2175
2176         result = SMB_VFS_NEXT_SETXATTR(handle, path, name, value, size,
2177                                        flags);
2178
2179         do_log(SMB_VFS_OP_SETXATTR, (result >= 0), handle,
2180                "%s|%s", path, name);
2181
2182         return result;
2183 }
2184
2185 static int smb_full_audit_fsetxattr(struct vfs_handle_struct *handle,
2186                            struct files_struct *fsp, const char *name,
2187                            const void *value, size_t size, int flags)
2188 {
2189         int result;
2190
2191         result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2192
2193         do_log(SMB_VFS_OP_FSETXATTR, (result >= 0), handle,
2194                "%s|%s", fsp_str_do_log(fsp), name);
2195
2196         return result;
2197 }
2198
2199 static bool smb_full_audit_aio_force(struct vfs_handle_struct *handle,
2200                                      struct files_struct *fsp)
2201 {
2202         bool result;
2203
2204         result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2205         do_log(SMB_VFS_OP_AIO_FORCE, result, handle,
2206                 "%s", fsp_str_do_log(fsp));
2207
2208         return result;
2209 }
2210
2211 static bool smb_full_audit_is_offline(struct vfs_handle_struct *handle,
2212                                       const struct smb_filename *fname,
2213                                       SMB_STRUCT_STAT *sbuf)
2214 {
2215         bool result;
2216
2217         result = SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
2218         do_log(SMB_VFS_OP_IS_OFFLINE, result, handle, "%s",
2219                smb_fname_str_do_log(fname));
2220         return result;
2221 }
2222
2223 static int smb_full_audit_set_offline(struct vfs_handle_struct *handle,
2224                                       const struct smb_filename *fname)
2225 {
2226         int result;
2227
2228         result = SMB_VFS_NEXT_SET_OFFLINE(handle, fname);
2229         do_log(SMB_VFS_OP_SET_OFFLINE, result >= 0, handle, "%s",
2230                smb_fname_str_do_log(fname));
2231         return result;
2232 }
2233
2234 static struct vfs_fn_pointers vfs_full_audit_fns = {
2235
2236         /* Disk operations */
2237
2238         .connect_fn = smb_full_audit_connect,
2239         .disconnect_fn = smb_full_audit_disconnect,
2240         .disk_free_fn = smb_full_audit_disk_free,
2241         .get_quota_fn = smb_full_audit_get_quota,
2242         .set_quota_fn = smb_full_audit_set_quota,
2243         .get_shadow_copy_data_fn = smb_full_audit_get_shadow_copy_data,
2244         .statvfs_fn = smb_full_audit_statvfs,
2245         .fs_capabilities_fn = smb_full_audit_fs_capabilities,
2246         .snap_check_path_fn =  smb_full_audit_snap_check_path,
2247         .snap_create_fn = smb_full_audit_snap_create,
2248         .snap_delete_fn = smb_full_audit_snap_delete,
2249         .opendir_fn = smb_full_audit_opendir,
2250         .fdopendir_fn = smb_full_audit_fdopendir,
2251         .readdir_fn = smb_full_audit_readdir,
2252         .seekdir_fn = smb_full_audit_seekdir,
2253         .telldir_fn = smb_full_audit_telldir,
2254         .rewind_dir_fn = smb_full_audit_rewinddir,
2255         .mkdir_fn = smb_full_audit_mkdir,
2256         .rmdir_fn = smb_full_audit_rmdir,
2257         .closedir_fn = smb_full_audit_closedir,
2258         .init_search_op_fn = smb_full_audit_init_search_op,
2259         .open_fn = smb_full_audit_open,
2260         .create_file_fn = smb_full_audit_create_file,
2261         .close_fn = smb_full_audit_close,
2262         .read_fn = smb_full_audit_read,
2263         .pread_fn = smb_full_audit_pread,
2264         .pread_send_fn = smb_full_audit_pread_send,
2265         .pread_recv_fn = smb_full_audit_pread_recv,
2266         .write_fn = smb_full_audit_write,
2267         .pwrite_fn = smb_full_audit_pwrite,
2268         .pwrite_send_fn = smb_full_audit_pwrite_send,
2269         .pwrite_recv_fn = smb_full_audit_pwrite_recv,
2270         .lseek_fn = smb_full_audit_lseek,
2271         .sendfile_fn = smb_full_audit_sendfile,
2272         .recvfile_fn = smb_full_audit_recvfile,
2273         .rename_fn = smb_full_audit_rename,
2274         .fsync_fn = smb_full_audit_fsync,
2275         .fsync_send_fn = smb_full_audit_fsync_send,
2276         .fsync_recv_fn = smb_full_audit_fsync_recv,
2277         .stat_fn = smb_full_audit_stat,
2278         .fstat_fn = smb_full_audit_fstat,
2279         .lstat_fn = smb_full_audit_lstat,
2280         .get_alloc_size_fn = smb_full_audit_get_alloc_size,
2281         .unlink_fn = smb_full_audit_unlink,
2282         .chmod_fn = smb_full_audit_chmod,
2283         .fchmod_fn = smb_full_audit_fchmod,
2284         .chown_fn = smb_full_audit_chown,
2285         .fchown_fn = smb_full_audit_fchown,
2286         .lchown_fn = smb_full_audit_lchown,
2287         .chdir_fn = smb_full_audit_chdir,
2288         .getwd_fn = smb_full_audit_getwd,
2289         .ntimes_fn = smb_full_audit_ntimes,
2290         .ftruncate_fn = smb_full_audit_ftruncate,
2291         .fallocate_fn = smb_full_audit_fallocate,
2292         .lock_fn = smb_full_audit_lock,
2293         .kernel_flock_fn = smb_full_audit_kernel_flock,
2294         .linux_setlease_fn = smb_full_audit_linux_setlease,
2295         .getlock_fn = smb_full_audit_getlock,
2296         .symlink_fn = smb_full_audit_symlink,
2297         .readlink_fn = smb_full_audit_readlink,
2298         .link_fn = smb_full_audit_link,
2299         .mknod_fn = smb_full_audit_mknod,
2300         .realpath_fn = smb_full_audit_realpath,
2301         .chflags_fn = smb_full_audit_chflags,
2302         .file_id_create_fn = smb_full_audit_file_id_create,
2303         .streaminfo_fn = smb_full_audit_streaminfo,
2304         .get_real_filename_fn = smb_full_audit_get_real_filename,
2305         .connectpath_fn = smb_full_audit_connectpath,
2306         .brl_lock_windows_fn = smb_full_audit_brl_lock_windows,
2307         .brl_unlock_windows_fn = smb_full_audit_brl_unlock_windows,
2308         .brl_cancel_windows_fn = smb_full_audit_brl_cancel_windows,
2309         .strict_lock_fn = smb_full_audit_strict_lock,
2310         .strict_unlock_fn = smb_full_audit_strict_unlock,
2311         .translate_name_fn = smb_full_audit_translate_name,
2312         .copy_chunk_send_fn = smb_full_audit_copy_chunk_send,
2313         .copy_chunk_recv_fn = smb_full_audit_copy_chunk_recv,
2314         .get_compression_fn = smb_full_audit_get_compression,
2315         .set_compression_fn = smb_full_audit_set_compression,
2316         .readdir_attr_fn = smb_full_audit_readdir_attr,
2317         .fget_nt_acl_fn = smb_full_audit_fget_nt_acl,
2318         .get_nt_acl_fn = smb_full_audit_get_nt_acl,
2319         .fset_nt_acl_fn = smb_full_audit_fset_nt_acl,
2320         .chmod_acl_fn = smb_full_audit_chmod_acl,
2321         .fchmod_acl_fn = smb_full_audit_fchmod_acl,
2322         .sys_acl_get_file_fn = smb_full_audit_sys_acl_get_file,
2323         .sys_acl_get_fd_fn = smb_full_audit_sys_acl_get_fd,
2324         .sys_acl_blob_get_file_fn = smb_full_audit_sys_acl_blob_get_file,
2325         .sys_acl_blob_get_fd_fn = smb_full_audit_sys_acl_blob_get_fd,
2326         .sys_acl_set_file_fn = smb_full_audit_sys_acl_set_file,
2327         .sys_acl_set_fd_fn = smb_full_audit_sys_acl_set_fd,
2328         .sys_acl_delete_def_file_fn = smb_full_audit_sys_acl_delete_def_file,
2329         .getxattr_fn = smb_full_audit_getxattr,
2330         .fgetxattr_fn = smb_full_audit_fgetxattr,
2331         .listxattr_fn = smb_full_audit_listxattr,
2332         .flistxattr_fn = smb_full_audit_flistxattr,
2333         .removexattr_fn = smb_full_audit_removexattr,
2334         .fremovexattr_fn = smb_full_audit_fremovexattr,
2335         .setxattr_fn = smb_full_audit_setxattr,
2336         .fsetxattr_fn = smb_full_audit_fsetxattr,
2337         .aio_force_fn = smb_full_audit_aio_force,
2338         .is_offline_fn = smb_full_audit_is_offline,
2339         .set_offline_fn = smb_full_audit_set_offline,
2340 };
2341
2342 static_decl_vfs;
2343 NTSTATUS vfs_full_audit_init(void)
2344 {
2345         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2346                                         "full_audit", &vfs_full_audit_fns);
2347         
2348         if (!NT_STATUS_IS_OK(ret))
2349                 return ret;
2350
2351         vfs_full_audit_debug_level = debug_add_class("full_audit");
2352         if (vfs_full_audit_debug_level == -1) {
2353                 vfs_full_audit_debug_level = DBGC_VFS;
2354                 DEBUG(0, ("vfs_full_audit: Couldn't register custom debugging "
2355                           "class!\n"));
2356         } else {
2357                 DEBUG(10, ("vfs_full_audit: Debug class number of "
2358                            "'full_audit': %d\n", vfs_full_audit_debug_level));
2359         }
2360         
2361         return ret;
2362 }