vfs_smb_traffic_analyzer.c: added function
[ira/wip.git] / source3 / modules / vfs_smb_traffic_analyzer.c
1 /*
2  * traffic-analyzer VFS module. Measure the smb traffic users create
3  * on the net.
4  *
5  * Copyright (C) Holger Hetterich, 2008
6  * Copyright (C) Jeremy Allison, 2008
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "includes.h"
23 #include "../lib/crypto/crypto.h"
24 #include "vfs_smb_traffic_analyzer.h"
25
26 /* abstraction for the send_over_network function */
27 enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};
28
29 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
30
31 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
32
33 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
34 {
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;
43         }
44 }
45
46
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)
50 {
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;
57
58         ZERO_STRUCT(hints);
59         /* By default make sure it supports TCP. */
60         hints.ai_socktype = SOCK_STREAM;
61         hints.ai_flags = AI_ADDRCONFIG;
62
63         ret = getaddrinfo(name,
64                         NULL,
65                         &hints,
66                         &ailist);
67
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;
74         }
75
76         DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
77                 "Port: %i\n", name, port));
78
79         for (res = ailist; res; res = res->ai_next) {
80                 struct sockaddr_storage ss;
81                 NTSTATUS status;
82
83                 if (!res->ai_addr || res->ai_addrlen == 0) {
84                         continue;
85                 }
86
87                 ZERO_STRUCT(ss);
88                 memcpy(&ss, res->ai_addr, res->ai_addrlen);
89
90                 status = open_socket_out(&ss, port, 10000, &sockfd);
91                 if (NT_STATUS_IS_OK(status)) {
92                         break;
93                 }
94         }
95
96         if (ailist) {
97                 freeaddrinfo(ailist);
98         }
99
100         if (sockfd == -1) {
101                 DEBUG(1, ("smb_traffic_analyzer: unable to create "
102                         "socket, error is %s",
103                         strerror(errno)));
104                 return -1;
105         }
106
107         return sockfd;
108 }
109
110 /* Connect to a unix domain socket */
111 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
112                                                 const char *name)
113 {
114         /* Create the socket to stad */
115         int len, sock;
116         struct sockaddr_un remote;
117
118         DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
119                         "Unix domain socket mode. Using %s\n",
120                         name ));
121
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;
127         }
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;
138         }
139         return sock;
140 }
141
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;
149 };
150
151
152 /**
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.
161  */
162 static char *smb_traffic_analyzer_encrypt( TALLOC_CTX *ctx,
163         const char *akey, const char *str, size_t *len)
164 {
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];
182         }
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;  
187 }
188
189 /**
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
194  */
195 static char *smb_traffic_analyzer_create_header( TALLOC_CTX *ctx,
196         const char *state_flags, size_t data_len)
197 {
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;
203 }
204
205
206 /**
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
212  */
213 static void smb_traffic_analyzer_write_data( char *header, char *data,
214                         int dlength, int socket)
215 {
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"));
221                 }
222                 DEBUG(10,("smb_traffic_analyzer_write_data: sending data:\n"));
223                 dump_data( 10, data, dlength);
224
225                 if (write_data( socket, data, dlength) != dlength) {
226                         DEBUG(1, ("smb_traffic_analyzer_write_data: "
227                                 "error sending crypted data to socket!\n"));
228                 }
229 }
230
231
232 /*
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
237  *
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.
241  */
242 static char *smb_traffic_analyzer_anonymize( TALLOC_CTX *ctx,
243                                         const char *str,
244                                         vfs_handle_struct *handle )
245 {
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);
252
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));
263                 }
264         } else {
265                 output = talloc_asprintf(ctx, "%s", str);
266         }
267
268         return output;
269 }
270
271
272 /* The marshaller for the protocol version 2. */
273 static char *smb_traffic_analyzer_create_string( TALLOC_CTX *ctx,
274         struct tm *tm, int seconds, vfs_handle_struct *handle, \
275         char *username, int vfs_operation, int count, ... )
276 {
277         
278         va_list ap;
279         char *arg = NULL;
280         int len;
281         char *header = NULL;
282         char *buf = NULL;
283         char *timestr = NULL;
284         char *opstr = NULL;
285         char *sidstr = NULL;
286         char *usersid = NULL;
287         /*
288          * first create the data that is transfered with any VFS op
289          * These are, in the following order:
290          *(0) number of data to come [6 in v2.0]
291          * 1.vfs_operation identifier
292          * 2.username
293          * 3.user-SID
294          * 4.affected share
295          * 5.domain
296          * 6.timestamp
297          */
298
299         /* number of common data blocks to come */
300         opstr = talloc_asprintf( ctx, "%i", SMBTA_COMMON_DATA_COUNT);
301         len = strlen(opstr);
302         buf = talloc_asprintf( ctx, "%04u%s", len, opstr);
303         talloc_free(opstr);
304         /* vfs operation identifier */
305         opstr = talloc_asprintf( ctx, "%i", vfs_operation);
306         len = strlen(opstr);
307         buf = talloc_asprintf_append( buf, "%04u%s", len, opstr);
308         talloc_free(opstr);
309         /*
310          * Handle anonymization. In protocol v2, we have to anonymize
311          * both the SID and the username. The name is already
312          * anonymized if needed, by the calling function.
313          */
314         usersid = dom_sid_string( ctx,
315                 &handle->conn->server_info->ptok->user_sids[0]);
316         sidstr = smb_traffic_analyzer_anonymize(ctx, usersid, handle);
317         talloc_free(usersid);
318         /* username */
319         len = strlen( username );
320         buf = talloc_asprintf_append(buf, "%04u%s", len, username);
321         /* user SID */
322         len = strlen( sidstr );
323         buf = talloc_asprintf_append(buf, "%04u%s", len, sidstr);
324         talloc_free(sidstr);
325         /* affected share */
326         len = strlen( handle->conn->connectpath );
327         buf = talloc_asprintf_append( buf, "%04u%s", len, \
328                 handle->conn->connectpath );
329         /* user's domain */
330         len = strlen( pdb_get_domain(handle->conn->server_info->sam_account) );
331         buf = talloc_asprintf_append( buf, "%04u%s", len, \
332                 pdb_get_domain(handle->conn->server_info->sam_account) );
333         /* time stamp */
334         timestr = talloc_asprintf( ctx, \
335                 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
336                 tm->tm_year+1900, \
337                 tm->tm_mon+1, \
338                 tm->tm_mday, \
339                 tm->tm_hour, \
340                 tm->tm_min, \
341                 tm->tm_sec, \
342                 (int)seconds);
343         len = strlen( timestr );
344         buf = talloc_asprintf_append( buf, "%04u%s", len, timestr);
345         talloc_free(timestr);
346         /* data blocks depending on the VFS function */ 
347         va_start( ap, count );
348         while ( count-- ) {
349                 arg = va_arg( ap, char * );
350                 /*
351                  *  protocol v2 sends a four byte string
352                  * as a header to each block, including
353                  * the numbers of bytes to come in the
354                  * next string.
355                  */
356                 len = strlen( arg );
357                 buf = talloc_asprintf_append( buf, "%04u%s", len, arg);
358         }
359         va_end( ap );
360         return buf;
361 }
362
363 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
364                                         void *data,
365                                         enum vfs_id vfs_operation )
366 {
367         struct refcounted_sock *rf_sock = NULL;
368         struct timeval tv;
369         time_t tv_sec;
370         struct tm *tm = NULL;
371         int seconds;
372         char *str = NULL;
373         char *username = NULL;
374         char *header = NULL;
375         const char *protocol_version = NULL;
376         bool Write = false;
377         size_t len;
378
379         /*
380          * The state flags are part of the header
381          * and are descripted in the protocol description
382          * in vfs_smb_traffic_analyzer.h. They begin at byte
383          * 03 of the header.
384          */
385         char state_flags[9] = "000000\0";
386
387         SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
388
389         if (rf_sock == NULL || rf_sock->sock == -1) {
390                 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
391                         "closed\n"));
392                 return;
393         }
394
395         GetTimeOfDay(&tv);
396         tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
397         tm = localtime(&tv_sec);
398         if (!tm) {
399                 return;
400         }
401         seconds=(float) (tv.tv_usec / 1000);
402
403         /*
404          * Check if anonymization is required, and if yes do this only for
405          * the username here, needed vor protocol version 1. In v2 we
406          * additionally anonymize the SID, which is done in it's marshalling
407          * function.
408          */
409         username = smb_traffic_analyzer_anonymize( talloc_tos(),
410                         handle->conn->server_info->sanitized_username,
411                         handle);
412
413         if (!username) {
414                 return;
415         }
416
417         protocol_version = lp_parm_const_string(SNUM(handle->conn),
418                                         "smb_traffic_analyzer",
419                                         "protocol_version", NULL );
420
421
422         if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
423
424                 struct rw_data *s_data = (struct rw_data *) data;
425
426                 /*
427                  * in case of protocol v1, ignore any vfs operations
428                  * except read,pread,write,pwrite, and set the "Write"
429                  * bool accordingly, send data and return.
430                  */
431                 if ( vfs_operation > vfs_id_pwrite ) return;
432
433                 if ( vfs_operation <= vfs_id_pread ) Write=false;
434                         else Write=true;
435
436                 str = talloc_asprintf(talloc_tos(),
437                         "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
438                         "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
439                         (unsigned int) s_data->len,
440                         username,
441                         pdb_get_domain(handle->conn->server_info->sam_account),
442                         Write ? 'W' : 'R',
443                         handle->conn->connectpath,
444                         s_data->filename,
445                         tm->tm_year+1900,
446                         tm->tm_mon+1,
447                         tm->tm_mday,
448                         tm->tm_hour,
449                         tm->tm_min,
450                         tm->tm_sec,
451                         (int)seconds);
452                 if (write_data(rf_sock->sock, str, len) != len) {
453                         DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
454                         "error sending V1 protocol data to socket!\n"));
455                 return;
456                 }
457
458         } else if ( strcmp( protocol_version, "V2") == 0) {
459
460                 switch( vfs_operation ) {
461                 case vfs_id_mkdir: ;
462                         str = smb_traffic_analyzer_create_string( talloc_tos(),
463                                 tm, seconds, handle, username, vfs_id_mkdir, \
464                                 3, ((struct mkdir_data *) data)->path, \
465                                 talloc_asprintf( talloc_tos(), "%u", \
466                                 ((struct mkdir_data *) data)->mode), \
467                                 talloc_asprintf( talloc_tos(), "%u", \
468                                 ((struct mkdir_data *) data)->result ));
469                         break;
470                 case vfs_id_rmdir: ;
471                         str = smb_traffic_analyzer_create_string( talloc_tos(),
472                                 tm, seconds, handle, username, vfs_id_rmdir,
473                                 2, ((struct rmdir_data *) data)->path, \
474                                 talloc_asprintf( talloc_tos(), "%u", \
475                                 ((struct rmdir_data *) data)->result ));
476                         break;
477                 case vfs_id_rename: ;
478                         str = smb_traffic_analyzer_create_string( talloc_tos(),
479                                 tm, seconds, handle, username, vfs_id_rename,
480                                 3, ((struct rename_data *) data)->src, \
481                                 ((struct rename_data *) data)->dst,
482                                 talloc_asprintf(talloc_tos(), "%u", \
483                                 ((struct rename_data *) data)->result));
484                         break;
485                 case vfs_id_chdir: ;
486                         str = smb_traffic_analyzer_create_string( talloc_tos(),
487                                 tm, seconds, handle, username, vfs_id_chdir,
488                                 2, ((struct chdir_data *) data)->path, \
489                                 talloc_asprintf(talloc_tos(), "%u", \
490                                 ((struct chdir_data *) data)->result));
491                         break;
492
493                 case vfs_id_write:
494                 case vfs_id_pwrite:
495                 case vfs_id_read:
496                 case vfs_id_pread: ;
497                         str = smb_traffic_analyzer_create_string( talloc_tos(),
498                                 tm, seconds, handle, username, vfs_operation,
499                                 2, ((struct rw_data *) data)->filename, \
500                                 talloc_asprintf(talloc_tos(), "%u", \
501                                 ((struct rw_data *) data)->len));
502                         break;
503                 default:
504                         DEBUG(1, ("smb_traffic_analyzer: error! "
505                                 "wrong VFS operation id detected!\n"));
506                         return;
507                 }
508
509         } else {
510                 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
511                         "error, unkown protocol given!\n"));
512                 return;
513         }
514
515         if (!str) {
516                 DEBUG(1, ("smb_traffic_analyzer_send_data: "
517                         "unable to create string to send!\n"));
518                 return;
519         }
520
521
522         /*
523          * If configured, optain the key and run AES encryption
524          * over the data.
525          */
526         size_t size;
527         become_root();
528         char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
529         unbecome_root();
530         if ( akey != NULL ) {
531                 state_flags[2] = 'E';
532                 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: a key was"
533                         " found, encrypting data!\n"));
534                 char *output = smb_traffic_analyzer_encrypt( talloc_tos(),
535                                                 akey, str, &len);
536                 header = smb_traffic_analyzer_create_header( talloc_tos(),
537                                                 state_flags, len);
538
539                 DEBUG(10, ("smb_traffic_analyzer_send_data_socket:"
540                         " header created for crypted data: %s\n", header));
541                 smb_traffic_analyzer_write_data(header, output, len,
542                                                         rf_sock->sock);
543                 return;
544
545         }
546
547         len = strlen(str);
548         header = smb_traffic_analyzer_create_header( talloc_tos(),
549                                 state_flags, len);
550         smb_traffic_analyzer_write_data(header, str, strlen(str),
551                                 rf_sock->sock);
552
553 }
554
555 static struct refcounted_sock *sock_list;
556
557 static void smb_traffic_analyzer_free_data(void **pptr)
558 {
559         struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
560         if (rf_sock == NULL) {
561                 return;
562         }
563         rf_sock->ref_count--;
564         if (rf_sock->ref_count != 0) {
565                 return;
566         }
567         if (rf_sock->sock != -1) {
568                 close(rf_sock->sock);
569         }
570         DLIST_REMOVE(sock_list, rf_sock);
571         TALLOC_FREE(rf_sock);
572 }
573
574 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
575                          const char *service,
576                          const char *user)
577 {
578         connection_struct *conn = handle->conn;
579         enum sock_type st = smb_traffic_analyzer_connMode(handle);
580         struct refcounted_sock *rf_sock = NULL;
581         const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
582                                 lp_parm_const_string(SNUM(conn),
583                                         "smb_traffic_analyzer",
584                                 "host", "localhost");
585         uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
586                                 atoi( lp_parm_const_string(SNUM(conn),
587                                 "smb_traffic_analyzer", "port", "9430"));
588         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
589
590         if (ret < 0) {
591                 return ret;
592         }
593
594         /* Are we already connected ? */
595         for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
596                 if (port == rf_sock->port &&
597                                 (strcmp(name, rf_sock->name) == 0)) {
598                         break;
599                 }
600         }
601
602         /* If we're connected already, just increase the
603          * reference count. */
604         if (rf_sock) {
605                 rf_sock->ref_count++;
606         } else {
607                 /* New connection. */
608                 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
609                 if (rf_sock == NULL) {
610                         SMB_VFS_NEXT_DISCONNECT(handle);
611                         errno = ENOMEM;
612                         return -1;
613                 }
614                 rf_sock->name = talloc_strdup(rf_sock, name);
615                 if (rf_sock->name == NULL) {
616                         SMB_VFS_NEXT_DISCONNECT(handle);
617                         TALLOC_FREE(rf_sock);
618                         errno = ENOMEM;
619                         return -1;
620                 }
621                 rf_sock->port = port;
622                 rf_sock->ref_count = 1;
623
624                 if (st == UNIX_DOMAIN_SOCKET) {
625                         rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
626                                                         name);
627                 } else {
628
629                         rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
630                                                         name,
631                                                         port);
632                 }
633                 if (rf_sock->sock == -1) {
634                         SMB_VFS_NEXT_DISCONNECT(handle);
635                         TALLOC_FREE(rf_sock);
636                         return -1;
637                 }
638                 DLIST_ADD(sock_list, rf_sock);
639         }
640
641         /* Store the private data. */
642         SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
643                                 struct refcounted_sock, return -1);
644         return 0;
645 }
646
647 /* VFS Functions */
648 static int smb_traffic_analyzer_chdir(vfs_handle_struct *handle, \
649                         const char *path)
650 {
651         struct chdir_data s_data;
652         s_data.result = SMB_VFS_NEXT_CHDIR(handle, path);
653         s_data.path = path;
654         DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path));
655         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_chdir);
656         return s_data.result;
657 }
658
659 static int smb_traffic_analyzer_rename(vfs_handle_struct *handle, \
660                 const struct smb_filename *smb_fname_src,
661                 const struct smb_filename *smb_fname_dst)
662 {
663         struct rename_data s_data;
664         s_data.result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, \
665                 smb_fname_dst);
666         s_data.src = smb_fname_src->base_name;
667         s_data.dst = smb_fname_dst->base_name;
668         DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
669                 smb_fname_src->base_name,
670                 smb_fname_dst->base_name));
671         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rename);
672         return s_data.result;
673 }
674
675 static int smb_traffic_analyzer_rmdir(vfs_handle_struct *handle, \
676                         const char *path)
677 {
678         struct rmdir_data s_data;
679         s_data.result = SMB_VFS_NEXT_RMDIR(handle, path);
680         s_data.path = path;
681         DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path));
682         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rmdir);
683         return s_data.result;
684 }
685
686 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
687                         const char *path, mode_t mode)
688 {
689         struct mkdir_data s_data;
690         s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
691         s_data.path = path;
692         s_data.mode = mode;
693         DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
694         smb_traffic_analyzer_send_data(handle,
695                         &s_data,
696                         vfs_id_mkdir);
697         return s_data.result;
698 }
699
700 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
701                                 files_struct *fsp, void *data, size_t n)
702 {
703         struct rw_data s_data;
704
705         s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
706         s_data.filename = fsp->fsp_name->base_name;
707         DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
708
709         smb_traffic_analyzer_send_data(handle,
710                         &s_data,
711                         vfs_id_read);
712         return s_data.len;
713 }
714
715
716 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
717                 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
718 {
719         struct rw_data s_data;
720
721         s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
722         s_data.filename = fsp->fsp_name->base_name;
723         DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
724                    fsp_str_dbg(fsp)));
725
726         smb_traffic_analyzer_send_data(handle,
727                         &s_data,
728                         vfs_id_pread);
729
730         return s_data.len;
731 }
732
733 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
734                         files_struct *fsp, const void *data, size_t n)
735 {
736         struct rw_data s_data;
737
738         s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
739         s_data.filename = fsp->fsp_name->base_name;
740         DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
741                    fsp_str_dbg(fsp)));
742
743         smb_traffic_analyzer_send_data(handle,
744                         &s_data,
745                         vfs_id_write);
746         return s_data.len;
747 }
748
749 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
750              files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
751 {
752         struct rw_data s_data;
753
754         s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
755         s_data.filename = fsp->fsp_name->base_name;
756         DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
757                 fsp_str_dbg(fsp)));
758
759         smb_traffic_analyzer_send_data(handle,
760                         &s_data,
761                         vfs_id_pwrite);
762         return s_data.len;
763 }
764
765 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
766         .connect_fn = smb_traffic_analyzer_connect,
767         .vfs_read = smb_traffic_analyzer_read,
768         .pread = smb_traffic_analyzer_pread,
769         .write = smb_traffic_analyzer_write,
770         .pwrite = smb_traffic_analyzer_pwrite,
771         .mkdir = smb_traffic_analyzer_mkdir,
772         .rename = smb_traffic_analyzer_rename,
773         .chdir = smb_traffic_analyzer_chdir
774 };
775
776 /* Module initialization */
777 NTSTATUS vfs_smb_traffic_analyzer_init(void)
778 {
779         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
780                                         "smb_traffic_analyzer",
781                                         &vfs_smb_traffic_analyzer_fns);
782
783         if (!NT_STATUS_IS_OK(ret)) {
784                 return ret;
785         }
786
787         vfs_smb_traffic_analyzer_debug_level =
788                 debug_add_class("smb_traffic_analyzer");
789
790         if (vfs_smb_traffic_analyzer_debug_level == -1) {
791                 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
792                 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
793                          "debugging class!\n"));
794         } else {
795                 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
796                         "'smb_traffic_analyzer': %d\n", \
797                         vfs_smb_traffic_analyzer_debug_level));
798         }
799
800         return ret;
801 }