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. */
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 vfs_operation
, int count
, ... )
213 char *timestr
= NULL
;
216 /* first create the data that is transfered with any VFS op */
217 opstr
= talloc_asprintf(talloc_tos(), "%i", vfs_operation
);
219 buf
= talloc_asprintf(talloc_tos(), "%04u%s", len
, opstr
);
220 len
= strlen( username
);
221 buf
= talloc_asprintf_append(buf
, "%04u%s", len
, username
);
222 len
= strlen( handle
->conn
->connectpath
);
223 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, \
224 handle
->conn
->connectpath
);
225 len
= strlen( pdb_get_domain(handle
->conn
->server_info
->sam_account
) );
226 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, \
227 pdb_get_domain(handle
->conn
->server_info
->sam_account
) );
228 timestr
= talloc_asprintf(talloc_tos(), \
229 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
237 len
= strlen( timestr
);
238 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, timestr
);
240 va_start( ap
, count
);
242 arg
= va_arg( ap
, char * );
243 /* protocol v2 sends a four byte string */
244 /* as a header to each block, including */
245 /* the numbers of bytes to come in the */
248 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, arg
);
252 /* now create the protocol v2 header. */
254 str
= talloc_asprintf_append( str
, "V2,%017u%s", len
, buf
);
255 DEBUG(10, ("smb_traffic_analyzer_create_string: %s\n",str
));
259 static void smb_traffic_analyzer_send_data(vfs_handle_struct
*handle
,
261 enum vfs_id vfs_operation
)
263 struct refcounted_sock
*rf_sock
= NULL
;
266 struct tm
*tm
= NULL
;
269 char *username
= NULL
;
270 const char *anon_prefix
= NULL
;
271 const char *total_anonymization
= NULL
;
272 const char *protocol_version
= NULL
;
276 SMB_VFS_HANDLE_GET_DATA(handle
, rf_sock
, struct refcounted_sock
, return);
278 if (rf_sock
== NULL
|| rf_sock
->sock
== -1) {
279 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
285 tv_sec
= convert_timespec_to_time_t(convert_timeval_to_timespec(tv
));
286 tm
= localtime(&tv_sec
);
290 seconds
=(float) (tv
.tv_usec
/ 1000);
292 /* check if anonymization is required */
294 total_anonymization
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",
295 "total_anonymization", NULL
);
297 anon_prefix
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",\
298 "anonymize_prefix", NULL
);
299 if (anon_prefix
!=NULL
) {
300 if (total_anonymization
!=NULL
) {
301 username
= talloc_asprintf(talloc_tos(),
305 username
= talloc_asprintf(talloc_tos(),
309 handle
->conn
->server_info
->sanitized_username
) );
313 username
= handle
->conn
->server_info
->sanitized_username
;
320 protocol_version
= lp_parm_const_string(SNUM(handle
->conn
),
321 "smb_traffic_analyzer",
322 "protocol_version", NULL
);
324 if ( protocol_version
== NULL
|| strcmp( protocol_version
,"V1") == 0) {
326 struct rw_data
*s_data
= (struct rw_data
*) data
;
328 /* in case of protocol v1, ignore any vfs operations */
329 /* except read,pread,write,pwrite, and set the "Write" */
330 /* bool accordingly. */
332 if ( vfs_operation
> vfs_id_pwrite
) return;
334 if ( vfs_operation
<= vfs_id_pread
) Write
=false;
337 str
= talloc_asprintf(talloc_tos(),
338 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
339 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
340 (unsigned int) s_data
->len
,
342 pdb_get_domain(handle
->conn
->server_info
->sam_account
),
344 handle
->conn
->connectpath
,
353 } else if ( strcmp( protocol_version
, "V2") == 0) {
355 switch( vfs_operation
) {
357 str
= smb_traffic_analyzer_create_string( tm
, \
358 seconds
, handle
, username
, vfs_id_mkdir
, 3,\
359 ((struct mkdir_data
*) data
)->path
, \
360 talloc_asprintf( talloc_tos(), "%u", \
361 ((struct mkdir_data
*) data
)->mode
), \
362 talloc_asprintf( talloc_tos(), "%u", \
363 ((struct mkdir_data
*) data
)->result
));
366 str
= smb_traffic_analyzer_create_string( tm
, \
367 seconds
, handle
, username
, vfs_id_rmdir
, 2,\
368 ((struct rmdir_data
*) data
)->path
, \
369 talloc_asprintf( talloc_tos(), "%u", \
370 ((struct rmdir_data
*) data
)->result
));
372 case vfs_id_rename
: ;
373 str
= smb_traffic_analyzer_create_string( tm
, \
374 seconds
, handle
, username
, vfs_id_rename
, 3,\
375 ((struct rename_data
*) data
)->src
, \
376 ((struct rename_data
*) data
)->dst
,
377 talloc_asprintf(talloc_tos(), "%u", \
378 ((struct rename_data
*) data
)->result
));
381 str
= smb_traffic_analyzer_create_string( tm
, \
382 seconds
, handle
, username
, vfs_id_chdir
, 2,\
383 ((struct chdir_data
*) data
)->path
, \
384 talloc_asprintf(talloc_tos(), "%u", \
385 ((struct chdir_data
*) data
)->result
));
392 str
= smb_traffic_analyzer_create_string( tm
, \
393 seconds
, handle
, username
, vfs_operation
, 2,\
394 ((struct rw_data
*) data
)->filename
, \
395 talloc_asprintf(talloc_tos(), "%u", \
396 ((struct rw_data
*) data
)->len
));
399 DEBUG(1, ("smb_traffic_analyzer: error! "
400 "wrong VFS operation id detected!\n"));
405 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
406 "error, unkown protocol given!\n"));
411 DEBUG(1, ("smb_traffic_analyzer_send_data: "
412 "unable to create string to send!\n"));
418 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: going to send "
420 /* If configured, optain the key and run AES encryption */
423 char *akey
= secrets_fetch("smb_traffic_analyzer_key", &size
);
424 if ( akey
!= NULL
) {
426 DEBUG(10, ("smb_traffic_analyzer: a key was found, encrypting "
429 samba_AES_set_encrypt_key(akey
, 128, key
);
430 samba_AES_encrypt( str
, crypted
, key
);
431 len
= strlen( crypted
);
432 if (write_data(rf_sock
->sock
, crypted
, len
) != len
) {
433 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
434 "error sending crypted data to socket!\n"));
439 if (write_data(rf_sock
->sock
, str
, len
) != len
) {
440 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
441 "error sending data to socket!\n"));
446 static struct refcounted_sock
*sock_list
;
448 static void smb_traffic_analyzer_free_data(void **pptr
)
450 struct refcounted_sock
*rf_sock
= *(struct refcounted_sock
**)pptr
;
451 if (rf_sock
== NULL
) {
454 rf_sock
->ref_count
--;
455 if (rf_sock
->ref_count
!= 0) {
458 if (rf_sock
->sock
!= -1) {
459 close(rf_sock
->sock
);
461 DLIST_REMOVE(sock_list
, rf_sock
);
462 TALLOC_FREE(rf_sock
);
465 static int smb_traffic_analyzer_connect(struct vfs_handle_struct
*handle
,
469 connection_struct
*conn
= handle
->conn
;
470 enum sock_type st
= smb_traffic_analyzer_connMode(handle
);
471 struct refcounted_sock
*rf_sock
= NULL
;
472 const char *name
= (st
== UNIX_DOMAIN_SOCKET
) ? LOCAL_PATHNAME
:
473 lp_parm_const_string(SNUM(conn
),
474 "smb_traffic_analyzer",
475 "host", "localhost");
476 uint16_t port
= (st
== UNIX_DOMAIN_SOCKET
) ? 0 :
477 atoi( lp_parm_const_string(SNUM(conn
),
478 "smb_traffic_analyzer", "port", "9430"));
479 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
485 /* Are we already connected ? */
486 for (rf_sock
= sock_list
; rf_sock
; rf_sock
= rf_sock
->next
) {
487 if (port
== rf_sock
->port
&&
488 (strcmp(name
, rf_sock
->name
) == 0)) {
493 /* If we're connected already, just increase the
494 * reference count. */
496 rf_sock
->ref_count
++;
498 /* New connection. */
499 rf_sock
= TALLOC_ZERO_P(NULL
, struct refcounted_sock
);
500 if (rf_sock
== NULL
) {
501 SMB_VFS_NEXT_DISCONNECT(handle
);
505 rf_sock
->name
= talloc_strdup(rf_sock
, name
);
506 if (rf_sock
->name
== NULL
) {
507 SMB_VFS_NEXT_DISCONNECT(handle
);
508 TALLOC_FREE(rf_sock
);
512 rf_sock
->port
= port
;
513 rf_sock
->ref_count
= 1;
515 if (st
== UNIX_DOMAIN_SOCKET
) {
516 rf_sock
->sock
= smb_traffic_analyzer_connect_unix_socket(handle
,
520 rf_sock
->sock
= smb_traffic_analyzer_connect_inet_socket(handle
,
524 if (rf_sock
->sock
== -1) {
525 SMB_VFS_NEXT_DISCONNECT(handle
);
526 TALLOC_FREE(rf_sock
);
529 DLIST_ADD(sock_list
, rf_sock
);
532 /* Store the private data. */
533 SMB_VFS_HANDLE_SET_DATA(handle
, rf_sock
, smb_traffic_analyzer_free_data
,
534 struct refcounted_sock
, return -1);
539 static int smb_traffic_analyzer_chdir(vfs_handle_struct
*handle
, \
542 struct chdir_data s_data
;
543 s_data
.result
= SMB_VFS_NEXT_CHDIR(handle
, path
);
545 DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path
));
546 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_chdir
);
547 return s_data
.result
;
550 static int smb_traffic_analyzer_rename(vfs_handle_struct
*handle
, \
551 const struct smb_filename
*smb_fname_src
,
552 const struct smb_filename
*smb_fname_dst
)
554 struct rename_data s_data
;
555 s_data
.result
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, \
557 s_data
.src
= smb_fname_src
->base_name
;
558 s_data
.dst
= smb_fname_dst
->base_name
;
559 DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
560 smb_fname_src
->base_name
,
561 smb_fname_dst
->base_name
));
562 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_rename
);
563 return s_data
.result
;
566 static int smb_traffic_analyzer_rmdir(vfs_handle_struct
*handle
, \
569 struct rmdir_data s_data
;
570 s_data
.result
= SMB_VFS_NEXT_RMDIR(handle
, path
);
572 DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path
));
573 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_rmdir
);
574 return s_data
.result
;
577 static int smb_traffic_analyzer_mkdir(vfs_handle_struct
*handle
, \
578 const char *path
, mode_t mode
)
580 struct mkdir_data s_data
;
581 s_data
.result
= SMB_VFS_NEXT_MKDIR(handle
, path
, mode
);
584 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path
));
585 smb_traffic_analyzer_send_data(handle
,
588 return s_data
.result
;
591 static ssize_t
smb_traffic_analyzer_read(vfs_handle_struct
*handle
, \
592 files_struct
*fsp
, void *data
, size_t n
)
594 struct rw_data s_data
;
596 s_data
.len
= SMB_VFS_NEXT_READ(handle
, fsp
, data
, n
);
597 s_data
.filename
= fsp
->fsp_name
->base_name
;
598 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp
)));
600 smb_traffic_analyzer_send_data(handle
,
607 static ssize_t
smb_traffic_analyzer_pread(vfs_handle_struct
*handle
, \
608 files_struct
*fsp
, void *data
, size_t n
, SMB_OFF_T offset
)
610 struct rw_data s_data
;
612 s_data
.len
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
613 s_data
.filename
= fsp
->fsp_name
->base_name
;
614 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
617 smb_traffic_analyzer_send_data(handle
,
624 static ssize_t
smb_traffic_analyzer_write(vfs_handle_struct
*handle
, \
625 files_struct
*fsp
, const void *data
, size_t n
)
627 struct rw_data s_data
;
629 s_data
.len
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, n
);
630 s_data
.filename
= fsp
->fsp_name
->base_name
;
631 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
634 smb_traffic_analyzer_send_data(handle
,
640 static ssize_t
smb_traffic_analyzer_pwrite(vfs_handle_struct
*handle
, \
641 files_struct
*fsp
, const void *data
, size_t n
, SMB_OFF_T offset
)
643 struct rw_data s_data
;
645 s_data
.len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
646 s_data
.filename
= fsp
->fsp_name
->base_name
;
647 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
650 smb_traffic_analyzer_send_data(handle
,
656 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns
= {
657 .connect_fn
= smb_traffic_analyzer_connect
,
658 .vfs_read
= smb_traffic_analyzer_read
,
659 .pread
= smb_traffic_analyzer_pread
,
660 .write
= smb_traffic_analyzer_write
,
661 .pwrite
= smb_traffic_analyzer_pwrite
,
662 .mkdir
= smb_traffic_analyzer_mkdir
,
663 .rename
= smb_traffic_analyzer_rename
,
664 .chdir
= smb_traffic_analyzer_chdir
667 /* Module initialization */
669 NTSTATUS
vfs_smb_traffic_analyzer_init(void)
671 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
672 "smb_traffic_analyzer",
673 &vfs_smb_traffic_analyzer_fns
);
675 if (!NT_STATUS_IS_OK(ret
)) {
679 vfs_smb_traffic_analyzer_debug_level
=
680 debug_add_class("smb_traffic_analyzer");
682 if (vfs_smb_traffic_analyzer_debug_level
== -1) {
683 vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
684 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
685 "debugging class!\n"));
687 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
688 "'smb_traffic_analyzer': %d\n", \
689 vfs_smb_traffic_analyzer_debug_level
));