6f08a8e7b4b522a7c6b799998567f9b71d7f072e
[ab/samba.git/.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, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "includes.h"
26
27 static int vfs_extd_audit_debug_level = DBGC_VFS;
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS vfs_extd_audit_debug_level
31
32 static int audit_syslog_facility(vfs_handle_struct *handle)
33 {
34         static const struct enum_list enum_log_facilities[] = {
35                 { LOG_USER, "USER" },
36                 { LOG_LOCAL0, "LOCAL0" },
37                 { LOG_LOCAL1, "LOCAL1" },
38                 { LOG_LOCAL2, "LOCAL2" },
39                 { LOG_LOCAL3, "LOCAL3" },
40                 { LOG_LOCAL4, "LOCAL4" },
41                 { LOG_LOCAL5, "LOCAL5" },
42                 { LOG_LOCAL6, "LOCAL6" },
43                 { LOG_LOCAL7, "LOCAL7" }
44         };
45
46         int facility;
47
48         facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
49
50         return facility;
51 }
52
53
54 static int audit_syslog_priority(vfs_handle_struct *handle)
55 {
56         static const struct enum_list enum_log_priorities[] = {
57                 { LOG_EMERG, "EMERG" },
58                 { LOG_ALERT, "ALERT" },
59                 { LOG_CRIT, "CRIT" },
60                 { LOG_ERR, "ERR" },
61                 { LOG_WARNING, "WARNING" },
62                 { LOG_NOTICE, "NOTICE" },
63                 { LOG_INFO, "INFO" },
64                 { LOG_DEBUG, "DEBUG" }
65         };
66
67         int priority;
68
69         priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority",
70                                 enum_log_priorities, LOG_NOTICE);
71         if (priority == -1) {
72                 priority = LOG_WARNING;
73         }
74
75         return priority;
76 }
77
78 /* Implementation of vfs_ops.  Pass everything on to the default
79    operation but log event first. */
80
81 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
82 {
83         int result;
84
85         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
86
87         if (lp_syslog() > 0) {
88                 syslog(audit_syslog_priority(handle),
89                        "connect to service %s by user %s\n",
90                        svc, user);
91         }
92         DEBUG(10, ("Connected to service %s as user %s\n",
93                svc, user));
94
95         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
96
97         return result;
98 }
99
100 static void audit_disconnect(vfs_handle_struct *handle)
101 {
102         if (lp_syslog() > 0) {
103                 syslog(audit_syslog_priority(handle), "disconnected\n");
104         }
105         DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
106         SMB_VFS_NEXT_DISCONNECT(handle);
107
108         return;
109 }
110
111 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
112 {
113         SMB_STRUCT_DIR *result;
114
115         result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
116
117         if (lp_syslog() > 0) {
118                 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
119                        fname,
120                        (result == NULL) ? "failed: " : "",
121                        (result == NULL) ? strerror(errno) : "");
122         }
123         DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
124                fname,
125                (result == NULL) ? "failed: " : "",
126                (result == NULL) ? strerror(errno) : ""));
127
128         return result;
129 }
130
131 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
132 {
133         int result;
134
135         result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
136
137         if (lp_syslog() > 0) {
138                 syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n",
139                        path,
140                        (result < 0) ? "failed: " : "",
141                        (result < 0) ? strerror(errno) : "");
142         }
143         DEBUG(0, ("vfs_extd_audit: mkdir %s %s %s\n",
144                path,
145                (result < 0) ? "failed: " : "",
146                (result < 0) ? strerror(errno) : ""));
147
148         return result;
149 }
150
151 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
152 {
153         int result;
154
155         result = SMB_VFS_NEXT_RMDIR(handle, path);
156
157         if (lp_syslog() > 0) {
158                 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
159                        path,
160                        (result < 0) ? "failed: " : "",
161                        (result < 0) ? strerror(errno) : "");
162         }
163         DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
164                path,
165                (result < 0) ? "failed: " : "",
166                (result < 0) ? strerror(errno) : ""));
167
168         return result;
169 }
170
171 static int audit_open(vfs_handle_struct *handle,
172                       struct smb_filename *smb_fname, files_struct *fsp,
173                       int flags, mode_t mode)
174 {
175         int result;
176
177         result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
178
179         if (lp_syslog() > 0) {
180                 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
181                        smb_fname_str_dbg(smb_fname), result,
182                        ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
183                        (result < 0) ? "failed: " : "",
184                        (result < 0) ? strerror(errno) : "");
185         }
186         DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
187                smb_fname_str_dbg(smb_fname),
188                (result < 0) ? "failed: " : "",
189                (result < 0) ? strerror(errno) : ""));
190
191         return result;
192 }
193
194 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
195 {
196         int result;
197
198         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
199
200         if (lp_syslog() > 0) {
201                 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
202                        fsp->fh->fd,
203                        (result < 0) ? "failed: " : "",
204                        (result < 0) ? strerror(errno) : "");
205         }
206         DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
207                fsp->fh->fd,
208                (result < 0) ? "failed: " : "",
209                (result < 0) ? strerror(errno) : ""));
210
211         return result;
212 }
213
214 static int audit_rename(vfs_handle_struct *handle,
215                         const struct smb_filename *smb_fname_src,
216                         const struct smb_filename *smb_fname_dst)
217 {
218         int result;
219
220         result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
221
222         if (lp_syslog() > 0) {
223                 syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
224                        smb_fname_str_dbg(smb_fname_src),
225                        smb_fname_str_dbg(smb_fname_dst),
226                        (result < 0) ? "failed: " : "",
227                        (result < 0) ? strerror(errno) : "");
228         }
229         DEBUG(1, ("vfs_extd_audit: rename old: %s newname: %s  %s %s\n",
230                 smb_fname_str_dbg(smb_fname_src),
231                 smb_fname_str_dbg(smb_fname_dst),
232                (result < 0) ? "failed: " : "",
233                (result < 0) ? strerror(errno) : ""));
234
235         return result;
236 }
237
238 static int audit_unlink(vfs_handle_struct *handle, const char *path)
239 {
240         int result;
241
242         result = SMB_VFS_NEXT_UNLINK(handle, path);
243
244         if (lp_syslog() > 0) {
245                 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
246                        path,
247                        (result < 0) ? "failed: " : "",
248                        (result < 0) ? strerror(errno) : "");
249         }
250         DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
251                path,
252                (result < 0) ? "failed: " : "",
253                (result < 0) ? strerror(errno) : ""));
254
255         return result;
256 }
257
258 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
259 {
260         int result;
261
262         result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
263
264         if (lp_syslog() > 0) {
265                 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
266                        path, mode,
267                        (result < 0) ? "failed: " : "",
268                        (result < 0) ? strerror(errno) : "");
269         }
270         DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
271                path, (unsigned int)mode,
272                (result < 0) ? "failed: " : "",
273                (result < 0) ? strerror(errno) : ""));
274
275         return result;
276 }
277
278 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
279 {
280         int result;
281
282         result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
283
284         if (lp_syslog() > 0) {
285                 syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
286                        path, mode,
287                        (result < 0) ? "failed: " : "",
288                        (result < 0) ? strerror(errno) : "");
289         }
290         DEBUG(1, ("vfs_extd_audit: chmod_acl %s mode 0x%x %s %s\n",
291                 path, (unsigned int)mode,
292                (result < 0) ? "failed: " : "",
293                (result < 0) ? strerror(errno) : ""));
294
295         return result;
296 }
297
298 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
299 {
300         int result;
301
302         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
303
304         if (lp_syslog() > 0) {
305                 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
306                        fsp->fsp_name, mode,
307                        (result < 0) ? "failed: " : "",
308                        (result < 0) ? strerror(errno) : "");
309         }
310         DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
311                fsp->fsp_name,  (unsigned int)mode,
312                (result < 0) ? "failed: " : "",
313                (result < 0) ? strerror(errno) : ""));
314
315         return result;
316 }
317
318 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
319 {
320         int result;
321
322         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
323
324         if (lp_syslog() > 0) {
325                 syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
326                        fsp->fsp_name, mode,
327                        (result < 0) ? "failed: " : "",
328                        (result < 0) ? strerror(errno) : "");
329         }
330         DEBUG(1, ("vfs_extd_audit: fchmod_acl %s mode 0x%x %s %s",
331                fsp->fsp_name,  (unsigned int)mode,
332                (result < 0) ? "failed: " : "",
333                (result < 0) ? strerror(errno) : ""));
334
335         return result;
336 }
337
338 /* VFS operations */
339 static vfs_op_tuple audit_op_tuples[] = {
340
341         /* Disk operations */
342
343         {SMB_VFS_OP(audit_connect),     SMB_VFS_OP_CONNECT,     SMB_VFS_LAYER_LOGGER},
344         {SMB_VFS_OP(audit_disconnect),  SMB_VFS_OP_DISCONNECT,  SMB_VFS_LAYER_LOGGER},
345
346         /* Directory operations */
347
348         {SMB_VFS_OP(audit_opendir),     SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_LOGGER},
349         {SMB_VFS_OP(audit_mkdir),               SMB_VFS_OP_MKDIR,       SMB_VFS_LAYER_LOGGER},
350         {SMB_VFS_OP(audit_rmdir),               SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_LOGGER},
351
352         /* File operations */
353
354         {SMB_VFS_OP(audit_open),                SMB_VFS_OP_OPEN,        SMB_VFS_LAYER_LOGGER},
355         {SMB_VFS_OP(audit_close),               SMB_VFS_OP_CLOSE,       SMB_VFS_LAYER_LOGGER},
356         {SMB_VFS_OP(audit_rename),              SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_LOGGER},
357         {SMB_VFS_OP(audit_unlink),              SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_LOGGER},
358         {SMB_VFS_OP(audit_chmod),               SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_LOGGER},
359         {SMB_VFS_OP(audit_fchmod),              SMB_VFS_OP_FCHMOD,      SMB_VFS_LAYER_LOGGER},
360         {SMB_VFS_OP(audit_chmod_acl),   SMB_VFS_OP_CHMOD_ACL,   SMB_VFS_LAYER_LOGGER},
361         {SMB_VFS_OP(audit_fchmod_acl),  SMB_VFS_OP_FCHMOD_ACL,  SMB_VFS_LAYER_LOGGER},
362
363         /* Finish VFS operations definition */
364
365         {SMB_VFS_OP(NULL),                      SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
366 };
367
368 NTSTATUS vfs_extd_audit_init(void)
369 {
370         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "extd_audit", audit_op_tuples);
371         
372         if (!NT_STATUS_IS_OK(ret))
373                 return ret;
374
375         vfs_extd_audit_debug_level = debug_add_class("extd_audit");
376         if (vfs_extd_audit_debug_level == -1) {
377                 vfs_extd_audit_debug_level = DBGC_VFS;
378                 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
379         } else {
380                 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));
381         }
382         
383         return ret;
384 }