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
;
46 /* Connect to an internet socket */
48 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct
*handle
,
49 const char *name
, uint16_t port
)
51 /* Create a streaming Socket */
53 struct addrinfo hints
;
54 struct addrinfo
*ailist
= NULL
;
55 struct addrinfo
*res
= NULL
;
59 /* By default make sure it supports TCP. */
60 hints
.ai_socktype
= SOCK_STREAM
;
61 hints
.ai_flags
= AI_ADDRCONFIG
;
63 ret
= getaddrinfo(name
,
69 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
70 "getaddrinfo failed for name %s [%s]\n",
76 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
77 "Port: %i\n", name
, port
));
79 for (res
= ailist
; res
; res
= res
->ai_next
) {
80 struct sockaddr_storage ss
;
82 if (!res
->ai_addr
|| res
->ai_addrlen
== 0) {
87 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
89 sockfd
= open_socket_out(SOCK_STREAM
, &ss
, port
, 10000);
100 DEBUG(1, ("smb_traffic_analyzer: unable to create "
101 "socket, error is %s",
109 /* Connect to a unix domain socket */
111 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct
*handle
,
114 /* Create the socket to stad */
116 struct sockaddr_un remote
;
118 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
119 "Unix domain socket mode. Using %s\n",
122 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
123 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
124 "Couldn't create socket, "
125 "make sure stad is running!\n"));
128 remote
.sun_family
= AF_UNIX
;
129 strlcpy(remote
.sun_path
, name
,
130 sizeof(remote
.sun_path
));
131 len
=strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
132 if (connect(sock
, (struct sockaddr
*)&remote
, len
) == -1 ) {
133 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
134 "Could not connect to "
135 "socket, make sure\nstad is running!\n"));
142 /* Private data allowing shared connection sockets. */
144 struct refcounted_sock
{
145 struct refcounted_sock
*next
, *prev
;
149 unsigned int ref_count
;
152 /* Send data over a socket */
154 static void smb_traffic_analyzer_send_data(vfs_handle_struct
*handle
,
156 const char *file_name
,
159 struct refcounted_sock
*rf_sock
= NULL
;
162 struct tm
*tm
= NULL
;
165 char *username
= NULL
;
166 const char *anon_prefix
= NULL
;
169 SMB_VFS_HANDLE_GET_DATA(handle
, rf_sock
, struct refcounted_sock
, return);
171 if (rf_sock
== NULL
|| rf_sock
->sock
== -1) {
172 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
178 tv_sec
= convert_timespec_to_time_t(convert_timeval_to_timespec(tv
));
179 tm
= localtime(&tv_sec
);
183 seconds
=(float) (tv
.tv_usec
/ 1000);
185 /* check if anonymization is required */
187 anon_prefix
=lp_parm_const_string(SNUM(handle
->conn
),"smb_traffic_analyzer",\
188 "anonymize_prefix", NULL
);
189 if (anon_prefix
!=NULL
) {
190 username
= talloc_asprintf(talloc_tos(),
194 handle
->conn
->server_info
->sanitized_username
) );
196 username
= handle
->conn
->server_info
->sanitized_username
;
203 str
= talloc_asprintf(talloc_tos(),
204 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
205 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
206 (unsigned int)result
,
208 pdb_get_domain(handle
->conn
->server_info
->sam_account
),
210 handle
->conn
->connectpath
,
226 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
228 if (write_data(rf_sock
->sock
, str
, len
) != len
) {
229 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
230 "error sending data to socket!\n"));
235 static struct refcounted_sock
*sock_list
;
237 static void smb_traffic_analyzer_free_data(void **pptr
)
239 struct refcounted_sock
*rf_sock
= *(struct refcounted_sock
**)pptr
;
240 if (rf_sock
== NULL
) {
243 rf_sock
->ref_count
--;
244 if (rf_sock
->ref_count
!= 0) {
247 if (rf_sock
->sock
!= -1) {
248 close(rf_sock
->sock
);
250 DLIST_REMOVE(sock_list
, rf_sock
);
251 TALLOC_FREE(rf_sock
);
254 static int smb_traffic_analyzer_connect(struct vfs_handle_struct
*handle
,
258 connection_struct
*conn
= handle
->conn
;
259 enum sock_type st
= smb_traffic_analyzer_connMode(handle
);
260 struct refcounted_sock
*rf_sock
= NULL
;
261 const char *name
= (st
== UNIX_DOMAIN_SOCKET
) ? LOCAL_PATHNAME
:
262 lp_parm_const_string(SNUM(conn
),
263 "smb_traffic_analyzer",
264 "host", "localhost");
265 uint16_t port
= (st
== UNIX_DOMAIN_SOCKET
) ? 0 :
266 atoi( lp_parm_const_string(SNUM(conn
),
267 "smb_traffic_analyzer", "port", "9430"));
269 /* Are we already connected ? */
270 for (rf_sock
= sock_list
; rf_sock
; rf_sock
= rf_sock
->next
) {
271 if (port
== rf_sock
->port
&&
272 (strcmp(name
, rf_sock
->name
) == 0)) {
277 /* If we're connected already, just increase the
278 * reference count. */
280 rf_sock
->ref_count
++;
282 /* New connection. */
283 rf_sock
= TALLOC_ZERO_P(NULL
, struct refcounted_sock
);
284 if (rf_sock
== NULL
) {
288 rf_sock
->name
= talloc_strdup(rf_sock
, name
);
289 if (rf_sock
->name
== NULL
) {
290 TALLOC_FREE(rf_sock
);
294 rf_sock
->port
= port
;
295 rf_sock
->ref_count
= 1;
297 if (st
== UNIX_DOMAIN_SOCKET
) {
298 rf_sock
->sock
= smb_traffic_analyzer_connect_unix_socket(handle
,
302 rf_sock
->sock
= smb_traffic_analyzer_connect_inet_socket(handle
,
306 if (rf_sock
->sock
== -1) {
307 TALLOC_FREE(rf_sock
);
310 DLIST_ADD(sock_list
, rf_sock
);
313 /* Store the private data. */
314 SMB_VFS_HANDLE_SET_DATA(handle
, rf_sock
, smb_traffic_analyzer_free_data
,
315 struct refcounted_sock
, return -1);
316 return SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
319 /* VFS Functions: write, read, pread, pwrite for now */
321 static ssize_t
smb_traffic_analyzer_read(vfs_handle_struct
*handle
, \
322 files_struct
*fsp
, void *data
, size_t n
)
326 result
= SMB_VFS_NEXT_READ(handle
, fsp
, data
, n
);
327 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp
->fsp_name
));
329 smb_traffic_analyzer_send_data(handle
,
337 static ssize_t
smb_traffic_analyzer_pread(vfs_handle_struct
*handle
, \
338 files_struct
*fsp
, void *data
, size_t n
, SMB_OFF_T offset
)
342 result
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
344 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n", fsp
->fsp_name
));
346 smb_traffic_analyzer_send_data(handle
,
354 static ssize_t
smb_traffic_analyzer_write(vfs_handle_struct
*handle
, \
355 files_struct
*fsp
, const void *data
, size_t n
)
359 result
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, n
);
361 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n", fsp
->fsp_name
));
363 smb_traffic_analyzer_send_data(handle
,
370 static ssize_t
smb_traffic_analyzer_pwrite(vfs_handle_struct
*handle
, \
371 files_struct
*fsp
, const void *data
, size_t n
, SMB_OFF_T offset
)
375 result
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
377 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp
->fsp_name
));
379 smb_traffic_analyzer_send_data(handle
,
386 /* VFS operations we use */
388 static vfs_op_tuple smb_traffic_analyzer_tuples
[] = {
390 {SMB_VFS_OP(smb_traffic_analyzer_connect
), SMB_VFS_OP_CONNECT
,
391 SMB_VFS_LAYER_LOGGER
},
392 {SMB_VFS_OP(smb_traffic_analyzer_read
), SMB_VFS_OP_READ
,
393 SMB_VFS_LAYER_LOGGER
},
394 {SMB_VFS_OP(smb_traffic_analyzer_pread
), SMB_VFS_OP_PREAD
,
395 SMB_VFS_LAYER_LOGGER
},
396 {SMB_VFS_OP(smb_traffic_analyzer_write
), SMB_VFS_OP_WRITE
,
397 SMB_VFS_LAYER_LOGGER
},
398 {SMB_VFS_OP(smb_traffic_analyzer_pwrite
), SMB_VFS_OP_PWRITE
,
399 SMB_VFS_LAYER_LOGGER
},
400 {SMB_VFS_OP(NULL
),SMB_VFS_OP_NOOP
,SMB_VFS_LAYER_NOOP
}
403 /* Module initialization */
405 NTSTATUS
vfs_smb_traffic_analyzer_init(void)
407 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, \
408 "smb_traffic_analyzer", smb_traffic_analyzer_tuples
);
410 if (!NT_STATUS_IS_OK(ret
)) {
414 vfs_smb_traffic_analyzer_debug_level
=
415 debug_add_class("smb_traffic_analyzer");
417 if (vfs_smb_traffic_analyzer_debug_level
== -1) {
418 vfs_smb_traffic_analyzer_debug_level
= DBGC_VFS
;
419 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
420 "debugging class!\n"));
422 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
423 "'smb_traffic_analyzer': %d\n", \
424 vfs_smb_traffic_analyzer_debug_level
));