This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[samba.git] / source3 / modules / vfs_extd_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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *  
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *  
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "config.h"
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #ifdef HAVE_UTIME_H
28 #include <utime.h>
29 #endif
30 #ifdef HAVE_DIRENT_H
31 #include <dirent.h>
32 #endif
33 #include <syslog.h>
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #include <errno.h>
38 #include <string.h>
39 #include <includes.h>
40 #include <vfs.h>
41
42 #ifndef SYSLOG_FACILITY
43 #define SYSLOG_FACILITY   LOG_USER
44 #endif
45
46 #ifndef SYSLOG_PRIORITY
47 #define SYSLOG_PRIORITY   LOG_NOTICE
48 #endif
49
50 /* Function prototypes */
51
52 static int audit_connect(struct connection_struct *conn, const char *svc, const char *user);
53 static void audit_disconnect(struct connection_struct *conn);
54 static DIR *audit_opendir(struct connection_struct *conn, const char *fname);
55 static int audit_mkdir(struct connection_struct *conn, const char *path, mode_t mode);
56 static int audit_rmdir(struct connection_struct *conn, const char *path);
57 static int audit_open(struct connection_struct *conn, const char *fname, int flags, mode_t mode);
58 static int audit_close(struct files_struct *fsp, int fd);
59 static int audit_rename(struct connection_struct *conn, const char *old, const char *new);
60 static int audit_unlink(struct connection_struct *conn, const char *path);
61 static int audit_chmod(struct connection_struct *conn, const char *path, mode_t mode);
62 static int audit_chmod_acl(struct connection_struct *conn, const char *name, mode_t mode);
63 static int audit_fchmod(struct files_struct *fsp, int fd, mode_t mode);
64 static int audit_fchmod_acl(struct files_struct *fsp, int fd, mode_t mode);
65
66 /* VFS operations */
67
68 static struct vfs_ops default_vfs_ops;   /* For passthrough operation */
69 static struct smb_vfs_handle_struct *audit_handle;
70
71 static vfs_op_tuple audit_ops[] = {
72     
73         /* Disk operations */
74
75         {audit_connect,         SMB_VFS_OP_CONNECT,     SMB_VFS_LAYER_LOGGER},
76         {audit_disconnect,      SMB_VFS_OP_DISCONNECT,  SMB_VFS_LAYER_LOGGER},
77
78         /* Directory operations */
79
80         {audit_opendir,         SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_LOGGER},
81         {audit_mkdir,           SMB_VFS_OP_MKDIR,       SMB_VFS_LAYER_LOGGER},
82         {audit_rmdir,           SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_LOGGER},
83
84         /* File operations */
85
86         {audit_open,            SMB_VFS_OP_OPEN,        SMB_VFS_LAYER_LOGGER},
87         {audit_close,           SMB_VFS_OP_CLOSE,       SMB_VFS_LAYER_LOGGER},
88         {audit_rename,          SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_LOGGER},
89         {audit_unlink,          SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_LOGGER},
90         {audit_chmod,           SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_LOGGER},
91         {audit_fchmod,          SMB_VFS_OP_FCHMOD,      SMB_VFS_LAYER_LOGGER},
92         {audit_chmod_acl,       SMB_VFS_OP_CHMOD_ACL,   SMB_VFS_LAYER_LOGGER},
93         {audit_fchmod_acl,      SMB_VFS_OP_FCHMOD_ACL,  SMB_VFS_LAYER_LOGGER},
94         
95         /* Finish VFS operations definition */
96         
97         {NULL,                  SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
98 };
99
100 /* VFS initialisation function.  Return vfs_op_tuple array back to SAMBA. */
101
102 vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops, 
103                         struct smb_vfs_handle_struct *vfs_handle)
104 {
105         *vfs_version = SMB_VFS_INTERFACE_VERSION;
106         memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
107         
108         audit_handle = vfs_handle;
109
110         openlog("smbd_audit", LOG_PID, SYSLOG_FACILITY);
111         syslog(SYSLOG_PRIORITY, "VFS_INIT: vfs_ops loaded\n");
112
113         return audit_ops;
114 }
115
116 /* VFS finalization function. */
117
118 void vfs_done(connection_struct *conn)
119 {
120         syslog(SYSLOG_PRIORITY, "VFS_DONE: vfs module unloaded\n");
121 }
122
123 /* Implementation of vfs_ops.  Pass everything on to the default
124    operation but log event first. */
125
126 static int audit_connect(struct connection_struct *conn, const char *svc, const char *user)
127 {
128         syslog(SYSLOG_PRIORITY, "connect to service %s by user %s\n", 
129                svc, user);
130         DEBUG(10, ("Connected to service %s as user %s\n",
131                svc, user));
132
133         return default_vfs_ops.connect(conn, svc, user);
134 }
135
136 static void audit_disconnect(struct connection_struct *conn)
137 {
138         syslog(SYSLOG_PRIORITY, "disconnected\n");
139         DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
140
141         default_vfs_ops.disconnect(conn);
142 }
143
144 static DIR *audit_opendir(struct connection_struct *conn, const char *fname)
145 {
146         DIR *result = default_vfs_ops.opendir(conn, fname);
147
148         syslog(SYSLOG_PRIORITY, "opendir %s %s%s\n",
149                fname,
150                (result == NULL) ? "failed: " : "",
151                (result == NULL) ? strerror(errno) : "");
152         DEBUG(1, ("vfs_extd_audit: opendir %s %s %s",
153                fname,
154                (result == NULL) ? "failed: " : "",
155                (result == NULL) ? strerror(errno) : ""));
156
157         return result;
158 }
159
160 static int audit_mkdir(struct connection_struct *conn, const char *path, mode_t mode)
161 {
162         int result = default_vfs_ops.mkdir(conn, path, mode);
163
164         syslog(SYSLOG_PRIORITY, "mkdir %s %s%s\n", 
165                path,
166                (result < 0) ? "failed: " : "",
167                (result < 0) ? strerror(errno) : "");
168         DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
169                path,
170                (result < 0) ? "failed: " : "",
171                (result < 0) ? strerror(errno) : ""));
172
173         return result;
174 }
175
176 static int audit_rmdir(struct connection_struct *conn, const char *path)
177 {
178         int result = default_vfs_ops.rmdir(conn, path);
179
180         syslog(SYSLOG_PRIORITY, "rmdir %s %s%s\n", 
181                path, 
182                (result < 0) ? "failed: " : "",
183                (result < 0) ? strerror(errno) : "");
184         DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
185                path,
186                (result < 0) ? "failed: " : "",
187                (result < 0) ? strerror(errno) : ""));
188
189         return result;
190 }
191
192 static int audit_open(struct connection_struct *conn, const char *fname, int flags, mode_t mode)
193 {
194         int result = default_vfs_ops.open(conn, fname, flags, mode);
195
196         syslog(SYSLOG_PRIORITY, "open %s (fd %d) %s%s%s\n", 
197                fname, result,
198                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
199                (result < 0) ? "failed: " : "",
200                (result < 0) ? strerror(errno) : "");
201         DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
202                fname,
203                (result < 0) ? "failed: " : "",
204                (result < 0) ? strerror(errno) : ""));
205
206         return result;
207 }
208
209 static int audit_close(struct files_struct *fsp, int fd)
210 {
211         int result = default_vfs_ops.close(fsp, fd);
212
213         syslog(SYSLOG_PRIORITY, "close fd %d %s%s\n",
214                fd,
215                (result < 0) ? "failed: " : "",
216                (result < 0) ? strerror(errno) : "");
217         DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
218                fd,
219                (result < 0) ? "failed: " : "",
220                (result < 0) ? strerror(errno) : ""));
221
222         return result;
223 }
224
225 static int audit_rename(struct connection_struct *conn, const char *old, const char *new)
226 {
227         int result = default_vfs_ops.rename(conn, old, new);
228
229         syslog(SYSLOG_PRIORITY, "rename %s -> %s %s%s\n",
230                old, new,
231                (result < 0) ? "failed: " : "",
232                (result < 0) ? strerror(errno) : "");
233         DEBUG(1, ("vfs_extd_audit: rename old: %s new: %s  %s %s\n",
234                old, new,
235                (result < 0) ? "failed: " : "",
236                (result < 0) ? strerror(errno) : ""));
237
238         return result;    
239 }
240
241 static int audit_unlink(struct connection_struct *conn, const char *path)
242 {
243         int result = default_vfs_ops.unlink(conn, path);
244
245         syslog(SYSLOG_PRIORITY, "unlink %s %s%s\n",
246                path,
247                (result < 0) ? "failed: " : "",
248                (result < 0) ? strerror(errno) : "");
249         DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
250                path,
251                (result < 0) ? "failed: " : "",
252                (result < 0) ? strerror(errno) : ""));
253
254         return result;
255 }
256
257 static int audit_chmod(struct connection_struct *conn, const char *path, mode_t mode)
258 {
259         int result = default_vfs_ops.chmod(conn, path, mode);
260
261         syslog(SYSLOG_PRIORITY, "chmod %s mode 0x%x %s%s\n",
262                path, mode,
263                (result < 0) ? "failed: " : "",
264                (result < 0) ? strerror(errno) : "");
265         DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
266                path, mode,
267                (result < 0) ? "failed: " : "",
268                (result < 0) ? strerror(errno) : ""));
269
270         return result;
271 }
272
273 static int audit_chmod_acl(struct connection_struct *conn, const char *path, mode_t mode)
274 {
275         int result = default_vfs_ops.chmod_acl(conn, path, mode);
276
277         syslog(SYSLOG_PRIORITY, "chmod_acl %s mode 0x%x %s%s\n",
278                path, mode,
279                (result < 0) ? "failed: " : "",
280                (result < 0) ? strerror(errno) : "");
281         DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
282                 path, mode,
283                (result < 0) ? "failed: " : "",
284                (result < 0) ? strerror(errno) : ""));
285
286         return result;
287 }
288
289 static int audit_fchmod(struct files_struct *fsp, int fd, mode_t mode)
290 {
291         int result = default_vfs_ops.fchmod(fsp, fd, mode);
292
293         syslog(SYSLOG_PRIORITY, "fchmod %s mode 0x%x %s%s\n",
294                fsp->fsp_name, mode,
295                (result < 0) ? "failed: " : "",
296                (result < 0) ? strerror(errno) : "");
297         DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
298                fsp->fsp_name,  mode,
299                (result < 0) ? "failed: " : "",
300                (result < 0) ? strerror(errno) : ""));
301
302         return result;
303 }
304
305 static int audit_fchmod_acl(struct files_struct *fsp, int fd, mode_t mode)
306 {
307         int result = default_vfs_ops.fchmod_acl(fsp, fd, mode);
308
309         syslog(SYSLOG_PRIORITY, "fchmod_acl %s mode 0x%x %s%s\n",
310                fsp->fsp_name, mode,
311                (result < 0) ? "failed: " : "",
312                (result < 0) ? strerror(errno) : "");
313         DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
314                fsp->fsp_name,  mode,
315                (result < 0) ? "failed: " : "",
316                (result < 0) ? strerror(errno) : ""));
317
318         return result;
319 }