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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
/*
* ProFTPD: mod_quotatab_file -- a mod_quotatab sub-module for managing quota
* data via file-based tables
* Copyright (c) 2002-2016 TJ Saunders
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders gives permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
#include "mod_quotatab.h"
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
/* bloody lack of consistency... */
#if defined(FREEBSD4)
# define QUOTATAB_IOV_BASE_TYPE (char *)
#elif defined(LINUX)
# define QUOTATAB_IOV_BASE_TYPE (__ptr_t)
#elif defined(SOLARIS2)
# define QUOTATAB_IOV_BASE_TYPE (caddr_t)
#else
# define QUOTATAB_IOV_BASE_TYPE (void *)
#endif
module quotatab_file_module;
static int filetab_close(quota_table_t *filetab) {
int res = close(filetab->tab_handle);
filetab->tab_handle = -1;
return res;
}
static int filetab_create(quota_table_t *filetab, void *ptr) {
int res = -1;
struct iovec quotav[8];
off_t current_pos = 0;
quota_tally_t *tally = ptr;
/* Use writev() to make this more efficient. It is done piecewise, rather
* than doing a normal write(2) directly from the struct pointer, to avoid
* alignment/padding issues.
*/
quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE tally->name;
quotav[0].iov_len = sizeof(tally->name);
quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->quota_type);
quotav[1].iov_len = sizeof(tally->quota_type);
quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_in_used);
quotav[2].iov_len = sizeof(tally->bytes_in_used);
quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_out_used);
quotav[3].iov_len = sizeof(tally->bytes_out_used);
quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_xfer_used);
quotav[4].iov_len = sizeof(tally->bytes_xfer_used);
quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_in_used);
quotav[5].iov_len = sizeof(tally->files_in_used);
quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_out_used);
quotav[6].iov_len = sizeof(tally->files_out_used);
quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_xfer_used);
quotav[7].iov_len = sizeof(tally->files_xfer_used);
/* Seek to the end of the table */
current_pos = lseek(filetab->tab_handle, (off_t) 0, SEEK_END);
if (current_pos < 0) {
return -1;
}
while ((res = writev(filetab->tab_handle, quotav, 8)) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
if (res > 0) {
/* Rewind to the start of the entry. */
if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
quotatab_log("error rewinding to start of tally entry: %s",
strerror(errno));
return -1;
}
} else if (res == 0) {
/* If no bytes were written, it's an error. */
quotatab_log("error: writev(2) returned zero when creating tally entry, "
"returning EPERM");
errno = EPERM;
res = -1;
}
return res;
}
static unsigned char filetab_lookup(quota_table_t *filetab, void *ptr,
const char *name, quota_type_t quota_type) {
/* Make sure the table pointer is positioned at the start of the table,
* skipping the magic header value of the table.
*/
if (lseek(filetab->tab_handle, (off_t) sizeof(unsigned int), SEEK_SET) < 0) {
quotatab_log("error seeking past table header: %s", strerror(errno));
return FALSE;
}
/* Handle tally and limit tables differently... */
if (filetab->tab_type == TYPE_TALLY) {
quota_tally_t *tally = ptr;
while (filetab->tab_read(filetab, ptr) >= 0) {
pr_signals_handle();
/* Compare quota types. If the quota type is ALL_QUOTA, don't
* worry about the name.
*/
if (quota_type == tally->quota_type) {
/* Match names if need be */
if (name &&
strcmp(name, tally->name) == 0) {
return TRUE;
}
if (quota_type == ALL_QUOTA) {
return TRUE;
}
}
/* Undo the auto-rewind of a single record's length done by
* filetab_read(), so that the while loop actually does iterate through
* all the available records.
*/
if (lseek(filetab->tab_handle, filetab->tab_quotalen, SEEK_CUR) < 0) {
quotatab_log("error seeking past tally record: %s", strerror(errno));
}
}
} else if (filetab->tab_type == TYPE_LIMIT) {
quota_limit_t *limit = ptr;
while (filetab->tab_read(filetab, ptr) >= 0) {
pr_signals_handle();
if (quota_type == limit->quota_type) {
if (name &&
strcmp(name, limit->name) == 0) {
return TRUE;
}
if (quota_type == ALL_QUOTA) {
return TRUE;
}
}
if (lseek(filetab->tab_handle, filetab->tab_quotalen, SEEK_CUR) < 0) {
quotatab_log("error seeking past limit record: %s", strerror(errno));
}
}
}
/* Default return value */
return FALSE;
}
static int filetab_read(quota_table_t *filetab, void *ptr) {
int res = -1;
struct iovec quotav[10];
/* Mark the current file position. */
off_t current_pos = lseek(filetab->tab_handle, (off_t) 0, SEEK_CUR);
if (current_pos < 0) {
return - 1;
}
/* Use readv() to make this more efficient. It is done piecewise, rather
* than doing a normal read(2) directly into the struct pointer, to avoid
* alignment/padding issues.
*/
/* Handle the limit and tally tables differently. */
if (filetab->tab_type == TYPE_TALLY) {
quota_tally_t *tally = ptr;
quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE tally->name;
quotav[0].iov_len = sizeof(tally->name);
quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->quota_type);
quotav[1].iov_len = sizeof(tally->quota_type);
quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_in_used);
quotav[2].iov_len = sizeof(tally->bytes_in_used);
quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_out_used);
quotav[3].iov_len = sizeof(tally->bytes_out_used);
quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_xfer_used);
quotav[4].iov_len = sizeof(tally->bytes_xfer_used);
quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_in_used);
quotav[5].iov_len = sizeof(tally->files_in_used);
quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_out_used);
quotav[6].iov_len = sizeof(tally->files_out_used);
quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_xfer_used);
quotav[7].iov_len = sizeof(tally->files_xfer_used);
while ((res = readv(filetab->tab_handle, quotav, 8)) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
if (res > 0) {
/* Always rewind after reading a record. */
if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
quotatab_log("error rewinding to start of tally entry: %s",
strerror(errno));
return -1;
}
} else if (res == 0) {
/* Assume end-of-file. */
errno = EOF;
res = -1;
}
return res;
} else if (filetab->tab_type == TYPE_LIMIT) {
quota_limit_t *limit = ptr;
quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE limit->name;
quotav[0].iov_len = sizeof(limit->name);
quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->quota_type);
quotav[1].iov_len = sizeof(limit->quota_type);
quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->quota_per_session);
quotav[2].iov_len = sizeof(limit->quota_per_session);
quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->quota_limit_type);
quotav[3].iov_len = sizeof(limit->quota_limit_type);
quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->bytes_in_avail);
quotav[4].iov_len = sizeof(limit->bytes_in_avail);
quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->bytes_out_avail);
quotav[5].iov_len = sizeof(limit->bytes_out_avail);
quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->bytes_xfer_avail);
quotav[6].iov_len = sizeof(limit->bytes_xfer_avail);
quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->files_in_avail);
quotav[7].iov_len = sizeof(limit->files_in_avail);
quotav[8].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->files_out_avail);
quotav[8].iov_len = sizeof(limit->files_out_avail);
quotav[9].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->files_xfer_avail);
quotav[9].iov_len = sizeof(limit->files_xfer_avail);
while ((res = readv(filetab->tab_handle, quotav, 10)) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
if (res > 0) {
/* Always rewind after reading a record. */
if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
quotatab_log("error rewinding to start of limit entry: %s",
strerror(errno));
return -1;
}
} else if (res == 0) {
/* Assume end-of-file. */
errno = EOF;
res = -1;
}
return res;
}
/* default */
errno = EINVAL;
return -1;
}
static unsigned char filetab_verify(quota_table_t *filetab) {
unsigned int magic = 0L;
/* Make sure we are positioned at the start of the table. */
if (lseek(filetab->tab_handle, (off_t) 0, SEEK_SET) < 0) {
quotatab_log("error seeking to start of table: %s", strerror(errno));
return FALSE;
}
/* Check the header of this table, to make sure it's valid. */
if (read(filetab->tab_handle, &magic, sizeof(magic)) != sizeof(magic)) {
return FALSE;
}
if (magic == filetab->tab_magic) {
return TRUE;
}
return FALSE;
}
static int filetab_write(quota_table_t *filetab, void *ptr) {
int res = -1;
struct iovec quotav[8];
quota_tally_t *tally = ptr;
/* Mark the current file position. */
off_t current_pos = lseek(filetab->tab_handle, (off_t) 0, SEEK_CUR);
if (current_pos < 0) {
return -1;
}
/* Use writev() to make this more efficient. It is done piecewise, rather
* than doing a normal write(2) directly from the struct pointer, to avoid
* alignment/padding issues.
*/
quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE tally->name;
quotav[0].iov_len = sizeof(tally->name);
quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->quota_type);
quotav[1].iov_len = sizeof(tally->quota_type);
quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_in_used);
quotav[2].iov_len = sizeof(tally->bytes_in_used);
quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_out_used);
quotav[3].iov_len = sizeof(tally->bytes_out_used);
quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_xfer_used);
quotav[4].iov_len = sizeof(tally->bytes_xfer_used);
quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_in_used);
quotav[5].iov_len = sizeof(tally->files_in_used);
quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_out_used);
quotav[6].iov_len = sizeof(tally->files_out_used);
quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_xfer_used);
quotav[7].iov_len = sizeof(tally->files_xfer_used);
while ((res = writev(filetab->tab_handle, quotav, 8)) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
if (res > 0) {
/* Rewind to the start of the entry. */
if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
quotatab_log("error rewinding to start of tally entry: %s",
strerror(errno));
return -1;
}
} else if (res == 0) {
/* If no bytes were written, it's an error. */
quotatab_log("error: writev(2) returned zero when updating tally entry, "
"returning EPERM");
errno = EPERM;
res = -1;
}
return res;
}
static int filetab_rlock(quota_table_t *filetab) {
filetab->tab_lock.l_type = F_RDLCK;
return fcntl(filetab->tab_handle, F_SETLK, &filetab->tab_lock);
}
static int filetab_unlock(quota_table_t *filetab) {
filetab->tab_lock.l_type = F_UNLCK;
return fcntl(filetab->tab_handle, F_SETLK, &filetab->tab_lock);
}
static int filetab_wlock(quota_table_t *filetab) {
filetab->tab_lock.l_type = F_WRLCK;
return fcntl(filetab->tab_handle, F_SETLK, &filetab->tab_lock);
}
static quota_table_t *filetab_open(pool *parent_pool,
quota_tabtype_t tab_type, const char *srcinfo) {
quota_table_t *tab = NULL;
pool *tab_pool = make_sub_pool(parent_pool);
tab = (quota_table_t *) pcalloc(tab_pool, sizeof(quota_table_t));
tab->tab_pool = tab_pool;
tab->tab_type = tab_type;
if (tab->tab_type == TYPE_TALLY) {
/* File-based tally table magic number */
tab->tab_magic = 0x07644;
/* File-based tally record length, manually defined to avoid alignment/
* padding issues when using sizeof().
*/
tab->tab_quotalen = 121;
tab->tab_lock.l_whence = SEEK_CUR;
tab->tab_lock.l_start = 0;
tab->tab_lock.l_len = tab->tab_quotalen;
/* Open the table handle */
if ((tab->tab_handle = open(srcinfo, O_RDWR)) < 0) {
destroy_pool(tab->tab_pool);
return NULL;
}
} else if (tab->tab_type == TYPE_LIMIT) {
/* File-based limit table magic number */
tab->tab_magic = 0x07626;
/* File-based limit record length, manually defined to avoid alignment/
* padding issues when using sizeof().
*/
tab->tab_quotalen = 126;
tab->tab_lock.l_whence = SEEK_CUR;
tab->tab_lock.l_start = 0;
tab->tab_lock.l_len = tab->tab_quotalen;
/* Open the table handle */
if ((tab->tab_handle = open(srcinfo, O_RDONLY)) < 0) {
destroy_pool(tab->tab_pool);
return NULL;
}
}
/* Set all the necessary function pointers. */
tab->tab_close = filetab_close;
tab->tab_create = filetab_create;
tab->tab_lookup = filetab_lookup;
tab->tab_read = filetab_read;
tab->tab_verify = filetab_verify;
tab->tab_write = filetab_write;
tab->tab_rlock = filetab_rlock;
tab->tab_unlock = filetab_unlock;
tab->tab_wlock = filetab_wlock;
return tab;
}
/* Event handlers
*/
#if defined(PR_SHARED_MODULE)
static void filetab_mod_unload_ev(const void *event_data, void *user_data) {
if (strcmp("mod_quotatab_file.c", (const char *) event_data) == 0) {
pr_event_unregister("atab_file_module, NULL, NULL);
quotatab_unregister_backend("file", QUOTATAB_LIMIT_SRC|QUOTATAB_TALLY_SRC);
}
}
#endif /* PR_SHARED_MODULE */
/* Initialization routines
*/
static int filetab_init(void) {
/* Initialize the quota source objects for type "file".
*/
quotatab_register_backend("file", filetab_open,
QUOTATAB_LIMIT_SRC|QUOTATAB_TALLY_SRC);
#if defined(PR_SHARED_MODULE)
pr_event_register("atab_file_module, "core.module-unload",
filetab_mod_unload_ev, NULL);
#endif /* PR_SHARED_MODULE */
return 0;
}
module quotatab_file_module = {
NULL, NULL,
/* Module API version 2.0 */
0x20,
/* Module name */
"quotatab_file",
/* Module configuration handler table */
NULL,
/* Module command handler table */
NULL,
/* Module authentication handler table */
NULL,
/* Module initialization function */
filetab_init,
/* Module child initialization function */
NULL
};
|