vfs_smb_traffic_analyzer: fix off by a second bug
[Samba/ita.git] / source3 / modules / vfs_smb_traffic_analyzer.c
blob53ea5f9339dffcf37f75c2a8dcd1f84e00fa21fa
1 /*
2 * traffic-analyzer VFS module. Measure the smb traffic users create
3 * on the net.
5 * Copyright (C) Holger Hetterich, 2008-2010
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/>.
22 #include "includes.h"
23 #include "../lib/crypto/crypto.h"
24 #include "vfs_smb_traffic_analyzer.h"
25 #include "../libcli/security/dom_sid.h"
26 #include "secrets.h"
27 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 /* abstraction for the send_over_network function */
30 enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};
32 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
34 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
36 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
38 connection_struct *conn = handle->conn;
39 const char *Mode;
40 Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
41 "internet_socket");
42 if (strstr(Mode,"unix_domain_socket")) {
43 return UNIX_DOMAIN_SOCKET;
44 } else {
45 return INTERNET_SOCKET;
50 /* Connect to an internet socket */
51 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle,
52 const char *name, uint16_t port)
54 /* Create a streaming Socket */
55 int sockfd = -1;
56 struct addrinfo hints;
57 struct addrinfo *ailist = NULL;
58 struct addrinfo *res = NULL;
59 int ret;
61 ZERO_STRUCT(hints);
62 /* By default make sure it supports TCP. */
63 hints.ai_socktype = SOCK_STREAM;
64 hints.ai_flags = AI_ADDRCONFIG;
66 ret = getaddrinfo(name,
67 NULL,
68 &hints,
69 &ailist);
71 if (ret) {
72 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
73 "getaddrinfo failed for name %s [%s]\n",
74 name,
75 gai_strerror(ret) ));
76 return -1;
79 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
80 "Port: %i\n", name, port));
82 for (res = ailist; res; res = res->ai_next) {
83 struct sockaddr_storage ss;
84 NTSTATUS status;
86 if (!res->ai_addr || res->ai_addrlen == 0) {
87 continue;
90 ZERO_STRUCT(ss);
91 memcpy(&ss, res->ai_addr, res->ai_addrlen);
93 status = open_socket_out(&ss, port, 10000, &sockfd);
94 if (NT_STATUS_IS_OK(status)) {
95 break;
99 if (ailist) {
100 freeaddrinfo(ailist);
103 if (sockfd == -1) {
104 DEBUG(1, ("smb_traffic_analyzer: unable to create "
105 "socket, error is %s",
106 strerror(errno)));
107 return -1;
110 return sockfd;
113 /* Connect to a unix domain socket */
114 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
115 const char *name)
117 /* Create the socket to stad */
118 int len, sock;
119 struct sockaddr_un remote;
121 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
122 "Unix domain socket mode. Using %s\n",
123 name ));
125 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
126 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
127 "Couldn't create socket, "
128 "make sure stad is running!\n"));
129 return -1;
131 remote.sun_family = AF_UNIX;
132 strlcpy(remote.sun_path, name,
133 sizeof(remote.sun_path));
134 len=strlen(remote.sun_path) + sizeof(remote.sun_family);
135 if (connect(sock, (struct sockaddr *)&remote, len) == -1 ) {
136 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
137 "Could not connect to "
138 "socket, make sure\nstad is running!\n"));
139 close(sock);
140 return -1;
142 return sock;
145 /* Private data allowing shared connection sockets. */
146 struct refcounted_sock {
147 struct refcounted_sock *next, *prev;
148 char *name;
149 uint16_t port;
150 int sock;
151 unsigned int ref_count;
156 * Encryption of a data block with AES
157 * TALLOC_CTX *ctx Talloc context to work on
158 * const char *akey 128bit key for the encryption
159 * const char *str Data buffer to encrypt, \0 terminated
160 * int *len Will be set to the length of the
161 * resulting data block
162 * The caller has to take care for the memory
163 * allocated on the context.
165 static char *smb_traffic_analyzer_encrypt( TALLOC_CTX *ctx,
166 const char *akey, const char *str, size_t *len)
168 int s1,s2,h,d;
169 AES_KEY key;
170 unsigned char filler[17]= "................";
171 char *output;
172 unsigned char crypted[18];
173 if (akey == NULL) return NULL;
174 samba_AES_set_encrypt_key((unsigned char *) akey, 128, &key);
175 s1 = strlen(str) / 16;
176 s2 = strlen(str) % 16;
177 for (h = 0; h < s2; h++) *(filler+h)=*(str+(s1*16)+h);
178 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: created %s"
179 " as filling block.\n", filler));
180 output = talloc_array(ctx, char, (s1*16)+17 );
181 d=0;
182 for (h = 0; h < s1; h++) {
183 samba_AES_encrypt((unsigned char *) str+(16*h), crypted, &key);
184 for (d = 0; d<16; d++) output[d+(16*h)]=crypted[d];
186 samba_AES_encrypt( (unsigned char *) str+(16*h), filler, &key );
187 for (d = 0;d < 16; d++) output[d+(16*h)]=*(filler+d);
188 *len = (s1*16)+16;
189 return output;
193 * Create a v2 header.
194 * TALLLOC_CTX *ctx Talloc context to work on
195 * const char *state_flags State flag string
196 * int len length of the data block
198 static char *smb_traffic_analyzer_create_header( TALLOC_CTX *ctx,
199 const char *state_flags, size_t data_len)
201 char *header = talloc_asprintf( ctx, "V2.%s%017u",
202 state_flags, (unsigned int) data_len);
203 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: created Header:\n"));
204 dump_data(10, (uint8_t *)header, strlen(header));
205 return header;
210 * Actually send header and data over the network
211 * char *header Header data
212 * char *data Data Block
213 * int dlength Length of data block
214 * int socket
216 static void smb_traffic_analyzer_write_data( char *header, char *data,
217 int dlength, int _socket)
219 int len = strlen(header);
220 if (write_data( _socket, header, len) != len) {
221 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
222 "error sending the header"
223 " over the socket!\n"));
225 DEBUG(10,("smb_traffic_analyzer_write_data: sending data:\n"));
226 dump_data( 10, (uint8_t *)data, dlength);
228 if (write_data( _socket, data, dlength) != dlength) {
229 DEBUG(1, ("smb_traffic_analyzer_write_data: "
230 "error sending crypted data to socket!\n"));
236 * Anonymize a string if required.
237 * TALLOC_CTX *ctx The talloc context to work on
238 * const char *str The string to anonymize
239 * vfs_handle_struct *handle The handle struct to work on
241 * Returns a newly allocated string, either the anonymized one,
242 * or a copy of const char *str. The caller has to take care for
243 * freeing the allocated memory.
245 static char *smb_traffic_analyzer_anonymize( TALLOC_CTX *ctx,
246 const char *str,
247 vfs_handle_struct *handle )
249 const char *total_anonymization;
250 const char *anon_prefix;
251 char *output;
252 total_anonymization=lp_parm_const_string(SNUM(handle->conn),
253 "smb_traffic_analyzer",
254 "total_anonymization", NULL);
256 anon_prefix=lp_parm_const_string(SNUM(handle->conn),
257 "smb_traffic_analyzer",
258 "anonymize_prefix", NULL );
259 if (anon_prefix != NULL) {
260 if (total_anonymization != NULL) {
261 output = talloc_asprintf(ctx, "%s",
262 anon_prefix);
263 } else {
264 output = talloc_asprintf(ctx, "%s%i", anon_prefix,
265 str_checksum(str));
267 } else {
268 output = talloc_asprintf(ctx, "%s", str);
271 return output;
276 * The marshalling function for protocol v2.
277 * TALLOC_CTX *ctx Talloc context to work on
278 * struct tm *tm tm struct for the timestamp
279 * int seconds milliseconds of the timestamp
280 * vfs_handle_struct *handle vfs_handle_struct
281 * char *username Name of the user
282 * int vfs_operation VFS operation identifier
283 * int count Number of the common data blocks
284 * [...] variable args data blocks taken from the individual
285 * VFS data structures
287 * Returns the complete data block to send. The caller has to
288 * take care for freeing the allocated buffer.
290 static char *smb_traffic_analyzer_create_string( TALLOC_CTX *ctx,
291 struct tm *tm, int seconds, vfs_handle_struct *handle, \
292 char *username, int vfs_operation, int count, ... )
295 va_list ap;
296 char *arg = NULL;
297 int len;
298 char *common_data_count_str = NULL;
299 char *timestr = NULL;
300 char *sidstr = NULL;
301 char *usersid = NULL;
302 char *buf = NULL;
303 char *vfs_operation_str = NULL;
304 const char *service_name = lp_const_servicename(handle->conn->params->service);
307 * first create the data that is transfered with any VFS op
308 * These are, in the following order:
309 *(0) number of data to come [6 in v2.0]
310 * 1.vfs_operation identifier
311 * 2.username
312 * 3.user-SID
313 * 4.affected share
314 * 5.domain
315 * 6.timestamp
319 * number of common data blocks to come,
320 * this is a #define in vfs_smb_traffic_anaylzer.h,
321 * it's length is known at compile time
323 common_data_count_str = talloc_strdup( ctx, SMBTA_COMMON_DATA_COUNT);
324 /* vfs operation identifier */
325 vfs_operation_str = talloc_asprintf( common_data_count_str, "%i",
326 vfs_operation);
328 * Handle anonymization. In protocol v2, we have to anonymize
329 * both the SID and the username. The name is already
330 * anonymized if needed, by the calling function.
332 usersid = dom_sid_string( common_data_count_str,
333 &handle->conn->server_info->ptok->user_sids[0]);
335 sidstr = smb_traffic_analyzer_anonymize(
336 common_data_count_str,
337 usersid,
338 handle);
340 /* time stamp */
341 timestr = talloc_asprintf( common_data_count_str, \
342 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
343 tm->tm_year+1900, \
344 tm->tm_mon+1, \
345 tm->tm_mday, \
346 tm->tm_hour, \
347 tm->tm_min, \
348 tm->tm_sec, \
349 (int)seconds);
350 len = strlen( timestr );
352 /* create the string of common data */
353 buf = talloc_asprintf(ctx,
354 "%s%04u%s%04u%s%04u%s%04u%s%04u%s%04u%s",
355 common_data_count_str,
356 (unsigned int) strlen(vfs_operation_str),
357 vfs_operation_str,
358 (unsigned int) strlen(username),
359 username,
360 (unsigned int) strlen(sidstr),
361 sidstr,
362 (unsigned int) strlen(service_name),
363 service_name,
364 (unsigned int)
365 strlen(handle->conn->server_info->info3->base.domain.string),
366 handle->conn->server_info->info3->base.domain.string,
367 (unsigned int) strlen(timestr),
368 timestr);
370 talloc_free(common_data_count_str);
372 /* data blocks depending on the VFS function */
373 va_start( ap, count );
374 while ( count-- ) {
375 arg = va_arg( ap, char * );
377 * protocol v2 sends a four byte string
378 * as a header to each block, including
379 * the numbers of bytes to come in the
380 * next string.
382 len = strlen( arg );
383 buf = talloc_asprintf_append( buf, "%04u%s", len, arg);
385 va_end( ap );
386 return buf;
389 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
390 void *data,
391 enum vfs_id vfs_operation )
393 struct refcounted_sock *rf_sock = NULL;
394 struct timeval tv;
395 time_t tv_sec;
396 struct tm *tm = NULL;
397 int seconds;
398 char *str = NULL;
399 char *username = NULL;
400 char *header = NULL;
401 const char *protocol_version = NULL;
402 bool Write = false;
403 size_t len;
404 size_t size;
405 char *akey, *output;
408 * The state flags are part of the header
409 * and are descripted in the protocol description
410 * in vfs_smb_traffic_analyzer.h. They begin at byte
411 * 03 of the header.
413 char state_flags[9] = "000000\0";
415 SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
417 if (rf_sock == NULL || rf_sock->sock == -1) {
418 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
419 "closed\n"));
420 return;
423 GetTimeOfDay(&tv);
424 tv_sec = tv.tv_sec;
425 tm = localtime(&tv_sec);
426 if (!tm) {
427 return;
429 seconds=(float) (tv.tv_usec / 1000);
432 * Check if anonymization is required, and if yes do this only for
433 * the username here, needed vor protocol version 1. In v2 we
434 * additionally anonymize the SID, which is done in it's marshalling
435 * function.
437 username = smb_traffic_analyzer_anonymize( talloc_tos(),
438 handle->conn->server_info->sanitized_username,
439 handle);
441 if (!username) {
442 return;
445 protocol_version = lp_parm_const_string(SNUM(handle->conn),
446 "smb_traffic_analyzer",
447 "protocol_version", NULL );
450 if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
452 struct rw_data *s_data = (struct rw_data *) data;
455 * in case of protocol v1, ignore any vfs operations
456 * except read,pread,write,pwrite, and set the "Write"
457 * bool accordingly, send data and return.
459 if ( vfs_operation > vfs_id_pwrite ) return;
461 if ( vfs_operation <= vfs_id_pread ) Write=false;
462 else Write=true;
464 str = talloc_asprintf(talloc_tos(),
465 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
466 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
467 (unsigned int) s_data->len,
468 username,
469 handle->conn->server_info->info3->base.domain.string,
470 Write ? 'W' : 'R',
471 handle->conn->connectpath,
472 s_data->filename,
473 tm->tm_year+1900,
474 tm->tm_mon+1,
475 tm->tm_mday,
476 tm->tm_hour,
477 tm->tm_min,
478 tm->tm_sec,
479 (int)seconds);
480 len = strlen(str);
481 if (write_data(rf_sock->sock, str, len) != len) {
482 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
483 "error sending V1 protocol data to socket!\n"));
484 return;
487 } else if ( strcmp( protocol_version, "V2") == 0) {
489 switch( vfs_operation ) {
490 case vfs_id_open: ;
491 str = smb_traffic_analyzer_create_string( talloc_tos(),
492 tm, seconds, handle, username, vfs_id_open,
493 3, ((struct open_data *) data)->filename,
494 talloc_asprintf( talloc_tos(), "%u",
495 ((struct open_data *) data)->mode),
496 talloc_asprintf( talloc_tos(), "%u",
497 ((struct open_data *) data)->result));
498 break;
499 case vfs_id_close: ;
500 str = smb_traffic_analyzer_create_string( talloc_tos(),
501 tm, seconds, handle, username, vfs_id_close,
502 2, ((struct close_data *) data)->filename,
503 talloc_asprintf( talloc_tos(), "%u",
504 ((struct close_data *) data)->result));
505 break;
506 case vfs_id_mkdir: ;
507 str = smb_traffic_analyzer_create_string( talloc_tos(),
508 tm, seconds, handle, username, vfs_id_mkdir, \
509 3, ((struct mkdir_data *) data)->path, \
510 talloc_asprintf( talloc_tos(), "%u", \
511 ((struct mkdir_data *) data)->mode), \
512 talloc_asprintf( talloc_tos(), "%u", \
513 ((struct mkdir_data *) data)->result ));
514 break;
515 case vfs_id_rmdir: ;
516 str = smb_traffic_analyzer_create_string( talloc_tos(),
517 tm, seconds, handle, username, vfs_id_rmdir,
518 2, ((struct rmdir_data *) data)->path, \
519 talloc_asprintf( talloc_tos(), "%u", \
520 ((struct rmdir_data *) data)->result ));
521 break;
522 case vfs_id_rename: ;
523 str = smb_traffic_analyzer_create_string( talloc_tos(),
524 tm, seconds, handle, username, vfs_id_rename,
525 3, ((struct rename_data *) data)->src, \
526 ((struct rename_data *) data)->dst,
527 talloc_asprintf(talloc_tos(), "%u", \
528 ((struct rename_data *) data)->result));
529 break;
530 case vfs_id_chdir: ;
531 str = smb_traffic_analyzer_create_string( talloc_tos(),
532 tm, seconds, handle, username, vfs_id_chdir,
533 2, ((struct chdir_data *) data)->path, \
534 talloc_asprintf(talloc_tos(), "%u", \
535 ((struct chdir_data *) data)->result));
536 break;
538 case vfs_id_write:
539 case vfs_id_pwrite:
540 case vfs_id_read:
541 case vfs_id_pread: ;
542 str = smb_traffic_analyzer_create_string( talloc_tos(),
543 tm, seconds, handle, username, vfs_operation,
544 2, ((struct rw_data *) data)->filename, \
545 talloc_asprintf(talloc_tos(), "%u", \
546 (unsigned int)
547 ((struct rw_data *) data)->len));
548 break;
549 default:
550 DEBUG(1, ("smb_traffic_analyzer: error! "
551 "wrong VFS operation id detected!\n"));
552 return;
555 } else {
556 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
557 "error, unkown protocol given!\n"));
558 return;
561 if (!str) {
562 DEBUG(1, ("smb_traffic_analyzer_send_data: "
563 "unable to create string to send!\n"));
564 return;
569 * If configured, optain the key and run AES encryption
570 * over the data.
572 become_root();
573 akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
574 unbecome_root();
575 if ( akey != NULL ) {
576 state_flags[2] = 'E';
577 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: a key was"
578 " found, encrypting data!\n"));
579 output = smb_traffic_analyzer_encrypt( talloc_tos(),
580 akey, str, &len);
581 header = smb_traffic_analyzer_create_header( talloc_tos(),
582 state_flags, len);
584 DEBUG(10, ("smb_traffic_analyzer_send_data_socket:"
585 " header created for crypted data: %s\n", header));
586 smb_traffic_analyzer_write_data(header, output, len,
587 rf_sock->sock);
588 return;
592 len = strlen(str);
593 header = smb_traffic_analyzer_create_header( talloc_tos(),
594 state_flags, len);
595 smb_traffic_analyzer_write_data(header, str, strlen(str),
596 rf_sock->sock);
600 static struct refcounted_sock *sock_list;
602 static void smb_traffic_analyzer_free_data(void **pptr)
604 struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
605 if (rf_sock == NULL) {
606 return;
608 rf_sock->ref_count--;
609 if (rf_sock->ref_count != 0) {
610 return;
612 if (rf_sock->sock != -1) {
613 close(rf_sock->sock);
615 DLIST_REMOVE(sock_list, rf_sock);
616 TALLOC_FREE(rf_sock);
619 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
620 const char *service,
621 const char *user)
623 connection_struct *conn = handle->conn;
624 enum sock_type st = smb_traffic_analyzer_connMode(handle);
625 struct refcounted_sock *rf_sock = NULL;
626 const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
627 lp_parm_const_string(SNUM(conn),
628 "smb_traffic_analyzer",
629 "host", "localhost");
630 uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
631 atoi( lp_parm_const_string(SNUM(conn),
632 "smb_traffic_analyzer", "port", "9430"));
633 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
635 if (ret < 0) {
636 return ret;
639 /* Are we already connected ? */
640 for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
641 if (port == rf_sock->port &&
642 (strcmp(name, rf_sock->name) == 0)) {
643 break;
647 /* If we're connected already, just increase the
648 * reference count. */
649 if (rf_sock) {
650 rf_sock->ref_count++;
651 } else {
652 /* New connection. */
653 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
654 if (rf_sock == NULL) {
655 SMB_VFS_NEXT_DISCONNECT(handle);
656 errno = ENOMEM;
657 return -1;
659 rf_sock->name = talloc_strdup(rf_sock, name);
660 if (rf_sock->name == NULL) {
661 SMB_VFS_NEXT_DISCONNECT(handle);
662 TALLOC_FREE(rf_sock);
663 errno = ENOMEM;
664 return -1;
666 rf_sock->port = port;
667 rf_sock->ref_count = 1;
669 if (st == UNIX_DOMAIN_SOCKET) {
670 rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
671 name);
672 } else {
674 rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
675 name,
676 port);
678 if (rf_sock->sock == -1) {
679 SMB_VFS_NEXT_DISCONNECT(handle);
680 TALLOC_FREE(rf_sock);
681 return -1;
683 DLIST_ADD(sock_list, rf_sock);
686 /* Store the private data. */
687 SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
688 struct refcounted_sock, return -1);
689 return 0;
692 /* VFS Functions */
693 static int smb_traffic_analyzer_chdir(vfs_handle_struct *handle, \
694 const char *path)
696 struct chdir_data s_data;
697 s_data.result = SMB_VFS_NEXT_CHDIR(handle, path);
698 s_data.path = path;
699 DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path));
700 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_chdir);
701 return s_data.result;
704 static int smb_traffic_analyzer_rename(vfs_handle_struct *handle, \
705 const struct smb_filename *smb_fname_src,
706 const struct smb_filename *smb_fname_dst)
708 struct rename_data s_data;
709 s_data.result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, \
710 smb_fname_dst);
711 s_data.src = smb_fname_src->base_name;
712 s_data.dst = smb_fname_dst->base_name;
713 DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
714 smb_fname_src->base_name,
715 smb_fname_dst->base_name));
716 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rename);
717 return s_data.result;
720 static int smb_traffic_analyzer_rmdir(vfs_handle_struct *handle, \
721 const char *path)
723 struct rmdir_data s_data;
724 s_data.result = SMB_VFS_NEXT_RMDIR(handle, path);
725 s_data.path = path;
726 DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path));
727 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rmdir);
728 return s_data.result;
731 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
732 const char *path, mode_t mode)
734 struct mkdir_data s_data;
735 s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
736 s_data.path = path;
737 s_data.mode = mode;
738 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
739 smb_traffic_analyzer_send_data(handle,
740 &s_data,
741 vfs_id_mkdir);
742 return s_data.result;
745 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
746 files_struct *fsp, void *data, size_t n)
748 struct rw_data s_data;
750 s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
751 s_data.filename = fsp->fsp_name->base_name;
752 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
754 smb_traffic_analyzer_send_data(handle,
755 &s_data,
756 vfs_id_read);
757 return s_data.len;
761 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
762 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
764 struct rw_data s_data;
766 s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
767 s_data.filename = fsp->fsp_name->base_name;
768 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
769 fsp_str_dbg(fsp)));
771 smb_traffic_analyzer_send_data(handle,
772 &s_data,
773 vfs_id_pread);
775 return s_data.len;
778 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
779 files_struct *fsp, const void *data, size_t n)
781 struct rw_data s_data;
783 s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
784 s_data.filename = fsp->fsp_name->base_name;
785 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
786 fsp_str_dbg(fsp)));
788 smb_traffic_analyzer_send_data(handle,
789 &s_data,
790 vfs_id_write);
791 return s_data.len;
794 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
795 files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
797 struct rw_data s_data;
799 s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
800 s_data.filename = fsp->fsp_name->base_name;
801 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
802 fsp_str_dbg(fsp)));
804 smb_traffic_analyzer_send_data(handle,
805 &s_data,
806 vfs_id_pwrite);
807 return s_data.len;
810 static int smb_traffic_analyzer_open(vfs_handle_struct *handle, \
811 struct smb_filename *smb_fname, files_struct *fsp,\
812 int flags, mode_t mode)
814 struct open_data s_data;
816 s_data.result = SMB_VFS_NEXT_OPEN( handle, smb_fname, fsp,
817 flags, mode);
818 DEBUG(10,("smb_traffic_analyzer_open: OPEN: %s\n",
819 fsp_str_dbg(fsp)));
820 s_data.filename = fsp->fsp_name->base_name;
821 s_data.mode = mode;
822 smb_traffic_analyzer_send_data(handle,
823 &s_data,
824 vfs_id_open);
825 return s_data.result;
828 static int smb_traffic_analyzer_close(vfs_handle_struct *handle, \
829 files_struct *fsp)
831 struct close_data s_data;
832 s_data.result = SMB_VFS_NEXT_CLOSE(handle, fsp);
833 DEBUG(10,("smb_traffic_analyzer_close: CLOSE: %s\n",
834 fsp_str_dbg(fsp)));
835 s_data.filename = fsp->fsp_name->base_name;
836 smb_traffic_analyzer_send_data(handle,
837 &s_data,
838 vfs_id_close);
839 return s_data.result;
843 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
844 .connect_fn = smb_traffic_analyzer_connect,
845 .vfs_read = smb_traffic_analyzer_read,
846 .pread = smb_traffic_analyzer_pread,
847 .write = smb_traffic_analyzer_write,
848 .pwrite = smb_traffic_analyzer_pwrite,
849 .mkdir = smb_traffic_analyzer_mkdir,
850 .rename = smb_traffic_analyzer_rename,
851 .chdir = smb_traffic_analyzer_chdir,
852 .open = smb_traffic_analyzer_open,
853 .rmdir = smb_traffic_analyzer_rmdir,
854 .close_fn = smb_traffic_analyzer_close
857 /* Module initialization */
858 NTSTATUS vfs_smb_traffic_analyzer_init(void)
860 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
861 "smb_traffic_analyzer",
862 &vfs_smb_traffic_analyzer_fns);
864 if (!NT_STATUS_IS_OK(ret)) {
865 return ret;
868 vfs_smb_traffic_analyzer_debug_level =
869 debug_add_class("smb_traffic_analyzer");
871 if (vfs_smb_traffic_analyzer_debug_level == -1) {
872 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
873 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
874 "debugging class!\n"));
875 } else {
876 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
877 "'smb_traffic_analyzer': %d\n", \
878 vfs_smb_traffic_analyzer_debug_level));
881 return ret;