-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd_file.c
512 lines (448 loc) · 11.5 KB
/
add_file.c
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* Copyright (C) 2000 [email protected]
This is free software distributed under the terms of the
GNU Public License.
$Id$ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "opennap.h"
#include "debug.h"
/* allowed bitrates for MPEG V1/V2 Layer III */
const int BitRate[18] =
{ 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 192, 224,
256, 320 };
/* allowed sample rates for MPEG V2/3 */
const int SampleRate[6] = { 16000, 24000, 22050, 32000, 44100, 48000 };
static void
fdb_add (HASH * table, char *key, DATUM * d)
{
FLIST *files;
LIST *list;
ASSERT (table != 0);
ASSERT (key != 0);
ASSERT (d != 0);
files = hash_lookup (table, key);
/* if there is no entry for this particular word, create one now */
if (!files)
{
files = CALLOC (1, sizeof (FLIST));
if (!files)
{
OUTOFMEMORY ("fdb_add");
return;
}
files->key = STRDUP (key);
if (!files->key)
{
OUTOFMEMORY ("fdb_add");
FREE (files);
return;
}
if (hash_add (table, files->key, files))
{
FREE (files->key);
FREE (files);
return;
}
}
list = CALLOC (1, sizeof (LIST));
if (!list)
{
OUTOFMEMORY ("list");
if (!files->list)
hash_remove (table, files->key);
return;
}
list->data = d;
files->list = list_append (files->list, list);
files->count++;
d->refcount++;
}
/* common code for inserting a file into the various hash tables */
static void
insert_datum (DATUM * info, char *av)
{
LIST *tokens, *ptr;
unsigned int fsize;
ASSERT (info != 0);
ASSERT (av != 0);
if (!info->user->con->uopt->files)
{
/* create the hash table */
info->user->con->uopt->files =
hash_init (257, (hash_destroy) free_datum);
if (!info->user->con->uopt->files)
{
OUTOFMEMORY ("insert_datum");
return;
}
}
hash_add (info->user->con->uopt->files, info->filename, info);
info->refcount++;
/* split the filename into words */
tokens = tokenize (av);
/* the filename may not consist of any searchable words, in which
* case its not entered into the index. this file will only be seen
* when browsing the user possessing it
*/
if(tokens)
{
/* add this entry to the global file list. the data entry currently
can't be referenced more than 32 times so if there are excess tokens,
discard the first several so that the refcount is not overflowed */
fsize = list_count (tokens);
ptr = tokens;
while (fsize > 30)
{
ptr = ptr->next;
fsize--;
}
for (; ptr; ptr = ptr->next)
fdb_add (File_Table, ptr->data, info);
list_free (tokens, 0);
}
#if RESUME
/* index by md5 hash */
fdb_add (MD5, info->hash, info);
#endif
fsize = info->size / 1024;
info->user->shared++;
info->user->libsize += fsize;
Num_Gigs += fsize; /* this is actually kB, not gB */
Num_Files++;
Local_Files++;
info->user->sharing = 1; /* note that we began sharing */
}
static DATUM *
new_datum (char *filename, char *hash)
{
DATUM *info = CALLOC (1, sizeof (DATUM));
(void) hash;
if (!info)
{
OUTOFMEMORY ("new_datum");
return 0;
}
info->filename = STRDUP (filename);
if (!info->filename)
{
OUTOFMEMORY ("new_datum");
FREE (info);
return 0;
}
#if RESUME
info->hash = STRDUP (hash);
if (!info->hash)
{
OUTOFMEMORY ("new_datum");
FREE (info->filename);
FREE (info);
return 0;
}
#endif
return info;
}
static int
bitrateToMask (int bitrate, USER * user)
{
unsigned int i;
for (i = 0; i < sizeof (BitRate) / sizeof (int); i++)
{
if (bitrate <= BitRate[i])
return i;
}
log ("bitrateToMask(): invalid bit rate %d (%s, \"%s\")", bitrate,
user->nick, user->clientinfo);
return 0; /* invalid bitrate */
}
static int
freqToMask (int freq, USER * user)
{
unsigned int i;
for (i = 0; i < sizeof (SampleRate) / sizeof (int); i++)
{
if (freq <= SampleRate[i])
return i;
}
log ("freqToMask(): invalid sample rate %d (%s, \"%s\")", freq,
user->nick, user->clientinfo);
return 0;
}
/* 100 "<filename>" <md5sum> <size> <bitrate> <frequency> <time>
client adding file to the shared database */
HANDLER (add_file)
{
char *av[6];
DATUM *info;
unsigned int fsize;
(void) tag;
(void) len;
ASSERT (validate_connection (con));
CHECK_USER_CLASS ("add_file");
ASSERT (validate_user (con->user));
if (Max_Shared && con->user->shared > Max_Shared)
{
send_cmd (con, MSG_SERVER_NOSUCH,
"You may only share %d files", Max_Shared);
return;
}
if (split_line (av, sizeof (av) / sizeof (char *), pkt) != 6)
{
unparsable(con);
return;
}
if (av[1] - av[0] > _POSIX_PATH_MAX + 2)
{
send_cmd (con, MSG_SERVER_NOSUCH, "filename too long");
return;
}
/* ensure we have a valid byte count */
fsize = strtoul (av[2],0,10);
/* check for overflow */
if(con->user->libsize + fsize < con->user->libsize)
{
log("add_file(): %u byte file would overflow %s's library size",
fsize, con->user->nick);
return;
}
/* make sure this isn't a duplicate - only compare the basename, not
* including the directory component
*/
if (con->uopt->files && hash_lookup (con->uopt->files, av[0]))
{
send_cmd (con, MSG_SERVER_NOSUCH, "duplicate file");
return;
}
/* create the db record for this file */
if (!(info = new_datum (av[0], av[1])))
return;
info->user = con->user;
info->size = fsize;
info->bitrate = bitrateToMask (atoi (av[3]), con->user);
info->frequency = freqToMask (atoi (av[4]), con->user);
info->duration = atoi (av[5]);
info->type = CT_MP3;
insert_datum (info, av[0]);
}
char *Content_Types[] = {
"mp3", /* not a real type, but what we use for audio/mp3 */
"audio",
"video",
"application",
"image",
"text"
};
/* 10300 "<filename>" <size> <hash> <content-type> */
HANDLER (share_file)
{
char *av[4];
DATUM *info;
int i, type;
unsigned int fsize;
(void) len;
(void) tag;
ASSERT (validate_connection (con));
CHECK_USER_CLASS ("share_file");
if (Max_Shared && con->user->shared > Max_Shared)
{
log ("add_file(): %s is already sharing %d files", con->user->nick,
con->user->shared);
if (ISUSER (con))
send_cmd (con, MSG_SERVER_NOSUCH,
"You may only share %d files", Max_Shared);
return;
}
if (split_line (av, sizeof (av) / sizeof (char *), pkt) != 4)
{
unparsable (con);
return;
}
/* make sure the content-type looks correct */
type = -1;
for (i = CT_AUDIO; i < CT_UNKNOWN; i++)
{
if (!strcasecmp (Content_Types[i], av[3]))
{
type = i;
break;
}
}
if (type == -1)
{
log ("share_file(): not a valid type: %s", av[3]);
if (ISUSER (con) == CLASS_USER)
send_cmd (con, MSG_SERVER_NOSUCH, "%s is not a valid type",
av[3]);
return;
}
if (av[1] - av[0] > _POSIX_PATH_MAX + 2)
{
send_cmd (con, MSG_SERVER_NOSUCH, "filename too long");
return;
}
if (con->uopt->files && hash_lookup (con->uopt->files, av[0]))
{
send_cmd (con, MSG_SERVER_NOSUCH, "duplicate file");
return;
}
fsize = strtoul (av[1], 0, 10);
if(fsize + con->user->libsize < con->user->libsize)
{
log("share_file(): %u byte file would overflow %s's library size",
fsize, con->user->nick);
return;
}
if (!(info = new_datum (av[0], av[2])))
return;
info->user = con->user;
info->size = fsize;
info->type = type;
insert_datum (info, av[0]);
}
/* 10012 <nick> <shared> <size>
remote server is notifying us that one of its users is sharing files */
HANDLER (user_sharing)
{
char *av[3];
USER *user;
int shared;
unsigned int libsize;
(void) len;
ASSERT (validate_connection (con));
CHECK_SERVER_CLASS ("user_sharing");
if (split_line (av, sizeof (av) / sizeof (char *), pkt) != 3)
{
log ("user_sharing(): wrong number of arguments");
return;
}
user = hash_lookup (Users, av[0]);
if (!user)
{
log ("user_sharing(): no such user %s (from %s)", av[0], con->host);
return;
}
shared = atoi(av[1]);
if(shared<0)
{
log("user_sharing(): negative count for %s from %s", av[0], con->host);
Num_Files -= user->shared;
Num_Gigs -= user->libsize;
user->shared = 0;
user->libsize = 0;
}
else
{
if(shared > user->shared)
Num_Files += shared - user->shared;
else
Num_Files -= user->shared - shared;
user->shared = shared;
libsize = strtoul (av[2],0,10);
if(libsize>user->libsize)
Num_Gigs += libsize - user->libsize;
else
Num_Gigs -= user->libsize - libsize;
user->libsize = libsize;
}
pass_message_args (con, tag, "%s %hu %u", user->nick, user->shared,
user->libsize);
}
/* 870 "<directory>" "<basename>" <md5> <size> <bitrate> <freq> <duration> [ ... ]
client command to add multiple files in the same directory */
HANDLER (add_directory)
{
char *dir, *basename, *md5, *size, *bitrate, *freq, *duration;
char path[_POSIX_PATH_MAX], dirbuf[_POSIX_PATH_MAX];
int pathlen, fsize;
DATUM *info;
(void) tag;
(void) len;
ASSERT (validate_connection (con));
CHECK_USER_CLASS ("add_directory");
dir = next_arg (&pkt); /* directory */
if (!dir)
{
log ("add_directory(): missing directory component");
return;
}
pathlen = strlen (dir);
if ((size_t) pathlen >= sizeof (dirbuf) - 1)
{
log ("add_directory(): directory component is too long, ignoring");
return;
}
ASSERT(pathlen<sizeof(dirbuf)-1);
dirbuf[sizeof (dirbuf) - 1] = 0; /* ensure nul termination */
strncpy (dirbuf, dir, sizeof (dirbuf) - 1);
if (pathlen > 0 && dirbuf[pathlen - 1] != '\\')
{
dirbuf[pathlen++]='\\';
dirbuf[pathlen]=0;
if ((size_t) pathlen >= sizeof (dirbuf) - 1)
{
ASSERT ((size_t) pathlen < sizeof (dirbuf));
log
("add_directory(): directory component is too long, ignoring");
return;
}
}
/* if the client passes a dir + file that is longer than 255 chars,
* strncpy() won't write a \0 at the end of the string, so ensure that
* this always happens
*/
path[sizeof(path)-1]=0;
while (pkt)
{
if (Max_Shared && con->user->shared > Max_Shared)
{
send_cmd (con, MSG_SERVER_NOSUCH,
"You may only share %d files", Max_Shared);
return;
}
basename = next_arg (&pkt);
md5 = next_arg (&pkt);
size = next_arg (&pkt);
bitrate = next_arg (&pkt);
freq = next_arg (&pkt);
duration = next_arg (&pkt);
if (!basename || !md5 || !size || !bitrate || !freq || !duration)
{
unparsable (con);
return;
}
strncpy(path,dirbuf,sizeof(path)-1);
strncpy(path+pathlen,basename,sizeof(path)-1-pathlen);
ASSERT(path[sizeof(path)-1]==0);
/* TODO: still seeing crashes here, we must be overwriting the
* stack on occasion. quit now if we detect this condition
*/
if(path[sizeof(path)-1] != 0)
{
log("add_directory(): ERROR! buffer overflow detected");
return;
}
if (con->uopt->files && hash_lookup (con->uopt->files, path))
{
send_cmd (con, MSG_SERVER_NOSUCH, "Duplicate file");
continue; /* get next file */
}
fsize = atoi (size);
if(fsize<1)
{
send_cmd(con,MSG_SERVER_NOSUCH,"invalid size");
continue;
}
/* create the db record for this file */
if (!(info = new_datum (path, md5)))
return;
info->user = con->user;
info->size = fsize;
info->bitrate = bitrateToMask (atoi (bitrate), con->user);
info->frequency = freqToMask (atoi (freq), con->user);
info->duration = atoi (duration);
info->type = CT_MP3;
insert_datum (info, path);
}
}