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/>.
23 #include "../lib/crypto/crypto.h"
25 /* 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. */
46 /* Specific data sets for the VFS functions. */
48 typedef struct mkdir_data
{
54 typedef struct rmdir_data
{
59 typedef struct rename_data
{
65 typedef struct chdir_data
{
70 /* rw_data used for read/write/pread/pwrite */
77 static int vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
79 static enum sock_type
smb_traffic_analyzer_connMode(vfs_handle_struct
*handle
)
81 connection_struct
*conn
= handle
->conn
;
83 Mode
=lp_parm_const_string(SNUM(conn
), "smb_traffic_analyzer","mode", \
85 if (strstr(Mode
,"unix_domain_socket")) {
86 return UNIX_DOMAIN_SOCKET
;
88 return INTERNET_SOCKET
;
93 /* Connect to an internet socket */
95 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct
*handle
,
96 const char *name
, uint16_t port
)
98 /* Create a streaming Socket */
100 struct addrinfo hints
;
101 struct addrinfo
*ailist
= NULL
;
102 struct addrinfo
*res
= NULL
;
106 /* By default make sure it supports TCP. */
107 hints
.ai_socktype
= SOCK_STREAM
;
108 hints
.ai_flags
= AI_ADDRCONFIG
;
110 ret
= getaddrinfo(name
,
116 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
117 "getaddrinfo failed for name %s [%s]\n",
119 gai_strerror(ret
) ));
123 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
124 "Port: %i\n", name
, port
));
126 for (res
= ailist
; res
; res
= res
->ai_next
) {
127 struct sockaddr_storage ss
;
130 if (!res
->ai_addr
|| res
->ai_addrlen
== 0) {
135 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
137 status
= open_socket_out(&ss
, port
, 10000, &sockfd
);
138 if (NT_STATUS_IS_OK(status
)) {
144 freeaddrinfo(ailist
);
148 DEBUG(1, ("smb_traffic_analyzer: unable to create "
149 "socket, error is %s",
157 /* Connect to a unix domain socket */
159 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct
*handle
,
162 /* Create the socket to stad */
164 struct sockaddr_un remote
;
166 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
167 "Unix domain socket mode. Using %s\n",
170 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
171 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
172 "Couldn't create socket, "
173 "make sure stad is running!\n"));
176 remote
.sun_family
= AF_UNIX
;
177 strlcpy(remote
.sun_path
, name
,
178 sizeof(remote
.sun_path
));
179 len
=strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
180 if (connect(sock
, (struct sockaddr
*)&remote
, len
) == -1 ) {
181 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
182 "Could not connect to "
183 "socket, make sure\nstad is running!\n"));
190 /* Private data allowing shared connection sockets. */
192 struct refcounted_sock
{
193 struct refcounted_sock
*next
, *prev
;
197 unsigned int ref_count
;
201 /* The marshaller for the protocol version 2. */
202 static char *smb_traffic_analyzer_create_string( struct tm
*tm
, \
203 int seconds
, vfs_handle_struct
*handle
, \
204 char *username
, int count
, ... )
213 char *timestr
= NULL
;
215 /* first create the data that is transfered with any VFS op */
216 len
= strlen( username
);
217 buf
= talloc_asprintf(talloc_tos(),"%04u%s", len
, username
);
218 len
= strlen( handle
->conn
->connectpath
);
219 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, \
220 handle
->conn
->connectpath
);
221 len
= strlen( pdb_get_domain(handle
->conn
->server_info
->sam_account
) );
222 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, \
223 pdb_get_domain(handle
->conn
->server_info
->sam_account
) );
224 timestr
= talloc_asprintf(talloc_tos(), \
225 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
233 len
= strlen( timestr
);
234 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, timestr
);
236 va_start( ap
, count
);
238 arg
= va_arg( ap
, char * );
239 /* protocol v2 sends a four byte string */
240 /* as a header to each block, including */
241 /* the numbers of bytes to come in the */
244 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, arg
);
248 /* now create the protocol v2 header. */
250 str
= talloc_asprintf_append( str
, "V2,%017u%s", len
, buf
);
251 DEBUG(10, ("smb_traffic_analyzer_create_string: %s\n",str
));
255 static void smb_traffic_analyzer_send_data(vfs_handle_struct
*handle
,
257 enum vfs_id vfs_operation
)
259 struct refcounted_sock
*rf_sock
= NULL
;
262 struct tm
*tm
= NULL
;
265 char *username
= NULL
;
266 const char *anon_prefix
= NULL
;
267 const char *total_anonymization
= NULL
;
268 const char *protocol_version
= NULL
;
272 SMB_VFS_HANDLE_GET_DATA(handle
, rf_sock
, struct refcounted_sock
, return);
274 if (rf_sock
== NULL
|| rf_sock
->sock
== -1) {
275 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
281 tv_sec
= convert_timespec_to_time_t(convert_timeval_to_timespec(tv
));
282 tm
= localtime(&tv_sec
);
286 seconds
=(float) (tv
.tv_usec
/ 1000);
288 /* check if anonymization is required */
290 total_anonymization
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",
291 "total_anonymization", NULL
);
293 anon_prefix
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",\
294 "anonymize_prefix", NULL
);
295 if (anon_prefix
!=NULL
) {
296 if (total_anonymization
!=NULL
) {
297 username
= talloc_asprintf(talloc_tos(),
301 username
= talloc_asprintf(talloc_tos(),
305 handle
->conn
->server_info
->sanitized_username
) );
309 username
= handle
->conn
->server_info
->sanitized_username
;
316 protocol_version
= lp_parm_const_string(SNUM(handle
->conn
),
317 "smb_traffic_analyzer",
318 "protocol_version", NULL
);
320 if ( protocol_version
== NULL
|| strcmp( protocol_version
,"V1") == 0) {
322 struct rw_data
*s_data
= (struct rw_data
*) data
;
324 /* in case of protocol v1, ignore any vfs operations */
325 /* except read,pread,write,pwrite, and set the "Write" */
326 /* bool accordingly. */
328 if ( vfs_operation
> vfs_id_pwrite
) return;
330 if ( vfs_operation
<= vfs_id_pread
) Write
=false;
333 str
= talloc_asprintf(talloc_tos(),
334 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
335 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
336 (unsigned int) s_data
->len
,
338 pdb_get_domain(handle
->conn
->server_info
->sam_account
),
340 handle
->conn
->connectpath
,
349 } else if ( strcmp( protocol_version
, "V2") == 0) {
351 switch( vfs_operation
) {
353 str
= smb_traffic_analyzer_create_string( tm
, \
354 seconds
, handle
, username
, \
355 3, ((t_mkdir
*) data
)->path
, \
356 talloc_asprintf( talloc_tos(), "%u", \
357 ((t_mkdir
*) data
)->mode
), \
358 talloc_asprintf( talloc_tos(), "%u", \
359 ((t_mkdir
*) data
)->result
));
362 str
= smb_traffic_analyzer_create_string( tm
, \
363 seconds
, handle
, username
, \
364 2, ((t_rmdir
*) data
)->path
, \
365 talloc_asprintf( talloc_tos(), "%u", \
366 ((t_rmdir
*) data
)->result
));
368 case vfs_id_rename
: ;
369 str
= smb_traffic_analyzer_create_string( tm
, \
370 seconds
, handle
, username
, \
371 3, ((t_rename
*) data
)->src
, \
372 ((t_rename
*) data
)->dst
,
373 talloc_asprintf(talloc_tos(), "%u", \
374 ((t_rename
*) data
)->result
));
377 str
= smb_traffic_analyzer_create_string( tm
, \
378 seconds
, handle
, username
, \
379 2, ((t_chdir
*) data
)->path
, \
380 talloc_asprintf(talloc_tos(), "%u", \
381 ((t_chdir
*) data
)->result
));
384 DEBUG(1, ("smb_traffic_analyzer: error! "
385 "wrong VFS operation id detected!\n"));
390 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
391 "error, unkown protocol given!\n"));
396 DEBUG(1, ("smb_traffic_analyzer_send_data: "
397 "unable to create string to send!\n"));
403 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: going to send "
405 /* If configured, optain the key and run AES encryption */
408 char *akey
= secrets_fetch("smb_traffic_analyzer_key", &size
);
409 if ( akey
!= NULL
) {
411 DEBUG(10, ("smb_traffic_analyzer: a key was found, encrypting "
414 samba_AES_set_encrypt_key(akey
, 128, key
);
415 samba_AES_encrypt( str
, crypted
, key
);
416 len
= strlen( crypted
);
417 if (write_data(rf_sock
->sock
, crypted
, len
) != len
) {
418 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
419 "error sending crypted data to socket!\n"));
424 if (write_data(rf_sock
->sock
, str
, len
) != len
) {
425 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
426 "error sending data to socket!\n"));
431 static struct refcounted_sock
*sock_list
;
433 static void smb_traffic_analyzer_free_data(void **pptr
)
435 struct refcounted_sock
*rf_sock
= *(struct refcounted_sock
**)pptr
;
436 if (rf_sock
== NULL
) {
439 rf_sock
->ref_count
--;
440 if (rf_sock
->ref_count
!= 0) {
443 if (rf_sock
->sock
!= -1) {
444 close(rf_sock
->sock
);
446 DLIST_REMOVE(sock_list
, rf_sock
);
447 TALLOC_FREE(rf_sock
);
450 static int smb_traffic_analyzer_connect(struct vfs_handle_struct
*handle
,
454 connection_struct
*conn
= handle
->conn
;
455 enum sock_type st
= smb_traffic_analyzer_connMode(handle
);
456 struct refcounted_sock
*rf_sock
= NULL
;
457 const char *name
= (st
== UNIX_DOMAIN_SOCKET
) ? LOCAL_PATHNAME
:
458 lp_parm_const_string(SNUM(conn
),
459 "smb_traffic_analyzer",
460 "host", "localhost");
461 uint16_t port
= (st
== UNIX_DOMAIN_SOCKET
) ? 0 :
462 atoi( lp_parm_const_string(SNUM(conn
),
463 "smb_traffic_analyzer", "port", "9430"));
464 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
470 /* Are we already connected ? */
471 for (rf_sock
= sock_list
; rf_sock
; rf_sock
= rf_sock
->next
) {
472 if (port
== rf_sock
->port
&&
473 (strcmp(name
, rf_sock
->name
) == 0)) {
478 /* If we're connected already, just increase the
479 * reference count. */
481 rf_sock
->ref_count
++;
483 /* New connection. */
484 rf_sock
= TALLOC_ZERO_P(NULL
, struct refcounted_sock
);
485 if (rf_sock
== NULL
) {
486 SMB_VFS_NEXT_DISCONNECT(handle
);
490 rf_sock
->name
= talloc_strdup(rf_sock
, name
);
491 if (rf_sock
->name
== NULL
) {
492 SMB_VFS_NEXT_DISCONNECT(handle
);
493 TALLOC_FREE(rf_sock
);
497 rf_sock
->port
= port
;
498 rf_sock
->ref_count
= 1;
500 if (st
== UNIX_DOMAIN_SOCKET
) {
501 rf_sock
->sock
= smb_traffic_analyzer_connect_unix_socket(handle
,
505 rf_sock
->sock
= smb_traffic_analyzer_connect_inet_socket(handle
,
509 if (rf_sock
->sock
== -1) {
510 SMB_VFS_NEXT_DISCONNECT(handle
);
511 TALLOC_FREE(rf_sock
);
514 DLIST_ADD(sock_list
, rf_sock
);
517 /* Store the private data. */
518 SMB_VFS_HANDLE_SET_DATA(handle
, rf_sock
, smb_traffic_analyzer_free_data
,
519 struct refcounted_sock
, return -1);
524 static int smb_traffic_analyzer_chdir(vfs_handle_struct
*handle
, \
527 struct chdir_data s_data
;
528 s_data
.result
= SMB_VFS_NEXT_CHDIR(handle
, path
);
530 DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path
));
531 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_chdir
);
532 return s_data
.result
;
535 static int smb_traffic_analyzer_rename(vfs_handle_struct
*handle
, \
536 const struct smb_filename
*smb_fname_src
,
537 const struct smb_filename
*smb_fname_dst
)
539 struct rename_data s_data
;
540 s_data
.result
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, \
542 s_data
.src
= smb_fname_src
->base_name
;
543 s_data
.dst
= smb_fname_dst
->base_name
;
544 DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
545 smb_fname_src
->base_name
,
546 smb_fname_dst
->base_name
));
547 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_rename
);
548 return s_data
.result
;
551 static int smb_traffic_analyzer_rmdir(vfs_handle_struct
*handle
, \
554 struct rmdir_data s_data
;
555 s_data
.result
= SMB_VFS_NEXT_RMDIR(handle
, path
);
557 DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path
));
558 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_rmdir
);
559 return s_data
.result
;
562 static int smb_traffic_analyzer_mkdir(vfs_handle_struct
*handle
, \
563 const char *path
, mode_t mode
)
565 struct mkdir_data s_data
;
566 s_data
.result
= SMB_VFS_NEXT_MKDIR(handle
, path
, mode
);
569 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path
));
570 smb_traffic_analyzer_send_data(handle
,
573 return s_data
.result
;
576 static ssize_t
smb_traffic_analyzer_read(vfs_handle_struct
*handle
, \
577 files_struct
*fsp
, void *data
, size_t n
)
579 struct rw_data s_data
;
581 s_data
.len
= SMB_VFS_NEXT_READ(handle
, fsp
, data
, n
);
582 s_data
.filename
= fsp
->fsp_name
->base_name
;
583 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp
)));
585 smb_traffic_analyzer_send_data(handle
,
592 static ssize_t
smb_traffic_analyzer_pread(vfs_handle_struct
*handle
, \
593 files_struct
*fsp
, void *data
, size_t n
, SMB_OFF_T offset
)
595 struct rw_data s_data
;
597 s_data
.len
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
598 s_data
.filename
= fsp
->fsp_name
->base_name
;
599 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
602 smb_traffic_analyzer_send_data(handle
,
609 static ssize_t
smb_traffic_analyzer_write(vfs_handle_struct
*handle
, \
610 files_struct
*fsp
, const void *data
, size_t n
)
612 struct rw_data s_data
;
614 s_data
.len
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, n
);
615 s_data
.filename
= fsp
->fsp_name
->base_name
;
616 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
619 smb_traffic_analyzer_send_data(handle
,
625 static ssize_t
smb_traffic_analyzer_pwrite(vfs_handle_struct
*handle
, \
626 files_struct
*fsp
, const void *data
, size_t n
, SMB_OFF_T offset
)
628 struct rw_data s_data
;
630 s_data
.len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
631 s_data
.filename
= fsp
->fsp_name
->base_name
;
632 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
635 smb_traffic_analyzer_send_data(handle
,
641 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns
= {
642 .connect_fn
= smb_traffic_analyzer_connect
,
643 .vfs_read
= smb_traffic_analyzer_read
,
644 .pread
= smb_traffic_analyzer_pread
,
645 .write
= smb_traffic_analyzer_write
,
646 .pwrite
= smb_traffic_analyzer_pwrite
,
647 .mkdir
= smb_traffic_analyzer_mkdir
,
648 .rename
= smb_traffic_analyzer_rename
,
649 .chdir
= smb_traffic_analyzer_chdir
652 /* Module initialization */
654 NTSTATUS
vfs_smb_traffic_analyzer_init(void)
656 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
657 "smb_traffic_analyzer",
658 &vfs_smb_traffic_analyzer_fns
);
660 if (!NT_STATUS_IS_OK(ret
)) {
664 vfs_smb_traffic_analyzer_debug_level
=
665 debug_add_class("smb_traffic_analyzer");
667 if (vfs_smb_traffic_analyzer_debug_level
== -1) {
668 vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
669 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
670 "debugging class!\n"));
672 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
673 "'smb_traffic_analyzer': %d\n", \
674 vfs_smb_traffic_analyzer_debug_level
));