s3: VFS: vfs_audit: Remove unlink_fn. No longer used.
[samba.git] / source3 / modules / vfs_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) Stefan (metze) Metzmacher      2002
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *  
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *  
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "system/syslog.h"
27 #include "smbd/smbd.h"
28 #include "lib/param/loadparm.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_VFS
32
33 static int audit_syslog_facility(vfs_handle_struct *handle)
34 {
35         static const struct enum_list enum_log_facilities[] = {
36 #ifdef LOG_AUTH
37                 { LOG_AUTH,             "AUTH" },
38 #endif
39 #ifdef LOG_AUTHPRIV
40                 { LOG_AUTHPRIV,         "AUTHPRIV" },
41 #endif
42 #ifdef LOG_AUDIT
43                 { LOG_AUDIT,            "AUDIT" },
44 #endif
45 #ifdef LOG_CONSOLE
46                 { LOG_CONSOLE,          "CONSOLE" },
47 #endif
48 #ifdef LOG_CRON
49                 { LOG_CRON,             "CRON" },
50 #endif
51 #ifdef LOG_DAEMON
52                 { LOG_DAEMON,           "DAEMON" },
53 #endif
54 #ifdef LOG_FTP
55                 { LOG_FTP,              "FTP" },
56 #endif
57 #ifdef LOG_INSTALL
58                 { LOG_INSTALL,          "INSTALL" },
59 #endif
60 #ifdef LOG_KERN
61                 { LOG_KERN,             "KERN" },
62 #endif
63 #ifdef LOG_LAUNCHD
64                 { LOG_LAUNCHD,          "LAUNCHD" },
65 #endif
66 #ifdef LOG_LFMT
67                 { LOG_LFMT,             "LFMT" },
68 #endif
69 #ifdef LOG_LPR
70                 { LOG_LPR,              "LPR" },
71 #endif
72 #ifdef LOG_MAIL
73                 { LOG_MAIL,             "MAIL" },
74 #endif
75 #ifdef LOG_MEGASAFE
76                 { LOG_MEGASAFE,         "MEGASAFE" },
77 #endif
78 #ifdef LOG_NETINFO
79                 { LOG_NETINFO,          "NETINFO" },
80 #endif
81 #ifdef LOG_NEWS
82                 { LOG_NEWS,             "NEWS" },
83 #endif
84 #ifdef LOG_NFACILITIES
85                 { LOG_NFACILITIES,      "NFACILITIES" },
86 #endif
87 #ifdef LOG_NTP
88                 { LOG_NTP,              "NTP" },
89 #endif
90 #ifdef LOG_RAS
91                 { LOG_RAS,              "RAS" },
92 #endif
93 #ifdef LOG_REMOTEAUTH
94                 { LOG_REMOTEAUTH,       "REMOTEAUTH" },
95 #endif
96 #ifdef LOG_SECURITY
97                 { LOG_SECURITY,         "SECURITY" },
98 #endif
99 #ifdef LOG_SYSLOG
100                 { LOG_SYSLOG,           "SYSLOG" },
101 #endif
102 #ifdef LOG_USER
103                 { LOG_USER,             "USER" },
104 #endif
105 #ifdef LOG_UUCP
106                 { LOG_UUCP,             "UUCP" },
107 #endif
108                 { LOG_LOCAL0,           "LOCAL0" },
109                 { LOG_LOCAL1,           "LOCAL1" },
110                 { LOG_LOCAL2,           "LOCAL2" },
111                 { LOG_LOCAL3,           "LOCAL3" },
112                 { LOG_LOCAL4,           "LOCAL4" },
113                 { LOG_LOCAL5,           "LOCAL5" },
114                 { LOG_LOCAL6,           "LOCAL6" },
115                 { LOG_LOCAL7,           "LOCAL7" },
116                 { -1,                   NULL }
117         };
118
119         int facility;
120
121         facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
122
123         return facility;
124 }
125
126
127 static int audit_syslog_priority(vfs_handle_struct *handle)
128 {
129         static const struct enum_list enum_log_priorities[] = {
130                 { LOG_EMERG, "EMERG" },
131                 { LOG_ALERT, "ALERT" },
132                 { LOG_CRIT, "CRIT" },
133                 { LOG_ERR, "ERR" },
134                 { LOG_WARNING, "WARNING" },
135                 { LOG_NOTICE, "NOTICE" },
136                 { LOG_INFO, "INFO" },
137                 { LOG_DEBUG, "DEBUG" },
138                 { -1, NULL }
139         };
140
141         int priority;
142
143         priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority",
144                                 enum_log_priorities, LOG_NOTICE);
145         if (priority == -1) {
146                 priority = LOG_WARNING;
147         }
148
149         return priority;
150 }
151
152 /* Implementation of vfs_ops.  Pass everything on to the default
153    operation but log event first. */
154
155 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
156 {
157         int result;
158
159         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
160         if (result < 0) {
161                 return result;
162         }
163
164         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
165
166         syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n", 
167                svc, user);
168
169         return 0;
170 }
171
172 static void audit_disconnect(vfs_handle_struct *handle)
173 {
174         syslog(audit_syslog_priority(handle), "disconnected\n");
175         SMB_VFS_NEXT_DISCONNECT(handle);
176
177         return;
178 }
179
180 static DIR *audit_opendir(vfs_handle_struct *handle,
181                         const struct smb_filename *smb_fname,
182                         const char *mask,
183                         uint32_t attr)
184 {
185         DIR *result;
186         
187         result = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
188
189         syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
190                smb_fname->base_name,
191                (result == NULL) ? "failed: " : "",
192                (result == NULL) ? strerror(errno) : "");
193
194         return result;
195 }
196
197 static int audit_mkdirat(vfs_handle_struct *handle,
198                 struct files_struct *dirfsp,
199                 const struct smb_filename *smb_fname,
200                 mode_t mode)
201 {
202         int result;
203
204         result = SMB_VFS_NEXT_MKDIRAT(handle,
205                         dirfsp,
206                         smb_fname,
207                         mode);
208
209         syslog(audit_syslog_priority(handle), "mkdirat %s %s%s\n",
210                smb_fname->base_name,
211                (result < 0) ? "failed: " : "",
212                (result < 0) ? strerror(errno) : "");
213
214         return result;
215 }
216
217 static int audit_rmdir(vfs_handle_struct *handle,
218                 const struct smb_filename *smb_fname)
219 {
220         int result;
221
222         result = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
223
224         syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n", 
225                smb_fname->base_name,
226                (result < 0) ? "failed: " : "",
227                (result < 0) ? strerror(errno) : "");
228
229         return result;
230 }
231
232 static int audit_open(vfs_handle_struct *handle,
233                       struct smb_filename *smb_fname, files_struct *fsp,
234                       int flags, mode_t mode)
235 {
236         int result;
237
238         result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
239
240         syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n", 
241                smb_fname->base_name, result,
242                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
243                (result < 0) ? "failed: " : "",
244                (result < 0) ? strerror(errno) : "");
245
246         return result;
247 }
248
249 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
250 {
251         int result;
252
253         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
254
255         syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
256                fsp->fh->fd,
257                (result < 0) ? "failed: " : "",
258                (result < 0) ? strerror(errno) : "");
259
260         return result;
261 }
262
263 static int audit_renameat(vfs_handle_struct *handle,
264                         files_struct *srcfsp,
265                         const struct smb_filename *smb_fname_src,
266                         files_struct *dstfsp,
267                         const struct smb_filename *smb_fname_dst)
268 {
269         int result;
270
271         result = SMB_VFS_NEXT_RENAMEAT(handle,
272                         srcfsp,
273                         smb_fname_src,
274                         dstfsp,
275                         smb_fname_dst);
276
277         syslog(audit_syslog_priority(handle), "renameat %s -> %s %s%s\n",
278                smb_fname_src->base_name,
279                smb_fname_dst->base_name,
280                (result < 0) ? "failed: " : "",
281                (result < 0) ? strerror(errno) : "");
282
283         return result;
284 }
285
286 static int audit_unlinkat(vfs_handle_struct *handle,
287                         struct files_struct *dirfsp,
288                         const struct smb_filename *smb_fname,
289                         int flags)
290 {
291         int result;
292
293         result = SMB_VFS_NEXT_UNLINKAT(handle,
294                         dirfsp,
295                         smb_fname,
296                         flags);
297
298         syslog(audit_syslog_priority(handle), "unlinkat %s %s%s\n",
299                smb_fname->base_name,
300                (result < 0) ? "failed: " : "",
301                (result < 0) ? strerror(errno) : "");
302
303         return result;
304 }
305
306 static int audit_chmod(vfs_handle_struct *handle,
307                         const struct smb_filename *smb_fname,
308                         mode_t mode)
309 {
310         int result;
311
312         result = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
313
314         syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
315                smb_fname->base_name, mode,
316                (result < 0) ? "failed: " : "",
317                (result < 0) ? strerror(errno) : "");
318
319         return result;
320 }
321
322 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
323 {
324         int result;
325
326         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
327
328         syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
329                fsp->fsp_name->base_name, mode,
330                (result < 0) ? "failed: " : "",
331                (result < 0) ? strerror(errno) : "");
332
333         return result;
334 }
335
336 static struct vfs_fn_pointers vfs_audit_fns = {
337         .connect_fn = audit_connect,
338         .disconnect_fn = audit_disconnect,
339         .opendir_fn = audit_opendir,
340         .mkdirat_fn = audit_mkdirat,
341         .rmdir_fn = audit_rmdir,
342         .open_fn = audit_open,
343         .close_fn = audit_close,
344         .renameat_fn = audit_renameat,
345         .unlinkat_fn = audit_unlinkat,
346         .chmod_fn = audit_chmod,
347         .fchmod_fn = audit_fchmod,
348 };
349
350 static_decl_vfs;
351 NTSTATUS vfs_audit_init(TALLOC_CTX *ctx)
352 {
353         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit",
354                                 &vfs_audit_fns);
355 }