2 * traffic-analyzer VFS module. Measure the smb traffic users create
5 * Copyright (C) Holger Hetterich, 2008
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 3 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, see <http://www.gnu.org/licenses/>.
23 /* abstraction for the send_over_network function */
24 #define UNIX_DOMAIN_SOCKET 1
25 #define INTERNET_SOCKET 0
30 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
32 /* create the timestamp in sqlite compatible format */
33 static void get_timestamp(fstring str)
40 gettimeofday(&tv, &tz);
41 tm=localtime(&tv.tv_sec);
42 seconds=(float) (tv.tv_usec / 1000);
44 fstr_sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d.%03d", \
45 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, \
46 tm->tm_hour, tm->tm_min, tm->tm_sec, (int)seconds);
50 static int smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
52 connection_struct *conn = handle->conn;
54 Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
56 if (strstr(Mode,"unix_domain_socket")) {
57 return UNIX_DOMAIN_SOCKET;
59 return INTERNET_SOCKET;
64 /* Connect to an internet socket */
66 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle)
68 /* Create a streaming Socket */
72 struct addrinfo hints;
73 struct addrinfo *ailist = NULL;
74 struct addrinfo *res = NULL;
75 connection_struct *conn = handle->conn;
78 /* get port number, target system from the config parameters */
79 Hostname=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer",
83 /* By default make sure it supports TCP. */
84 hints.ai_socktype = SOCK_STREAM;
85 hints.ai_flags = AI_ADDRCONFIG;
87 ret = getaddrinfo(Hostname,
93 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
94 "getaddrinfo failed for name %s [%s]\n",
100 port = atoi( lp_parm_const_string(SNUM(conn),
101 "smb_traffic_analyzer", "port", "9430"));
103 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
104 "Port: %i\n", Hostname, port));
106 for (res = ailist; res; res = res->ai_next) {
107 struct sockaddr_storage ss;
109 if (!res->ai_addr || res->ai_addrlen == 0) {
114 memcpy(&ss, res->ai_addr, res->ai_addrlen);
116 sockfd = open_socket_out(SOCK_STREAM, &ss, port, 10000);
123 freeaddrinfo(ailist);
127 DEBUG(1, ("smb_traffic_analyzer: unable to create "
128 "socket, error is %s",
136 /* Connect to a unix domain socket */
138 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle)
140 /* Create the socket to stad */
142 struct sockaddr_un remote;
144 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
145 "Unix domain socket mode. Using "
146 "/var/tmp/stadsocket\n"));
148 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
149 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
150 "Couldn't create socket, "
151 "make sure stad is running!\n"));
153 remote.sun_family = AF_UNIX;
154 strlcpy(remote.sun_path, "/var/tmp/stadsocket",
155 sizeof(remote.sun_path));
156 len=strlen(remote.sun_path) + sizeof(remote.sun_family);
157 if (connect(sock, (struct sockaddr *)&remote, len) == -1 ) {
158 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
159 "Could not connect to "
160 "socket, make sure\nstad is running!\n"));
167 /* Send data over a socket */
169 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
171 const char *file_name,
178 SMB_VFS_HANDLE_GET_DATA(handle, psockfd, int, return);
180 if (psockfd == NULL || *psockfd == -1) {
181 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
186 strlcpy(Sender, str, sizeof(Sender));
187 strlcat(Sender, ",\"", sizeof(Sender));
188 strlcat(Sender, handle->conn->server_info->sanitized_username, sizeof(Sender));
189 strlcat(Sender, "\",\"", sizeof(Sender));
190 strlcat(Sender, pdb_get_domain(handle->conn->server_info->sam_account), sizeof(Sender));
191 strlcat(Sender, "\",\"", sizeof(Sender));
193 strlcat(Sender, "W", sizeof(Sender));
195 strlcat(Sender, "R", sizeof(Sender));
196 strlcat(Sender, "\",\"", sizeof(Sender));
197 strlcat(Sender, handle->conn->connectpath, sizeof(Sender));
198 strlcat(Sender, "\",\"", sizeof(Sender) - 1);
199 strlcat(Sender, file_name, sizeof(Sender) - 1);
200 strlcat(Sender, "\",\"", sizeof(Sender) - 1);
201 get_timestamp(TimeStamp);
202 strlcat(Sender, TimeStamp, sizeof(Sender) - 1);
203 strlcat(Sender, "\");", sizeof(Sender) - 1);
204 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
206 if (send(*psockfd, Sender, strlen(Sender), 0) == -1 ) {
207 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
208 "error sending data to socket!\n"));
213 static void smb_traffic_analyzer_free_data(void **pptr)
215 int *pfd = *(int **)pptr;
225 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
229 int *pfd = TALLOC_P(handle, int);
236 if (smb_traffic_analyzer_connMode(handle) == UNIX_DOMAIN_SOCKET) {
237 *pfd = smb_traffic_analyzer_connect_unix_socket(handle);
239 *pfd = smb_traffic_analyzer_connect_inet_socket(handle);
245 /* Store the private data. */
246 SMB_VFS_HANDLE_SET_DATA(handle, pfd, smb_traffic_analyzer_free_data,
248 return SMB_VFS_NEXT_CONNECT(handle, service, user);
251 /* VFS Functions: write, read, pread, pwrite for now */
253 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
254 files_struct *fsp, void *data, size_t n)
259 result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
260 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp->fsp_name ));
262 fstr_sprintf(Buffer, "%u", (uint) result);
264 smb_traffic_analyzer_send_data(handle,
272 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
273 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
278 result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
280 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n", fsp->fsp_name ));
282 fstr_sprintf(Buffer,"%u", (uint) result);
283 smb_traffic_analyzer_send_data(handle,
291 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
292 files_struct *fsp, const void *data, size_t n)
297 result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
299 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n", fsp->fsp_name ));
301 fstr_sprintf(Buffer, "%u", (uint) result);
302 smb_traffic_analyzer_send_data(handle,
309 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
310 files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
315 result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
317 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp->fsp_name ));
319 fstr_sprintf(Buffer, "%u", (uint) result);
320 smb_traffic_analyzer_send_data(handle,
327 /* VFS operations we use */
329 static vfs_op_tuple smb_traffic_analyzer_tuples[] = {
331 {SMB_VFS_OP(smb_traffic_analyzer_connect), SMB_VFS_OP_CONNECT,
332 SMB_VFS_LAYER_LOGGER},
333 {SMB_VFS_OP(smb_traffic_analyzer_read), SMB_VFS_OP_READ,
334 SMB_VFS_LAYER_LOGGER},
335 {SMB_VFS_OP(smb_traffic_analyzer_pread), SMB_VFS_OP_PREAD,
336 SMB_VFS_LAYER_LOGGER},
337 {SMB_VFS_OP(smb_traffic_analyzer_write), SMB_VFS_OP_WRITE,
338 SMB_VFS_LAYER_LOGGER},
339 {SMB_VFS_OP(smb_traffic_analyzer_pwrite), SMB_VFS_OP_PWRITE,
340 SMB_VFS_LAYER_LOGGER},
341 {SMB_VFS_OP(NULL),SMB_VFS_OP_NOOP,SMB_VFS_LAYER_NOOP}
344 /* Module initialization */
346 NTSTATUS vfs_smb_traffic_analyzer_init(void)
348 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, \
349 "smb_traffic_analyzer", smb_traffic_analyzer_tuples);
351 if (!NT_STATUS_IS_OK(ret)) {
355 vfs_smb_traffic_analyzer_debug_level =
356 debug_add_class("smb_traffic_analyzer");
358 if (vfs_smb_traffic_analyzer_debug_level == -1) {
359 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
360 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
361 "debugging class!\n"));
363 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
364 "'smb_traffic_analyzer': %d\n", \
365 vfs_smb_traffic_analyzer_debug_level));