VFS: Rename kernel_flock to filesystem_sharemode
[samba.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 create_file
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|/tmp
42  * smbd_audit: nobody|192.168.234.1|create_file|fail (No such file or directory)|0x1|file|open|/ts/doesNotExist
43  * smbd_audit: nobody|192.168.234.1|open|ok|w|/tmp/file.txt
44  * smbd_audit: nobody|192.168.234.1|create_file|ok|0x3|file|open|/tmp/file.txt
45  *
46  * where "nobody" is the connected username and "192.168.234.1" is the
47  * client's IP address. 
48  *
49  * Options:
50  *
51  * prefix: A macro expansion template prepended to the syslog entry.
52  *
53  * success: A list of VFS operations for which a successful completion should
54  * be logged. Defaults to no logging at all. The special operation "all" logs
55  * - you guessed it - everything.
56  *
57  * failure: A list of VFS operations for which failure to complete should be
58  * logged. Defaults to logging everything.
59  */
60
61
62 #include "includes.h"
63 #include "system/filesys.h"
64 #include "system/syslog.h"
65 #include "smbd/smbd.h"
66 #include "../librpc/gen_ndr/ndr_netlogon.h"
67 #include "auth.h"
68 #include "ntioctl.h"
69 #include "lib/param/loadparm.h"
70 #include "lib/util/bitmap.h"
71 #include "lib/util/tevent_unix.h"
72 #include "libcli/security/sddl.h"
73 #include "passdb/machine_sid.h"
74 #include "lib/util/tevent_ntstatus.h"
75 #include "lib/util/string_wrappers.h"
76
77 static int vfs_full_audit_debug_level = DBGC_VFS;
78
79 struct vfs_full_audit_private_data {
80         struct bitmap *success_ops;
81         struct bitmap *failure_ops;
82         int syslog_facility;
83         int syslog_priority;
84         bool log_secdesc;
85         bool do_syslog;
86 };
87
88 #undef DBGC_CLASS
89 #define DBGC_CLASS vfs_full_audit_debug_level
90
91 typedef enum _vfs_op_type {
92         SMB_VFS_OP_NOOP = -1,
93
94         /* Disk operations */
95
96         SMB_VFS_OP_CONNECT = 0,
97         SMB_VFS_OP_DISCONNECT,
98         SMB_VFS_OP_DISK_FREE,
99         SMB_VFS_OP_GET_QUOTA,
100         SMB_VFS_OP_SET_QUOTA,
101         SMB_VFS_OP_GET_SHADOW_COPY_DATA,
102         SMB_VFS_OP_STATVFS,
103         SMB_VFS_OP_FS_CAPABILITIES,
104         SMB_VFS_OP_GET_DFS_REFERRALS,
105         SMB_VFS_OP_CREATE_DFS_PATHAT,
106         SMB_VFS_OP_READ_DFS_PATHAT,
107
108         /* Directory operations */
109
110         SMB_VFS_OP_FDOPENDIR,
111         SMB_VFS_OP_READDIR,
112         SMB_VFS_OP_SEEKDIR,
113         SMB_VFS_OP_TELLDIR,
114         SMB_VFS_OP_REWINDDIR,
115         SMB_VFS_OP_MKDIRAT,
116         SMB_VFS_OP_CLOSEDIR,
117
118         /* File operations */
119
120         SMB_VFS_OP_OPEN,
121         SMB_VFS_OP_OPENAT,
122         SMB_VFS_OP_CREATE_FILE,
123         SMB_VFS_OP_CLOSE,
124         SMB_VFS_OP_READ,
125         SMB_VFS_OP_PREAD,
126         SMB_VFS_OP_PREAD_SEND,
127         SMB_VFS_OP_PREAD_RECV,
128         SMB_VFS_OP_WRITE,
129         SMB_VFS_OP_PWRITE,
130         SMB_VFS_OP_PWRITE_SEND,
131         SMB_VFS_OP_PWRITE_RECV,
132         SMB_VFS_OP_LSEEK,
133         SMB_VFS_OP_SENDFILE,
134         SMB_VFS_OP_RECVFILE,
135         SMB_VFS_OP_RENAMEAT,
136         SMB_VFS_OP_FSYNC,
137         SMB_VFS_OP_FSYNC_SEND,
138         SMB_VFS_OP_FSYNC_RECV,
139         SMB_VFS_OP_STAT,
140         SMB_VFS_OP_FSTAT,
141         SMB_VFS_OP_LSTAT,
142         SMB_VFS_OP_GET_ALLOC_SIZE,
143         SMB_VFS_OP_UNLINKAT,
144         SMB_VFS_OP_FCHMOD,
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_FNTIMES,
151         SMB_VFS_OP_FTRUNCATE,
152         SMB_VFS_OP_FALLOCATE,
153         SMB_VFS_OP_LOCK,
154         SMB_VFS_OP_KERNEL_FLOCK,
155         SMB_VFS_OP_FCNTL,
156         SMB_VFS_OP_LINUX_SETLEASE,
157         SMB_VFS_OP_GETLOCK,
158         SMB_VFS_OP_SYMLINKAT,
159         SMB_VFS_OP_READLINKAT,
160         SMB_VFS_OP_LINKAT,
161         SMB_VFS_OP_MKNODAT,
162         SMB_VFS_OP_REALPATH,
163         SMB_VFS_OP_FCHFLAGS,
164         SMB_VFS_OP_FILE_ID_CREATE,
165         SMB_VFS_OP_FS_FILE_ID,
166         SMB_VFS_OP_FSTREAMINFO,
167         SMB_VFS_OP_GET_REAL_FILENAME,
168         SMB_VFS_OP_CONNECTPATH,
169         SMB_VFS_OP_BRL_LOCK_WINDOWS,
170         SMB_VFS_OP_BRL_UNLOCK_WINDOWS,
171         SMB_VFS_OP_STRICT_LOCK_CHECK,
172         SMB_VFS_OP_TRANSLATE_NAME,
173         SMB_VFS_OP_PARENT_PATHNAME,
174         SMB_VFS_OP_FSCTL,
175         SMB_VFS_OP_OFFLOAD_READ_SEND,
176         SMB_VFS_OP_OFFLOAD_READ_RECV,
177         SMB_VFS_OP_OFFLOAD_WRITE_SEND,
178         SMB_VFS_OP_OFFLOAD_WRITE_RECV,
179         SMB_VFS_OP_FGET_COMPRESSION,
180         SMB_VFS_OP_SET_COMPRESSION,
181         SMB_VFS_OP_SNAP_CHECK_PATH,
182         SMB_VFS_OP_SNAP_CREATE,
183         SMB_VFS_OP_SNAP_DELETE,
184
185         /* DOS attribute operations. */
186         SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
187         SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
188         SMB_VFS_OP_FGET_DOS_ATTRIBUTES,
189         SMB_VFS_OP_FSET_DOS_ATTRIBUTES,
190
191         /* NT ACL operations. */
192
193         SMB_VFS_OP_FGET_NT_ACL,
194         SMB_VFS_OP_FSET_NT_ACL,
195         SMB_VFS_OP_AUDIT_FILE,
196
197         /* POSIX ACL operations. */
198
199         SMB_VFS_OP_SYS_ACL_GET_FD,
200         SMB_VFS_OP_SYS_ACL_BLOB_GET_FD,
201         SMB_VFS_OP_SYS_ACL_SET_FD,
202         SMB_VFS_OP_SYS_ACL_DELETE_DEF_FD,
203
204         /* EA operations. */
205         SMB_VFS_OP_GETXATTRAT_SEND,
206         SMB_VFS_OP_GETXATTRAT_RECV,
207         SMB_VFS_OP_FGETXATTR,
208         SMB_VFS_OP_FLISTXATTR,
209         SMB_VFS_OP_REMOVEXATTR,
210         SMB_VFS_OP_FREMOVEXATTR,
211         SMB_VFS_OP_FSETXATTR,
212
213         /* aio operations */
214         SMB_VFS_OP_AIO_FORCE,
215
216         /* offline operations */
217         SMB_VFS_OP_IS_OFFLINE,
218         SMB_VFS_OP_SET_OFFLINE,
219
220         /* Durable handle operations. */
221         SMB_VFS_OP_DURABLE_COOKIE,
222         SMB_VFS_OP_DURABLE_DISCONNECT,
223         SMB_VFS_OP_DURABLE_RECONNECT,
224
225         SMB_VFS_OP_FREADDIR_ATTR,
226
227         /* This should always be last enum value */
228
229         SMB_VFS_OP_LAST
230 } vfs_op_type;
231
232 /* The following array *must* be in the same order as defined in vfs_op_type */
233
234 static struct {
235         vfs_op_type type;
236         const char *name;
237 } vfs_op_names[] = {
238         { SMB_VFS_OP_CONNECT,   "connect" },
239         { SMB_VFS_OP_DISCONNECT,        "disconnect" },
240         { SMB_VFS_OP_DISK_FREE, "disk_free" },
241         { SMB_VFS_OP_GET_QUOTA, "get_quota" },
242         { SMB_VFS_OP_SET_QUOTA, "set_quota" },
243         { SMB_VFS_OP_GET_SHADOW_COPY_DATA,      "get_shadow_copy_data" },
244         { SMB_VFS_OP_STATVFS,   "statvfs" },
245         { SMB_VFS_OP_FS_CAPABILITIES,   "fs_capabilities" },
246         { SMB_VFS_OP_GET_DFS_REFERRALS, "get_dfs_referrals" },
247         { SMB_VFS_OP_CREATE_DFS_PATHAT, "create_dfs_pathat" },
248         { SMB_VFS_OP_READ_DFS_PATHAT,   "read_dfs_pathat" },
249         { SMB_VFS_OP_FDOPENDIR, "fdopendir" },
250         { SMB_VFS_OP_READDIR,   "readdir" },
251         { SMB_VFS_OP_SEEKDIR,   "seekdir" },
252         { SMB_VFS_OP_TELLDIR,   "telldir" },
253         { SMB_VFS_OP_REWINDDIR, "rewinddir" },
254         { SMB_VFS_OP_MKDIRAT,   "mkdirat" },
255         { SMB_VFS_OP_CLOSEDIR,  "closedir" },
256         { SMB_VFS_OP_OPEN,      "open" },
257         { SMB_VFS_OP_OPENAT,    "openat" },
258         { SMB_VFS_OP_CREATE_FILE, "create_file" },
259         { SMB_VFS_OP_CLOSE,     "close" },
260         { SMB_VFS_OP_READ,      "read" },
261         { SMB_VFS_OP_PREAD,     "pread" },
262         { SMB_VFS_OP_PREAD_SEND,        "pread_send" },
263         { SMB_VFS_OP_PREAD_RECV,        "pread_recv" },
264         { SMB_VFS_OP_WRITE,     "write" },
265         { SMB_VFS_OP_PWRITE,    "pwrite" },
266         { SMB_VFS_OP_PWRITE_SEND,       "pwrite_send" },
267         { SMB_VFS_OP_PWRITE_RECV,       "pwrite_recv" },
268         { SMB_VFS_OP_LSEEK,     "lseek" },
269         { SMB_VFS_OP_SENDFILE,  "sendfile" },
270         { SMB_VFS_OP_RECVFILE,  "recvfile" },
271         { SMB_VFS_OP_RENAMEAT,  "renameat" },
272         { SMB_VFS_OP_FSYNC,     "fsync" },
273         { SMB_VFS_OP_FSYNC_SEND,        "fsync_send" },
274         { SMB_VFS_OP_FSYNC_RECV,        "fsync_recv" },
275         { SMB_VFS_OP_STAT,      "stat" },
276         { SMB_VFS_OP_FSTAT,     "fstat" },
277         { SMB_VFS_OP_LSTAT,     "lstat" },
278         { SMB_VFS_OP_GET_ALLOC_SIZE,    "get_alloc_size" },
279         { SMB_VFS_OP_UNLINKAT,  "unlinkat" },
280         { SMB_VFS_OP_FCHMOD,    "fchmod" },
281         { SMB_VFS_OP_FCHOWN,    "fchown" },
282         { SMB_VFS_OP_LCHOWN,    "lchown" },
283         { SMB_VFS_OP_CHDIR,     "chdir" },
284         { SMB_VFS_OP_GETWD,     "getwd" },
285         { SMB_VFS_OP_NTIMES,    "ntimes" },
286         { SMB_VFS_OP_FNTIMES,   "fntimes" },
287         { SMB_VFS_OP_FTRUNCATE, "ftruncate" },
288         { SMB_VFS_OP_FALLOCATE,"fallocate" },
289         { SMB_VFS_OP_LOCK,      "lock" },
290         { SMB_VFS_OP_KERNEL_FLOCK,      "kernel_flock" },
291         { SMB_VFS_OP_FCNTL,     "fcntl" },
292         { SMB_VFS_OP_LINUX_SETLEASE, "linux_setlease" },
293         { SMB_VFS_OP_GETLOCK,   "getlock" },
294         { SMB_VFS_OP_SYMLINKAT, "symlinkat" },
295         { SMB_VFS_OP_READLINKAT,"readlinkat" },
296         { SMB_VFS_OP_LINKAT,    "linkat" },
297         { SMB_VFS_OP_MKNODAT,   "mknodat" },
298         { SMB_VFS_OP_REALPATH,  "realpath" },
299         { SMB_VFS_OP_FCHFLAGS,  "fchflags" },
300         { SMB_VFS_OP_FILE_ID_CREATE,    "file_id_create" },
301         { SMB_VFS_OP_FS_FILE_ID,        "fs_file_id" },
302         { SMB_VFS_OP_FSTREAMINFO,       "fstreaminfo" },
303         { SMB_VFS_OP_GET_REAL_FILENAME, "get_real_filename" },
304         { SMB_VFS_OP_CONNECTPATH,       "connectpath" },
305         { SMB_VFS_OP_BRL_LOCK_WINDOWS,  "brl_lock_windows" },
306         { SMB_VFS_OP_BRL_UNLOCK_WINDOWS, "brl_unlock_windows" },
307         { SMB_VFS_OP_STRICT_LOCK_CHECK, "strict_lock_check" },
308         { SMB_VFS_OP_TRANSLATE_NAME,    "translate_name" },
309         { SMB_VFS_OP_PARENT_PATHNAME,   "parent_pathname" },
310         { SMB_VFS_OP_FSCTL,             "fsctl" },
311         { SMB_VFS_OP_OFFLOAD_READ_SEND, "offload_read_send" },
312         { SMB_VFS_OP_OFFLOAD_READ_RECV, "offload_read_recv" },
313         { SMB_VFS_OP_OFFLOAD_WRITE_SEND,        "offload_write_send" },
314         { SMB_VFS_OP_OFFLOAD_WRITE_RECV,        "offload_write_recv" },
315         { SMB_VFS_OP_FGET_COMPRESSION,  "fget_compression" },
316         { SMB_VFS_OP_SET_COMPRESSION,   "set_compression" },
317         { SMB_VFS_OP_SNAP_CHECK_PATH, "snap_check_path" },
318         { SMB_VFS_OP_SNAP_CREATE, "snap_create" },
319         { SMB_VFS_OP_SNAP_DELETE, "snap_delete" },
320         { SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND, "get_dos_attributes_send" },
321         { SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV, "get_dos_attributes_recv" },
322         { SMB_VFS_OP_FGET_DOS_ATTRIBUTES, "fget_dos_attributes" },
323         { SMB_VFS_OP_FSET_DOS_ATTRIBUTES, "fset_dos_attributes" },
324         { SMB_VFS_OP_FGET_NT_ACL,       "fget_nt_acl" },
325         { SMB_VFS_OP_FSET_NT_ACL,       "fset_nt_acl" },
326         { SMB_VFS_OP_AUDIT_FILE,        "audit_file" },
327         { SMB_VFS_OP_SYS_ACL_GET_FD,    "sys_acl_get_fd" },
328         { SMB_VFS_OP_SYS_ACL_BLOB_GET_FD,       "sys_acl_blob_get_fd" },
329         { SMB_VFS_OP_SYS_ACL_SET_FD,    "sys_acl_set_fd" },
330         { SMB_VFS_OP_SYS_ACL_DELETE_DEF_FD,     "sys_acl_delete_def_fd" },
331         { SMB_VFS_OP_GETXATTRAT_SEND, "getxattrat_send" },
332         { SMB_VFS_OP_GETXATTRAT_RECV, "getxattrat_recv" },
333         { SMB_VFS_OP_FGETXATTR, "fgetxattr" },
334         { SMB_VFS_OP_FLISTXATTR,        "flistxattr" },
335         { SMB_VFS_OP_REMOVEXATTR,       "removexattr" },
336         { SMB_VFS_OP_FREMOVEXATTR,      "fremovexattr" },
337         { SMB_VFS_OP_FSETXATTR, "fsetxattr" },
338         { SMB_VFS_OP_AIO_FORCE, "aio_force" },
339         { SMB_VFS_OP_IS_OFFLINE, "is_offline" },
340         { SMB_VFS_OP_SET_OFFLINE, "set_offline" },
341         { SMB_VFS_OP_DURABLE_COOKIE, "durable_cookie" },
342         { SMB_VFS_OP_DURABLE_DISCONNECT, "durable_disconnect" },
343         { SMB_VFS_OP_DURABLE_RECONNECT, "durable_reconnect" },
344         { SMB_VFS_OP_FREADDIR_ATTR,      "freaddir_attr" },
345         { SMB_VFS_OP_LAST, NULL }
346 };
347
348 static int audit_syslog_facility(vfs_handle_struct *handle)
349 {
350         static const struct enum_list enum_log_facilities[] = {
351 #ifdef LOG_AUTH
352                 { LOG_AUTH,             "AUTH" },
353 #endif
354 #ifdef LOG_AUTHPRIV
355                 { LOG_AUTHPRIV,         "AUTHPRIV" },
356 #endif
357 #ifdef LOG_AUDIT
358                 { LOG_AUDIT,            "AUDIT" },
359 #endif
360 #ifdef LOG_CONSOLE
361                 { LOG_CONSOLE,          "CONSOLE" },
362 #endif
363 #ifdef LOG_CRON
364                 { LOG_CRON,             "CRON" },
365 #endif
366 #ifdef LOG_DAEMON
367                 { LOG_DAEMON,           "DAEMON" },
368 #endif
369 #ifdef LOG_FTP
370                 { LOG_FTP,              "FTP" },
371 #endif
372 #ifdef LOG_INSTALL
373                 { LOG_INSTALL,          "INSTALL" },
374 #endif
375 #ifdef LOG_KERN
376                 { LOG_KERN,             "KERN" },
377 #endif
378 #ifdef LOG_LAUNCHD
379                 { LOG_LAUNCHD,          "LAUNCHD" },
380 #endif
381 #ifdef LOG_LFMT
382                 { LOG_LFMT,             "LFMT" },
383 #endif
384 #ifdef LOG_LPR
385                 { LOG_LPR,              "LPR" },
386 #endif
387 #ifdef LOG_MAIL
388                 { LOG_MAIL,             "MAIL" },
389 #endif
390 #ifdef LOG_MEGASAFE
391                 { LOG_MEGASAFE,         "MEGASAFE" },
392 #endif
393 #ifdef LOG_NETINFO
394                 { LOG_NETINFO,          "NETINFO" },
395 #endif
396 #ifdef LOG_NEWS
397                 { LOG_NEWS,             "NEWS" },
398 #endif
399 #ifdef LOG_NFACILITIES
400                 { LOG_NFACILITIES,      "NFACILITIES" },
401 #endif
402 #ifdef LOG_NTP
403                 { LOG_NTP,              "NTP" },
404 #endif
405 #ifdef LOG_RAS
406                 { LOG_RAS,              "RAS" },
407 #endif
408 #ifdef LOG_REMOTEAUTH
409                 { LOG_REMOTEAUTH,       "REMOTEAUTH" },
410 #endif
411 #ifdef LOG_SECURITY
412                 { LOG_SECURITY,         "SECURITY" },
413 #endif
414 #ifdef LOG_SYSLOG
415                 { LOG_SYSLOG,           "SYSLOG" },
416 #endif
417 #ifdef LOG_USER
418                 { LOG_USER,             "USER" },
419 #endif
420 #ifdef LOG_UUCP
421                 { LOG_UUCP,             "UUCP" },
422 #endif
423                 { LOG_LOCAL0,           "LOCAL0" },
424                 { LOG_LOCAL1,           "LOCAL1" },
425                 { LOG_LOCAL2,           "LOCAL2" },
426                 { LOG_LOCAL3,           "LOCAL3" },
427                 { LOG_LOCAL4,           "LOCAL4" },
428                 { LOG_LOCAL5,           "LOCAL5" },
429                 { LOG_LOCAL6,           "LOCAL6" },
430                 { LOG_LOCAL7,           "LOCAL7" },
431                 { -1,                   NULL }
432         };
433
434         int facility;
435
436         facility = lp_parm_enum(SNUM(handle->conn), "full_audit", "facility", enum_log_facilities, LOG_USER);
437
438         return facility;
439 }
440
441 static int audit_syslog_priority(vfs_handle_struct *handle)
442 {
443         static const struct enum_list enum_log_priorities[] = {
444                 { LOG_EMERG, "EMERG" },
445                 { LOG_ALERT, "ALERT" },
446                 { LOG_CRIT, "CRIT" },
447                 { LOG_ERR, "ERR" },
448                 { LOG_WARNING, "WARNING" },
449                 { LOG_NOTICE, "NOTICE" },
450                 { LOG_INFO, "INFO" },
451                 { LOG_DEBUG, "DEBUG" },
452                 { -1, NULL }
453         };
454
455         int priority;
456
457         priority = lp_parm_enum(SNUM(handle->conn), "full_audit", "priority",
458                                 enum_log_priorities, LOG_NOTICE);
459         if (priority == -1) {
460                 priority = LOG_WARNING;
461         }
462
463         return priority;
464 }
465
466 static char *audit_prefix(TALLOC_CTX *ctx, connection_struct *conn)
467 {
468         const struct loadparm_substitution *lp_sub =
469                 loadparm_s3_global_substitution();
470         char *prefix = NULL;
471         char *result;
472
473         prefix = talloc_strdup(ctx,
474                         lp_parm_const_string(SNUM(conn), "full_audit",
475                                              "prefix", "%u|%I"));
476         if (!prefix) {
477                 return NULL;
478         }
479         result = talloc_sub_full(ctx,
480                         lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
481                         conn->session_info->unix_info->unix_name,
482                         conn->connectpath,
483                         conn->session_info->unix_token->gid,
484                         conn->session_info->unix_info->sanitized_username,
485                         conn->session_info->info->domain_name,
486                         prefix);
487         TALLOC_FREE(prefix);
488         return result;
489 }
490
491 static bool log_success(struct vfs_full_audit_private_data *pd, vfs_op_type op)
492 {
493         if (pd->success_ops == NULL) {
494                 return True;
495         }
496
497         return bitmap_query(pd->success_ops, op);
498 }
499
500 static bool log_failure(struct vfs_full_audit_private_data *pd, vfs_op_type op)
501 {
502         if (pd->failure_ops == NULL)
503                 return True;
504
505         return bitmap_query(pd->failure_ops, op);
506 }
507
508 static struct bitmap *init_bitmap(TALLOC_CTX *mem_ctx, const char **ops)
509 {
510         struct bitmap *bm;
511
512         if (ops == NULL) {
513                 return NULL;
514         }
515
516         bm = bitmap_talloc(mem_ctx, SMB_VFS_OP_LAST);
517         if (bm == NULL) {
518                 DEBUG(0, ("Could not alloc bitmap -- "
519                           "defaulting to logging everything\n"));
520                 return NULL;
521         }
522
523         for (; *ops != NULL; ops += 1) {
524                 int i;
525                 bool neg = false;
526                 const char *op;
527
528                 if (strequal(*ops, "all")) {
529                         for (i=0; i<SMB_VFS_OP_LAST; i++) {
530                                 bitmap_set(bm, i);
531                         }
532                         continue;
533                 }
534
535                 if (strequal(*ops, "none")) {
536                         break;
537                 }
538
539                 op = ops[0];
540                 if (op[0] == '!') {
541                         neg = true;
542                         op += 1;
543                 }
544
545                 for (i=0; i<SMB_VFS_OP_LAST; i++) {
546                         if ((vfs_op_names[i].name == NULL)
547                          || (vfs_op_names[i].type != i)) {
548                                 smb_panic("vfs_full_audit.c: name table not "
549                                           "in sync with vfs_op_type enums\n");
550                         }
551                         if (strequal(op, vfs_op_names[i].name)) {
552                                 if (neg) {
553                                         bitmap_clear(bm, i);
554                                 } else {
555                                         bitmap_set(bm, i);
556                                 }
557                                 break;
558                         }
559                 }
560                 if (i == SMB_VFS_OP_LAST) {
561                         DEBUG(0, ("Could not find opname %s, logging all\n",
562                                   *ops));
563                         TALLOC_FREE(bm);
564                         return NULL;
565                 }
566         }
567         return bm;
568 }
569
570 static const char *audit_opname(vfs_op_type op)
571 {
572         if (op >= SMB_VFS_OP_LAST)
573                 return "INVALID VFS OP";
574         return vfs_op_names[op].name;
575 }
576
577 static TALLOC_CTX *tmp_do_log_ctx;
578 /*
579  * Get us a temporary talloc context usable just for DEBUG arguments
580  */
581 static TALLOC_CTX *do_log_ctx(void)
582 {
583         if (tmp_do_log_ctx == NULL) {
584                 tmp_do_log_ctx = talloc_named_const(NULL, 0, "do_log_ctx");
585         }
586         return tmp_do_log_ctx;
587 }
588
589 static void do_log(vfs_op_type op, bool success, vfs_handle_struct *handle,
590                    const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
591
592 static void do_log(vfs_op_type op, bool success, vfs_handle_struct *handle,
593                    const char *format, ...)
594 {
595         struct vfs_full_audit_private_data *pd;
596         fstring err_msg;
597         char *audit_pre = NULL;
598         va_list ap;
599         char *op_msg = NULL;
600
601         SMB_VFS_HANDLE_GET_DATA(handle, pd,
602                                 struct vfs_full_audit_private_data,
603                                 return;);
604
605         if (success && (!log_success(pd, op)))
606                 goto out;
607
608         if (!success && (!log_failure(pd, op)))
609                 goto out;
610
611         if (success)
612                 fstrcpy(err_msg, "ok");
613         else
614                 fstr_sprintf(err_msg, "fail (%s)", strerror(errno));
615
616         va_start(ap, format);
617         op_msg = talloc_vasprintf(talloc_tos(), format, ap);
618         va_end(ap);
619
620         if (!op_msg) {
621                 goto out;
622         }
623
624         audit_pre = audit_prefix(talloc_tos(), handle->conn);
625
626         if (pd->do_syslog) {
627                 int priority;
628
629                 /*
630                  * Specify the facility to interoperate with other syslog
631                  * callers (smbd for example).
632                  */
633                 priority = pd->syslog_priority | pd->syslog_facility;
634
635                 syslog(priority, "%s|%s|%s|%s\n",
636                        audit_pre ? audit_pre : "",
637                        audit_opname(op), err_msg, op_msg);
638         } else {
639                 DEBUG(1, ("%s|%s|%s|%s\n",
640                           audit_pre ? audit_pre : "",
641                           audit_opname(op), err_msg, op_msg));
642         }
643  out:
644         TALLOC_FREE(audit_pre);
645         TALLOC_FREE(op_msg);
646         TALLOC_FREE(tmp_do_log_ctx);
647 }
648
649 /**
650  * Return a string using the do_log_ctx()
651  */
652 static const char *smb_fname_str_do_log(struct connection_struct *conn,
653                                 const struct smb_filename *smb_fname)
654 {
655         char *fname = NULL;
656         NTSTATUS status;
657
658         if (smb_fname == NULL) {
659                 return "";
660         }
661
662         if (smb_fname->base_name[0] != '/') {
663                 char *abs_name = NULL;
664                 struct smb_filename *fname_copy = cp_smb_filename(
665                                                         do_log_ctx(),
666                                                         smb_fname);
667                 if (fname_copy == NULL) {
668                         return "";
669                 }
670
671                 if (!ISDOT(smb_fname->base_name)) {
672                         abs_name = talloc_asprintf(do_log_ctx(),
673                                         "%s/%s",
674                                         conn->cwd_fsp->fsp_name->base_name,
675                                         smb_fname->base_name);
676                 } else {
677                         abs_name = talloc_strdup(do_log_ctx(),
678                                         conn->cwd_fsp->fsp_name->base_name);
679                 }
680                 if (abs_name == NULL) {
681                         return "";
682                 }
683                 fname_copy->base_name = abs_name;
684                 smb_fname = fname_copy;
685         }
686
687         status = get_full_smb_filename(do_log_ctx(), smb_fname, &fname);
688         if (!NT_STATUS_IS_OK(status)) {
689                 return "";
690         }
691         return fname;
692 }
693
694 /**
695  * Return an fsp debug string using the do_log_ctx()
696  */
697 static const char *fsp_str_do_log(const struct files_struct *fsp)
698 {
699         return smb_fname_str_do_log(fsp->conn, fsp->fsp_name);
700 }
701
702 /* Implementation of vfs_ops.  Pass everything on to the default
703    operation but log event first. */
704
705 static int smb_full_audit_connect(vfs_handle_struct *handle,
706                          const char *svc, const char *user)
707 {
708         int result;
709         const char *none[] = { "none" };
710         struct vfs_full_audit_private_data *pd = NULL;
711
712         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
713         if (result < 0) {
714                 return result;
715         }
716
717         pd = talloc_zero(handle, struct vfs_full_audit_private_data);
718         if (!pd) {
719                 SMB_VFS_NEXT_DISCONNECT(handle);
720                 return -1;
721         }
722
723         pd->syslog_facility = audit_syslog_facility(handle);
724         if (pd->syslog_facility == -1) {
725                 DEBUG(1, ("%s: Unknown facility %s\n", __func__,
726                           lp_parm_const_string(SNUM(handle->conn),
727                                                "full_audit", "facility",
728                                                "USER")));
729                 SMB_VFS_NEXT_DISCONNECT(handle);
730                 return -1;
731         }
732
733         pd->syslog_priority = audit_syslog_priority(handle);
734
735         pd->log_secdesc = lp_parm_bool(SNUM(handle->conn),
736                                        "full_audit", "log_secdesc", false);
737
738         pd->do_syslog = lp_parm_bool(SNUM(handle->conn),
739                                      "full_audit", "syslog", true);
740
741 #ifdef WITH_SYSLOG
742         if (pd->do_syslog) {
743                 openlog("smbd_audit", 0, pd->syslog_facility);
744         }
745 #endif
746
747         pd->success_ops = init_bitmap(
748                 pd, lp_parm_string_list(SNUM(handle->conn), "full_audit",
749                                         "success", none));
750         pd->failure_ops = init_bitmap(
751                 pd, lp_parm_string_list(SNUM(handle->conn), "full_audit",
752                                         "failure", none));
753
754         /* Store the private data. */
755         SMB_VFS_HANDLE_SET_DATA(handle, pd, NULL,
756                                 struct vfs_full_audit_private_data, return -1);
757
758         do_log(SMB_VFS_OP_CONNECT, True, handle,
759                "%s", svc);
760
761         return 0;
762 }
763
764 static void smb_full_audit_disconnect(vfs_handle_struct *handle)
765 {
766         const struct loadparm_substitution *lp_sub =
767                 loadparm_s3_global_substitution();
768
769         SMB_VFS_NEXT_DISCONNECT(handle);
770
771         do_log(SMB_VFS_OP_DISCONNECT, True, handle,
772                "%s", lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn)));
773
774         /* The bitmaps will be disconnected when the private
775            data is deleted. */
776 }
777
778 static uint64_t smb_full_audit_disk_free(vfs_handle_struct *handle,
779                                 const struct smb_filename *smb_fname,
780                                 uint64_t *bsize,
781                                 uint64_t *dfree,
782                                 uint64_t *dsize)
783 {
784         uint64_t result;
785
786         result = SMB_VFS_NEXT_DISK_FREE(handle, smb_fname, bsize, dfree, dsize);
787
788         /* Don't have a reasonable notion of failure here */
789
790         do_log(SMB_VFS_OP_DISK_FREE,
791                True,
792                handle,
793                "%s",
794                smb_fname_str_do_log(handle->conn, smb_fname));
795
796         return result;
797 }
798
799 static int smb_full_audit_get_quota(struct vfs_handle_struct *handle,
800                                 const struct smb_filename *smb_fname,
801                                 enum SMB_QUOTA_TYPE qtype,
802                                 unid_t id,
803                                 SMB_DISK_QUOTA *qt)
804 {
805         int result;
806
807         result = SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, qt);
808
809         do_log(SMB_VFS_OP_GET_QUOTA,
810                (result >= 0),
811                handle,
812                "%s",
813                smb_fname_str_do_log(handle->conn, smb_fname));
814
815         return result;
816 }
817
818 static int smb_full_audit_set_quota(struct vfs_handle_struct *handle,
819                            enum SMB_QUOTA_TYPE qtype, unid_t id,
820                            SMB_DISK_QUOTA *qt)
821 {
822         int result;
823
824         result = SMB_VFS_NEXT_SET_QUOTA(handle, qtype, id, qt);
825
826         do_log(SMB_VFS_OP_SET_QUOTA, (result >= 0), handle, "");
827
828         return result;
829 }
830
831 static int smb_full_audit_get_shadow_copy_data(struct vfs_handle_struct *handle,
832                                 struct files_struct *fsp,
833                                 struct shadow_copy_data *shadow_copy_data,
834                                 bool labels)
835 {
836         int result;
837
838         result = SMB_VFS_NEXT_GET_SHADOW_COPY_DATA(handle, fsp, shadow_copy_data, labels);
839
840         do_log(SMB_VFS_OP_GET_SHADOW_COPY_DATA, (result >= 0), handle, "");
841
842         return result;
843 }
844
845 static int smb_full_audit_statvfs(struct vfs_handle_struct *handle,
846                                 const struct smb_filename *smb_fname,
847                                 struct vfs_statvfs_struct *statbuf)
848 {
849         int result;
850
851         result = SMB_VFS_NEXT_STATVFS(handle, smb_fname, statbuf);
852
853         do_log(SMB_VFS_OP_STATVFS, (result >= 0), handle, "");
854
855         return result;
856 }
857
858 static uint32_t smb_full_audit_fs_capabilities(struct vfs_handle_struct *handle, enum timestamp_set_resolution *p_ts_res)
859 {
860         int result;
861
862         result = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
863
864         do_log(SMB_VFS_OP_FS_CAPABILITIES, true, handle, "");
865
866         return result;
867 }
868
869 static NTSTATUS smb_full_audit_get_dfs_referrals(
870                                 struct vfs_handle_struct *handle,
871                                 struct dfs_GetDFSReferral *r)
872 {
873         NTSTATUS status;
874
875         status = SMB_VFS_NEXT_GET_DFS_REFERRALS(handle, r);
876
877         do_log(SMB_VFS_OP_GET_DFS_REFERRALS, NT_STATUS_IS_OK(status),
878                handle, "");
879
880         return status;
881 }
882
883 static NTSTATUS smb_full_audit_create_dfs_pathat(struct vfs_handle_struct *handle,
884                                 struct files_struct *dirfsp,
885                                 const struct smb_filename *smb_fname,
886                                 const struct referral *reflist,
887                                 size_t referral_count)
888 {
889         NTSTATUS status;
890         struct smb_filename *full_fname = NULL;
891
892         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
893                                                   dirfsp,
894                                                   smb_fname);
895         if (full_fname == NULL) {
896                 return NT_STATUS_NO_MEMORY;
897         }
898
899         status = SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
900                         dirfsp,
901                         smb_fname,
902                         reflist,
903                         referral_count);
904
905         do_log(SMB_VFS_OP_CREATE_DFS_PATHAT,
906                 NT_STATUS_IS_OK(status),
907                 handle,
908                 "%s",
909                 smb_fname_str_do_log(handle->conn, full_fname));
910
911         TALLOC_FREE(full_fname);
912         return status;
913 }
914
915 static NTSTATUS smb_full_audit_read_dfs_pathat(struct vfs_handle_struct *handle,
916                         TALLOC_CTX *mem_ctx,
917                         struct files_struct *dirfsp,
918                         struct smb_filename *smb_fname,
919                         struct referral **ppreflist,
920                         size_t *preferral_count)
921 {
922         struct smb_filename *full_fname = NULL;
923         NTSTATUS status;
924
925         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
926                                                   dirfsp,
927                                                   smb_fname);
928         if (full_fname == NULL) {
929                 return NT_STATUS_NO_MEMORY;
930         }
931
932         status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
933                         mem_ctx,
934                         dirfsp,
935                         smb_fname,
936                         ppreflist,
937                         preferral_count);
938
939         do_log(SMB_VFS_OP_READ_DFS_PATHAT,
940                 NT_STATUS_IS_OK(status),
941                 handle,
942                 "%s",
943                 smb_fname_str_do_log(handle->conn, full_fname));
944
945         TALLOC_FREE(full_fname);
946         return status;
947 }
948
949 static NTSTATUS smb_full_audit_snap_check_path(struct vfs_handle_struct *handle,
950                                                TALLOC_CTX *mem_ctx,
951                                                const char *service_path,
952                                                char **base_volume)
953 {
954         NTSTATUS status;
955
956         status = SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx, service_path,
957                                               base_volume);
958         do_log(SMB_VFS_OP_SNAP_CHECK_PATH, NT_STATUS_IS_OK(status),
959                handle, "");
960
961         return status;
962 }
963
964 static NTSTATUS smb_full_audit_snap_create(struct vfs_handle_struct *handle,
965                                            TALLOC_CTX *mem_ctx,
966                                            const char *base_volume,
967                                            time_t *tstamp,
968                                            bool rw,
969                                            char **base_path,
970                                            char **snap_path)
971 {
972         NTSTATUS status;
973
974         status = SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume, tstamp,
975                                           rw, base_path, snap_path);
976         do_log(SMB_VFS_OP_SNAP_CREATE, NT_STATUS_IS_OK(status), handle, "");
977
978         return status;
979 }
980
981 static NTSTATUS smb_full_audit_snap_delete(struct vfs_handle_struct *handle,
982                                            TALLOC_CTX *mem_ctx,
983                                            char *base_path,
984                                            char *snap_path)
985 {
986         NTSTATUS status;
987
988         status = SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx, base_path,
989                                           snap_path);
990         do_log(SMB_VFS_OP_SNAP_DELETE, NT_STATUS_IS_OK(status), handle, "");
991
992         return status;
993 }
994
995 static DIR *smb_full_audit_fdopendir(vfs_handle_struct *handle,
996                           files_struct *fsp, const char *mask, uint32_t attr)
997 {
998         DIR *result;
999
1000         result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
1001
1002         do_log(SMB_VFS_OP_FDOPENDIR, (result != NULL), handle, "%s",
1003                         fsp_str_do_log(fsp));
1004
1005         return result;
1006 }
1007
1008 static struct dirent *smb_full_audit_readdir(vfs_handle_struct *handle,
1009                                              struct files_struct *dirfsp,
1010                                              DIR *dirp,
1011                                              SMB_STRUCT_STAT *sbuf)
1012 {
1013         struct dirent *result;
1014
1015         result = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirp, sbuf);
1016
1017         /* This operation has no reasonable error condition
1018          * (End of dir is also failure), so always succeed.
1019          */
1020         do_log(SMB_VFS_OP_READDIR, True, handle, "");
1021
1022         return result;
1023 }
1024
1025 static void smb_full_audit_seekdir(vfs_handle_struct *handle,
1026                         DIR *dirp, long offset)
1027 {
1028         SMB_VFS_NEXT_SEEKDIR(handle, dirp, offset);
1029
1030         do_log(SMB_VFS_OP_SEEKDIR, True, handle, "");
1031 }
1032
1033 static long smb_full_audit_telldir(vfs_handle_struct *handle,
1034                         DIR *dirp)
1035 {
1036         long result;
1037
1038         result = SMB_VFS_NEXT_TELLDIR(handle, dirp);
1039
1040         do_log(SMB_VFS_OP_TELLDIR, True, handle, "");
1041
1042         return result;
1043 }
1044
1045 static void smb_full_audit_rewinddir(vfs_handle_struct *handle,
1046                         DIR *dirp)
1047 {
1048         SMB_VFS_NEXT_REWINDDIR(handle, dirp);
1049
1050         do_log(SMB_VFS_OP_REWINDDIR, True, handle, "");
1051 }
1052
1053 static int smb_full_audit_mkdirat(vfs_handle_struct *handle,
1054                         struct files_struct *dirfsp,
1055                         const struct smb_filename *smb_fname,
1056                         mode_t mode)
1057 {
1058         struct smb_filename *full_fname = NULL;
1059         int result;
1060
1061         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1062                                                   dirfsp,
1063                                                   smb_fname);
1064         if (full_fname == NULL) {
1065                 errno = ENOMEM;
1066                 return -1;
1067         }
1068
1069         result = SMB_VFS_NEXT_MKDIRAT(handle,
1070                         dirfsp,
1071                         smb_fname,
1072                         mode);
1073
1074         do_log(SMB_VFS_OP_MKDIRAT,
1075                (result >= 0),
1076                handle,
1077                "%s",
1078                smb_fname_str_do_log(handle->conn, full_fname));
1079
1080         TALLOC_FREE(full_fname);
1081
1082         return result;
1083 }
1084
1085 static int smb_full_audit_closedir(vfs_handle_struct *handle,
1086                           DIR *dirp)
1087 {
1088         int result;
1089
1090         result = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
1091         
1092         do_log(SMB_VFS_OP_CLOSEDIR, (result >= 0), handle, "");
1093
1094         return result;
1095 }
1096
1097 static int smb_full_audit_openat(vfs_handle_struct *handle,
1098                                  const struct files_struct *dirfsp,
1099                                  const struct smb_filename *smb_fname,
1100                                  struct files_struct *fsp,
1101                                  int flags,
1102                                  mode_t mode)
1103 {
1104         int result;
1105
1106         result = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
1107
1108         do_log(SMB_VFS_OP_OPENAT, (result >= 0), handle, "%s|%s",
1109                ((flags & O_WRONLY) || (flags & O_RDWR))?"w":"r",
1110                fsp_str_do_log(fsp));
1111
1112         return result;
1113 }
1114
1115 static NTSTATUS smb_full_audit_create_file(vfs_handle_struct *handle,
1116                                       struct smb_request *req,
1117                                       struct smb_filename *smb_fname,
1118                                       uint32_t access_mask,
1119                                       uint32_t share_access,
1120                                       uint32_t create_disposition,
1121                                       uint32_t create_options,
1122                                       uint32_t file_attributes,
1123                                       uint32_t oplock_request,
1124                                       const struct smb2_lease *lease,
1125                                       uint64_t allocation_size,
1126                                       uint32_t private_flags,
1127                                       struct security_descriptor *sd,
1128                                       struct ea_list *ea_list,
1129                                       files_struct **result_fsp,
1130                                       int *pinfo,
1131                                       const struct smb2_create_blobs *in_context_blobs,
1132                                       struct smb2_create_blobs *out_context_blobs)
1133 {
1134         NTSTATUS result;
1135         const char* str_create_disposition;
1136
1137         switch (create_disposition) {
1138         case FILE_SUPERSEDE:
1139                 str_create_disposition = "supersede";
1140                 break;
1141         case FILE_OVERWRITE_IF:
1142                 str_create_disposition = "overwrite_if";
1143                 break;
1144         case FILE_OPEN:
1145                 str_create_disposition = "open";
1146                 break;
1147         case FILE_OVERWRITE:
1148                 str_create_disposition = "overwrite";
1149                 break;
1150         case FILE_CREATE:
1151                 str_create_disposition = "create";
1152                 break;
1153         case FILE_OPEN_IF:
1154                 str_create_disposition = "open_if";
1155                 break;
1156         default:
1157                 str_create_disposition = "unknown";
1158         }
1159
1160         result = SMB_VFS_NEXT_CREATE_FILE(
1161                 handle,                                 /* handle */
1162                 req,                                    /* req */
1163                 smb_fname,                              /* fname */
1164                 access_mask,                            /* access_mask */
1165                 share_access,                           /* share_access */
1166                 create_disposition,                     /* create_disposition*/
1167                 create_options,                         /* create_options */
1168                 file_attributes,                        /* file_attributes */
1169                 oplock_request,                         /* oplock_request */
1170                 lease,                                  /* lease */
1171                 allocation_size,                        /* allocation_size */
1172                 private_flags,
1173                 sd,                                     /* sd */
1174                 ea_list,                                /* ea_list */
1175                 result_fsp,                             /* result */
1176                 pinfo,                                  /* pinfo */
1177                 in_context_blobs, out_context_blobs);   /* create context */
1178
1179         do_log(SMB_VFS_OP_CREATE_FILE, (NT_STATUS_IS_OK(result)), handle,
1180                "0x%x|%s|%s|%s", access_mask,
1181                create_options & FILE_DIRECTORY_FILE ? "dir" : "file",
1182                str_create_disposition,
1183                 smb_fname_str_do_log(handle->conn, smb_fname));
1184
1185         return result;
1186 }
1187
1188 static int smb_full_audit_close(vfs_handle_struct *handle, files_struct *fsp)
1189 {
1190         int result;
1191         
1192         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
1193
1194         do_log(SMB_VFS_OP_CLOSE, (result >= 0), handle, "%s",
1195                fsp_str_do_log(fsp));
1196
1197         return result;
1198 }
1199
1200 static ssize_t smb_full_audit_pread(vfs_handle_struct *handle, files_struct *fsp,
1201                            void *data, size_t n, off_t offset)
1202 {
1203         ssize_t result;
1204
1205         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1206
1207         do_log(SMB_VFS_OP_PREAD, (result >= 0), handle, "%s",
1208                fsp_str_do_log(fsp));
1209
1210         return result;
1211 }
1212
1213 struct smb_full_audit_pread_state {
1214         vfs_handle_struct *handle;
1215         files_struct *fsp;
1216         ssize_t ret;
1217         struct vfs_aio_state vfs_aio_state;
1218 };
1219
1220 static void smb_full_audit_pread_done(struct tevent_req *subreq);
1221
1222 static struct tevent_req *smb_full_audit_pread_send(
1223         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1224         struct tevent_context *ev, struct files_struct *fsp,
1225         void *data, size_t n, off_t offset)
1226 {
1227         struct tevent_req *req, *subreq;
1228         struct smb_full_audit_pread_state *state;
1229
1230         req = tevent_req_create(mem_ctx, &state,
1231                                 struct smb_full_audit_pread_state);
1232         if (req == NULL) {
1233                 do_log(SMB_VFS_OP_PREAD_SEND, false, handle, "%s",
1234                        fsp_str_do_log(fsp));
1235                 return NULL;
1236         }
1237         state->handle = handle;
1238         state->fsp = fsp;
1239
1240         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
1241                                          n, offset);
1242         if (tevent_req_nomem(subreq, req)) {
1243                 do_log(SMB_VFS_OP_PREAD_SEND, false, handle, "%s",
1244                        fsp_str_do_log(fsp));
1245                 return tevent_req_post(req, ev);
1246         }
1247         tevent_req_set_callback(subreq, smb_full_audit_pread_done, req);
1248
1249         do_log(SMB_VFS_OP_PREAD_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1250         return req;
1251 }
1252
1253 static void smb_full_audit_pread_done(struct tevent_req *subreq)
1254 {
1255         struct tevent_req *req = tevent_req_callback_data(
1256                 subreq, struct tevent_req);
1257         struct smb_full_audit_pread_state *state = tevent_req_data(
1258                 req, struct smb_full_audit_pread_state);
1259
1260         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1261         TALLOC_FREE(subreq);
1262         tevent_req_done(req);
1263 }
1264
1265 static ssize_t smb_full_audit_pread_recv(struct tevent_req *req,
1266                                          struct vfs_aio_state *vfs_aio_state)
1267 {
1268         struct smb_full_audit_pread_state *state = tevent_req_data(
1269                 req, struct smb_full_audit_pread_state);
1270
1271         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1272                 do_log(SMB_VFS_OP_PREAD_RECV, false, state->handle, "%s",
1273                        fsp_str_do_log(state->fsp));
1274                 return -1;
1275         }
1276
1277         do_log(SMB_VFS_OP_PREAD_RECV, (state->ret >= 0), state->handle, "%s",
1278                fsp_str_do_log(state->fsp));
1279
1280         *vfs_aio_state = state->vfs_aio_state;
1281         return state->ret;
1282 }
1283
1284 static ssize_t smb_full_audit_pwrite(vfs_handle_struct *handle, files_struct *fsp,
1285                             const void *data, size_t n,
1286                             off_t offset)
1287 {
1288         ssize_t result;
1289
1290         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
1291
1292         do_log(SMB_VFS_OP_PWRITE, (result >= 0), handle, "%s",
1293                fsp_str_do_log(fsp));
1294
1295         return result;
1296 }
1297
1298 struct smb_full_audit_pwrite_state {
1299         vfs_handle_struct *handle;
1300         files_struct *fsp;
1301         ssize_t ret;
1302         struct vfs_aio_state vfs_aio_state;
1303 };
1304
1305 static void smb_full_audit_pwrite_done(struct tevent_req *subreq);
1306
1307 static struct tevent_req *smb_full_audit_pwrite_send(
1308         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1309         struct tevent_context *ev, struct files_struct *fsp,
1310         const void *data, size_t n, off_t offset)
1311 {
1312         struct tevent_req *req, *subreq;
1313         struct smb_full_audit_pwrite_state *state;
1314
1315         req = tevent_req_create(mem_ctx, &state,
1316                                 struct smb_full_audit_pwrite_state);
1317         if (req == NULL) {
1318                 do_log(SMB_VFS_OP_PWRITE_SEND, false, handle, "%s",
1319                        fsp_str_do_log(fsp));
1320                 return NULL;
1321         }
1322         state->handle = handle;
1323         state->fsp = fsp;
1324
1325         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
1326                                          n, offset);
1327         if (tevent_req_nomem(subreq, req)) {
1328                 do_log(SMB_VFS_OP_PWRITE_SEND, false, handle, "%s",
1329                        fsp_str_do_log(fsp));
1330                 return tevent_req_post(req, ev);
1331         }
1332         tevent_req_set_callback(subreq, smb_full_audit_pwrite_done, req);
1333
1334         do_log(SMB_VFS_OP_PWRITE_SEND, true, handle, "%s",
1335                fsp_str_do_log(fsp));
1336         return req;
1337 }
1338
1339 static void smb_full_audit_pwrite_done(struct tevent_req *subreq)
1340 {
1341         struct tevent_req *req = tevent_req_callback_data(
1342                 subreq, struct tevent_req);
1343         struct smb_full_audit_pwrite_state *state = tevent_req_data(
1344                 req, struct smb_full_audit_pwrite_state);
1345
1346         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1347         TALLOC_FREE(subreq);
1348         tevent_req_done(req);
1349 }
1350
1351 static ssize_t smb_full_audit_pwrite_recv(struct tevent_req *req,
1352                                           struct vfs_aio_state *vfs_aio_state)
1353 {
1354         struct smb_full_audit_pwrite_state *state = tevent_req_data(
1355                 req, struct smb_full_audit_pwrite_state);
1356
1357         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1358                 do_log(SMB_VFS_OP_PWRITE_RECV, false, state->handle, "%s",
1359                        fsp_str_do_log(state->fsp));
1360                 return -1;
1361         }
1362
1363         do_log(SMB_VFS_OP_PWRITE_RECV, (state->ret >= 0), state->handle, "%s",
1364                fsp_str_do_log(state->fsp));
1365
1366         *vfs_aio_state = state->vfs_aio_state;
1367         return state->ret;
1368 }
1369
1370 static off_t smb_full_audit_lseek(vfs_handle_struct *handle, files_struct *fsp,
1371                              off_t offset, int whence)
1372 {
1373         ssize_t result;
1374
1375         result = SMB_VFS_NEXT_LSEEK(handle, fsp, offset, whence);
1376
1377         do_log(SMB_VFS_OP_LSEEK, (result != (ssize_t)-1), handle,
1378                "%s", fsp_str_do_log(fsp));
1379
1380         return result;
1381 }
1382
1383 static ssize_t smb_full_audit_sendfile(vfs_handle_struct *handle, int tofd,
1384                               files_struct *fromfsp,
1385                               const DATA_BLOB *hdr, off_t offset,
1386                               size_t n)
1387 {
1388         ssize_t result;
1389
1390         result = SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp, hdr, offset, n);
1391
1392         do_log(SMB_VFS_OP_SENDFILE, (result >= 0), handle,
1393                "%s", fsp_str_do_log(fromfsp));
1394
1395         return result;
1396 }
1397
1398 static ssize_t smb_full_audit_recvfile(vfs_handle_struct *handle, int fromfd,
1399                       files_struct *tofsp,
1400                               off_t offset,
1401                               size_t n)
1402 {
1403         ssize_t result;
1404
1405         result = SMB_VFS_NEXT_RECVFILE(handle, fromfd, tofsp, offset, n);
1406
1407         do_log(SMB_VFS_OP_RECVFILE, (result >= 0), handle,
1408                "%s", fsp_str_do_log(tofsp));
1409
1410         return result;
1411 }
1412
1413 static int smb_full_audit_renameat(vfs_handle_struct *handle,
1414                                 files_struct *srcfsp,
1415                                 const struct smb_filename *smb_fname_src,
1416                                 files_struct *dstfsp,
1417                                 const struct smb_filename *smb_fname_dst)
1418 {
1419         int result;
1420         int saved_errno;
1421         struct smb_filename *full_fname_src = NULL;
1422         struct smb_filename *full_fname_dst = NULL;
1423
1424         full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1425                                                       srcfsp,
1426                                                       smb_fname_src);
1427         if (full_fname_src == NULL) {
1428                 errno = ENOMEM;
1429                 return -1;
1430         }
1431         full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1432                                                       dstfsp,
1433                                                       smb_fname_dst);
1434         if (full_fname_dst == NULL) {
1435                 TALLOC_FREE(full_fname_src);
1436                 errno = ENOMEM;
1437                 return -1;
1438         }
1439
1440         result = SMB_VFS_NEXT_RENAMEAT(handle,
1441                                 srcfsp,
1442                                 smb_fname_src,
1443                                 dstfsp,
1444                                 smb_fname_dst);
1445
1446         if (result == -1) {
1447                 saved_errno = errno;
1448         }
1449         do_log(SMB_VFS_OP_RENAMEAT, (result >= 0), handle, "%s|%s",
1450                smb_fname_str_do_log(handle->conn, full_fname_src),
1451                smb_fname_str_do_log(handle->conn, full_fname_dst));
1452
1453         TALLOC_FREE(full_fname_src);
1454         TALLOC_FREE(full_fname_dst);
1455
1456         if (result == -1) {
1457                 errno = saved_errno;
1458         }
1459         return result;
1460 }
1461
1462 struct smb_full_audit_fsync_state {
1463         vfs_handle_struct *handle;
1464         files_struct *fsp;
1465         int ret;
1466         struct vfs_aio_state vfs_aio_state;
1467 };
1468
1469 static void smb_full_audit_fsync_done(struct tevent_req *subreq);
1470
1471 static struct tevent_req *smb_full_audit_fsync_send(
1472         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1473         struct tevent_context *ev, struct files_struct *fsp)
1474 {
1475         struct tevent_req *req, *subreq;
1476         struct smb_full_audit_fsync_state *state;
1477
1478         req = tevent_req_create(mem_ctx, &state,
1479                                 struct smb_full_audit_fsync_state);
1480         if (req == NULL) {
1481                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1482                        fsp_str_do_log(fsp));
1483                 return NULL;
1484         }
1485         state->handle = handle;
1486         state->fsp = fsp;
1487
1488         subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1489         if (tevent_req_nomem(subreq, req)) {
1490                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1491                        fsp_str_do_log(fsp));
1492                 return tevent_req_post(req, ev);
1493         }
1494         tevent_req_set_callback(subreq, smb_full_audit_fsync_done, req);
1495
1496         do_log(SMB_VFS_OP_FSYNC_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1497         return req;
1498 }
1499
1500 static void smb_full_audit_fsync_done(struct tevent_req *subreq)
1501 {
1502         struct tevent_req *req = tevent_req_callback_data(
1503                 subreq, struct tevent_req);
1504         struct smb_full_audit_fsync_state *state = tevent_req_data(
1505                 req, struct smb_full_audit_fsync_state);
1506
1507         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1508         TALLOC_FREE(subreq);
1509         tevent_req_done(req);
1510 }
1511
1512 static int smb_full_audit_fsync_recv(struct tevent_req *req,
1513                                      struct vfs_aio_state *vfs_aio_state)
1514 {
1515         struct smb_full_audit_fsync_state *state = tevent_req_data(
1516                 req, struct smb_full_audit_fsync_state);
1517
1518         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1519                 do_log(SMB_VFS_OP_FSYNC_RECV, false, state->handle, "%s",
1520                        fsp_str_do_log(state->fsp));
1521                 return -1;
1522         }
1523
1524         do_log(SMB_VFS_OP_FSYNC_RECV, (state->ret >= 0), state->handle, "%s",
1525                fsp_str_do_log(state->fsp));
1526
1527         *vfs_aio_state = state->vfs_aio_state;
1528         return state->ret;
1529 }
1530
1531 static int smb_full_audit_stat(vfs_handle_struct *handle,
1532                                struct smb_filename *smb_fname)
1533 {
1534         int result;
1535         
1536         result = SMB_VFS_NEXT_STAT(handle, smb_fname);
1537
1538         do_log(SMB_VFS_OP_STAT, (result >= 0), handle, "%s",
1539                smb_fname_str_do_log(handle->conn, smb_fname));
1540
1541         return result;    
1542 }
1543
1544 static int smb_full_audit_fstat(vfs_handle_struct *handle, files_struct *fsp,
1545                        SMB_STRUCT_STAT *sbuf)
1546 {
1547         int result;
1548         
1549         result = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1550
1551         do_log(SMB_VFS_OP_FSTAT, (result >= 0), handle, "%s",
1552                fsp_str_do_log(fsp));
1553
1554         return result;
1555 }
1556
1557 static int smb_full_audit_lstat(vfs_handle_struct *handle,
1558                                 struct smb_filename *smb_fname)
1559 {
1560         int result;
1561         
1562         result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1563
1564         do_log(SMB_VFS_OP_LSTAT, (result >= 0), handle, "%s",
1565                smb_fname_str_do_log(handle->conn, smb_fname));
1566
1567         return result;    
1568 }
1569
1570 static uint64_t smb_full_audit_get_alloc_size(vfs_handle_struct *handle,
1571                        files_struct *fsp, const SMB_STRUCT_STAT *sbuf)
1572 {
1573         uint64_t result;
1574
1575         result = SMB_VFS_NEXT_GET_ALLOC_SIZE(handle, fsp, sbuf);
1576
1577         do_log(SMB_VFS_OP_GET_ALLOC_SIZE, (result != (uint64_t)-1), handle,
1578                         "%llu", (unsigned long long)result);
1579
1580         return result;
1581 }
1582
1583 static int smb_full_audit_unlinkat(vfs_handle_struct *handle,
1584                         struct files_struct *dirfsp,
1585                         const struct smb_filename *smb_fname,
1586                         int flags)
1587 {
1588         struct smb_filename *full_fname = NULL;
1589         int result;
1590
1591         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1592                                                   dirfsp,
1593                                                   smb_fname);
1594         if (full_fname == NULL) {
1595                 return -1;
1596         }
1597
1598         result = SMB_VFS_NEXT_UNLINKAT(handle,
1599                         dirfsp,
1600                         smb_fname,
1601                         flags);
1602
1603         do_log(SMB_VFS_OP_UNLINKAT, (result >= 0), handle, "%s",
1604                smb_fname_str_do_log(handle->conn, full_fname));
1605
1606         TALLOC_FREE(full_fname);
1607         return result;
1608 }
1609
1610 static int smb_full_audit_fchmod(vfs_handle_struct *handle, files_struct *fsp,
1611                         mode_t mode)
1612 {
1613         int result;
1614         
1615         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1616
1617         do_log(SMB_VFS_OP_FCHMOD, (result >= 0), handle,
1618                "%s|%o", fsp_str_do_log(fsp), mode);
1619
1620         return result;
1621 }
1622
1623 static int smb_full_audit_fchown(vfs_handle_struct *handle, files_struct *fsp,
1624                         uid_t uid, gid_t gid)
1625 {
1626         int result;
1627
1628         result = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1629
1630         do_log(SMB_VFS_OP_FCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1631                fsp_str_do_log(fsp), (long int)uid, (long int)gid);
1632
1633         return result;
1634 }
1635
1636 static int smb_full_audit_lchown(vfs_handle_struct *handle,
1637                         const struct smb_filename *smb_fname,
1638                         uid_t uid,
1639                         gid_t gid)
1640 {
1641         int result;
1642
1643         result = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1644
1645         do_log(SMB_VFS_OP_LCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1646                smb_fname->base_name, (long int)uid, (long int)gid);
1647
1648         return result;
1649 }
1650
1651 static int smb_full_audit_chdir(vfs_handle_struct *handle,
1652                         const struct smb_filename *smb_fname)
1653 {
1654         int result;
1655
1656         result = SMB_VFS_NEXT_CHDIR(handle, smb_fname);
1657
1658         do_log(SMB_VFS_OP_CHDIR,
1659                (result >= 0),
1660                handle,
1661                "chdir|%s",
1662                smb_fname_str_do_log(handle->conn, smb_fname));
1663
1664         return result;
1665 }
1666
1667 static struct smb_filename *smb_full_audit_getwd(vfs_handle_struct *handle,
1668                                 TALLOC_CTX *ctx)
1669 {
1670         struct smb_filename *result;
1671
1672         result = SMB_VFS_NEXT_GETWD(handle, ctx);
1673         
1674         do_log(SMB_VFS_OP_GETWD, (result != NULL), handle, "%s",
1675                 result == NULL? "" : result->base_name);
1676
1677         return result;
1678 }
1679
1680 static int smb_full_audit_fntimes(vfs_handle_struct *handle,
1681                                   files_struct *fsp,
1682                                   struct smb_file_time *ft)
1683 {
1684         int result;
1685         time_t create_time = convert_timespec_to_time_t(ft->create_time);
1686         time_t atime = convert_timespec_to_time_t(ft->atime);
1687         time_t mtime = convert_timespec_to_time_t(ft->mtime);
1688         time_t ctime = convert_timespec_to_time_t(ft->ctime);
1689         const char *create_time_str = "";
1690         const char *atime_str = "";
1691         const char *mtime_str = "";
1692         const char *ctime_str = "";
1693         TALLOC_CTX *frame = talloc_stackframe();
1694
1695         if (frame == NULL) {
1696                 errno = ENOMEM;
1697                 return -1;
1698         }
1699
1700         result = SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
1701
1702         if (create_time > 0) {
1703                 create_time_str = timestring(frame, create_time);
1704         }
1705         if (atime > 0) {
1706                 atime_str = timestring(frame, atime);
1707         }
1708         if (mtime > 0) {
1709                 mtime_str = timestring(frame, mtime);
1710         }
1711         if (ctime > 0) {
1712                 ctime_str = timestring(frame, ctime);
1713         }
1714
1715         do_log(SMB_VFS_OP_FNTIMES,
1716                (result >= 0),
1717                handle,
1718                "%s|%s|%s|%s|%s",
1719                fsp_str_do_log(fsp),
1720                create_time_str,
1721                atime_str,
1722                mtime_str,
1723                ctime_str);
1724
1725         TALLOC_FREE(frame);
1726
1727         return result;
1728 }
1729
1730 static int smb_full_audit_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1731                            off_t len)
1732 {
1733         int result;
1734
1735         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1736
1737         do_log(SMB_VFS_OP_FTRUNCATE, (result >= 0), handle,
1738                "%s", fsp_str_do_log(fsp));
1739
1740         return result;
1741 }
1742
1743 static int smb_full_audit_fallocate(vfs_handle_struct *handle, files_struct *fsp,
1744                            uint32_t mode,
1745                            off_t offset,
1746                            off_t len)
1747 {
1748         int result;
1749
1750         result = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1751
1752         do_log(SMB_VFS_OP_FALLOCATE, (result >= 0), handle,
1753                "%s", fsp_str_do_log(fsp));
1754
1755         return result;
1756 }
1757
1758 static bool smb_full_audit_lock(vfs_handle_struct *handle, files_struct *fsp,
1759                        int op, off_t offset, off_t count, int type)
1760 {
1761         bool result;
1762
1763         result = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1764
1765         do_log(SMB_VFS_OP_LOCK, result, handle, "%s", fsp_str_do_log(fsp));
1766
1767         return result;
1768 }
1769
1770 static int smb_full_audit_kernel_flock(struct vfs_handle_struct *handle,
1771                                        struct files_struct *fsp,
1772                                        uint32_t share_access,
1773                                        uint32_t access_mask)
1774 {
1775         int result;
1776
1777         result = SMB_VFS_NEXT_FILESYSTEM_SHAREMODE(handle,
1778                                                    fsp,
1779                                                    share_access,
1780                                                    access_mask);
1781
1782         do_log(SMB_VFS_OP_KERNEL_FLOCK, (result >= 0), handle, "%s",
1783                fsp_str_do_log(fsp));
1784
1785         return result;
1786 }
1787
1788 static int smb_full_audit_fcntl(struct vfs_handle_struct *handle,
1789                                 struct files_struct *fsp,
1790                                 int cmd, va_list cmd_arg)
1791 {
1792         void *arg;
1793         va_list dup_cmd_arg;
1794         int result;
1795
1796         va_copy(dup_cmd_arg, cmd_arg);
1797         arg = va_arg(dup_cmd_arg, void *);
1798         result = SMB_VFS_NEXT_FCNTL(handle, fsp, cmd, arg);
1799         va_end(dup_cmd_arg);
1800
1801         do_log(SMB_VFS_OP_FCNTL, (result >= 0), handle, "%s",
1802                fsp_str_do_log(fsp));
1803
1804         return result;
1805 }
1806
1807 static int smb_full_audit_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1808                                  int leasetype)
1809 {
1810         int result;
1811
1812         result = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1813
1814         do_log(SMB_VFS_OP_LINUX_SETLEASE, (result >= 0), handle, "%s",
1815                fsp_str_do_log(fsp));
1816
1817         return result;
1818 }
1819
1820 static bool smb_full_audit_getlock(vfs_handle_struct *handle, files_struct *fsp,
1821                        off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1822 {
1823         bool result;
1824
1825         result = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype, ppid);
1826
1827         do_log(SMB_VFS_OP_GETLOCK, result, handle, "%s", fsp_str_do_log(fsp));
1828
1829         return result;
1830 }
1831
1832 static int smb_full_audit_symlinkat(vfs_handle_struct *handle,
1833                         const struct smb_filename *link_contents,
1834                         struct files_struct *dirfsp,
1835                         const struct smb_filename *new_smb_fname)
1836 {
1837         struct smb_filename *full_fname = NULL;
1838         int result;
1839
1840         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1841                                                 dirfsp,
1842                                                 new_smb_fname);
1843         if (full_fname == NULL) {
1844                 return -1;
1845         }
1846
1847         result = SMB_VFS_NEXT_SYMLINKAT(handle,
1848                                 link_contents,
1849                                 dirfsp,
1850                                 new_smb_fname);
1851
1852         do_log(SMB_VFS_OP_SYMLINKAT,
1853                (result >= 0),
1854                handle,
1855                "%s|%s",
1856                link_contents->base_name,
1857                smb_fname_str_do_log(handle->conn, full_fname));
1858
1859         TALLOC_FREE(full_fname);
1860
1861         return result;
1862 }
1863
1864 static int smb_full_audit_readlinkat(vfs_handle_struct *handle,
1865                         const struct files_struct *dirfsp,
1866                         const struct smb_filename *smb_fname,
1867                         char *buf,
1868                         size_t bufsiz)
1869 {
1870         struct smb_filename *full_fname = NULL;
1871         int result;
1872
1873         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1874                                                 dirfsp,
1875                                                 smb_fname);
1876         if (full_fname == NULL) {
1877                 return -1;
1878         }
1879
1880         result = SMB_VFS_NEXT_READLINKAT(handle,
1881                         dirfsp,
1882                         smb_fname,
1883                         buf,
1884                         bufsiz);
1885
1886         do_log(SMB_VFS_OP_READLINKAT,
1887                (result >= 0),
1888                handle,
1889                "%s",
1890                smb_fname_str_do_log(handle->conn, full_fname));
1891
1892         TALLOC_FREE(full_fname);
1893
1894         return result;
1895 }
1896
1897 static int smb_full_audit_linkat(vfs_handle_struct *handle,
1898                         files_struct *srcfsp,
1899                         const struct smb_filename *old_smb_fname,
1900                         files_struct *dstfsp,
1901                         const struct smb_filename *new_smb_fname,
1902                         int flags)
1903 {
1904         struct smb_filename *old_full_fname = NULL;
1905         struct smb_filename *new_full_fname = NULL;
1906         int result;
1907
1908         old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1909                                                 srcfsp,
1910                                                 old_smb_fname);
1911         if (old_full_fname == NULL) {
1912                 return -1;
1913         }
1914         new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1915                                                 dstfsp,
1916                                                 new_smb_fname);
1917         if (new_full_fname == NULL) {
1918                 TALLOC_FREE(old_full_fname);
1919                 return -1;
1920         }
1921         result = SMB_VFS_NEXT_LINKAT(handle,
1922                         srcfsp,
1923                         old_smb_fname,
1924                         dstfsp,
1925                         new_smb_fname,
1926                         flags);
1927
1928         do_log(SMB_VFS_OP_LINKAT,
1929                (result >= 0),
1930                handle,
1931                "%s|%s",
1932                smb_fname_str_do_log(handle->conn, old_full_fname),
1933                smb_fname_str_do_log(handle->conn, new_full_fname));
1934
1935         TALLOC_FREE(old_full_fname);
1936         TALLOC_FREE(new_full_fname);
1937
1938         return result;
1939 }
1940
1941 static int smb_full_audit_mknodat(vfs_handle_struct *handle,
1942                         files_struct *dirfsp,
1943                         const struct smb_filename *smb_fname,
1944                         mode_t mode,
1945                         SMB_DEV_T dev)
1946 {
1947         struct smb_filename *full_fname = NULL;
1948         int result;
1949
1950         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1951                                                 dirfsp,
1952                                                 smb_fname);
1953         if (full_fname == NULL) {
1954                 return -1;
1955         }
1956
1957         result = SMB_VFS_NEXT_MKNODAT(handle,
1958                                 dirfsp,
1959                                 smb_fname,
1960                                 mode,
1961                                 dev);
1962
1963         do_log(SMB_VFS_OP_MKNODAT,
1964                (result >= 0),
1965                handle,
1966                "%s",
1967                smb_fname_str_do_log(handle->conn, full_fname));
1968
1969         TALLOC_FREE(full_fname);
1970
1971         return result;
1972 }
1973
1974 static struct smb_filename *smb_full_audit_realpath(vfs_handle_struct *handle,
1975                                 TALLOC_CTX *ctx,
1976                                 const struct smb_filename *smb_fname)
1977 {
1978         struct smb_filename *result_fname = NULL;
1979
1980         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1981
1982         do_log(SMB_VFS_OP_REALPATH,
1983                (result_fname != NULL),
1984                handle,
1985                "%s",
1986                smb_fname_str_do_log(handle->conn, smb_fname));
1987
1988         return result_fname;
1989 }
1990
1991 static int smb_full_audit_fchflags(vfs_handle_struct *handle,
1992                         struct files_struct *fsp,
1993                         unsigned int flags)
1994 {
1995         int result;
1996
1997         result = SMB_VFS_NEXT_FCHFLAGS(handle, fsp, flags);
1998
1999         do_log(SMB_VFS_OP_FCHFLAGS,
2000                (result != 0),
2001                handle,
2002                "%s",
2003                smb_fname_str_do_log(handle->conn, fsp->fsp_name));
2004
2005         return result;
2006 }
2007
2008 static struct file_id smb_full_audit_file_id_create(struct vfs_handle_struct *handle,
2009                                                     const SMB_STRUCT_STAT *sbuf)
2010 {
2011         struct file_id id_zero = { 0 };
2012         struct file_id result;
2013         struct file_id_buf idbuf;
2014
2015         result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
2016
2017         do_log(SMB_VFS_OP_FILE_ID_CREATE,
2018                !file_id_equal(&id_zero, &result),
2019                handle,
2020                "%s",
2021                file_id_str_buf(result, &idbuf));
2022
2023         return result;
2024 }
2025
2026 static uint64_t smb_full_audit_fs_file_id(struct vfs_handle_struct *handle,
2027                                           const SMB_STRUCT_STAT *sbuf)
2028 {
2029         uint64_t result;
2030
2031         result = SMB_VFS_NEXT_FS_FILE_ID(handle, sbuf);
2032
2033         do_log(SMB_VFS_OP_FS_FILE_ID,
2034                result != 0,
2035                handle, "%" PRIu64, result);
2036
2037         return result;
2038 }
2039
2040 static NTSTATUS smb_full_audit_fstreaminfo(vfs_handle_struct *handle,
2041                                           struct files_struct *fsp,
2042                                           TALLOC_CTX *mem_ctx,
2043                                           unsigned int *pnum_streams,
2044                                           struct stream_struct **pstreams)
2045 {
2046         NTSTATUS result;
2047
2048         result = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
2049                                          pnum_streams, pstreams);
2050
2051         do_log(SMB_VFS_OP_FSTREAMINFO,
2052                NT_STATUS_IS_OK(result),
2053                handle,
2054                "%s",
2055                smb_fname_str_do_log(handle->conn, fsp->fsp_name));
2056
2057         return result;
2058 }
2059
2060 static int smb_full_audit_get_real_filename(struct vfs_handle_struct *handle,
2061                                             const struct smb_filename *path,
2062                                             const char *name,
2063                                             TALLOC_CTX *mem_ctx,
2064                                             char **found_name)
2065 {
2066         int result;
2067
2068         result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
2069                                                 found_name);
2070
2071         do_log(SMB_VFS_OP_GET_REAL_FILENAME, (result == 0), handle,
2072                "%s/%s->%s",
2073                path->base_name, name, (result == 0) ? *found_name : "");
2074
2075         return result;
2076 }
2077
2078 static const char *smb_full_audit_connectpath(vfs_handle_struct *handle,
2079                                         const struct smb_filename *smb_fname)
2080 {
2081         const char *result;
2082
2083         result = SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname);
2084
2085         do_log(SMB_VFS_OP_CONNECTPATH,
2086                result != NULL,
2087                handle,
2088                "%s",
2089                smb_fname_str_do_log(handle->conn, smb_fname));
2090
2091         return result;
2092 }
2093
2094 static NTSTATUS smb_full_audit_brl_lock_windows(struct vfs_handle_struct *handle,
2095                                                 struct byte_range_lock *br_lck,
2096                                                 struct lock_struct *plock)
2097 {
2098         NTSTATUS result;
2099
2100         result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock);
2101
2102         do_log(SMB_VFS_OP_BRL_LOCK_WINDOWS, NT_STATUS_IS_OK(result), handle,
2103             "%s:%llu-%llu. type=%d.",
2104                fsp_str_do_log(brl_fsp(br_lck)),
2105                (unsigned long long)plock->start,
2106                (unsigned long long)plock->size,
2107                plock->lock_type);
2108
2109         return result;
2110 }
2111
2112 static bool smb_full_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
2113                                               struct byte_range_lock *br_lck,
2114                                               const struct lock_struct *plock)
2115 {
2116         bool result;
2117
2118         result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, br_lck, plock);
2119
2120         do_log(SMB_VFS_OP_BRL_UNLOCK_WINDOWS, (result == 0), handle,
2121                "%s:%llu-%llu:%d", fsp_str_do_log(brl_fsp(br_lck)),
2122                (unsigned long long)plock->start,
2123                (unsigned long long)plock->size,
2124                plock->lock_type);
2125
2126         return result;
2127 }
2128
2129 static bool smb_full_audit_strict_lock_check(struct vfs_handle_struct *handle,
2130                                              struct files_struct *fsp,
2131                                              struct lock_struct *plock)
2132 {
2133         bool result;
2134
2135         result = SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
2136
2137         do_log(SMB_VFS_OP_STRICT_LOCK_CHECK, result, handle,
2138                "%s:%llu-%llu:%d", fsp_str_do_log(fsp),
2139                (unsigned long long)plock->start,
2140                (unsigned long long)plock->size,
2141                plock->lock_type);
2142
2143         return result;
2144 }
2145
2146 static NTSTATUS smb_full_audit_translate_name(struct vfs_handle_struct *handle,
2147                                               const char *name,
2148                                               enum vfs_translate_direction direction,
2149                                               TALLOC_CTX *mem_ctx,
2150                                               char **mapped_name)
2151 {
2152         NTSTATUS result;
2153
2154         result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
2155                                              mapped_name);
2156
2157         do_log(SMB_VFS_OP_TRANSLATE_NAME, NT_STATUS_IS_OK(result), handle, "");
2158
2159         return result;
2160 }
2161
2162 static NTSTATUS smb_full_audit_parent_pathname(struct vfs_handle_struct *handle,
2163                                                TALLOC_CTX *mem_ctx,
2164                                                const struct smb_filename *smb_fname_in,
2165                                                struct smb_filename **parent_dir_out,
2166                                                struct smb_filename **atname_out)
2167 {
2168         NTSTATUS result;
2169
2170         result = SMB_VFS_NEXT_PARENT_PATHNAME(handle,
2171                                               mem_ctx,
2172                                               smb_fname_in,
2173                                               parent_dir_out,
2174                                               atname_out);
2175         do_log(SMB_VFS_OP_CONNECTPATH,
2176                NT_STATUS_IS_OK(result),
2177                handle,
2178                "%s",
2179                smb_fname_str_do_log(handle->conn, smb_fname_in));
2180
2181         return result;
2182 }
2183
2184 static NTSTATUS smb_full_audit_fsctl(struct vfs_handle_struct *handle,
2185                                 struct files_struct *fsp,
2186                                 TALLOC_CTX *ctx,
2187                                 uint32_t function,
2188                                 uint16_t req_flags,
2189                                 const uint8_t *_in_data,
2190                                 uint32_t in_len,
2191                                 uint8_t **_out_data,
2192                                 uint32_t max_out_len,
2193                                 uint32_t *out_len)
2194 {
2195         NTSTATUS result;
2196
2197         result = SMB_VFS_NEXT_FSCTL(handle,
2198                                 fsp,
2199                                 ctx,
2200                                 function,
2201                                 req_flags,
2202                                 _in_data,
2203                                 in_len,
2204                                 _out_data,
2205                                 max_out_len,
2206                                 out_len);
2207
2208         do_log(SMB_VFS_OP_FSCTL, NT_STATUS_IS_OK(result), handle, "");
2209
2210         return result;
2211 }
2212
2213 static struct tevent_req *smb_full_audit_offload_read_send(
2214         TALLOC_CTX *mem_ctx,
2215         struct tevent_context *ev,
2216         struct vfs_handle_struct *handle,
2217         struct files_struct *fsp,
2218         uint32_t fsctl,
2219         uint32_t ttl,
2220         off_t offset,
2221         size_t to_copy)
2222 {
2223         struct tevent_req *req = NULL;
2224
2225         req = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
2226                                              fsctl, ttl, offset, to_copy);
2227
2228         do_log(SMB_VFS_OP_OFFLOAD_READ_SEND, req, handle, "");
2229
2230         return req;
2231 }
2232
2233 static NTSTATUS smb_full_audit_offload_read_recv(
2234         struct tevent_req *req,
2235         struct vfs_handle_struct *handle,
2236         TALLOC_CTX *mem_ctx,
2237         DATA_BLOB *_token_blob)
2238 {
2239         NTSTATUS status;
2240
2241         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(req, handle, mem_ctx,
2242                                                 _token_blob);
2243
2244         do_log(SMB_VFS_OP_OFFLOAD_READ_RECV, NT_STATUS_IS_OK(status), handle, "");
2245
2246         return status;
2247 }
2248
2249 static struct tevent_req *smb_full_audit_offload_write_send(struct vfs_handle_struct *handle,
2250                                                          TALLOC_CTX *mem_ctx,
2251                                                          struct tevent_context *ev,
2252                                                          uint32_t fsctl,
2253                                                          DATA_BLOB *token,
2254                                                          off_t transfer_offset,
2255                                                          struct files_struct *dest_fsp,
2256                                                          off_t dest_off,
2257                                                             off_t num)
2258 {
2259         struct tevent_req *req;
2260
2261         req = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle, mem_ctx, ev,
2262                                            fsctl, token, transfer_offset,
2263                                            dest_fsp, dest_off, num);
2264
2265         do_log(SMB_VFS_OP_OFFLOAD_WRITE_SEND, req, handle, "");
2266
2267         return req;
2268 }
2269
2270 static NTSTATUS smb_full_audit_offload_write_recv(struct vfs_handle_struct *handle,
2271                                                struct tevent_req *req,
2272                                                off_t *copied)
2273 {
2274         NTSTATUS result;
2275
2276         result = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(handle, req, copied);
2277
2278         do_log(SMB_VFS_OP_OFFLOAD_WRITE_RECV, NT_STATUS_IS_OK(result), handle, "");
2279
2280         return result;
2281 }
2282
2283 static NTSTATUS smb_full_audit_fget_compression(vfs_handle_struct *handle,
2284                                                TALLOC_CTX *mem_ctx,
2285                                                struct files_struct *fsp,
2286                                                uint16_t *_compression_fmt)
2287 {
2288         NTSTATUS result;
2289
2290         result = SMB_VFS_NEXT_FGET_COMPRESSION(handle, mem_ctx, fsp,
2291                                               _compression_fmt);
2292
2293         do_log(SMB_VFS_OP_FGET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2294                "%s",
2295                fsp_str_do_log(fsp));
2296
2297         return result;
2298 }
2299
2300 static NTSTATUS smb_full_audit_set_compression(vfs_handle_struct *handle,
2301                                                TALLOC_CTX *mem_ctx,
2302                                                struct files_struct *fsp,
2303                                                uint16_t compression_fmt)
2304 {
2305         NTSTATUS result;
2306
2307         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
2308                                               compression_fmt);
2309
2310         do_log(SMB_VFS_OP_SET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2311                "%s", fsp_str_do_log(fsp));
2312
2313         return result;
2314 }
2315
2316 static NTSTATUS smb_full_audit_freaddir_attr(struct vfs_handle_struct *handle,
2317                                         struct files_struct *fsp,
2318                                         TALLOC_CTX *mem_ctx,
2319                                         struct readdir_attr_data **pattr_data)
2320 {
2321         NTSTATUS status;
2322
2323         status = SMB_VFS_NEXT_FREADDIR_ATTR(handle, fsp, mem_ctx, pattr_data);
2324
2325         do_log(SMB_VFS_OP_FREADDIR_ATTR,
2326                NT_STATUS_IS_OK(status),
2327                handle,
2328                "%s",
2329                fsp_str_do_log(fsp));
2330
2331         return status;
2332 }
2333
2334 struct smb_full_audit_get_dos_attributes_state {
2335         struct vfs_aio_state aio_state;
2336         vfs_handle_struct *handle;
2337         files_struct *dir_fsp;
2338         const struct smb_filename *smb_fname;
2339         uint32_t dosmode;
2340 };
2341
2342 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq);
2343
2344 static struct tevent_req *smb_full_audit_get_dos_attributes_send(
2345                 TALLOC_CTX *mem_ctx,
2346                 struct tevent_context *ev,
2347                 struct vfs_handle_struct *handle,
2348                 files_struct *dir_fsp,
2349                 struct smb_filename *smb_fname)
2350 {
2351         struct tevent_req *req = NULL;
2352         struct smb_full_audit_get_dos_attributes_state *state = NULL;
2353         struct tevent_req *subreq = NULL;
2354
2355         req = tevent_req_create(mem_ctx, &state,
2356                                 struct smb_full_audit_get_dos_attributes_state);
2357         if (req == NULL) {
2358                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2359                        false,
2360                        handle,
2361                        "%s/%s",
2362                        fsp_str_do_log(dir_fsp),
2363                        smb_fname->base_name);
2364                 return NULL;
2365         }
2366         *state = (struct smb_full_audit_get_dos_attributes_state) {
2367                 .handle = handle,
2368                 .dir_fsp = dir_fsp,
2369                 .smb_fname = smb_fname,
2370         };
2371
2372         subreq = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_SEND(mem_ctx,
2373                                                       ev,
2374                                                       handle,
2375                                                       dir_fsp,
2376                                                       smb_fname);
2377         if (tevent_req_nomem(subreq, req)) {
2378                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2379                        false,
2380                        handle,
2381                        "%s/%s",
2382                        fsp_str_do_log(dir_fsp),
2383                        smb_fname->base_name);
2384                 return tevent_req_post(req, ev);
2385         }
2386         tevent_req_set_callback(subreq,
2387                                 smb_full_audit_get_dos_attributes_done,
2388                                 req);
2389
2390         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2391                true,
2392                handle,
2393                "%s/%s",
2394                fsp_str_do_log(dir_fsp),
2395                smb_fname->base_name);
2396
2397         return req;
2398 }
2399
2400 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq)
2401 {
2402         struct tevent_req *req =
2403                 tevent_req_callback_data(subreq,
2404                 struct tevent_req);
2405         struct smb_full_audit_get_dos_attributes_state *state =
2406                 tevent_req_data(req,
2407                 struct smb_full_audit_get_dos_attributes_state);
2408         NTSTATUS status;
2409
2410         status = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_RECV(subreq,
2411                                                       &state->aio_state,
2412                                                       &state->dosmode);
2413         TALLOC_FREE(subreq);
2414         if (tevent_req_nterror(req, status)) {
2415                 return;
2416         }
2417
2418         tevent_req_done(req);
2419         return;
2420 }
2421
2422 static NTSTATUS smb_full_audit_get_dos_attributes_recv(struct tevent_req *req,
2423                                                 struct vfs_aio_state *aio_state,
2424                                                 uint32_t *dosmode)
2425 {
2426         struct smb_full_audit_get_dos_attributes_state *state =
2427                 tevent_req_data(req,
2428                 struct smb_full_audit_get_dos_attributes_state);
2429         NTSTATUS status;
2430
2431         if (tevent_req_is_nterror(req, &status)) {
2432                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2433                        false,
2434                        state->handle,
2435                        "%s/%s",
2436                        fsp_str_do_log(state->dir_fsp),
2437                        state->smb_fname->base_name);
2438                 tevent_req_received(req);
2439                 return status;
2440         }
2441
2442         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2443                true,
2444                state->handle,
2445                "%s/%s",
2446                fsp_str_do_log(state->dir_fsp),
2447                state->smb_fname->base_name);
2448
2449         *aio_state = state->aio_state;
2450         *dosmode = state->dosmode;
2451         tevent_req_received(req);
2452         return NT_STATUS_OK;
2453 }
2454
2455 static NTSTATUS smb_full_audit_fget_dos_attributes(
2456                                 struct vfs_handle_struct *handle,
2457                                 struct files_struct *fsp,
2458                                 uint32_t *dosmode)
2459 {
2460         NTSTATUS status;
2461
2462         status = SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle,
2463                                 fsp,
2464                                 dosmode);
2465
2466         do_log(SMB_VFS_OP_FGET_DOS_ATTRIBUTES,
2467                 NT_STATUS_IS_OK(status),
2468                 handle,
2469                 "%s",
2470                 fsp_str_do_log(fsp));
2471
2472         return status;
2473 }
2474
2475 static NTSTATUS smb_full_audit_fset_dos_attributes(
2476                                 struct vfs_handle_struct *handle,
2477                                 struct files_struct *fsp,
2478                                 uint32_t dosmode)
2479 {
2480         NTSTATUS status;
2481
2482         status = SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle,
2483                                 fsp,
2484                                 dosmode);
2485
2486         do_log(SMB_VFS_OP_FSET_DOS_ATTRIBUTES,
2487                 NT_STATUS_IS_OK(status),
2488                 handle,
2489                 "%s",
2490                 fsp_str_do_log(fsp));
2491
2492         return status;
2493 }
2494
2495 static NTSTATUS smb_full_audit_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2496                                            uint32_t security_info,
2497                                            TALLOC_CTX *mem_ctx,
2498                                            struct security_descriptor **ppdesc)
2499 {
2500         NTSTATUS result;
2501
2502         result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2503                                           mem_ctx, ppdesc);
2504
2505         do_log(SMB_VFS_OP_FGET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2506                "%s", fsp_str_do_log(fsp));
2507
2508         return result;
2509 }
2510
2511 static NTSTATUS smb_full_audit_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2512                               uint32_t security_info_sent,
2513                               const struct security_descriptor *psd)
2514 {
2515         struct vfs_full_audit_private_data *pd;
2516         NTSTATUS result;
2517         char *sd = NULL;
2518
2519         SMB_VFS_HANDLE_GET_DATA(handle, pd,
2520                                 struct vfs_full_audit_private_data,
2521                                 return NT_STATUS_INTERNAL_ERROR);
2522
2523         if (pd->log_secdesc) {
2524                 sd = sddl_encode(talloc_tos(), psd, get_global_sam_sid());
2525         }
2526
2527         result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
2528
2529         do_log(SMB_VFS_OP_FSET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2530                "%s [%s]", fsp_str_do_log(fsp), sd ? sd : "");
2531
2532         TALLOC_FREE(sd);
2533
2534         return result;
2535 }
2536
2537 static NTSTATUS smb_full_audit_audit_file(struct vfs_handle_struct *handle,
2538                                 struct smb_filename *file,
2539                                 struct security_acl *sacl,
2540                                 uint32_t access_requested,
2541                                 uint32_t access_denied)
2542 {
2543         NTSTATUS result;
2544
2545         result = SMB_VFS_NEXT_AUDIT_FILE(handle,
2546                                         file,
2547                                         sacl,
2548                                         access_requested,
2549                                         access_denied);
2550
2551         do_log(SMB_VFS_OP_AUDIT_FILE, NT_STATUS_IS_OK(result), handle,
2552                         "%s",
2553                         smb_fname_str_do_log(handle->conn, file));
2554
2555         return result;
2556 }
2557
2558 static SMB_ACL_T smb_full_audit_sys_acl_get_fd(vfs_handle_struct *handle,
2559                                                files_struct *fsp,
2560                                                SMB_ACL_TYPE_T type,
2561                                                TALLOC_CTX *mem_ctx)
2562 {
2563         SMB_ACL_T result;
2564
2565         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle,
2566                                              fsp,
2567                                              type,
2568                                              mem_ctx);
2569
2570         do_log(SMB_VFS_OP_SYS_ACL_GET_FD, (result != NULL), handle,
2571                "%s", fsp_str_do_log(fsp));
2572
2573         return result;
2574 }
2575
2576 static int smb_full_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
2577                                               files_struct *fsp,
2578                                               TALLOC_CTX *mem_ctx,
2579                                               char **blob_description,
2580                                               DATA_BLOB *blob)
2581 {
2582         int result;
2583
2584         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
2585
2586         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FD, (result >= 0), handle,
2587                "%s", fsp_str_do_log(fsp));
2588
2589         return result;
2590 }
2591
2592 static int smb_full_audit_sys_acl_set_fd(vfs_handle_struct *handle,
2593                                          struct files_struct *fsp,
2594                                          SMB_ACL_TYPE_T type,
2595                                          SMB_ACL_T theacl)
2596 {
2597         int result;
2598
2599         result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
2600
2601         do_log(SMB_VFS_OP_SYS_ACL_SET_FD, (result >= 0), handle,
2602                "%s", fsp_str_do_log(fsp));
2603
2604         return result;
2605 }
2606
2607 static int smb_full_audit_sys_acl_delete_def_fd(vfs_handle_struct *handle,
2608                                 struct files_struct *fsp)
2609 {
2610         int result;
2611
2612         result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FD(handle, fsp);
2613
2614         do_log(SMB_VFS_OP_SYS_ACL_DELETE_DEF_FD,
2615                (result >= 0),
2616                handle,
2617                "%s",
2618                fsp_str_do_log(fsp));
2619
2620         return result;
2621 }
2622
2623 struct smb_full_audit_getxattrat_state {
2624         struct vfs_aio_state aio_state;
2625         vfs_handle_struct *handle;
2626         files_struct *dir_fsp;
2627         const struct smb_filename *smb_fname;
2628         const char *xattr_name;
2629         ssize_t xattr_size;
2630         uint8_t *xattr_value;
2631 };
2632
2633 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq);
2634
2635 static struct tevent_req *smb_full_audit_getxattrat_send(
2636                         TALLOC_CTX *mem_ctx,
2637                         struct tevent_context *ev,
2638                         struct vfs_handle_struct *handle,
2639                         files_struct *dir_fsp,
2640                         const struct smb_filename *smb_fname,
2641                         const char *xattr_name,
2642                         size_t alloc_hint)
2643 {
2644         struct tevent_req *req = NULL;
2645         struct tevent_req *subreq = NULL;
2646         struct smb_full_audit_getxattrat_state *state = NULL;
2647
2648         req = tevent_req_create(mem_ctx, &state,
2649                                 struct smb_full_audit_getxattrat_state);
2650         if (req == NULL) {
2651                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2652                        false,
2653                        handle,
2654                        "%s/%s|%s",
2655                        fsp_str_do_log(dir_fsp),
2656                        smb_fname->base_name,
2657                        xattr_name);
2658                 return NULL;
2659         }
2660         *state = (struct smb_full_audit_getxattrat_state) {
2661                 .handle = handle,
2662                 .dir_fsp = dir_fsp,
2663                 .smb_fname = smb_fname,
2664                 .xattr_name = xattr_name,
2665         };
2666
2667         subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
2668                                               ev,
2669                                               handle,
2670                                               dir_fsp,
2671                                               smb_fname,
2672                                               xattr_name,
2673                                               alloc_hint);
2674         if (tevent_req_nomem(subreq, req)) {
2675                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2676                        false,
2677                        handle,
2678                        "%s/%s|%s",
2679                        fsp_str_do_log(dir_fsp),
2680                        smb_fname->base_name,
2681                        xattr_name);
2682                 return tevent_req_post(req, ev);
2683         }
2684         tevent_req_set_callback(subreq, smb_full_audit_getxattrat_done, req);
2685
2686         do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2687                true,
2688                handle,
2689                "%s/%s|%s",
2690                fsp_str_do_log(dir_fsp),
2691                smb_fname->base_name,
2692                xattr_name);
2693
2694         return req;
2695 }
2696
2697 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq)
2698 {
2699         struct tevent_req *req = tevent_req_callback_data(
2700                 subreq, struct tevent_req);
2701         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2702                 req, struct smb_full_audit_getxattrat_state);
2703
2704         state->xattr_size = SMB_VFS_NEXT_GETXATTRAT_RECV(subreq,
2705                                                          &state->aio_state,
2706                                                          state,
2707                                                          &state->xattr_value);
2708         TALLOC_FREE(subreq);
2709         if (state->xattr_size == -1) {
2710                 tevent_req_error(req, state->aio_state.error);
2711                 return;
2712         }
2713
2714         tevent_req_done(req);
2715 }
2716
2717 static ssize_t smb_full_audit_getxattrat_recv(struct tevent_req *req,
2718                                               struct vfs_aio_state *aio_state,
2719                                               TALLOC_CTX *mem_ctx,
2720                                               uint8_t **xattr_value)
2721 {
2722         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2723                 req, struct smb_full_audit_getxattrat_state);
2724         ssize_t xattr_size;
2725
2726         if (tevent_req_is_unix_error(req, &aio_state->error)) {
2727                 do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2728                        false,
2729                        state->handle,
2730                        "%s/%s|%s",
2731                        fsp_str_do_log(state->dir_fsp),
2732                        state->smb_fname->base_name,
2733                        state->xattr_name);
2734                 tevent_req_received(req);
2735                 return -1;
2736         }
2737
2738         do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2739                true,
2740                state->handle,
2741                "%s/%s|%s",
2742                fsp_str_do_log(state->dir_fsp),
2743                state->smb_fname->base_name,
2744                state->xattr_name);
2745
2746         *aio_state = state->aio_state;
2747         xattr_size = state->xattr_size;
2748         if (xattr_value != NULL) {
2749                 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2750         }
2751
2752         tevent_req_received(req);
2753         return xattr_size;
2754 }
2755
2756 static ssize_t smb_full_audit_fgetxattr(struct vfs_handle_struct *handle,
2757                                struct files_struct *fsp,
2758                                const char *name, void *value, size_t size)
2759 {
2760         ssize_t result;
2761
2762         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
2763
2764         do_log(SMB_VFS_OP_FGETXATTR, (result >= 0), handle,
2765                "%s|%s", fsp_str_do_log(fsp), name);
2766
2767         return result;
2768 }
2769
2770 static ssize_t smb_full_audit_flistxattr(struct vfs_handle_struct *handle,
2771                                 struct files_struct *fsp, char *list,
2772                                 size_t size)
2773 {
2774         ssize_t result;
2775
2776         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
2777
2778         do_log(SMB_VFS_OP_FLISTXATTR, (result >= 0), handle,
2779                "%s", fsp_str_do_log(fsp));
2780
2781         return result;
2782 }
2783
2784 static int smb_full_audit_fremovexattr(struct vfs_handle_struct *handle,
2785                               struct files_struct *fsp,
2786                               const char *name)
2787 {
2788         int result;
2789
2790         result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2791
2792         do_log(SMB_VFS_OP_FREMOVEXATTR, (result >= 0), handle,
2793                "%s|%s", fsp_str_do_log(fsp), name);
2794
2795         return result;
2796 }
2797
2798 static int smb_full_audit_fsetxattr(struct vfs_handle_struct *handle,
2799                            struct files_struct *fsp, const char *name,
2800                            const void *value, size_t size, int flags)
2801 {
2802         int result;
2803
2804         result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2805
2806         do_log(SMB_VFS_OP_FSETXATTR, (result >= 0), handle,
2807                "%s|%s", fsp_str_do_log(fsp), name);
2808
2809         return result;
2810 }
2811
2812 static bool smb_full_audit_aio_force(struct vfs_handle_struct *handle,
2813                                      struct files_struct *fsp)
2814 {
2815         bool result;
2816
2817         result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2818         do_log(SMB_VFS_OP_AIO_FORCE, result, handle,
2819                 "%s", fsp_str_do_log(fsp));
2820
2821         return result;
2822 }
2823
2824 static NTSTATUS smb_full_audit_durable_cookie(struct vfs_handle_struct *handle,
2825                                 struct files_struct *fsp,
2826                                 TALLOC_CTX *mem_ctx,
2827                                 DATA_BLOB *cookie)
2828 {
2829         NTSTATUS result;
2830
2831         result = SMB_VFS_NEXT_DURABLE_COOKIE(handle,
2832                                         fsp,
2833                                         mem_ctx,
2834                                         cookie);
2835
2836         do_log(SMB_VFS_OP_DURABLE_COOKIE, NT_STATUS_IS_OK(result), handle,
2837                         "%s", fsp_str_do_log(fsp));
2838
2839         return result;
2840 }
2841
2842 static NTSTATUS smb_full_audit_durable_disconnect(
2843                                 struct vfs_handle_struct *handle,
2844                                 struct files_struct *fsp,
2845                                 const DATA_BLOB old_cookie,
2846                                 TALLOC_CTX *mem_ctx,
2847                                 DATA_BLOB *new_cookie)
2848 {
2849         NTSTATUS result;
2850
2851         result = SMB_VFS_NEXT_DURABLE_DISCONNECT(handle,
2852                                         fsp,
2853                                         old_cookie,
2854                                         mem_ctx,
2855                                         new_cookie);
2856
2857         do_log(SMB_VFS_OP_DURABLE_DISCONNECT, NT_STATUS_IS_OK(result), handle,
2858                         "%s", fsp_str_do_log(fsp));
2859
2860         return result;
2861 }
2862
2863 static NTSTATUS smb_full_audit_durable_reconnect(
2864                                 struct vfs_handle_struct *handle,
2865                                 struct smb_request *smb1req,
2866                                 struct smbXsrv_open *op,
2867                                 const DATA_BLOB old_cookie,
2868                                 TALLOC_CTX *mem_ctx,
2869                                 struct files_struct **fsp,
2870                                 DATA_BLOB *new_cookie)
2871 {
2872         NTSTATUS result;
2873
2874         result = SMB_VFS_NEXT_DURABLE_RECONNECT(handle,
2875                                         smb1req,
2876                                         op,
2877                                         old_cookie,
2878                                         mem_ctx,
2879                                         fsp,
2880                                         new_cookie);
2881
2882         do_log(SMB_VFS_OP_DURABLE_RECONNECT,
2883                         NT_STATUS_IS_OK(result),
2884                         handle,
2885                         "");
2886
2887         return result;
2888 }
2889
2890 static struct vfs_fn_pointers vfs_full_audit_fns = {
2891
2892         /* Disk operations */
2893
2894         .connect_fn = smb_full_audit_connect,
2895         .disconnect_fn = smb_full_audit_disconnect,
2896         .disk_free_fn = smb_full_audit_disk_free,
2897         .get_quota_fn = smb_full_audit_get_quota,
2898         .set_quota_fn = smb_full_audit_set_quota,
2899         .get_shadow_copy_data_fn = smb_full_audit_get_shadow_copy_data,
2900         .statvfs_fn = smb_full_audit_statvfs,
2901         .fs_capabilities_fn = smb_full_audit_fs_capabilities,
2902         .get_dfs_referrals_fn = smb_full_audit_get_dfs_referrals,
2903         .create_dfs_pathat_fn = smb_full_audit_create_dfs_pathat,
2904         .read_dfs_pathat_fn = smb_full_audit_read_dfs_pathat,
2905         .fdopendir_fn = smb_full_audit_fdopendir,
2906         .readdir_fn = smb_full_audit_readdir,
2907         .seekdir_fn = smb_full_audit_seekdir,
2908         .telldir_fn = smb_full_audit_telldir,
2909         .rewind_dir_fn = smb_full_audit_rewinddir,
2910         .mkdirat_fn = smb_full_audit_mkdirat,
2911         .closedir_fn = smb_full_audit_closedir,
2912         .openat_fn = smb_full_audit_openat,
2913         .create_file_fn = smb_full_audit_create_file,
2914         .close_fn = smb_full_audit_close,
2915         .pread_fn = smb_full_audit_pread,
2916         .pread_send_fn = smb_full_audit_pread_send,
2917         .pread_recv_fn = smb_full_audit_pread_recv,
2918         .pwrite_fn = smb_full_audit_pwrite,
2919         .pwrite_send_fn = smb_full_audit_pwrite_send,
2920         .pwrite_recv_fn = smb_full_audit_pwrite_recv,
2921         .lseek_fn = smb_full_audit_lseek,
2922         .sendfile_fn = smb_full_audit_sendfile,
2923         .recvfile_fn = smb_full_audit_recvfile,
2924         .renameat_fn = smb_full_audit_renameat,
2925         .fsync_send_fn = smb_full_audit_fsync_send,
2926         .fsync_recv_fn = smb_full_audit_fsync_recv,
2927         .stat_fn = smb_full_audit_stat,
2928         .fstat_fn = smb_full_audit_fstat,
2929         .lstat_fn = smb_full_audit_lstat,
2930         .get_alloc_size_fn = smb_full_audit_get_alloc_size,
2931         .unlinkat_fn = smb_full_audit_unlinkat,
2932         .fchmod_fn = smb_full_audit_fchmod,
2933         .fchown_fn = smb_full_audit_fchown,
2934         .lchown_fn = smb_full_audit_lchown,
2935         .chdir_fn = smb_full_audit_chdir,
2936         .getwd_fn = smb_full_audit_getwd,
2937         .fntimes_fn = smb_full_audit_fntimes,
2938         .ftruncate_fn = smb_full_audit_ftruncate,
2939         .fallocate_fn = smb_full_audit_fallocate,
2940         .lock_fn = smb_full_audit_lock,
2941         .filesystem_sharemode_fn = smb_full_audit_kernel_flock,
2942         .fcntl_fn = smb_full_audit_fcntl,
2943         .linux_setlease_fn = smb_full_audit_linux_setlease,
2944         .getlock_fn = smb_full_audit_getlock,
2945         .symlinkat_fn = smb_full_audit_symlinkat,
2946         .readlinkat_fn = smb_full_audit_readlinkat,
2947         .linkat_fn = smb_full_audit_linkat,
2948         .mknodat_fn = smb_full_audit_mknodat,
2949         .realpath_fn = smb_full_audit_realpath,
2950         .fchflags_fn = smb_full_audit_fchflags,
2951         .file_id_create_fn = smb_full_audit_file_id_create,
2952         .fs_file_id_fn = smb_full_audit_fs_file_id,
2953         .offload_read_send_fn = smb_full_audit_offload_read_send,
2954         .offload_read_recv_fn = smb_full_audit_offload_read_recv,
2955         .offload_write_send_fn = smb_full_audit_offload_write_send,
2956         .offload_write_recv_fn = smb_full_audit_offload_write_recv,
2957         .fget_compression_fn = smb_full_audit_fget_compression,
2958         .set_compression_fn = smb_full_audit_set_compression,
2959         .snap_check_path_fn =  smb_full_audit_snap_check_path,
2960         .snap_create_fn = smb_full_audit_snap_create,
2961         .snap_delete_fn = smb_full_audit_snap_delete,
2962         .fstreaminfo_fn = smb_full_audit_fstreaminfo,
2963         .get_real_filename_fn = smb_full_audit_get_real_filename,
2964         .connectpath_fn = smb_full_audit_connectpath,
2965         .brl_lock_windows_fn = smb_full_audit_brl_lock_windows,
2966         .brl_unlock_windows_fn = smb_full_audit_brl_unlock_windows,
2967         .strict_lock_check_fn = smb_full_audit_strict_lock_check,
2968         .translate_name_fn = smb_full_audit_translate_name,
2969         .parent_pathname_fn = smb_full_audit_parent_pathname,
2970         .fsctl_fn = smb_full_audit_fsctl,
2971         .get_dos_attributes_send_fn = smb_full_audit_get_dos_attributes_send,
2972         .get_dos_attributes_recv_fn = smb_full_audit_get_dos_attributes_recv,
2973         .fget_dos_attributes_fn = smb_full_audit_fget_dos_attributes,
2974         .fset_dos_attributes_fn = smb_full_audit_fset_dos_attributes,
2975         .fget_nt_acl_fn = smb_full_audit_fget_nt_acl,
2976         .fset_nt_acl_fn = smb_full_audit_fset_nt_acl,
2977         .audit_file_fn = smb_full_audit_audit_file,
2978         .sys_acl_get_fd_fn = smb_full_audit_sys_acl_get_fd,
2979         .sys_acl_blob_get_fd_fn = smb_full_audit_sys_acl_blob_get_fd,
2980         .sys_acl_set_fd_fn = smb_full_audit_sys_acl_set_fd,
2981         .sys_acl_delete_def_fd_fn = smb_full_audit_sys_acl_delete_def_fd,
2982         .getxattrat_send_fn = smb_full_audit_getxattrat_send,
2983         .getxattrat_recv_fn = smb_full_audit_getxattrat_recv,
2984         .fgetxattr_fn = smb_full_audit_fgetxattr,
2985         .flistxattr_fn = smb_full_audit_flistxattr,
2986         .fremovexattr_fn = smb_full_audit_fremovexattr,
2987         .fsetxattr_fn = smb_full_audit_fsetxattr,
2988         .aio_force_fn = smb_full_audit_aio_force,
2989         .durable_cookie_fn = smb_full_audit_durable_cookie,
2990         .durable_disconnect_fn = smb_full_audit_durable_disconnect,
2991         .durable_reconnect_fn = smb_full_audit_durable_reconnect,
2992         .freaddir_attr_fn = smb_full_audit_freaddir_attr,
2993 };
2994
2995 static_decl_vfs;
2996 NTSTATUS vfs_full_audit_init(TALLOC_CTX *ctx)
2997 {
2998         NTSTATUS ret;
2999
3000         smb_vfs_assert_all_fns(&vfs_full_audit_fns, "full_audit");
3001
3002         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "full_audit",
3003                                &vfs_full_audit_fns);
3004
3005         if (!NT_STATUS_IS_OK(ret))
3006                 return ret;
3007
3008         vfs_full_audit_debug_level = debug_add_class("full_audit");
3009         if (vfs_full_audit_debug_level == -1) {
3010                 vfs_full_audit_debug_level = DBGC_VFS;
3011                 DEBUG(0, ("vfs_full_audit: Couldn't register custom debugging "
3012                           "class!\n"));
3013         } else {
3014                 DEBUG(10, ("vfs_full_audit: Debug class number of "
3015                            "'full_audit': %d\n", vfs_full_audit_debug_level));
3016         }
3017         
3018         return ret;
3019 }