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