s3:auth use info3 in auth_serversupplied_info
[amitay/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-2010
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         unsigned char filler[17]= "................";
168         char *output;
169         unsigned char crypted[18];
170         if (akey == NULL) return NULL;
171         samba_AES_set_encrypt_key((unsigned char *) 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((unsigned char *) str+(16*h), crypted, &key);
181                 for (d = 0; d<16; d++) output[d+(16*h)]=crypted[d];
182         }
183         samba_AES_encrypt( (unsigned char *) 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, (unsigned int) data_len);
200         DEBUG(10, ("smb_traffic_analyzer_send_data_socket: created Header:\n"));
201         dump_data(10, (uint8_t *)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, (uint8_t *)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 /**
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
283  *
284  * Returns the complete data block to send. The caller has to
285  * take care for freeing the allocated buffer.
286  */
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, ... )
290 {
291         
292         va_list ap;
293         char *arg = NULL;
294         int len;
295         char *common_data_count_str = NULL;
296         char *timestr = NULL;
297         char *sidstr = NULL;
298         char *usersid = NULL;
299         char *buf = NULL;
300         char *vfs_operation_str = NULL;
301         /*
302          * first create the data that is transfered with any VFS op
303          * These are, in the following order:
304          *(0) number of data to come [6 in v2.0]
305          * 1.vfs_operation identifier
306          * 2.username
307          * 3.user-SID
308          * 4.affected share
309          * 5.domain
310          * 6.timestamp
311          */
312
313         /*
314          * number of common data blocks to come,
315          * this is a #define in vfs_smb_traffic_anaylzer.h,
316          * it's length is known at compile time
317          */
318         common_data_count_str = talloc_strdup( ctx, SMBTA_COMMON_DATA_COUNT);
319         /* vfs operation identifier */
320         vfs_operation_str = talloc_asprintf( common_data_count_str, "%i",
321                                                         vfs_operation);
322         /*
323          * Handle anonymization. In protocol v2, we have to anonymize
324          * both the SID and the username. The name is already
325          * anonymized if needed, by the calling function.
326          */
327         usersid = dom_sid_string( common_data_count_str,
328                 &handle->conn->server_info->ptok->user_sids[0]);
329
330         sidstr = smb_traffic_analyzer_anonymize(
331                 common_data_count_str,
332                 usersid,
333                 handle);
334         
335         /* time stamp */
336         timestr = talloc_asprintf( common_data_count_str, \
337                 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
338                 tm->tm_year+1900, \
339                 tm->tm_mon+1, \
340                 tm->tm_mday, \
341                 tm->tm_hour, \
342                 tm->tm_min, \
343                 tm->tm_sec, \
344                 (int)seconds);
345         len = strlen( timestr );
346         /* create the string of common data */
347         buf = talloc_asprintf(ctx,
348                 "%s%04u%s%04u%s%04u%s%04u%s%04u%s%04u%s",
349                 common_data_count_str,
350                 (unsigned int) strlen(vfs_operation_str),
351                 vfs_operation_str,
352                 (unsigned int) strlen(username),
353                 username,
354                 (unsigned int) strlen(sidstr),
355                 sidstr,
356                 (unsigned int) strlen(handle->conn->connectpath),
357                 handle->conn->connectpath,
358                 (unsigned int)
359                 strlen(handle->conn->server_info->info3->base.domain.string),
360                 handle->conn->server_info->info3->base.domain.string,
361                 (unsigned int) strlen(timestr),
362                 timestr);
363
364         talloc_free(common_data_count_str);
365
366         /* data blocks depending on the VFS function */ 
367         va_start( ap, count );
368         while ( count-- ) {
369                 arg = va_arg( ap, char * );
370                 /*
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.
375                  */
376                 len = strlen( arg );
377                 buf = talloc_asprintf_append( buf, "%04u%s", len, arg);
378         }
379         va_end( ap );
380         return buf;
381 }
382
383 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
384                                         void *data,
385                                         enum vfs_id vfs_operation )
386 {
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;
398         size_t size;
399         char *akey, *output;
400
401         /*
402          * The state flags are part of the header
403          * and are descripted in the protocol description
404          * in vfs_smb_traffic_analyzer.h. They begin at byte
405          * 03 of the header.
406          */
407         char state_flags[9] = "000000\0";
408
409         SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
410
411         if (rf_sock == NULL || rf_sock->sock == -1) {
412                 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
413                         "closed\n"));
414                 return;
415         }
416
417         GetTimeOfDay(&tv);
418         tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
419         tm = localtime(&tv_sec);
420         if (!tm) {
421                 return;
422         }
423         seconds=(float) (tv.tv_usec / 1000);
424
425         /*
426          * Check if anonymization is required, and if yes do this only for
427          * the username here, needed vor protocol version 1. In v2 we
428          * additionally anonymize the SID, which is done in it's marshalling
429          * function.
430          */
431         username = smb_traffic_analyzer_anonymize( talloc_tos(),
432                         handle->conn->server_info->sanitized_username,
433                         handle);
434
435         if (!username) {
436                 return;
437         }
438
439         protocol_version = lp_parm_const_string(SNUM(handle->conn),
440                                         "smb_traffic_analyzer",
441                                         "protocol_version", NULL );
442
443
444         if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
445
446                 struct rw_data *s_data = (struct rw_data *) data;
447
448                 /*
449                  * in case of protocol v1, ignore any vfs operations
450                  * except read,pread,write,pwrite, and set the "Write"
451                  * bool accordingly, send data and return.
452                  */
453                 if ( vfs_operation > vfs_id_pwrite ) return;
454
455                 if ( vfs_operation <= vfs_id_pread ) Write=false;
456                         else Write=true;
457
458                 str = talloc_asprintf(talloc_tos(),
459                         "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
460                         "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
461                         (unsigned int) s_data->len,
462                         username,
463                         handle->conn->server_info->info3->base.domain.string,
464                         Write ? 'W' : 'R',
465                         handle->conn->connectpath,
466                         s_data->filename,
467                         tm->tm_year+1900,
468                         tm->tm_mon+1,
469                         tm->tm_mday,
470                         tm->tm_hour,
471                         tm->tm_min,
472                         tm->tm_sec,
473                         (int)seconds);
474                 len = strlen(str);
475                 if (write_data(rf_sock->sock, str, len) != len) {
476                         DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
477                         "error sending V1 protocol data to socket!\n"));
478                 return;
479                 }
480
481         } else if ( strcmp( protocol_version, "V2") == 0) {
482
483                 switch( vfs_operation ) {
484                 case vfs_id_open: ;
485                         str = smb_traffic_analyzer_create_string( talloc_tos(),
486                                 tm, seconds, handle, username, vfs_id_open,
487                                 3, ((struct open_data *) data)->filename,
488                                 talloc_asprintf( talloc_tos(), "%u",
489                                 ((struct open_data *) data)->mode),
490                                 talloc_asprintf( talloc_tos(), "%u",
491                                 ((struct open_data *) data)->result));
492                         break;
493                 case vfs_id_close: ;
494                         str = smb_traffic_analyzer_create_string( talloc_tos(),
495                                 tm, seconds, handle, username, vfs_id_close,
496                                 2, ((struct close_data *) data)->filename,
497                                 talloc_asprintf( talloc_tos(), "%u",
498                                 ((struct close_data *) data)->result));
499                         break;
500                 case vfs_id_mkdir: ;
501                         str = smb_traffic_analyzer_create_string( talloc_tos(),
502                                 tm, seconds, handle, username, vfs_id_mkdir, \
503                                 3, ((struct mkdir_data *) data)->path, \
504                                 talloc_asprintf( talloc_tos(), "%u", \
505                                 ((struct mkdir_data *) data)->mode), \
506                                 talloc_asprintf( talloc_tos(), "%u", \
507                                 ((struct mkdir_data *) data)->result ));
508                         break;
509                 case vfs_id_rmdir: ;
510                         str = smb_traffic_analyzer_create_string( talloc_tos(),
511                                 tm, seconds, handle, username, vfs_id_rmdir,
512                                 2, ((struct rmdir_data *) data)->path, \
513                                 talloc_asprintf( talloc_tos(), "%u", \
514                                 ((struct rmdir_data *) data)->result ));
515                         break;
516                 case vfs_id_rename: ;
517                         str = smb_traffic_analyzer_create_string( talloc_tos(),
518                                 tm, seconds, handle, username, vfs_id_rename,
519                                 3, ((struct rename_data *) data)->src, \
520                                 ((struct rename_data *) data)->dst,
521                                 talloc_asprintf(talloc_tos(), "%u", \
522                                 ((struct rename_data *) data)->result));
523                         break;
524                 case vfs_id_chdir: ;
525                         str = smb_traffic_analyzer_create_string( talloc_tos(),
526                                 tm, seconds, handle, username, vfs_id_chdir,
527                                 2, ((struct chdir_data *) data)->path, \
528                                 talloc_asprintf(talloc_tos(), "%u", \
529                                 ((struct chdir_data *) data)->result));
530                         break;
531
532                 case vfs_id_write:
533                 case vfs_id_pwrite:
534                 case vfs_id_read:
535                 case vfs_id_pread: ;
536                         str = smb_traffic_analyzer_create_string( talloc_tos(),
537                                 tm, seconds, handle, username, vfs_operation,
538                                 2, ((struct rw_data *) data)->filename, \
539                                 talloc_asprintf(talloc_tos(), "%u", \
540                                 (unsigned int)
541                                         ((struct rw_data *) data)->len));
542                         break;
543                 default:
544                         DEBUG(1, ("smb_traffic_analyzer: error! "
545                                 "wrong VFS operation id detected!\n"));
546                         return;
547                 }
548
549         } else {
550                 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
551                         "error, unkown protocol given!\n"));
552                 return;
553         }
554
555         if (!str) {
556                 DEBUG(1, ("smb_traffic_analyzer_send_data: "
557                         "unable to create string to send!\n"));
558                 return;
559         }
560
561
562         /*
563          * If configured, optain the key and run AES encryption
564          * over the data.
565          */
566         become_root();
567         akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
568         unbecome_root();
569         if ( akey != NULL ) {
570                 state_flags[2] = 'E';
571                 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: a key was"
572                         " found, encrypting data!\n"));
573                 output = smb_traffic_analyzer_encrypt( talloc_tos(),
574                                                 akey, str, &len);
575                 header = smb_traffic_analyzer_create_header( talloc_tos(),
576                                                 state_flags, len);
577
578                 DEBUG(10, ("smb_traffic_analyzer_send_data_socket:"
579                         " header created for crypted data: %s\n", header));
580                 smb_traffic_analyzer_write_data(header, output, len,
581                                                         rf_sock->sock);
582                 return;
583
584         }
585
586         len = strlen(str);
587         header = smb_traffic_analyzer_create_header( talloc_tos(),
588                                 state_flags, len);
589         smb_traffic_analyzer_write_data(header, str, strlen(str),
590                                 rf_sock->sock);
591
592 }
593
594 static struct refcounted_sock *sock_list;
595
596 static void smb_traffic_analyzer_free_data(void **pptr)
597 {
598         struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
599         if (rf_sock == NULL) {
600                 return;
601         }
602         rf_sock->ref_count--;
603         if (rf_sock->ref_count != 0) {
604                 return;
605         }
606         if (rf_sock->sock != -1) {
607                 close(rf_sock->sock);
608         }
609         DLIST_REMOVE(sock_list, rf_sock);
610         TALLOC_FREE(rf_sock);
611 }
612
613 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
614                          const char *service,
615                          const char *user)
616 {
617         connection_struct *conn = handle->conn;
618         enum sock_type st = smb_traffic_analyzer_connMode(handle);
619         struct refcounted_sock *rf_sock = NULL;
620         const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
621                                 lp_parm_const_string(SNUM(conn),
622                                         "smb_traffic_analyzer",
623                                 "host", "localhost");
624         uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
625                                 atoi( lp_parm_const_string(SNUM(conn),
626                                 "smb_traffic_analyzer", "port", "9430"));
627         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
628
629         if (ret < 0) {
630                 return ret;
631         }
632
633         /* Are we already connected ? */
634         for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
635                 if (port == rf_sock->port &&
636                                 (strcmp(name, rf_sock->name) == 0)) {
637                         break;
638                 }
639         }
640
641         /* If we're connected already, just increase the
642          * reference count. */
643         if (rf_sock) {
644                 rf_sock->ref_count++;
645         } else {
646                 /* New connection. */
647                 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
648                 if (rf_sock == NULL) {
649                         SMB_VFS_NEXT_DISCONNECT(handle);
650                         errno = ENOMEM;
651                         return -1;
652                 }
653                 rf_sock->name = talloc_strdup(rf_sock, name);
654                 if (rf_sock->name == NULL) {
655                         SMB_VFS_NEXT_DISCONNECT(handle);
656                         TALLOC_FREE(rf_sock);
657                         errno = ENOMEM;
658                         return -1;
659                 }
660                 rf_sock->port = port;
661                 rf_sock->ref_count = 1;
662
663                 if (st == UNIX_DOMAIN_SOCKET) {
664                         rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
665                                                         name);
666                 } else {
667
668                         rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
669                                                         name,
670                                                         port);
671                 }
672                 if (rf_sock->sock == -1) {
673                         SMB_VFS_NEXT_DISCONNECT(handle);
674                         TALLOC_FREE(rf_sock);
675                         return -1;
676                 }
677                 DLIST_ADD(sock_list, rf_sock);
678         }
679
680         /* Store the private data. */
681         SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
682                                 struct refcounted_sock, return -1);
683         return 0;
684 }
685
686 /* VFS Functions */
687 static int smb_traffic_analyzer_chdir(vfs_handle_struct *handle, \
688                         const char *path)
689 {
690         struct chdir_data s_data;
691         s_data.result = SMB_VFS_NEXT_CHDIR(handle, path);
692         s_data.path = path;
693         DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path));
694         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_chdir);
695         return s_data.result;
696 }
697
698 static int smb_traffic_analyzer_rename(vfs_handle_struct *handle, \
699                 const struct smb_filename *smb_fname_src,
700                 const struct smb_filename *smb_fname_dst)
701 {
702         struct rename_data s_data;
703         s_data.result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, \
704                 smb_fname_dst);
705         s_data.src = smb_fname_src->base_name;
706         s_data.dst = smb_fname_dst->base_name;
707         DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
708                 smb_fname_src->base_name,
709                 smb_fname_dst->base_name));
710         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rename);
711         return s_data.result;
712 }
713
714 static int smb_traffic_analyzer_rmdir(vfs_handle_struct *handle, \
715                         const char *path)
716 {
717         struct rmdir_data s_data;
718         s_data.result = SMB_VFS_NEXT_RMDIR(handle, path);
719         s_data.path = path;
720         DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path));
721         smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rmdir);
722         return s_data.result;
723 }
724
725 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
726                         const char *path, mode_t mode)
727 {
728         struct mkdir_data s_data;
729         s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
730         s_data.path = path;
731         s_data.mode = mode;
732         DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
733         smb_traffic_analyzer_send_data(handle,
734                         &s_data,
735                         vfs_id_mkdir);
736         return s_data.result;
737 }
738
739 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
740                                 files_struct *fsp, void *data, size_t n)
741 {
742         struct rw_data s_data;
743
744         s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
745         s_data.filename = fsp->fsp_name->base_name;
746         DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
747
748         smb_traffic_analyzer_send_data(handle,
749                         &s_data,
750                         vfs_id_read);
751         return s_data.len;
752 }
753
754
755 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
756                 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
757 {
758         struct rw_data s_data;
759
760         s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
761         s_data.filename = fsp->fsp_name->base_name;
762         DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
763                    fsp_str_dbg(fsp)));
764
765         smb_traffic_analyzer_send_data(handle,
766                         &s_data,
767                         vfs_id_pread);
768
769         return s_data.len;
770 }
771
772 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
773                         files_struct *fsp, const void *data, size_t n)
774 {
775         struct rw_data s_data;
776
777         s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
778         s_data.filename = fsp->fsp_name->base_name;
779         DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
780                    fsp_str_dbg(fsp)));
781
782         smb_traffic_analyzer_send_data(handle,
783                         &s_data,
784                         vfs_id_write);
785         return s_data.len;
786 }
787
788 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
789              files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
790 {
791         struct rw_data s_data;
792
793         s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
794         s_data.filename = fsp->fsp_name->base_name;
795         DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
796                 fsp_str_dbg(fsp)));
797
798         smb_traffic_analyzer_send_data(handle,
799                         &s_data,
800                         vfs_id_pwrite);
801         return s_data.len;
802 }
803
804 static int smb_traffic_analyzer_open(vfs_handle_struct *handle, \
805         struct smb_filename *smb_fname, files_struct *fsp,\
806         int flags, mode_t mode)
807 {
808         struct open_data s_data;
809
810         s_data.result = SMB_VFS_NEXT_OPEN( handle, smb_fname, fsp,
811                         flags, mode);
812         DEBUG(10,("smb_traffic_analyzer_open: OPEN: %s\n",
813                 fsp_str_dbg(fsp)));
814         s_data.filename = fsp->fsp_name->base_name;
815         s_data.mode = mode;
816         smb_traffic_analyzer_send_data(handle,
817                         &s_data,
818                         vfs_id_open);
819         return s_data.result;
820 }
821
822 static int smb_traffic_analyzer_close(vfs_handle_struct *handle, \
823         files_struct *fsp)
824 {
825         struct close_data s_data;
826         s_data.result = SMB_VFS_NEXT_CLOSE(handle, fsp);
827         DEBUG(10,("smb_traffic_analyzer_close: CLOSE: %s\n",
828                 fsp_str_dbg(fsp)));
829         s_data.filename = fsp->fsp_name->base_name;
830         smb_traffic_analyzer_send_data(handle,
831                         &s_data,
832                         vfs_id_close);
833         return s_data.result;
834 }
835
836         
837 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
838         .connect_fn = smb_traffic_analyzer_connect,
839         .vfs_read = smb_traffic_analyzer_read,
840         .pread = smb_traffic_analyzer_pread,
841         .write = smb_traffic_analyzer_write,
842         .pwrite = smb_traffic_analyzer_pwrite,
843         .mkdir = smb_traffic_analyzer_mkdir,
844         .rename = smb_traffic_analyzer_rename,
845         .chdir = smb_traffic_analyzer_chdir,
846         .open = smb_traffic_analyzer_open,
847         .rmdir = smb_traffic_analyzer_rmdir,
848         .close_fn = smb_traffic_analyzer_close
849 };
850
851 /* Module initialization */
852 NTSTATUS vfs_smb_traffic_analyzer_init(void)
853 {
854         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
855                                         "smb_traffic_analyzer",
856                                         &vfs_smb_traffic_analyzer_fns);
857
858         if (!NT_STATUS_IS_OK(ret)) {
859                 return ret;
860         }
861
862         vfs_smb_traffic_analyzer_debug_level =
863                 debug_add_class("smb_traffic_analyzer");
864
865         if (vfs_smb_traffic_analyzer_debug_level == -1) {
866                 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
867                 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
868                          "debugging class!\n"));
869         } else {
870                 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
871                         "'smb_traffic_analyzer': %d\n", \
872                         vfs_smb_traffic_analyzer_debug_level));
873         }
874
875         return ret;
876 }