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 */
76 static int vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
78 static enum sock_type
smb_traffic_analyzer_connMode(vfs_handle_struct
*handle
)
80 connection_struct
*conn
= handle
->conn
;
82 Mode
=lp_parm_const_string(SNUM(conn
), "smb_traffic_analyzer","mode", \
84 if (strstr(Mode
,"unix_domain_socket")) {
85 return UNIX_DOMAIN_SOCKET
;
87 return INTERNET_SOCKET
;
92 /* Connect to an internet socket */
94 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct
*handle
,
95 const char *name
, uint16_t port
)
97 /* Create a streaming Socket */
99 struct addrinfo hints
;
100 struct addrinfo
*ailist
= NULL
;
101 struct addrinfo
*res
= NULL
;
105 /* By default make sure it supports TCP. */
106 hints
.ai_socktype
= SOCK_STREAM
;
107 hints
.ai_flags
= AI_ADDRCONFIG
;
109 ret
= getaddrinfo(name
,
115 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
116 "getaddrinfo failed for name %s [%s]\n",
118 gai_strerror(ret
) ));
122 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
123 "Port: %i\n", name
, port
));
125 for (res
= ailist
; res
; res
= res
->ai_next
) {
126 struct sockaddr_storage ss
;
129 if (!res
->ai_addr
|| res
->ai_addrlen
== 0) {
134 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
136 status
= open_socket_out(&ss
, port
, 10000, &sockfd
);
137 if (NT_STATUS_IS_OK(status
)) {
143 freeaddrinfo(ailist
);
147 DEBUG(1, ("smb_traffic_analyzer: unable to create "
148 "socket, error is %s",
156 /* Connect to a unix domain socket */
158 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct
*handle
,
161 /* Create the socket to stad */
163 struct sockaddr_un remote
;
165 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
166 "Unix domain socket mode. Using %s\n",
169 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
170 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
171 "Couldn't create socket, "
172 "make sure stad is running!\n"));
175 remote
.sun_family
= AF_UNIX
;
176 strlcpy(remote
.sun_path
, name
,
177 sizeof(remote
.sun_path
));
178 len
=strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
179 if (connect(sock
, (struct sockaddr
*)&remote
, len
) == -1 ) {
180 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
181 "Could not connect to "
182 "socket, make sure\nstad is running!\n"));
189 /* Private data allowing shared connection sockets. */
191 struct refcounted_sock
{
192 struct refcounted_sock
*next
, *prev
;
196 unsigned int ref_count
;
200 /* The marshaller for the protocol version 2. */
201 static char *smb_traffic_analyzer_create_string( struct tm
*tm
, \
202 int seconds
, vfs_handle_struct
*handle
, \
203 char *username
, int vfs_operation
, int count
, ... )
212 char *timestr
= NULL
;
214 char *userSID
= 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 userSID
= dom_sid_string( talloc_tos(),
223 &handle
->conn
->server_info
->ptok
->user_sids
[0]);
224 len
= strlen( userSID
);
225 buf
= talloc_asprintf_append(buf
, "%04u%s", len
, userSID
);
226 len
= strlen( handle
->conn
->connectpath
);
227 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, \
228 handle
->conn
->connectpath
);
229 len
= strlen( pdb_get_domain(handle
->conn
->server_info
->sam_account
) );
230 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, \
231 pdb_get_domain(handle
->conn
->server_info
->sam_account
) );
232 timestr
= talloc_asprintf(talloc_tos(), \
233 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
241 len
= strlen( timestr
);
242 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, timestr
);
244 va_start( ap
, count
);
246 arg
= va_arg( ap
, char * );
247 /* protocol v2 sends a four byte string */
248 /* as a header to each block, including */
249 /* the numbers of bytes to come in the */
252 buf
= talloc_asprintf_append( buf
, "%04u%s", len
, arg
);
256 /* now create the protocol v2 header. */
258 str
= talloc_asprintf_append( str
, "V2,%017u%s", len
, buf
);
259 DEBUG(10, ("smb_traffic_analyzer_create_string: %s\n",str
));
263 static void smb_traffic_analyzer_send_data(vfs_handle_struct
*handle
,
265 enum vfs_id vfs_operation
)
267 struct refcounted_sock
*rf_sock
= NULL
;
270 struct tm
*tm
= NULL
;
273 char *username
= NULL
;
274 const char *anon_prefix
= NULL
;
275 const char *total_anonymization
= NULL
;
276 const char *protocol_version
= NULL
;
280 SMB_VFS_HANDLE_GET_DATA(handle
, rf_sock
, struct refcounted_sock
, return);
282 if (rf_sock
== NULL
|| rf_sock
->sock
== -1) {
283 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
289 tv_sec
= convert_timespec_to_time_t(convert_timeval_to_timespec(tv
));
290 tm
= localtime(&tv_sec
);
294 seconds
=(float) (tv
.tv_usec
/ 1000);
296 /* check if anonymization is required */
298 total_anonymization
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",
299 "total_anonymization", NULL
);
301 anon_prefix
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",\
302 "anonymize_prefix", NULL
);
303 if (anon_prefix
!=NULL
) {
304 if (total_anonymization
!=NULL
) {
305 username
= talloc_asprintf(talloc_tos(),
309 username
= talloc_asprintf(talloc_tos(),
313 handle
->conn
->server_info
->sanitized_username
) );
317 username
= handle
->conn
->server_info
->sanitized_username
;
323 protocol_version
= lp_parm_const_string(SNUM(handle
->conn
),
324 "smb_traffic_analyzer",
325 "protocol_version", NULL
);
327 if ( protocol_version
== NULL
|| strcmp( protocol_version
,"V1") == 0) {
329 struct rw_data
*s_data
= (struct rw_data
*) data
;
331 /* in case of protocol v1, ignore any vfs operations */
332 /* except read,pread,write,pwrite, and set the "Write" */
333 /* bool accordingly. */
335 if ( vfs_operation
> vfs_id_pwrite
) return;
337 if ( vfs_operation
<= vfs_id_pread
) Write
=false;
340 str
= talloc_asprintf(talloc_tos(),
341 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
342 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
343 (unsigned int) s_data
->len
,
345 pdb_get_domain(handle
->conn
->server_info
->sam_account
),
347 handle
->conn
->connectpath
,
356 } else if ( strcmp( protocol_version
, "V2") == 0) {
358 switch( vfs_operation
) {
360 str
= smb_traffic_analyzer_create_string( tm
, \
361 seconds
, handle
, username
, vfs_id_mkdir
, 3,\
362 ((struct mkdir_data
*) data
)->path
, \
363 talloc_asprintf( talloc_tos(), "%u", \
364 ((struct mkdir_data
*) data
)->mode
), \
365 talloc_asprintf( talloc_tos(), "%u", \
366 ((struct mkdir_data
*) data
)->result
));
369 str
= smb_traffic_analyzer_create_string( tm
, \
370 seconds
, handle
, username
, vfs_id_rmdir
, 2,\
371 ((struct rmdir_data
*) data
)->path
, \
372 talloc_asprintf( talloc_tos(), "%u", \
373 ((struct rmdir_data
*) data
)->result
));
375 case vfs_id_rename
: ;
376 str
= smb_traffic_analyzer_create_string( tm
, \
377 seconds
, handle
, username
, vfs_id_rename
, 3,\
378 ((struct rename_data
*) data
)->src
, \
379 ((struct rename_data
*) data
)->dst
,
380 talloc_asprintf(talloc_tos(), "%u", \
381 ((struct rename_data
*) data
)->result
));
384 str
= smb_traffic_analyzer_create_string( tm
, \
385 seconds
, handle
, username
, vfs_id_chdir
, 2,\
386 ((struct chdir_data
*) data
)->path
, \
387 talloc_asprintf(talloc_tos(), "%u", \
388 ((struct chdir_data
*) data
)->result
));
395 str
= smb_traffic_analyzer_create_string( tm
, \
396 seconds
, handle
, username
, vfs_operation
, 2,\
397 ((struct rw_data
*) data
)->filename
, \
398 talloc_asprintf(talloc_tos(), "%u", \
399 ((struct rw_data
*) data
)->len
));
402 DEBUG(1, ("smb_traffic_analyzer: error! "
403 "wrong VFS operation id detected!\n"));
408 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
409 "error, unkown protocol given!\n"));
414 DEBUG(1, ("smb_traffic_analyzer_send_data: "
415 "unable to create string to send!\n"));
421 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: going to send "
423 /* If configured, optain the key and run AES encryption */
426 char *akey
= secrets_fetch("smb_traffic_analyzer_key", &size
);
427 if ( akey
!= NULL
) {
429 DEBUG(10, ("smb_traffic_analyzer: a key was found, encrypting "
432 samba_AES_set_encrypt_key(akey
, 128, key
);
433 samba_AES_encrypt( str
, crypted
, key
);
434 len
= strlen( crypted
);
435 if (write_data(rf_sock
->sock
, crypted
, len
) != len
) {
436 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
437 "error sending crypted data to socket!\n"));
442 if (write_data(rf_sock
->sock
, str
, len
) != len
) {
443 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
444 "error sending data to socket!\n"));
449 static struct refcounted_sock
*sock_list
;
451 static void smb_traffic_analyzer_free_data(void **pptr
)
453 struct refcounted_sock
*rf_sock
= *(struct refcounted_sock
**)pptr
;
454 if (rf_sock
== NULL
) {
457 rf_sock
->ref_count
--;
458 if (rf_sock
->ref_count
!= 0) {
461 if (rf_sock
->sock
!= -1) {
462 close(rf_sock
->sock
);
464 DLIST_REMOVE(sock_list
, rf_sock
);
465 TALLOC_FREE(rf_sock
);
468 static int smb_traffic_analyzer_connect(struct vfs_handle_struct
*handle
,
472 connection_struct
*conn
= handle
->conn
;
473 enum sock_type st
= smb_traffic_analyzer_connMode(handle
);
474 struct refcounted_sock
*rf_sock
= NULL
;
475 const char *name
= (st
== UNIX_DOMAIN_SOCKET
) ? LOCAL_PATHNAME
:
476 lp_parm_const_string(SNUM(conn
),
477 "smb_traffic_analyzer",
478 "host", "localhost");
479 uint16_t port
= (st
== UNIX_DOMAIN_SOCKET
) ? 0 :
480 atoi( lp_parm_const_string(SNUM(conn
),
481 "smb_traffic_analyzer", "port", "9430"));
482 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
488 /* Are we already connected ? */
489 for (rf_sock
= sock_list
; rf_sock
; rf_sock
= rf_sock
->next
) {
490 if (port
== rf_sock
->port
&&
491 (strcmp(name
, rf_sock
->name
) == 0)) {
496 /* If we're connected already, just increase the
497 * reference count. */
499 rf_sock
->ref_count
++;
501 /* New connection. */
502 rf_sock
= TALLOC_ZERO_P(NULL
, struct refcounted_sock
);
503 if (rf_sock
== NULL
) {
504 SMB_VFS_NEXT_DISCONNECT(handle
);
508 rf_sock
->name
= talloc_strdup(rf_sock
, name
);
509 if (rf_sock
->name
== NULL
) {
510 SMB_VFS_NEXT_DISCONNECT(handle
);
511 TALLOC_FREE(rf_sock
);
515 rf_sock
->port
= port
;
516 rf_sock
->ref_count
= 1;
518 if (st
== UNIX_DOMAIN_SOCKET
) {
519 rf_sock
->sock
= smb_traffic_analyzer_connect_unix_socket(handle
,
523 rf_sock
->sock
= smb_traffic_analyzer_connect_inet_socket(handle
,
527 if (rf_sock
->sock
== -1) {
528 SMB_VFS_NEXT_DISCONNECT(handle
);
529 TALLOC_FREE(rf_sock
);
532 DLIST_ADD(sock_list
, rf_sock
);
535 /* Store the private data. */
536 SMB_VFS_HANDLE_SET_DATA(handle
, rf_sock
, smb_traffic_analyzer_free_data
,
537 struct refcounted_sock
, return -1);
542 static int smb_traffic_analyzer_chdir(vfs_handle_struct
*handle
, \
545 struct chdir_data s_data
;
546 s_data
.result
= SMB_VFS_NEXT_CHDIR(handle
, path
);
548 DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path
));
549 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_chdir
);
550 return s_data
.result
;
553 static int smb_traffic_analyzer_rename(vfs_handle_struct
*handle
, \
554 const struct smb_filename
*smb_fname_src
,
555 const struct smb_filename
*smb_fname_dst
)
557 struct rename_data s_data
;
558 s_data
.result
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, \
560 s_data
.src
= smb_fname_src
->base_name
;
561 s_data
.dst
= smb_fname_dst
->base_name
;
562 DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
563 smb_fname_src
->base_name
,
564 smb_fname_dst
->base_name
));
565 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_rename
);
566 return s_data
.result
;
569 static int smb_traffic_analyzer_rmdir(vfs_handle_struct
*handle
, \
572 struct rmdir_data s_data
;
573 s_data
.result
= SMB_VFS_NEXT_RMDIR(handle
, path
);
575 DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path
));
576 smb_traffic_analyzer_send_data(handle
, &s_data
, vfs_id_rmdir
);
577 return s_data
.result
;
580 static int smb_traffic_analyzer_mkdir(vfs_handle_struct
*handle
, \
581 const char *path
, mode_t mode
)
583 struct mkdir_data s_data
;
584 s_data
.result
= SMB_VFS_NEXT_MKDIR(handle
, path
, mode
);
587 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path
));
588 smb_traffic_analyzer_send_data(handle
,
591 return s_data
.result
;
594 static ssize_t
smb_traffic_analyzer_read(vfs_handle_struct
*handle
, \
595 files_struct
*fsp
, void *data
, size_t n
)
597 struct rw_data s_data
;
599 s_data
.len
= SMB_VFS_NEXT_READ(handle
, fsp
, data
, n
);
600 s_data
.filename
= fsp
->fsp_name
->base_name
;
601 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp
)));
603 smb_traffic_analyzer_send_data(handle
,
610 static ssize_t
smb_traffic_analyzer_pread(vfs_handle_struct
*handle
, \
611 files_struct
*fsp
, void *data
, size_t n
, SMB_OFF_T offset
)
613 struct rw_data s_data
;
615 s_data
.len
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
616 s_data
.filename
= fsp
->fsp_name
->base_name
;
617 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
620 smb_traffic_analyzer_send_data(handle
,
627 static ssize_t
smb_traffic_analyzer_write(vfs_handle_struct
*handle
, \
628 files_struct
*fsp
, const void *data
, size_t n
)
630 struct rw_data s_data
;
632 s_data
.len
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, n
);
633 s_data
.filename
= fsp
->fsp_name
->base_name
;
634 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
637 smb_traffic_analyzer_send_data(handle
,
643 static ssize_t
smb_traffic_analyzer_pwrite(vfs_handle_struct
*handle
, \
644 files_struct
*fsp
, const void *data
, size_t n
, SMB_OFF_T offset
)
646 struct rw_data s_data
;
648 s_data
.len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
649 s_data
.filename
= fsp
->fsp_name
->base_name
;
650 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
653 smb_traffic_analyzer_send_data(handle
,
659 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns
= {
660 .connect_fn
= smb_traffic_analyzer_connect
,
661 .vfs_read
= smb_traffic_analyzer_read
,
662 .pread
= smb_traffic_analyzer_pread
,
663 .write
= smb_traffic_analyzer_write
,
664 .pwrite
= smb_traffic_analyzer_pwrite
,
665 .mkdir
= smb_traffic_analyzer_mkdir
,
666 .rename
= smb_traffic_analyzer_rename
,
667 .chdir
= smb_traffic_analyzer_chdir
670 /* Module initialization */
672 NTSTATUS
vfs_smb_traffic_analyzer_init(void)
674 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
675 "smb_traffic_analyzer",
676 &vfs_smb_traffic_analyzer_fns
);
678 if (!NT_STATUS_IS_OK(ret
)) {
682 vfs_smb_traffic_analyzer_debug_level
=
683 debug_add_class("smb_traffic_analyzer");
685 if (vfs_smb_traffic_analyzer_debug_level
== -1) {
686 vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
687 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
688 "debugging class!\n"));
690 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
691 "'smb_traffic_analyzer': %d\n", \
692 vfs_smb_traffic_analyzer_debug_level
));