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