Skip to content

Commit 4d71f59

Browse files
author
drscholl
committed
added more checking to prevent a user's library size from being negative
1 parent be63a47 commit 4d71f59

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

FAQ

+5
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ A: If you have memory to spare, you can increase the value of
5555

5656
Q: How do I get my server listed on napigator.com?
5757
A: Go to www.napigator.com and look for the section on adding servers.
58+
59+
Q: How do I link my server to other servers?
60+
A: Find the people who run the server you want to link with and discuss it
61+
with them. If both parties agree you exchange a password and follow
62+
the directions in the README for linking up.

add_file.c

+56-16
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void
7272
insert_datum (DATUM * info, char *av)
7373
{
7474
LIST *tokens, *ptr;
75-
int fsize;
75+
unsigned int fsize;
7676

7777
ASSERT (info != 0);
7878
ASSERT (av != 0);
@@ -199,7 +199,7 @@ HANDLER (add_file)
199199
{
200200
char *av[6];
201201
DATUM *info;
202-
int fsize;
202+
unsigned int fsize;
203203

204204
(void) tag;
205205
(void) len;
@@ -229,10 +229,12 @@ HANDLER (add_file)
229229
}
230230

231231
/* ensure we have a valid byte count */
232-
fsize = atoi (av[2]);
233-
if (fsize < 1)
232+
fsize = strtoul (av[2],0,10);
233+
/* check for overflow */
234+
if(con->user->libsize + fsize < con->user->libsize)
234235
{
235-
send_cmd (con, MSG_SERVER_NOSUCH, "invalid file size");
236+
log("add_file(): %u byte file would overflow %s's library size",
237+
fsize, con->user->nick);
236238
return;
237239
}
238240

@@ -273,6 +275,7 @@ HANDLER (share_file)
273275
char *av[4];
274276
DATUM *info;
275277
int i, type;
278+
unsigned int fsize;
276279

277280
(void) len;
278281
(void) tag;
@@ -327,10 +330,18 @@ HANDLER (share_file)
327330
return;
328331
}
329332

333+
fsize = strtoul (av[1], 0, 10);
334+
if(fsize + con->user->libsize < con->user->libsize)
335+
{
336+
log("share_file(): %u byte file would overflow %s's library size",
337+
fsize, con->user->nick);
338+
return;
339+
}
340+
330341
if (!(info = new_datum (av[0], av[2])))
331342
return;
332343
info->user = con->user;
333-
info->size = atoi (av[1]);
344+
info->size = fsize;
334345
info->type = type;
335346

336347
insert_datum (info, av[0]);
@@ -342,7 +353,8 @@ HANDLER (user_sharing)
342353
{
343354
char *av[3];
344355
USER *user;
345-
int deltanum, deltasize;
356+
int shared;
357+
unsigned int libsize;
346358

347359
(void) len;
348360
ASSERT (validate_connection (con));
@@ -358,13 +370,34 @@ HANDLER (user_sharing)
358370
log ("user_sharing(): no such user %s (from %s)", av[0], con->host);
359371
return;
360372
}
361-
deltanum = atoi (av[1]) - user->shared;
362-
Num_Files += deltanum;
363-
user->shared += deltanum;
364-
deltasize = atoi (av[2]) - user->libsize;
365-
Num_Gigs += deltasize;
366-
user->libsize += deltasize;
367-
pass_message_args (con, tag, "%s %d %d", user->nick, user->shared,
373+
374+
shared = atoi(av[1]);
375+
376+
if(shared<0)
377+
{
378+
log("user_sharing(): negative count for %s from %s", av[0], con->host);
379+
Num_Files -= user->shared;
380+
Num_Gigs -= user->libsize;
381+
user->shared = 0;
382+
user->libsize = 0;
383+
}
384+
else
385+
{
386+
if(shared > user->shared)
387+
Num_Files += shared - user->shared;
388+
else
389+
Num_Files -= user->shared - shared;
390+
user->shared = shared;
391+
392+
libsize = strtoul (av[2],0,10);
393+
if(libsize>user->libsize)
394+
Num_Gigs += libsize - user->libsize;
395+
else
396+
Num_Gigs -= user->libsize - libsize;
397+
user->libsize = libsize;
398+
}
399+
400+
pass_message_args (con, tag, "%s %hu %u", user->nick, user->shared,
368401
user->libsize);
369402
}
370403

@@ -374,7 +407,7 @@ HANDLER (add_directory)
374407
{
375408
char *dir, *basename, *md5, *size, *bitrate, *freq, *duration;
376409
char path[_POSIX_PATH_MAX], dirbuf[_POSIX_PATH_MAX];
377-
int pathlen;
410+
int pathlen, fsize;
378411
DATUM *info;
379412

380413
(void) tag;
@@ -457,11 +490,18 @@ HANDLER (add_directory)
457490
continue; /* get next file */
458491
}
459492

493+
fsize = atoi (size);
494+
if(fsize<1)
495+
{
496+
send_cmd(con,MSG_SERVER_NOSUCH,"invalid size");
497+
continue;
498+
}
499+
460500
/* create the db record for this file */
461501
if (!(info = new_datum (path, md5)))
462502
return;
463503
info->user = con->user;
464-
info->size = atoi (size);
504+
info->size = fsize;
465505
info->bitrate = bitrateToMask (atoi (bitrate), con->user);
466506
info->frequency = freqToMask (atoi (freq), con->user);
467507
info->duration = atoi (duration);

handler.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
#include <ctype.h>
1717
#endif
1818

19+
/* 214 */
1920
HANDLER (server_stats)
2021
{
2122
(void) pkt;
22-
(void) tag;
2323
(void) len;
24-
send_cmd (con, MSG_SERVER_STATS, "%d %d %d", Users->dbsize, Num_Files,
25-
Num_Gigs / (1024 * 1024));
24+
send_cmd (con, tag, "%d %d %.0f", Users->dbsize, Num_Files,
25+
Num_Gigs / 1048576.);
2626
}
2727

2828
/* 10018 :<server> <target> <packet>
@@ -479,7 +479,7 @@ handle_connection (CONNECTION * con)
479479
&& tag != MSG_CLIENT_ADD_DIRECTORY)
480480
{
481481
pass_message_args (con, MSG_SERVER_USER_SHARING,
482-
"%s %d %d", con->user->nick,
482+
"%s %hu %u", con->user->nick,
483483
con->user->shared, con->user->libsize);
484484
con->user->sharing = 0;
485485
}
@@ -489,7 +489,7 @@ handle_connection (CONNECTION * con)
489489
if (tag != MSG_CLIENT_REMOVE_FILE)
490490
{
491491
pass_message_args (con, MSG_SERVER_USER_SHARING,
492-
"%s %d %d", con->user->nick,
492+
"%s %hu %u", con->user->nick,
493493
con->user->shared, con->user->libsize);
494494
con->user->unsharing = 0;
495495
}

remove_file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ HANDLER (remove_file)
1313
{
1414
USER *user;
1515
DATUM *info;
16-
int fsize;
16+
unsigned int fsize;
1717

1818
(void) tag;
1919
(void) len;

synch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ sync_user (USER * user, CONNECTION * con)
5858
/* do this before the joins so the user's already in the channel see
5959
the real file count */
6060
if (user->shared)
61-
send_cmd (con, MSG_SERVER_USER_SHARING, "%s %d %d", user->nick,
61+
send_cmd (con, MSG_SERVER_USER_SHARING, "%s %hu %u", user->nick,
6262
user->shared, user->libsize);
6363

6464
/* send the channels this user is listening on */

0 commit comments

Comments
 (0)