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