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