1 |
|
|
2 |
/* Copyright (C) 1999 Beau Kuiper |
/* Copyright (C) 1999 Beau Kuiper |
3 |
this is a quick hack for ratiotool, works ok but that is all |
this is a quick hack for ratiotool, works ok but that is all |
4 |
warning, this code is not buffer overflow safe. DONT RUN AS SETUID ROOT |
warning, this code is not buffer overflow safe. DONT RUN AS SETUID ROOT |
27 |
#define RELATIVE TRUE |
#define RELATIVE TRUE |
28 |
#define ABSOLUTE FALSE |
#define ABSOLUTE FALSE |
29 |
|
|
30 |
int lockarea(int fd, int pos, int len, int locktype, int do_wait) |
int lockarea(int fd, |
31 |
|
int pos, |
32 |
|
int len, |
33 |
|
int locktype, |
34 |
|
int do_wait) |
35 |
{ |
{ |
36 |
struct flock lock; |
struct flock lock; |
37 |
register int err; |
register int err; |
38 |
int lockt; |
int lockt; |
39 |
|
|
40 |
lockt = (do_wait == TRUE ? F_SETLKW : F_SETLK); |
lockt = (do_wait == TRUE ? F_SETLKW : F_SETLK); |
41 |
lock.l_type = locktype; |
lock.l_type = locktype; |
42 |
lock.l_whence = SEEK_SET; |
lock.l_whence = SEEK_SET; |
43 |
lock.l_start = pos; |
lock.l_start = pos; |
44 |
lock.l_len = len; |
lock.l_len = len; |
45 |
NOSIGNALINTR(err = fcntl(fd, lockt, &lock)); |
NOSIGNALINTR(err = fcntl(fd, lockt, &lock)); |
46 |
|
|
47 |
if (err == -1) |
if (err == -1) |
48 |
{ |
{ |
49 |
if (errno != EAGAIN) |
if (errno != EAGAIN) |
50 |
exit(0); |
exit(0); |
51 |
else |
else |
52 |
return(FALSE); |
return (FALSE); |
53 |
} |
} |
54 |
return(TRUE); |
return (TRUE); |
55 |
} |
} |
56 |
|
|
57 |
void lock_n_read(RATIOHANDLE *rh) |
void lock_n_read(RATIOHANDLE * rh) |
58 |
{ |
{ |
59 |
if (rh->ratiofilefd != 0) |
if (rh->ratiofilefd != 0) |
60 |
{ |
{ |
61 |
lockarea(rh->ratiofilefd, rh->filepos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
lockarea(rh->ratiofilefd, rh->filepos, sizeof(RATIOFILEDATA), F_WRLCK, |
62 |
|
TRUE); |
63 |
lseek(rh->ratiofilefd, rh->filepos, SEEK_SET); |
lseek(rh->ratiofilefd, rh->filepos, SEEK_SET); |
64 |
read(rh->ratiofilefd, rh->rdat, sizeof(RATIOFILEDATA)); |
read(rh->ratiofilefd, rh->rdat, sizeof(RATIOFILEDATA)); |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
68 |
void write_n_unlock(RATIOHANDLE *rh) |
void write_n_unlock(RATIOHANDLE * rh) |
69 |
{ |
{ |
70 |
if (rh->ratiofilefd != 0) |
if (rh->ratiofilefd != 0) |
71 |
{ |
{ |
72 |
lseek(rh->ratiofilefd, rh->filepos, SEEK_SET); |
lseek(rh->ratiofilefd, rh->filepos, SEEK_SET); |
73 |
write(rh->ratiofilefd, rh->rdat, sizeof(RATIOFILEDATA)); |
write(rh->ratiofilefd, rh->rdat, sizeof(RATIOFILEDATA)); |
74 |
lockarea(rh->ratiofilefd, rh->filepos, sizeof(RATIOFILEDATA), F_UNLCK, TRUE); |
lockarea(rh->ratiofilefd, rh->filepos, sizeof(RATIOFILEDATA), F_UNLCK, |
75 |
|
TRUE); |
76 |
} |
} |
77 |
} |
} |
78 |
|
|
79 |
int decoderatiostr(char *ratstr, int *pa, int *pb) |
int decoderatiostr(char *ratstr, |
80 |
|
int *pa, |
81 |
|
int *pb) |
82 |
{ |
{ |
83 |
return(sscanf(ratstr, "%d:%d", pa, pb) == 2); |
return (sscanf(ratstr, "%d:%d", pa, pb) == 2); |
84 |
} |
} |
85 |
|
|
86 |
RATIOHANDLE *getuserrh(char *filename, char *username) |
RATIOHANDLE *getuserrh(char *filename, |
87 |
|
char *username) |
88 |
{ |
{ |
89 |
static RATIOHANDLE rh; |
static RATIOHANDLE rh; |
90 |
static RATIOFILEDATA filedata; |
static RATIOFILEDATA filedata; |
91 |
int pos = 0; |
int pos = 0; |
92 |
|
|
93 |
rh.rdat = &filedata; |
rh.rdat = &filedata; |
94 |
rh.ratiofilefd = open(filename, O_RDWR); |
rh.ratiofilefd = open(filename, O_RDWR); |
95 |
if (rh.ratiofilefd == -1) |
if (rh.ratiofilefd == -1) |
96 |
ERRORMSGFATAL("couldn't open ratio file!"); |
ERRORMSGFATAL("couldn't open ratio file!"); |
97 |
|
|
98 |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
99 |
|
|
100 |
while(read(rh.ratiofilefd, &filedata, sizeof(RATIOFILEDATA)) == |
while (read(rh.ratiofilefd, &filedata, sizeof(RATIOFILEDATA)) == |
101 |
sizeof(RATIOFILEDATA)) |
sizeof(RATIOFILEDATA)) |
102 |
{ |
{ |
103 |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_UNLCK, TRUE); |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_UNLCK, TRUE); |
104 |
|
|
105 |
if (strcmp(filedata.username, username) == 0) |
if (strcmp(filedata.username, username) == 0) |
106 |
{ |
{ |
107 |
rh.filepos = pos; |
rh.filepos = pos; |
108 |
return(&rh); |
return (&rh); |
109 |
} |
} |
110 |
|
|
111 |
pos += sizeof(RATIOFILEDATA); |
pos += sizeof(RATIOFILEDATA); |
112 |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
113 |
} |
} |
114 |
close(rh.ratiofilefd); |
close(rh.ratiofilefd); |
115 |
return(NULL); |
return (NULL); |
116 |
} |
} |
117 |
|
|
118 |
/* this just returns a pointer to the end of the file, and checks to make user |
/* this just returns a pointer to the end of the file, and checks to make user |
119 |
the user doesn't exist yet */ |
the user doesn't exist yet */ |
120 |
|
|
121 |
RATIOHANDLE *adduserrh(char *filename, char *username) |
RATIOHANDLE *adduserrh(char *filename, |
122 |
|
char *username) |
123 |
{ |
{ |
124 |
static RATIOHANDLE rh; |
static RATIOHANDLE rh; |
125 |
static RATIOFILEDATA filedata; |
static RATIOFILEDATA filedata; |
126 |
int pos = 0; |
int pos = 0; |
127 |
|
|
128 |
rh.rdat = &filedata; |
rh.rdat = &filedata; |
129 |
rh.ratiofilefd = open(filename, O_RDWR | O_CREAT, 0600); |
rh.ratiofilefd = open(filename, O_RDWR | O_CREAT, 0600); |
130 |
if (rh.ratiofilefd == -1) |
if (rh.ratiofilefd == -1) |
131 |
ERRORMSGFATAL("couldn't open ratio file!"); |
ERRORMSGFATAL("couldn't open ratio file!"); |
132 |
|
|
133 |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
134 |
|
|
135 |
while(read(rh.ratiofilefd, &filedata, sizeof(RATIOFILEDATA)) == |
while (read(rh.ratiofilefd, &filedata, sizeof(RATIOFILEDATA)) == |
136 |
sizeof(RATIOFILEDATA)) |
sizeof(RATIOFILEDATA)) |
137 |
{ |
{ |
138 |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_UNLCK, TRUE); |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_UNLCK, TRUE); |
139 |
|
|
140 |
if (strcmp(filedata.username, username) == 0) |
if (strcmp(filedata.username, username) == 0) |
141 |
{ |
{ |
142 |
close(rh.ratiofilefd); |
close(rh.ratiofilefd); |
143 |
return(NULL); |
return (NULL); |
144 |
} |
} |
145 |
|
|
146 |
pos += sizeof(RATIOFILEDATA); |
pos += sizeof(RATIOFILEDATA); |
147 |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
lockarea(rh.ratiofilefd, pos, sizeof(RATIOFILEDATA), F_WRLCK, TRUE); |
148 |
} |
} |
149 |
rh.filepos = pos; |
rh.filepos = pos; |
150 |
return(&rh); |
return (&rh); |
151 |
} |
} |
152 |
|
|
153 |
void usage(void) |
void usage(void) |
166 |
printf(" -h Displays this usage message.\n"); |
printf(" -h Displays this usage message.\n"); |
167 |
printf(" -V Displays version information.\n\n"); |
printf(" -V Displays version information.\n\n"); |
168 |
|
|
169 |
printf("You must specify -f and only one of -a, -e, -d or -i when running ratiotool\n\n"); |
printf |
170 |
|
("You must specify -f and only one of -a, -e, -d or -i when running ratiotool\n\n"); |
171 |
exit(0); |
exit(0); |
172 |
} |
} |
173 |
|
|
174 |
void showinfo(RATIOHANDLE *rh) |
void showinfo(RATIOHANDLE * rh) |
175 |
{ |
{ |
176 |
printf("Showing ratio information for user '%s'\n", rh->rdat->username); |
printf("Showing ratio information for user '%s'\n", rh->rdat->username); |
177 |
printf("\nThis user has the following download credits:\n\n"); |
printf("\nThis user has the following download credits:\n\n"); |
178 |
if (rh->rdat->flags & BYTECREDITS) |
if (rh->rdat->flags & BYTECREDITS) |
179 |
printf("%15lld bytes\n", rh->rdat->downloadcredits / rh->rdat->byteoutmult); |
printf("%15lld bytes\n", |
180 |
|
rh->rdat->downloadcredits / rh->rdat->byteoutmult); |
181 |
if (rh->rdat->flags & FILECREDITS) |
if (rh->rdat->flags & FILECREDITS) |
182 |
printf("%15d files\n", rh->rdat->filecredits / rh->rdat->fileoutmult); |
printf("%15d files\n", rh->rdat->filecredits / rh->rdat->fileoutmult); |
183 |
if (rh->rdat->flags == 0) |
if (rh->rdat->flags == 0) |
184 |
printf("This person does not use ratios and has no credits\n"); |
printf("This person does not use ratios and has no credits\n"); |
185 |
printf("\nThis user is subject to the following ratios:\n\n"); |
printf("\nThis user is subject to the following ratios:\n\n"); |
186 |
if (rh->rdat->flags & BYTECREDITS) |
if (rh->rdat->flags & BYTECREDITS) |
187 |
printf("Byte ratio: %d:%d (ie %d bytes uploaded = %d download bytes)\n", |
printf |
188 |
rh->rdat->byteinmult, rh->rdat->byteoutmult, |
("Byte ratio: %d:%d (ie %d bytes uploaded = %d download bytes)\n", |
189 |
rh->rdat->byteoutmult, rh->rdat->byteinmult); |
rh->rdat->byteinmult, rh->rdat->byteoutmult, |
190 |
|
rh->rdat->byteoutmult, rh->rdat->byteinmult); |
191 |
if (rh->rdat->flags & FILECREDITS) |
if (rh->rdat->flags & FILECREDITS) |
192 |
printf("File ratio: %d:%d (ie %d files uploaded = %d download files)\n", |
printf |
193 |
rh->rdat->fileinmult, rh->rdat->fileoutmult, |
("File ratio: %d:%d (ie %d files uploaded = %d download files)\n", |
194 |
rh->rdat->fileoutmult, rh->rdat->fileinmult); |
rh->rdat->fileinmult, rh->rdat->fileoutmult, |
195 |
|
rh->rdat->fileoutmult, rh->rdat->fileinmult); |
196 |
if (rh->rdat->flags == 0) |
if (rh->rdat->flags == 0) |
197 |
printf("This person does not use ratios.\n"); |
printf("This person does not use ratios.\n"); |
198 |
printf("\n\n"); |
printf("\n\n"); |
199 |
} |
} |
200 |
|
|
201 |
void adduser(char *rationame, char *username, char *byteratio, char *fileratio, |
void adduser(char *rationame, |
202 |
char *filecount, char *bytecount) |
char *username, |
203 |
|
char *byteratio, |
204 |
|
char *fileratio, |
205 |
|
char *filecount, |
206 |
|
char *bytecount) |
207 |
{ |
{ |
208 |
int flags = 0; |
int flags = 0; |
209 |
int upb, downb, upf, downf; |
int upb, downb, upf, downf; |
210 |
int initalf; |
int initalf; |
211 |
long long int initalb; |
long long int initalb; |
212 |
RATIOHANDLE *rh; |
RATIOHANDLE *rh; |
213 |
|
|
214 |
if (byteratio == NULL) |
if (byteratio == NULL) |
215 |
{ |
{ |
216 |
byteratio = (char *)malloc(4096); |
byteratio = (char *) malloc(4096); |
217 |
printf("Enter a byte ratio for the user (down:up or 'none') : "); |
printf("Enter a byte ratio for the user (down:up or 'none') : "); |
218 |
fgets(byteratio, 4096, stdin); |
fgets(byteratio, 4096, stdin); |
219 |
byteratio[strlen(byteratio) - 1] = 0; |
byteratio[strlen(byteratio) - 1] = 0; |
220 |
} |
} |
221 |
if (fileratio == NULL) |
if (fileratio == NULL) |
222 |
{ |
{ |
223 |
fileratio = (char *)malloc(4096); |
fileratio = (char *) malloc(4096); |
224 |
printf("Enter a file ratio for the user (down:up or 'none') : "); |
printf("Enter a file ratio for the user (down:up or 'none') : "); |
225 |
fgets(fileratio, 4096, stdin); |
fgets(fileratio, 4096, stdin); |
226 |
fileratio[strlen(fileratio) - 1] = 0; |
fileratio[strlen(fileratio) - 1] = 0; |
227 |
} |
} |
228 |
|
|
229 |
if (strcasecmp(byteratio, "none") != 0) |
if (strcasecmp(byteratio, "none") != 0) |
230 |
flags |= BYTECREDITS; |
flags |= BYTECREDITS; |
231 |
if (strcasecmp(fileratio, "none") != 0) |
if (strcasecmp(fileratio, "none") != 0) |
232 |
flags |= FILECREDITS; |
flags |= FILECREDITS; |
233 |
|
|
234 |
if (flags & BYTECREDITS) |
if (flags & BYTECREDITS) |
235 |
{ |
{ |
236 |
if (bytecount == NULL) |
if (bytecount == NULL) |
237 |
{ |
{ |
238 |
bytecount = (char *)malloc(4096); |
bytecount = (char *) malloc(4096); |
239 |
printf("Enter an inital byte credit for the user (bytes) : "); |
printf("Enter an inital byte credit for the user (bytes) : "); |
240 |
fgets(bytecount, 4096, stdin); |
fgets(bytecount, 4096, stdin); |
241 |
bytecount[strlen(bytecount) - 1] = 0; |
bytecount[strlen(bytecount) - 1] = 0; |
242 |
} |
} |
243 |
if (sscanf(byteratio, "%d:%d", &upb, &downb) != 2) |
if (sscanf(byteratio, "%d:%d", &upb, &downb) != 2) |
244 |
ERRORMSGFATAL("could not decode byte ratio"); |
ERRORMSGFATAL("could not decode byte ratio"); |
245 |
if (sscanf(bytecount, "%lld", &initalb) != 1) |
if (sscanf(bytecount, "%lld", &initalb) != 1) |
246 |
ERRORMSGFATAL("could not decode inital byte credits"); |
ERRORMSGFATAL("could not decode inital byte credits"); |
247 |
initalb *= downb; |
initalb *= downb; |
248 |
} |
} |
249 |
|
|
250 |
if (flags & FILECREDITS) |
if (flags & FILECREDITS) |
251 |
{ |
{ |
252 |
if (filecount == NULL) |
if (filecount == NULL) |
253 |
{ |
{ |
254 |
filecount = (char *)malloc(4096); |
filecount = (char *) malloc(4096); |
255 |
printf("Enter a inital file credit for the user : "); |
printf("Enter a inital file credit for the user : "); |
256 |
fgets(filecount, 4096, stdin); |
fgets(filecount, 4096, stdin); |
257 |
filecount[strlen(filecount) - 1] = 0; |
filecount[strlen(filecount) - 1] = 0; |
259 |
if (sscanf(fileratio, "%d:%d", &upf, &downf) != 2) |
if (sscanf(fileratio, "%d:%d", &upf, &downf) != 2) |
260 |
ERRORMSGFATAL("could not decode file ratio"); |
ERRORMSGFATAL("could not decode file ratio"); |
261 |
printf("%d\n", sscanf(filecount, "%d", &initalf)); |
printf("%d\n", sscanf(filecount, "%d", &initalf)); |
262 |
|
|
263 |
if (sscanf(filecount, "%d", &initalf) != 1) |
if (sscanf(filecount, "%d", &initalf) != 1) |
264 |
ERRORMSGFATAL("could not decode inital file credits"); |
ERRORMSGFATAL("could not decode inital file credits"); |
265 |
initalf *= downf; |
initalf *= downf; |
266 |
} |
} |
267 |
|
|
268 |
rh = adduserrh(rationame, username); |
rh = adduserrh(rationame, username); |
269 |
if (rh == NULL) |
if (rh == NULL) |
270 |
ERRORMSGFATAL("user by that name already exists in the ratio file"); |
ERRORMSGFATAL("user by that name already exists in the ratio file"); |
271 |
|
|
272 |
lock_n_read(rh); |
lock_n_read(rh); |
273 |
memset(rh->rdat, 0, sizeof(RATIOFILEDATA)); |
memset(rh->rdat, 0, sizeof(RATIOFILEDATA)); |
274 |
strncpy(rh->rdat->username, username, MAXNAMELEN); |
strncpy(rh->rdat->username, username, MAXNAMELEN); |
275 |
rh->rdat->flags = flags; |
rh->rdat->flags = flags; |
276 |
if (flags & FILECREDITS) |
if (flags & FILECREDITS) |
277 |
{ |
{ |
278 |
rh->rdat->filecredits = initalf; |
rh->rdat->filecredits = initalf; |
279 |
rh->rdat->fileinmult = (short int)upf; |
rh->rdat->fileinmult = (short int) upf; |
280 |
rh->rdat->fileoutmult = (short int)downf; |
rh->rdat->fileoutmult = (short int) downf; |
281 |
} |
} |
282 |
if (flags & BYTECREDITS) |
if (flags & BYTECREDITS) |
283 |
{ |
{ |
284 |
rh->rdat->downloadcredits = initalb; |
rh->rdat->downloadcredits = initalb; |
285 |
rh->rdat->byteinmult = (short int)upb; |
rh->rdat->byteinmult = (short int) upb; |
286 |
rh->rdat->byteoutmult = (short int)downb; |
rh->rdat->byteoutmult = (short int) downb; |
287 |
} |
} |
288 |
write_n_unlock(rh); |
write_n_unlock(rh); |
289 |
close(rh->ratiofilefd); |
close(rh->ratiofilefd); |
290 |
printf("Adding user to ratio file successful!\n"); |
printf("Adding user to ratio file successful!\n"); |
291 |
} |
} |
292 |
|
|
293 |
void edituser(char *rationame, char *username, char *byteratio, char *fileratio, |
void edituser(char *rationame, |
294 |
char *filecount, char *bytecount, int edit_mask) |
char *username, |
295 |
|
char *byteratio, |
296 |
|
char *fileratio, |
297 |
|
char *filecount, |
298 |
|
char *bytecount, |
299 |
|
int edit_mask) |
300 |
{ |
{ |
301 |
int upb, downb, upf, downf; |
int upb, downb, upf, downf; |
302 |
int initalf; |
int initalf; |
309 |
#define FILERATIOMSK 2 |
#define FILERATIOMSK 2 |
310 |
#define BYTECREDITMSK 4 |
#define BYTECREDITMSK 4 |
311 |
#define FILECREDITMSK 8 |
#define FILECREDITMSK 8 |
312 |
|
|
313 |
/* decode arguments */ |
/* |
314 |
|
* decode arguments |
315 |
|
*/ |
316 |
if (edit_mask & BYTERATIOMSK) |
if (edit_mask & BYTERATIOMSK) |
317 |
if (strcasecmp(byteratio, "none") != 0) |
if (strcasecmp(byteratio, "none") != 0) |
318 |
if (sscanf(byteratio, "%d:%d", &upb, &downb) != 2) |
if (sscanf(byteratio, "%d:%d", &upb, &downb) != 2) |
330 |
if (sscanf(bytecount, "%lld", &initalb) != 1) |
if (sscanf(bytecount, "%lld", &initalb) != 1) |
331 |
ERRORMSGFATAL("could not decode byte credits"); |
ERRORMSGFATAL("could not decode byte credits"); |
332 |
} |
} |
333 |
|
|
334 |
if (edit_mask & FILERATIOMSK) |
if (edit_mask & FILERATIOMSK) |
335 |
if (strcasecmp(fileratio, "none") != 0) |
if (strcasecmp(fileratio, "none") != 0) |
336 |
if (sscanf(fileratio, "%d:%d", &upf, &downf) != 2) |
if (sscanf(fileratio, "%d:%d", &upf, &downf) != 2) |
337 |
ERRORMSGFATAL("could not decode file ratio"); |
ERRORMSGFATAL("could not decode file ratio"); |
338 |
|
|
339 |
if (edit_mask & FILECREDITMSK) |
if (edit_mask & FILECREDITMSK) |
340 |
{ |
{ |
341 |
if (filecount[0] == 'r') |
if (filecount[0] == 'r') |
348 |
if (sscanf(filecount, "%d", &initalf) != 1) |
if (sscanf(filecount, "%d", &initalf) != 1) |
349 |
ERRORMSGFATAL("could not decode file credits"); |
ERRORMSGFATAL("could not decode file credits"); |
350 |
} |
} |
351 |
|
|
352 |
rh = getuserrh(rationame, username); |
rh = getuserrh(rationame, username); |
353 |
if (rh == NULL) |
if (rh == NULL) |
354 |
ERRORMSGFATAL("Username is not in ratio file."); |
ERRORMSGFATAL("Username is not in ratio file."); |
355 |
|
|
356 |
lock_n_read(rh); |
lock_n_read(rh); |
357 |
|
|
358 |
if (edit_mask & BYTERATIOMSK) |
if (edit_mask & BYTERATIOMSK) |
359 |
{ |
{ |
360 |
if (strcasecmp(byteratio, "none") == 0) |
if (strcasecmp(byteratio, "none") == 0) |
361 |
rh->rdat->flags &= FILECREDITS; |
rh->rdat->flags &= FILECREDITS; |
362 |
else |
else |
363 |
{ |
{ |
364 |
/* this updates the ratio and the credits */ |
/* |
365 |
|
* this updates the ratio and the credits |
366 |
|
*/ |
367 |
rh->rdat->flags |= BYTECREDITS; |
rh->rdat->flags |= BYTECREDITS; |
368 |
rh->rdat->downloadcredits /= rh->rdat->byteoutmult; |
rh->rdat->downloadcredits /= rh->rdat->byteoutmult; |
369 |
rh->rdat->byteinmult = (short int)upb; |
rh->rdat->byteinmult = (short int) upb; |
370 |
rh->rdat->byteoutmult = (short int)downb; |
rh->rdat->byteoutmult = (short int) downb; |
371 |
rh->rdat->downloadcredits *= rh->rdat->byteoutmult; |
rh->rdat->downloadcredits *= rh->rdat->byteoutmult; |
372 |
} |
} |
373 |
} |
} |
374 |
|
|
375 |
if (edit_mask & FILERATIOMSK) |
if (edit_mask & FILERATIOMSK) |
376 |
{ |
{ |
377 |
if (strcasecmp(fileratio, "none") == 0) |
if (strcasecmp(fileratio, "none") == 0) |
378 |
rh->rdat->flags &= BYTECREDITS; |
rh->rdat->flags &= BYTECREDITS; |
379 |
else |
else |
380 |
{ |
{ |
381 |
/* this updates the ratio and the credits */ |
/* |
382 |
|
* this updates the ratio and the credits |
383 |
|
*/ |
384 |
rh->rdat->flags |= FILECREDITS; |
rh->rdat->flags |= FILECREDITS; |
385 |
rh->rdat->filecredits /= rh->rdat->fileoutmult; |
rh->rdat->filecredits /= rh->rdat->fileoutmult; |
386 |
rh->rdat->fileinmult = (short int)upf; |
rh->rdat->fileinmult = (short int) upf; |
387 |
rh->rdat->fileoutmult = (short int)downf; |
rh->rdat->fileoutmult = (short int) downf; |
388 |
rh->rdat->filecredits *= rh->rdat->fileoutmult; |
rh->rdat->filecredits *= rh->rdat->fileoutmult; |
389 |
} |
} |
390 |
} |
} |
391 |
|
|
392 |
if (edit_mask & BYTECREDITMSK) |
if (edit_mask & BYTECREDITMSK) |
393 |
{ |
{ |
394 |
if (modb == ABSOLUTE) |
if (modb == ABSOLUTE) |
395 |
rh->rdat->downloadcredits = initalb * rh->rdat->byteoutmult; |
rh->rdat->downloadcredits = initalb * rh->rdat->byteoutmult; |
396 |
else |
else |
397 |
rh->rdat->downloadcredits += initalb * rh->rdat->byteoutmult; |
rh->rdat->downloadcredits += initalb * rh->rdat->byteoutmult; |
398 |
} |
} |
399 |
if (edit_mask & FILECREDITMSK) |
if (edit_mask & FILECREDITMSK) |
400 |
{ |
{ |
401 |
if (modf == ABSOLUTE) |
if (modf == ABSOLUTE) |
402 |
rh->rdat->filecredits = initalf * rh->rdat->fileoutmult; |
rh->rdat->filecredits = initalf * rh->rdat->fileoutmult; |
403 |
else |
else |
404 |
rh->rdat->filecredits += initalf * rh->rdat->fileoutmult; |
rh->rdat->filecredits += initalf * rh->rdat->fileoutmult; |
405 |
} |
} |
406 |
|
|
407 |
write_n_unlock(rh); |
write_n_unlock(rh); |
408 |
close(rh->ratiofilefd); |
close(rh->ratiofilefd); |
409 |
printf("Editing user in ratio file successful!\n"); |
printf("Editing user in ratio file successful!\n"); |
410 |
} |
} |
411 |
|
|
412 |
void deluser(char *ratiofile, char *username) |
void deluser(char *ratiofile, |
413 |
|
char *username) |
414 |
{ |
{ |
415 |
RATIOHANDLE *rh; |
RATIOHANDLE *rh; |
416 |
|
|
417 |
rh = getuserrh(ratiofile, username); |
rh = getuserrh(ratiofile, username); |
418 |
if (rh == NULL) |
if (rh == NULL) |
419 |
ERRORMSGFATAL("Username is not in ratio file!"); |
ERRORMSGFATAL("Username is not in ratio file!"); |
420 |
|
|
421 |
lock_n_read(rh); |
lock_n_read(rh); |
422 |
rh->rdat->username[0] = 0; |
rh->rdat->username[0] = 0; |
423 |
write_n_unlock(rh); |
write_n_unlock(rh); |
424 |
|
|
425 |
close(rh->ratiofilefd); |
close(rh->ratiofilefd); |
426 |
printf("Deleting user from ratio file successful!\n"); |
printf("Deleting user from ratio file successful!\n"); |
427 |
} |
} |
428 |
|
|
429 |
int main(int argc, char **argv) |
int main(int argc, |
430 |
|
char **argv) |
431 |
{ |
{ |
432 |
int ch; |
int ch; |
433 |
char *ratiofile = NULL; |
char *ratiofile = NULL; |
442 |
int get_info = FALSE; |
int get_info = FALSE; |
443 |
int edit_mask = FALSE; |
int edit_mask = FALSE; |
444 |
extern char *optarg; |
extern char *optarg; |
445 |
|
|
446 |
/* stolen from smbpasswd - checking for setuid root! */ |
/* |
447 |
|
* stolen from smbpasswd - checking for setuid root! |
448 |
/* Check the effective uid - make sure we are not setuid */ |
*/ |
449 |
if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) |
|
450 |
|
/* |
451 |
|
* Check the effective uid - make sure we are not setuid |
452 |
|
*/ |
453 |
|
if ((geteuid() == (uid_t) 0) && (getuid() != (uid_t) 0)) |
454 |
ERRORMSGFATAL("ratiotool must *NOT* be setuid root.\n"); |
ERRORMSGFATAL("ratiotool must *NOT* be setuid root.\n"); |
455 |
|
|
456 |
while ((ch = getopt(argc, argv, "f:a:d:e:r:R:c:C:i:hV")) != EOF) |
while ((ch = getopt(argc, argv, "f:a:d:e:r:R:c:C:i:hV")) != EOF) |
457 |
{ |
{ |
458 |
switch(ch) |
switch (ch) |
459 |
{ |
{ |
460 |
case 'V': |
case 'V': |
461 |
showversion("ratiotool"); |
showversion("ratiotool"); |
504 |
|
|
505 |
if (!ratiofile) |
if (!ratiofile) |
506 |
usage(); |
usage(); |
507 |
|
|
508 |
if (add_user) |
if (add_user) |
509 |
{ |
{ |
510 |
if (edit_user || delete_user || get_info) |
if (edit_user || delete_user || get_info) |
511 |
usage(); |
usage(); |
512 |
|
|
513 |
adduser(ratiofile, username, byteratio, fileratio, |
adduser(ratiofile, username, byteratio, fileratio, |
514 |
filecount, bytecount); |
filecount, bytecount); |
515 |
|
|
516 |
exit(0); |
exit(0); |
517 |
} |
} |
518 |
|
|
519 |
if (delete_user) |
if (delete_user) |
520 |
{ |
{ |
521 |
if (edit_user || get_info) |
if (edit_user || get_info) |
522 |
usage(); |
usage(); |
523 |
|
|
524 |
deluser(ratiofile, username); |
deluser(ratiofile, username); |
525 |
exit(0); |
exit(0); |
526 |
} |
} |
527 |
|
|
528 |
if (edit_user) |
if (edit_user) |
529 |
{ |
{ |
530 |
if (!edit_mask) |
if (!edit_mask) |
531 |
usage(); |
usage(); |
532 |
|
|
533 |
edituser(ratiofile, username, byteratio, fileratio, |
edituser(ratiofile, username, byteratio, fileratio, |
534 |
filecount, bytecount, edit_mask); |
filecount, bytecount, edit_mask); |
535 |
exit(0); |
exit(0); |
536 |
} |
} |
537 |
|
|
538 |
if (get_info) |
if (get_info) |
539 |
{ |
{ |
540 |
RATIOHANDLE *rh; |
RATIOHANDLE *rh; |
541 |
|
|
542 |
rh = getuserrh(ratiofile, username); |
rh = getuserrh(ratiofile, username); |
543 |
if (rh == NULL) |
if (rh == NULL) |
544 |
ERRORMSGFATAL("Username is not in ratio file!"); |
ERRORMSGFATAL("Username is not in ratio file!"); |
546 |
close(rh->ratiofilefd); |
close(rh->ratiofilefd); |
547 |
exit(0); |
exit(0); |
548 |
} |
} |
549 |
usage(); |
usage(); |
550 |
return(0); |
return (0); |
551 |
} |
} |