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