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