2 * traffic-analyzer VFS module. Measure the smb traffic users create
5 * Copyright (C) Holger Hetterich, 2008
6 * Copyright (C) Jeremy Allison, 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 /* abstraction for the send_over_network function */
26 enum sock_type
{INTERNET_SOCKET
= 0, UNIX_DOMAIN_SOCKET
};
28 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
30 static int vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
32 static enum sock_type
smb_traffic_analyzer_connMode(vfs_handle_struct
*handle
)
34 connection_struct
*conn
= handle
->conn
;
36 Mode
=lp_parm_const_string(SNUM(conn
), "smb_traffic_analyzer","mode", \
38 if (strstr(Mode
,"unix_domain_socket")) {
39 return UNIX_DOMAIN_SOCKET
;
41 return INTERNET_SOCKET
;
45 /* Connect to an internet socket */
47 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct
*handle
,
48 const char *name
, uint16_t port
)
50 /* Create a streaming Socket */
52 struct addrinfo hints
;
53 struct addrinfo
*ailist
= NULL
;
54 struct addrinfo
*res
= NULL
;
58 /* By default make sure it supports TCP. */
59 hints
.ai_socktype
= SOCK_STREAM
;
60 hints
.ai_flags
= AI_ADDRCONFIG
;
62 ret
= getaddrinfo(name
,
68 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
69 "getaddrinfo failed for name %s [%s]\n",
75 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
76 "Port: %i\n", name
, port
));
78 for (res
= ailist
; res
; res
= res
->ai_next
) {
79 struct sockaddr_storage ss
;
81 if (!res
->ai_addr
|| res
->ai_addrlen
== 0) {
86 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
88 sockfd
= open_socket_out(SOCK_STREAM
, &ss
, port
, 10000);
99 DEBUG(1, ("smb_traffic_analyzer: unable to create "
100 "socket, error is %s",
108 /* Connect to a unix domain socket */
110 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct
*handle
,
113 /* Create the socket to stad */
115 struct sockaddr_un remote
;
117 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
118 "Unix domain socket mode. Using %s\n",
121 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
122 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
123 "Couldn't create socket, "
124 "make sure stad is running!\n"));
126 remote
.sun_family
= AF_UNIX
;
127 strlcpy(remote
.sun_path
, name
,
128 sizeof(remote
.sun_path
));
129 len
=strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
130 if (connect(sock
, (struct sockaddr
*)&remote
, len
) == -1 ) {
131 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
132 "Could not connect to "
133 "socket, make sure\nstad is running!\n"));
140 /* Private data allowing shared connection sockets. */
142 struct refcounted_sock
{
143 struct refcounted_sock
*next
, *prev
;
147 unsigned int ref_count
;
150 /* Send data over a socket */
152 static void smb_traffic_analyzer_send_data(vfs_handle_struct
*handle
,
154 const char *file_name
,
157 struct refcounted_sock
*rf_sock
= NULL
;
159 struct tm
*tm
= NULL
;
164 SMB_VFS_HANDLE_GET_DATA(handle
, rf_sock
, struct refcounted_sock
, return);
166 if (rf_sock
== NULL
|| rf_sock
->sock
== -1) {
167 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
173 tm
=localtime(&tv
.tv_sec
);
177 seconds
=(float) (tv
.tv_usec
/ 1000);
179 str
= talloc_asprintf(talloc_tos(),
180 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
181 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
182 (unsigned int)result
,
183 handle
->conn
->server_info
->sanitized_username
,
184 pdb_get_domain(handle
->conn
->server_info
->sam_account
),
186 handle
->conn
->connectpath
,
202 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
204 if (write_data(rf_sock
->sock
, str
, len
) != len
) {
205 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
206 "error sending data to socket!\n"));
211 static struct refcounted_sock
*sock_list
;
213 static void smb_traffic_analyzer_free_data(void **pptr
)
215 struct refcounted_sock
*rf_sock
= *(struct refcounted_sock
**)pptr
;
216 if (rf_sock
== NULL
) {
219 rf_sock
->ref_count
--;
220 if (rf_sock
->ref_count
!= 0) {
223 if (rf_sock
->sock
!= -1) {
224 close(rf_sock
->sock
);
226 DLIST_REMOVE(sock_list
, rf_sock
);
227 TALLOC_FREE(rf_sock
);
230 static int smb_traffic_analyzer_connect(struct vfs_handle_struct
*handle
,
234 connection_struct
*conn
= handle
->conn
;
235 enum sock_type st
= smb_traffic_analyzer_connMode(handle
);
236 struct refcounted_sock
*rf_sock
= NULL
;
237 const char *name
= (st
== UNIX_DOMAIN_SOCKET
) ? LOCAL_PATHNAME
:
238 lp_parm_const_string(SNUM(conn
),
239 "smb_traffic_analyzer",
240 "host", "localhost");
241 uint16_t port
= (st
== UNIX_DOMAIN_SOCKET
) ? 0 :
242 atoi( lp_parm_const_string(SNUM(conn
),
243 "smb_traffic_analyzer", "port", "9430"));
245 /* Are we already connected ? */
246 for (rf_sock
= sock_list
; rf_sock
; rf_sock
= rf_sock
->next
) {
247 if (port
== rf_sock
->port
&&
248 (strcmp(name
, rf_sock
->name
) == 0)) {
253 /* If we're connected already, just increase the
254 * reference count. */
256 rf_sock
->ref_count
++;
258 /* New connection. */
259 rf_sock
= TALLOC_ZERO_P(NULL
, struct refcounted_sock
);
260 if (rf_sock
== NULL
) {
264 rf_sock
->name
= talloc_strdup(rf_sock
, name
);
265 if (rf_sock
->name
== NULL
) {
266 TALLOC_FREE(rf_sock
);
270 rf_sock
->port
= port
;
271 rf_sock
->ref_count
= 1;
273 if (st
== UNIX_DOMAIN_SOCKET
) {
274 rf_sock
->sock
= smb_traffic_analyzer_connect_unix_socket(handle
,
278 rf_sock
->sock
= smb_traffic_analyzer_connect_inet_socket(handle
,
282 if (rf_sock
->sock
== -1) {
283 TALLOC_FREE(rf_sock
);
286 DLIST_ADD(sock_list
, rf_sock
);
289 /* Store the private data. */
290 SMB_VFS_HANDLE_SET_DATA(handle
, rf_sock
, smb_traffic_analyzer_free_data
,
291 struct refcounted_sock
, return -1);
292 return SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
295 /* VFS Functions: write, read, pread, pwrite for now */
297 static ssize_t
smb_traffic_analyzer_read(vfs_handle_struct
*handle
, \
298 files_struct
*fsp
, void *data
, size_t n
)
302 result
= SMB_VFS_NEXT_READ(handle
, fsp
, data
, n
);
303 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp
->fsp_name
));
305 smb_traffic_analyzer_send_data(handle
,
313 static ssize_t
smb_traffic_analyzer_pread(vfs_handle_struct
*handle
, \
314 files_struct
*fsp
, void *data
, size_t n
, SMB_OFF_T offset
)
318 result
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
320 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n", fsp
->fsp_name
));
322 smb_traffic_analyzer_send_data(handle
,
330 static ssize_t
smb_traffic_analyzer_write(vfs_handle_struct
*handle
, \
331 files_struct
*fsp
, const void *data
, size_t n
)
335 result
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, n
);
337 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n", fsp
->fsp_name
));
339 smb_traffic_analyzer_send_data(handle
,
346 static ssize_t
smb_traffic_analyzer_pwrite(vfs_handle_struct
*handle
, \
347 files_struct
*fsp
, const void *data
, size_t n
, SMB_OFF_T offset
)
351 result
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
353 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp
->fsp_name
));
355 smb_traffic_analyzer_send_data(handle
,
362 /* VFS operations we use */
364 static vfs_op_tuple smb_traffic_analyzer_tuples
[] = {
366 {SMB_VFS_OP(smb_traffic_analyzer_connect
), SMB_VFS_OP_CONNECT
,
367 SMB_VFS_LAYER_LOGGER
},
368 {SMB_VFS_OP(smb_traffic_analyzer_read
), SMB_VFS_OP_READ
,
369 SMB_VFS_LAYER_LOGGER
},
370 {SMB_VFS_OP(smb_traffic_analyzer_pread
), SMB_VFS_OP_PREAD
,
371 SMB_VFS_LAYER_LOGGER
},
372 {SMB_VFS_OP(smb_traffic_analyzer_write
), SMB_VFS_OP_WRITE
,
373 SMB_VFS_LAYER_LOGGER
},
374 {SMB_VFS_OP(smb_traffic_analyzer_pwrite
), SMB_VFS_OP_PWRITE
,
375 SMB_VFS_LAYER_LOGGER
},
376 {SMB_VFS_OP(NULL
),SMB_VFS_OP_NOOP
,SMB_VFS_LAYER_NOOP
}
379 /* Module initialization */
381 NTSTATUS
vfs_smb_traffic_analyzer_init(void)
383 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, \
384 "smb_traffic_analyzer", smb_traffic_analyzer_tuples
);
386 if (!NT_STATUS_IS_OK(ret
)) {
390 vfs_smb_traffic_analyzer_debug_level
=
391 debug_add_class("smb_traffic_analyzer");
393 if (vfs_smb_traffic_analyzer_debug_level
== -1) {
394 vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
395 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
396 "debugging class!\n"));
398 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
399 "'smb_traffic_analyzer': %d\n", \
400 vfs_smb_traffic_analyzer_debug_level
));