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 /* VFS Functions identifier table. In protocol version 2, every vfs */
31 /* function is given a unique id. */
33 /* care for the order here, required for compatibility */
34 /* with protocol version 1. */
39 /* end of protocol version 1 identifiers. */
44 static int vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
46 static enum sock_type
smb_traffic_analyzer_connMode(vfs_handle_struct
*handle
)
48 connection_struct
*conn
= handle
->conn
;
50 Mode
=lp_parm_const_string(SNUM(conn
), "smb_traffic_analyzer","mode", \
52 if (strstr(Mode
,"unix_domain_socket")) {
53 return UNIX_DOMAIN_SOCKET
;
55 return INTERNET_SOCKET
;
60 /* Connect to an internet socket */
62 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct
*handle
,
63 const char *name
, uint16_t port
)
65 /* Create a streaming Socket */
67 struct addrinfo hints
;
68 struct addrinfo
*ailist
= NULL
;
69 struct addrinfo
*res
= NULL
;
73 /* By default make sure it supports TCP. */
74 hints
.ai_socktype
= SOCK_STREAM
;
75 hints
.ai_flags
= AI_ADDRCONFIG
;
77 ret
= getaddrinfo(name
,
83 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
84 "getaddrinfo failed for name %s [%s]\n",
90 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
91 "Port: %i\n", name
, port
));
93 for (res
= ailist
; res
; res
= res
->ai_next
) {
94 struct sockaddr_storage ss
;
97 if (!res
->ai_addr
|| res
->ai_addrlen
== 0) {
102 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
104 status
= open_socket_out(&ss
, port
, 10000, &sockfd
);
105 if (NT_STATUS_IS_OK(status
)) {
111 freeaddrinfo(ailist
);
115 DEBUG(1, ("smb_traffic_analyzer: unable to create "
116 "socket, error is %s",
124 /* Connect to a unix domain socket */
126 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct
*handle
,
129 /* Create the socket to stad */
131 struct sockaddr_un remote
;
133 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
134 "Unix domain socket mode. Using %s\n",
137 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
138 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
139 "Couldn't create socket, "
140 "make sure stad is running!\n"));
143 remote
.sun_family
= AF_UNIX
;
144 strlcpy(remote
.sun_path
, name
,
145 sizeof(remote
.sun_path
));
146 len
=strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
147 if (connect(sock
, (struct sockaddr
*)&remote
, len
) == -1 ) {
148 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
149 "Could not connect to "
150 "socket, make sure\nstad is running!\n"));
157 /* Private data allowing shared connection sockets. */
159 struct refcounted_sock
{
160 struct refcounted_sock
*next
, *prev
;
164 unsigned int ref_count
;
167 /* Send data over a socket */
169 static void smb_traffic_analyzer_send_data(vfs_handle_struct
*handle
,
171 const char *file_name
,
172 enum vfs_id vfs_operation
)
174 struct refcounted_sock
*rf_sock
= NULL
;
177 struct tm
*tm
= NULL
;
180 char *username
= NULL
;
181 const char *anon_prefix
= NULL
;
182 const char *total_anonymization
= NULL
;
183 const char *protocol_version
= NULL
;
187 SMB_VFS_HANDLE_GET_DATA(handle
, rf_sock
, struct refcounted_sock
, return);
189 if (rf_sock
== NULL
|| rf_sock
->sock
== -1) {
190 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
196 tv_sec
= convert_timespec_to_time_t(convert_timeval_to_timespec(tv
));
197 tm
= localtime(&tv_sec
);
201 seconds
=(float) (tv
.tv_usec
/ 1000);
203 /* check if anonymization is required */
205 total_anonymization
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",
206 "total_anonymization", NULL
);
208 anon_prefix
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",\
209 "anonymize_prefix", NULL
);
210 if (anon_prefix
!=NULL
) {
211 if (total_anonymization
!=NULL
) {
212 username
= talloc_asprintf(talloc_tos(),
216 username
= talloc_asprintf(talloc_tos(),
220 handle
->conn
->server_info
->sanitized_username
) );
224 username
= handle
->conn
->server_info
->sanitized_username
;
231 protocol_version
= lp_parm_const_string(SNUM(handle
->conn
),
232 "smb_traffic_analyzer",
233 "protocol_version", NULL
);
235 if ( protocol_version
== NULL
|| strcmp( protocol_version
,"V1") == 0) {
237 /* in case of protocol v1, ignore any vfs operations */
238 /* except read,pread,write,pwrite, and set the "Write" */
239 /* bool accordingly. */
241 if ( vfs_operation
> vfs_id_pwrite
) return;
243 if ( vfs_operation
<= vfs_id_pread
) Write
=false;
246 str
= talloc_asprintf(talloc_tos(),
247 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
248 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
249 (unsigned int)result
,
251 pdb_get_domain(handle
->conn
->server_info
->sam_account
),
253 handle
->conn
->connectpath
,
262 } else if ( strcmp( protocol_version
, "V2") == 0) {
263 /* protocol version 2 */
266 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
267 "error, unkown protocol given!\n"));
272 DEBUG(1, ("smb_traffic_analyzer_send_data: "
273 "unable to create string to send!\n"));
279 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
281 if (write_data(rf_sock
->sock
, str
, len
) != len
) {
282 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
283 "error sending data to socket!\n"));
288 static struct refcounted_sock
*sock_list
;
290 static void smb_traffic_analyzer_free_data(void **pptr
)
292 struct refcounted_sock
*rf_sock
= *(struct refcounted_sock
**)pptr
;
293 if (rf_sock
== NULL
) {
296 rf_sock
->ref_count
--;
297 if (rf_sock
->ref_count
!= 0) {
300 if (rf_sock
->sock
!= -1) {
301 close(rf_sock
->sock
);
303 DLIST_REMOVE(sock_list
, rf_sock
);
304 TALLOC_FREE(rf_sock
);
307 static int smb_traffic_analyzer_connect(struct vfs_handle_struct
*handle
,
311 connection_struct
*conn
= handle
->conn
;
312 enum sock_type st
= smb_traffic_analyzer_connMode(handle
);
313 struct refcounted_sock
*rf_sock
= NULL
;
314 const char *name
= (st
== UNIX_DOMAIN_SOCKET
) ? LOCAL_PATHNAME
:
315 lp_parm_const_string(SNUM(conn
),
316 "smb_traffic_analyzer",
317 "host", "localhost");
318 uint16_t port
= (st
== UNIX_DOMAIN_SOCKET
) ? 0 :
319 atoi( lp_parm_const_string(SNUM(conn
),
320 "smb_traffic_analyzer", "port", "9430"));
321 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
327 /* Are we already connected ? */
328 for (rf_sock
= sock_list
; rf_sock
; rf_sock
= rf_sock
->next
) {
329 if (port
== rf_sock
->port
&&
330 (strcmp(name
, rf_sock
->name
) == 0)) {
335 /* If we're connected already, just increase the
336 * reference count. */
338 rf_sock
->ref_count
++;
340 /* New connection. */
341 rf_sock
= TALLOC_ZERO_P(NULL
, struct refcounted_sock
);
342 if (rf_sock
== NULL
) {
343 SMB_VFS_NEXT_DISCONNECT(handle
);
347 rf_sock
->name
= talloc_strdup(rf_sock
, name
);
348 if (rf_sock
->name
== NULL
) {
349 SMB_VFS_NEXT_DISCONNECT(handle
);
350 TALLOC_FREE(rf_sock
);
354 rf_sock
->port
= port
;
355 rf_sock
->ref_count
= 1;
357 if (st
== UNIX_DOMAIN_SOCKET
) {
358 rf_sock
->sock
= smb_traffic_analyzer_connect_unix_socket(handle
,
362 rf_sock
->sock
= smb_traffic_analyzer_connect_inet_socket(handle
,
366 if (rf_sock
->sock
== -1) {
367 SMB_VFS_NEXT_DISCONNECT(handle
);
368 TALLOC_FREE(rf_sock
);
371 DLIST_ADD(sock_list
, rf_sock
);
374 /* Store the private data. */
375 SMB_VFS_HANDLE_SET_DATA(handle
, rf_sock
, smb_traffic_analyzer_free_data
,
376 struct refcounted_sock
, return -1);
382 static int smb_traffic_analyzer_mkdir(vfs_handle_struct
*handle
, \
383 const char *path
, mode_t mode
)
386 result
= SMB_VFS_NEXT_MKDIR(handle
, path
, mode
);
387 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path
));
388 smb_traffic_analyzer_send_data(handle
,
395 static ssize_t
smb_traffic_analyzer_read(vfs_handle_struct
*handle
, \
396 files_struct
*fsp
, void *data
, size_t n
)
400 result
= SMB_VFS_NEXT_READ(handle
, fsp
, data
, n
);
401 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp
)));
403 smb_traffic_analyzer_send_data(handle
,
405 fsp
->fsp_name
->base_name
,
411 static ssize_t
smb_traffic_analyzer_pread(vfs_handle_struct
*handle
, \
412 files_struct
*fsp
, void *data
, size_t n
, SMB_OFF_T offset
)
416 result
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
418 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
421 smb_traffic_analyzer_send_data(handle
,
423 fsp
->fsp_name
->base_name
,
429 static ssize_t
smb_traffic_analyzer_write(vfs_handle_struct
*handle
, \
430 files_struct
*fsp
, const void *data
, size_t n
)
434 result
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, n
);
436 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
439 smb_traffic_analyzer_send_data(handle
,
441 fsp
->fsp_name
->base_name
,
446 static ssize_t
smb_traffic_analyzer_pwrite(vfs_handle_struct
*handle
, \
447 files_struct
*fsp
, const void *data
, size_t n
, SMB_OFF_T offset
)
451 result
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
453 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp_str_dbg(fsp
)));
455 smb_traffic_analyzer_send_data(handle
,
457 fsp
->fsp_name
->base_name
,
462 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns
= {
463 .connect_fn
= smb_traffic_analyzer_connect
,
464 .vfs_read
= smb_traffic_analyzer_read
,
465 .pread
= smb_traffic_analyzer_pread
,
466 .write
= smb_traffic_analyzer_write
,
467 .pwrite
= smb_traffic_analyzer_pwrite
,
468 .mkdir
= smb_traffic_analyzer_mkdir
471 /* Module initialization */
473 NTSTATUS
vfs_smb_traffic_analyzer_init(void)
475 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
476 "smb_traffic_analyzer",
477 &vfs_smb_traffic_analyzer_fns
);
479 if (!NT_STATUS_IS_OK(ret
)) {
483 vfs_smb_traffic_analyzer_debug_level
=
484 debug_add_class("smb_traffic_analyzer");
486 if (vfs_smb_traffic_analyzer_debug_level
== -1) {
487 vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
488 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
489 "debugging class!\n"));
491 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
492 "'smb_traffic_analyzer': %d\n", \
493 vfs_smb_traffic_analyzer_debug_level
));