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