Async wrapper for open_socket_out_send/recv
[metze/samba/wip.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
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
24 /* abstraction for the send_over_network function */
25
26 enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};
27
28 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
29
30 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
31
32 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
33 {
34         connection_struct *conn = handle->conn;
35         const char *Mode;
36         Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
37                         "internet_socket");
38         if (strstr(Mode,"unix_domain_socket")) {
39                 return UNIX_DOMAIN_SOCKET;
40         } else {
41                 return INTERNET_SOCKET;
42         }
43 }
44
45
46 /* Connect to an internet socket */
47
48 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle,
49                                         const char *name, uint16_t port)
50 {
51         /* Create a streaming Socket */
52         int sockfd = -1;
53         struct addrinfo hints;
54         struct addrinfo *ailist = NULL;
55         struct addrinfo *res = NULL;
56         int ret;
57
58         ZERO_STRUCT(hints);
59         /* By default make sure it supports TCP. */
60         hints.ai_socktype = SOCK_STREAM;
61         hints.ai_flags = AI_ADDRCONFIG;
62
63         ret = getaddrinfo(name,
64                         NULL,
65                         &hints,
66                         &ailist);
67
68         if (ret) {
69                 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
70                         "getaddrinfo failed for name %s [%s]\n",
71                         name,
72                         gai_strerror(ret) ));
73                 return -1;
74         }
75
76         DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
77                 "Port: %i\n", name, port));
78
79         for (res = ailist; res; res = res->ai_next) {
80                 struct sockaddr_storage ss;
81                 NTSTATUS status;
82
83                 if (!res->ai_addr || res->ai_addrlen == 0) {
84                         continue;
85                 }
86
87                 ZERO_STRUCT(ss);
88                 memcpy(&ss, res->ai_addr, res->ai_addrlen);
89
90                 status = open_socket_out(&ss, port, 10000, &sockfd);
91                 if (NT_STATUS_IS_OK(status)) {
92                         break;
93                 }
94         }
95
96         if (ailist) {
97                 freeaddrinfo(ailist);
98         }
99
100         if (sockfd == -1) {
101                 DEBUG(1, ("smb_traffic_analyzer: unable to create "
102                         "socket, error is %s",
103                         strerror(errno)));
104                 return -1;
105         }
106
107         return sockfd;
108 }
109
110 /* Connect to a unix domain socket */
111
112 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
113                                                 const char *name)
114 {
115         /* Create the socket to stad */
116         int len, sock;
117         struct sockaddr_un remote;
118
119         DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
120                         "Unix domain socket mode. Using %s\n",
121                         name ));
122
123         if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
124                 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
125                         "Couldn't create socket, "
126                         "make sure stad is running!\n"));
127                 return -1;
128         }
129         remote.sun_family = AF_UNIX;
130         strlcpy(remote.sun_path, name,
131                     sizeof(remote.sun_path));
132         len=strlen(remote.sun_path) + sizeof(remote.sun_family);
133         if (connect(sock, (struct sockaddr *)&remote, len) == -1 ) {
134                 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
135                         "Could not connect to "
136                         "socket, make sure\nstad is running!\n"));
137                 close(sock);
138                 return -1;
139         }
140         return sock;
141 }
142
143 /* Private data allowing shared connection sockets. */
144
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 /* Send data over a socket */
154
155 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
156                                         ssize_t result,
157                                         const char *file_name,
158                                         bool Write)
159 {
160         struct refcounted_sock *rf_sock = NULL;
161         struct timeval tv;
162         time_t tv_sec;
163         struct tm *tm = NULL;
164         int seconds;
165         char *str = NULL;
166         char *username = NULL;
167         const char *anon_prefix = NULL;
168         size_t len;
169
170         SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
171
172         if (rf_sock == NULL || rf_sock->sock == -1) {
173                 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
174                         "closed\n"));
175                 return;
176         }
177
178         GetTimeOfDay(&tv);
179         tv_sec = convert_timespec_to_time_t(convert_timeval_to_timespec(tv));
180         tm = localtime(&tv_sec);
181         if (!tm) {
182                 return;
183         }
184         seconds=(float) (tv.tv_usec / 1000);
185
186         /* check if anonymization is required */
187
188         anon_prefix=lp_parm_const_string(SNUM(handle->conn),"smb_traffic_analyzer",\
189                                         "anonymize_prefix", NULL );
190         if (anon_prefix!=NULL) {
191                 username = talloc_asprintf(talloc_tos(),
192                         "%s%i",
193                         anon_prefix,
194                         str_checksum(
195                                 handle->conn->server_info->sanitized_username ) ); 
196         } else {
197                 username = handle->conn->server_info->sanitized_username;
198         }
199
200         if (!username) {
201                 return;
202         }
203
204         str = talloc_asprintf(talloc_tos(),
205                         "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
206                         "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
207                         (unsigned int)result,
208                         username,
209                         pdb_get_domain(handle->conn->server_info->sam_account),
210                         Write ? 'W' : 'R',
211                         handle->conn->connectpath,
212                         file_name,
213                         tm->tm_year+1900,
214                         tm->tm_mon+1,
215                         tm->tm_mday,
216                         tm->tm_hour,
217                         tm->tm_min,
218                         tm->tm_sec,
219                         (int)seconds);
220
221         if (!str) {
222                 return;
223         }
224
225         len = strlen(str);
226
227         DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
228                         str));
229         if (write_data(rf_sock->sock, str, len) != len) {
230                 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
231                         "error sending data to socket!\n"));
232                 return ;
233         }
234 }
235
236 static struct refcounted_sock *sock_list;
237
238 static void smb_traffic_analyzer_free_data(void **pptr)
239 {
240         struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
241         if (rf_sock == NULL) {
242                 return;
243         }
244         rf_sock->ref_count--;
245         if (rf_sock->ref_count != 0) {
246                 return;
247         }
248         if (rf_sock->sock != -1) {
249                 close(rf_sock->sock);
250         }
251         DLIST_REMOVE(sock_list, rf_sock);
252         TALLOC_FREE(rf_sock);
253 }
254
255 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
256                          const char *service,
257                          const char *user)
258 {
259         connection_struct *conn = handle->conn;
260         enum sock_type st = smb_traffic_analyzer_connMode(handle);
261         struct refcounted_sock *rf_sock = NULL;
262         const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
263                                 lp_parm_const_string(SNUM(conn),
264                                         "smb_traffic_analyzer",
265                                 "host", "localhost");
266         uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
267                                 atoi( lp_parm_const_string(SNUM(conn),
268                                 "smb_traffic_analyzer", "port", "9430"));
269
270         /* Are we already connected ? */
271         for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
272                 if (port == rf_sock->port &&
273                                 (strcmp(name, rf_sock->name) == 0)) {
274                         break;
275                 }
276         }
277
278         /* If we're connected already, just increase the
279          * reference count. */
280         if (rf_sock) {
281                 rf_sock->ref_count++;
282         } else {
283                 /* New connection. */
284                 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
285                 if (rf_sock == NULL) {
286                         errno = ENOMEM;
287                         return -1;
288                 }
289                 rf_sock->name = talloc_strdup(rf_sock, name);
290                 if (rf_sock->name == NULL) {
291                         TALLOC_FREE(rf_sock);
292                         errno = ENOMEM;
293                         return -1;
294                 }
295                 rf_sock->port = port;
296                 rf_sock->ref_count = 1;
297
298                 if (st == UNIX_DOMAIN_SOCKET) {
299                         rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
300                                                         name);
301                 } else {
302
303                         rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
304                                                         name,
305                                                         port);
306                 }
307                 if (rf_sock->sock == -1) {
308                         TALLOC_FREE(rf_sock);
309                         return -1;
310                 }
311                 DLIST_ADD(sock_list, rf_sock);
312         }
313
314         /* Store the private data. */
315         SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
316                                 struct refcounted_sock, return -1);
317         return SMB_VFS_NEXT_CONNECT(handle, service, user);
318 }
319
320 /* VFS Functions: write, read, pread, pwrite for now */
321
322 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
323                                 files_struct *fsp, void *data, size_t n)
324 {
325         ssize_t result;
326
327         result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
328         DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp->fsp_name ));
329
330         smb_traffic_analyzer_send_data(handle,
331                         result,
332                         fsp->fsp_name,
333                         false);
334         return result;
335 }
336
337
338 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
339                 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
340 {
341         ssize_t result;
342
343         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
344
345         DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n", fsp->fsp_name ));
346
347         smb_traffic_analyzer_send_data(handle,
348                         result,
349                         fsp->fsp_name,
350                         false);
351
352         return result;
353 }
354
355 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
356                         files_struct *fsp, const void *data, size_t n)
357 {
358         ssize_t result;
359
360         result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
361
362         DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n", fsp->fsp_name ));
363
364         smb_traffic_analyzer_send_data(handle,
365                         result,
366                         fsp->fsp_name,
367                         true);
368         return result;
369 }
370
371 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
372              files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
373 {
374         ssize_t result;
375
376         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
377
378         DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp->fsp_name ));
379
380         smb_traffic_analyzer_send_data(handle,
381                         result,
382                         fsp->fsp_name,
383                         true);
384         return result;
385 }
386
387 /* VFS operations we use */
388
389 static vfs_op_tuple smb_traffic_analyzer_tuples[] = {
390
391         {SMB_VFS_OP(smb_traffic_analyzer_connect), SMB_VFS_OP_CONNECT,
392          SMB_VFS_LAYER_LOGGER},
393         {SMB_VFS_OP(smb_traffic_analyzer_read), SMB_VFS_OP_READ,
394          SMB_VFS_LAYER_LOGGER},
395         {SMB_VFS_OP(smb_traffic_analyzer_pread), SMB_VFS_OP_PREAD,
396          SMB_VFS_LAYER_LOGGER},
397         {SMB_VFS_OP(smb_traffic_analyzer_write), SMB_VFS_OP_WRITE,
398          SMB_VFS_LAYER_LOGGER},
399         {SMB_VFS_OP(smb_traffic_analyzer_pwrite), SMB_VFS_OP_PWRITE,
400          SMB_VFS_LAYER_LOGGER},
401         {SMB_VFS_OP(NULL),SMB_VFS_OP_NOOP,SMB_VFS_LAYER_NOOP}
402 };
403
404 /* Module initialization */
405
406 NTSTATUS vfs_smb_traffic_analyzer_init(void)
407 {
408         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, \
409                 "smb_traffic_analyzer", smb_traffic_analyzer_tuples);
410
411         if (!NT_STATUS_IS_OK(ret)) {
412                 return ret;
413         }
414
415         vfs_smb_traffic_analyzer_debug_level =
416                 debug_add_class("smb_traffic_analyzer");
417
418         if (vfs_smb_traffic_analyzer_debug_level == -1) {
419                 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
420                 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
421                          "debugging class!\n"));
422         } else {
423                 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
424                         "'smb_traffic_analyzer': %d\n", \
425                         vfs_smb_traffic_analyzer_debug_level));
426         }
427
428         return ret;
429 }