s3: VFS: vfs_extd_audit. Implement unlinkat().
[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/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 #ifdef LOG_AUTH
40                 { LOG_AUTH,             "AUTH" },
41 #endif
42 #ifdef LOG_AUTHPRIV
43                 { LOG_AUTHPRIV,         "AUTHPRIV" },
44 #endif
45 #ifdef LOG_AUDIT
46                 { LOG_AUDIT,            "AUDIT" },
47 #endif
48 #ifdef LOG_CONSOLE
49                 { LOG_CONSOLE,          "CONSOLE" },
50 #endif
51 #ifdef LOG_CRON
52                 { LOG_CRON,             "CRON" },
53 #endif
54 #ifdef LOG_DAEMON
55                 { LOG_DAEMON,           "DAEMON" },
56 #endif
57 #ifdef LOG_FTP
58                 { LOG_FTP,              "FTP" },
59 #endif
60 #ifdef LOG_INSTALL
61                 { LOG_INSTALL,          "INSTALL" },
62 #endif
63 #ifdef LOG_KERN
64                 { LOG_KERN,             "KERN" },
65 #endif
66 #ifdef LOG_LAUNCHD
67                 { LOG_LAUNCHD,          "LAUNCHD" },
68 #endif
69 #ifdef LOG_LFMT
70                 { LOG_LFMT,             "LFMT" },
71 #endif
72 #ifdef LOG_LPR
73                 { LOG_LPR,              "LPR" },
74 #endif
75 #ifdef LOG_MAIL
76                 { LOG_MAIL,             "MAIL" },
77 #endif
78 #ifdef LOG_MEGASAFE
79                 { LOG_MEGASAFE,         "MEGASAFE" },
80 #endif
81 #ifdef LOG_NETINFO
82                 { LOG_NETINFO,          "NETINFO" },
83 #endif
84 #ifdef LOG_NEWS
85                 { LOG_NEWS,             "NEWS" },
86 #endif
87 #ifdef LOG_NFACILITIES
88                 { LOG_NFACILITIES,      "NFACILITIES" },
89 #endif
90 #ifdef LOG_NTP
91                 { LOG_NTP,              "NTP" },
92 #endif
93 #ifdef LOG_RAS
94                 { LOG_RAS,              "RAS" },
95 #endif
96 #ifdef LOG_REMOTEAUTH
97                 { LOG_REMOTEAUTH,       "REMOTEAUTH" },
98 #endif
99 #ifdef LOG_SECURITY
100                 { LOG_SECURITY,         "SECURITY" },
101 #endif
102 #ifdef LOG_SYSLOG
103                 { LOG_SYSLOG,           "SYSLOG" },
104 #endif
105 #ifdef LOG_USER
106                 { LOG_USER,             "USER" },
107 #endif
108 #ifdef LOG_UUCP
109                 { LOG_UUCP,             "UUCP" },
110 #endif
111                 { LOG_LOCAL0,           "LOCAL0" },
112                 { LOG_LOCAL1,           "LOCAL1" },
113                 { LOG_LOCAL2,           "LOCAL2" },
114                 { LOG_LOCAL3,           "LOCAL3" },
115                 { LOG_LOCAL4,           "LOCAL4" },
116                 { LOG_LOCAL5,           "LOCAL5" },
117                 { LOG_LOCAL6,           "LOCAL6" },
118                 { LOG_LOCAL7,           "LOCAL7" },
119                 { -1,                   NULL }
120         };
121
122         int facility;
123
124         facility = lp_parm_enum(SNUM(handle->conn), "extd_audit", "facility", enum_log_facilities, LOG_USER);
125
126         return facility;
127 }
128
129
130 static int audit_syslog_priority(vfs_handle_struct *handle)
131 {
132         static const struct enum_list enum_log_priorities[] = {
133                 { LOG_EMERG, "EMERG" },
134                 { LOG_ALERT, "ALERT" },
135                 { LOG_CRIT, "CRIT" },
136                 { LOG_ERR, "ERR" },
137                 { LOG_WARNING, "WARNING" },
138                 { LOG_NOTICE, "NOTICE" },
139                 { LOG_INFO, "INFO" },
140                 { LOG_DEBUG, "DEBUG" },
141                 { -1, NULL }
142         };
143
144         int priority;
145
146         priority = lp_parm_enum(SNUM(handle->conn), "extd_audit", "priority",
147                                 enum_log_priorities, LOG_NOTICE);
148         if (priority == -1) {
149                 priority = LOG_WARNING;
150         }
151
152         return priority;
153 }
154
155 /* Implementation of vfs_ops.  Pass everything on to the default
156    operation but log event first. */
157
158 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
159 {
160         int result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
161
162         if (result < 0) {
163                 return result;
164         }
165
166         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
167
168         if (lp_syslog() > 0) {
169                 syslog(audit_syslog_priority(handle),
170                        "connect to service %s by user %s\n",
171                        svc, user);
172         }
173         DEBUG(10, ("Connected to service %s as user %s\n",
174                svc, user));
175
176         return 0;
177 }
178
179 static void audit_disconnect(vfs_handle_struct *handle)
180 {
181         if (lp_syslog() > 0) {
182                 syslog(audit_syslog_priority(handle), "disconnected\n");
183         }
184         DEBUG(10, ("Disconnected from VFS module extd_audit\n"));
185         SMB_VFS_NEXT_DISCONNECT(handle);
186
187         return;
188 }
189
190 static DIR *audit_opendir(vfs_handle_struct *handle,
191                         const struct smb_filename *smb_fname,
192                         const char *mask,
193                         uint32_t attr)
194 {
195         DIR *result;
196
197         result = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
198
199         if (lp_syslog() > 0) {
200                 syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
201                        smb_fname->base_name,
202                        (result == NULL) ? "failed: " : "",
203                        (result == NULL) ? strerror(errno) : "");
204         }
205         DEBUG(1, ("vfs_extd_audit: opendir %s %s %s\n",
206                smb_fname->base_name,
207                (result == NULL) ? "failed: " : "",
208                (result == NULL) ? strerror(errno) : ""));
209
210         return result;
211 }
212
213 static int audit_mkdirat(vfs_handle_struct *handle,
214                         struct files_struct *dirfsp,
215                         const struct smb_filename *smb_fname,
216                         mode_t mode)
217 {
218         int result;
219
220         result = SMB_VFS_NEXT_MKDIRAT(handle,
221                         dirfsp,
222                         smb_fname,
223                         mode);
224
225         if (lp_syslog() > 0) {
226                 syslog(audit_syslog_priority(handle), "mkdirat %s %s%s\n",
227                        smb_fname->base_name,
228                        (result < 0) ? "failed: " : "",
229                        (result < 0) ? strerror(errno) : "");
230         }
231         DEBUG(0, ("vfs_extd_audit: mkdirat %s %s %s\n",
232                smb_fname->base_name,
233                (result < 0) ? "failed: " : "",
234                (result < 0) ? strerror(errno) : ""));
235
236         return result;
237 }
238
239 static int audit_rmdir(vfs_handle_struct *handle,
240                         const struct smb_filename *smb_fname)
241 {
242         int result;
243
244         result = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
245
246         if (lp_syslog() > 0) {
247                 syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n",
248                        smb_fname->base_name,
249                        (result < 0) ? "failed: " : "",
250                        (result < 0) ? strerror(errno) : "");
251         }
252         DEBUG(0, ("vfs_extd_audit: rmdir %s %s %s\n",
253                smb_fname->base_name,
254                (result < 0) ? "failed: " : "",
255                (result < 0) ? strerror(errno) : ""));
256
257         return result;
258 }
259
260 static int audit_open(vfs_handle_struct *handle,
261                       struct smb_filename *smb_fname, files_struct *fsp,
262                       int flags, mode_t mode)
263 {
264         int result;
265
266         result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
267
268         if (lp_syslog() > 0) {
269                 syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n",
270                        smb_fname->base_name, result,
271                        ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
272                        (result < 0) ? "failed: " : "",
273                        (result < 0) ? strerror(errno) : "");
274         }
275         DEBUG(2, ("vfs_extd_audit: open %s %s %s\n",
276                smb_fname_str_dbg(smb_fname),
277                (result < 0) ? "failed: " : "",
278                (result < 0) ? strerror(errno) : ""));
279
280         return result;
281 }
282
283 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
284 {
285         int result;
286
287         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
288
289         if (lp_syslog() > 0) {
290                 syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
291                        fsp->fh->fd,
292                        (result < 0) ? "failed: " : "",
293                        (result < 0) ? strerror(errno) : "");
294         }
295         DEBUG(2, ("vfs_extd_audit: close fd %d %s %s\n",
296                fsp->fh->fd,
297                (result < 0) ? "failed: " : "",
298                (result < 0) ? strerror(errno) : ""));
299
300         return result;
301 }
302
303 static int audit_renameat(vfs_handle_struct *handle,
304                         files_struct *srcfsp,
305                         const struct smb_filename *smb_fname_src,
306                         files_struct *dstfsp,
307                         const struct smb_filename *smb_fname_dst)
308 {
309         int result;
310
311         result = SMB_VFS_NEXT_RENAMEAT(handle,
312                         srcfsp,
313                         smb_fname_src,
314                         dstfsp,
315                         smb_fname_dst);
316
317         if (lp_syslog() > 0) {
318                 syslog(audit_syslog_priority(handle), "renameat %s -> %s %s%s\n",
319                        smb_fname_src->base_name,
320                        smb_fname_dst->base_name,
321                        (result < 0) ? "failed: " : "",
322                        (result < 0) ? strerror(errno) : "");
323         }
324         DEBUG(1, ("vfs_extd_audit: renameat old: %s newname: %s  %s %s\n",
325                 smb_fname_str_dbg(smb_fname_src),
326                 smb_fname_str_dbg(smb_fname_dst),
327                (result < 0) ? "failed: " : "",
328                (result < 0) ? strerror(errno) : ""));
329
330         return result;
331 }
332
333 static int audit_unlink(vfs_handle_struct *handle,
334                         const struct smb_filename *smb_fname)
335 {
336         int result;
337
338         result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
339
340         if (lp_syslog() > 0) {
341                 syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
342                        smb_fname->base_name,
343                        (result < 0) ? "failed: " : "",
344                        (result < 0) ? strerror(errno) : "");
345         }
346         DEBUG(0, ("vfs_extd_audit: unlink %s %s %s\n",
347                smb_fname_str_dbg(smb_fname),
348                (result < 0) ? "failed: " : "",
349                (result < 0) ? strerror(errno) : ""));
350
351         return result;
352 }
353
354 static int audit_unlinkat(vfs_handle_struct *handle,
355                         struct files_struct *dirfsp,
356                         const struct smb_filename *smb_fname,
357                         int flags)
358 {
359         int result;
360
361         result = SMB_VFS_NEXT_UNLINKAT(handle,
362                         dirfsp,
363                         smb_fname,
364                         flags);
365
366         if (lp_syslog() > 0) {
367                 syslog(audit_syslog_priority(handle), "unlinkat %s %s%s\n",
368                        smb_fname->base_name,
369                        (result < 0) ? "failed: " : "",
370                        (result < 0) ? strerror(errno) : "");
371         }
372         DBG_ERR("unlinkat %s %s %s\n",
373                smb_fname_str_dbg(smb_fname),
374                (result < 0) ? "failed: " : "",
375                (result < 0) ? strerror(errno) : "");
376
377         return result;
378 }
379
380 static int audit_chmod(vfs_handle_struct *handle,
381                         const struct smb_filename *smb_fname,
382                         mode_t mode)
383 {
384         int result;
385
386         result = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
387
388         if (lp_syslog() > 0) {
389                 syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
390                        smb_fname->base_name, mode,
391                        (result < 0) ? "failed: " : "",
392                        (result < 0) ? strerror(errno) : "");
393         }
394         DEBUG(1, ("vfs_extd_audit: chmod %s mode 0x%x %s %s\n",
395                smb_fname->base_name, (unsigned int)mode,
396                (result < 0) ? "failed: " : "",
397                (result < 0) ? strerror(errno) : ""));
398
399         return result;
400 }
401
402 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
403 {
404         int result;
405
406         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
407
408         if (lp_syslog() > 0) {
409                 syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
410                        fsp->fsp_name->base_name, mode,
411                        (result < 0) ? "failed: " : "",
412                        (result < 0) ? strerror(errno) : "");
413         }
414         DEBUG(1, ("vfs_extd_audit: fchmod %s mode 0x%x %s %s",
415                fsp_str_dbg(fsp), (unsigned int)mode,
416                (result < 0) ? "failed: " : "",
417                (result < 0) ? strerror(errno) : ""));
418
419         return result;
420 }
421
422 static struct vfs_fn_pointers vfs_extd_audit_fns = {
423         .connect_fn = audit_connect,
424         .disconnect_fn = audit_disconnect,
425         .opendir_fn = audit_opendir,
426         .mkdirat_fn = audit_mkdirat,
427         .rmdir_fn = audit_rmdir,
428         .open_fn = audit_open,
429         .close_fn = audit_close,
430         .renameat_fn = audit_renameat,
431         .unlink_fn = audit_unlink,
432         .unlinkat_fn = audit_unlinkat,
433         .chmod_fn = audit_chmod,
434         .fchmod_fn = audit_fchmod,
435 };
436
437 static_decl_vfs;
438 NTSTATUS vfs_extd_audit_init(TALLOC_CTX *ctx)
439 {
440         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
441                                         "extd_audit", &vfs_extd_audit_fns);
442         
443         if (!NT_STATUS_IS_OK(ret))
444                 return ret;
445
446         vfs_extd_audit_debug_level = debug_add_class("extd_audit");
447         if (vfs_extd_audit_debug_level == -1) {
448                 vfs_extd_audit_debug_level = DBGC_VFS;
449                 DEBUG(0, ("vfs_extd_audit: Couldn't register custom debugging class!\n"));
450         } else {
451                 DEBUG(10, ("vfs_extd_audit: Debug class number of 'extd_audit': %d\n", vfs_extd_audit_debug_level));
452         }
453         
454         return ret;
455 }