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