Simplify the code a bit by creating the functions:
[samba.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 /* The marshaller for the protocol version 2. */
232 static char *smb_traffic_analyzer_create_string( TALLOC_CTX *ctx,
233         struct tm *tm, int seconds, vfs_handle_struct *handle, \
234         char *username, int vfs_operation, int count, ... )
235 {
236         
237         va_list ap;
238         char *arg = NULL;
239         int len;
240         char *header = NULL;
241         char *buf = NULL;
242         char *timestr = NULL;
243         char *opstr = NULL;
244         char *sidstr = NULL;
245         char *userstr = NULL;
246         char *usersid = NULL;
247         const char *total_anonymization = NULL;
248         const char *anon_prefix = NULL;
249         /*
250          * first create the data that is transfered with any VFS op
251          * These are, in the following order:
252          *(0) number of data to come [6 in v2.0]
253          * 1.vfs_operation identifier
254          * 2.username
255          * 3.user-SID
256          * 4.affected share
257          * 5.domain
258          * 6.timestamp
259          */
260
261         /* number of common data blocks to come */
262         opstr = talloc_asprintf( ctx, "%i", SMBTA_COMMON_DATA_COUNT);
263         len = strlen(opstr);
264         buf = talloc_asprintf( ctx, "%04u%s", len, opstr);
265         talloc_free(opstr);
266         /* vfs operation identifier */
267         opstr = talloc_asprintf( ctx, "%i", vfs_operation);
268         len = strlen(opstr);
269         buf = talloc_asprintf_append( buf, "%04u%s", len, opstr);
270         talloc_free(opstr);
271         /*
272          * Handle anonymization. In protocol v2, we have to anonymize
273          * both the SID and the username.
274          */
275         total_anonymization=lp_parm_const_string(SNUM(handle->conn),
276                                         "smb_traffic_analyzer",
277                                         "total_anonymization", NULL);
278
279         anon_prefix=lp_parm_const_string(SNUM(handle->conn),
280                                         "smb_traffic_analyzer",
281                                         "anonymize_prefix", NULL );
282         usersid = dom_sid_string( ctx,
283                 &handle->conn->server_info->ptok->user_sids[0]);
284         if (anon_prefix != NULL) {
285                 if (total_anonymization != NULL) {
286                         userstr = talloc_asprintf(ctx, "%s",
287                                                         anon_prefix);                   
288                         sidstr = talloc_asprintf(ctx, "%s",
289                                                         anon_prefix);
290                 } else {
291                         userstr = talloc_asprintf(ctx, "%s%i",
292                                 anon_prefix,
293                                 str_checksum(username));
294                         sidstr = talloc_asprintf(ctx, "%s%i",
295                                 anon_prefix,
296                                 str_checksum(usersid));
297                 }
298         } else {
299                 userstr = username;
300                 sidstr = usersid;
301         }
302
303         /* username */
304         len = strlen( userstr );
305         buf = talloc_asprintf_append(buf, "%04u%s", len, userstr);
306         if (anon_prefix != NULL) talloc_free(userstr);
307         /* user SID */
308         len = strlen( sidstr );
309         buf = talloc_asprintf_append(buf, "%04u%s", len, sidstr);
310         if (anon_prefix != NULL) talloc_free(sidstr);
311         /* affected share */
312         len = strlen( handle->conn->connectpath );
313         buf = talloc_asprintf_append( buf, "%04u%s", len, \
314                 handle->conn->connectpath );
315         /* user's domain */
316         len = strlen( pdb_get_domain(handle->conn->server_info->sam_account) );
317         buf = talloc_asprintf_append( buf, "%04u%s", len, \
318                 pdb_get_domain(handle->conn->server_info->sam_account) );
319         /* time stamp */
320         timestr = talloc_asprintf( ctx, \
321                 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
322                 tm->tm_year+1900, \
323                 tm->tm_mon+1, \
324                 tm->tm_mday, \
325                 tm->tm_hour, \
326                 tm->tm_min, \
327                 tm->tm_sec, \
328                 (int)seconds);
329         len = strlen( timestr );
330         buf = talloc_asprintf_append( buf, "%04u%s", len, timestr);
331         talloc_free(timestr);
332         /* data blocks depending on the VFS function */ 
333         va_start( ap, count );
334         while ( count-- ) {
335                 arg = va_arg( ap, char * );
336                 /*
337                  *  protocol v2 sends a four byte string
338                  * as a header to each block, including
339                  * the numbers of bytes to come in the
340                  * next string.
341                  */
342                 len = strlen( arg );
343                 buf = talloc_asprintf_append( buf, "%04u%s", len, arg);
344         }
345         va_end( ap );
346         return buf;
347 }
348
349 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
350                                         void *data,
351                                         enum vfs_id vfs_operation )
352 {
353         struct refcounted_sock *rf_sock = NULL;
354         struct timeval tv;
355         time_t tv_sec;
356         struct tm *tm = NULL;
357         int seconds;
358         char *str = NULL;
359         char *username = NULL;
360         char *header = NULL;
361         const char *anon_prefix = NULL;
362         const char *total_anonymization = NULL;
363         const char *protocol_version = NULL;
364         bool Write = false;
365         size_t len;
366
367         /*
368          * The state flags are part of the header
369          * and are descripted in the protocol description
370          * in vfs_smb_traffic_analyzer.h. They begin at byte
371          * 03 of the header.
372          */
373         char state_flags[9] = "000000\0";
374
375         SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
376
377         if (rf_sock == NULL || rf_sock->sock == -1) {
378                 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
379                         "closed\n"));
380                 return;
381         }
382
383         GetTimeOfDay(&tv);
384         tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
385         tm = localtime(&tv_sec);
386         if (!tm) {
387                 return;
388         }
389         seconds=(float) (tv.tv_usec / 1000);
390
391         /*
392          * Check if anonymization is required, and if yes do this only if
393          * we run on protocol version 1. Anonynization for protocol v2 is
394          * handled in it's marshaller function.
395          */
396         total_anonymization=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",
397                                         "total_anonymization", NULL);
398
399         anon_prefix=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",\
400                                         "anonymize_prefix", NULL );
401
402         protocol_version = lp_parm_const_string(SNUM(handle->conn),
403                                         "smb_traffic_analyzer",
404                                         "protocol_version", NULL );
405
406         if (anon_prefix!=NULL && strcmp(protocol_version,"V2") != 0) {
407                 if (total_anonymization!=NULL) {
408                         username = talloc_asprintf(talloc_tos(),
409                                 "%s",
410                                 anon_prefix);
411                 } else {
412                         username = talloc_asprintf(talloc_tos(),
413                                 "%s%i",
414                                 anon_prefix,
415                                 str_checksum(
416                                         handle->conn->server_info->sanitized_username ) ); 
417                 }
418
419         } else {
420                 username = handle->conn->server_info->sanitized_username;
421         }
422
423         if (!username) {
424                 return;
425         }
426
427         if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
428
429                 struct rw_data *s_data = (struct rw_data *) data;
430
431                 /*
432                  * in case of protocol v1, ignore any vfs operations
433                  * except read,pread,write,pwrite, and set the "Write"
434                  * bool accordingly, send data and return.
435                  */
436                 if ( vfs_operation > vfs_id_pwrite ) return;
437
438                 if ( vfs_operation <= vfs_id_pread ) Write=false;
439                         else Write=true;
440
441                 str = talloc_asprintf(talloc_tos(),
442                         "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
443                         "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
444                         (unsigned int) s_data->len,
445                         username,
446                         pdb_get_domain(handle->conn->server_info->sam_account),
447                         Write ? 'W' : 'R',
448                         handle->conn->connectpath,
449                         s_data->filename,
450                         tm->tm_year+1900,
451                         tm->tm_mon+1,
452                         tm->tm_mday,
453                         tm->tm_hour,
454                         tm->tm_min,
455                         tm->tm_sec,
456                         (int)seconds);
457                 if (write_data(rf_sock->sock, str, len) != len) {
458                         DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
459                         "error sending V1 protocol data to socket!\n"));
460                 return;
461                 }
462
463         } else if ( strcmp( protocol_version, "V2") == 0) {
464
465                 switch( vfs_operation ) {
466                 case vfs_id_mkdir: ;
467                         str = smb_traffic_analyzer_create_string( talloc_tos(),
468                                 tm, seconds, handle, username, vfs_id_mkdir, \
469                                 3, ((struct mkdir_data *) data)->path, \
470                                 talloc_asprintf( talloc_tos(), "%u", \
471                                 ((struct mkdir_data *) data)->mode), \
472                                 talloc_asprintf( talloc_tos(), "%u", \
473                                 ((struct mkdir_data *) data)->result ));
474                         break;
475                 case vfs_id_rmdir: ;
476                         str = smb_traffic_analyzer_create_string( talloc_tos(),
477                                 tm, seconds, handle, username, vfs_id_rmdir,
478                                 2, ((struct rmdir_data *) data)->path, \
479                                 talloc_asprintf( talloc_tos(), "%u", \
480                                 ((struct rmdir_data *) data)->result ));
481                         break;
482                 case vfs_id_rename: ;
483                         str = smb_traffic_analyzer_create_string( talloc_tos(),
484                                 tm, seconds, handle, username, vfs_id_rename,
485                                 3, ((struct rename_data *) data)->src, \
486                                 ((struct rename_data *) data)->dst,
487                                 talloc_asprintf(talloc_tos(), "%u", \
488                                 ((struct rename_data *) data)->result));
489                         break;
490                 case vfs_id_chdir: ;
491                         str = smb_traffic_analyzer_create_string( talloc_tos(),
492                                 tm, seconds, handle, username, vfs_id_chdir,
493                                 2, ((struct chdir_data *) data)->path, \
494                                 talloc_asprintf(talloc_tos(), "%u", \
495                                 ((struct chdir_data *) data)->result));
496                         break;
497
498                 case vfs_id_write:
499                 case vfs_id_pwrite:
500                 case vfs_id_read:
501                 case vfs_id_pread: ;
502                         str = smb_traffic_analyzer_create_string( talloc_tos(),
503                                 tm, seconds, handle, username, vfs_operation,
504                                 2, ((struct rw_data *) data)->filename, \
505                                 talloc_asprintf(talloc_tos(), "%u", \
506                                 ((struct rw_data *) data)->len));
507                         break;
508                 default:
509                         DEBUG(1, ("smb_traffic_analyzer: error! "
510                                 "wrong VFS operation id detected!\n"));
511                         return;
512                 }
513
514         } else {
515                 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
516                         "error, unkown protocol given!\n"));
517                 return;
518         }
519
520         if (!str) {
521                 DEBUG(1, ("smb_traffic_analyzer_send_data: "
522                         "unable to create string to send!\n"));
523                 return;
524         }
525
526
527         /*
528          * If configured, optain the key and run AES encryption
529          * over the data.
530          */
531         size_t size;
532         become_root();
533         char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
534         unbecome_root();
535         if ( akey != NULL ) {
536                 state_flags[2] = 'E';
537                 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: a key was"
538                         " found, encrypting data!\n"));
539                 char *output = smb_traffic_analyzer_encrypt( talloc_tos(),
540                                                 akey, str, &len);
541                 header = smb_traffic_analyzer_create_header( talloc_tos(),
542                                                 state_flags, len);
543
544                 DEBUG(10, ("smb_traffic_analyzer_send_data_socket:"
545                         " header created for crypted data: %s\n", header));
546                 smb_traffic_analyzer_write_data(header, output, len,
547                                                         rf_sock->sock);
548                 return;
549
550         }
551
552         len = strlen(str);
553         header = smb_traffic_analyzer_create_header( talloc_tos(),
554                                 state_flags, len);
555         smb_traffic_analyzer_write_data(header, str, strlen(str),
556                                 rf_sock->sock);
557
558 }
559
560 static struct refcounted_sock *sock_list;
561
562 static void smb_traffic_analyzer_free_data(void **pptr)
563 {
564         struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
565         if (rf_sock == NULL) {
566                 return;
567         }
568         rf_sock->ref_count--;
569         if (rf_sock->ref_count != 0) {
570                 return;
571         }
572         if (rf_sock->sock != -1) {
573                 close(rf_sock->sock);
574         }
575         DLIST_REMOVE(sock_list, rf_sock);
576         TALLOC_FREE(rf_sock);
577 }
578
579 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
580                          const char *service,
581                          const char *user)
582 {
583         connection_struct *conn = handle->conn;
584         enum sock_type st = smb_traffic_analyzer_connMode(handle);
585         struct refcounted_sock *rf_sock = NULL;
586         const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
587                                 lp_parm_const_string(SNUM(conn),
588                                         "smb_traffic_analyzer",
589                                 "host", "localhost");
590         uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
591                                 atoi( lp_parm_const_string(SNUM(conn),
592                                 "smb_traffic_analyzer", "port", "9430"));
593         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
594
595         if (ret < 0) {
596                 return ret;
597         }
598
599         /* Are we already connected ? */
600         for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
601                 if (port == rf_sock->port &&
602                                 (strcmp(name, rf_sock->name) == 0)) {
603                         break;
604                 }
605         }
606
607         /* If we're connected already, just increase the
608          * reference count. */
609         if (rf_sock) {
610                 rf_sock->ref_count++;
611         } else {
612                 /* New connection. */
613                 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
614                 if (rf_sock == NULL) {
615                         SMB_VFS_NEXT_DISCONNECT(handle);
616                         errno = ENOMEM;
617                         return -1;
618                 }
619                 rf_sock->name = talloc_strdup(rf_sock, name);
620                 if (rf_sock->name == NULL) {
621                         SMB_VFS_NEXT_DISCONNECT(handle);
622                         TALLOC_FREE(rf_sock);
623                         errno = ENOMEM;
624                         return -1;
625                 }
626                 rf_sock->port = port;
627                 rf_sock->ref_count = 1;
628
629                 if (st == UNIX_DOMAIN_SOCKET) {
630                         rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
631                                                         name);
632                 } else {
633
634                         rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
635                                                         name,
636                                                         port);
637                 }
638                 if (rf_sock->sock == -1) {
639                         SMB_VFS_NEXT_DISCONNECT(handle);
640                         TALLOC_FREE(rf_sock);
641                         return -1;
642                 }
643                 DLIST_ADD(sock_list, rf_sock);
644         }
645
646         /* Store the private data. */
647         SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
648                                 struct refcounted_sock, return -1);
649         return 0;
650 }
651
652 /* VFS Functions */
653 static int smb_traffic_analyzer_chdir(vfs_handle_struct *handle, \
654                         const char *path)
655 {
656         struct chdir_data s_data;
657         s_data.result = SMB_VFS_NEXT_CHDIR(handle, path);
658         s_data.path = path;
659         DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path));
660         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_chdir);
661         return s_data.result;
662 }
663
664 static int smb_traffic_analyzer_rename(vfs_handle_struct *handle, \
665                 const struct smb_filename *smb_fname_src,
666                 const struct smb_filename *smb_fname_dst)
667 {
668         struct rename_data s_data;
669         s_data.result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, \
670                 smb_fname_dst);
671         s_data.src = smb_fname_src->base_name;
672         s_data.dst = smb_fname_dst->base_name;
673         DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
674                 smb_fname_src->base_name,
675                 smb_fname_dst->base_name));
676         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rename);
677         return s_data.result;
678 }
679
680 static int smb_traffic_analyzer_rmdir(vfs_handle_struct *handle, \
681                         const char *path)
682 {
683         struct rmdir_data s_data;
684         s_data.result = SMB_VFS_NEXT_RMDIR(handle, path);
685         s_data.path = path;
686         DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path));
687         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rmdir);
688         return s_data.result;
689 }
690
691 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
692                         const char *path, mode_t mode)
693 {
694         struct mkdir_data s_data;
695         s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
696         s_data.path = path;
697         s_data.mode = mode;
698         DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
699         smb_traffic_analyzer_send_data(handle,
700                         &s_data,
701                         vfs_id_mkdir);
702         return s_data.result;
703 }
704
705 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
706                                 files_struct *fsp, void *data, size_t n)
707 {
708         struct rw_data s_data;
709
710         s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
711         s_data.filename = fsp->fsp_name->base_name;
712         DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
713
714         smb_traffic_analyzer_send_data(handle,
715                         &s_data,
716                         vfs_id_read);
717         return s_data.len;
718 }
719
720
721 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
722                 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
723 {
724         struct rw_data s_data;
725
726         s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
727         s_data.filename = fsp->fsp_name->base_name;
728         DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
729                    fsp_str_dbg(fsp)));
730
731         smb_traffic_analyzer_send_data(handle,
732                         &s_data,
733                         vfs_id_pread);
734
735         return s_data.len;
736 }
737
738 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
739                         files_struct *fsp, const void *data, size_t n)
740 {
741         struct rw_data s_data;
742
743         s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
744         s_data.filename = fsp->fsp_name->base_name;
745         DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
746                    fsp_str_dbg(fsp)));
747
748         smb_traffic_analyzer_send_data(handle,
749                         &s_data,
750                         vfs_id_write);
751         return s_data.len;
752 }
753
754 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
755              files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
756 {
757         struct rw_data s_data;
758
759         s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
760         s_data.filename = fsp->fsp_name->base_name;
761         DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
762                 fsp_str_dbg(fsp)));
763
764         smb_traffic_analyzer_send_data(handle,
765                         &s_data,
766                         vfs_id_pwrite);
767         return s_data.len;
768 }
769
770 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
771         .connect_fn = smb_traffic_analyzer_connect,
772         .vfs_read = smb_traffic_analyzer_read,
773         .pread = smb_traffic_analyzer_pread,
774         .write = smb_traffic_analyzer_write,
775         .pwrite = smb_traffic_analyzer_pwrite,
776         .mkdir = smb_traffic_analyzer_mkdir,
777         .rename = smb_traffic_analyzer_rename,
778         .chdir = smb_traffic_analyzer_chdir
779 };
780
781 /* Module initialization */
782 NTSTATUS vfs_smb_traffic_analyzer_init(void)
783 {
784         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
785                                         "smb_traffic_analyzer",
786                                         &vfs_smb_traffic_analyzer_fns);
787
788         if (!NT_STATUS_IS_OK(ret)) {
789                 return ret;
790         }
791
792         vfs_smb_traffic_analyzer_debug_level =
793                 debug_add_class("smb_traffic_analyzer");
794
795         if (vfs_smb_traffic_analyzer_debug_level == -1) {
796                 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
797                 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
798                          "debugging class!\n"));
799         } else {
800                 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
801                         "'smb_traffic_analyzer': %d\n", \
802                         vfs_smb_traffic_analyzer_debug_level));
803         }
804
805         return ret;
806 }