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