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