Make sure that vfs*audit modules recognize and accept all the syslog facilities.
[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_AUTH,     "AUTH" },
37                 { LOG_CRON,     "CRON" },
38                 { LOG_DAEMON,   "DAEMON" },
39                 { LOG_FTP,      "FTP" },
40                 { LOG_KERN,     "KERN" },
41                 { LOG_LPR,      "LPR" },
42                 { LOG_MAIL,     "MAIL" },
43                 { LOG_NEWS,     "NEWS" },
44 #ifdef LOG_NTP
45                 { LOG_NTP,      "NTP" },
46 #endif
47 #ifdef LOG_SECURITY
48                 { LOG_SECURITY, "SECURITY" },
49 #endif
50                 { LOG_SYSLOG,   "SYSLOG" },
51                 { LOG_USER,     "USER" },
52                 { LOG_UUCP,     "UUCP" },
53                 { LOG_LOCAL0,   "LOCAL0" },
54                 { LOG_LOCAL1,   "LOCAL1" },
55                 { LOG_LOCAL2,   "LOCAL2" },
56                 { LOG_LOCAL3,   "LOCAL3" },
57                 { LOG_LOCAL4,   "LOCAL4" },
58                 { LOG_LOCAL5,   "LOCAL5" },
59                 { LOG_LOCAL6,   "LOCAL6" },
60                 { LOG_LOCAL7,   "LOCAL7" },
61                 { -1,           NULL }
62         };
63
64         int facility;
65
66         facility = lp_parm_enum(SNUM(handle->conn), "audit", "facility", enum_log_facilities, LOG_USER);
67
68         return facility;
69 }
70
71
72 static int audit_syslog_priority(vfs_handle_struct *handle)
73 {
74         static const struct enum_list enum_log_priorities[] = {
75                 { LOG_EMERG, "EMERG" },
76                 { LOG_ALERT, "ALERT" },
77                 { LOG_CRIT, "CRIT" },
78                 { LOG_ERR, "ERR" },
79                 { LOG_WARNING, "WARNING" },
80                 { LOG_NOTICE, "NOTICE" },
81                 { LOG_INFO, "INFO" },
82                 { LOG_DEBUG, "DEBUG" },
83                 { -1, NULL }
84         };
85
86         int priority;
87
88         priority = lp_parm_enum(SNUM(handle->conn), "audit", "priority",
89                                 enum_log_priorities, LOG_NOTICE);
90         if (priority == -1) {
91                 priority = LOG_WARNING;
92         }
93
94         return priority;
95 }
96
97 /* Implementation of vfs_ops.  Pass everything on to the default
98    operation but log event first. */
99
100 static int audit_connect(vfs_handle_struct *handle, const char *svc, const char *user)
101 {
102         int result;
103
104         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
105         if (result < 0) {
106                 return result;
107         }
108
109         openlog("smbd_audit", LOG_PID, audit_syslog_facility(handle));
110
111         syslog(audit_syslog_priority(handle), "connect to service %s by user %s\n", 
112                svc, user);
113
114         return 0;
115 }
116
117 static void audit_disconnect(vfs_handle_struct *handle)
118 {
119         syslog(audit_syslog_priority(handle), "disconnected\n");
120         SMB_VFS_NEXT_DISCONNECT(handle);
121
122         return;
123 }
124
125 static DIR *audit_opendir(vfs_handle_struct *handle,
126                         const struct smb_filename *smb_fname,
127                         const char *mask,
128                         uint32_t attr)
129 {
130         DIR *result;
131         
132         result = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
133
134         syslog(audit_syslog_priority(handle), "opendir %s %s%s\n",
135                smb_fname->base_name,
136                (result == NULL) ? "failed: " : "",
137                (result == NULL) ? strerror(errno) : "");
138
139         return result;
140 }
141
142 static int audit_mkdir(vfs_handle_struct *handle,
143                 const struct smb_filename *smb_fname,
144                 mode_t mode)
145 {
146         int result;
147         
148         result = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
149         
150         syslog(audit_syslog_priority(handle), "mkdir %s %s%s\n", 
151                smb_fname->base_name,
152                (result < 0) ? "failed: " : "",
153                (result < 0) ? strerror(errno) : "");
154
155         return result;
156 }
157
158 static int audit_rmdir(vfs_handle_struct *handle,
159                 const struct smb_filename *smb_fname)
160 {
161         int result;
162
163         result = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
164
165         syslog(audit_syslog_priority(handle), "rmdir %s %s%s\n", 
166                smb_fname->base_name,
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         syslog(audit_syslog_priority(handle), "open %s (fd %d) %s%s%s\n", 
182                smb_fname->base_name, result,
183                ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "", 
184                (result < 0) ? "failed: " : "",
185                (result < 0) ? strerror(errno) : "");
186
187         return result;
188 }
189
190 static int audit_close(vfs_handle_struct *handle, files_struct *fsp)
191 {
192         int result;
193
194         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
195
196         syslog(audit_syslog_priority(handle), "close fd %d %s%s\n",
197                fsp->fh->fd,
198                (result < 0) ? "failed: " : "",
199                (result < 0) ? strerror(errno) : "");
200
201         return result;
202 }
203
204 static int audit_rename(vfs_handle_struct *handle,
205                         const struct smb_filename *smb_fname_src,
206                         const struct smb_filename *smb_fname_dst)
207 {
208         int result;
209
210         result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
211
212         syslog(audit_syslog_priority(handle), "rename %s -> %s %s%s\n",
213                smb_fname_src->base_name,
214                smb_fname_dst->base_name,
215                (result < 0) ? "failed: " : "",
216                (result < 0) ? strerror(errno) : "");
217
218         return result;    
219 }
220
221 static int audit_unlink(vfs_handle_struct *handle,
222                         const struct smb_filename *smb_fname)
223 {
224         int result;
225
226         result = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
227
228         syslog(audit_syslog_priority(handle), "unlink %s %s%s\n",
229                smb_fname->base_name,
230                (result < 0) ? "failed: " : "",
231                (result < 0) ? strerror(errno) : "");
232
233         return result;
234 }
235
236 static int audit_chmod(vfs_handle_struct *handle,
237                         const struct smb_filename *smb_fname,
238                         mode_t mode)
239 {
240         int result;
241
242         result = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
243
244         syslog(audit_syslog_priority(handle), "chmod %s mode 0x%x %s%s\n",
245                smb_fname->base_name, mode,
246                (result < 0) ? "failed: " : "",
247                (result < 0) ? strerror(errno) : "");
248
249         return result;
250 }
251
252 static int audit_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
253 {
254         int result;
255
256         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
257
258         syslog(audit_syslog_priority(handle), "fchmod %s mode 0x%x %s%s\n",
259                fsp->fsp_name->base_name, mode,
260                (result < 0) ? "failed: " : "",
261                (result < 0) ? strerror(errno) : "");
262
263         return result;
264 }
265
266 static struct vfs_fn_pointers vfs_audit_fns = {
267         .connect_fn = audit_connect,
268         .disconnect_fn = audit_disconnect,
269         .opendir_fn = audit_opendir,
270         .mkdir_fn = audit_mkdir,
271         .rmdir_fn = audit_rmdir,
272         .open_fn = audit_open,
273         .close_fn = audit_close,
274         .rename_fn = audit_rename,
275         .unlink_fn = audit_unlink,
276         .chmod_fn = audit_chmod,
277         .fchmod_fn = audit_fchmod,
278 };
279
280 static_decl_vfs;
281 NTSTATUS vfs_audit_init(TALLOC_CTX *ctx)
282 {
283         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "audit",
284                                 &vfs_audit_fns);
285 }