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