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. */
43 /* Specific data sets for the VFS functions. */
51 /* rw_data used for read/write/pread/pwrite */
58 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
60 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
62 connection_struct *conn = handle->conn;
64 Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
66 if (strstr(Mode,"unix_domain_socket")) {
67 return UNIX_DOMAIN_SOCKET;
69 return INTERNET_SOCKET;
74 /* Connect to an internet socket */
76 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle,
77 const char *name, uint16_t port)
79 /* Create a streaming Socket */
81 struct addrinfo hints;
82 struct addrinfo *ailist = NULL;
83 struct addrinfo *res = NULL;
87 /* By default make sure it supports TCP. */
88 hints.ai_socktype = SOCK_STREAM;
89 hints.ai_flags = AI_ADDRCONFIG;
91 ret = getaddrinfo(name,
97 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
98 "getaddrinfo failed for name %s [%s]\n",
100 gai_strerror(ret) ));
104 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
105 "Port: %i\n", name, port));
107 for (res = ailist; res; res = res->ai_next) {
108 struct sockaddr_storage ss;
111 if (!res->ai_addr || res->ai_addrlen == 0) {
116 memcpy(&ss, res->ai_addr, res->ai_addrlen);
118 status = open_socket_out(&ss, port, 10000, &sockfd);
119 if (NT_STATUS_IS_OK(status)) {
125 freeaddrinfo(ailist);
129 DEBUG(1, ("smb_traffic_analyzer: unable to create "
130 "socket, error is %s",
138 /* Connect to a unix domain socket */
140 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
143 /* Create the socket to stad */
145 struct sockaddr_un remote;
147 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
148 "Unix domain socket mode. Using %s\n",
151 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
152 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
153 "Couldn't create socket, "
154 "make sure stad is running!\n"));
157 remote.sun_family = AF_UNIX;
158 strlcpy(remote.sun_path, name,
159 sizeof(remote.sun_path));
160 len=strlen(remote.sun_path) + sizeof(remote.sun_family);
161 if (connect(sock, (struct sockaddr *)&remote, len) == -1 ) {
162 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
163 "Could not connect to "
164 "socket, make sure\nstad is running!\n"));
171 /* Private data allowing shared connection sockets. */
173 struct refcounted_sock {
174 struct refcounted_sock *next, *prev;
178 unsigned int ref_count;
181 /* Send data over a socket */
183 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
185 enum vfs_id vfs_operation )
187 struct refcounted_sock *rf_sock = NULL;
190 struct tm *tm = NULL;
193 char *username = NULL;
194 const char *anon_prefix = NULL;
195 const char *total_anonymization = NULL;
196 const char *protocol_version = NULL;
200 SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
202 if (rf_sock == NULL || rf_sock->sock == -1) {
203 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
209 tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
210 tm = localtime(&tv_sec);
214 seconds=(float) (tv.tv_usec / 1000);
216 /* check if anonymization is required */
218 total_anonymization=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",
219 "total_anonymization", NULL);
221 anon_prefix=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",\
222 "anonymize_prefix", NULL );
223 if (anon_prefix!=NULL) {
224 if (total_anonymization!=NULL) {
225 username = talloc_asprintf(talloc_tos(),
229 username = talloc_asprintf(talloc_tos(),
233 handle->conn->server_info->sanitized_username ) );
237 username = handle->conn->server_info->sanitized_username;
244 protocol_version = lp_parm_const_string(SNUM(handle->conn),
245 "smb_traffic_analyzer",
246 "protocol_version", NULL );
248 if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
250 struct rw_data *s_data = (struct rw_data *) data;
252 /* in case of protocol v1, ignore any vfs operations */
253 /* except read,pread,write,pwrite, and set the "Write" */
254 /* bool accordingly. */
256 if ( vfs_operation > vfs_id_pwrite ) return;
258 if ( vfs_operation <= vfs_id_pread ) Write=false;
261 str = talloc_asprintf(talloc_tos(),
262 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
263 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
264 (unsigned int) s_data->len,
266 pdb_get_domain(handle->conn->server_info->sam_account),
268 handle->conn->connectpath,
277 } else if ( strcmp( protocol_version, "V2") == 0) {
278 /* protocol version 2 */
281 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
282 "error, unkown protocol given!\n"));
287 DEBUG(1, ("smb_traffic_analyzer_send_data: "
288 "unable to create string to send!\n"));
294 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
296 if (write_data(rf_sock->sock, str, len) != len) {
297 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
298 "error sending data to socket!\n"));
303 static struct refcounted_sock *sock_list;
305 static void smb_traffic_analyzer_free_data(void **pptr)
307 struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
308 if (rf_sock == NULL) {
311 rf_sock->ref_count--;
312 if (rf_sock->ref_count != 0) {
315 if (rf_sock->sock != -1) {
316 close(rf_sock->sock);
318 DLIST_REMOVE(sock_list, rf_sock);
319 TALLOC_FREE(rf_sock);
322 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
326 connection_struct *conn = handle->conn;
327 enum sock_type st = smb_traffic_analyzer_connMode(handle);
328 struct refcounted_sock *rf_sock = NULL;
329 const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
330 lp_parm_const_string(SNUM(conn),
331 "smb_traffic_analyzer",
332 "host", "localhost");
333 uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
334 atoi( lp_parm_const_string(SNUM(conn),
335 "smb_traffic_analyzer", "port", "9430"));
336 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
342 /* Are we already connected ? */
343 for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
344 if (port == rf_sock->port &&
345 (strcmp(name, rf_sock->name) == 0)) {
350 /* If we're connected already, just increase the
351 * reference count. */
353 rf_sock->ref_count++;
355 /* New connection. */
356 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
357 if (rf_sock == NULL) {
358 SMB_VFS_NEXT_DISCONNECT(handle);
362 rf_sock->name = talloc_strdup(rf_sock, name);
363 if (rf_sock->name == NULL) {
364 SMB_VFS_NEXT_DISCONNECT(handle);
365 TALLOC_FREE(rf_sock);
369 rf_sock->port = port;
370 rf_sock->ref_count = 1;
372 if (st == UNIX_DOMAIN_SOCKET) {
373 rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
377 rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
381 if (rf_sock->sock == -1) {
382 SMB_VFS_NEXT_DISCONNECT(handle);
383 TALLOC_FREE(rf_sock);
386 DLIST_ADD(sock_list, rf_sock);
389 /* Store the private data. */
390 SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
391 struct refcounted_sock, return -1);
397 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
398 const char *path, mode_t mode)
400 struct mkdir_data s_data;
401 s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
404 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
405 smb_traffic_analyzer_send_data(handle,
408 return s_data.result;
411 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
412 files_struct *fsp, void *data, size_t n)
414 struct rw_data s_data;
416 s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
417 s_data.filename = fsp->fsp_name->base_name;
418 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
420 smb_traffic_analyzer_send_data(handle,
427 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
428 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
430 struct rw_data s_data;
432 s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
433 s_data.filename = fsp->fsp_name->base_name;
434 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
437 smb_traffic_analyzer_send_data(handle,
444 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
445 files_struct *fsp, const void *data, size_t n)
447 struct rw_data s_data;
449 s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
450 s_data.filename = fsp->fsp_name->base_name;
451 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
454 smb_traffic_analyzer_send_data(handle,
460 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
461 files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
463 struct rw_data s_data;
465 s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
466 s_data.filename = fsp->fsp_name->base_name;
467 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
470 smb_traffic_analyzer_send_data(handle,
476 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
477 .connect_fn = smb_traffic_analyzer_connect,
478 .vfs_read = smb_traffic_analyzer_read,
479 .pread = smb_traffic_analyzer_pread,
480 .write = smb_traffic_analyzer_write,
481 .pwrite = smb_traffic_analyzer_pwrite,
482 .mkdir = smb_traffic_analyzer_mkdir
485 /* Module initialization */
487 NTSTATUS vfs_smb_traffic_analyzer_init(void)
489 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
490 "smb_traffic_analyzer",
491 &vfs_smb_traffic_analyzer_fns);
493 if (!NT_STATUS_IS_OK(ret)) {
497 vfs_smb_traffic_analyzer_debug_level =
498 debug_add_class("smb_traffic_analyzer");
500 if (vfs_smb_traffic_analyzer_debug_level == -1) {
501 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
502 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
503 "debugging class!\n"));
505 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
506 "'smb_traffic_analyzer': %d\n", \
507 vfs_smb_traffic_analyzer_debug_level));