Remove current_user_info - not needed.
[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
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22
23 /* abstraction for the send_over_network function */
24 #define UNIX_DOMAIN_SOCKET 1
25 #define INTERNET_SOCKET 0
26
27
28 /* Prototypes */
29
30 static int vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
31
32 /* create the timestamp in sqlite compatible format */
33 static void get_timestamp(fstring str)
34 {
35         struct timeval tv;
36         struct timezone tz;
37         struct tm *tm;
38         int seconds;
39
40         gettimeofday(&tv, &tz);
41         tm=localtime(&tv.tv_sec);
42         seconds=(float) (tv.tv_usec / 1000);
43
44         fstr_sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d.%03d", \
45                         tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, \
46                         tm->tm_hour, tm->tm_min, tm->tm_sec, (int)seconds);
47
48 }
49
50 static int smb_traffic_analyzer_connMode(vfs_handle_struct *handle)
51 {
52         connection_struct *conn = handle->conn;
53         const char *Mode;
54         Mode=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer","mode", \
55                         "internet_socket");
56         if (strstr(Mode,"unix_domain_socket")) {
57                 return UNIX_DOMAIN_SOCKET;
58         } else {
59                 return INTERNET_SOCKET;
60         }
61
62 }
63
64 /* Connect to an internet socket */
65
66 static int smb_traffic_analyzer_connect_inet_socket(vfs_handle_struct *handle)
67 {
68         /* Create a streaming Socket */
69         const char *Hostname;
70         int sockfd = -1;
71         uint16_t port;
72         struct addrinfo hints;
73         struct addrinfo *ailist = NULL;
74         struct addrinfo *res = NULL;
75         connection_struct *conn = handle->conn;
76         int ret;
77
78         /* get port number, target system from the config parameters */
79         Hostname=lp_parm_const_string(SNUM(conn), "smb_traffic_analyzer",
80                                 "host", "localhost");
81
82         ZERO_STRUCT(hints);
83         /* By default make sure it supports TCP. */
84         hints.ai_socktype = SOCK_STREAM;
85         hints.ai_flags = AI_ADDRCONFIG;
86
87         ret = getaddrinfo(Hostname,
88                         NULL,
89                         &hints,
90                         &ailist);
91
92         if (ret) {
93                 DEBUG(3,("smb_traffic_analyzer_connect_inet_socket: "
94                         "getaddrinfo failed for name %s [%s]\n",
95                         Hostname,
96                         gai_strerror(ret) ));
97                 return -1;
98         }
99
100         port = atoi( lp_parm_const_string(SNUM(conn),
101                                 "smb_traffic_analyzer", "port", "9430"));
102
103         DEBUG(3,("smb_traffic_analyzer: Internet socket mode. Hostname: %s,"
104                 "Port: %i\n", Hostname, port));
105
106         for (res = ailist; res; res = res->ai_next) {
107                 struct sockaddr_storage ss;
108
109                 if (!res->ai_addr || res->ai_addrlen == 0) {
110                         continue;
111                 }
112
113                 ZERO_STRUCT(ss);
114                 memcpy(&ss, res->ai_addr, res->ai_addrlen);
115
116                 sockfd = open_socket_out(SOCK_STREAM, &ss, port, 10000);
117                 if (sockfd != -1) {
118                         break;
119                 }
120         }
121
122         if (ailist) {
123                 freeaddrinfo(ailist);
124         }
125
126         if (sockfd == -1) {
127                 DEBUG(1, ("smb_traffic_analyzer: unable to create "
128                         "socket, error is %s",
129                         strerror(errno)));
130                 return -1;
131         }
132
133         return sockfd;
134 }
135
136 /* Connect to a unix domain socket */
137
138 static int smb_traffic_analyzer_connect_unix_socket(vfs_handle_struct *handle)
139 {
140         /* Create the socket to stad */
141         int len, sock;
142         struct sockaddr_un remote;
143
144         DEBUG(7, ("smb_traffic_analyzer_connect_unix_socket: "
145                         "Unix domain socket mode. Using "
146                         "/var/tmp/stadsocket\n"));
147
148         if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
149                 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
150                         "Couldn't create socket, "
151                         "make sure stad is running!\n"));
152         }
153         remote.sun_family = AF_UNIX;
154         strlcpy(remote.sun_path, "/var/tmp/stadsocket",
155                     sizeof(remote.sun_path));
156         len=strlen(remote.sun_path) + sizeof(remote.sun_family);
157         if (connect(sock, (struct sockaddr *)&remote, len) == -1 ) {
158                 DEBUG(1, ("smb_traffic_analyzer_connect_unix_socket: "
159                         "Could not connect to "
160                         "socket, make sure\nstad is running!\n"));
161                 close(sock);
162                 return -1;
163         }
164         return sock;
165 }
166
167 /* Send data over a socket */
168
169 static void smb_traffic_analyzer_send_data(vfs_handle_struct *handle,
170                                         char *str,
171                                         const char *file_name,
172                                         bool Write)
173 {
174         int *psockfd = NULL;
175         char Sender[200];
176         char TimeStamp[200];
177
178         SMB_VFS_HANDLE_GET_DATA(handle, psockfd, int, return);
179
180         if (psockfd == NULL || *psockfd == -1) {
181                 DEBUG(1, ("smb_traffic_analyzer_send_data: socket is "
182                         "closed\n"));
183                 return;
184         }
185
186         strlcpy(Sender, str, sizeof(Sender));
187         strlcat(Sender, ",\"", sizeof(Sender));
188         strlcat(Sender, handle->conn->server_info->sanitized_username, sizeof(Sender));
189         strlcat(Sender, "\",\"", sizeof(Sender));
190         strlcat(Sender, pdb_get_domain(handle->conn->server_info->sam_account), sizeof(Sender));
191         strlcat(Sender, "\",\"", sizeof(Sender));
192         if (Write)
193                 strlcat(Sender, "W", sizeof(Sender));
194         else
195                 strlcat(Sender, "R", sizeof(Sender));
196         strlcat(Sender, "\",\"", sizeof(Sender));
197         strlcat(Sender, handle->conn->connectpath, sizeof(Sender));
198         strlcat(Sender, "\",\"", sizeof(Sender) - 1);
199         strlcat(Sender, file_name, sizeof(Sender) - 1);
200         strlcat(Sender, "\",\"", sizeof(Sender) - 1);
201         get_timestamp(TimeStamp);
202         strlcat(Sender, TimeStamp, sizeof(Sender) - 1);
203         strlcat(Sender, "\");", sizeof(Sender) - 1);
204         DEBUG(10, ("smb_traffic_analyzer_send_data_socket: sending %s\n",
205                         Sender));
206         if (send(*psockfd, Sender, strlen(Sender), 0) == -1 ) {
207                 DEBUG(1, ("smb_traffic_analyzer_send_data_socket: "
208                         "error sending data to socket!\n"));
209                 return ;
210         }
211 }
212
213 static void smb_traffic_analyzer_free_data(void **pptr)
214 {
215         int *pfd = *(int **)pptr;
216         if(!pfd) {
217                 return;
218         }
219         if (*pfd != -1) {
220                 close(*pfd);
221         }
222         TALLOC_FREE(pfd);
223 }
224
225 static int smb_traffic_analyzer_connect(struct vfs_handle_struct *handle,
226                          const char *service,
227                          const char *user)
228 {
229         int *pfd = TALLOC_P(handle, int);
230
231         if (!pfd) {
232                 errno = ENOMEM;
233                 return -1;
234         }
235
236         if (smb_traffic_analyzer_connMode(handle) == UNIX_DOMAIN_SOCKET) {
237                 *pfd = smb_traffic_analyzer_connect_unix_socket(handle);
238         } else {
239                 *pfd = smb_traffic_analyzer_connect_inet_socket(handle);
240         }
241         if (*pfd == -1) {
242                 return -1;
243         }
244
245         /* Store the private data. */
246         SMB_VFS_HANDLE_SET_DATA(handle, pfd, smb_traffic_analyzer_free_data,
247                                 int, return -1);
248         return SMB_VFS_NEXT_CONNECT(handle, service, user);
249 }
250
251 /* VFS Functions: write, read, pread, pwrite for now */
252
253 static ssize_t smb_traffic_analyzer_read(vfs_handle_struct *handle, \
254                                 files_struct *fsp, void *data, size_t n)
255 {
256         ssize_t result;
257         fstring Buffer;
258
259         result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
260         DEBUG(10, ("smb_traffic_analyzer_read: READ: %s\n", fsp->fsp_name ));
261
262         fstr_sprintf(Buffer, "%u", (uint) result);
263
264         smb_traffic_analyzer_send_data(handle,
265                         Buffer,
266                         fsp->fsp_name,
267                         false);
268         return result;
269 }
270
271
272 static ssize_t smb_traffic_analyzer_pread(vfs_handle_struct *handle, \
273                 files_struct *fsp, void *data, size_t n, SMB_OFF_T offset)
274 {
275         ssize_t result;
276         fstring Buffer;
277
278         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
279
280         DEBUG(10, ("smb_traffic_analyzer_pread: PREAD: %s\n", fsp->fsp_name ));
281
282         fstr_sprintf(Buffer,"%u", (uint) result);
283         smb_traffic_analyzer_send_data(handle,
284                         Buffer,
285                         fsp->fsp_name,
286                         false);
287
288         return result;
289 }
290
291 static ssize_t smb_traffic_analyzer_write(vfs_handle_struct *handle, \
292                         files_struct *fsp, const void *data, size_t n)
293 {
294         ssize_t result;
295         fstring Buffer;
296
297         result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
298
299         DEBUG(10, ("smb_traffic_analyzer_write: WRITE: %s\n", fsp->fsp_name ));
300
301         fstr_sprintf(Buffer, "%u", (uint) result);
302         smb_traffic_analyzer_send_data(handle,
303                         Buffer,
304                         fsp->fsp_name,
305                         true);
306         return result;
307 }
308
309 static ssize_t smb_traffic_analyzer_pwrite(vfs_handle_struct *handle, \
310              files_struct *fsp, const void *data, size_t n, SMB_OFF_T offset)
311 {
312         ssize_t result;
313         fstring Buffer;
314
315         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
316
317         DEBUG(10, ("smb_traffic_analyzer_pwrite: PWRITE: %s\n", fsp->fsp_name ));
318
319         fstr_sprintf(Buffer, "%u", (uint) result);
320         smb_traffic_analyzer_send_data(handle,
321                         Buffer,
322                         fsp->fsp_name,
323                         true);
324         return result;
325 }
326
327 /* VFS operations we use */
328
329 static vfs_op_tuple smb_traffic_analyzer_tuples[] = {
330
331         {SMB_VFS_OP(smb_traffic_analyzer_connect), SMB_VFS_OP_CONNECT,
332          SMB_VFS_LAYER_LOGGER},
333         {SMB_VFS_OP(smb_traffic_analyzer_read), SMB_VFS_OP_READ,
334          SMB_VFS_LAYER_LOGGER},
335         {SMB_VFS_OP(smb_traffic_analyzer_pread), SMB_VFS_OP_PREAD,
336          SMB_VFS_LAYER_LOGGER},
337         {SMB_VFS_OP(smb_traffic_analyzer_write), SMB_VFS_OP_WRITE,
338          SMB_VFS_LAYER_LOGGER},
339         {SMB_VFS_OP(smb_traffic_analyzer_pwrite), SMB_VFS_OP_PWRITE,
340          SMB_VFS_LAYER_LOGGER},
341         {SMB_VFS_OP(NULL),SMB_VFS_OP_NOOP,SMB_VFS_LAYER_NOOP}
342 };
343
344 /* Module initialization */
345
346 NTSTATUS vfs_smb_traffic_analyzer_init(void)
347 {
348         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, \
349                 "smb_traffic_analyzer", smb_traffic_analyzer_tuples);
350
351         if (!NT_STATUS_IS_OK(ret)) {
352                 return ret;
353         }
354
355         vfs_smb_traffic_analyzer_debug_level =
356                 debug_add_class("smb_traffic_analyzer");
357
358         if (vfs_smb_traffic_analyzer_debug_level == -1) {
359                 vfs_smb_traffic_analyzer_debug_level = DBGC_VFS;
360                 DEBUG(1, ("smb_traffic_analyzer_init: Couldn't register custom"
361                          "debugging class!\n"));
362         } else {
363                 DEBUG(3, ("smb_traffic_analyzer_init: Debug class number of"
364                         "'smb_traffic_analyzer': %d\n", \
365                         vfs_smb_traffic_analyzer_debug_level));
366         }
367
368         return ret;
369 }