Fix detection of dns_sd libraries.
[Samba.git] / source / modules / vfs_smb_traffic_analyzer.c
blob10611cd0f56b9e2056eba4220135139450700f43
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"
24 /* abstraction for the send_over_network function */
25 enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};
27 #define LOCAL_PATHNAME "/var/tmp/stadsocket"
29 extern userdom_struct current_user_info;
31 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
33 static enum sock_type smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
35 connection_struct *conn = handle->conn;
36 const char *Mode;
37 Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
38 "internet_socket");
39 if (strstr(Mode,"unix_domain_socket")) {
40 return UNIX_DOMAIN_SOCKET;
41 } else {
42 return INTERNET_SOCKET;
47 /* Connect to an internet socket */
49 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle,
50 const char *name, uint16_t port)
52 /* Create a streaming Socket */
53 int sockfd = -1;
54 struct addrinfo hints;
55 struct addrinfo *ailist = NULL;
56 struct addrinfo *res = NULL;
57 int ret;
59 ZERO_STRUCT(hints);
60 /* By default make sure it supports TCP. */
61 hints.ai_socktype = SOCK_STREAM;
62 hints.ai_flags = AI_ADDRCONFIG;
64 ret = getaddrinfo(name,
65 NULL,
66 &hints,
67 &ailist);
69 if (ret) {
70 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
71 "getaddrinfo failed for name %s [%s]\n",
72 name,
73 gai_strerror(ret) ));
74 return -1;
77 DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
78 "Port: %i\n", name, port));
80 for (res = ailist; res; res = res->ai_next) {
81 struct sockaddr_storage ss;
83 if (!res->ai_addr || res->ai_addrlen == 0) {
84 continue;
87 ZERO_STRUCT(ss);
88 memcpy(&ss, res->ai_addr, res->ai_addrlen);
90 sockfd = open_socket_out(SOCK_STREAM, &ss, port, 10000);
91 if (sockfd != -1) {
92 break;
96 if (ailist) {
97 freeaddrinfo(ailist);
100 if (sockfd == -1) {
101 DEBUG(1, ("smb_traffic_analyzer: unable to create "
102 "socket, error is %s",
103 strerror(errno)));
104 return -1;
107 return sockfd;
110 /* Connect to a unix domain socket */
112 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle,
113 const char *name)
115 /* Create the socket to stad */
116 int len, sock;
117 struct sockaddr_un remote;
119 DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
120 "Unix domain socket mode. Using %s\n",
121 name ));
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;
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;
140 return sock;
143 /* 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;
153 /* Send data over a socket */
155 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
156 ssize_t result,
157 const char *file_name,
158 bool Write)
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 const char *username = NULL;
167 const char *anon_prefix = NULL;
168 size_t len;
170 SMB_VFS_HANDLE_GET_DATA(handle, rf_sock, struct refcounted_sock, return);
172 if (rf_sock == NULL || rf_sock->sock == -1) {
173 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
174 "closed\n"));
175 return;
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;
184 seconds=(float) (tv.tv_usec / 1000);
186 /* check if anonymization is required */
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(get_current_username()));
195 } else {
196 username = get_current_username();
199 if (!username) {
200 return;
202 str = talloc_asprintf(talloc_tos(),
203 "V1,%u,\"%s\",\"%s\",\"%c\",\"%s\",\"%s\","
204 "\"%04d-%02d-%02d %02d:%02d:%02d.%03d\"\n",
205 (unsigned int)result,
206 username,
207 current_user_info.domain,
208 Write ? 'W' : 'R',
209 handle->conn->connectpath,
210 file_name,
211 tm->tm_year+1900,
212 tm->tm_mon+1,
213 tm->tm_mday,
214 tm->tm_hour,
215 tm->tm_min,
216 tm->tm_sec,
217 (int)seconds);
219 if (!str) {
220 return;
223 len = strlen(str);
225 DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
226 str));
227 if (write_data(rf_sock->sock, str, len) != len) {
228 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
229 "error sending data to socket!\n"));
230 return ;
234 static struct refcounted_sock *sock_list;
236 static void smb_traffic_analyzer_free_data(void **pptr)
238 struct refcounted_sock *rf_sock = *(struct refcounted_sock **)pptr;
239 if (rf_sock == NULL) {
240 return;
242 rf_sock->ref_count--;
243 if (rf_sock->ref_count != 0) {
244 return;
246 if (rf_sock->sock != -1) {
247 close(rf_sock->sock);
249 DLIST_REMOVE(sock_list, rf_sock);
250 TALLOC_FREE(rf_sock);
253 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
254 const char *service,
255 const char *user)
257 connection_struct *conn = handle->conn;
258 enum sock_type st = smb_traffic_analyzer_connMode(handle);
259 struct refcounted_sock *rf_sock = NULL;
260 const char *name = (st == UNIX_DOMAIN_SOCKET) ? LOCAL_PATHNAME :
261 lp_parm_const_string(SNUM(conn),
262 "smb_traffic_analyzer",
263 "host", "localhost");
264 uint16_t port = (st == UNIX_DOMAIN_SOCKET) ? 0 :
265 atoi( lp_parm_const_string(SNUM(conn),
266 "smb_traffic_analyzer", "port", "9430"));
268 /* Are we already connected ? */
269 for (rf_sock = sock_list; rf_sock; rf_sock = rf_sock->next) {
270 if (port == rf_sock->port &&
271 (strcmp(name, rf_sock->name) == 0)) {
272 break;
276 /* If we're connected already, just increase the
277 * reference count. */
278 if (rf_sock) {
279 rf_sock->ref_count++;
280 } else {
281 /* New connection. */
282 rf_sock = TALLOC_ZERO_P(NULL, struct refcounted_sock);
283 if (rf_sock == NULL) {
284 errno = ENOMEM;
285 return -1;
287 rf_sock->name = talloc_strdup(rf_sock, name);
288 if (rf_sock->name == NULL) {
289 TALLOC_FREE(rf_sock);
290 errno = ENOMEM;
291 return -1;
293 rf_sock->port = port;
294 rf_sock->ref_count = 1;
296 if (st == UNIX_DOMAIN_SOCKET) {
297 rf_sock->sock = smb_traffic_analyzer_connect_unix_socket(handle,
298 name);
299 } else {
301 rf_sock->sock = smb_traffic_analyzer_connect_inet_socket(handle,
302 name,
303 port);
305 if (rf_sock->sock == -1) {
306 TALLOC_FREE(rf_sock);
307 return -1;
309 DLIST_ADD(sock_list, rf_sock);
312 /* Store the private data. */
313 SMB_VFS_HANDLE_SET_DATA(handle, rf_sock, smb_traffic_analyzer_free_data,
314 struct refcounted_sock, return -1);
315 return SMB_VFS_NEXT_CONNECT(handle, service, user);
318 /* VFS Functions: write, read, pread, pwrite for now */
320 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
321 files_struct *fsp, void *data, size_t n)
323 ssize_t result;
325 result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
326 DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp->fsp_name ));
328 smb_traffic_analyzer_send_data(handle,
329 result,
330 fsp->fsp_name,
331 false);
332 return result;
336 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
337 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
339 ssize_t result;
341 result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
343 DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n", fsp->fsp_name ));
345 smb_traffic_analyzer_send_data(handle,
346 result,
347 fsp->fsp_name,
348 false);
350 return result;
353 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
354 files_struct *fsp, const void *data, size_t n)
356 ssize_t result;
358 result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
360 DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n", fsp->fsp_name ));
362 smb_traffic_analyzer_send_data(handle,
363 result,
364 fsp->fsp_name,
365 true);
366 return result;
369 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
370 files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
372 ssize_t result;
374 result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
376 DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp->fsp_name ));
378 smb_traffic_analyzer_send_data(handle,
379 result,
380 fsp->fsp_name,
381 true);
382 return result;
385 /* VFS operations we use */
387 static vfs_op_tuple smb_traffic_analyzer_tuples[] = {
389 {SMB_VFS_OP(smb_traffic_analyzer_connect), SMB_VFS_OP_CONNECT,
390 SMB_VFS_LAYER_LOGGER},
391 {SMB_VFS_OP(smb_traffic_analyzer_read), SMB_VFS_OP_READ,
392 SMB_VFS_LAYER_LOGGER},
393 {SMB_VFS_OP(smb_traffic_analyzer_pread), SMB_VFS_OP_PREAD,
394 SMB_VFS_LAYER_LOGGER},
395 {SMB_VFS_OP(smb_traffic_analyzer_write), SMB_VFS_OP_WRITE,
396 SMB_VFS_LAYER_LOGGER},
397 {SMB_VFS_OP(smb_traffic_analyzer_pwrite), SMB_VFS_OP_PWRITE,
398 SMB_VFS_LAYER_LOGGER},
399 {SMB_VFS_OP(NULL),SMB_VFS_OP_NOOP,SMB_VFS_LAYER_NOOP}
402 /* Module initialization */
404 NTSTATUS vfs_smb_traffic_analyzer_init(void)
406 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, \
407 "smb_traffic_analyzer", smb_traffic_analyzer_tuples);
409 if (!NT_STATUS_IS_OK(ret)) {
410 return ret;
413 vfs_smb_traffic_analyzer_debug_level =
414 debug_add_class("smb_traffic_analyzer");
416 if (vfs_smb_traffic_analyzer_debug_level == -1) {
417 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
418 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
419 "debugging class!\n"));
420 } else {
421 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
422 "'smb_traffic_analyzer': %d\n", \
423 vfs_smb_traffic_analyzer_debug_level));
426 return ret;