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