r23779: Change from v2 or later to v3 or later.
[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  * Copyright (C) Stefan (metze) Metzmacher, 2003
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *  
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *  
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #include "includes.h"
27
28 static int vfs_extd_audit_debug_level = DBGC_VFS;
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS vfs_extd_audit_debug_level
32
33 /* Function prototypes */
34
35 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user);
36 static void audit_disconnect(vfs_handle_struct *handle);
37 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr);
38 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode);
39 static int audit_rmdir(vfs_handle_struct *handle, const char *path);
40 static int audit_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode);
41 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd);
42 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname);
43 static int audit_unlink(vfs_handle_struct *handle, const char *path);
44 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode);
45 static int audit_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode);
46 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode);
47 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode);
48
49 /* VFS operations */
50
51 static vfs_op_tuple audit_op_tuples[] = {
52     
53         /* Disk operations */
54
55         {SMB_VFS_OP(audit_connect),     SMB_VFS_OP_CONNECT,     SMB_VFS_LAYER_LOGGER},
56         {SMB_VFS_OP(audit_disconnect),  SMB_VFS_OP_DISCONNECT,  SMB_VFS_LAYER_LOGGER},
57
58         /* Directory operations */
59
60         {SMB_VFS_OP(audit_opendir),     SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_LOGGER},
61         {SMB_VFS_OP(audit_mkdir),               SMB_VFS_OP_MKDIR,       SMB_VFS_LAYER_LOGGER},
62         {SMB_VFS_OP(audit_rmdir),               SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_LOGGER},
63
64         /* File operations */
65
66         {SMB_VFS_OP(audit_open),                SMB_VFS_OP_OPEN,        SMB_VFS_LAYER_LOGGER},
67         {SMB_VFS_OP(audit_close),               SMB_VFS_OP_CLOSE,       SMB_VFS_LAYER_LOGGER},
68         {SMB_VFS_OP(audit_rename),              SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_LOGGER},
69         {SMB_VFS_OP(audit_unlink),              SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_LOGGER},
70         {SMB_VFS_OP(audit_chmod),               SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_LOGGER},
71         {SMB_VFS_OP(audit_fchmod),              SMB_VFS_OP_FCHMOD,      SMB_VFS_LAYER_LOGGER},
72         {SMB_VFS_OP(audit_chmod_acl),   SMB_VFS_OP_CHMOD_ACL,   SMB_VFS_LAYER_LOGGER},
73         {SMB_VFS_OP(audit_fchmod_acl),  SMB_VFS_OP_FCHMOD_ACL,  SMB_VFS_LAYER_LOGGER},
74         
75         /* Finish VFS operations definition */
76         
77         {SMB_VFS_OP(NULL),                      SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
78 };
79
80
81 static int audit_syslog_facility(vfs_handle_struct *handle)
82 {
83         static const struct enum_list enum_log_facilities[] = {
84                 { LOG_USER, "USER" },
85                 { LOG_LOCAL0, "LOCAL0" },
86                 { LOG_LOCAL1, "LOCAL1" },
87                 { LOG_LOCAL2, "LOCAL2" },
88                 { LOG_LOCAL3, "LOCAL3" },
89                 { LOG_LOCAL4, "LOCAL4" },
90                 { LOG_LOCAL5, "LOCAL5" },
91                 { LOG_LOCAL6, "LOCAL6" },
92                 { LOG_LOCAL7, "LOCAL7" }
93         };
94
95         int facility;
96
97         facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
98
99         return facility;
100 }
101
102
103 static int audit_syslog_priority(vfs_handle_struct *handle)
104 {
105         static const struct enum_list enum_log_priorities[] = {
106                 { LOG_EMERG, "EMERG" },
107                 { LOG_ALERT, "ALERT" },
108                 { LOG_CRIT, "CRIT" },
109                 { LOG_ERR, "ERR" },
110                 { LOG_WARNING, "WARNING" },
111                 { LOG_NOTICE, "NOTICE" },
112                 { LOG_INFO, "INFO" },
113                 { LOG_DEBUG, "DEBUG" }
114         };
115
116         int priority;
117
118         priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority", enum_log_priorities, LOG_NOTICE);
119
120         return priority;
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(vfs_handle_struct *handle, const char *svc, const char *user)
127 {
128         int result;
129
130         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
131
132         syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n", 
133                svc, user);
134         DEBUG(10, ("Connected to service %s as user %s\n",
135                svc, user));
136
137         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
138
139         return result;
140 }
141
142 static void audit_disconnect(vfs_handle_struct *handle)
143 {
144         syslog(audit_syslog_priority(handle), "disconnected\n");
145         DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
146         SMB_VFS_NEXT_DISCONNECT(handle);
147
148         return;
149 }
150
151 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
152 {
153         SMB_STRUCT_DIR *result;
154
155         result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
156
157         syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
158                fname,
159                (result == NULL) ? "failed: " : "",
160                (result == NULL) ? strerror(errno) : "");
161         DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
162                fname,
163                (result == NULL) ? "failed: " : "",
164                (result == NULL) ? strerror(errno) : ""));
165
166         return result;
167 }
168
169 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
170 {
171         int result;
172         
173         result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
174         
175         syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n", 
176                path,
177                (result < 0) ? "failed: " : "",
178                (result < 0) ? strerror(errno) : "");
179         DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
180                path,
181                (result < 0) ? "failed: " : "",
182                (result < 0) ? strerror(errno) : ""));
183
184         return result;
185 }
186
187 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
188 {
189         int result;
190         
191         result = SMB_VFS_NEXT_RMDIR(handle, path);
192
193         syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n", 
194                path, 
195                (result < 0) ? "failed: " : "",
196                (result < 0) ? strerror(errno) : "");
197         DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
198                path,
199                (result < 0) ? "failed: " : "",
200                (result < 0) ? strerror(errno) : ""));
201
202         return result;
203 }
204
205 static int audit_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
206 {
207         int result;
208         
209         result = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
210
211         syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n", 
212                fname, result,
213                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
214                (result < 0) ? "failed: " : "",
215                (result < 0) ? strerror(errno) : "");
216         DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
217                fname,
218                (result < 0) ? "failed: " : "",
219                (result < 0) ? strerror(errno) : ""));
220
221         return result;
222 }
223
224 static int audit_close(vfs_handle_struct *handle, files_struct *fsp, int fd)
225 {
226         int result;
227         
228         result = SMB_VFS_NEXT_CLOSE(handle, fsp, fd);
229
230         syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
231                fd,
232                (result < 0) ? "failed: " : "",
233                (result < 0) ? strerror(errno) : "");
234         DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
235                fd,
236                (result < 0) ? "failed: " : "",
237                (result < 0) ? strerror(errno) : ""));
238
239         return result;
240 }
241
242 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
243 {
244         int result;
245         
246         result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
247
248         syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
249                oldname, newname,
250                (result < 0) ? "failed: " : "",
251                (result < 0) ? strerror(errno) : "");
252         DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s  %s %s\n",
253                oldname, newname,
254                (result < 0) ? "failed: " : "",
255                (result < 0) ? strerror(errno) : ""));
256
257         return result;    
258 }
259
260 static int audit_unlink(vfs_handle_struct *handle, const char *path)
261 {
262         int result;
263         
264         result = SMB_VFS_NEXT_UNLINK(handle, path);
265
266         syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
267                path,
268                (result < 0) ? "failed: " : "",
269                (result < 0) ? strerror(errno) : "");
270         DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
271                path,
272                (result < 0) ? "failed: " : "",
273                (result < 0) ? strerror(errno) : ""));
274
275         return result;
276 }
277
278 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
279 {
280         int result;
281
282         result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
283
284         syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
285                path, mode,
286                (result < 0) ? "failed: " : "",
287                (result < 0) ? strerror(errno) : "");
288         DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
289                path, mode,
290                (result < 0) ? "failed: " : "",
291                (result < 0) ? strerror(errno) : ""));
292
293         return result;
294 }
295
296 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
297 {
298         int result;
299         
300         result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
301
302         syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
303                path, mode,
304                (result < 0) ? "failed: " : "",
305                (result < 0) ? strerror(errno) : "");
306         DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
307                 path, mode,
308                (result < 0) ? "failed: " : "",
309                (result < 0) ? strerror(errno) : ""));
310
311         return result;
312 }
313
314 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode)
315 {
316         int result;
317         
318         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, fd, mode);
319
320         syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
321                fsp->fsp_name, mode,
322                (result < 0) ? "failed: " : "",
323                (result < 0) ? strerror(errno) : "");
324         DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
325                fsp->fsp_name,  mode,
326                (result < 0) ? "failed: " : "",
327                (result < 0) ? strerror(errno) : ""));
328
329         return result;
330 }
331
332 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, int fd, mode_t mode)
333 {
334         int result;
335         
336         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, fd, mode);
337
338         syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
339                fsp->fsp_name, mode,
340                (result < 0) ? "failed: " : "",
341                (result < 0) ? strerror(errno) : "");
342         DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
343                fsp->fsp_name,  mode,
344                (result < 0) ? "failed: " : "",
345                (result < 0) ? strerror(errno) : ""));
346
347         return result;
348 }
349
350 NTSTATUS vfs_extd_audit_init(void);
351 NTSTATUS vfs_extd_audit_init(void)
352 {
353         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "extd_audit", audit_op_tuples);
354         
355         if (!NT_STATUS_IS_OK(ret))
356                 return ret;
357
358         vfs_extd_audit_debug_level = debug_add_class("extd_audit");
359         if (vfs_extd_audit_debug_level == -1) {
360                 vfs_extd_audit_debug_level = DBGC_VFS;
361                 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
362         } else {
363                 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));
364         }
365         
366         return ret;
367 }