6963c3726b454cf5e8dd3f65cb7ac814019cd062
[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                 { LOG_USER, "USER" },
37                 { LOG_LOCAL0, "LOCAL0" },
38                 { LOG_LOCAL1, "LOCAL1" },
39                 { LOG_LOCAL2, "LOCAL2" },
40                 { LOG_LOCAL3, "LOCAL3" },
41                 { LOG_LOCAL4, "LOCAL4" },
42                 { LOG_LOCAL5, "LOCAL5" },
43                 { LOG_LOCAL6, "LOCAL6" },
44                 { LOG_LOCAL7, "LOCAL7" },
45                 { -1, NULL}
46         };
47
48         int facility;
49
50         facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
51
52         return facility;
53 }
54
55
56 static int audit_syslog_priority(vfs_handle_struct *handle)
57 {
58         static const struct enum_list enum_log_priorities[] = {
59                 { LOG_EMERG, "EMERG" },
60                 { LOG_ALERT, "ALERT" },
61                 { LOG_CRIT, "CRIT" },
62                 { LOG_ERR, "ERR" },
63                 { LOG_WARNING, "WARNING" },
64                 { LOG_NOTICE, "NOTICE" },
65                 { LOG_INFO, "INFO" },
66                 { LOG_DEBUG, "DEBUG" },
67                 { -1, NULL}
68         };
69
70         int priority;
71
72         priority = lp_parm_enum(SNUM(handle->conn), "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;
87
88         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
89         if (result < 0) {
90                 return result;
91         }
92
93         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
94
95         syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n", 
96                svc, user);
97
98         return 0;
99 }
100
101 static void audit_disconnect(vfs_handle_struct *handle)
102 {
103         syslog(audit_syslog_priority(handle), "disconnected\n");
104         SMB_VFS_NEXT_DISCONNECT(handle);
105
106         return;
107 }
108
109 static DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32_t attr)
110 {
111         DIR *result;
112         
113         result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
114
115         syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
116                fname,
117                (result == NULL) ? "failed: " : "",
118                (result == NULL) ? strerror(errno) : "");
119
120         return result;
121 }
122
123 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
124 {
125         int result;
126         
127         result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
128         
129         syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n", 
130                path,
131                (result < 0) ? "failed: " : "",
132                (result < 0) ? strerror(errno) : "");
133
134         return result;
135 }
136
137 static int audit_rmdir(vfs_handle_struct *handle, const char *path)
138 {
139         int result;
140
141         result = SMB_VFS_NEXT_RMDIR(handle, path);
142
143         syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n", 
144                path, 
145                (result < 0) ? "failed: " : "",
146                (result < 0) ? strerror(errno) : "");
147
148         return result;
149 }
150
151 static int audit_open(vfs_handle_struct *handle,
152                       struct smb_filename *smb_fname, files_struct *fsp,
153                       int flags, mode_t mode)
154 {
155         int result;
156
157         result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
158
159         syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n", 
160                smb_fname->base_name, result,
161                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
162                (result < 0) ? "failed: " : "",
163                (result < 0) ? strerror(errno) : "");
164
165         return result;
166 }
167
168 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
169 {
170         int result;
171
172         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
173
174         syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
175                fsp->fh->fd,
176                (result < 0) ? "failed: " : "",
177                (result < 0) ? strerror(errno) : "");
178
179         return result;
180 }
181
182 static int audit_rename(vfs_handle_struct *handle,
183                         const struct smb_filename *smb_fname_src,
184                         const struct smb_filename *smb_fname_dst)
185 {
186         int result;
187
188         result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
189
190         syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
191                smb_fname_src->base_name,
192                smb_fname_dst->base_name,
193                (result < 0) ? "failed: " : "",
194                (result < 0) ? strerror(errno) : "");
195
196         return result;    
197 }
198
199 static int audit_unlink(vfs_handle_struct *handle,
200                         const struct smb_filename *smb_fname)
201 {
202         int result;
203
204         result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
205
206         syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
207                smb_fname->base_name,
208                (result < 0) ? "failed: " : "",
209                (result < 0) ? strerror(errno) : "");
210
211         return result;
212 }
213
214 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
215 {
216         int result;
217
218         result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
219
220         syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
221                path, mode,
222                (result < 0) ? "failed: " : "",
223                (result < 0) ? strerror(errno) : "");
224
225         return result;
226 }
227
228 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
229 {
230         int result;
231
232         result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
233
234         syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
235                path, mode,
236                (result < 0) ? "failed: " : "",
237                (result < 0) ? strerror(errno) : "");
238
239         return result;
240 }
241
242 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
243 {
244         int result;
245
246         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
247
248         syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
249                fsp->fsp_name->base_name, mode,
250                (result < 0) ? "failed: " : "",
251                (result < 0) ? strerror(errno) : "");
252
253         return result;
254 }
255
256 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
257 {
258         int result;
259
260         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
261
262         syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
263                fsp->fsp_name->base_name, mode,
264                (result < 0) ? "failed: " : "",
265                (result < 0) ? strerror(errno) : "");
266
267         return result;
268 }
269
270 static struct vfs_fn_pointers vfs_audit_fns = {
271         .connect_fn = audit_connect,
272         .disconnect_fn = audit_disconnect,
273         .opendir_fn = audit_opendir,
274         .mkdir_fn = audit_mkdir,
275         .rmdir_fn = audit_rmdir,
276         .open_fn = audit_open,
277         .close_fn = audit_close,
278         .rename_fn = audit_rename,
279         .unlink_fn = audit_unlink,
280         .chmod_fn = audit_chmod,
281         .fchmod_fn = audit_fchmod,
282         .chmod_acl_fn = audit_chmod_acl,
283         .fchmod_acl_fn = audit_fchmod_acl
284 };
285
286 NTSTATUS vfs_audit_init(void)
287 {
288         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit",
289                                 &vfs_audit_fns);
290 }