trying to get HEAD building again. If you want the code
[kai/samba-autobuild/.git] / source3 / 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  * Copyright (C) Stefan (metze) Metzmacher      2002
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
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_VFS
29
30 /* Function prototypes */
31
32 static int audit_connect(vfs_handle_struct *handle, connection_struct *conn, const char *svc, const char *user);
33 static void audit_disconnect(vfs_handle_struct *handle, connection_struct *conn);
34 static DIR *audit_opendir(vfs_handle_struct *handle, connection_struct *conn, const char *fname);
35 static int audit_mkdir(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode);
36 static int audit_rmdir(vfs_handle_struct *handle, connection_struct *conn, const char *path);
37 static int audit_open(vfs_handle_struct *handle, connection_struct *conn, const char *fname, int flags, mode_t mode);
38 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd);
39 static int audit_rename(vfs_handle_struct *handle, connection_struct *conn, const char *old, const char *new);
40 static int audit_unlink(vfs_handle_struct *handle, connection_struct *conn, const char *path);
41 static int audit_chmod(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode);
42 static int audit_chmod_acl(vfs_handle_struct *handle, connection_struct *conn, const char *name, mode_t mode);
43 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode);
44 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode);
45
46 /* VFS operations */
47
48 static vfs_op_tuple audit_op_tuples[] = {
49     
50         /* Disk operations */
51
52         {SMB_VFS_OP(audit_connect),     SMB_VFS_OP_CONNECT,     SMB_VFS_LAYER_LOGGER},
53         {SMB_VFS_OP(audit_disconnect),  SMB_VFS_OP_DISCONNECT,  SMB_VFS_LAYER_LOGGER},
54
55         /* Directory operations */
56
57         {SMB_VFS_OP(audit_opendir),     SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_LOGGER},
58         {SMB_VFS_OP(audit_mkdir),               SMB_VFS_OP_MKDIR,       SMB_VFS_LAYER_LOGGER},
59         {SMB_VFS_OP(audit_rmdir),               SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_LOGGER},
60
61         /* File operations */
62
63         {SMB_VFS_OP(audit_open),                SMB_VFS_OP_OPEN,        SMB_VFS_LAYER_LOGGER},
64         {SMB_VFS_OP(audit_close),               SMB_VFS_OP_CLOSE,       SMB_VFS_LAYER_LOGGER},
65         {SMB_VFS_OP(audit_rename),              SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_LOGGER},
66         {SMB_VFS_OP(audit_unlink),              SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_LOGGER},
67         {SMB_VFS_OP(audit_chmod),               SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_LOGGER},
68         {SMB_VFS_OP(audit_fchmod),              SMB_VFS_OP_FCHMOD,      SMB_VFS_LAYER_LOGGER},
69         {SMB_VFS_OP(audit_chmod_acl),   SMB_VFS_OP_CHMOD_ACL,   SMB_VFS_LAYER_LOGGER},
70         {SMB_VFS_OP(audit_fchmod_acl),  SMB_VFS_OP_FCHMOD_ACL,  SMB_VFS_LAYER_LOGGER},
71         
72         /* Finish VFS operations definition */
73         
74         {SMB_VFS_OP(NULL),                      SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
75 };
76
77
78 static int audit_syslog_facility(vfs_handle_struct *handle)
79 {
80         /* fix me: let this be configurable by:
81          *      lp_param_enum(SNUM(handle->conn),(handle->param?handle->param:"audit"),"syslog facility",
82          *              audit_enum_facility,LOG_USER); 
83          */
84         return LOG_USER;
85 }
86
87
88 static int audit_syslog_priority(vfs_handle_struct *handle)
89 {
90         /* fix me: let this be configurable by:
91          *      lp_param_enum(SNUM(handle->conn),(handle->param?handle->param:"audit"),"syslog priority",
92          *              audit_enum_priority,LOG_NOTICE); 
93          */
94         return LOG_NOTICE;
95 }
96
97 /* Implementation of vfs_ops.  Pass everything on to the default
98    operation but log event first. */
99
100 static int audit_connect(vfs_handle_struct *handle, connection_struct *conn, const char *svc, const char *user)
101 {
102         int result;
103         
104         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
105
106         syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n", 
107                svc, user);
108
109         result = SMB_VFS_NEXT_CONNECT(handle, conn, svc, user);
110
111         return result;
112 }
113
114 static void audit_disconnect(vfs_handle_struct *handle, connection_struct *conn)
115 {
116         syslog(audit_syslog_priority(handle), "disconnected\n");
117         SMB_VFS_NEXT_DISCONNECT(handle, conn);
118
119         return;
120 }
121
122 static DIR *audit_opendir(vfs_handle_struct *handle, connection_struct *conn, const char *fname)
123 {
124         DIR *result;
125         
126         result = SMB_VFS_NEXT_OPENDIR(handle, conn, fname);
127
128         syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
129                fname,
130                (result == NULL) ? "failed: " : "",
131                (result == NULL) ? strerror(errno) : "");
132
133         return result;
134 }
135
136 static int audit_mkdir(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode)
137 {
138         int result;
139         
140         result = SMB_VFS_NEXT_MKDIR(handle, conn, path, mode);
141         
142         syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n", 
143                path,
144                (result < 0) ? "failed: " : "",
145                (result < 0) ? strerror(errno) : "");
146
147         return result;
148 }
149
150 static int audit_rmdir(vfs_handle_struct *handle, connection_struct *conn, const char *path)
151 {
152         int result;
153
154         result = SMB_VFS_NEXT_RMDIR(handle, conn, path);
155
156         syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n", 
157                path, 
158                (result < 0) ? "failed: " : "",
159                (result < 0) ? strerror(errno) : "");
160
161         return result;
162 }
163
164 static int audit_open(vfs_handle_struct *handle, connection_struct *conn, const char *fname, int flags, mode_t mode)
165 {
166         int result;
167
168         result = SMB_VFS_NEXT_OPEN(handle, conn, fname, flags, mode);
169
170         syslog(audit_syslog_priority(handle), "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(vfs_handle_struct *handle, files_struct *fsp, int fd)
180 {
181         int result;
182
183         result = SMB_VFS_NEXT_CLOSE(handle, fsp, fd);
184
185         syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
186                fd,
187                (result < 0) ? "failed: " : "",
188                (result < 0) ? strerror(errno) : "");
189
190         return result;
191 }
192
193 static int audit_rename(vfs_handle_struct *handle, connection_struct *conn, const char *old, const char *new)
194 {
195         int result;
196
197         result = SMB_VFS_NEXT_RENAME(handle, conn, old, new);
198
199         syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
200                old, new,
201                (result < 0) ? "failed: " : "",
202                (result < 0) ? strerror(errno) : "");
203
204         return result;    
205 }
206
207 static int audit_unlink(vfs_handle_struct *handle, connection_struct *conn, const char *path)
208 {
209         int result;
210
211         result = SMB_VFS_NEXT_UNLINK(handle, conn, path);
212
213         syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
214                path,
215                (result < 0) ? "failed: " : "",
216                (result < 0) ? strerror(errno) : "");
217
218         return result;
219 }
220
221 static int audit_chmod(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode)
222 {
223         int result;
224
225         result = SMB_VFS_NEXT_CHMOD(handle, conn, path, mode);
226
227         syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
228                path, mode,
229                (result < 0) ? "failed: " : "",
230                (result < 0) ? strerror(errno) : "");
231
232         return result;
233 }
234
235 static int audit_chmod_acl(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode)
236 {
237         int result;
238
239         result = SMB_VFS_NEXT_CHMOD_ACL(handle, conn, path, mode);
240
241         syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
242                path, mode,
243                (result < 0) ? "failed: " : "",
244                (result < 0) ? strerror(errno) : "");
245
246         return result;
247 }
248
249 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode)
250 {
251         int result;
252
253         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, fd, mode);
254
255         syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
256                fsp->fsp_name, mode,
257                (result < 0) ? "failed: " : "",
258                (result < 0) ? strerror(errno) : "");
259
260         return result;
261 }
262
263 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode)
264 {
265         int result;
266
267         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, fd, mode);
268
269         syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
270                fsp->fsp_name, mode,
271                (result < 0) ? "failed: " : "",
272                (result < 0) ? strerror(errno) : "");
273
274         return result;
275 }
276
277 NTSTATUS vfs_audit_init(void)
278 {
279         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit", audit_op_tuples);
280 }