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