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 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
|
/*
* ProFTPD: mod_delay -- a module for adding arbitrary delays to the FTP
* session lifecycle
*
* Copyright (c) 2004-2011 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 and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* This is mod_delay, contrib software for proftpd 1.2.10 and above.
* For more information contact TJ Saunders <tj@castaglia.org>.
*
* $Id: mod_delay.c,v 1.60 2011/06/05 23:18:23 castaglia Exp $
*/
#include "conf.h"
#include "privs.h"
#define MOD_DELAY_VERSION "mod_delay/0.7"
/* Make sure the version of proftpd is as necessary. */
#if PROFTPD_VERSION_NUMBER < 0x0001021001
# error "ProFTPD 1.2.10rc1 or later required"
#endif
#ifdef HAVE_SYS_MMAN_H
# include <sys/mman.h>
#endif
/* On some platforms, this may not be defined. On AIX, for example, this
* symbol is only defined when _NO_PROTO is defined, and _XOPEN_SOURCE is 500.
* How annoying.
*/
#ifndef MAP_FAILED
# define MAP_FAILED ((void *) -1)
#endif
#if defined(PR_USE_CTRLS)
# include <mod_ctrls.h>
#endif /* PR_USE_CTRLS */
/* Number of values to keep in a row. */
#ifndef DELAY_NVALUES
# define DELAY_NVALUES 256
#endif
/* Fraction of total values that are accepted from a single session. */
#ifndef DELAY_SESS_NVALUES
# define DELAY_SESS_NVALUES 16
#endif
/* The mod_delay tables have separate entries for different protocols;
* the implementation/handling of one protocol means that that protocol can
* have different timings from others. For example, due to the encryption
* overhead, authentication via SSL can take longer than without SSL. Thus
* we need to keep the per-protocol timings separate.
*
* We currently allocate space for three protocols:
*
* ftp
* ftps
* ssh2
*
* If more protocols are supported by proftpd, this this DELAY_NPROTO value
* should be increased accordingly. The delay_table_reset() function will
* also need updating, to include the new protocol, as well.
*/
#define DELAY_NPROTO 3
/* Define an absolute upper limit on the delay values selected, in usecs.
*
* I originally wanted a ceiling of 1 hour, but when converted to usecs, the
* value exceeded LONG_MAX. So instead, just use LONG_MAX (which turns out to
* 35 minutes or so); it's a good enough ceiling, and avoids the compiler
* warnings about integer overflows.
*/
#define DELAY_MAX_DELAY_USECS LONG_MAX
#if defined(PR_USE_CTRLS)
static ctrls_acttab_t delay_acttab[];
#endif /* PR_USE_CTRLS */
extern xaset_t *server_list;
module delay_module;
struct delay_vals_rec {
char dv_proto[16];
unsigned int dv_nvals;
long dv_vals[DELAY_NVALUES];
};
struct delay_rec {
unsigned int d_sid;
char d_addr[80];
unsigned int d_port;
struct delay_vals_rec d_vals[DELAY_NPROTO];
};
struct {
const char *dt_path;
int dt_fd;
off_t dt_size;
void *dt_data;
} delay_tab;
static unsigned int delay_engine = TRUE;
static unsigned int delay_nuser = 0;
static unsigned int delay_npass = 0;
static pool *delay_pool = NULL;
static struct timeval delay_tv;
static void delay_table_reset(void);
#define delay_swap(a, b) \
tmp = (a); \
(a) = (b); \
(b) = tmp;
static const char *trace_channel = "delay";
static long delay_select_k(unsigned long k, array_header *values) {
unsigned long l, ir, tmp = 0;
long *elts = (long *) values->elts;
int nelts = values->nelts;
/* This is from "Numeric Recipes in C", Ch. 8.5, as the select()
* algorithm, an in-place sorting algorithm for finding the Kth
* element in an array, where all elements to the left of K are
* smaller than K, and all elements to the right are larger.
*/
l = 1;
ir = values->nelts - 1;
while (TRUE) {
if (ir <= l+1) {
if (ir == l+1 &&
elts[ir] < elts[l])
delay_swap(elts[l], elts[ir]);
return elts[k];
} else {
unsigned long i, j;
long p;
unsigned long mid = (l + ir) >> 1;
delay_swap(elts[mid], elts[l+1]);
if (elts[l] > elts[ir])
delay_swap(elts[l], elts[ir]);
if (elts[l+1] > elts[ir])
delay_swap(elts[l+1], elts[ir]);
if (elts[l] > elts[l+1])
delay_swap(elts[l], elts[l+1]);
i = l + 1;
j = ir;
p = elts[l+1];
while (TRUE) {
do i++;
while (i < nelts && elts[i] < p);
do j--;
while (j >= 0 && elts[j] > p);
if (j < i)
break;
delay_swap(elts[i], elts[j]);
}
elts[l+1] = elts[j];
elts[j] = p;
if (p >= k)
ir = j - 1;
if (p <= k)
l = i;
if (l >= (nelts - 1) ||
ir >= nelts)
break;
}
}
return -1;
}
static long delay_get_median(pool *p, unsigned int rownum, const char *protocol,
long interval) {
register unsigned int i;
struct delay_rec *row;
struct delay_vals_rec *dv = NULL;
long *tab_vals = NULL, median;
array_header *list = make_array(p, 1, sizeof(long));
/* Calculate the median value of the current command's recorded values,
* taking the protocol (e.g. "ftp", "ftps", "ssh2") into account.
*
* When calculating the median, we use the current interval as well
* as the recorded intervals in the table, giving us an odd number of
* values.
*
* If the number of values in this row is less than the watermark
* value, we'll actually return the maximum value, rather than the
* median. Below the watermark, the server is "cold" and has not
* yet accumulated enough data to make the median a useful value.
*/
row = &((struct delay_rec *) delay_tab.dt_data)[rownum];
/* Find the list of delay values that match the given protocol. */
for (i = 0; i < DELAY_NPROTO; i++) {
dv = &(row->d_vals[i]);
if (strcmp(dv->dv_proto, protocol) == 0) {
tab_vals = dv->dv_vals;
break;
}
}
/* Start at the end of the row and work backward, as values are
* always added at the end of the row, shifting everything to the left.
*/
if (tab_vals != NULL) {
for (i = 1; i < dv->dv_nvals; i++) {
/* Ignore any possible garbage (i.e. negative) values in the
* DelayTable.
*/
if (tab_vals[DELAY_NVALUES - 1 - i] >= 0) {
*((long *) push_array(list)) = tab_vals[DELAY_NVALUES - 1 - i];
}
}
}
*((long *) push_array(list)) = interval;
pr_trace_msg(trace_channel, 6, "selecting median interval from %d %s",
list->nelts, list->nelts != 1 ? "values" : "value");
median = delay_select_k(((list->nelts + 1) / 2), list);
if (median >= 0) {
/* Enforce an additional restriction: no delays over a hard limit. */
if (median >= DELAY_MAX_DELAY_USECS) {
pr_trace_msg(trace_channel, 1,
"selected median (%ld usecs) exceeds max delay (%ld usecs), ignoring",
median, (long) DELAY_MAX_DELAY_USECS);
pr_log_debug(DEBUG5, MOD_DELAY_VERSION
": selected median (%ld usecs) exceeds max delay (%ld usecs), ignoring",
median, (long) DELAY_MAX_DELAY_USECS);
median = -1;
} else {
pr_trace_msg(trace_channel, 7, "selected median interval of %ld usecs",
median);
}
}
return median;
}
static void delay_mask_signals(unsigned char block) {
static sigset_t mask_sigset;
if (block) {
sigemptyset(&mask_sigset);
sigaddset(&mask_sigset, SIGCHLD);
sigaddset(&mask_sigset, SIGUSR1);
sigaddset(&mask_sigset, SIGINT);
sigaddset(&mask_sigset, SIGQUIT);
#ifdef SIGIO
sigaddset(&mask_sigset, SIGIO);
#endif
#ifdef SIGBUS
sigaddset(&mask_sigset, SIGBUS);
#endif
sigaddset(&mask_sigset, SIGHUP);
sigprocmask(SIG_BLOCK, &mask_sigset, NULL);
} else {
sigprocmask(SIG_UNBLOCK, &mask_sigset, NULL);
}
}
static void delay_signals_block(void) {
delay_mask_signals(TRUE);
}
static void delay_signals_unblock(void) {
delay_mask_signals(FALSE);
}
static void delay_delay(long interval) {
struct timeval tv;
long rand_usec;
int res, xerrno;
/* Add an additional delay of a random number of usecs, with a
* maximum of half of the given interval.
*/
rand_usec = ((interval / 2.0) * rand()) / RAND_MAX;
interval += rand_usec;
pr_trace_msg(trace_channel, 8, "additional random delay of %ld usecs added",
(long int) rand_usec);
tv.tv_sec = interval / 1000000;
tv.tv_usec = interval % 1000000;
pr_trace_msg(trace_channel, 8, "delaying for %ld usecs",
(long int) ((tv.tv_sec * 1000000) + tv.tv_usec));
delay_signals_block();
res = select(0, NULL, NULL, NULL, &tv);
xerrno = errno;
delay_signals_unblock();
if (res < 0 &&
xerrno == EINTR) {
/* If we were interrupted, handle the interrupting signal. */
pr_signals_handle();
}
}
/* There are two rows (USER and PASS) for each server ID (SID).
*
* The main server has a SID of 1. Thus to access the USER row for SID 1,
* the row index is 0; the PASS row for SID 1 has a row index of 1.
*
* The general formula for the USER row of a given SID is:
*
* r = (sid * 2) - 2;
*
* and thus for accessing the PASS row, the formula is:
*
* r = (sid * 2) - 1;
*/
static unsigned int delay_get_user_rownum(unsigned int sid) {
unsigned int r;
r = (sid * 2) - 2;
return r;
}
static unsigned int delay_get_pass_rownum(unsigned int sid) {
unsigned int r;
r = (sid * 2) - 1;
return r;
}
static void delay_table_add_interval(unsigned int rownum, const char *protocol,
long interval) {
register unsigned int i;
struct delay_rec *row;
struct delay_vals_rec *dv = NULL;
row = &((struct delay_rec *) delay_tab.dt_data)[rownum];
for (i = 0; i < DELAY_NPROTO; i++) {
dv = &(row->d_vals[i]);
if (strcmp(dv->dv_proto, protocol) == 0) {
break;
}
}
/* Shift all existing values to the left one position. */
memmove(&(dv->dv_vals[0]), &(dv->dv_vals[1]),
sizeof(long) * (DELAY_NVALUES - 1));
/* Add the given value to the end. */
dv->dv_vals[DELAY_NVALUES-1] = interval;
if (dv->dv_nvals < DELAY_NVALUES) {
dv->dv_nvals++;
}
}
static int delay_table_init(void) {
pr_fh_t *fh;
struct stat st;
server_rec *s;
unsigned int nservers = 0;
off_t tab_size;
int flags = O_RDWR|O_CREAT;
int reset_table = FALSE, xerrno = 0;
/* We only want to create the table if it does not already exist.
*
* If the ServerType is inetd, we want to leave the current contents
* alone (don't we want to check to see that it's appropriate, sid-wise,
* for the current server_list?), otherwse, we reset the table.
*
* The size of the table should be:
*
* number of vhosts * 2 * row size
*/
for (s = (server_rec *) server_list->xas_list; s; s = s->next)
nservers++;
tab_size = nservers * 2 * sizeof(struct delay_rec);
PRIVS_ROOT
fh = pr_fsio_open(delay_tab.dt_path, flags);
if (fh == NULL) {
xerrno = errno;
}
PRIVS_RELINQUISH
if (fh == NULL) {
pr_log_debug(DEBUG0, MOD_DELAY_VERSION
": error opening DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1, "error opening DelayTable '%s': %s",
delay_tab.dt_path, strerror(xerrno));
errno = xerrno;
return -1;
}
if (pr_fsio_fstat(fh, &st) < 0) {
xerrno = errno;
pr_trace_msg(trace_channel, 1, "error stat'ing DelayTable '%s': %s",
delay_tab.dt_path, strerror(xerrno));
errno = xerrno;
return -1;
}
if (st.st_size != tab_size) {
/* This check is for cases when the ServerType is inetd, and the
* current DelayTable has the wrong size, which can happen if the
* configuration has changed by having vhosts added or removed.
*/
pr_trace_msg(trace_channel, 3,
"expected table size %" PR_LU ", found %" PR_LU ", resetting table",
(pr_off_t) tab_size, (pr_off_t) st.st_size);
reset_table = TRUE;
}
if (reset_table) {
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
pr_trace_msg(trace_channel, 8, "write-locking DelayTable '%s'",
fh->fh_path);
while (fcntl(fh->fh_fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
} else {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to obtain write lock on DelayTable '%s': %s",
fh->fh_path, strerror(errno));
pr_trace_msg(trace_channel, 1,
"unable to obtain write lock on DelayTable '%s': %s", fh->fh_path,
strerror(errno));
pr_fsio_close(fh);
return -1;
}
}
/* Seek to the desired table size (actually, one byte less than the
* desired size) and write a single byte, so that there's enough
* allocated backing store on the filesystem to support the ensuing
* mmap() call.
*/
if (lseek(fh->fh_fd, tab_size-1, SEEK_SET) < 0) {
xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error seeking to %lu in DelayTable '%s': %s",
(unsigned long) tab_size-1, fh->fh_path, strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"error seeking to %lu in DelayTable '%s': %s",
(unsigned long) tab_size-1, fh->fh_path, strerror(xerrno));
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
if (write(fh->fh_fd, "", 1) != 1) {
xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error writing single byte to DelayTable '%s': %s", fh->fh_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"error writing single byte to DelayTable '%s': %s", fh->fh_path,
strerror(xerrno));
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
/* Truncate the table, in case we're shrinking an existing table. */
pr_fsio_ftruncate(fh, tab_size);
lock.l_type = F_UNLCK;
pr_trace_msg(trace_channel, 8, "unlocking DelayTable '%s'", fh->fh_path);
fcntl(fh->fh_fd, F_SETLK, &lock);
}
delay_tab.dt_fd = fh->fh_fd;
delay_tab.dt_size = tab_size;
pr_trace_msg(trace_channel, 8, "mapping DelayTable '%s' (%" PR_LU
" bytes, fd %d) into memory", fh->fh_path, (pr_off_t) delay_tab.dt_size,
delay_tab.dt_fd);
delay_tab.dt_data = mmap(NULL, delay_tab.dt_size, PROT_READ|PROT_WRITE,
MAP_SHARED, delay_tab.dt_fd, 0);
if (delay_tab.dt_data == MAP_FAILED) {
xerrno = errno;
delay_tab.dt_data = NULL;
pr_log_pri(PR_LOG_ERR, MOD_DELAY_VERSION
": error mapping DelayTable '%s' into memory: %s", delay_tab.dt_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"error mapping DelayTable '%s' into memory: %s", delay_tab.dt_path,
strerror(xerrno));
pr_fsio_close(fh);
delay_tab.dt_fd = -1;
errno = xerrno;
return -1;
}
if (!reset_table) {
struct delay_rec *row;
for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
unsigned int r;
/* Row for USER values */
r = delay_get_user_rownum(s->sid);
row = &((struct delay_rec *) delay_tab.dt_data)[r];
if (strcmp(pr_netaddr_get_ipstr(s->addr), row->d_addr) != 0) {
reset_table = TRUE;
break;
}
if (s->ServerPort != row->d_port) {
reset_table = TRUE;
break;
}
/* Row for PASS values */
r = delay_get_pass_rownum(s->sid);
row = &((struct delay_rec *) delay_tab.dt_data)[r];
if (strcmp(pr_netaddr_get_ipstr(s->addr), row->d_addr) != 0) {
reset_table = TRUE;
break;
}
if (s->ServerPort != row->d_port) {
reset_table = TRUE;
break;
}
}
}
if (reset_table) {
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
pr_trace_msg(trace_channel, 8, "write-locking DelayTable '%s'",
fh->fh_path);
while (fcntl(fh->fh_fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
} else {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to obtain write lock on DelayTable '%s': %s",
fh->fh_path, strerror(errno));
pr_trace_msg(trace_channel, 1,
"unable to obtain write lock on DelayTable '%s': %s", fh->fh_path,
strerror(errno));
pr_fsio_close(fh);
return -1;
}
}
/* Seek to the desired table size (actually, one byte less than the
* desired size) and write a single byte, so that there's enough
* allocated backing store on the filesystem to support the ensuing
* mmap() call.
*/
if (lseek(fh->fh_fd, tab_size-1, SEEK_SET) < 0) {
xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error seeking to %lu in DelayTable '%s': %s",
(unsigned long) tab_size-1, fh->fh_path, strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"error seeking to %lu in DelayTable '%s': %s",
(unsigned long) tab_size-1, fh->fh_path, strerror(xerrno));
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
if (write(fh->fh_fd, "", 1) != 1) {
xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error writing single byte to DelayTable '%s': %s", fh->fh_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"error writing single byte to DelayTable '%s': %s", fh->fh_path,
strerror(xerrno));
pr_fsio_close(fh);
errno = xerrno;
return -1;
}
/* Truncate the table, in case we're shrinking an existing table. */
pr_fsio_ftruncate(fh, tab_size);
pr_trace_msg(trace_channel, 6, "resetting DelayTable '%s'",
delay_tab.dt_path);
delay_table_reset();
lock.l_type = F_UNLCK;
pr_trace_msg(trace_channel, 8, "unlocking DelayTable '%s'", fh->fh_path);
fcntl(fh->fh_fd, F_SETLK, &lock);
}
/* Done */
pr_trace_msg(trace_channel, 8, "unmapping DelayTable '%s' from memory",
delay_tab.dt_path);
if (munmap(delay_tab.dt_data, delay_tab.dt_size) < 0) {
xerrno = errno;
pr_fsio_close(fh);
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error unmapping DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1, "error unmapping DelayTable '%s': %s",
delay_tab.dt_path, strerror(xerrno));
errno = xerrno;
return -1;
}
delay_tab.dt_data = NULL;
delay_tab.dt_fd = -1;
if (pr_fsio_close(fh) < 0) {
xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error closing DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1, "error closing DelayTable '%s': %s",
delay_tab.dt_path, strerror(xerrno));
errno = xerrno;
return -1;
}
return 0;
}
static int delay_table_load(int lock_table) {
struct flock lock;
if (lock_table) {
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
pr_trace_msg(trace_channel, 8, "write-locking DelayTable '%s'",
delay_tab.dt_path);
while (fcntl(delay_tab.dt_fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
}
if (delay_tab.dt_data == NULL) {
pr_trace_msg(trace_channel, 8, "mapping DelayTable '%s' (%" PR_LU
" bytes, fd %d) into memory", delay_tab.dt_path,
(pr_off_t) delay_tab.dt_size, delay_tab.dt_fd);
delay_tab.dt_data = mmap(NULL, delay_tab.dt_size, PROT_READ|PROT_WRITE,
MAP_SHARED, delay_tab.dt_fd, 0);
if (delay_tab.dt_data == MAP_FAILED) {
int xerrno = errno;
delay_tab.dt_data = NULL;
if (lock_table) {
/* Make sure we release the lock before returning. */
lock.l_type = F_UNLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
pr_trace_msg(trace_channel, 8, "unlocking DelayTable '%s'",
delay_tab.dt_path);
while (fcntl(delay_tab.dt_fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
}
}
errno = xerrno;
return -1;
}
}
return 0;
}
static void delay_table_reset(void) {
server_rec *s;
for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
unsigned int r;
struct delay_rec *row;
struct delay_vals_rec *dv;
/* Row for USER values */
r = delay_get_user_rownum(s->sid);
row = &((struct delay_rec *) delay_tab.dt_data)[r];
row->d_sid = s->sid;
sstrncpy(row->d_addr, pr_netaddr_get_ipstr(s->addr), sizeof(row->d_addr));
row->d_port = s->ServerPort;
memset(row->d_vals, 0, sizeof(row->d_vals));
/* Initialize value subsets for "ftp", "ftps", and "ssh2". */
dv = &(row->d_vals[0]);
memset(dv->dv_proto, 0, sizeof(dv->dv_proto));
sstrcat(dv->dv_proto, "ftp", sizeof(dv->dv_proto));
dv->dv_nvals = 0;
memset(dv->dv_vals, -1, sizeof(dv->dv_vals));
dv = &(row->d_vals[1]);
memset(dv->dv_proto, 0, sizeof(dv->dv_proto));
sstrcat(dv->dv_proto, "ftps", sizeof(dv->dv_proto));
dv->dv_nvals = 0;
memset(dv->dv_vals, -1, sizeof(dv->dv_vals));
dv = &(row->d_vals[2]);
memset(dv->dv_proto, 0, sizeof(dv->dv_proto));
sstrcat(dv->dv_proto, "ssh2", sizeof(dv->dv_proto));
dv->dv_nvals = 0;
memset(dv->dv_vals, -1, sizeof(dv->dv_vals));
/* Row for PASS values */
r = delay_get_pass_rownum(s->sid);
row = &((struct delay_rec *) delay_tab.dt_data)[r];
row->d_sid = s->sid;
sstrncpy(row->d_addr, pr_netaddr_get_ipstr(s->addr), sizeof(row->d_addr));
row->d_port = s->ServerPort;
memset(row->d_vals, 0, sizeof(row->d_vals));
dv = &(row->d_vals[0]);
memset(dv->dv_proto, 0, sizeof(dv->dv_proto));
sstrcat(dv->dv_proto, "ftp", sizeof(dv->dv_proto));
dv->dv_nvals = 0;
memset(dv->dv_vals, -1, sizeof(dv->dv_vals));
dv = &(row->d_vals[1]);
memset(dv->dv_proto, 0, sizeof(dv->dv_proto));
sstrcat(dv->dv_proto, "ftps", sizeof(dv->dv_proto));
dv->dv_nvals = 0;
memset(dv->dv_vals, -1, sizeof(dv->dv_vals));
dv = &(row->d_vals[2]);
memset(dv->dv_proto, 0, sizeof(dv->dv_proto));
sstrcat(dv->dv_proto, "ssh2", sizeof(dv->dv_proto));
dv->dv_nvals = 0;
memset(dv->dv_vals, -1, sizeof(dv->dv_vals));
}
return;
}
static int delay_table_wlock(unsigned int rownum) {
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = sizeof(struct delay_rec) * rownum;
lock.l_len = sizeof(struct delay_rec);
pr_trace_msg(trace_channel, 8, "write-locking DelayTable '%s', row %u",
delay_tab.dt_path, rownum + 1);
while (fcntl(delay_tab.dt_fd, F_SETLKW, &lock) < 0) {
int xerrno = errno;
if (xerrno == EINTR) {
pr_signals_handle();
continue;
}
pr_trace_msg(trace_channel, 1, "error locking row: %s", strerror(xerrno));
errno = xerrno;
return -1;
}
return 0;
}
static int delay_table_unload(int unlock_table) {
if (delay_tab.dt_data) {
pr_trace_msg(trace_channel, 8, "unmapping DelayTable '%s' from memory",
delay_tab.dt_path);
if (munmap(delay_tab.dt_data, delay_tab.dt_size) < 0) {
int xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error unmapping DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1, "error unmapping DelayTable '%s': %s",
delay_tab.dt_path, strerror(xerrno));
errno = xerrno;
return -1;
}
delay_tab.dt_data = NULL;
}
if (unlock_table) {
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
pr_trace_msg(trace_channel, 8, "unlocking DelayTable '%s'",
delay_tab.dt_path);
while (fcntl(delay_tab.dt_fd, F_SETLK, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
}
return 0;
}
static int delay_table_unlock(unsigned int rownum) {
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = 0;
lock.l_start = sizeof(struct delay_rec) * rownum;
lock.l_len = sizeof(struct delay_rec);
pr_trace_msg(trace_channel, 8, "unlocking DelayTable '%s', row %u",
delay_tab.dt_path, rownum + 1);
while (fcntl(delay_tab.dt_fd, F_SETLKW, &lock) < 0) {
int xerrno = errno;
if (xerrno == EINTR) {
pr_signals_handle();
continue;
}
pr_trace_msg(trace_channel, 1, "error unlocking row: %s", strerror(xerrno));
errno = xerrno;
return -1;
}
return 0;
}
#if defined(PR_USE_CTRLS)
/* Control handlers
*/
static int delay_handle_info(pr_ctrls_t *ctrl, int reqargc,
char **reqargv) {
register server_rec *s;
pool *tmp_pool;
pr_fh_t *fh;
char *vals;
int xerrno = 0;
PRIVS_ROOT
fh = pr_fsio_open(delay_tab.dt_path, O_RDWR);
if (fh == NULL) {
xerrno = errno;
}
PRIVS_RELINQUISH
if (fh == NULL) {
pr_ctrls_add_response(ctrl,
"warning: unable to open DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
errno = xerrno;
return -1;
}
delay_tab.dt_fd = fh->fh_fd;
delay_tab.dt_data = NULL;
if (delay_table_load(TRUE) < 0) {
xerrno = errno;
pr_ctrls_add_response(ctrl,
"unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
pr_fsio_close(fh);
delay_tab.dt_fd = -1;
delay_tab.dt_data = NULL;
errno = xerrno;
return -1;
}
tmp_pool = make_sub_pool(delay_pool);
for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
unsigned int r;
register unsigned int i;
struct delay_rec *row;
/* Row for USER values */
r = delay_get_user_rownum(s->sid);
row = &((struct delay_rec *) delay_tab.dt_data)[r];
pr_ctrls_add_response(ctrl, "Address %s#%u: USER values (usecs):",
row->d_addr, row->d_port);
for (i = 0; i < DELAY_NPROTO; i++) {
struct delay_vals_rec *dv;
register unsigned int j;
dv = &(row->d_vals[i]);
if ((dv->dv_proto)[0] == '\0') {
continue;
}
pr_ctrls_add_response(ctrl, " + Protocol %s, %u values:", dv->dv_proto,
dv->dv_nvals);
/* Start at the end of the row and work backward, as values are
* always added at the end of the row, shifting everything to the left.
*/
vals = "";
for (j = 0; j < dv->dv_nvals; j++) {
char buf[80];
memset(buf, '\0', sizeof(buf));
snprintf(buf, sizeof(buf)-1, "%10ld",
dv->dv_vals[DELAY_NVALUES - 1 - j]);
vals = pstrcat(tmp_pool, vals, " ", buf, NULL);
if (j != 0 &&
j % 4 == 0) {
pr_ctrls_add_response(ctrl, " %s", vals);
vals = "";
}
}
if (strlen(vals) > 0)
pr_ctrls_add_response(ctrl, " %s", vals);
}
pr_ctrls_add_response(ctrl, "%s", "");
/* Row for PASS values */
r = delay_get_pass_rownum(s->sid);
row = &((struct delay_rec *) delay_tab.dt_data)[r];
pr_ctrls_add_response(ctrl, "Address %s#%u: PASS values (usecs):",
row->d_addr, row->d_port);
for (i = 0; i < DELAY_NPROTO; i++) {
struct delay_vals_rec *dv;
register unsigned int j;
dv = &(row->d_vals[i]);
if ((dv->dv_proto)[0] == '\0') {
continue;
}
pr_ctrls_add_response(ctrl, " + Protocol %s, %u values:", dv->dv_proto,
dv->dv_nvals);
vals = "";
for (j = 0; j < dv->dv_nvals; j++) {
char buf[80];
memset(buf, '\0', sizeof(buf));
snprintf(buf, sizeof(buf)-1, "%10ld",
dv->dv_vals[DELAY_NVALUES - 1 - j]);
vals = pstrcat(tmp_pool, vals, " ", buf, NULL);
if (j != 0 &&
j % 4 == 0) {
pr_ctrls_add_response(ctrl, " %s", vals);
vals = "";
}
}
if (strlen(vals) > 0)
pr_ctrls_add_response(ctrl, " %s", vals);
}
pr_ctrls_add_response(ctrl, "%s", "");
}
if (delay_table_unload(TRUE) < 0) {
pr_ctrls_add_response(ctrl,
"unable to unload DelayTable '%s' from memory: %s",
delay_tab.dt_path, strerror(errno));
}
pr_fsio_close(fh);
delay_tab.dt_fd = -1;
delay_tab.dt_data = NULL;
destroy_pool(tmp_pool);
return 0;
}
static int delay_handle_reset(pr_ctrls_t *ctrl, int reqargc,
char **reqarg) {
struct flock lock;
pr_fh_t *fh;
int xerrno = 0;
PRIVS_ROOT
fh = pr_fsio_open(delay_tab.dt_path, O_RDWR);
if (fh == NULL) {
xerrno = errno;
}
PRIVS_RELINQUISH
if (!fh) {
pr_ctrls_add_response(ctrl,
"unable to open DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
errno = xerrno;
return -1;
}
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
while (fcntl(fh->fh_fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
} else {
pr_ctrls_add_response(ctrl,
"unable to obtain write lock on DelayTable '%s': %s",
fh->fh_path, strerror(errno));
pr_fsio_close(fh);
return -1;
}
}
if (pr_fsio_ftruncate(fh, 0) < 0) {
pr_ctrls_add_response(ctrl,
"error truncating DelayTable '%s': %s", fh->fh_path, strerror(errno));
pr_fsio_close(fh);
return -1;
}
lock.l_type = F_UNLCK;
fcntl(fh->fh_fd, F_SETLK, &lock);
if (pr_fsio_close(fh) < 0) {
pr_ctrls_add_response(ctrl,
"error closing DelayTable '%s': %s", delay_tab.dt_path,
strerror(errno));
return -1;
}
pr_ctrls_add_response(ctrl, "DelayTable '%s' reset", delay_tab.dt_path);
return 0;
}
static int delay_handle_delay(pr_ctrls_t *ctrl, int reqargc,
char **reqargv) {
if (reqargc == 0 ||
reqargv == NULL) {
pr_ctrls_add_response(ctrl, "delay: missing required parameters");
return -1;
}
if (strcmp(reqargv[0], "info") == 0) {
if (!pr_ctrls_check_acl(ctrl, delay_acttab, "info")) {
pr_ctrls_add_response(ctrl, "access denied");
return -1;
}
return delay_handle_info(ctrl, --reqargc, ++reqargv);
} else if (strcmp(reqargv[0], "reset") == 0) {
if (!pr_ctrls_check_acl(ctrl, delay_acttab, "reset")) {
pr_ctrls_add_response(ctrl, "access denied");
return -1;
}
return delay_handle_reset(ctrl, --reqargc, ++reqargv);
}
pr_ctrls_add_response(ctrl, "unknown delay action: '%s'", reqargv[0]);
return -1;
}
#endif /* PR_USE_CTRLS */
/* Configuration handlers
*/
/* usage: DelayControlsACLs actions|all allow|deny user|group list */
MODRET set_delayctrlsacls(cmd_rec *cmd) {
#if defined(PR_USE_CTRLS)
char *bad_action = NULL, **actions = NULL;
CHECK_ARGS(cmd, 4);
CHECK_CONF(cmd, CONF_ROOT);
actions = ctrls_parse_acl(cmd->tmp_pool, cmd->argv[1]);
/* Check the second parameter to make sure it is "allow" or "deny" */
if (strcmp(cmd->argv[2], "allow") != 0 &&
strcmp(cmd->argv[2], "deny") != 0)
CONF_ERROR(cmd, "second parameter must be 'allow' or 'deny'");
/* Check the third parameter to make sure it is "user" or "group" */
if (strcmp(cmd->argv[3], "user") != 0 &&
strcmp(cmd->argv[3], "group") != 0)
CONF_ERROR(cmd, "third parameter must be 'user' or 'group'");
bad_action = pr_ctrls_set_module_acls(delay_acttab, delay_pool, actions,
cmd->argv[2], cmd->argv[3], cmd->argv[4]);
if (bad_action != NULL)
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown delay action: '",
bad_action, "'", NULL));
return PR_HANDLED(cmd);
#else
CONF_ERROR(cmd, "requires Controls support (--enable-ctrls)")
#endif /* PR_USE_CTRLS */
}
/* usage: DelayEngine on|off */
MODRET set_delayengine(cmd_rec *cmd) {
config_rec *c;
int bool;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
bool = get_boolean(cmd, 1);
if (bool == -1)
CONF_ERROR(cmd, "expected Boolean parameter");
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
*((unsigned int *) c->argv[0]) = bool;
return PR_HANDLED(cmd);
}
/* usage: DelayTable path */
MODRET set_delaytable(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT);
if (pr_fs_valid_path(cmd->argv[1]) < 0)
CONF_ERROR(cmd, "must be an absolute path");
add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* Command handlers
*/
MODRET delay_post_pass(cmd_rec *cmd) {
struct timeval tv;
unsigned int rownum;
long interval, median;
const char *proto;
unsigned char *authenticated;
if (!delay_engine)
return PR_DECLINED(cmd);
/* Has the client already authenticated? */
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
if (authenticated != NULL &&
*authenticated == TRUE) {
return PR_DECLINED(cmd);
}
rownum = delay_get_pass_rownum(main_server->sid);
/* Prepare for manipulating the table. */
if (delay_table_load(FALSE) < 0) {
int xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
errno = xerrno;
return PR_DECLINED(cmd);
}
gettimeofday(&tv, NULL);
delay_table_wlock(rownum);
interval = (tv.tv_sec - delay_tv.tv_sec) * 1000000 +
(tv.tv_usec - delay_tv.tv_usec);
pr_trace_msg(trace_channel, 9,
"interval between USER and PASS commands: %ld usecs", interval);
proto = pr_session_get_protocol(0);
/* Get the median interval value. */
median = delay_get_median(cmd->tmp_pool, rownum, proto, interval);
/* Add the interval to the table. Only allow a single session to
* add a portion of the cache size, to prevent a single client from
* poisoning the cache.
*/
if (delay_npass < (DELAY_NVALUES / DELAY_SESS_NVALUES)) {
pr_trace_msg(trace_channel, 8, "adding %ld usecs to PASS row", interval);
delay_table_add_interval(rownum, proto, interval);
delay_npass++;
} else {
/* Generate an event, in case a module (i.e. mod_ban) might want
* to do something appropriate to such an ill-behaved client.
*/
pr_event_generate("mod_delay.max-pass", session.c);
}
/* Done with the table. */
delay_table_unlock(rownum);
if (delay_table_unload(FALSE) < 0) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to unload DelayTable '%s' from memory: %s",
delay_tab.dt_path, strerror(errno));
}
/* If this is a POST_CMD phase, then close the table. If the phase is
* POST_CMD_ERR, then leave the table open; the client may send another
* set of USER/PASS commands.
*/
if (session.curr_phase == POST_CMD) {
(void) close(delay_tab.dt_fd);
delay_tab.dt_fd = -1;
}
/* If the current interval is less than the median interval (and a valid
* median interval was selected), we need to delay ourselves a little.
*/
if (median >= 0) {
if (interval < median) {
pr_trace_msg(trace_channel, 9,
"interval (%ld usecs) less than selected median (%ld usecs), delaying",
interval, median);
delay_delay(median - interval);
}
} else {
pr_trace_msg(trace_channel, 9,
"invalid median value (%ld usecs) selected, ignoring", median);
}
return PR_DECLINED(cmd);
}
MODRET delay_pre_pass(cmd_rec *cmd) {
if (!delay_engine)
return PR_DECLINED(cmd);
gettimeofday(&delay_tv, NULL);
return PR_DECLINED(cmd);
}
MODRET delay_post_user(cmd_rec *cmd) {
struct timeval tv;
unsigned int rownum;
long interval, median;
const char *proto;
unsigned char *authenticated;
if (!delay_engine)
return PR_DECLINED(cmd);
/* Has the client already authenticated? */
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
if (authenticated != NULL &&
*authenticated == TRUE) {
return PR_DECLINED(cmd);
}
rownum = delay_get_user_rownum(main_server->sid);
/* Prepare for manipulating the table. */
if (delay_table_load(FALSE) < 0) {
int xerrno = errno;
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
errno = xerrno;
return PR_DECLINED(cmd);
}
gettimeofday(&tv, NULL);
delay_table_wlock(rownum);
interval = (tv.tv_sec - delay_tv.tv_sec) * 1000000 +
(tv.tv_usec - delay_tv.tv_usec);
pr_trace_msg(trace_channel, 9,
"interval between connect and USER command: %ld usecs", interval);
proto = pr_session_get_protocol(0);
/* Get the median interval value. */
median = delay_get_median(cmd->tmp_pool, rownum, proto, interval);
/* Add the interval to the table. Only allow a single session to
* add a portion of the cache size, to prevent a single client from
* poisoning the cache.
*/
if (delay_nuser < (DELAY_NVALUES / DELAY_SESS_NVALUES)) {
pr_trace_msg(trace_channel, 8, "adding %ld usecs to USER row", interval);
delay_table_add_interval(rownum, proto, interval);
delay_nuser++;
} else {
/* Generate an event, in case a module (i.e. mod_ban) might want
* to do something appropriate to such an ill-behaved client.
*/
pr_event_generate("mod_delay.max-user", session.c);
}
/* Done with the table. */
delay_table_unlock(rownum);
if (delay_table_unload(FALSE) < 0) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to unload DelayTable '%s' from memory: %s",
delay_tab.dt_path, strerror(errno));
}
/* If the current interval is less than the median interval (and a valid
* median interval was selected), we need to delay ourselves a little.
*/
if (median >= 0) {
if (interval < median) {
pr_trace_msg(trace_channel, 9,
"interval (%ld usecs) less than selected median (%ld usecs), delaying",
interval, median);
delay_delay(median - interval);
}
} else {
pr_trace_msg(trace_channel, 9,
"invalid median value (%ld usecs) selected, ignoring", median);
}
return PR_DECLINED(cmd);
}
MODRET delay_pre_user(cmd_rec *cmd) {
if (!delay_engine)
return PR_DECLINED(cmd);
gettimeofday(&delay_tv, NULL);
return PR_DECLINED(cmd);
}
/* Event handlers
*/
#if defined(PR_SHARED_MODULE)
static void delay_mod_unload_ev(const void *event_data, void *user_data) {
if (strcmp("mod_delay.c", (const char *) event_data) == 0) {
/* Unregister ourselves from all events. */
pr_event_unregister(&delay_module, NULL, NULL);
# ifdef PR_USE_CTRLS
pr_ctrls_unregister(&delay_module, "delay");
# endif
}
}
#endif
static void delay_postparse_ev(const void *event_data, void *user_data) {
config_rec *c;
c = find_config(main_server->conf, CONF_PARAM, "DelayEngine", FALSE);
if (c && *((unsigned int *) c->argv[0]) == FALSE)
delay_engine = FALSE;
if (!delay_engine)
return;
c = find_config(main_server->conf, CONF_PARAM, "DelayTable", FALSE);
if (c)
delay_tab.dt_path = c->argv[0];
(void) delay_table_init();
return;
}
static void delay_restart_ev(const void *event_data, void *user_data) {
#if defined(PR_USE_CTRLS)
register unsigned int i;
#endif /* PR_USE_CTRLS */
if (delay_pool)
destroy_pool(delay_pool);
delay_pool = make_sub_pool(permanent_pool);
pr_pool_tag(delay_pool, MOD_DELAY_VERSION);
#if defined(PR_USE_CTRLS)
for (i = 0; delay_acttab[i].act_action; i++) {
delay_acttab[i].act_acl = pcalloc(delay_pool, sizeof(ctrls_acl_t));
pr_ctrls_init_acl(delay_acttab[i].act_acl);
}
#endif /* PR_USE_CTRLS */
return;
}
static void delay_shutdown_ev(const void *event_data, void *user_data) {
pr_fh_t *fh;
char *data;
size_t datalen;
int xerrno = 0;
if (!delay_engine)
return;
/* Write out the DelayTable to the filesystem, thus updating the
* file metadata.
*/
PRIVS_ROOT
fh = pr_fsio_open(delay_tab.dt_path, O_RDWR);
if (fh == NULL) {
xerrno = errno;
}
PRIVS_RELINQUISH
if (fh == NULL) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to open DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
errno = xerrno;
return;
}
delay_tab.dt_fd = fh->fh_fd;
delay_tab.dt_data = NULL;
if (delay_table_load(TRUE) < 0) {
xerrno = errno;
pr_fsio_close(fh);
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
pr_trace_msg(trace_channel, 1,
"unable to load DelayTable '%s' (fd %d) into memory: %s",
delay_tab.dt_path, delay_tab.dt_fd, strerror(xerrno));
errno = xerrno;
return;
}
datalen = delay_tab.dt_size;
data = palloc(delay_pool, datalen);
memcpy(data, delay_tab.dt_data, datalen);
if (delay_table_unload(TRUE) < 0) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error unloading DelayTable '%s' from memory: %s",
delay_tab.dt_path, strerror(errno));
}
if (pr_fsio_write(fh, data, datalen) < 0) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error updating DelayTable '%s': %s", delay_tab.dt_path,
strerror(errno));
}
delay_tab.dt_fd = -1;
if (pr_fsio_close(fh) < 0) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": error writing DelayTable '%s': %s", delay_tab.dt_path,
strerror(errno));
}
return;
}
/* Initialization functions
*/
static int delay_init(void) {
delay_tab.dt_path = PR_RUN_DIR "/proftpd.delay";
delay_tab.dt_data = NULL;
#if defined(PR_SHARED_MODULE)
pr_event_register(&delay_module, "core.module-unload", delay_mod_unload_ev,
NULL);
#endif
pr_event_register(&delay_module, "core.postparse", delay_postparse_ev, NULL);
pr_event_register(&delay_module, "core.restart", delay_restart_ev, NULL);
pr_event_register(&delay_module, "core.shutdown", delay_shutdown_ev, NULL);
delay_pool = make_sub_pool(permanent_pool);
pr_pool_tag(delay_pool, MOD_DELAY_VERSION);
#if defined(PR_USE_CTRLS)
if (pr_ctrls_register(&delay_module, "delay", "tune mod_delay settings",
delay_handle_delay) < 0) {
pr_log_pri(PR_LOG_INFO, MOD_DELAY_VERSION
": error registering 'delay' control: %s", strerror(errno));
} else {
register unsigned int i;
for (i = 0; delay_acttab[i].act_action; i++) {
delay_acttab[i].act_acl = pcalloc(delay_pool, sizeof(ctrls_acl_t));
pr_ctrls_init_acl(delay_acttab[i].act_acl);
}
}
#endif /* PR_USE_CTRLS */
return 0;
}
static int delay_sess_init(void) {
pr_fh_t *fh;
config_rec *c;
int xerrno = errno;
if (!delay_engine)
return 0;
/* Look up DelayEngine again, as it may have been disabled in an
* <IfClass> section.
*/
c = find_config(main_server->conf, CONF_PARAM, "DelayEngine", FALSE);
if (c && *((unsigned int *) c->argv[0]) == FALSE)
delay_engine = FALSE;
if (!delay_engine)
return 0;
delay_nuser = 0;
delay_npass = 0;
pr_trace_msg(trace_channel, 6, "opening DelayTable '%s'", delay_tab.dt_path);
PRIVS_ROOT
fh = pr_fsio_open(delay_tab.dt_path, O_RDWR);
if (fh == NULL) {
xerrno = errno;
}
PRIVS_RELINQUISH
if (fh == NULL) {
pr_log_pri(PR_LOG_WARNING, MOD_DELAY_VERSION
": unable to open DelayTable '%s': %s", delay_tab.dt_path,
strerror(xerrno));
pr_trace_msg(trace_channel, 1, "unable to open DelayTable '%s': %s",
delay_tab.dt_path, strerror(xerrno));
delay_engine = FALSE;
return 0;
}
delay_tab.dt_fd = fh->fh_fd;
delay_tab.dt_data = NULL;
return 0;
}
/* Module API tables
*/
#if defined(PR_USE_CTRLS)
static ctrls_acttab_t delay_acttab[] = {
{ "info", NULL, NULL, NULL },
{ "reset", NULL, NULL, NULL },
{ NULL, NULL, NULL, NULL }
};
#endif /* PR_USE_CTRLS */
static conftable delay_conftab[] = {
{ "DelayControlsACLs",set_delayctrlsacls, NULL },
{ "DelayEngine", set_delayengine, NULL },
{ "DelayTable", set_delaytable, NULL },
{ NULL }
};
static cmdtable delay_cmdtab[] = {
{ PRE_CMD, C_PASS, G_NONE, delay_pre_pass, FALSE, FALSE },
{ POST_CMD, C_PASS, G_NONE, delay_post_pass, FALSE, FALSE },
{ POST_CMD_ERR, C_PASS, G_NONE, delay_post_pass, FALSE, FALSE },
{ PRE_CMD, C_USER, G_NONE, delay_pre_user, FALSE, FALSE },
{ POST_CMD, C_USER, G_NONE, delay_post_user, FALSE, FALSE },
{ POST_CMD_ERR, C_USER, G_NONE, delay_post_user, FALSE, FALSE },
{ 0, NULL }
};
module delay_module = {
NULL, NULL,
/* Module API version 2.0 */
0x20,
/* Module name */
"delay",
/* Module configuration handler table */
delay_conftab,
/* Module command handler table */
delay_cmdtab,
/* Module authentication handler table */
NULL,
/* Module initialization function */
delay_init,
/* Session initialization function */
delay_sess_init,
/* Module version */
MOD_DELAY_VERSION
};
|