VERSION: Re-enable git snapshots.
[Samba.git] / source3 / modules / vfs_time_audit.c
blob95b4148232b369878c62a599d44d5b70358bddff
1 /*
2 * Time auditing VFS module for samba. Log time taken for VFS call to syslog
3 * facility.
5 * Copyright (C) Abhidnya Chirmule <achirmul@in.ibm.com> 2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * This module implements logging for time taken for all Samba VFS operations.
24 * vfs objects = time_audit
28 #include "includes.h"
29 #include "smbd/smbd.h"
30 #include "ntioctl.h"
31 #include "lib/util/tevent_unix.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_VFS
36 static double audit_timeout;
38 static void smb_time_audit_log_msg(const char *syscallname, double elapsed,
39 const char *msg)
41 DEBUG(0, ("WARNING: VFS call \"%s\" took unexpectedly long "
42 "(%.2f seconds) %s%s-- Validate that file and storage "
43 "subsystems are operating normally\n", syscallname,
44 elapsed, (msg != NULL) ? msg : "",
45 (msg != NULL) ? " " : ""));
48 static void smb_time_audit_log(const char *syscallname, double elapsed)
50 smb_time_audit_log_msg(syscallname, elapsed, NULL);
53 static void smb_time_audit_log_fsp(const char *syscallname, double elapsed,
54 const struct files_struct *fsp)
56 char *base_name = NULL;
57 char *connectpath = NULL;
58 char *msg = NULL;
60 if (fsp == NULL) {
61 smb_time_audit_log(syscallname, elapsed);
62 return;
64 if (fsp->conn)
65 connectpath = fsp->conn->connectpath;
66 if (fsp->fsp_name)
67 base_name = fsp->fsp_name->base_name;
69 if (connectpath != NULL && base_name != NULL) {
70 msg = talloc_asprintf(talloc_tos(), "filename = \"%s/%s\"",
71 connectpath, base_name);
72 } else if (connectpath != NULL && base_name == NULL) {
73 msg = talloc_asprintf(talloc_tos(), "connectpath = \"%s\", "
74 "base_name = <NULL>",
75 connectpath);
76 } else if (connectpath == NULL && base_name != NULL) {
77 msg = talloc_asprintf(talloc_tos(), "connectpath = <NULL>, "
78 "base_name = \"%s\"",
79 base_name);
80 } else { /* connectpath == NULL && base_name == NULL */
81 msg = talloc_asprintf(talloc_tos(), "connectpath = <NULL>, "
82 "base_name = <NULL>");
84 smb_time_audit_log_msg(syscallname, elapsed, msg);
85 TALLOC_FREE(msg);
88 static void smb_time_audit_log_fname(const char *syscallname, double elapsed,
89 const char *fname)
91 char cwd[PATH_MAX];
92 char *msg = NULL;
94 if (getcwd(cwd, sizeof(cwd)) == NULL) {
95 snprintf(cwd, sizeof(cwd), "<getcwd() error %d>", errno);
97 if (fname != NULL) {
98 msg = talloc_asprintf(talloc_tos(),
99 "cwd = \"%s\", filename = \"%s\"",
100 cwd, fname);
101 } else {
102 msg = talloc_asprintf(talloc_tos(),
103 "cwd = \"%s\", filename = <NULL>",
104 cwd);
106 smb_time_audit_log_msg(syscallname, elapsed, msg);
107 TALLOC_FREE(msg);
110 static void smb_time_audit_log_smb_fname(const char *syscallname, double elapsed,
111 const struct smb_filename *smb_fname)
113 if (smb_fname != NULL) {
114 smb_time_audit_log_fname(syscallname, elapsed,
115 smb_fname->base_name);
116 } else {
117 smb_time_audit_log_fname(syscallname, elapsed,
118 "smb_fname = <NULL>");
122 static int smb_time_audit_connect(vfs_handle_struct *handle,
123 const char *svc, const char *user)
125 int result;
126 struct timespec ts1,ts2;
127 double timediff;
129 if (!handle) {
130 return -1;
133 clock_gettime_mono(&ts1);
134 result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
135 clock_gettime_mono(&ts2);
136 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
137 if (timediff > audit_timeout) {
138 smb_time_audit_log_msg("connect", timediff, user);
140 return result;
143 static void smb_time_audit_disconnect(vfs_handle_struct *handle)
145 struct timespec ts1,ts2;
146 double timediff;
148 clock_gettime_mono(&ts1);
149 SMB_VFS_NEXT_DISCONNECT(handle);
150 clock_gettime_mono(&ts2);
151 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
153 if (timediff > audit_timeout) {
154 smb_time_audit_log("disconnect", timediff);
158 static uint64_t smb_time_audit_disk_free(vfs_handle_struct *handle,
159 const char *path,
160 bool small_query, uint64_t *bsize,
161 uint64_t *dfree, uint64_t *dsize)
163 uint64_t result;
164 struct timespec ts1,ts2;
165 double timediff;
167 clock_gettime_mono(&ts1);
168 result = SMB_VFS_NEXT_DISK_FREE(handle, path, small_query, bsize,
169 dfree, dsize);
170 clock_gettime_mono(&ts2);
171 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
173 /* Don't have a reasonable notion of failure here */
174 if (timediff > audit_timeout) {
175 smb_time_audit_log_fname("disk_free", timediff, path);
178 return result;
181 static int smb_time_audit_get_quota(struct vfs_handle_struct *handle,
182 enum SMB_QUOTA_TYPE qtype, unid_t id,
183 SMB_DISK_QUOTA *qt)
185 int result;
186 struct timespec ts1,ts2;
187 double timediff;
189 clock_gettime_mono(&ts1);
190 result = SMB_VFS_NEXT_GET_QUOTA(handle, qtype, id, qt);
191 clock_gettime_mono(&ts2);
192 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
194 if (timediff > audit_timeout) {
195 smb_time_audit_log("get_quota", timediff);
197 return result;
200 static int smb_time_audit_set_quota(struct vfs_handle_struct *handle,
201 enum SMB_QUOTA_TYPE qtype, unid_t id,
202 SMB_DISK_QUOTA *qt)
204 int result;
205 struct timespec ts1,ts2;
206 double timediff;
208 clock_gettime_mono(&ts1);
209 result = SMB_VFS_NEXT_SET_QUOTA(handle, qtype, id, qt);
210 clock_gettime_mono(&ts2);
211 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
213 if (timediff > audit_timeout) {
214 smb_time_audit_log("set_quota", timediff);
217 return result;
220 static int smb_time_audit_get_shadow_copy_data(struct vfs_handle_struct *handle,
221 struct files_struct *fsp,
222 struct shadow_copy_data *shadow_copy_data,
223 bool labels)
225 int result;
226 struct timespec ts1,ts2;
227 double timediff;
229 clock_gettime_mono(&ts1);
230 result = SMB_VFS_NEXT_GET_SHADOW_COPY_DATA(handle, fsp,
231 shadow_copy_data, labels);
232 clock_gettime_mono(&ts2);
233 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
235 if (timediff > audit_timeout) {
236 smb_time_audit_log_fsp("get_shadow_copy_data", timediff, fsp);
239 return result;
242 static int smb_time_audit_statvfs(struct vfs_handle_struct *handle,
243 const char *path,
244 struct vfs_statvfs_struct *statbuf)
246 int result;
247 struct timespec ts1,ts2;
248 double timediff;
250 clock_gettime_mono(&ts1);
251 result = SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
252 clock_gettime_mono(&ts2);
253 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
255 if (timediff > audit_timeout) {
256 smb_time_audit_log_fname("statvfs", timediff, path);
259 return result;
262 static uint32_t smb_time_audit_fs_capabilities(struct vfs_handle_struct *handle,
263 enum timestamp_set_resolution *p_ts_res)
265 uint32_t result;
266 struct timespec ts1,ts2;
267 double timediff;
269 clock_gettime_mono(&ts1);
270 result = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
271 clock_gettime_mono(&ts2);
272 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
274 if (timediff > audit_timeout) {
275 smb_time_audit_log("fs_capabilities", timediff);
278 return result;
281 static DIR *smb_time_audit_opendir(vfs_handle_struct *handle,
282 const char *fname,
283 const char *mask, uint32 attr)
285 DIR *result;
286 struct timespec ts1,ts2;
287 double timediff;
289 clock_gettime_mono(&ts1);
290 result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
291 clock_gettime_mono(&ts2);
292 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
294 if (timediff > audit_timeout) {
295 smb_time_audit_log_fname("opendir", timediff, fname);
298 return result;
301 static DIR *smb_time_audit_fdopendir(vfs_handle_struct *handle,
302 files_struct *fsp,
303 const char *mask, uint32 attr)
305 DIR *result;
306 struct timespec ts1,ts2;
307 double timediff;
309 clock_gettime_mono(&ts1);
310 result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
311 clock_gettime_mono(&ts2);
312 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
314 if (timediff > audit_timeout) {
315 smb_time_audit_log_fsp("fdopendir", timediff, fsp);
318 return result;
321 static struct dirent *smb_time_audit_readdir(vfs_handle_struct *handle,
322 DIR *dirp,
323 SMB_STRUCT_STAT *sbuf)
325 struct dirent *result;
326 struct timespec ts1,ts2;
327 double timediff;
329 clock_gettime_mono(&ts1);
330 result = SMB_VFS_NEXT_READDIR(handle, dirp, sbuf);
331 clock_gettime_mono(&ts2);
332 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
334 if (timediff > audit_timeout) {
335 smb_time_audit_log("readdir", timediff);
338 return result;
341 static void smb_time_audit_seekdir(vfs_handle_struct *handle,
342 DIR *dirp, long offset)
344 struct timespec ts1,ts2;
345 double timediff;
347 clock_gettime_mono(&ts1);
348 SMB_VFS_NEXT_SEEKDIR(handle, dirp, offset);
349 clock_gettime_mono(&ts2);
350 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
352 if (timediff > audit_timeout) {
353 smb_time_audit_log("seekdir", timediff);
358 static long smb_time_audit_telldir(vfs_handle_struct *handle,
359 DIR *dirp)
361 long result;
362 struct timespec ts1,ts2;
363 double timediff;
365 clock_gettime_mono(&ts1);
366 result = SMB_VFS_NEXT_TELLDIR(handle, dirp);
367 clock_gettime_mono(&ts2);
368 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
370 if (timediff > audit_timeout) {
371 smb_time_audit_log("telldir", timediff);
374 return result;
377 static void smb_time_audit_rewinddir(vfs_handle_struct *handle,
378 DIR *dirp)
380 struct timespec ts1,ts2;
381 double timediff;
383 clock_gettime_mono(&ts1);
384 SMB_VFS_NEXT_REWINDDIR(handle, dirp);
385 clock_gettime_mono(&ts2);
386 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
388 if (timediff > audit_timeout) {
389 smb_time_audit_log("rewinddir", timediff);
394 static int smb_time_audit_mkdir(vfs_handle_struct *handle,
395 const char *path, mode_t mode)
397 int result;
398 struct timespec ts1,ts2;
399 double timediff;
401 clock_gettime_mono(&ts1);
402 result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
403 clock_gettime_mono(&ts2);
404 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
406 if (timediff > audit_timeout) {
407 smb_time_audit_log_fname("mkdir", timediff, path);
410 return result;
413 static int smb_time_audit_rmdir(vfs_handle_struct *handle,
414 const char *path)
416 int result;
417 struct timespec ts1,ts2;
418 double timediff;
420 clock_gettime_mono(&ts1);
421 result = SMB_VFS_NEXT_RMDIR(handle, path);
422 clock_gettime_mono(&ts2);
423 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
425 if (timediff > audit_timeout) {
426 smb_time_audit_log_fname("rmdir", timediff, path);
429 return result;
432 static int smb_time_audit_closedir(vfs_handle_struct *handle,
433 DIR *dirp)
435 int result;
436 struct timespec ts1,ts2;
437 double timediff;
439 clock_gettime_mono(&ts1);
440 result = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
441 clock_gettime_mono(&ts2);
442 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
444 if (timediff > audit_timeout) {
445 smb_time_audit_log("closedir", timediff);
448 return result;
451 static void smb_time_audit_init_search_op(vfs_handle_struct *handle,
452 DIR *dirp)
454 struct timespec ts1,ts2;
455 double timediff;
457 clock_gettime_mono(&ts1);
458 SMB_VFS_NEXT_INIT_SEARCH_OP(handle, dirp);
459 clock_gettime_mono(&ts2);
460 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
462 if (timediff > audit_timeout) {
463 smb_time_audit_log("init_search_op", timediff);
467 static int smb_time_audit_open(vfs_handle_struct *handle,
468 struct smb_filename *fname,
469 files_struct *fsp,
470 int flags, mode_t mode)
472 int result;
473 struct timespec ts1,ts2;
474 double timediff;
476 clock_gettime_mono(&ts1);
477 result = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
478 clock_gettime_mono(&ts2);
479 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
481 if (timediff > audit_timeout) {
482 smb_time_audit_log_fsp("open", timediff, fsp);
485 return result;
488 static NTSTATUS smb_time_audit_create_file(vfs_handle_struct *handle,
489 struct smb_request *req,
490 uint16_t root_dir_fid,
491 struct smb_filename *fname,
492 uint32_t access_mask,
493 uint32_t share_access,
494 uint32_t create_disposition,
495 uint32_t create_options,
496 uint32_t file_attributes,
497 uint32_t oplock_request,
498 uint64_t allocation_size,
499 uint32_t private_flags,
500 struct security_descriptor *sd,
501 struct ea_list *ea_list,
502 files_struct **result_fsp,
503 int *pinfo)
505 NTSTATUS result;
506 struct timespec ts1,ts2;
507 double timediff;
509 clock_gettime_mono(&ts1);
510 result = SMB_VFS_NEXT_CREATE_FILE(
511 handle, /* handle */
512 req, /* req */
513 root_dir_fid, /* root_dir_fid */
514 fname, /* fname */
515 access_mask, /* access_mask */
516 share_access, /* share_access */
517 create_disposition, /* create_disposition*/
518 create_options, /* create_options */
519 file_attributes, /* file_attributes */
520 oplock_request, /* oplock_request */
521 allocation_size, /* allocation_size */
522 private_flags,
523 sd, /* sd */
524 ea_list, /* ea_list */
525 result_fsp, /* result */
526 pinfo);
527 clock_gettime_mono(&ts2);
528 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
530 if (timediff > audit_timeout) {
532 * can't use result_fsp this time, may have
533 * invalid content causing smbd crash
535 smb_time_audit_log_smb_fname("create_file", timediff,
536 fname);
539 return result;
542 static int smb_time_audit_close(vfs_handle_struct *handle, files_struct *fsp)
544 int result;
545 struct timespec ts1,ts2;
546 double timediff;
548 clock_gettime_mono(&ts1);
549 result = SMB_VFS_NEXT_CLOSE(handle, fsp);
550 clock_gettime_mono(&ts2);
551 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
553 if (timediff > audit_timeout) {
554 smb_time_audit_log_fsp("close", timediff, fsp);
557 return result;
560 static ssize_t smb_time_audit_read(vfs_handle_struct *handle,
561 files_struct *fsp, void *data, size_t n)
563 ssize_t result;
564 struct timespec ts1,ts2;
565 double timediff;
567 clock_gettime_mono(&ts1);
568 result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
569 clock_gettime_mono(&ts2);
570 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
572 if (timediff > audit_timeout) {
573 smb_time_audit_log_fsp("read", timediff, fsp);
576 return result;
579 static ssize_t smb_time_audit_pread(vfs_handle_struct *handle,
580 files_struct *fsp,
581 void *data, size_t n, off_t offset)
583 ssize_t result;
584 struct timespec ts1,ts2;
585 double timediff;
587 clock_gettime_mono(&ts1);
588 result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
589 clock_gettime_mono(&ts2);
590 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
592 if (timediff > audit_timeout) {
593 smb_time_audit_log_fsp("pread", timediff, fsp);
596 return result;
599 struct smb_time_audit_pread_state {
600 struct files_struct *fsp;
601 struct timespec ts1;
602 ssize_t ret;
603 int err;
606 static void smb_time_audit_pread_done(struct tevent_req *subreq);
608 static struct tevent_req *smb_time_audit_pread_send(
609 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
610 struct tevent_context *ev, struct files_struct *fsp,
611 void *data, size_t n, off_t offset)
613 struct tevent_req *req, *subreq;
614 struct smb_time_audit_pread_state *state;
616 req = tevent_req_create(mem_ctx, &state,
617 struct smb_time_audit_pread_state);
618 if (req == NULL) {
619 return NULL;
621 clock_gettime_mono(&state->ts1);
622 state->fsp = fsp;
624 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
625 n, offset);
626 if (tevent_req_nomem(subreq, req)) {
627 return tevent_req_post(req, ev);
629 tevent_req_set_callback(subreq, smb_time_audit_pread_done, req);
630 return req;
633 static void smb_time_audit_pread_done(struct tevent_req *subreq)
635 struct tevent_req *req = tevent_req_callback_data(
636 subreq, struct tevent_req);
637 struct smb_time_audit_pread_state *state = tevent_req_data(
638 req, struct smb_time_audit_pread_state);
640 state->ret = SMB_VFS_PREAD_RECV(subreq, &state->err);
641 TALLOC_FREE(subreq);
642 tevent_req_done(req);
645 static ssize_t smb_time_audit_pread_recv(struct tevent_req *req, int *err)
647 struct smb_time_audit_pread_state *state = tevent_req_data(
648 req, struct smb_time_audit_pread_state);
649 struct timespec ts2;
650 double timediff;
652 clock_gettime_mono(&ts2);
653 timediff = nsec_time_diff(&ts2,&state->ts1)*1.0e-9;
655 if (timediff > audit_timeout) {
656 smb_time_audit_log_fsp("pread", timediff, state->fsp);
659 if (tevent_req_is_unix_error(req, err)) {
660 return -1;
662 *err = state->err;
663 return state->ret;
666 static ssize_t smb_time_audit_write(vfs_handle_struct *handle,
667 files_struct *fsp,
668 const void *data, size_t n)
670 ssize_t result;
671 struct timespec ts1,ts2;
672 double timediff;
674 clock_gettime_mono(&ts1);
675 result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
676 clock_gettime_mono(&ts2);
677 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
679 if (timediff > audit_timeout) {
680 smb_time_audit_log_fsp("write", timediff, fsp);
683 return result;
686 static ssize_t smb_time_audit_pwrite(vfs_handle_struct *handle,
687 files_struct *fsp,
688 const void *data, size_t n,
689 off_t offset)
691 ssize_t result;
692 struct timespec ts1,ts2;
693 double timediff;
695 clock_gettime_mono(&ts1);
696 result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
697 clock_gettime_mono(&ts2);
698 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
700 if (timediff > audit_timeout) {
701 smb_time_audit_log_fsp("pwrite", timediff, fsp);
704 return result;
707 struct smb_time_audit_pwrite_state {
708 struct files_struct *fsp;
709 struct timespec ts1;
710 ssize_t ret;
711 int err;
714 static void smb_time_audit_pwrite_done(struct tevent_req *subreq);
716 static struct tevent_req *smb_time_audit_pwrite_send(
717 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
718 struct tevent_context *ev, struct files_struct *fsp,
719 const void *data, size_t n, off_t offset)
721 struct tevent_req *req, *subreq;
722 struct smb_time_audit_pwrite_state *state;
724 req = tevent_req_create(mem_ctx, &state,
725 struct smb_time_audit_pwrite_state);
726 if (req == NULL) {
727 return NULL;
729 clock_gettime_mono(&state->ts1);
730 state->fsp = fsp;
732 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
733 n, offset);
734 if (tevent_req_nomem(subreq, req)) {
735 return tevent_req_post(req, ev);
737 tevent_req_set_callback(subreq, smb_time_audit_pwrite_done, req);
738 return req;
741 static void smb_time_audit_pwrite_done(struct tevent_req *subreq)
743 struct tevent_req *req = tevent_req_callback_data(
744 subreq, struct tevent_req);
745 struct smb_time_audit_pwrite_state *state = tevent_req_data(
746 req, struct smb_time_audit_pwrite_state);
748 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->err);
749 TALLOC_FREE(subreq);
750 tevent_req_done(req);
753 static ssize_t smb_time_audit_pwrite_recv(struct tevent_req *req, int *err)
755 struct smb_time_audit_pwrite_state *state = tevent_req_data(
756 req, struct smb_time_audit_pwrite_state);
757 struct timespec ts2;
758 double timediff;
760 clock_gettime_mono(&ts2);
761 timediff = nsec_time_diff(&ts2,&state->ts1)*1.0e-9;
763 if (timediff > audit_timeout) {
764 smb_time_audit_log_fsp("pwrite", timediff, state->fsp);
767 if (tevent_req_is_unix_error(req, err)) {
768 return -1;
770 *err = state->err;
771 return state->ret;
774 static off_t smb_time_audit_lseek(vfs_handle_struct *handle,
775 files_struct *fsp,
776 off_t offset, int whence)
778 off_t result;
779 struct timespec ts1,ts2;
780 double timediff;
782 clock_gettime_mono(&ts1);
783 result = SMB_VFS_NEXT_LSEEK(handle, fsp, offset, whence);
784 clock_gettime_mono(&ts2);
785 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
787 if (timediff > audit_timeout) {
788 smb_time_audit_log_fsp("lseek", timediff, fsp);
791 return result;
794 static ssize_t smb_time_audit_sendfile(vfs_handle_struct *handle, int tofd,
795 files_struct *fromfsp,
796 const DATA_BLOB *hdr, off_t offset,
797 size_t n)
799 ssize_t result;
800 struct timespec ts1,ts2;
801 double timediff;
803 clock_gettime_mono(&ts1);
804 result = SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp, hdr, offset, n);
805 clock_gettime_mono(&ts2);
806 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
808 if (timediff > audit_timeout) {
809 smb_time_audit_log_fsp("sendfile", timediff, fromfsp);
812 return result;
815 static ssize_t smb_time_audit_recvfile(vfs_handle_struct *handle, int fromfd,
816 files_struct *tofsp,
817 off_t offset,
818 size_t n)
820 ssize_t result;
821 struct timespec ts1,ts2;
822 double timediff;
824 clock_gettime_mono(&ts1);
825 result = SMB_VFS_NEXT_RECVFILE(handle, fromfd, tofsp, offset, n);
826 clock_gettime_mono(&ts2);
827 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
829 if (timediff > audit_timeout) {
830 smb_time_audit_log_fsp("recvfile", timediff, tofsp);
833 return result;
836 static int smb_time_audit_rename(vfs_handle_struct *handle,
837 const struct smb_filename *oldname,
838 const struct smb_filename *newname)
840 int result;
841 struct timespec ts1,ts2;
842 double timediff;
844 clock_gettime_mono(&ts1);
845 result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
846 clock_gettime_mono(&ts2);
847 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
849 if (timediff > audit_timeout) {
850 smb_time_audit_log_smb_fname("rename", timediff, newname);
853 return result;
856 static int smb_time_audit_fsync(vfs_handle_struct *handle, files_struct *fsp)
858 int result;
859 struct timespec ts1,ts2;
860 double timediff;
862 clock_gettime_mono(&ts1);
863 result = SMB_VFS_NEXT_FSYNC(handle, fsp);
864 clock_gettime_mono(&ts2);
865 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
867 if (timediff > audit_timeout) {
868 smb_time_audit_log_fsp("fsync", timediff, fsp);
871 return result;
874 struct smb_time_audit_fsync_state {
875 struct files_struct *fsp;
876 struct timespec ts1;
877 int ret;
878 int err;
881 static void smb_time_audit_fsync_done(struct tevent_req *subreq);
883 static struct tevent_req *smb_time_audit_fsync_send(
884 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
885 struct tevent_context *ev, struct files_struct *fsp)
887 struct tevent_req *req, *subreq;
888 struct smb_time_audit_fsync_state *state;
890 req = tevent_req_create(mem_ctx, &state,
891 struct smb_time_audit_fsync_state);
892 if (req == NULL) {
893 return NULL;
895 clock_gettime_mono(&state->ts1);
896 state->fsp = fsp;
898 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
899 if (tevent_req_nomem(subreq, req)) {
900 return tevent_req_post(req, ev);
902 tevent_req_set_callback(subreq, smb_time_audit_fsync_done, req);
903 return req;
906 static void smb_time_audit_fsync_done(struct tevent_req *subreq)
908 struct tevent_req *req = tevent_req_callback_data(
909 subreq, struct tevent_req);
910 struct smb_time_audit_fsync_state *state = tevent_req_data(
911 req, struct smb_time_audit_fsync_state);
913 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->err);
914 TALLOC_FREE(subreq);
915 tevent_req_done(req);
918 static int smb_time_audit_fsync_recv(struct tevent_req *req, int *err)
920 struct smb_time_audit_fsync_state *state = tevent_req_data(
921 req, struct smb_time_audit_fsync_state);
922 struct timespec ts2;
923 double timediff;
925 clock_gettime_mono(&ts2);
926 timediff = nsec_time_diff(&ts2,&state->ts1)*1.0e-9;
928 if (timediff > audit_timeout) {
929 smb_time_audit_log_fsp("fsync", timediff, state->fsp);
932 if (tevent_req_is_unix_error(req, err)) {
933 return -1;
935 *err = state->err;
936 return state->ret;
939 static int smb_time_audit_stat(vfs_handle_struct *handle,
940 struct smb_filename *fname)
942 int result;
943 struct timespec ts1,ts2;
944 double timediff;
946 clock_gettime_mono(&ts1);
947 result = SMB_VFS_NEXT_STAT(handle, fname);
948 clock_gettime_mono(&ts2);
949 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
951 if (timediff > audit_timeout) {
952 smb_time_audit_log_smb_fname("stat", timediff, fname);
955 return result;
958 static int smb_time_audit_fstat(vfs_handle_struct *handle, files_struct *fsp,
959 SMB_STRUCT_STAT *sbuf)
961 int result;
962 struct timespec ts1,ts2;
963 double timediff;
965 clock_gettime_mono(&ts1);
966 result = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
967 clock_gettime_mono(&ts2);
968 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
970 if (timediff > audit_timeout) {
971 smb_time_audit_log_fsp("fstat", timediff, fsp);
974 return result;
977 static int smb_time_audit_lstat(vfs_handle_struct *handle,
978 struct smb_filename *path)
980 int result;
981 struct timespec ts1,ts2;
982 double timediff;
984 clock_gettime_mono(&ts1);
985 result = SMB_VFS_NEXT_LSTAT(handle, path);
986 clock_gettime_mono(&ts2);
987 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
989 if (timediff > audit_timeout) {
990 smb_time_audit_log_smb_fname("lstat", timediff, path);
993 return result;
996 static uint64_t smb_time_audit_get_alloc_size(vfs_handle_struct *handle,
997 files_struct *fsp,
998 const SMB_STRUCT_STAT *sbuf)
1000 uint64_t result;
1001 struct timespec ts1,ts2;
1002 double timediff;
1004 clock_gettime_mono(&ts1);
1005 result = SMB_VFS_NEXT_GET_ALLOC_SIZE(handle, fsp, sbuf);
1006 clock_gettime_mono(&ts2);
1007 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1009 if (timediff > audit_timeout) {
1010 smb_time_audit_log_fsp("get_alloc_size", timediff, fsp);
1013 return result;
1016 static int smb_time_audit_unlink(vfs_handle_struct *handle,
1017 const struct smb_filename *path)
1019 int result;
1020 struct timespec ts1,ts2;
1021 double timediff;
1023 clock_gettime_mono(&ts1);
1024 result = SMB_VFS_NEXT_UNLINK(handle, path);
1025 clock_gettime_mono(&ts2);
1026 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1028 if (timediff > audit_timeout) {
1029 smb_time_audit_log_smb_fname("unlink", timediff, path);
1032 return result;
1035 static int smb_time_audit_chmod(vfs_handle_struct *handle,
1036 const char *path, mode_t mode)
1038 int result;
1039 struct timespec ts1,ts2;
1040 double timediff;
1042 clock_gettime_mono(&ts1);
1043 result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
1044 clock_gettime_mono(&ts2);
1045 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1047 if (timediff > audit_timeout) {
1048 smb_time_audit_log_fname("chmod", timediff, path);
1051 return result;
1054 static int smb_time_audit_fchmod(vfs_handle_struct *handle, files_struct *fsp,
1055 mode_t mode)
1057 int result;
1058 struct timespec ts1,ts2;
1059 double timediff;
1061 clock_gettime_mono(&ts1);
1062 result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1063 clock_gettime_mono(&ts2);
1064 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1066 if (timediff > audit_timeout) {
1067 smb_time_audit_log_fsp("fchmod", timediff, fsp);
1070 return result;
1073 static int smb_time_audit_chown(vfs_handle_struct *handle,
1074 const char *path, uid_t uid, gid_t gid)
1076 int result;
1077 struct timespec ts1,ts2;
1078 double timediff;
1080 clock_gettime_mono(&ts1);
1081 result = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
1082 clock_gettime_mono(&ts2);
1083 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1085 if (timediff > audit_timeout) {
1086 smb_time_audit_log_fname("chown", timediff, path);
1089 return result;
1092 static int smb_time_audit_fchown(vfs_handle_struct *handle, files_struct *fsp,
1093 uid_t uid, gid_t gid)
1095 int result;
1096 struct timespec ts1,ts2;
1097 double timediff;
1099 clock_gettime_mono(&ts1);
1100 result = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1101 clock_gettime_mono(&ts2);
1102 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1104 if (timediff > audit_timeout) {
1105 smb_time_audit_log_fsp("fchown", timediff, fsp);
1108 return result;
1111 static int smb_time_audit_lchown(vfs_handle_struct *handle,
1112 const char *path, uid_t uid, gid_t gid)
1114 int result;
1115 struct timespec ts1,ts2;
1116 double timediff;
1118 clock_gettime_mono(&ts1);
1119 result = SMB_VFS_NEXT_LCHOWN(handle, path, uid, gid);
1120 clock_gettime_mono(&ts2);
1121 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1123 if (timediff > audit_timeout) {
1124 smb_time_audit_log_fname("lchown", timediff, path);
1127 return result;
1130 static int smb_time_audit_chdir(vfs_handle_struct *handle, const char *path)
1132 int result;
1133 struct timespec ts1,ts2;
1134 double timediff;
1136 clock_gettime_mono(&ts1);
1137 result = SMB_VFS_NEXT_CHDIR(handle, path);
1138 clock_gettime_mono(&ts2);
1139 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1141 if (timediff > audit_timeout) {
1142 smb_time_audit_log_fname("chdir", timediff, path);
1145 return result;
1148 static char *smb_time_audit_getwd(vfs_handle_struct *handle)
1150 char *result;
1151 struct timespec ts1,ts2;
1152 double timediff;
1154 clock_gettime_mono(&ts1);
1155 result = SMB_VFS_NEXT_GETWD(handle);
1156 clock_gettime_mono(&ts2);
1157 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1159 if (timediff > audit_timeout) {
1160 smb_time_audit_log("getwd", timediff);
1163 return result;
1166 static int smb_time_audit_ntimes(vfs_handle_struct *handle,
1167 const struct smb_filename *path,
1168 struct smb_file_time *ft)
1170 int result;
1171 struct timespec ts1,ts2;
1172 double timediff;
1174 clock_gettime_mono(&ts1);
1175 result = SMB_VFS_NEXT_NTIMES(handle, path, ft);
1176 clock_gettime_mono(&ts2);
1177 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1179 if (timediff > audit_timeout) {
1180 smb_time_audit_log_smb_fname("ntimes", timediff, path);
1183 return result;
1186 static int smb_time_audit_ftruncate(vfs_handle_struct *handle,
1187 files_struct *fsp,
1188 off_t len)
1190 int result;
1191 struct timespec ts1,ts2;
1192 double timediff;
1194 clock_gettime_mono(&ts1);
1195 result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1196 clock_gettime_mono(&ts2);
1197 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1199 if (timediff > audit_timeout) {
1200 smb_time_audit_log_fsp("ftruncate", timediff, fsp);
1203 return result;
1206 static int smb_time_audit_fallocate(vfs_handle_struct *handle,
1207 files_struct *fsp,
1208 enum vfs_fallocate_mode mode,
1209 off_t offset,
1210 off_t len)
1212 int result;
1213 struct timespec ts1,ts2;
1214 double timediff;
1216 clock_gettime_mono(&ts1);
1217 result = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1218 clock_gettime_mono(&ts2);
1219 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1221 if (timediff > audit_timeout) {
1222 smb_time_audit_log_fsp("fallocate", timediff, fsp);
1225 return result;
1228 static bool smb_time_audit_lock(vfs_handle_struct *handle, files_struct *fsp,
1229 int op, off_t offset, off_t count,
1230 int type)
1232 bool result;
1233 struct timespec ts1,ts2;
1234 double timediff;
1236 clock_gettime_mono(&ts1);
1237 result = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1238 clock_gettime_mono(&ts2);
1239 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1241 if (timediff > audit_timeout) {
1242 smb_time_audit_log_fsp("lock", timediff, fsp);
1245 return result;
1248 static int smb_time_audit_kernel_flock(struct vfs_handle_struct *handle,
1249 struct files_struct *fsp,
1250 uint32 share_mode, uint32 access_mask)
1252 int result;
1253 struct timespec ts1,ts2;
1254 double timediff;
1256 clock_gettime_mono(&ts1);
1257 result = SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp, share_mode,
1258 access_mask);
1259 clock_gettime_mono(&ts2);
1260 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1262 if (timediff > audit_timeout) {
1263 smb_time_audit_log_fsp("kernel_flock", timediff, fsp);
1266 return result;
1269 static int smb_time_audit_linux_setlease(vfs_handle_struct *handle,
1270 files_struct *fsp,
1271 int leasetype)
1273 int result;
1274 struct timespec ts1,ts2;
1275 double timediff;
1277 clock_gettime_mono(&ts1);
1278 result = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1279 clock_gettime_mono(&ts2);
1280 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1282 if (timediff > audit_timeout) {
1283 smb_time_audit_log_fsp("linux_setlease", timediff, fsp);
1286 return result;
1289 static bool smb_time_audit_getlock(vfs_handle_struct *handle,
1290 files_struct *fsp,
1291 off_t *poffset, off_t *pcount,
1292 int *ptype, pid_t *ppid)
1294 bool result;
1295 struct timespec ts1,ts2;
1296 double timediff;
1298 clock_gettime_mono(&ts1);
1299 result = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype,
1300 ppid);
1301 clock_gettime_mono(&ts2);
1302 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1304 if (timediff > audit_timeout) {
1305 smb_time_audit_log_fsp("getlock", timediff, fsp);
1308 return result;
1311 static int smb_time_audit_symlink(vfs_handle_struct *handle,
1312 const char *oldpath, const char *newpath)
1314 int result;
1315 struct timespec ts1,ts2;
1316 double timediff;
1318 clock_gettime_mono(&ts1);
1319 result = SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
1320 clock_gettime_mono(&ts2);
1321 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1323 if (timediff > audit_timeout) {
1324 smb_time_audit_log_fname("symlink", timediff, newpath);
1327 return result;
1330 static int smb_time_audit_readlink(vfs_handle_struct *handle,
1331 const char *path, char *buf, size_t bufsiz)
1333 int result;
1334 struct timespec ts1,ts2;
1335 double timediff;
1337 clock_gettime_mono(&ts1);
1338 result = SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
1339 clock_gettime_mono(&ts2);
1340 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1342 if (timediff > audit_timeout) {
1343 smb_time_audit_log_fname("readlink", timediff, path);
1346 return result;
1349 static int smb_time_audit_link(vfs_handle_struct *handle,
1350 const char *oldpath, const char *newpath)
1352 int result;
1353 struct timespec ts1,ts2;
1354 double timediff;
1356 clock_gettime_mono(&ts1);
1357 result = SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
1358 clock_gettime_mono(&ts2);
1359 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1361 if (timediff > audit_timeout) {
1362 smb_time_audit_log_fname("link", timediff, newpath);
1365 return result;
1368 static int smb_time_audit_mknod(vfs_handle_struct *handle,
1369 const char *pathname, mode_t mode,
1370 SMB_DEV_T dev)
1372 int result;
1373 struct timespec ts1,ts2;
1374 double timediff;
1376 clock_gettime_mono(&ts1);
1377 result = SMB_VFS_NEXT_MKNOD(handle, pathname, mode, dev);
1378 clock_gettime_mono(&ts2);
1379 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1381 if (timediff > audit_timeout) {
1382 smb_time_audit_log_fname("mknod", timediff, pathname);
1385 return result;
1388 static char *smb_time_audit_realpath(vfs_handle_struct *handle,
1389 const char *path)
1391 char *result;
1392 struct timespec ts1,ts2;
1393 double timediff;
1395 clock_gettime_mono(&ts1);
1396 result = SMB_VFS_NEXT_REALPATH(handle, path);
1397 clock_gettime_mono(&ts2);
1398 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1400 if (timediff > audit_timeout) {
1401 smb_time_audit_log_fname("realpath", timediff, path);
1404 return result;
1407 static NTSTATUS smb_time_audit_notify_watch(struct vfs_handle_struct *handle,
1408 struct sys_notify_context *ctx,
1409 const char *path,
1410 uint32_t *filter,
1411 uint32_t *subdir_filter,
1412 void (*callback)(struct sys_notify_context *ctx,
1413 void *private_data,
1414 struct notify_event *ev),
1415 void *private_data, void *handle_p)
1417 NTSTATUS result;
1418 struct timespec ts1,ts2;
1419 double timediff;
1421 clock_gettime_mono(&ts1);
1422 result = SMB_VFS_NEXT_NOTIFY_WATCH(handle, ctx, path,
1423 filter, subdir_filter, callback,
1424 private_data, handle_p);
1425 clock_gettime_mono(&ts2);
1426 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1428 if (timediff > audit_timeout) {
1429 smb_time_audit_log_fname("notify_watch", timediff, path);
1432 return result;
1435 static int smb_time_audit_chflags(vfs_handle_struct *handle,
1436 const char *path, unsigned int flags)
1438 int result;
1439 struct timespec ts1,ts2;
1440 double timediff;
1442 clock_gettime_mono(&ts1);
1443 result = SMB_VFS_NEXT_CHFLAGS(handle, path, flags);
1444 clock_gettime_mono(&ts2);
1445 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1447 if (timediff > audit_timeout) {
1448 smb_time_audit_log_fname("chflags", timediff, path);
1451 return result;
1454 static struct file_id smb_time_audit_file_id_create(struct vfs_handle_struct *handle,
1455 const SMB_STRUCT_STAT *sbuf)
1457 struct file_id id_zero;
1458 struct file_id result;
1459 struct timespec ts1,ts2;
1460 double timediff;
1462 ZERO_STRUCT(id_zero);
1464 clock_gettime_mono(&ts1);
1465 result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
1466 clock_gettime_mono(&ts2);
1467 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1469 if (timediff > audit_timeout) {
1470 smb_time_audit_log("file_id_create", timediff);
1473 return result;
1476 static NTSTATUS smb_time_audit_streaminfo(vfs_handle_struct *handle,
1477 struct files_struct *fsp,
1478 const char *fname,
1479 TALLOC_CTX *mem_ctx,
1480 unsigned int *pnum_streams,
1481 struct stream_struct **pstreams)
1483 NTSTATUS result;
1484 struct timespec ts1,ts2;
1485 double timediff;
1487 clock_gettime_mono(&ts1);
1488 result = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
1489 pnum_streams, pstreams);
1490 clock_gettime_mono(&ts2);
1491 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1493 if (timediff > audit_timeout) {
1494 smb_time_audit_log_fsp("streaminfo", timediff, fsp);
1497 return result;
1500 static int smb_time_audit_get_real_filename(struct vfs_handle_struct *handle,
1501 const char *path,
1502 const char *name,
1503 TALLOC_CTX *mem_ctx,
1504 char **found_name)
1506 int result;
1507 struct timespec ts1,ts2;
1508 double timediff;
1510 clock_gettime_mono(&ts1);
1511 result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
1512 found_name);
1513 clock_gettime_mono(&ts2);
1514 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1516 if (timediff > audit_timeout) {
1517 smb_time_audit_log_fname("get_real_filename", timediff, path);
1520 return result;
1523 static const char *smb_time_audit_connectpath(vfs_handle_struct *handle,
1524 const char *fname)
1526 const char *result;
1527 struct timespec ts1,ts2;
1528 double timediff;
1530 clock_gettime_mono(&ts1);
1531 result = SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1532 clock_gettime_mono(&ts2);
1533 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1535 if (timediff > audit_timeout) {
1536 smb_time_audit_log_fname("connectpath", timediff, fname);
1539 return result;
1542 static NTSTATUS smb_time_audit_brl_lock_windows(struct vfs_handle_struct *handle,
1543 struct byte_range_lock *br_lck,
1544 struct lock_struct *plock,
1545 bool blocking_lock,
1546 struct blocking_lock_record *blr)
1548 NTSTATUS result;
1549 struct timespec ts1,ts2;
1550 double timediff;
1552 clock_gettime_mono(&ts1);
1553 result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock,
1554 blocking_lock, blr);
1555 clock_gettime_mono(&ts2);
1556 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1558 if (timediff > audit_timeout) {
1559 smb_time_audit_log_fsp("brl_lock_windows", timediff,
1560 br_lck->fsp);
1563 return result;
1566 static bool smb_time_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
1567 struct messaging_context *msg_ctx,
1568 struct byte_range_lock *br_lck,
1569 const struct lock_struct *plock)
1571 bool result;
1572 struct timespec ts1,ts2;
1573 double timediff;
1575 clock_gettime_mono(&ts1);
1576 result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, msg_ctx, br_lck,
1577 plock);
1578 clock_gettime_mono(&ts2);
1579 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1581 if (timediff > audit_timeout) {
1582 smb_time_audit_log_fsp("brl_unlock_windows", timediff,
1583 br_lck->fsp);
1586 return result;
1589 static bool smb_time_audit_brl_cancel_windows(struct vfs_handle_struct *handle,
1590 struct byte_range_lock *br_lck,
1591 struct lock_struct *plock,
1592 struct blocking_lock_record *blr)
1594 bool result;
1595 struct timespec ts1,ts2;
1596 double timediff;
1598 clock_gettime_mono(&ts1);
1599 result = SMB_VFS_NEXT_BRL_CANCEL_WINDOWS(handle, br_lck, plock, blr);
1600 clock_gettime_mono(&ts2);
1601 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1603 if (timediff > audit_timeout) {
1604 smb_time_audit_log_fsp("brl_cancel_windows", timediff,
1605 br_lck->fsp);
1608 return result;
1611 static bool smb_time_audit_strict_lock(struct vfs_handle_struct *handle,
1612 struct files_struct *fsp,
1613 struct lock_struct *plock)
1615 bool result;
1616 struct timespec ts1,ts2;
1617 double timediff;
1619 clock_gettime_mono(&ts1);
1620 result = SMB_VFS_NEXT_STRICT_LOCK(handle, fsp, plock);
1621 clock_gettime_mono(&ts2);
1622 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1624 if (timediff > audit_timeout) {
1625 smb_time_audit_log_fsp("strict_lock", timediff, fsp);
1628 return result;
1631 static void smb_time_audit_strict_unlock(struct vfs_handle_struct *handle,
1632 struct files_struct *fsp,
1633 struct lock_struct *plock)
1635 struct timespec ts1,ts2;
1636 double timediff;
1638 clock_gettime_mono(&ts1);
1639 SMB_VFS_NEXT_STRICT_UNLOCK(handle, fsp, plock);
1640 clock_gettime_mono(&ts2);
1641 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1643 if (timediff > audit_timeout) {
1644 smb_time_audit_log_fsp("strict_unlock", timediff, fsp);
1648 static NTSTATUS smb_time_audit_translate_name(struct vfs_handle_struct *handle,
1649 const char *name,
1650 enum vfs_translate_direction direction,
1651 TALLOC_CTX *mem_ctx,
1652 char **mapped_name)
1654 NTSTATUS result;
1655 struct timespec ts1,ts2;
1656 double timediff;
1658 clock_gettime_mono(&ts1);
1659 result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
1660 mapped_name);
1661 clock_gettime_mono(&ts2);
1662 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1664 if (timediff > audit_timeout) {
1665 smb_time_audit_log_fname("translate_name", timediff, name);
1668 return result;
1671 static NTSTATUS smb_time_audit_fget_nt_acl(vfs_handle_struct *handle,
1672 files_struct *fsp,
1673 uint32 security_info,
1674 TALLOC_CTX *mem_ctx,
1675 struct security_descriptor **ppdesc)
1677 NTSTATUS result;
1678 struct timespec ts1,ts2;
1679 double timediff;
1681 clock_gettime_mono(&ts1);
1682 result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1683 mem_ctx, ppdesc);
1684 clock_gettime_mono(&ts2);
1685 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1687 if (timediff > audit_timeout) {
1688 smb_time_audit_log_fsp("fget_nt_acl", timediff, fsp);
1691 return result;
1694 static NTSTATUS smb_time_audit_get_nt_acl(vfs_handle_struct *handle,
1695 const char *name,
1696 uint32 security_info,
1697 TALLOC_CTX *mem_ctx,
1698 struct security_descriptor **ppdesc)
1700 NTSTATUS result;
1701 struct timespec ts1,ts2;
1702 double timediff;
1704 clock_gettime_mono(&ts1);
1705 result = SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info,
1706 mem_ctx, ppdesc);
1707 clock_gettime_mono(&ts2);
1708 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1710 if (timediff > audit_timeout) {
1711 smb_time_audit_log_fname("get_nt_acl", timediff, name);
1714 return result;
1717 static NTSTATUS smb_time_audit_fset_nt_acl(vfs_handle_struct *handle,
1718 files_struct *fsp,
1719 uint32 security_info_sent,
1720 const struct security_descriptor *psd)
1722 NTSTATUS result;
1723 struct timespec ts1,ts2;
1724 double timediff;
1726 clock_gettime_mono(&ts1);
1727 result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent,
1728 psd);
1729 clock_gettime_mono(&ts2);
1730 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1732 if (timediff > audit_timeout) {
1733 smb_time_audit_log_fsp("fset_nt_acl", timediff, fsp);
1736 return result;
1739 static int smb_time_audit_chmod_acl(vfs_handle_struct *handle,
1740 const char *path, mode_t mode)
1742 int result;
1743 struct timespec ts1,ts2;
1744 double timediff;
1746 clock_gettime_mono(&ts1);
1747 result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
1748 clock_gettime_mono(&ts2);
1749 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1751 if (timediff > audit_timeout) {
1752 smb_time_audit_log_fname("chmod_acl", timediff, path);
1755 return result;
1758 static int smb_time_audit_fchmod_acl(vfs_handle_struct *handle,
1759 files_struct *fsp, mode_t mode)
1761 int result;
1762 struct timespec ts1,ts2;
1763 double timediff;
1765 clock_gettime_mono(&ts1);
1766 result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1767 clock_gettime_mono(&ts2);
1768 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1770 if (timediff > audit_timeout) {
1771 smb_time_audit_log_fsp("fchmod_acl", timediff, fsp);
1774 return result;
1777 static SMB_ACL_T smb_time_audit_sys_acl_get_file(vfs_handle_struct *handle,
1778 const char *path_p,
1779 SMB_ACL_TYPE_T type,
1780 TALLOC_CTX *mem_ctx)
1782 SMB_ACL_T result;
1783 struct timespec ts1,ts2;
1784 double timediff;
1786 clock_gettime_mono(&ts1);
1787 result = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p, type, mem_ctx);
1788 clock_gettime_mono(&ts2);
1789 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1791 if (timediff > audit_timeout) {
1792 smb_time_audit_log_fname("sys_acl_get_file", timediff, path_p);
1795 return result;
1798 static SMB_ACL_T smb_time_audit_sys_acl_get_fd(vfs_handle_struct *handle,
1799 files_struct *fsp,
1800 TALLOC_CTX *mem_ctx)
1802 SMB_ACL_T result;
1803 struct timespec ts1,ts2;
1804 double timediff;
1806 clock_gettime_mono(&ts1);
1807 result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1808 clock_gettime_mono(&ts2);
1809 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1811 if (timediff > audit_timeout) {
1812 smb_time_audit_log_fsp("sys_acl_get_fd", timediff, fsp);
1815 return result;
1819 static int smb_time_audit_sys_acl_blob_get_file(vfs_handle_struct *handle,
1820 const char *path_p,
1821 TALLOC_CTX *mem_ctx,
1822 char **blob_description,
1823 DATA_BLOB *blob)
1825 int result;
1826 struct timespec ts1,ts2;
1827 double timediff;
1829 clock_gettime_mono(&ts1);
1830 result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, path_p, mem_ctx, blob_description, blob);
1831 clock_gettime_mono(&ts2);
1832 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1834 if (timediff > audit_timeout) {
1835 smb_time_audit_log("sys_acl_blob_get_file", timediff);
1838 return result;
1841 static int smb_time_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1842 files_struct *fsp,
1843 TALLOC_CTX *mem_ctx,
1844 char **blob_description,
1845 DATA_BLOB *blob)
1847 int result;
1848 struct timespec ts1,ts2;
1849 double timediff;
1851 clock_gettime_mono(&ts1);
1852 result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
1853 clock_gettime_mono(&ts2);
1854 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1856 if (timediff > audit_timeout) {
1857 smb_time_audit_log("sys_acl_blob_get_fd", timediff);
1860 return result;
1863 static int smb_time_audit_sys_acl_set_file(vfs_handle_struct *handle,
1864 const char *name,
1865 SMB_ACL_TYPE_T acltype,
1866 SMB_ACL_T theacl)
1868 int result;
1869 struct timespec ts1,ts2;
1870 double timediff;
1872 clock_gettime_mono(&ts1);
1873 result = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, acltype,
1874 theacl);
1875 clock_gettime_mono(&ts2);
1876 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1878 if (timediff > audit_timeout) {
1879 smb_time_audit_log_fname("sys_acl_set_file", timediff, name);
1882 return result;
1885 static int smb_time_audit_sys_acl_set_fd(vfs_handle_struct *handle,
1886 files_struct *fsp,
1887 SMB_ACL_T theacl)
1889 int result;
1890 struct timespec ts1,ts2;
1891 double timediff;
1893 clock_gettime_mono(&ts1);
1894 result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1895 clock_gettime_mono(&ts2);
1896 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1898 if (timediff > audit_timeout) {
1899 smb_time_audit_log_fsp("sys_acl_set_fd", timediff, fsp);
1902 return result;
1905 static int smb_time_audit_sys_acl_delete_def_file(vfs_handle_struct *handle,
1906 const char *path)
1908 int result;
1909 struct timespec ts1,ts2;
1910 double timediff;
1912 clock_gettime_mono(&ts1);
1913 result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
1914 clock_gettime_mono(&ts2);
1915 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1917 if (timediff > audit_timeout) {
1918 smb_time_audit_log_fname("sys_acl_delete_def_file", timediff, path);
1921 return result;
1924 static ssize_t smb_time_audit_getxattr(struct vfs_handle_struct *handle,
1925 const char *path, const char *name,
1926 void *value, size_t size)
1928 ssize_t result;
1929 struct timespec ts1,ts2;
1930 double timediff;
1932 clock_gettime_mono(&ts1);
1933 result = SMB_VFS_NEXT_GETXATTR(handle, path, name, value, size);
1934 clock_gettime_mono(&ts2);
1935 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1937 if (timediff > audit_timeout) {
1938 smb_time_audit_log_fname("getxattr", timediff, path);
1941 return result;
1944 static ssize_t smb_time_audit_fgetxattr(struct vfs_handle_struct *handle,
1945 struct files_struct *fsp,
1946 const char *name, void *value,
1947 size_t size)
1949 ssize_t result;
1950 struct timespec ts1,ts2;
1951 double timediff;
1953 clock_gettime_mono(&ts1);
1954 result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1955 clock_gettime_mono(&ts2);
1956 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1958 if (timediff > audit_timeout) {
1959 smb_time_audit_log_fsp("fgetxattr", timediff, fsp);
1962 return result;
1965 static ssize_t smb_time_audit_listxattr(struct vfs_handle_struct *handle,
1966 const char *path, char *list,
1967 size_t size)
1969 ssize_t result;
1970 struct timespec ts1,ts2;
1971 double timediff;
1973 clock_gettime_mono(&ts1);
1974 result = SMB_VFS_NEXT_LISTXATTR(handle, path, list, size);
1975 clock_gettime_mono(&ts2);
1976 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1978 if (timediff > audit_timeout) {
1979 smb_time_audit_log_fname("listxattr", timediff, path);
1982 return result;
1985 static ssize_t smb_time_audit_flistxattr(struct vfs_handle_struct *handle,
1986 struct files_struct *fsp, char *list,
1987 size_t size)
1989 ssize_t result;
1990 struct timespec ts1,ts2;
1991 double timediff;
1993 clock_gettime_mono(&ts1);
1994 result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1995 clock_gettime_mono(&ts2);
1996 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1998 if (timediff > audit_timeout) {
1999 smb_time_audit_log_fsp("flistxattr", timediff, fsp);
2002 return result;
2005 static int smb_time_audit_removexattr(struct vfs_handle_struct *handle,
2006 const char *path, const char *name)
2008 int result;
2009 struct timespec ts1,ts2;
2010 double timediff;
2012 clock_gettime_mono(&ts1);
2013 result = SMB_VFS_NEXT_REMOVEXATTR(handle, path, name);
2014 clock_gettime_mono(&ts2);
2015 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2017 if (timediff > audit_timeout) {
2018 smb_time_audit_log_fname("removexattr", timediff, path);
2021 return result;
2024 static int smb_time_audit_fremovexattr(struct vfs_handle_struct *handle,
2025 struct files_struct *fsp,
2026 const char *name)
2028 int result;
2029 struct timespec ts1,ts2;
2030 double timediff;
2032 clock_gettime_mono(&ts1);
2033 result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2034 clock_gettime_mono(&ts2);
2035 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2037 if (timediff > audit_timeout) {
2038 smb_time_audit_log_fsp("fremovexattr", timediff, fsp);
2041 return result;
2044 static int smb_time_audit_setxattr(struct vfs_handle_struct *handle,
2045 const char *path, const char *name,
2046 const void *value, size_t size,
2047 int flags)
2049 int result;
2050 struct timespec ts1,ts2;
2051 double timediff;
2053 clock_gettime_mono(&ts1);
2054 result = SMB_VFS_NEXT_SETXATTR(handle, path, name, value, size,
2055 flags);
2056 clock_gettime_mono(&ts2);
2057 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2059 if (timediff > audit_timeout) {
2060 smb_time_audit_log_fname("setxattr", timediff, path);
2063 return result;
2066 static int smb_time_audit_fsetxattr(struct vfs_handle_struct *handle,
2067 struct files_struct *fsp, const char *name,
2068 const void *value, size_t size, int flags)
2070 int result;
2071 struct timespec ts1,ts2;
2072 double timediff;
2074 clock_gettime_mono(&ts1);
2075 result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2076 clock_gettime_mono(&ts2);
2077 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2079 if (timediff > audit_timeout) {
2080 smb_time_audit_log_fsp("fsetxattr", timediff, fsp);
2083 return result;
2086 static bool smb_time_audit_aio_force(struct vfs_handle_struct *handle,
2087 struct files_struct *fsp)
2089 bool result;
2090 struct timespec ts1,ts2;
2091 double timediff;
2093 clock_gettime_mono(&ts1);
2094 result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2095 clock_gettime_mono(&ts2);
2096 timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2098 if (timediff > audit_timeout) {
2099 smb_time_audit_log_fsp("aio_force", timediff, fsp);
2102 return result;
2107 /* VFS operations */
2109 static struct vfs_fn_pointers vfs_time_audit_fns = {
2110 .connect_fn = smb_time_audit_connect,
2111 .disconnect_fn = smb_time_audit_disconnect,
2112 .disk_free_fn = smb_time_audit_disk_free,
2113 .get_quota_fn = smb_time_audit_get_quota,
2114 .set_quota_fn = smb_time_audit_set_quota,
2115 .get_shadow_copy_data_fn = smb_time_audit_get_shadow_copy_data,
2116 .statvfs_fn = smb_time_audit_statvfs,
2117 .fs_capabilities_fn = smb_time_audit_fs_capabilities,
2118 .opendir_fn = smb_time_audit_opendir,
2119 .fdopendir_fn = smb_time_audit_fdopendir,
2120 .readdir_fn = smb_time_audit_readdir,
2121 .seekdir_fn = smb_time_audit_seekdir,
2122 .telldir_fn = smb_time_audit_telldir,
2123 .rewind_dir_fn = smb_time_audit_rewinddir,
2124 .mkdir_fn = smb_time_audit_mkdir,
2125 .rmdir_fn = smb_time_audit_rmdir,
2126 .closedir_fn = smb_time_audit_closedir,
2127 .init_search_op_fn = smb_time_audit_init_search_op,
2128 .open_fn = smb_time_audit_open,
2129 .create_file_fn = smb_time_audit_create_file,
2130 .close_fn = smb_time_audit_close,
2131 .read_fn = smb_time_audit_read,
2132 .pread_fn = smb_time_audit_pread,
2133 .pread_send_fn = smb_time_audit_pread_send,
2134 .pread_recv_fn = smb_time_audit_pread_recv,
2135 .write_fn = smb_time_audit_write,
2136 .pwrite_fn = smb_time_audit_pwrite,
2137 .pwrite_send_fn = smb_time_audit_pwrite_send,
2138 .pwrite_recv_fn = smb_time_audit_pwrite_recv,
2139 .lseek_fn = smb_time_audit_lseek,
2140 .sendfile_fn = smb_time_audit_sendfile,
2141 .recvfile_fn = smb_time_audit_recvfile,
2142 .rename_fn = smb_time_audit_rename,
2143 .fsync_fn = smb_time_audit_fsync,
2144 .fsync_send_fn = smb_time_audit_fsync_send,
2145 .fsync_recv_fn = smb_time_audit_fsync_recv,
2146 .stat_fn = smb_time_audit_stat,
2147 .fstat_fn = smb_time_audit_fstat,
2148 .lstat_fn = smb_time_audit_lstat,
2149 .get_alloc_size_fn = smb_time_audit_get_alloc_size,
2150 .unlink_fn = smb_time_audit_unlink,
2151 .chmod_fn = smb_time_audit_chmod,
2152 .fchmod_fn = smb_time_audit_fchmod,
2153 .chown_fn = smb_time_audit_chown,
2154 .fchown_fn = smb_time_audit_fchown,
2155 .lchown_fn = smb_time_audit_lchown,
2156 .chdir_fn = smb_time_audit_chdir,
2157 .getwd_fn = smb_time_audit_getwd,
2158 .ntimes_fn = smb_time_audit_ntimes,
2159 .ftruncate_fn = smb_time_audit_ftruncate,
2160 .fallocate_fn = smb_time_audit_fallocate,
2161 .lock_fn = smb_time_audit_lock,
2162 .kernel_flock_fn = smb_time_audit_kernel_flock,
2163 .linux_setlease_fn = smb_time_audit_linux_setlease,
2164 .getlock_fn = smb_time_audit_getlock,
2165 .symlink_fn = smb_time_audit_symlink,
2166 .readlink_fn = smb_time_audit_readlink,
2167 .link_fn = smb_time_audit_link,
2168 .mknod_fn = smb_time_audit_mknod,
2169 .realpath_fn = smb_time_audit_realpath,
2170 .notify_watch_fn = smb_time_audit_notify_watch,
2171 .chflags_fn = smb_time_audit_chflags,
2172 .file_id_create_fn = smb_time_audit_file_id_create,
2173 .streaminfo_fn = smb_time_audit_streaminfo,
2174 .get_real_filename_fn = smb_time_audit_get_real_filename,
2175 .connectpath_fn = smb_time_audit_connectpath,
2176 .brl_lock_windows_fn = smb_time_audit_brl_lock_windows,
2177 .brl_unlock_windows_fn = smb_time_audit_brl_unlock_windows,
2178 .brl_cancel_windows_fn = smb_time_audit_brl_cancel_windows,
2179 .strict_lock_fn = smb_time_audit_strict_lock,
2180 .strict_unlock_fn = smb_time_audit_strict_unlock,
2181 .translate_name_fn = smb_time_audit_translate_name,
2182 .fget_nt_acl_fn = smb_time_audit_fget_nt_acl,
2183 .get_nt_acl_fn = smb_time_audit_get_nt_acl,
2184 .fset_nt_acl_fn = smb_time_audit_fset_nt_acl,
2185 .chmod_acl_fn = smb_time_audit_chmod_acl,
2186 .fchmod_acl_fn = smb_time_audit_fchmod_acl,
2187 .sys_acl_get_file_fn = smb_time_audit_sys_acl_get_file,
2188 .sys_acl_get_fd_fn = smb_time_audit_sys_acl_get_fd,
2189 .sys_acl_blob_get_file_fn = smb_time_audit_sys_acl_blob_get_file,
2190 .sys_acl_blob_get_fd_fn = smb_time_audit_sys_acl_blob_get_fd,
2191 .sys_acl_set_file_fn = smb_time_audit_sys_acl_set_file,
2192 .sys_acl_set_fd_fn = smb_time_audit_sys_acl_set_fd,
2193 .sys_acl_delete_def_file_fn = smb_time_audit_sys_acl_delete_def_file,
2194 .getxattr_fn = smb_time_audit_getxattr,
2195 .fgetxattr_fn = smb_time_audit_fgetxattr,
2196 .listxattr_fn = smb_time_audit_listxattr,
2197 .flistxattr_fn = smb_time_audit_flistxattr,
2198 .removexattr_fn = smb_time_audit_removexattr,
2199 .fremovexattr_fn = smb_time_audit_fremovexattr,
2200 .setxattr_fn = smb_time_audit_setxattr,
2201 .fsetxattr_fn = smb_time_audit_fsetxattr,
2202 .aio_force_fn = smb_time_audit_aio_force,
2206 NTSTATUS vfs_time_audit_init(void);
2207 NTSTATUS vfs_time_audit_init(void)
2209 audit_timeout = (double)lp_parm_int(-1, "time_audit", "timeout",
2210 10000) / 1000.0;
2211 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "time_audit",
2212 &vfs_time_audit_fns);