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