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