| 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 |
#define MOD_WRAP_VERSION "mod_wrap/1.2.4" |
| 29 |
|
| 30 |
#include "conf.h" |
| 31 |
#include "privs.h" |
| 32 |
#include "tcpd.h" |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
int allow_severity = PR_LOG_INFO; |
| 38 |
int deny_severity = PR_LOG_WARNING; |
| 39 |
|
| 40 |
module wrap_module; |
| 41 |
|
| 42 |
|
| 43 |
static int wrap_eval_expression(char **, array_header *); |
| 44 |
static const char *wrap_get_user_table(cmd_rec *, const char *, char *); |
| 45 |
static int wrap_is_usable_file(const char *); |
| 46 |
static void wrap_log_request_allowed(int, struct request_info *); |
| 47 |
static void wrap_log_request_denied(int, struct request_info *); |
| 48 |
static config_rec *wrap_resolve_user(pool *, const char **); |
| 49 |
static int wrap_sess_init(void); |
| 50 |
|
| 51 |
static char *wrap_service_name = "proftpd"; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static int wrap_eval_expression(char **config_expr, |
| 59 |
array_header *session_expr) { |
| 60 |
char *elem = NULL, **list = NULL; |
| 61 |
|
| 62 |
|
| 63 |
if (!config_expr || !*config_expr || !session_expr) { |
| 64 |
return FALSE; |
| 65 |
} |
| 66 |
|
| 67 |
list = (char **) session_expr->elts; |
| 68 |
|
| 69 |
for (; *config_expr; config_expr++) { |
| 70 |
register unsigned int i; |
| 71 |
unsigned char found; |
| 72 |
|
| 73 |
elem = *config_expr; |
| 74 |
found = FALSE; |
| 75 |
|
| 76 |
if (*elem == '!') { |
| 77 |
found = !found; |
| 78 |
elem++; |
| 79 |
} |
| 80 |
|
| 81 |
for (i = 0; i < session_expr->nelts; i++) { |
| 82 |
if (list[i] && |
| 83 |
strcmp(list[i], elem) == 0) { |
| 84 |
found = !found; |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (!found) { |
| 90 |
config_expr = NULL; |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (config_expr) { |
| 96 |
return TRUE; |
| 97 |
} |
| 98 |
|
| 99 |
return FALSE; |
| 100 |
} |
| 101 |
|
| 102 |
static const char *wrap_get_user_table(cmd_rec *cmd, const char *user, |
| 103 |
char *path) { |
| 104 |
int xerrno = 0; |
| 105 |
|
| 106 |
char *real_path = NULL; |
| 107 |
struct passwd *pw = NULL; |
| 108 |
|
| 109 |
pw = pr_auth_getpwnam(cmd->pool, user); |
| 110 |
|
| 111 |
|
| 112 |
if (pw == NULL) { |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
session.user = pstrdup(cmd->pool, pw->pw_name); |
| 120 |
session.login_uid = pw->pw_uid; |
| 121 |
|
| 122 |
PRIVS_USER |
| 123 |
real_path = dir_realpath(cmd->pool, path); |
| 124 |
xerrno = errno; |
| 125 |
PRIVS_RELINQUISH |
| 126 |
|
| 127 |
if (real_path) { |
| 128 |
path = real_path; |
| 129 |
} |
| 130 |
|
| 131 |
errno = xerrno; |
| 132 |
return path; |
| 133 |
} |
| 134 |
|
| 135 |
static int wrap_is_usable_file(const char *filename) { |
| 136 |
struct stat st; |
| 137 |
pr_fh_t *fh = NULL; |
| 138 |
|
| 139 |
|
| 140 |
if (filename == NULL) { |
| 141 |
return FALSE; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
fh = pr_fsio_open(filename, O_RDONLY); |
| 146 |
if (fh == NULL) { |
| 147 |
int xerrno = errno; |
| 148 |
|
| 149 |
pr_log_pri(PR_LOG_NOTICE, MOD_WRAP_VERSION ": failed to read \"%s\": %s", |
| 150 |
filename, strerror(xerrno)); |
| 151 |
|
| 152 |
errno = xerrno; |
| 153 |
return FALSE; |
| 154 |
} |
| 155 |
|
| 156 |
if (pr_fsio_fstat(fh, &st) < 0) { |
| 157 |
int xerrno = errno; |
| 158 |
|
| 159 |
pr_log_pri(PR_LOG_NOTICE, MOD_WRAP_VERSION ": failed to stat \"%s\": %s", |
| 160 |
filename, strerror(xerrno)); |
| 161 |
|
| 162 |
pr_fsio_close(fh); |
| 163 |
errno = xerrno; |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
if (S_ISDIR(st.st_mode)) { |
| 168 |
int xerrno = EISDIR; |
| 169 |
|
| 170 |
pr_log_pri(PR_LOG_NOTICE, MOD_WRAP_VERSION ": unable to use \"%s\": %s", |
| 171 |
filename, strerror(xerrno)); |
| 172 |
|
| 173 |
pr_fsio_close(fh); |
| 174 |
errno = xerrno; |
| 175 |
return FALSE; |
| 176 |
} |
| 177 |
|
| 178 |
pr_fsio_close(fh); |
| 179 |
return TRUE; |
| 180 |
} |
| 181 |
|
| 182 |
static void wrap_log_request_allowed(int severity, |
| 183 |
struct request_info *request) { |
| 184 |
int priority; |
| 185 |
|
| 186 |
|
| 187 |
priority = (severity & PR_LOG_PRIMASK); |
| 188 |
|
| 189 |
pr_log_pri(priority, MOD_WRAP_VERSION ": allowed connection from %s", |
| 190 |
eval_client(request)); |
| 191 |
|
| 192 |
|
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
static void wrap_log_request_denied(int severity, |
| 197 |
struct request_info *request) { |
| 198 |
int priority; |
| 199 |
|
| 200 |
|
| 201 |
priority = (severity & PR_LOG_PRIMASK); |
| 202 |
|
| 203 |
pr_log_pri(priority, MOD_WRAP_VERSION ": refused connection from %s", |
| 204 |
eval_client(request)); |
| 205 |
|
| 206 |
|
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
static config_rec *wrap_resolve_user(pool *p, const char **user) { |
| 211 |
config_rec *conf = NULL, *top_conf; |
| 212 |
char *ourname = NULL, *anonname = NULL; |
| 213 |
unsigned char is_alias = FALSE, force_anon = FALSE; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
ourname = (char*) get_param_ptr(main_server->conf, "UserName", FALSE); |
| 222 |
|
| 223 |
conf = find_config(main_server->conf, CONF_PARAM, "UserAlias", TRUE); |
| 224 |
|
| 225 |
if (conf) do { |
| 226 |
if (strcmp(conf->argv[0], "*") == 0 || |
| 227 |
strcmp(conf->argv[0], *user) == 0) { |
| 228 |
is_alias = TRUE; |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
} while ((conf = find_config_next(conf, conf->next, CONF_PARAM, |
| 233 |
"UserAlias", TRUE)) != NULL); |
| 234 |
|
| 235 |
|
| 236 |
top_conf = conf; |
| 237 |
|
| 238 |
while (conf && conf->parent && |
| 239 |
find_config(conf->parent->set, CONF_PARAM, "AuthAliasOnly", FALSE)) { |
| 240 |
|
| 241 |
is_alias = FALSE; |
| 242 |
find_config_set_top(top_conf); |
| 243 |
conf = find_config_next(conf, conf->next, CONF_PARAM, "UserAlias", TRUE); |
| 244 |
|
| 245 |
if (conf && |
| 246 |
(strcmp(conf->argv[0], "*") == 0 || |
| 247 |
strcmp(conf->argv[0], *user) == 0)) |
| 248 |
is_alias = TRUE; |
| 249 |
} |
| 250 |
|
| 251 |
if (conf != NULL) { |
| 252 |
*user = conf->argv[1]; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
if (conf->parent && |
| 258 |
conf->parent->config_type == CONF_ANON) { |
| 259 |
conf = conf->parent; |
| 260 |
|
| 261 |
} else { |
| 262 |
conf = NULL; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
if (conf == NULL) { |
| 268 |
conf = find_config(main_server->conf, CONF_ANON, NULL, FALSE); |
| 269 |
|
| 270 |
} else { |
| 271 |
find_config_set_top(conf); |
| 272 |
} |
| 273 |
|
| 274 |
if (conf != NULL) do { |
| 275 |
anonname = (char*) get_param_ptr(conf->subset, "UserName", FALSE); |
| 276 |
|
| 277 |
if (!anonname) |
| 278 |
anonname = ourname; |
| 279 |
|
| 280 |
if (anonname && |
| 281 |
strcmp(anonname, *user) == 0) { |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
} while ((conf = find_config_next(conf, conf->next, CONF_ANON, NULL, |
| 286 |
FALSE)) != NULL); |
| 287 |
|
| 288 |
if (!is_alias && !force_anon) { |
| 289 |
|
| 290 |
if (find_config((conf ? conf->subset : |
| 291 |
main_server->conf), CONF_PARAM, "AuthAliasOnly", FALSE)) { |
| 292 |
|
| 293 |
if (conf != NULL && |
| 294 |
conf->config_type == CONF_ANON) { |
| 295 |
conf = NULL; |
| 296 |
|
| 297 |
} else { |
| 298 |
*user = NULL; |
| 299 |
} |
| 300 |
|
| 301 |
if (*user != NULL && |
| 302 |
find_config(main_server->conf, CONF_PARAM, "AuthAliasOnly", FALSE)) { |
| 303 |
*user = NULL; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return conf; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
MODRET set_tcpaccessfiles(cmd_rec *cmd) { |
| 315 |
config_rec *c = NULL; |
| 316 |
|
| 317 |
|
| 318 |
char *allow_filename = "/etc/hosts.allow"; |
| 319 |
char *deny_filename = "/etc/hosts.deny"; |
| 320 |
|
| 321 |
CHECK_ARGS(cmd, 2); |
| 322 |
CHECK_CONF(cmd, CONF_ROOT|CONF_ANON|CONF_VIRTUAL|CONF_GLOBAL); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
allow_filename = cmd->argv[1]; |
| 328 |
deny_filename = cmd->argv[2]; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if (allow_filename[0] == '/') { |
| 338 |
|
| 339 |
|
| 340 |
if (!wrap_is_usable_file(allow_filename)) |
| 341 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 342 |
cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL)); |
| 343 |
|
| 344 |
} else if (allow_filename[0] == '~' && allow_filename[1] != '/') { |
| 345 |
char *allow_real_file = NULL; |
| 346 |
|
| 347 |
allow_real_file = dir_realpath(cmd->pool, allow_filename); |
| 348 |
|
| 349 |
if (allow_real_file == NULL || !wrap_is_usable_file(allow_real_file)) |
| 350 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 351 |
cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL)); |
| 352 |
|
| 353 |
allow_filename = allow_real_file; |
| 354 |
|
| 355 |
} else if (allow_filename[0] != '~' && allow_filename[0] != '/') { |
| 356 |
|
| 357 |
|
| 358 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 359 |
cmd->argv[0], ": '", allow_filename, "' must start with \"/\" or \"~\"", |
| 360 |
NULL)); |
| 361 |
|
| 362 |
} else { |
| 363 |
|
| 364 |
|
| 365 |
; |
| 366 |
} |
| 367 |
|
| 368 |
if (deny_filename[0] == '/') { |
| 369 |
|
| 370 |
|
| 371 |
if (!wrap_is_usable_file(deny_filename)) |
| 372 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 373 |
cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL)); |
| 374 |
|
| 375 |
} else if (deny_filename[0] == '~' && deny_filename[1] != '/') { |
| 376 |
char *deny_real_file = NULL; |
| 377 |
|
| 378 |
deny_real_file = dir_realpath(cmd->pool, deny_filename); |
| 379 |
|
| 380 |
if (deny_real_file == NULL || !wrap_is_usable_file(deny_real_file)) |
| 381 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 382 |
cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL)); |
| 383 |
|
| 384 |
deny_filename = deny_real_file; |
| 385 |
|
| 386 |
} else if (deny_filename[0] != '~' && deny_filename[0] != '/') { |
| 387 |
|
| 388 |
|
| 389 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 390 |
cmd->argv[0], ": '", deny_filename, "' must start with \"/\" or \"~\"", |
| 391 |
NULL)); |
| 392 |
|
| 393 |
} else { |
| 394 |
|
| 395 |
|
| 396 |
; |
| 397 |
} |
| 398 |
|
| 399 |
c = add_config_param_str(cmd->argv[0], 2, (void *) allow_filename, |
| 400 |
(void *) deny_filename); |
| 401 |
c->flags |= CF_MERGEDOWN; |
| 402 |
|
| 403 |
|
| 404 |
return PR_HANDLED(cmd); |
| 405 |
} |
| 406 |
|
| 407 |
MODRET set_tcpgroupaccessfiles(cmd_rec *cmd) { |
| 408 |
unsigned int group_argc = 1; |
| 409 |
char *expr, **group_argv = NULL; |
| 410 |
array_header *group_acl = NULL; |
| 411 |
config_rec *c = NULL; |
| 412 |
|
| 413 |
|
| 414 |
char *allow_filename = NULL, *deny_filename = NULL; |
| 415 |
|
| 416 |
CHECK_ARGS(cmd, 3); |
| 417 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
allow_filename = cmd->argv[2]; |
| 423 |
deny_filename = cmd->argv[3]; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
if (allow_filename[0] == '/') { |
| 433 |
|
| 434 |
|
| 435 |
if (!wrap_is_usable_file(allow_filename)) |
| 436 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 437 |
cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL)); |
| 438 |
|
| 439 |
} else if (allow_filename[0] == '~' && allow_filename[1] != '/') { |
| 440 |
char *allow_real_file = NULL; |
| 441 |
|
| 442 |
allow_real_file = dir_realpath(cmd->pool, allow_filename); |
| 443 |
|
| 444 |
if (allow_real_file == NULL || !wrap_is_usable_file(allow_real_file)) |
| 445 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 446 |
cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL)); |
| 447 |
|
| 448 |
allow_filename = allow_real_file; |
| 449 |
|
| 450 |
} else if (allow_filename[0] != '~' && allow_filename[0] != '/') { |
| 451 |
|
| 452 |
|
| 453 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 454 |
cmd->argv[0], ": '", allow_filename, "' must start with \"/\" or \"~\"", |
| 455 |
NULL)); |
| 456 |
|
| 457 |
} else { |
| 458 |
|
| 459 |
|
| 460 |
; |
| 461 |
} |
| 462 |
|
| 463 |
if (deny_filename[0] == '/') { |
| 464 |
|
| 465 |
|
| 466 |
if (!wrap_is_usable_file(deny_filename)) |
| 467 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 468 |
cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL)); |
| 469 |
|
| 470 |
} else if (deny_filename[0] == '~' && deny_filename[1] != '/') { |
| 471 |
char *deny_real_file = NULL; |
| 472 |
|
| 473 |
deny_real_file = dir_realpath(cmd->pool, deny_filename); |
| 474 |
|
| 475 |
if (deny_real_file == NULL || !wrap_is_usable_file(deny_real_file)) |
| 476 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 477 |
cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL)); |
| 478 |
|
| 479 |
deny_filename = deny_real_file; |
| 480 |
|
| 481 |
} else if (deny_filename[0] != '~' && deny_filename[0] != '/') { |
| 482 |
|
| 483 |
|
| 484 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 485 |
cmd->argv[0], ": '", deny_filename, "' must start with \"/\" or \"~\"", |
| 486 |
NULL)); |
| 487 |
|
| 488 |
} else { |
| 489 |
|
| 490 |
|
| 491 |
; |
| 492 |
} |
| 493 |
|
| 494 |
c = add_config_param(cmd->argv[0], 0); |
| 495 |
|
| 496 |
expr = (char *) cmd->argv[0]; |
| 497 |
group_acl = pr_expr_create(cmd->tmp_pool, &group_argc, &expr); |
| 498 |
|
| 499 |
|
| 500 |
c->argc = group_argc + 2; |
| 501 |
c->argv = pcalloc(c->pool, (group_argc + 3) * sizeof(char *)); |
| 502 |
group_argv = (char **) c->argv; |
| 503 |
|
| 504 |
|
| 505 |
*group_argv++ = pstrdup(c->pool, allow_filename); |
| 506 |
*group_argv++ = pstrdup(c->pool, deny_filename); |
| 507 |
|
| 508 |
|
| 509 |
if (group_argc && group_acl) |
| 510 |
while (group_argc--) { |
| 511 |
*group_argv++ = pstrdup(c->pool, *((char **) group_acl->elts)); |
| 512 |
group_acl->elts = ((char **) group_acl->elts) + 1; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
*group_argv = NULL; |
| 517 |
|
| 518 |
c->flags |= CF_MERGEDOWN; |
| 519 |
|
| 520 |
|
| 521 |
return PR_HANDLED(cmd); |
| 522 |
} |
| 523 |
|
| 524 |
MODRET set_tcpuseraccessfiles(cmd_rec *cmd) { |
| 525 |
unsigned int user_argc = 1; |
| 526 |
char *expr, **user_argv = NULL; |
| 527 |
array_header *user_acl = NULL; |
| 528 |
config_rec *c = NULL; |
| 529 |
|
| 530 |
|
| 531 |
char *allow_filename = NULL, *deny_filename = NULL; |
| 532 |
|
| 533 |
CHECK_ARGS(cmd, 3); |
| 534 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
allow_filename = cmd->argv[2]; |
| 540 |
deny_filename = cmd->argv[3]; |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
if (allow_filename[0] == '/') { |
| 550 |
|
| 551 |
|
| 552 |
if (!wrap_is_usable_file(allow_filename)) |
| 553 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 554 |
cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL)); |
| 555 |
|
| 556 |
} else if (allow_filename[0] == '~' && allow_filename[1] != '/') { |
| 557 |
char *allow_real_file = NULL; |
| 558 |
|
| 559 |
allow_real_file = dir_realpath(cmd->pool, allow_filename); |
| 560 |
|
| 561 |
if (allow_real_file == NULL || !wrap_is_usable_file(allow_real_file)) |
| 562 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 563 |
cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL)); |
| 564 |
|
| 565 |
allow_filename = allow_real_file; |
| 566 |
|
| 567 |
} else if (allow_filename[0] != '~' && allow_filename[0] != '/') { |
| 568 |
|
| 569 |
|
| 570 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 571 |
cmd->argv[0], ": '", allow_filename, "' must start with \"/\" or \"~\"", |
| 572 |
NULL)); |
| 573 |
|
| 574 |
} else { |
| 575 |
|
| 576 |
|
| 577 |
; |
| 578 |
} |
| 579 |
|
| 580 |
if (deny_filename[0] == '/') { |
| 581 |
|
| 582 |
|
| 583 |
if (!wrap_is_usable_file(deny_filename)) |
| 584 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 585 |
cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL)); |
| 586 |
|
| 587 |
} else if (deny_filename[0] == '~' && deny_filename[1] != '/') { |
| 588 |
char *deny_real_file = NULL; |
| 589 |
|
| 590 |
deny_real_file = dir_realpath(cmd->pool, deny_filename); |
| 591 |
|
| 592 |
if (deny_real_file == NULL || !wrap_is_usable_file(deny_real_file)) |
| 593 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 594 |
cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL)); |
| 595 |
|
| 596 |
deny_filename = deny_real_file; |
| 597 |
|
| 598 |
} else if (deny_filename[0] != '~' && deny_filename[0] != '/') { |
| 599 |
|
| 600 |
|
| 601 |
return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool, |
| 602 |
cmd->argv[0], ": '", deny_filename, "' must start with \"/\" or \"~\"", |
| 603 |
NULL)); |
| 604 |
|
| 605 |
} else { |
| 606 |
|
| 607 |
|
| 608 |
; |
| 609 |
} |
| 610 |
|
| 611 |
c = add_config_param_str(cmd->argv[0], 0); |
| 612 |
|
| 613 |
expr = (char *) cmd->argv[0]; |
| 614 |
user_acl = pr_expr_create(cmd->tmp_pool, &user_argc, &expr); |
| 615 |
|
| 616 |
|
| 617 |
c->argc = user_argc + 2; |
| 618 |
c->argv = pcalloc(c->pool, (user_argc + 3) * sizeof(char *)); |
| 619 |
user_argv = (char **) c->argv; |
| 620 |
|
| 621 |
|
| 622 |
*user_argv++ = pstrdup(c->pool, allow_filename); |
| 623 |
*user_argv++ = pstrdup(c->pool, deny_filename); |
| 624 |
|
| 625 |
|
| 626 |
if (user_argc && user_acl) |
| 627 |
while (user_argc--) { |
| 628 |
*user_argv++ = pstrdup(c->pool, *((char **) user_acl->elts)); |
| 629 |
user_acl->elts = ((char **) user_acl->elts) + 1; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
*user_argv = NULL; |
| 634 |
|
| 635 |
c->flags |= CF_MERGEDOWN; |
| 636 |
|
| 637 |
|
| 638 |
return PR_HANDLED(cmd); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
MODRET set_tcpaccesssysloglevels(cmd_rec *cmd) { |
| 646 |
config_rec *c = NULL; |
| 647 |
int allow_level = PR_LOG_DEBUG, deny_level = PR_LOG_DEBUG; |
| 648 |
|
| 649 |
CHECK_ARGS(cmd, 2); |
| 650 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_ANON|CONF_GLOBAL); |
| 651 |
|
| 652 |
if (strcasecmp(cmd->argv[1], "emerg") == 0) { |
| 653 |
allow_level = PR_LOG_EMERG; |
| 654 |
|
| 655 |
} else if (strcasecmp(cmd->argv[1], "alert") == 0) { |
| 656 |
allow_level = PR_LOG_ALERT; |
| 657 |
|
| 658 |
} else if (strcasecmp(cmd->argv[1], "crit") == 0) { |
| 659 |
allow_level = PR_LOG_CRIT; |
| 660 |
|
| 661 |
} else if (strcasecmp(cmd->argv[1], "error") == 0) { |
| 662 |
allow_level = PR_LOG_ERR; |
| 663 |
|
| 664 |
} else if (strcasecmp(cmd->argv[1], "warn") == 0) { |
| 665 |
allow_level = PR_LOG_WARNING; |
| 666 |
|
| 667 |
} else if (strcasecmp(cmd->argv[1], "notice") == 0) { |
| 668 |
allow_level = PR_LOG_NOTICE; |
| 669 |
|
| 670 |
} else if (strcasecmp(cmd->argv[1], "info") == 0) { |
| 671 |
allow_level = PR_LOG_INFO; |
| 672 |
|
| 673 |
} else if (strcasecmp(cmd->argv[1], "debug") == 0) { |
| 674 |
allow_level = PR_LOG_DEBUG; |
| 675 |
|
| 676 |
} else { |
| 677 |
CONF_ERROR(cmd, "TCPAccessSyslogLevels requires \"allow\" level keyword: " |
| 678 |
"one of emerg/alert/crit/error/warn/notice/info/debug"); |
| 679 |
} |
| 680 |
|
| 681 |
if (strcasecmp(cmd->argv[2], "emerg") == 0) { |
| 682 |
deny_level = PR_LOG_EMERG; |
| 683 |
|
| 684 |
} else if (strcasecmp(cmd->argv[2], "alert") == 0) { |
| 685 |
deny_level = PR_LOG_ALERT; |
| 686 |
|
| 687 |
} else if (strcasecmp(cmd->argv[2], "crit") == 0) { |
| 688 |
deny_level = PR_LOG_CRIT; |
| 689 |
|
| 690 |
} else if (strcasecmp(cmd->argv[2], "error") == 0) { |
| 691 |
deny_level = PR_LOG_ERR; |
| 692 |
|
| 693 |
} else if (strcasecmp(cmd->argv[2], "warn") == 0) { |
| 694 |
deny_level = PR_LOG_WARNING; |
| 695 |
|
| 696 |
} else if (strcasecmp(cmd->argv[2], "notice") == 0) { |
| 697 |
deny_level = PR_LOG_NOTICE; |
| 698 |
|
| 699 |
} else if (strcasecmp(cmd->argv[2], "info") == 0) { |
| 700 |
deny_level = PR_LOG_INFO; |
| 701 |
|
| 702 |
} else if (strcasecmp(cmd->argv[2], "debug") == 0 ) { |
| 703 |
deny_level = PR_LOG_DEBUG; |
| 704 |
|
| 705 |
} else { |
| 706 |
CONF_ERROR(cmd, "TCPAccessSyslogLevels requires \"deny\" level keyword: " |
| 707 |
"one of emerg/alert/crit/error/warn/notice/info/debug"); |
| 708 |
} |
| 709 |
|
| 710 |
c = add_config_param(cmd->argv[0], 2, NULL, NULL); |
| 711 |
c->argv[0] = palloc(c->pool, sizeof(int)); |
| 712 |
*((int *) c->argv[0]) = allow_level; |
| 713 |
c->argv[1] = palloc(c->pool, sizeof(int)); |
| 714 |
*((int *) c->argv[1]) = deny_level; |
| 715 |
|
| 716 |
c->flags |= CF_MERGEDOWN; |
| 717 |
|
| 718 |
return PR_HANDLED(cmd); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
MODRET set_tcpservicename(cmd_rec *cmd) { |
| 723 |
CHECK_ARGS(cmd, 1); |
| 724 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 725 |
|
| 726 |
add_config_param_str(cmd->argv[0], 1, cmd->argv[1]); |
| 727 |
return PR_HANDLED(cmd); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
MODRET wrap_handle_request(cmd_rec *cmd) { |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
struct request_info request; |
| 738 |
|
| 739 |
const char *user = NULL; |
| 740 |
config_rec *conf = NULL, *access_conf = NULL, *syslog_conf = NULL; |
| 741 |
hosts_allow_table = NULL; |
| 742 |
hosts_deny_table = NULL; |
| 743 |
|
| 744 |
|
| 745 |
session.hide_password = TRUE; |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
user = pr_table_get(session.notes, "mod_auth.orig-user", NULL); |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
if (user == NULL) { |
| 757 |
return PR_DECLINED(cmd); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
conf = wrap_resolve_user(cmd->pool, &user); |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
if ((access_conf = find_config(conf ? conf->subset : CURRENT_CONF, CONF_PARAM, |
| 771 |
"TCPUserAccessFiles", FALSE)) != NULL) { |
| 772 |
int matched = FALSE; |
| 773 |
array_header *user_array = NULL; |
| 774 |
|
| 775 |
while (access_conf) { |
| 776 |
|
| 777 |
user_array = make_array(cmd->tmp_pool, 0, sizeof(char *)); |
| 778 |
*((char **) push_array(user_array)) = pstrdup(cmd->tmp_pool, user); |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
if (wrap_eval_expression(((char **) access_conf->argv) + 2, |
| 784 |
user_array)) { |
| 785 |
pr_log_debug(DEBUG4, MOD_WRAP_VERSION |
| 786 |
": matched TCPUserAccessFiles expression"); |
| 787 |
matched = TRUE; |
| 788 |
break; |
| 789 |
} |
| 790 |
|
| 791 |
access_conf = find_config_next(access_conf, access_conf->next, |
| 792 |
CONF_PARAM, "TCPUserAccessFiles", FALSE); |
| 793 |
} |
| 794 |
|
| 795 |
if (!matched) |
| 796 |
access_conf = NULL; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
if (!access_conf && (access_conf = find_config(conf ? conf->subset : |
| 803 |
CURRENT_CONF, CONF_PARAM, "TCPGroupAccessFiles", FALSE)) != NULL) { |
| 804 |
unsigned char matched = FALSE; |
| 805 |
|
| 806 |
|
| 807 |
array_header *gid_array = make_array(cmd->pool, 0, sizeof(gid_t)); |
| 808 |
|
| 809 |
array_header *group_array = make_array(cmd->pool, 0, sizeof(char *)); |
| 810 |
|
| 811 |
while (access_conf) { |
| 812 |
if (pr_auth_getgroups(cmd->pool, user, &gid_array, &group_array) < 1) { |
| 813 |
pr_log_debug(DEBUG3, MOD_WRAP_VERSION |
| 814 |
": no supplemental groups found for user '%s'", user); |
| 815 |
|
| 816 |
} else { |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
if (wrap_eval_expression(((char **) access_conf->argv) + 2, |
| 822 |
group_array)) { |
| 823 |
pr_log_debug(DEBUG4, MOD_WRAP_VERSION |
| 824 |
": matched TCPGroupAccessFiles expression"); |
| 825 |
matched = TRUE; |
| 826 |
break; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
access_conf = find_config_next(access_conf, access_conf->next, |
| 831 |
CONF_PARAM, "TCPGroupAccessFiles", FALSE); |
| 832 |
} |
| 833 |
|
| 834 |
if (!matched) |
| 835 |
access_conf = NULL; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
if (!access_conf) { |
| 842 |
access_conf = find_config(conf ? conf->subset : CURRENT_CONF, |
| 843 |
CONF_PARAM, "TCPAccessFiles", FALSE); |
| 844 |
} |
| 845 |
|
| 846 |
if (access_conf) { |
| 847 |
hosts_allow_table = (char *) access_conf->argv[0]; |
| 848 |
hosts_deny_table = (char *) access_conf->argv[1]; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
if (hosts_allow_table != NULL && hosts_allow_table[0] == '~' && |
| 855 |
hosts_allow_table[1] == '/') { |
| 856 |
const char *allow_real_table = NULL; |
| 857 |
|
| 858 |
allow_real_table = wrap_get_user_table(cmd, user, hosts_allow_table); |
| 859 |
|
| 860 |
if (!wrap_is_usable_file(allow_real_table)) { |
| 861 |
pr_log_pri(PR_LOG_WARNING, MOD_WRAP_VERSION |
| 862 |
": configured TCPAllowFile %s is unusable", hosts_allow_table); |
| 863 |
hosts_allow_table = NULL; |
| 864 |
|
| 865 |
} else |
| 866 |
hosts_allow_table = (char *) allow_real_table; |
| 867 |
} |
| 868 |
|
| 869 |
if (hosts_deny_table != NULL && hosts_deny_table[0] == '~' && |
| 870 |
hosts_deny_table[1] == '/') { |
| 871 |
char *deny_real_table = NULL; |
| 872 |
|
| 873 |
deny_real_table = dir_realpath(cmd->pool, hosts_deny_table); |
| 874 |
|
| 875 |
if (!wrap_is_usable_file(deny_real_table)) { |
| 876 |
pr_log_pri(PR_LOG_WARNING, MOD_WRAP_VERSION |
| 877 |
": configured TCPDenyFile %s is unusable", hosts_deny_table); |
| 878 |
hosts_deny_table = NULL; |
| 879 |
|
| 880 |
} else { |
| 881 |
hosts_deny_table = deny_real_table; |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
if (hosts_allow_table != NULL && hosts_deny_table != NULL) { |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
} else if (hosts_allow_table == NULL && hosts_deny_table != NULL) { |
| 893 |
|
| 894 |
|
| 895 |
pr_log_pri(PR_LOG_INFO, MOD_WRAP_VERSION ": no usable allow access file -- " |
| 896 |
"allowing connection"); |
| 897 |
|
| 898 |
return PR_DECLINED(cmd); |
| 899 |
|
| 900 |
} else if (hosts_allow_table != NULL && hosts_deny_table == NULL) { |
| 901 |
|
| 902 |
|
| 903 |
pr_log_pri(PR_LOG_INFO, MOD_WRAP_VERSION ": no usable deny access file -- " |
| 904 |
"allowing connection"); |
| 905 |
|
| 906 |
return PR_DECLINED(cmd); |
| 907 |
|
| 908 |
} else { |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
return PR_DECLINED(cmd); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
pr_log_pri(PR_LOG_DEBUG, MOD_WRAP_VERSION ": using access files: %s, %s", |
| 918 |
hosts_allow_table, hosts_deny_table); |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
syslog_conf = find_config(main_server->conf, CONF_PARAM, |
| 924 |
"TCPAccessSyslogLevels", FALSE); |
| 925 |
|
| 926 |
if (syslog_conf) { |
| 927 |
allow_severity = *((int *) syslog_conf->argv[0]); |
| 928 |
deny_severity = *((int *) syslog_conf->argv[1]); |
| 929 |
|
| 930 |
} else { |
| 931 |
allow_severity = PR_LOG_INFO; |
| 932 |
deny_severity = PR_LOG_WARNING; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
allow_severity = log_getfacility() | allow_severity; |
| 946 |
deny_severity = log_getfacility() | deny_severity; |
| 947 |
|
| 948 |
pr_log_debug(DEBUG4, MOD_WRAP_VERSION ": checking under service name '%s'", |
| 949 |
wrap_service_name); |
| 950 |
request_init(&request, RQ_DAEMON, wrap_service_name, RQ_FILE, |
| 951 |
session.c->rfd, 0); |
| 952 |
|
| 953 |
fromhost(&request); |
| 954 |
|
| •955 |
if (!hosts_access(&request)) { |
| 956 |
char *denymsg = NULL; |
| 957 |
|
| 958 |
|
| 959 |
wrap_log_request_denied(deny_severity, &request); |
| 960 |
|
| 961 |
|
| 962 |
pr_event_generate("mod_wrap.connection-denied", NULL); |
| 963 |
|
| 964 |
|
| 965 |
denymsg = (char *) get_param_ptr(TOPLEVEL_CONF, "AccessDenyMsg", FALSE); |
| 966 |
if (denymsg != NULL) { |
| 967 |
denymsg = (char *) sreplace(cmd->tmp_pool, denymsg, "%u", user, NULL); |
| 968 |
} |
| 969 |
|
| 970 |
if (denymsg != NULL) { |
| 971 |
return PR_ERROR_MSG(cmd, R_530, denymsg); |
| 972 |
} |
| 973 |
|
| 974 |
return PR_ERROR_MSG(cmd, R_530, _("Access denied")); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
wrap_log_request_allowed(allow_severity, &request); |
| 982 |
|
| 983 |
return PR_DECLINED(cmd); |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
static void wrap_sess_reinit_ev(const void *event_data, void *user_data) { |
| 990 |
int res; |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
pr_event_unregister(&wrap_module, "core.session-reinit", wrap_sess_reinit_ev); |
| 995 |
|
| 996 |
|
| 997 |
wrap_service_name = "proftpd"; |
| 998 |
|
| 999 |
res = wrap_sess_init(); |
| 1000 |
if (res < 0) { |
| 1001 |
pr_session_disconnect(&wrap_module, PR_SESS_DISCONNECT_SESSION_INIT_FAILED, |
| 1002 |
NULL); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
static int wrap_sess_init(void) { |
| 1010 |
pr_event_register(&wrap_module, "core.session-reinit", wrap_sess_reinit_ev, |
| 1011 |
NULL); |
| 1012 |
|
| 1013 |
|
| 1014 |
wrap_service_name = get_param_ptr(main_server->conf, "TCPServiceName", FALSE); |
| 1015 |
if (wrap_service_name == NULL) { |
| 1016 |
wrap_service_name = "proftpd"; |
| 1017 |
} |
| 1018 |
|
| 1019 |
return 0; |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
static conftable wrap_conftab[] = { |
| 1026 |
{ "TCPAccessFiles", set_tcpaccessfiles, NULL }, |
| 1027 |
{ "TCPAccessSyslogLevels", set_tcpaccesssysloglevels, NULL }, |
| 1028 |
{ "TCPGroupAccessFiles", set_tcpgroupaccessfiles, NULL }, |
| 1029 |
{ "TCPServiceName", set_tcpservicename, NULL }, |
| 1030 |
{ "TCPUserAccessFiles", set_tcpuseraccessfiles, NULL }, |
| 1031 |
{ NULL } |
| 1032 |
}; |
| 1033 |
|
| 1034 |
static cmdtable wrap_cmdtab[] = { |
| 1035 |
{ PRE_CMD, C_PASS, G_NONE, wrap_handle_request, FALSE, FALSE }, |
| 1036 |
{ 0, NULL } |
| 1037 |
}; |
| 1038 |
|
| 1039 |
module wrap_module = { |
| 1040 |
NULL, NULL, |
| 1041 |
|
| 1042 |
|
| 1043 |
0x20, |
| 1044 |
|
| 1045 |
|
| 1046 |
"wrap", |
| 1047 |
|
| 1048 |
|
| 1049 |
wrap_conftab, |
| 1050 |
|
| 1051 |
|
| 1052 |
wrap_cmdtab, |
| 1053 |
|
| 1054 |
|
| 1055 |
NULL, |
| 1056 |
|
| 1057 |
|
| 1058 |
NULL, |
| 1059 |
|
| 1060 |
|
| 1061 |
wrap_sess_init, |
| 1062 |
|
| 1063 |
|
| 1064 |
MOD_WRAP_VERSION |
| 1065 |
}; |