Updated VFS examples to use ftruncate() and lock() functions.
[sfrench/samba-autobuild/.git] / examples / VFS / 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  *
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 2 of the License, or
10  * (at your option) any later version.
11  *  
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.
16  *  
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #ifdef HAVE_UTIME_H
26 #include <utime.h>
27 #endif
28 #ifdef HAVE_DIRENT_H
29 #include <dirent.h>
30 #endif
31 #include <syslog.h>
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif
35 #include <errno.h>
36 #include <string.h>
37 #include <vfs.h>
38
39 #ifndef SYSLOG_FACILITY
40 #define SYSLOG_FACILITY   LOG_USER
41 #endif
42
43 #ifndef SYSLOG_PRIORITY
44 #define SYSLOG_PRIORITY   LOG_NOTICE
45 #endif
46
47 /* Function prototypes */
48
49 int audit_connect(struct vfs_connection_struct *conn, char *svc, char *user);
50 void audit_disconnect(void);
51 DIR *audit_opendir(char *fname);
52 int audit_mkdir(char *path, mode_t mode);
53 int audit_rmdir(char *path);
54 int audit_open(char *fname, int flags, mode_t mode);
55 int audit_close(int fd);
56 int audit_rename(char *old, char *new);
57 int audit_unlink(char *path);
58 int audit_chmod(char *path, mode_t mode);
59
60 /* VFS operations */
61
62 extern struct vfs_ops default_vfs_ops;   /* For passthrough operation */
63
64 struct vfs_ops audit_ops = {
65     
66         /* Disk operations */
67
68         audit_connect,
69         audit_disconnect,
70         NULL,                     /* disk free */
71
72         /* Directory operations */
73
74         audit_opendir,
75         NULL,                     /* readdir */
76         audit_mkdir,
77         audit_rmdir,
78         NULL,                     /* closedir */
79
80         /* File operations */
81
82         audit_open,
83         audit_close,
84         NULL,                     /* read  */
85         NULL,                     /* write */
86         NULL,                     /* lseek */
87         audit_rename,
88         NULL,                     /* fsync */
89         NULL,                     /* stat  */
90         NULL,                     /* fstat */
91         NULL,                     /* lstat */
92         audit_unlink,
93         NULL,                     /* chmod */
94         NULL,                     /* utime */
95         NULL,                     /* ftruncate */
96         NULL                      /* lock */
97 };
98
99 /* VFS initialisation function.  Return initialised vfs_ops structure
100    back to SAMBA. */
101
102 struct vfs_ops *vfs_init(void)
103 {
104         openlog("smbd_audit", LOG_PID, SYSLOG_FACILITY);
105         return(&audit_ops);
106 }
107
108 /* Implementation of vfs_ops.  Pass everything on to the default
109    operation but log event first. */
110
111 int audit_connect(struct vfs_connection_struct *conn, char *svc, char *user)
112 {
113         syslog(SYSLOG_PRIORITY, "connect to service %s by user %s\n", 
114                svc, user);
115
116         return default_vfs_ops.connect(conn, svc, user);
117 }
118
119 void audit_disconnect(void)
120 {
121         syslog(SYSLOG_PRIORITY, "disconnected\n");
122         default_vfs_ops.disconnect();
123 }
124
125 DIR *audit_opendir(char *fname)
126 {
127         DIR *result = default_vfs_ops.opendir(fname);
128
129         syslog(SYSLOG_PRIORITY, "opendir %s %s%s\n",
130                fname,
131                (result == NULL) ? "failed: " : "",
132                (result == NULL) ? strerror(errno) : "");
133
134         return result;
135 }
136
137 int audit_mkdir(char *path, mode_t mode)
138 {
139         int result = default_vfs_ops.mkdir(path, mode);
140
141         syslog(SYSLOG_PRIORITY, "mkdir %s %s%s\n", 
142                path,
143                (result < 0) ? "failed: " : "",
144                (result < 0) ? strerror(errno) : "");
145
146         return result;
147 }
148
149 int audit_rmdir(char *path)
150 {
151         int result = default_vfs_ops.rmdir(path);
152
153         syslog(SYSLOG_PRIORITY, "rmdir %s %s%s\n", 
154                path, 
155                (result < 0) ? "failed: " : "",
156                (result < 0) ? strerror(errno) : "");
157
158         return result;
159 }
160
161 int audit_open(char *fname, int flags, mode_t mode)
162 {
163         int result = default_vfs_ops.open(fname, flags, mode);
164
165         syslog(SYSLOG_PRIORITY, "open %s (fd %d) %s%s%s\n", 
166                fname, result,
167                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
168                (result < 0) ? "failed: " : "",
169                (result < 0) ? strerror(errno) : "");
170
171         return result;
172 }
173
174 int audit_close(int fd)
175 {
176         int result = default_vfs_ops.close(fd);
177
178         syslog(SYSLOG_PRIORITY, "close fd %d %s%s\n",
179                fd,
180                (result < 0) ? "failed: " : "",
181                (result < 0) ? strerror(errno) : "");
182
183         return result;
184 }
185
186 int audit_rename(char *old, char *new)
187 {
188         int result = default_vfs_ops.rename(old, new);
189
190         syslog(SYSLOG_PRIORITY, "rename %s -> %s %s%s\n",
191                old, new,
192                (result < 0) ? "failed: " : "",
193                (result < 0) ? strerror(errno) : "");
194
195         return result;    
196 }
197
198 int audit_unlink(char *path)
199 {
200         int result = default_vfs_ops.unlink(path);
201
202         syslog(SYSLOG_PRIORITY, "unlink %s %s%s\n",
203                path,
204                (result < 0) ? "failed: " : "",
205                (result < 0) ? strerror(errno) : "");
206
207         return result;
208 }