Enable AES encryption of the data if a key was found in secrets.tdb.
[Samba/ekacnet.git] / source3 / modules / vfs_smb_traffic_analyzer.c
blob68a4991e0f4c33a7bbec1536511918f29fc6d07b
1 /*
2 * traffic-analyzer VFS module. Measure the smb traffic users create
3 * on the net.
5 * Copyright (C) Holger Hetterich, 2008
6 * Copyright (C) Jeremy Allison, 2008
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.
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.
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/>.
22 #include "includes.h"
23 #include "../lib/crypto/crypto.h"
25 /* abstraction for the send_over_network function */
26 enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};
28 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
30 /* VFS Functions identifier table. In protocol version 2, every vfs */
31 /* function is given a unique id. */
32 enum vfs_id {
33 /* care for the order here, required for compatibility */
34 /* with protocol version 1. */
35 vfs_id_read,
36 vfs_id_pread,
37 vfs_id_write,
38 vfs_id_pwrite,
39 /* end of protocol version 1 identifiers. */
40 vfs_id_mkdir,
41 vfs_id_rmdir,
42 vfs_id_rename,
43 vfs_id_chdir
46 /* Specific data sets for the VFS functions. */
48 typedef struct mkdir_data {
49 const char *path;
50 mode_t mode;
51 int result;
52 } t_mkdir;
54 typedef struct rmdir_data {
55 const char *path;
56 int result;
57 } t_rmdir;
59 typedef struct rename_data {
60 const char *src;
61 const char *dst;
62 int result;
63 } t_rename;
65 typedef struct chdir_data {
66 const char *path;
67 int result;
68 } t_chdir;
70 /* rw_data used for read/write/pread/pwrite */
71 struct rw_data {
72 char *filename;
73 size_t len;
77 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
79 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
81 connection_struct *conn = handle->conn;
82 const char *Mode;
83 Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
84 "internet_socket");
85 if (strstr(Mode,"unix_domain_socket")) {
86 return UNIX_DOMAIN_SOCKET;
87 } else {
88 return INTERNET_SOCKET;
93 /* Connect to an internet socket */
95 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle,
96 const char *name, uint16_t port)
98 /* Create a streaming Socket */
99 int sockfd = -1;
100 struct addrinfo hints;
101 struct addrinfo *ailist = NULL;
102 struct addrinfo *res = NULL;
103 int ret;
105 ZERO_STRUCT(hints);
106 /* By default make sure it supports TCP. */
107 hints.ai_socktype = SOCK_STREAM;
108 hints.ai_flags = AI_ADDRCONFIG;
110 ret = getaddrinfo(name,
111 NULL,
112 &hints,
113 &ailist);
115 if (ret) {
116 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
117 "getaddrinfo failed for name %s [%s]\n",
118 name,
119 gai_strerror(ret) ));
120 return -1;
123 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
124 "Port: %i\n", name, port));
126 for (res = ailist; res; res = res->ai_next) {
127 struct sockaddr_storage ss;
128 NTSTATUS status;
130 if (!res->ai_addr || res->ai_addrlen == 0) {
131 continue;
134 ZERO_STRUCT(ss);
135 memcpy(&ss, res->ai_addr, res->ai_addrlen);
137 status = open_socket_out(&ss, port, 10000, &sockfd);
138 if (NT_STATUS_IS_OK(status)) {
139 break;
143 if (ailist) {
144 freeaddrinfo(ailist);
147 if (sockfd == -1) {
148 DEBUG(1, ("smb_traffic_analyzer: unable to create "
149 "socket, error is %s",
150 strerror(errno)));
151 return -1;
154 return sockfd;
157 /* Connect to a unix domain socket */
159 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
160 const char *name)
162 /* Create the socket to stad */
163 int len, sock;
164 struct sockaddr_un remote;
166 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
167 "Unix domain socket mode. Using %s\n",
168 name ));
170 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
171 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
172 "Couldn't create socket, "
173 "make sure stad is running!\n"));
174 return -1;
176 remote.sun_family = AF_UNIX;
177 strlcpy(remote.sun_path, name,
178 sizeof(remote.sun_path));
179 len=strlen(remote.sun_path) + sizeof(remote.sun_family);
180 if (connect(sock, (struct sockaddr *)&remote, len) == -1 ) {
181 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
182 "Could not connect to "
183 "socket, make sure\nstad is running!\n"));
184 close(sock);
185 return -1;
187 return sock;
190 /* Private data allowing shared connection sockets. */
192 struct refcounted_sock {
193 struct refcounted_sock *next, *prev;
194 char *name;
195 uint16_t port;
196 int sock;
197 unsigned int ref_count;
201 /* The marshaller for the protocol version 2. */
202 static char *smb_traffic_analyzer_create_string( struct tm *tm, \
203 int seconds, vfs_handle_struct *handle, \
204 char *username, int count, ... )
207 va_list ap;
208 char *arg = NULL;
209 char *str = NULL;
210 int len;
211 char *header = NULL;
212 char *buf = NULL;
213 char *timestr = NULL;
215 /* first create the data that is transfered with any VFS op */
216 len = strlen( username );
217 buf = talloc_asprintf(talloc_tos(),"%04u%s", len, username);
218 len = strlen( handle->conn->connectpath );
219 buf = talloc_asprintf_append( buf, "%04u%s", len, \
220 handle->conn->connectpath );
221 len = strlen( pdb_get_domain(handle->conn->server_info->sam_account) );
222 buf = talloc_asprintf_append( buf, "%04u%s", len, \
223 pdb_get_domain(handle->conn->server_info->sam_account) );
224 timestr = talloc_asprintf(talloc_tos(), \
225 "%04d-%02d-%02d %02d:%02d:%02d.%03d", \
226 tm->tm_year+1900, \
227 tm->tm_mon+1, \
228 tm->tm_mday, \
229 tm->tm_hour, \
230 tm->tm_min, \
231 tm->tm_sec, \
232 (int)seconds);
233 len = strlen( timestr );
234 buf = talloc_asprintf_append( buf, "%04u%s", len, timestr);
236 va_start( ap, count );
237 while ( count-- ) {
238 arg = va_arg( ap, char * );
239 /* protocol v2 sends a four byte string */
240 /* as a header to each block, including */
241 /* the numbers of bytes to come in the */
242 /* next string. */
243 len = strlen( arg );
244 buf = talloc_asprintf_append( buf, "%04u%s", len, arg);
246 va_end( ap );
248 /* now create the protocol v2 header. */
249 len = strlen( buf );
250 str = talloc_asprintf_append( str, "V2,%017u%s", len, buf);
251 DEBUG(10, ("smb_traffic_analyzer_create_string: %s\n",str));
252 return str;
255 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
256 void *data,
257 enum vfs_id vfs_operation )
259 struct refcounted_sock *rf_sock = NULL;
260 struct timeval tv;
261 time_t tv_sec;
262 struct tm *tm = NULL;
263 int seconds;
264 char *str = NULL;
265 char *username = NULL;
266 const char *anon_prefix = NULL;
267 const char *total_anonymization = NULL;
268 const char *protocol_version = NULL;
269 bool Write = false;
270 size_t len;
272 SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
274 if (rf_sock == NULL || rf_sock->sock == -1) {
275 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
276 "closed\n"));
277 return;
280 GetTimeOfDay(&tv);
281 tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
282 tm = localtime(&tv_sec);
283 if (!tm) {
284 return;
286 seconds=(float) (tv.tv_usec / 1000);
288 /* check if anonymization is required */
290 total_anonymization=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",
291 "total_anonymization", NULL);
293 anon_prefix=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",\
294 "anonymize_prefix", NULL );
295 if (anon_prefix!=NULL) {
296 if (total_anonymization!=NULL) {
297 username = talloc_asprintf(talloc_tos(),
298 "%s",
299 anon_prefix);
300 } else {
301 username = talloc_asprintf(talloc_tos(),
302 "%s%i",
303 anon_prefix,
304 str_checksum(
305 handle->conn->server_info->sanitized_username ) );
308 } else {
309 username = handle->conn->server_info->sanitized_username;
312 if (!username) {
313 return;
316 protocol_version = lp_parm_const_string(SNUM(handle->conn),
317 "smb_traffic_analyzer",
318 "protocol_version", NULL );
320 if ( protocol_version == NULL || strcmp( protocol_version,"V1") == 0) {
322 struct rw_data *s_data = (struct rw_data *) data;
324 /* in case of protocol v1, ignore any vfs operations */
325 /* except read,pread,write,pwrite, and set the "Write" */
326 /* bool accordingly. */
328 if ( vfs_operation > vfs_id_pwrite ) return;
330 if ( vfs_operation <= vfs_id_pread ) Write=false;
331 else Write=true;
333 str = talloc_asprintf(talloc_tos(),
334 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
335 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
336 (unsigned int) s_data->len,
337 username,
338 pdb_get_domain(handle->conn->server_info->sam_account),
339 Write ? 'W' : 'R',
340 handle->conn->connectpath,
341 s_data->filename,
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 } else if ( strcmp( protocol_version, "V2") == 0) {
351 switch( vfs_operation ) {
352 case vfs_id_mkdir: ;
353 str = smb_traffic_analyzer_create_string( tm, \
354 seconds, handle, username, \
355 3, ((t_mkdir *) data)->path, \
356 talloc_asprintf( talloc_tos(), "%u", \
357 ((t_mkdir *) data)->mode), \
358 talloc_asprintf( talloc_tos(), "%u", \
359 ((t_mkdir *) data)->result ));
360 break;
361 case vfs_id_rmdir: ;
362 str = smb_traffic_analyzer_create_string( tm, \
363 seconds, handle, username, \
364 2, ((t_rmdir *) data)->path, \
365 talloc_asprintf( talloc_tos(), "%u", \
366 ((t_rmdir *) data)->result ));
367 break;
368 case vfs_id_rename: ;
369 str = smb_traffic_analyzer_create_string( tm, \
370 seconds, handle, username, \
371 3, ((t_rename *) data)->src, \
372 ((t_rename *) data)->dst,
373 talloc_asprintf(talloc_tos(), "%u", \
374 ((t_rename *) data)->result));
375 break;
376 case vfs_id_chdir: ;
377 str = smb_traffic_analyzer_create_string( tm, \
378 seconds, handle, username, \
379 2, ((t_chdir *) data)->path, \
380 talloc_asprintf(talloc_tos(), "%u", \
381 ((t_chdir *) data)->result));
382 break;
383 default:
384 DEBUG(1, ("smb_traffic_analyzer: error! "
385 "wrong VFS operation id detected!\n"));
386 return;
389 } else {
390 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
391 "error, unkown protocol given!\n"));
392 return;
395 if (!str) {
396 DEBUG(1, ("smb_traffic_analyzer_send_data: "
397 "unable to create string to send!\n"));
398 return;
401 len = strlen(str);
403 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: going to send "
404 "%s\n", str));
405 /* If configured, optain the key and run AES encryption */
406 /* over the data. */
407 size_t size;
408 char *akey = secrets_fetch("smb_traffic_analyzer_key", &size);
409 if ( akey != NULL ) {
410 char *crypted;
411 DEBUG(10, ("smb_traffic_analyzer: a key was found, encrypting "
412 "data!"));
413 AES_KEY *key;
414 samba_AES_set_encrypt_key(akey, 128, key);
415 samba_AES_encrypt( str, crypted, key );
416 len = strlen( crypted );
417 if (write_data(rf_sock->sock, crypted, len) != len) {
418 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
419 "error sending crypted data to socket!\n"));
420 free( crypted );
421 return ;
424 if (write_data(rf_sock->sock, str, len) != len) {
425 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
426 "error sending data to socket!\n"));
427 return ;
431 static struct refcounted_sock *sock_list;
433 static void smb_traffic_analyzer_free_data(void **pptr)
435 struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
436 if (rf_sock == NULL) {
437 return;
439 rf_sock->ref_count--;
440 if (rf_sock->ref_count != 0) {
441 return;
443 if (rf_sock->sock != -1) {
444 close(rf_sock->sock);
446 DLIST_REMOVE(sock_list, rf_sock);
447 TALLOC_FREE(rf_sock);
450 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
451 const char *service,
452 const char *user)
454 connection_struct *conn = handle->conn;
455 enum sock_type st = smb_traffic_analyzer_connMode(handle);
456 struct refcounted_sock *rf_sock = NULL;
457 const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
458 lp_parm_const_string(SNUM(conn),
459 "smb_traffic_analyzer",
460 "host", "localhost");
461 uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
462 atoi( lp_parm_const_string(SNUM(conn),
463 "smb_traffic_analyzer", "port", "9430"));
464 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
466 if (ret < 0) {
467 return ret;
470 /* Are we already connected ? */
471 for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
472 if (port == rf_sock->port &&
473 (strcmp(name, rf_sock->name) == 0)) {
474 break;
478 /* If we're connected already, just increase the
479 * reference count. */
480 if (rf_sock) {
481 rf_sock->ref_count++;
482 } else {
483 /* New connection. */
484 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
485 if (rf_sock == NULL) {
486 SMB_VFS_NEXT_DISCONNECT(handle);
487 errno = ENOMEM;
488 return -1;
490 rf_sock->name = talloc_strdup(rf_sock, name);
491 if (rf_sock->name == NULL) {
492 SMB_VFS_NEXT_DISCONNECT(handle);
493 TALLOC_FREE(rf_sock);
494 errno = ENOMEM;
495 return -1;
497 rf_sock->port = port;
498 rf_sock->ref_count = 1;
500 if (st == UNIX_DOMAIN_SOCKET) {
501 rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
502 name);
503 } else {
505 rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
506 name,
507 port);
509 if (rf_sock->sock == -1) {
510 SMB_VFS_NEXT_DISCONNECT(handle);
511 TALLOC_FREE(rf_sock);
512 return -1;
514 DLIST_ADD(sock_list, rf_sock);
517 /* Store the private data. */
518 SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
519 struct refcounted_sock, return -1);
520 return 0;
523 /* VFS Functions */
524 static int smb_traffic_analyzer_chdir(vfs_handle_struct *handle, \
525 const char *path)
527 struct chdir_data s_data;
528 s_data.result = SMB_VFS_NEXT_CHDIR(handle, path);
529 s_data.path = path;
530 DEBUG(10, ("smb_traffic_analyzer_chdir: CHDIR: %s\n", path));
531 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_chdir);
532 return s_data.result;
535 static int smb_traffic_analyzer_rename(vfs_handle_struct *handle, \
536 const struct smb_filename *smb_fname_src,
537 const struct smb_filename *smb_fname_dst)
539 struct rename_data s_data;
540 s_data.result = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, \
541 smb_fname_dst);
542 s_data.src = smb_fname_src->base_name;
543 s_data.dst = smb_fname_dst->base_name;
544 DEBUG(10, ("smb_traffic_analyzer_rename: RENAME: %s / %s\n",
545 smb_fname_src->base_name,
546 smb_fname_dst->base_name));
547 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rename);
548 return s_data.result;
551 static int smb_traffic_analyzer_rmdir(vfs_handle_struct *handle, \
552 const char *path)
554 struct rmdir_data s_data;
555 s_data.result = SMB_VFS_NEXT_RMDIR(handle, path);
556 s_data.path = path;
557 DEBUG(10, ("smb_traffic_analyzer_rmdir: RMDIR: %s\n", path));
558 smb_traffic_analyzer_send_data(handle, &s_data, vfs_id_rmdir);
559 return s_data.result;
562 static int smb_traffic_analyzer_mkdir(vfs_handle_struct *handle, \
563 const char *path, mode_t mode)
565 struct mkdir_data s_data;
566 s_data.result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
567 s_data.path = path;
568 s_data.mode = mode;
569 DEBUG(10, ("smb_traffic_analyzer_mkdir: MKDIR: %s\n", path));
570 smb_traffic_analyzer_send_data(handle,
571 &s_data,
572 vfs_id_mkdir);
573 return s_data.result;
576 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
577 files_struct *fsp, void *data, size_t n)
579 struct rw_data s_data;
581 s_data.len = SMB_VFS_NEXT_READ(handle, fsp, data, n);
582 s_data.filename = fsp->fsp_name->base_name;
583 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp_str_dbg(fsp)));
585 smb_traffic_analyzer_send_data(handle,
586 &s_data,
587 vfs_id_read);
588 return s_data.len;
592 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
593 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
595 struct rw_data s_data;
597 s_data.len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
598 s_data.filename = fsp->fsp_name->base_name;
599 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n",
600 fsp_str_dbg(fsp)));
602 smb_traffic_analyzer_send_data(handle,
603 &s_data,
604 vfs_id_pread);
606 return s_data.len;
609 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
610 files_struct *fsp, const void *data, size_t n)
612 struct rw_data s_data;
614 s_data.len = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
615 s_data.filename = fsp->fsp_name->base_name;
616 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n",
617 fsp_str_dbg(fsp)));
619 smb_traffic_analyzer_send_data(handle,
620 &s_data,
621 vfs_id_write);
622 return s_data.len;
625 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
626 files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
628 struct rw_data s_data;
630 s_data.len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
631 s_data.filename = fsp->fsp_name->base_name;
632 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", \
633 fsp_str_dbg(fsp)));
635 smb_traffic_analyzer_send_data(handle,
636 &s_data,
637 vfs_id_pwrite);
638 return s_data.len;
641 static struct vfs_fn_pointers vfs_smb_traffic_analyzer_fns = {
642 .connect_fn = smb_traffic_analyzer_connect,
643 .vfs_read = smb_traffic_analyzer_read,
644 .pread = smb_traffic_analyzer_pread,
645 .write = smb_traffic_analyzer_write,
646 .pwrite = smb_traffic_analyzer_pwrite,
647 .mkdir = smb_traffic_analyzer_mkdir,
648 .rename = smb_traffic_analyzer_rename,
649 .chdir = smb_traffic_analyzer_chdir
652 /* Module initialization */
654 NTSTATUS vfs_smb_traffic_analyzer_init(void)
656 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
657 "smb_traffic_analyzer",
658 &vfs_smb_traffic_analyzer_fns);
660 if (!NT_STATUS_IS_OK(ret)) {
661 return ret;
664 vfs_smb_traffic_analyzer_debug_level =
665 debug_add_class("smb_traffic_analyzer");
667 if (vfs_smb_traffic_analyzer_debug_level == -1) {
668 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
669 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
670 "debugging class!\n"));
671 } else {
672 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
673 "'smb_traffic_analyzer': %d\n", \
674 vfs_smb_traffic_analyzer_debug_level));
677 return ret;