s3: vfs_smb_traffic_analyzer.c: add VFS functions for file open and close
[Samba/ekacnet.git] / source3 / modules / vfs_smb_traffic_analyzer.c
blobf454c457817a0943dfa1799cc337fefe7fb98058
1 /*
2 * traffic-analyzer VFS module. Measure the smb traffic users create
3 * on the net.
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/>.
22 #include "includes.h"
23 #include "../lib/crypto/crypto.h"
24 #include "vfs_smb_traffic_analyzer.h"
26 /* abstraction for the send_over_network function */
27 enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};
29 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
31 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
33 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
35 connection_struct *conn = handle->conn;
36 const char *Mode;
37 Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
38 "internet_socket");
39 if (strstr(Mode,"unix_domain_socket")) {
40 return UNIX_DOMAIN_SOCKET;
41 } else {
42 return INTERNET_SOCKET;
47 /* 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 */
52 int sockfd = -1;
53 struct addrinfo hints;
54 struct addrinfo *ailist = NULL;
55 struct addrinfo *res = NULL;
56 int ret;
58 ZERO_STRUCT(hints);
59 /* By default make sure it supports TCP. */
60 hints.ai_socktype = SOCK_STREAM;
61 hints.ai_flags = AI_ADDRCONFIG;
63 ret = getaddrinfo(name,
64 NULL,
65 &hints,
66 &ailist);
68 if (ret) {
69 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
70 "getaddrinfo failed for name %s [%s]\n",
71 name,
72 gai_strerror(ret) ));
73 return -1;
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;
81 NTSTATUS status;
83 if (!res->ai_addr || res->ai_addrlen == 0) {
84 continue;
87 ZERO_STRUCT(ss);
88 memcpy(&ss, res->ai_addr, res->ai_addrlen);
90 status = open_socket_out(&ss, port, 10000, &sockfd);
91 if (NT_STATUS_IS_OK(status)) {
92 break;
96 if (ailist) {
97 freeaddrinfo(ailist);
100 if (sockfd == -1) {
101 DEBUG(1, ("smb_traffic_analyzer: unable to create "
102 "socket, error is %s",
103 strerror(errno)));
104 return -1;
107 return sockfd;
110 /* Connect to a unix domain socket */
111 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
112 const char *name)
114 /* Create the socket to stad */
115 int len, sock;
116 struct sockaddr_un remote;
118 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
119 "Unix domain socket mode. Using %s\n",
120 name ));
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"));
126 return -1;
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"));
136 close(sock);
137 return -1;
139 return sock;
142 /* Private data allowing shared connection sockets. */
143 struct refcounted_sock {
144 struct refcounted_sock *next, *prev;
145 char *name;
146 uint16_t port;
147 int sock;
148 unsigned int ref_count;
153 * Encryption of a data block with AES
154 * TALLOC_CTX *ctx Talloc context to work on
155 * const char *akey 128bit key for the encryption
156 * const char *str Data buffer to encrypt, \0 terminated
157 * int *len Will be set to the length of the
158 * resulting data block
159 * The caller has to take care for the memory
160 * allocated on the context.
162 static char *smb_traffic_analyzer_encrypt( TALLOC_CTX *ctx,
163 const char *akey, const char *str, size_t *len)
165 int s1,s2,h,d;
166 AES_KEY key;
167 char filler[17]= "................";
168 char *output;
169 char crypted[18];
170 if (akey == NULL) return NULL;
171 samba_AES_set_encrypt_key(akey, 128, &key);
172 s1 = strlen(str) / 16;
173 s2 = strlen(str) % 16;
174 for (h = 0; h < s2; h++) *(filler+h)=*(str+(s1*16)+h);
175 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: created %s"
176 " as filling block.\n", filler));
177 output = talloc_array(ctx, char, (s1*16)+17 );
178 d=0;
179 for (h = 0; h < s1; h++) {
180 samba_AES_encrypt(str+(16*h), crypted, &key);
181 for (d = 0; d<16; d++) output[d+(16*h)]=crypted[d];
183 samba_AES_encrypt( str+(16*h), filler, &key );
184 for (d = 0;d < 16; d++) output[d+(16*h)]=*(filler+d);
185 *len = (s1*16)+16;
186 return output;
190 * Create a v2 header.
191 * TALLLOC_CTX *ctx Talloc context to work on
192 * const char *state_flags State flag string
193 * int len length of the data block
195 static char *smb_traffic_analyzer_create_header( TALLOC_CTX *ctx,
196 const char *state_flags, size_t data_len)
198 char *header = talloc_asprintf( ctx, "V2.%s%017u",
199 state_flags, data_len);
200 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: created Header:\n"));
201 dump_data(10, header, strlen(header));
202 return header;
207 * Actually send header and data over the network
208 * char *header Header data
209 * char *data Data Block
210 * int dlength Length of data block
211 * int socket
213 static void smb_traffic_analyzer_write_data( char *header, char *data,
214 int dlength, int socket)
216 int len = strlen(header);
217 if (write_data( socket, header, len) != len) {
218 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
219 "error sending the header"
220 " over the socket!\n"));
222 DEBUG(10,("smb_traffic_analyzer_write_data: sending data:\n"));
223 dump_data( 10, data, dlength);
225 if (write_data( socket, data, dlength) != dlength) {
226 DEBUG(1, ("smb_traffic_analyzer_write_data: "
227 "error sending crypted data to socket!\n"));
233 * Anonymize a string if required.
234 * TALLOC_CTX *ctx The talloc context to work on
235 * const char *str The string to anonymize
236 * vfs_handle_struct *handle The handle struct to work on
238 * Returns a newly allocated string, either the anonymized one,
239 * or a copy of const char *str. The caller has to take care for
240 * freeing the allocated memory.
242 static char *smb_traffic_analyzer_anonymize( TALLOC_CTX *ctx,
243 const char *str,
244 vfs_handle_struct *handle )
246 const char *total_anonymization;
247 const char *anon_prefix;
248 char *output;
249 total_anonymization=lp_parm_const_string(SNUM(handle->conn),
250 "smb_traffic_analyzer",
251 "total_anonymization", NULL);
253 anon_prefix=lp_parm_const_string(SNUM(handle->conn),
254 "smb_traffic_analyzer",
255 "anonymize_prefix", NULL );
256 if (anon_prefix != NULL) {
257 if (total_anonymization != NULL) {
258 output = talloc_asprintf(ctx, "%s",
259 anon_prefix);
260 } else {
261 output = talloc_asprintf(ctx, "%s%i", anon_prefix,
262 str_checksum(str));
264 } else {
265 output = talloc_asprintf(ctx, "%s", str);
268 return output;
273 * The marshalling function for protocol v2.
274 * TALLOC_CTX *ctx Talloc context to work on
275 * struct tm *tm tm struct for the timestamp
276 * int seconds milliseconds of the timestamp
277 * vfs_handle_struct *handle vfs_handle_struct
278 * char *username Name of the user
279 * int vfs_operation VFS operation identifier
280 * int count Number of the common data blocks
281 * [...] variable args data blocks taken from the individual
282 * VFS data structures
284 * Returns the complete data block to send. The caller has to
285 * take care for freeing the allocated buffer.
287 static char *smb_traffic_analyzer_create_string( TALLOC_CTX *ctx,
288 struct tm *tm, int seconds, vfs_handle_struct *handle, \
289 char *username, int vfs_operation, int count, ... )
292 va_list ap;
293 char *arg = NULL;
294 int len;
295 char *header = NULL;
296 char *common_data_count_str = NULL;
297 char *timestr = NULL;
298 char *sidstr = NULL;
299 char *usersid = NULL;
300 char *buf = NULL;
301 char *vfs_operation_str = NULL;
303 * first create the data that is transfered with any VFS op
304 * These are, in the following order:
305 *(0) number of data to come [6 in v2.0]
306 * 1.vfs_operation identifier
307 * 2.username
308 * 3.user-SID
309 * 4.affected share
310 * 5.domain
311 * 6.timestamp
315 * number of common data blocks to come,
316 * this is a #define in vfs_smb_traffic_anaylzer.h,
317 * it's length is known at compile time
319 common_data_count_str = talloc_strdup( ctx, SMBTA_COMMON_DATA_COUNT);
320 /* vfs operation identifier */
321 vfs_operation_str = talloc_asprintf( common_data_count_str, "%i",
322 vfs_operation);
324 * Handle anonymization. In protocol v2, we have to anonymize
325 * both the SID and the username. The name is already
326 * anonymized if needed, by the calling function.
328 usersid = dom_sid_string( common_data_count_str,
329 &handle->conn->server_info->ptok->user_sids[0]);
331 sidstr = smb_traffic_analyzer_anonymize(
332 common_data_count_str,
333 usersid,
334 handle);
336 /* time stamp */
337 timestr = talloc_asprintf( common_data_count_str, \
338 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
339 tm->tm_year+1900, \
340 tm->tm_mon+1, \
341 tm->tm_mday, \
342 tm->tm_hour, \
343 tm->tm_min, \
344 tm->tm_sec, \
345 (int)seconds);
346 len = strlen( timestr );
347 /* create the string of common data */
348 buf = talloc_asprintf(ctx,
349 "%s%04u%s%04u%s%04u%s%04u%s%04u%s%04u%s",
350 common_data_count_str,
351 strlen(vfs_operation_str),
352 vfs_operation_str,
353 strlen(username),
354 username,
355 strlen(sidstr),
356 sidstr,
357 strlen(handle->conn->connectpath),
358 handle->conn->connectpath,
359 strlen(pdb_get_domain(handle->conn->server_info->sam_account)),
360 pdb_get_domain(handle->conn->server_info->sam_account),
361 strlen(timestr),
362 timestr);
364 talloc_free(common_data_count_str);
366 /* data blocks depending on the VFS function */
367 va_start( ap, count );
368 while ( count-- ) {
369 arg = va_arg( ap, char * );
371 * protocol v2 sends a four byte string
372 * as a header to each block, including
373 * the numbers of bytes to come in the
374 * next string.
376 len = strlen( arg );
377 buf = talloc_asprintf_append( buf, "%04u%s", len, arg);
379 va_end( ap );
380 return buf;
383 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
384 void *data,
385 enum vfs_id vfs_operation )
387 struct refcounted_sock *rf_sock = NULL;
388 struct timeval tv;
389 time_t tv_sec;
390 struct tm *tm = NULL;
391 int seconds;
392 char *str = NULL;
393 char *username = NULL;
394 char *header = NULL;
395 const char *protocol_version = NULL;
396 bool Write = false;
397 size_t len;
400 * The state flags are part of the header
401 * and are descripted in the protocol description
402 * in vfs_smb_traffic_analyzer.h. They begin at byte
403 * 03 of the header.
405 char state_flags[9] = "000000\0";
407 SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
409 if (rf_sock == NULL || rf_sock->sock == -1) {
410 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
411 "closed\n"));
412 return;
415 GetTimeOfDay(&tv);
416 tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
417 tm = localtime(&tv_sec);
418 if (!tm) {
419 return;
421 seconds=(float) (tv.tv_usec / 1000);
424 * Check if anonymization is required, and if yes do this only for
425 * the username here, needed vor protocol version 1. In v2 we
426 * additionally anonymize the SID, which is done in it's marshalling
427 * function.
429 username = smb_traffic_analyzer_anonymize( talloc_tos(),
430 handle->conn->server_info->sanitized_username,
431 handle);
433 if (!username) {
434 return;
437 protocol_version = lp_parm_const_string(SNUM(handle->conn),
438 "smb_traffic_analyzer",
439 "protocol_version", NULL );
442 if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
444 struct rw_data *s_data = (struct rw_data *) data;
447 * in case of protocol v1, ignore any vfs operations
448 * except read,pread,write,pwrite, and set the "Write"
449 * bool accordingly, send data and return.
451 if ( vfs_operation > vfs_id_pwrite ) return;
453 if ( vfs_operation <= vfs_id_pread ) Write=false;
454 else Write=true;
456 str = talloc_asprintf(talloc_tos(),
457 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
458 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
459 (unsigned int) s_data->len,
460 username,
461 pdb_get_domain(handle->conn->server_info->sam_account),
462 Write ? 'W' : 'R',
463 handle->conn->connectpath,
464 s_data->filename,
465 tm->tm_year+1900,
466 tm->tm_mon+1,
467 tm->tm_mday,
468 tm->tm_hour,
469 tm->tm_min,
470 tm->tm_sec,
471 (int)seconds);
472 if (write_data(rf_sock->sock, str, len) != len) {
473 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
474 "error sending V1 protocol data to socket!\n"));
475 return;
478 } else if ( strcmp( protocol_version, "V2") == 0) {
480 switch( vfs_operation ) {
481 case vfs_id_open: ;
482 str = smb_traffic_analyzer_create_string( talloc_tos(),
483 tm, seconds, handle, username, vfs_id_open,
484 3, ((struct open_data *) data)->filename,
485 talloc_asprintf( talloc_tos(), "%u",
486 ((struct open_data *) data)->mode),
487 talloc_asprintf( talloc_tos(), "%u",
488 ((struct open_data *) data)->result));
489 break;
490 case vfs_id_close: ;
491 str = smb_traffic_analyzer_create_string( talloc_tos(),
492 tm, seconds, handle, username, vfs_id_close,
493 2, ((struct close_data *) data)->filename,
494 talloc_asprintf( talloc_tos(), "%u",
495 ((struct close_data *) data)->result));
496 break;
497 case vfs_id_mkdir: ;
498 str = smb_traffic_analyzer_create_string( talloc_tos(),
499 tm, seconds, handle, username, vfs_id_mkdir, \
500 3, ((struct mkdir_data *) data)->path, \
501 talloc_asprintf( talloc_tos(), "%u", \
502 ((struct mkdir_data *) data)->mode), \
503 talloc_asprintf( talloc_tos(), "%u", \
504 ((struct mkdir_data *) data)->result ));
505 break;
506 case vfs_id_rmdir: ;
507 str = smb_traffic_analyzer_create_string( talloc_tos(),
508 tm, seconds, handle, username, vfs_id_rmdir,
509 2, ((struct rmdir_data *) data)->path, \
510 talloc_asprintf( talloc_tos(), "%u", \
511 ((struct rmdir_data *) data)->result ));
512 break;
513 case vfs_id_rename: ;
514 str = smb_traffic_analyzer_create_string( talloc_tos(),
515 tm, seconds, handle, username, vfs_id_rename,
516 3, ((struct rename_data *) data)->src, \
517 ((struct rename_data *) data)->dst,
518 talloc_asprintf(talloc_tos(), "%u", \
519 ((struct rename_data *) data)->result));
520 break;
521 case vfs_id_chdir: ;
522 str = smb_traffic_analyzer_create_string( talloc_tos(),
523 tm, seconds, handle, username, vfs_id_chdir,
524 2, ((struct chdir_data *) data)->path, \
525 talloc_asprintf(talloc_tos(), "%u", \
526 ((struct chdir_data *) data)->result));
527 break;
529 case vfs_id_write:
530 case vfs_id_pwrite:
531 case vfs_id_read:
532 case vfs_id_pread: ;
533 str = smb_traffic_analyzer_create_string( talloc_tos(),
534 tm, seconds, handle, username, vfs_operation,
535 2, ((struct rw_data *) data)->filename, \
536 talloc_asprintf(talloc_tos(), "%u", \
537 ((struct rw_data *) data)->len));
538 break;
539 default:
540 DEBUG(1, ("smb_traffic_analyzer: error! "
541 "wrong VFS operation id detected!\n"));
542 return;
545 } else {
546 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
547 "error, unkown protocol given!\n"));
548 return;
551 if (!str) {
552 DEBUG(1, ("smb_traffic_analyzer_send_data: "
553 "unable to create string to send!\n"));
554 return;
559 * If configured, optain the key and run AES encryption
560 * over the data.
562 size_t size;
563 become_root();
564 char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
565 unbecome_root();
566 if ( akey != NULL ) {
567 state_flags[2] = 'E';
568 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: a key was"
569 " found, encrypting data!\n"));
570 char *output = smb_traffic_analyzer_encrypt( talloc_tos(),
571 akey, str, &len);
572 header = smb_traffic_analyzer_create_header( talloc_tos(),
573 state_flags, len);
575 DEBUG(10, ("smb_traffic_analyzer_send_data_socket:"
576 " header created for crypted data: %s\n", header));
577 smb_traffic_analyzer_write_data(header, output, len,
578 rf_sock->sock);
579 return;
583 len = strlen(str);
584 header = smb_traffic_analyzer_create_header( talloc_tos(),
585 state_flags, len);
586 smb_traffic_analyzer_write_data(header, str, strlen(str),
587 rf_sock->sock);
591 static struct refcounted_sock *sock_list;
593 static void smb_traffic_analyzer_free_data(void **pptr)
595 struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
596 if (rf_sock == NULL) {
597 return;
599 rf_sock->ref_count--;
600 if (rf_sock->ref_count != 0) {
601 return;
603 if (rf_sock->sock != -1) {
604 close(rf_sock->sock);
606 DLIST_REMOVE(sock_list, rf_sock);
607 TALLOC_FREE(rf_sock);
610 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
611 const char *service,
612 const char *user)
614 connection_struct *conn = handle->conn;
615 enum sock_type st = smb_traffic_analyzer_connMode(handle);
616 struct refcounted_sock *rf_sock = NULL;
617 const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
618 lp_parm_const_string(SNUM(conn),
619 "smb_traffic_analyzer",
620 "host", "localhost");
621 uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
622 atoi( lp_parm_const_string(SNUM(conn),
623 "smb_traffic_analyzer", "port", "9430"));
624 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
626 if (ret < 0) {
627 return ret;
630 /* Are we already connected ? */
631 for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
632 if (port == rf_sock->port &&
633 (strcmp(name, rf_sock->name) == 0)) {
634 break;
638 /* If we're connected already, just increase the
639 * reference count. */
640 if (rf_sock) {
641 rf_sock->ref_count++;
642 } else {
643 /* New connection. */
644 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
645 if (rf_sock == NULL) {
646 SMB_VFS_NEXT_DISCONNECT(handle);
647 errno = ENOMEM;
648 return -1;
650 rf_sock->name = talloc_strdup(rf_sock, name);
651 if (rf_sock->name == NULL) {
652 SMB_VFS_NEXT_DISCONNECT(handle);
653 TALLOC_FREE(rf_sock);
654 errno = ENOMEM;
655 return -1;
657 rf_sock->port = port;
658 rf_sock->ref_count = 1;
660 if (st == UNIX_DOMAIN_SOCKET) {
661 rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
662 name);
663 } else {
665 rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
666 name,
667 port);
669 if (rf_sock->sock == -1) {
670 SMB_VFS_NEXT_DISCONNECT(handle);
671 TALLOC_FREE(rf_sock);
672 return -1;
674 DLIST_ADD(sock_list, rf_sock);
677 /* Store the private data. */
678 SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
679 struct refcounted_sock, return -1);
680 return 0;
683 /* VFS Functions */
684 static int smb_traffic_analyzer_chdir(vfs_handle_struct *handle, \
685 const char *path)
687 struct chdir_data s_data;
688 s_data.result = SMB_VFS_NEXT_CHDIR(handle, path);
689 s_data.path = path;
690 DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path));
691 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_chdir);
692 return s_data.result;
695 static int smb_traffic_analyzer_rename(vfs_handle_struct *handle, \
696 const struct smb_filename *smb_fname_src,
697 const struct smb_filename *smb_fname_dst)
699 struct rename_data s_data;
700 s_data.result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, \
701 smb_fname_dst);
702 s_data.src = smb_fname_src->base_name;
703 s_data.dst = smb_fname_dst->base_name;
704 DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
705 smb_fname_src->base_name,
706 smb_fname_dst->base_name));
707 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rename);
708 return s_data.result;
711 static int smb_traffic_analyzer_rmdir(vfs_handle_struct *handle, \
712 const char *path)
714 struct rmdir_data s_data;
715 s_data.result = SMB_VFS_NEXT_RMDIR(handle, path);
716 s_data.path = path;
717 DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path));
718 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rmdir);
719 return s_data.result;
722 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
723 const char *path, mode_t mode)
725 struct mkdir_data s_data;
726 s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
727 s_data.path = path;
728 s_data.mode = mode;
729 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
730 smb_traffic_analyzer_send_data(handle,
731 &s_data,
732 vfs_id_mkdir);
733 return s_data.result;
736 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
737 files_struct *fsp, void *data, size_t n)
739 struct rw_data s_data;
741 s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
742 s_data.filename = fsp->fsp_name->base_name;
743 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
745 smb_traffic_analyzer_send_data(handle,
746 &s_data,
747 vfs_id_read);
748 return s_data.len;
752 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
753 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
755 struct rw_data s_data;
757 s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
758 s_data.filename = fsp->fsp_name->base_name;
759 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
760 fsp_str_dbg(fsp)));
762 smb_traffic_analyzer_send_data(handle,
763 &s_data,
764 vfs_id_pread);
766 return s_data.len;
769 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
770 files_struct *fsp, const void *data, size_t n)
772 struct rw_data s_data;
774 s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
775 s_data.filename = fsp->fsp_name->base_name;
776 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
777 fsp_str_dbg(fsp)));
779 smb_traffic_analyzer_send_data(handle,
780 &s_data,
781 vfs_id_write);
782 return s_data.len;
785 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
786 files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
788 struct rw_data s_data;
790 s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
791 s_data.filename = fsp->fsp_name->base_name;
792 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
793 fsp_str_dbg(fsp)));
795 smb_traffic_analyzer_send_data(handle,
796 &s_data,
797 vfs_id_pwrite);
798 return s_data.len;
801 static int smb_traffic_analyzer_open(vfs_handle_struct *handle, \
802 struct smb_filename *smb_fname, files_struct *fsp,\
803 int flags, mode_t mode)
805 struct open_data s_data;
807 s_data.result = SMB_VFS_NEXT_OPEN( handle, smb_fname, fsp,
808 flags, mode);
809 DEBUG(10,("smb_traffic_analyzer_open: OPEN: %s\n",
810 fsp_str_dbg(fsp)));
811 s_data.filename = fsp->fsp_name->base_name;
812 smb_traffic_analyzer_send_data(handle,
813 &s_data,
814 vfs_id_open);
815 return s_data.result;
818 static int smb_traffic_analyzer_close(vfs_handle_struct *handle, \
819 files_struct *fsp)
821 struct close_data s_data;
822 s_data.result = SMB_VFS_NEXT_CLOSE(handle, fsp);
823 DEBUG(10,("smb_traffic_analyzer_close: CLOSE: %s\n",
824 fsp_str_dbg(fsp)));
825 s_data.filename = fsp->fsp_name->base_name;
826 smb_traffic_analyzer_send_data(handle,
827 &s_data,
828 vfs_id_close);
829 return s_data.result;
833 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
834 .connect_fn = smb_traffic_analyzer_connect,
835 .vfs_read = smb_traffic_analyzer_read,
836 .pread = smb_traffic_analyzer_pread,
837 .write = smb_traffic_analyzer_write,
838 .pwrite = smb_traffic_analyzer_pwrite,
839 .mkdir = smb_traffic_analyzer_mkdir,
840 .rename = smb_traffic_analyzer_rename,
841 .chdir = smb_traffic_analyzer_chdir,
842 .open = smb_traffic_analyzer_open,
843 .close_fn = smb_traffic_analyzer_close
846 /* Module initialization */
847 NTSTATUS vfs_smb_traffic_analyzer_init(void)
849 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
850 "smb_traffic_analyzer",
851 &vfs_smb_traffic_analyzer_fns);
853 if (!NT_STATUS_IS_OK(ret)) {
854 return ret;
857 vfs_smb_traffic_analyzer_debug_level =
858 debug_add_class("smb_traffic_analyzer");
860 if (vfs_smb_traffic_analyzer_debug_level == -1) {
861 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
862 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
863 "debugging class!\n"));
864 } else {
865 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
866 "'smb_traffic_analyzer': %d\n", \
867 vfs_smb_traffic_analyzer_debug_level));
870 return ret;