s3: Change SMB_VFS_OPEN to take an smb_filename struct
[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
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
28
29 /* Function prototypes */
30
31 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user);
32 static void audit_disconnect(vfs_handle_struct *handle);
33 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr);
34 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode);
35 static int audit_rmdir(vfs_handle_struct *handle, const char *path);
36 static int audit_open(vfs_handle_struct *handle, struct smb_filename *smb_fname, files_struct *fsp, int flags, mode_t mode);
37 static int audit_close(vfs_handle_struct *handle, files_struct *fsp);
38 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname);
39 static int audit_unlink(vfs_handle_struct *handle, const char *path);
40 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode);
41 static int audit_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode);
42 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode);
43 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode);
44
45 /* VFS operations */
46
47 static vfs_op_tuple audit_op_tuples[] = {
48     
49         /* Disk operations */
50
51         {SMB_VFS_OP(audit_connect),     SMB_VFS_OP_CONNECT,     SMB_VFS_LAYER_LOGGER},
52         {SMB_VFS_OP(audit_disconnect),  SMB_VFS_OP_DISCONNECT,  SMB_VFS_LAYER_LOGGER},
53
54         /* Directory operations */
55
56         {SMB_VFS_OP(audit_opendir),     SMB_VFS_OP_OPENDIR,     SMB_VFS_LAYER_LOGGER},
57         {SMB_VFS_OP(audit_mkdir),               SMB_VFS_OP_MKDIR,       SMB_VFS_LAYER_LOGGER},
58         {SMB_VFS_OP(audit_rmdir),               SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_LOGGER},
59
60         /* File operations */
61
62         {SMB_VFS_OP(audit_open),                SMB_VFS_OP_OPEN,        SMB_VFS_LAYER_LOGGER},
63         {SMB_VFS_OP(audit_close),               SMB_VFS_OP_CLOSE,       SMB_VFS_LAYER_LOGGER},
64         {SMB_VFS_OP(audit_rename),              SMB_VFS_OP_RENAME,      SMB_VFS_LAYER_LOGGER},
65         {SMB_VFS_OP(audit_unlink),              SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_LOGGER},
66         {SMB_VFS_OP(audit_chmod),               SMB_VFS_OP_CHMOD,       SMB_VFS_LAYER_LOGGER},
67         {SMB_VFS_OP(audit_fchmod),              SMB_VFS_OP_FCHMOD,      SMB_VFS_LAYER_LOGGER},
68         {SMB_VFS_OP(audit_chmod_acl),   SMB_VFS_OP_CHMOD_ACL,   SMB_VFS_LAYER_LOGGER},
69         {SMB_VFS_OP(audit_fchmod_acl),  SMB_VFS_OP_FCHMOD_ACL,  SMB_VFS_LAYER_LOGGER},
70         
71         /* Finish VFS operations definition */
72         
73         {SMB_VFS_OP(NULL),                      SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
74 };
75
76
77 static int audit_syslog_facility(vfs_handle_struct *handle)
78 {
79         static const struct enum_list enum_log_facilities[] = {
80                 { LOG_USER, "USER" },
81                 { LOG_LOCAL0, "LOCAL0" },
82                 { LOG_LOCAL1, "LOCAL1" },
83                 { LOG_LOCAL2, "LOCAL2" },
84                 { LOG_LOCAL3, "LOCAL3" },
85                 { LOG_LOCAL4, "LOCAL4" },
86                 { LOG_LOCAL5, "LOCAL5" },
87                 { LOG_LOCAL6, "LOCAL6" },
88                 { LOG_LOCAL7, "LOCAL7" }
89         };
90
91         int facility;
92
93         facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
94
95         return facility;
96 }
97
98
99 static int audit_syslog_priority(vfs_handle_struct *handle)
100 {
101         static const struct enum_list enum_log_priorities[] = {
102                 { LOG_EMERG, "EMERG" },
103                 { LOG_ALERT, "ALERT" },
104                 { LOG_CRIT, "CRIT" },
105                 { LOG_ERR, "ERR" },
106                 { LOG_WARNING, "WARNING" },
107                 { LOG_NOTICE, "NOTICE" },
108                 { LOG_INFO, "INFO" },
109                 { LOG_DEBUG, "DEBUG" }
110         };
111
112         int priority;
113
114         priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority",
115                                 enum_log_priorities, LOG_NOTICE);
116         if (priority == -1) {
117                 priority = LOG_WARNING;
118         }
119
120         return priority;
121 }
122
123 /* Implementation of vfs_ops.  Pass everything on to the default
124    operation but log event first. */
125
126 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
127 {
128         int result;
129         
130         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
131
132         syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n", 
133                svc, user);
134
135         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
136
137         return result;
138 }
139
140 static void audit_disconnect(vfs_handle_struct *handle)
141 {
142         syslog(audit_syslog_priority(handle), "disconnected\n");
143         SMB_VFS_NEXT_DISCONNECT(handle);
144
145         return;
146 }
147
148 static SMB_STRUCT_DIR *audit_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
149 {
150         SMB_STRUCT_DIR *result;
151         
152         result = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
153
154         syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
155                fname,
156                (result == NULL) ? "failed: " : "",
157                (result == NULL) ? strerror(errno) : "");
158
159         return result;
160 }
161
162 static int audit_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
163 {
164         int result;
165         
166         result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
167         
168         syslog(audit_syslog_priority(handle), "mkdir %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_rmdir(vfs_handle_struct *handle, const char *path)
177 {
178         int result;
179
180         result = SMB_VFS_NEXT_RMDIR(handle, path);
181
182         syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n", 
183                path, 
184                (result < 0) ? "failed: " : "",
185                (result < 0) ? strerror(errno) : "");
186
187         return result;
188 }
189
190 static int audit_open(vfs_handle_struct *handle,
191                       struct smb_filename *smb_fname, files_struct *fsp,
192                       int flags, mode_t mode)
193 {
194         int result;
195
196         result = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
197
198         syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n", 
199                smb_fname_str_dbg(smb_fname), result,
200                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
201                (result < 0) ? "failed: " : "",
202                (result < 0) ? strerror(errno) : "");
203
204         return result;
205 }
206
207 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
208 {
209         int result;
210
211         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
212
213         syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
214                fsp->fh->fd,
215                (result < 0) ? "failed: " : "",
216                (result < 0) ? strerror(errno) : "");
217
218         return result;
219 }
220
221 static int audit_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
222 {
223         int result;
224
225         result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
226
227         syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
228                oldname, newname,
229                (result < 0) ? "failed: " : "",
230                (result < 0) ? strerror(errno) : "");
231
232         return result;    
233 }
234
235 static int audit_unlink(vfs_handle_struct *handle, const char *path)
236 {
237         int result;
238
239         result = SMB_VFS_NEXT_UNLINK(handle, path);
240
241         syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
242                path,
243                (result < 0) ? "failed: " : "",
244                (result < 0) ? strerror(errno) : "");
245
246         return result;
247 }
248
249 static int audit_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
250 {
251         int result;
252
253         result = SMB_VFS_NEXT_CHMOD(handle, path, mode);
254
255         syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
256                path, mode,
257                (result < 0) ? "failed: " : "",
258                (result < 0) ? strerror(errno) : "");
259
260         return result;
261 }
262
263 static int audit_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
264 {
265         int result;
266
267         result = SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
268
269         syslog(audit_syslog_priority(handle), "chmod_acl %s mode 0x%x %s%s\n",
270                path, mode,
271                (result < 0) ? "failed: " : "",
272                (result < 0) ? strerror(errno) : "");
273
274         return result;
275 }
276
277 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
278 {
279         int result;
280
281         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
282
283         syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
284                fsp->fsp_name, mode,
285                (result < 0) ? "failed: " : "",
286                (result < 0) ? strerror(errno) : "");
287
288         return result;
289 }
290
291 static int audit_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
292 {
293         int result;
294
295         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
296
297         syslog(audit_syslog_priority(handle), "fchmod_acl %s mode 0x%x %s%s\n",
298                fsp->fsp_name, mode,
299                (result < 0) ? "failed: " : "",
300                (result < 0) ? strerror(errno) : "");
301
302         return result;
303 }
304
305 NTSTATUS vfs_audit_init(void);
306 NTSTATUS vfs_audit_init(void)
307 {
308         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit", audit_op_tuples);
309 }