clunky support for calling AddPrinterEx(). The code currently reports
[Samba.git] / examples / VFS / audit.c
blob036438e90e40bc59508ad3fc2b9a983707f0cd96
1 /*
2 * Auditing VFS module for samba. Log selected file operations to syslog
3 * facility.
5 * Copyright (C) Tim Potter, 1999-2000
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "config.h"
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #ifdef HAVE_UTIME_H
26 #include <utime.h>
27 #endif
28 #ifdef HAVE_DIRENT_H
29 #include <dirent.h>
30 #endif
31 #include <syslog.h>
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif
35 #include <errno.h>
36 #include <string.h>
37 #include <vfs.h>
39 #ifndef SYSLOG_FACILITY
40 #define SYSLOG_FACILITY LOG_USER
41 #endif
43 #ifndef SYSLOG_PRIORITY
44 #define SYSLOG_PRIORITY LOG_NOTICE
45 #endif
47 /* Function prototypes */
49 int audit_connect(struct vfs_connection_struct *conn, char *svc, char *user);
50 void audit_disconnect(void);
51 DIR *audit_opendir(char *fname);
52 int audit_mkdir(char *path, mode_t mode);
53 int audit_rmdir(char *path);
54 int audit_open(char *fname, int flags, mode_t mode);
55 int audit_close(int fd);
56 int audit_rename(char *old, char *new);
57 int audit_unlink(char *path);
58 int audit_chmod(char *path, mode_t mode);
60 /* VFS operations */
62 extern struct vfs_ops default_vfs_ops; /* For passthrough operation */
64 struct vfs_ops audit_ops = {
66 /* Disk operations */
68 audit_connect,
69 audit_disconnect,
70 NULL, /* disk free */
72 /* Directory operations */
74 audit_opendir,
75 NULL, /* readdir */
76 audit_mkdir,
77 audit_rmdir,
78 NULL, /* closedir */
80 /* File operations */
82 audit_open,
83 audit_close,
84 NULL, /* read */
85 NULL, /* write */
86 NULL, /* lseek */
87 audit_rename,
88 NULL, /* fsync */
89 NULL, /* stat */
90 NULL, /* fstat */
91 NULL, /* lstat */
92 audit_unlink,
93 NULL, /* chmod */
94 NULL, /* utime */
95 NULL, /* ftruncate */
96 NULL /* lock */
99 /* VFS initialisation function. Return initialised vfs_ops structure
100 back to SAMBA. */
102 struct vfs_ops *vfs_init(void)
104 openlog("smbd_audit", LOG_PID, SYSLOG_FACILITY);
105 return(&audit_ops);
108 /* Implementation of vfs_ops. Pass everything on to the default
109 operation but log event first. */
111 int audit_connect(struct vfs_connection_struct *conn, char *svc, char *user)
113 syslog(SYSLOG_PRIORITY, "connect to service %s by user %s\n",
114 svc, user);
116 return default_vfs_ops.connect(conn, svc, user);
119 void audit_disconnect(void)
121 syslog(SYSLOG_PRIORITY, "disconnected\n");
122 default_vfs_ops.disconnect();
125 DIR *audit_opendir(char *fname)
127 DIR *result = default_vfs_ops.opendir(fname);
129 syslog(SYSLOG_PRIORITY, "opendir %s %s%s\n",
130 fname,
131 (result == NULL) ? "failed: " : "",
132 (result == NULL) ? strerror(errno) : "");
134 return result;
137 int audit_mkdir(char *path, mode_t mode)
139 int result = default_vfs_ops.mkdir(path, mode);
141 syslog(SYSLOG_PRIORITY, "mkdir %s %s%s\n",
142 path,
143 (result < 0) ? "failed: " : "",
144 (result < 0) ? strerror(errno) : "");
146 return result;
149 int audit_rmdir(char *path)
151 int result = default_vfs_ops.rmdir(path);
153 syslog(SYSLOG_PRIORITY, "rmdir %s %s%s\n",
154 path,
155 (result < 0) ? "failed: " : "",
156 (result < 0) ? strerror(errno) : "");
158 return result;
161 int audit_open(char *fname, int flags, mode_t mode)
163 int result = default_vfs_ops.open(fname, flags, mode);
165 syslog(SYSLOG_PRIORITY, "open %s (fd %d) %s%s%s\n",
166 fname, result,
167 ((flags & O_WRONLY) || (flags & O_RDWR)) ? "for writing " : "",
168 (result < 0) ? "failed: " : "",
169 (result < 0) ? strerror(errno) : "");
171 return result;
174 int audit_close(int fd)
176 int result = default_vfs_ops.close(fd);
178 syslog(SYSLOG_PRIORITY, "close fd %d %s%s\n",
180 (result < 0) ? "failed: " : "",
181 (result < 0) ? strerror(errno) : "");
183 return result;
186 int audit_rename(char *old, char *new)
188 int result = default_vfs_ops.rename(old, new);
190 syslog(SYSLOG_PRIORITY, "rename %s -> %s %s%s\n",
191 old, new,
192 (result < 0) ? "failed: " : "",
193 (result < 0) ? strerror(errno) : "");
195 return result;
198 int audit_unlink(char *path)
200 int result = default_vfs_ops.unlink(path);
202 syslog(SYSLOG_PRIORITY, "unlink %s %s%s\n",
203 path,
204 (result < 0) ? "failed: " : "",
205 (result < 0) ? strerror(errno) : "");
207 return result;