| 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 |
#include "conf.h" |
| 26 |
|
| 27 |
#define MOD_SITE_MISC_VERSION "mod_site_misc/1.6" |
| 28 |
|
| 29 |
extern pr_response_t *resp_list, *resp_err_list; |
| 30 |
|
| 31 |
module site_misc_module; |
| 32 |
|
| 33 |
static unsigned int site_misc_engine = TRUE; |
| 34 |
|
| 35 |
|
| 36 |
static int site_misc_sess_init(void); |
| 37 |
|
| 38 |
static int site_misc_check_filters(cmd_rec *cmd, const char *path) { |
| 39 |
#ifdef PR_USE_REGEX |
| 40 |
pr_regex_t *pre = get_param_ptr(CURRENT_CONF, "PathAllowFilter", FALSE); |
| 41 |
if (pre != NULL && |
| 42 |
pr_regexp_exec(pre, path, 0, NULL, 0, 0, 0) != 0) { |
| 43 |
pr_log_pri(PR_LOG_NOTICE, MOD_SITE_MISC_VERSION |
| 44 |
": 'SITE %s' denied by PathAllowFilter", cmd->arg); |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
pre = get_param_ptr(CURRENT_CONF, "PathDenyFilter", FALSE); |
| 49 |
if (pre != NULL && |
| 50 |
pr_regexp_exec(pre, path, 0, NULL, 0, 0, 0) == 0) { |
| 51 |
pr_log_pri(PR_LOG_NOTICE, MOD_SITE_MISC_VERSION |
| 52 |
": 'SITE %s' denied by PathDenyFilter", cmd->arg); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
static int site_misc_create_dir(const char *dir) { |
| 61 |
struct stat st; |
| 62 |
int res; |
| 63 |
|
| 64 |
pr_fs_clear_cache2(dir); |
| 65 |
res = pr_fsio_stat(dir, &st); |
| 66 |
if (res < 0 && |
| 67 |
errno != ENOENT) { |
| 68 |
int xerrno = errno; |
| 69 |
|
| 70 |
pr_log_debug(DEBUG2, MOD_SITE_MISC_VERSION ": error checking '%s': %s", |
| 71 |
dir, strerror(xerrno)); |
| 72 |
|
| 73 |
errno = xerrno; |
| 74 |
return -1; |
| 75 |
} |
| 76 |
|
| 77 |
if (res == 0) { |
| 78 |
|
| 79 |
return 1; |
| 80 |
} |
| 81 |
|
| 82 |
if (pr_fsio_mkdir(dir, 0777) < 0) { |
| 83 |
int xerrno = errno; |
| 84 |
|
| 85 |
pr_log_debug(DEBUG2, MOD_SITE_MISC_VERSION ": error creating '%s': %s", |
| 86 |
dir, strerror(xerrno)); |
| 87 |
|
| 88 |
errno = xerrno; |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static int site_misc_create_path(pool *p, const char *path) { |
| 96 |
struct stat st; |
| 97 |
char *curr_path, *tmp_path; |
| 98 |
|
| 99 |
pr_fs_clear_cache2(path); |
| 100 |
if (pr_fsio_stat(path, &st) == 0) { |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
tmp_path = pstrdup(p, path); |
| 109 |
|
| 110 |
curr_path = "/"; |
| 111 |
while (tmp_path && |
| 112 |
*tmp_path) { |
| 113 |
char *curr_dir; |
| 114 |
int res; |
| 115 |
cmd_rec *cmd; |
| 116 |
pool *sub_pool; |
| 117 |
|
| 118 |
pr_signals_handle(); |
| 119 |
|
| 120 |
curr_dir = strsep(&tmp_path, "/"); |
| 121 |
curr_path = pdircat(p, curr_path, curr_dir, NULL); |
| 122 |
|
| 123 |
|
| 124 |
sub_pool = pr_pool_create_sz(p, 64); |
| 125 |
cmd = pr_cmd_alloc(sub_pool, 2, pstrdup(sub_pool, C_MKD), |
| 126 |
pstrdup(sub_pool, curr_path)); |
| 127 |
cmd->arg = pstrdup(cmd->pool, curr_path); |
| 128 |
cmd->cmd_class = CL_DIRS|CL_WRITE; |
| 129 |
|
| 130 |
res = pr_cmd_dispatch_phase(cmd, PRE_CMD, 0); |
| 131 |
if (res < 0) { |
| 132 |
int xerrno = errno; |
| 133 |
|
| 134 |
pr_log_debug(DEBUG3, MOD_SITE_MISC_VERSION |
| 135 |
": creating directory '%s' blocked by MKD handler: %s", curr_path, |
| 136 |
strerror(xerrno)); |
| 137 |
|
| 138 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 139 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 140 |
pr_response_clear(&resp_err_list); |
| 141 |
|
| 142 |
destroy_pool(sub_pool); |
| 143 |
sub_pool = NULL; |
| 144 |
cmd = NULL; |
| 145 |
|
| 146 |
errno = xerrno; |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
res = site_misc_create_dir(curr_path); |
| 151 |
if (res < 0) { |
| 152 |
int xerrno = errno; |
| 153 |
|
| 154 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 155 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 156 |
pr_response_clear(&resp_err_list); |
| 157 |
|
| 158 |
destroy_pool(sub_pool); |
| 159 |
sub_pool = NULL; |
| 160 |
cmd = NULL; |
| 161 |
|
| 162 |
errno = xerrno; |
| 163 |
return -1; |
| 164 |
} |
| 165 |
|
| 166 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 167 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 168 |
pr_response_clear(&resp_list); |
| 169 |
|
| 170 |
destroy_pool(sub_pool); |
| 171 |
sub_pool = NULL; |
| 172 |
cmd = NULL; |
| 173 |
} |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static int site_misc_delete_dir(pool *p, const char *dir) { |
| 179 |
void *dirh; |
| 180 |
struct dirent *dent; |
| 181 |
int res, xerrno; |
| 182 |
cmd_rec *cmd; |
| 183 |
pool *sub_pool; |
| 184 |
|
| 185 |
dirh = pr_fsio_opendir(dir); |
| 186 |
xerrno = errno; |
| 187 |
|
| 188 |
if (dirh == NULL) { |
| 189 |
pr_log_debug(DEBUG2, MOD_SITE_MISC_VERSION |
| 190 |
": error reading directory '%s': %s", dir, strerror(xerrno)); |
| 191 |
errno = xerrno; |
| 192 |
return -1; |
| 193 |
} |
| 194 |
|
| 195 |
while ((dent = pr_fsio_readdir(dirh)) != NULL) { |
| 196 |
struct stat st; |
| 197 |
char *file; |
| 198 |
|
| 199 |
pr_signals_handle(); |
| 200 |
|
| 201 |
if (strcmp(dent->d_name, ".") == 0 || |
| 202 |
strcmp(dent->d_name, "..") == 0) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
file = pdircat(p, dir, dent->d_name, NULL); |
| 207 |
|
| 208 |
if (pr_fsio_stat(file, &st) < 0) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
if (S_ISDIR(st.st_mode)) { |
| 213 |
res = site_misc_delete_dir(p, file); |
| 214 |
xerrno = errno; |
| 215 |
|
| 216 |
if (res < 0) { |
| 217 |
pr_fsio_closedir(dirh); |
| 218 |
|
| 219 |
errno = xerrno; |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
} else { |
| 224 |
|
| 225 |
sub_pool = pr_pool_create_sz(p, 64); |
| 226 |
cmd = pr_cmd_alloc(sub_pool, 2, pstrdup(sub_pool, C_DELE), |
| 227 |
pstrdup(sub_pool, file)); |
| 228 |
cmd->arg = pstrdup(cmd->pool, file); |
| 229 |
cmd->cmd_class = CL_WRITE; |
| 230 |
|
| 231 |
pr_response_block(TRUE); |
| 232 |
res = pr_cmd_dispatch_phase(cmd, PRE_CMD, 0); |
| 233 |
xerrno = errno; |
| 234 |
|
| 235 |
if (res < 0) { |
| 236 |
pr_log_debug(DEBUG3, MOD_SITE_MISC_VERSION |
| 237 |
": deleting file '%s' blocked by DELE handler: %s", file, |
| 238 |
strerror(xerrno)); |
| 239 |
|
| 240 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 241 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 242 |
pr_response_clear(&resp_err_list); |
| 243 |
pr_response_block(FALSE); |
| 244 |
|
| 245 |
destroy_pool(sub_pool); |
| 246 |
pr_fsio_closedir(dirh); |
| 247 |
|
| 248 |
errno = xerrno; |
| 249 |
return -1; |
| 250 |
} |
| 251 |
|
| 252 |
res = pr_fsio_unlink(file); |
| 253 |
xerrno = errno; |
| 254 |
|
| 255 |
if (res < 0) { |
| 256 |
pr_fsio_closedir(dirh); |
| 257 |
|
| 258 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 259 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 260 |
pr_response_clear(&resp_err_list); |
| 261 |
pr_response_block(FALSE); |
| 262 |
|
| 263 |
destroy_pool(sub_pool); |
| 264 |
pr_fsio_closedir(dirh); |
| 265 |
|
| 266 |
errno = xerrno; |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
pr_response_add(R_250, _("%s command successful"), (char *) cmd->argv[0]); |
| 271 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 272 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 273 |
pr_response_clear(&resp_list); |
| 274 |
destroy_pool(sub_pool); |
| 275 |
pr_response_block(FALSE); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
pr_fsio_closedir(dirh); |
| 280 |
|
| 281 |
|
| 282 |
sub_pool = pr_pool_create_sz(p, 64); |
| 283 |
cmd = pr_cmd_alloc(sub_pool, 2, pstrdup(sub_pool, C_RMD), |
| 284 |
pstrdup(sub_pool, dir)); |
| 285 |
cmd->arg = pstrdup(cmd->pool, dir); |
| 286 |
cmd->cmd_class = CL_DIRS|CL_WRITE; |
| 287 |
|
| 288 |
pr_response_block(TRUE); |
| 289 |
res = pr_cmd_dispatch_phase(cmd, PRE_CMD, 0); |
| 290 |
xerrno = errno; |
| 291 |
|
| 292 |
if (res < 0) { |
| 293 |
pr_log_debug(DEBUG3, MOD_SITE_MISC_VERSION |
| 294 |
": removing directory '%s' blocked by RMD handler: %s", dir, |
| 295 |
strerror(xerrno)); |
| 296 |
|
| 297 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 298 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 299 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 300 |
pr_response_clear(&resp_err_list); |
| 301 |
pr_response_block(FALSE); |
| 302 |
|
| 303 |
destroy_pool(sub_pool); |
| 304 |
|
| 305 |
errno = xerrno; |
| 306 |
return -1; |
| 307 |
} |
| 308 |
|
| 309 |
res = pr_fsio_rmdir(dir); |
| 310 |
xerrno = errno; |
| 311 |
|
| 312 |
if (res < 0) { |
| 313 |
pr_log_debug(DEBUG3, MOD_SITE_MISC_VERSION |
| 314 |
": error removing directory '%s': %s", dir, |
| 315 |
strerror(xerrno)); |
| 316 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 317 |
pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 318 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 319 |
pr_response_clear(&resp_err_list); |
| 320 |
pr_response_block(FALSE); |
| 321 |
|
| 322 |
destroy_pool(sub_pool); |
| 323 |
errno = xerrno; |
| 324 |
return -1; |
| 325 |
} |
| 326 |
|
| 327 |
pr_response_add(R_257, _("\"%s\" - Directory successfully created"), |
| 328 |
quote_dir(cmd->tmp_pool, (char *) dir)); |
| 329 |
pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 330 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 331 |
pr_response_clear(&resp_list); |
| 332 |
pr_response_block(FALSE); |
| 333 |
destroy_pool(sub_pool); |
| 334 |
|
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
static int site_misc_delete_path(pool *p, const char *path) { |
| 339 |
struct stat st; |
| 340 |
|
| 341 |
pr_fs_clear_cache2(path); |
| 342 |
if (pr_fsio_stat(path, &st) < 0) { |
| 343 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 344 |
": error checking path %s", path); |
| 345 |
return -1; |
| 346 |
} |
| 347 |
|
| 348 |
if (!S_ISDIR(st.st_mode)) { |
| 349 |
errno = EINVAL; |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
return site_misc_delete_dir(p, path); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
static int site_misc_parsetime(char *timestamp, size_t timestamp_len, |
| 363 |
unsigned int *year, unsigned int *month, unsigned int *day, |
| 364 |
unsigned int *hour, unsigned int *min, unsigned int *sec) { |
| 365 |
register unsigned int i; |
| 366 |
char c, *ptr; |
| 367 |
int have_secs = FALSE, valid_timestamp = TRUE; |
| 368 |
|
| 369 |
|
| 370 |
for (i = 0; i < timestamp_len; i++) { |
| 371 |
if (PR_ISDIGIT((int) timestamp[i]) == 0) { |
| 372 |
valid_timestamp = FALSE; |
| 373 |
break; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if (!valid_timestamp) { |
| 378 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 379 |
": timestamp '%s' contains non-digits", timestamp); |
| 380 |
errno = EINVAL; |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
if (timestamp_len == 14) { |
| 385 |
have_secs = TRUE; |
| 386 |
} |
| 387 |
|
| 388 |
ptr = timestamp; |
| 389 |
c = timestamp[4]; |
| 390 |
timestamp[4] = '\0'; |
| 391 |
*year = atoi(ptr); |
| 392 |
timestamp[4] = c; |
| 393 |
|
| 394 |
ptr = &(timestamp[4]); |
| 395 |
c = timestamp[6]; |
| 396 |
timestamp[6] = '\0'; |
| 397 |
*month = atoi(ptr); |
| 398 |
timestamp[6] = c; |
| 399 |
|
| 400 |
if (*month > 12) { |
| 401 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 402 |
": bad number of months in '%s' (%u)", timestamp, *month); |
| 403 |
errno = EINVAL; |
| 404 |
return -1; |
| 405 |
} |
| 406 |
|
| 407 |
ptr = &(timestamp[6]); |
| 408 |
c = timestamp[8]; |
| 409 |
timestamp[8] = '\0'; |
| 410 |
*day = atoi(ptr); |
| 411 |
timestamp[8] = c; |
| 412 |
|
| 413 |
if (*day > 31) { |
| 414 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 415 |
": bad number of days in '%s' (%u)", timestamp, *day); |
| 416 |
errno = EINVAL; |
| 417 |
return -1; |
| 418 |
} |
| 419 |
|
| 420 |
ptr = &(timestamp[8]); |
| 421 |
c = timestamp[10]; |
| 422 |
timestamp[10] = '\0'; |
| 423 |
*hour = atoi(ptr); |
| 424 |
timestamp[10] = c; |
| 425 |
|
| 426 |
if (*hour > 24) { |
| 427 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 428 |
": bad number of hours in '%s' (%u)", timestamp, *hour); |
| 429 |
errno = EINVAL; |
| 430 |
return -1; |
| 431 |
} |
| 432 |
|
| 433 |
ptr = &(timestamp[10]); |
| 434 |
|
| 435 |
|
| 436 |
if (have_secs) { |
| 437 |
c = timestamp[12]; |
| 438 |
timestamp[12] = '\0'; |
| 439 |
} |
| 440 |
|
| 441 |
*min = atoi(ptr); |
| 442 |
|
| 443 |
if (have_secs) { |
| 444 |
timestamp[12] = c; |
| 445 |
} |
| 446 |
|
| 447 |
if (*min > 60) { |
| 448 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 449 |
": bad number of minutes in '%s' (%u)", timestamp, *min); |
| 450 |
errno = EINVAL; |
| 451 |
return -1; |
| 452 |
} |
| 453 |
|
| 454 |
if (have_secs) { |
| 455 |
ptr = &(timestamp[12]); |
| 456 |
*sec = atoi(ptr); |
| 457 |
|
| 458 |
if (*sec > 60) { |
| 459 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 460 |
": bad number of seconds in '%s' (%u)", timestamp, *sec); |
| 461 |
errno = EINVAL; |
| 462 |
return -1; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
static time_t site_misc_mktime(unsigned int year, unsigned int month, |
| 470 |
unsigned int mday, unsigned int hour, unsigned int min, unsigned int sec) { |
| 471 |
struct tm tm; |
| 472 |
time_t res; |
| 473 |
char *env; |
| 474 |
|
| 475 |
#ifdef HAVE_TZNAME |
| 476 |
char *tzname_dup[2]; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
memcpy(&tzname_dup, tzname, sizeof(tzname_dup)); |
| 499 |
#endif |
| 500 |
|
| 501 |
env = pr_env_get(session.pool, "TZ"); |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
if (pr_env_set(session.pool, "TZ", "GMT") < 0) { |
| 507 |
pr_log_debug(DEBUG8, MOD_SITE_MISC_VERSION |
| 508 |
": error setting TZ environment variable to 'GMT': %s", strerror(errno)); |
| 509 |
} |
| 510 |
|
| 511 |
tm.tm_sec = sec; |
| 512 |
tm.tm_min = min; |
| 513 |
tm.tm_hour = hour; |
| 514 |
tm.tm_mday = mday; |
| 515 |
tm.tm_mon = (month - 1); |
| 516 |
tm.tm_year = (year - 1900); |
| 517 |
tm.tm_wday = 0; |
| 518 |
tm.tm_yday = 0; |
| 519 |
tm.tm_isdst = -1; |
| 520 |
|
| 521 |
res = mktime(&tm); |
| 522 |
|
| 523 |
|
| 524 |
if (env) { |
| 525 |
if (pr_env_set(session.pool, "TZ", env) < 0) { |
| 526 |
pr_log_debug(DEBUG8, MOD_SITE_MISC_VERSION |
| 527 |
": error setting TZ environment variable to '%s': %s", env, |
| 528 |
strerror(errno)); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
#ifdef HAVE_TZNAME |
| 533 |
|
| 534 |
memcpy(tzname, tzname_dup, sizeof(tzname_dup)); |
| 535 |
#endif |
| 536 |
|
| 537 |
return res; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
MODRET set_sitemiscengine(cmd_rec *cmd) { |
| 545 |
config_rec *c; |
| 546 |
int engine; |
| 547 |
|
| 548 |
CHECK_ARGS(cmd, 1); |
| 549 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 550 |
|
| 551 |
engine = get_boolean(cmd, 1); |
| 552 |
if (engine == -1) { |
| 553 |
CONF_ERROR(cmd, "expected Boolean parameter"); |
| 554 |
} |
| 555 |
|
| 556 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 557 |
c->argv[0] = pcalloc(c->pool, sizeof(unsigned int)); |
| 558 |
*((unsigned int *) c->argv[0]) = engine; |
| 559 |
|
| 560 |
return PR_HANDLED(cmd); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
MODRET site_misc_mkdir(cmd_rec *cmd) { |
| 567 |
if (!site_misc_engine) { |
| 568 |
return PR_DECLINED(cmd); |
| 569 |
} |
| 570 |
|
| 571 |
if (cmd->argc < 2) { |
| 572 |
pr_log_debug(DEBUG5, MOD_SITE_MISC_VERSION |
| 573 |
"%s : wrong number of parameters (%d)", (char *) cmd->argv[0], cmd->argc); |
| 574 |
return PR_DECLINED(cmd); |
| 575 |
} |
| 576 |
|
| 577 |
if (strncasecmp(cmd->argv[1], "MKDIR", 6) == 0) { |
| 578 |
register unsigned int i; |
| 579 |
char *cmd_name, *decoded_path, *path = ""; |
| 580 |
unsigned char *authenticated; |
| 581 |
|
| 582 |
if (cmd->argc < 3) { |
| 583 |
return PR_DECLINED(cmd); |
| 584 |
} |
| 585 |
|
| 586 |
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE); |
| 587 |
if (authenticated == NULL || |
| 588 |
*authenticated == FALSE) { |
| 589 |
pr_response_add_err(R_530, _("Please login with USER and PASS")); |
| 590 |
|
| 591 |
pr_cmd_set_errno(cmd, EPERM); |
| 592 |
errno = EPERM; |
| 593 |
return PR_ERROR(cmd); |
| 594 |
} |
| 595 |
|
| 596 |
for (i = 2; i < cmd->argc; i++) { |
| 597 |
path = pstrcat(cmd->tmp_pool, path, *path ? " " : "", cmd->argv[i], NULL); |
| 598 |
} |
| 599 |
|
| 600 |
decoded_path = pr_fs_decode_path2(cmd->tmp_pool, path, |
| 601 |
FSIO_DECODE_FL_TELL_ERRORS); |
| 602 |
if (decoded_path == NULL) { |
| 603 |
int xerrno = errno; |
| 604 |
|
| 605 |
pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", path, |
| 606 |
strerror(xerrno)); |
| 607 |
pr_response_add_err(R_550, |
| 608 |
_("%s: Illegal character sequence in filename"), path); |
| 609 |
|
| 610 |
pr_cmd_set_errno(cmd, xerrno); |
| 611 |
errno = xerrno; |
| 612 |
return PR_ERROR(cmd); |
| 613 |
} |
| 614 |
|
| 615 |
path = decoded_path; |
| 616 |
|
| 617 |
if (site_misc_check_filters(cmd, path) < 0) { |
| 618 |
int xerrno = EPERM; |
| 619 |
|
| 620 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 621 |
|
| 622 |
pr_cmd_set_errno(cmd, xerrno); |
| 623 |
errno = xerrno; |
| 624 |
return PR_ERROR(cmd); |
| 625 |
} |
| 626 |
|
| 627 |
path = dir_canonical_path(cmd->tmp_pool, path); |
| 628 |
if (path == NULL) { |
| 629 |
int xerrno = EINVAL; |
| 630 |
|
| 631 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 632 |
|
| 633 |
pr_cmd_set_errno(cmd, xerrno); |
| 634 |
errno = xerrno; |
| 635 |
return PR_ERROR(cmd); |
| 636 |
} |
| 637 |
|
| 638 |
cmd_name = cmd->argv[0]; |
| 639 |
cmd->argv[0] = "SITE_MKDIR"; |
| 640 |
if (!dir_check_canon(cmd->tmp_pool, cmd, G_WRITE, path, NULL)) { |
| 641 |
int xerrno = EPERM; |
| 642 |
|
| 643 |
cmd->argv[0] = cmd_name; |
| 644 |
|
| 645 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 646 |
": %s command denied by <Limit>", (char *) cmd->argv[0]); |
| 647 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 648 |
|
| 649 |
pr_cmd_set_errno(cmd, xerrno); |
| 650 |
errno = xerrno; |
| 651 |
return PR_ERROR(cmd); |
| 652 |
} |
| 653 |
cmd->argv[0] = cmd_name; |
| 654 |
|
| 655 |
if (site_misc_create_path(cmd->tmp_pool, path) < 0) { |
| 656 |
int xerrno = errno; |
| 657 |
|
| 658 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 659 |
|
| 660 |
pr_cmd_set_errno(cmd, xerrno); |
| 661 |
errno = xerrno; |
| 662 |
return PR_ERROR(cmd); |
| 663 |
} |
| 664 |
|
| 665 |
pr_response_add(R_200, _("SITE %s command successful"), |
| 666 |
(char *) cmd->argv[1]); |
| 667 |
return PR_HANDLED(cmd); |
| 668 |
} |
| 669 |
|
| 670 |
if (strncasecmp(cmd->argv[1], "HELP", 5) == 0) { |
| 671 |
pr_response_add(R_214, "MKDIR <sp> path"); |
| 672 |
} |
| 673 |
|
| 674 |
return PR_DECLINED(cmd); |
| 675 |
} |
| 676 |
|
| 677 |
MODRET site_misc_rmdir(cmd_rec *cmd) { |
| 678 |
if (!site_misc_engine) { |
| 679 |
return PR_DECLINED(cmd); |
| 680 |
} |
| 681 |
|
| 682 |
if (cmd->argc < 2) { |
| 683 |
pr_log_debug(DEBUG5, MOD_SITE_MISC_VERSION |
| 684 |
"%s : wrong number of parameters (%d)", (char *) cmd->argv[0], cmd->argc); |
| 685 |
return PR_DECLINED(cmd); |
| 686 |
} |
| 687 |
|
| 688 |
if (strncasecmp(cmd->argv[1], "RMDIR", 6) == 0) { |
| 689 |
register unsigned int i; |
| 690 |
char *cmd_name, *decoded_path, *path = ""; |
| 691 |
unsigned char *authenticated; |
| 692 |
|
| 693 |
if (cmd->argc < 3) |
| 694 |
return PR_DECLINED(cmd); |
| 695 |
|
| 696 |
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE); |
| 697 |
|
| 698 |
if (!authenticated || |
| 699 |
*authenticated == FALSE) { |
| 700 |
pr_response_add_err(R_530, _("Please login with USER and PASS")); |
| 701 |
|
| 702 |
pr_cmd_set_errno(cmd, EPERM); |
| 703 |
errno = EPERM; |
| 704 |
return PR_ERROR(cmd); |
| 705 |
} |
| 706 |
|
| 707 |
for (i = 2; i < cmd->argc; i++) { |
| 708 |
path = pstrcat(cmd->tmp_pool, path, *path ? " " : "", cmd->argv[i], NULL); |
| 709 |
} |
| 710 |
|
| 711 |
decoded_path = pr_fs_decode_path2(cmd->tmp_pool, path, |
| 712 |
FSIO_DECODE_FL_TELL_ERRORS); |
| 713 |
if (decoded_path == NULL) { |
| 714 |
int xerrno = errno; |
| 715 |
|
| 716 |
pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", path, |
| 717 |
strerror(xerrno)); |
| 718 |
pr_response_add_err(R_550, |
| 719 |
_("%s: Illegal character sequence in filename"), path); |
| 720 |
|
| 721 |
pr_cmd_set_errno(cmd, xerrno); |
| 722 |
errno = xerrno; |
| 723 |
return PR_ERROR(cmd); |
| 724 |
} |
| 725 |
|
| 726 |
path = dir_canonical_path(cmd->tmp_pool, decoded_path); |
| 727 |
if (path == NULL) { |
| 728 |
int xerrno = EINVAL; |
| 729 |
|
| 730 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 731 |
|
| 732 |
pr_cmd_set_errno(cmd, xerrno); |
| 733 |
errno = xerrno; |
| 734 |
return PR_ERROR(cmd); |
| 735 |
} |
| 736 |
|
| 737 |
cmd_name = cmd->argv[0]; |
| 738 |
cmd->argv[0] = "SITE_RMDIR"; |
| 739 |
if (!dir_check_canon(cmd->tmp_pool, cmd, G_WRITE, path, NULL)) { |
| 740 |
int xerrno = EPERM; |
| 741 |
|
| 742 |
cmd->argv[0] = cmd_name; |
| 743 |
|
| 744 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 745 |
": %s command denied by <Limit>", (char *) cmd->argv[0]); |
| 746 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 747 |
|
| 748 |
pr_cmd_set_errno(cmd, xerrno); |
| 749 |
errno = xerrno; |
| 750 |
return PR_ERROR(cmd); |
| 751 |
} |
| 752 |
cmd->argv[0] = cmd_name; |
| 753 |
|
| 754 |
if (site_misc_delete_path(cmd->tmp_pool, path) < 0) { |
| 755 |
int xerrno = errno; |
| 756 |
|
| 757 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 758 |
|
| 759 |
pr_cmd_set_errno(cmd, xerrno); |
| 760 |
errno = xerrno; |
| 761 |
return PR_ERROR(cmd); |
| 762 |
} |
| 763 |
|
| 764 |
pr_response_add(R_200, _("SITE %s command successful"), |
| 765 |
(char *) cmd->argv[1]); |
| 766 |
return PR_HANDLED(cmd); |
| 767 |
} |
| 768 |
|
| 769 |
if (strncasecmp(cmd->argv[1], "HELP", 5) == 0) { |
| 770 |
pr_response_add(R_214, "RMDIR <sp> path"); |
| 771 |
} |
| 772 |
|
| 773 |
return PR_DECLINED(cmd); |
| 774 |
} |
| 775 |
|
| 776 |
MODRET site_misc_symlink(cmd_rec *cmd) { |
| 777 |
if (!site_misc_engine) { |
| 778 |
return PR_DECLINED(cmd); |
| 779 |
} |
| 780 |
|
| 781 |
if (cmd->argc < 2) { |
| 782 |
pr_log_debug(DEBUG5, MOD_SITE_MISC_VERSION |
| 783 |
"%s : wrong number of parameters (%d)", (char *) cmd->argv[0], cmd->argc); |
| 784 |
return PR_DECLINED(cmd); |
| 785 |
} |
| 786 |
|
| 787 |
if (strncasecmp(cmd->argv[1], "SYMLINK", 8) == 0) { |
| 788 |
struct stat st; |
| 789 |
int res; |
| 790 |
char *cmd_name, *decoded_path, *src, *dst; |
| 791 |
unsigned char *authenticated; |
| 792 |
|
| 793 |
if (cmd->argc < 4) |
| 794 |
return PR_DECLINED(cmd); |
| 795 |
|
| 796 |
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE); |
| 797 |
|
| 798 |
if (!authenticated || |
| 799 |
*authenticated == FALSE) { |
| 800 |
pr_response_add_err(R_530, _("Please login with USER and PASS")); |
| 801 |
|
| 802 |
pr_cmd_set_errno(cmd, EPERM); |
| 803 |
errno = EPERM; |
| 804 |
return PR_ERROR(cmd); |
| 805 |
} |
| 806 |
|
| 807 |
decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->argv[2], |
| 808 |
FSIO_DECODE_FL_TELL_ERRORS); |
| 809 |
if (decoded_path == NULL) { |
| 810 |
int xerrno = errno; |
| 811 |
|
| 812 |
pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", |
| 813 |
(char *) cmd->argv[2], strerror(xerrno)); |
| 814 |
pr_response_add_err(R_550, |
| 815 |
_("%s: Illegal character sequence in filename"), (char *) cmd->argv[2]); |
| 816 |
|
| 817 |
pr_cmd_set_errno(cmd, xerrno); |
| 818 |
errno = xerrno; |
| 819 |
return PR_ERROR(cmd); |
| 820 |
} |
| 821 |
|
| 822 |
src = dir_canonical_path(cmd->tmp_pool, decoded_path); |
| 823 |
if (src == NULL) { |
| 824 |
int xerrno = EINVAL; |
| 825 |
|
| 826 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 827 |
|
| 828 |
errno = xerrno; |
| 829 |
return PR_ERROR(cmd); |
| 830 |
} |
| 831 |
|
| 832 |
cmd_name = cmd->argv[0]; |
| 833 |
cmd->argv[0] = "SITE_SYMLINK"; |
| 834 |
if (!dir_check_canon(cmd->tmp_pool, cmd, G_READ, src, NULL)) { |
| 835 |
int xerrno = EPERM; |
| 836 |
|
| 837 |
cmd->argv[0] = cmd_name; |
| 838 |
|
| 839 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 840 |
": %s command denied by <Limit>", (char *) cmd->argv[0]); |
| 841 |
pr_response_add_err(R_550, "%s: %s", (char *) cmd->argv[2], |
| 842 |
strerror(xerrno)); |
| 843 |
|
| 844 |
errno = xerrno; |
| 845 |
return PR_ERROR(cmd); |
| 846 |
} |
| 847 |
|
| 848 |
decoded_path = pr_fs_decode_path2(cmd->tmp_pool, cmd->argv[3], |
| 849 |
FSIO_DECODE_FL_TELL_ERRORS); |
| 850 |
if (decoded_path == NULL) { |
| 851 |
int xerrno = errno; |
| 852 |
|
| 853 |
pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", |
| 854 |
(char *) cmd->argv[3], strerror(xerrno)); |
| 855 |
pr_response_add_err(R_550, |
| 856 |
_("%s: Illegal character sequence in filename"), (char *) cmd->argv[3]); |
| 857 |
|
| 858 |
pr_cmd_set_errno(cmd, xerrno); |
| 859 |
errno = xerrno; |
| 860 |
return PR_ERROR(cmd); |
| 861 |
} |
| 862 |
|
| 863 |
dst = dir_canonical_path(cmd->tmp_pool, decoded_path); |
| 864 |
if (dst == NULL) { |
| 865 |
int xerrno = EINVAL; |
| 866 |
|
| 867 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 868 |
|
| 869 |
errno = xerrno; |
| 870 |
return PR_ERROR(cmd); |
| 871 |
} |
| 872 |
|
| 873 |
if (!dir_check_canon(cmd->tmp_pool, cmd, G_WRITE, dst, NULL)) { |
| 874 |
int xerrno = EPERM; |
| 875 |
|
| 876 |
cmd->argv[0] = cmd_name; |
| 877 |
|
| 878 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 879 |
": %s command denied by <Limit>", (char *) cmd->argv[0]); |
| 880 |
pr_response_add_err(R_550, "%s: %s", (char *) cmd->argv[3], |
| 881 |
strerror(xerrno)); |
| 882 |
|
| 883 |
errno = xerrno; |
| 884 |
return PR_ERROR(cmd); |
| 885 |
} |
| 886 |
cmd->argv[0] = cmd_name; |
| 887 |
|
| 888 |
if (site_misc_check_filters(cmd, dst) < 0) { |
| 889 |
int xerrno = EPERM; |
| 890 |
|
| 891 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 892 |
|
| 893 |
errno = xerrno; |
| 894 |
return PR_ERROR(cmd); |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
pr_fs_clear_cache2(src); |
| 904 |
res = pr_fsio_stat(src, &st); |
| 905 |
if (res < 0) { |
| 906 |
int xerrno = errno; |
| 907 |
|
| 908 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 909 |
": error checking '%s': %s", src, strerror(xerrno)); |
| 910 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 911 |
|
| 912 |
errno = xerrno; |
| 913 |
return PR_ERROR(cmd); |
| 914 |
} |
| 915 |
|
| 916 |
if (pr_fsio_symlink(src, dst) < 0) { |
| 917 |
int xerrno = errno; |
| 918 |
|
| 919 |
pr_log_debug(DEBUG2, MOD_SITE_MISC_VERSION |
| 920 |
": error symlinking '%s' to '%s': %s", src, dst, strerror(xerrno)); |
| 921 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 922 |
|
| 923 |
errno = xerrno; |
| 924 |
return PR_ERROR(cmd); |
| 925 |
} |
| 926 |
|
| 927 |
pr_response_add(R_200, _("SITE %s command successful"), |
| 928 |
(char *) cmd->argv[1]); |
| 929 |
return PR_HANDLED(cmd); |
| 930 |
} |
| 931 |
|
| 932 |
if (strncasecmp(cmd->argv[1], "HELP", 5) == 0) { |
| 933 |
pr_response_add(R_214, "SYMLINK <sp> source <sp> destination"); |
| 934 |
} |
| 935 |
|
| 936 |
return PR_DECLINED(cmd); |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
MODRET site_misc_utime_mtime(cmd_rec *cmd) { |
| 941 |
register unsigned int i; |
| 942 |
unsigned char *authenticated; |
| 943 |
char *cmd_name, *decoded_path, *path = ""; |
| 944 |
size_t timestamp_len; |
| 945 |
unsigned int year, month, day, hour, min, sec = 0; |
| 946 |
struct timeval tvs[2]; |
| 947 |
struct stat st; |
| 948 |
|
| 949 |
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE); |
| 950 |
if (authenticated == NULL || |
| 951 |
*authenticated == FALSE) { |
| 952 |
pr_response_add_err(R_501, _("Please login with USER and PASS")); |
| 953 |
|
| 954 |
pr_cmd_set_errno(cmd, EPERM); |
| 955 |
errno = EPERM; |
| 956 |
return PR_ERROR(cmd); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
timestamp_len = strlen(cmd->argv[2]); |
| 961 |
if (timestamp_len != 12 && |
| 962 |
timestamp_len != 14) { |
| 963 |
int xerrno = EINVAL; |
| 964 |
|
| 965 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 966 |
": wrong number of digits in timestamp argument '%s' (%lu)", |
| 967 |
(char *) cmd->argv[2], (unsigned long) timestamp_len); |
| 968 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 969 |
|
| 970 |
errno = xerrno; |
| 971 |
return PR_ERROR(cmd); |
| 972 |
} |
| 973 |
|
| 974 |
for (i = 3; i < cmd->argc; i++) { |
| 975 |
path = pstrcat(cmd->tmp_pool, path, *path ? " " : "", cmd->argv[i], NULL); |
| 976 |
} |
| 977 |
|
| 978 |
decoded_path = pr_fs_decode_path2(cmd->tmp_pool, path, |
| 979 |
FSIO_DECODE_FL_TELL_ERRORS); |
| 980 |
if (decoded_path == NULL) { |
| 981 |
int xerrno = errno; |
| 982 |
|
| 983 |
pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", path, |
| 984 |
strerror(xerrno)); |
| 985 |
pr_response_add_err(R_550, _("%s: Illegal character sequence in filename"), |
| 986 |
path); |
| 987 |
|
| 988 |
pr_cmd_set_errno(cmd, xerrno); |
| 989 |
errno = xerrno; |
| 990 |
return PR_ERROR(cmd); |
| 991 |
} |
| 992 |
|
| 993 |
if (pr_fsio_lstat(decoded_path, &st) == 0) { |
| 994 |
if (S_ISLNK(st.st_mode)) { |
| 995 |
char link_path[PR_TUNABLE_PATH_MAX]; |
| 996 |
int len; |
| 997 |
|
| 998 |
memset(link_path, '\0', sizeof(link_path)); |
| 999 |
len = dir_readlink(cmd->tmp_pool, decoded_path, link_path, |
| 1000 |
sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH); |
| 1001 |
if (len > 0) { |
| 1002 |
link_path[len] = '\0'; |
| 1003 |
decoded_path = pstrdup(cmd->tmp_pool, link_path); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
path = dir_canonical_path(cmd->tmp_pool, decoded_path); |
| 1009 |
if (path == NULL) { |
| 1010 |
int xerrno = EINVAL; |
| 1011 |
|
| 1012 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1013 |
|
| 1014 |
errno = xerrno; |
| 1015 |
return PR_ERROR(cmd); |
| 1016 |
} |
| 1017 |
|
| 1018 |
cmd_name = cmd->argv[0]; |
| 1019 |
cmd->argv[0] = "SITE_UTIME"; |
| 1020 |
if (!dir_check_canon(cmd->tmp_pool, cmd, G_WRITE, path, NULL)) { |
| 1021 |
int xerrno = EPERM; |
| 1022 |
|
| 1023 |
cmd->argv[0] = cmd_name; |
| 1024 |
|
| 1025 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 1026 |
": %s command denied by <Limit>", (char *) cmd->argv[0]); |
| 1027 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1028 |
|
| 1029 |
errno = xerrno; |
| 1030 |
return PR_ERROR(cmd); |
| 1031 |
} |
| 1032 |
cmd->argv[0] = cmd_name; |
| 1033 |
|
| 1034 |
if (site_misc_check_filters(cmd, path) < 0) { |
| 1035 |
int xerrno = EPERM; |
| 1036 |
|
| 1037 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1038 |
|
| 1039 |
errno = xerrno; |
| 1040 |
return PR_ERROR(cmd); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (site_misc_parsetime(cmd->argv[2], timestamp_len, &year, &month, &day, |
| 1044 |
&hour, &min, &sec) < 0) { |
| 1045 |
int xerrno = errno; |
| 1046 |
|
| 1047 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1048 |
|
| 1049 |
errno = xerrno; |
| 1050 |
return PR_ERROR(cmd); |
| 1051 |
} |
| 1052 |
|
| 1053 |
tvs[0].tv_usec = tvs[1].tv_usec = 0; |
| 1054 |
tvs[0].tv_sec = tvs[1].tv_sec = site_misc_mktime(year, month, day, hour, |
| 1055 |
min, sec); |
| 1056 |
|
| 1057 |
if (pr_fsio_utimes_with_root(path, tvs) < 0) { |
| 1058 |
int xerrno = errno; |
| 1059 |
|
| 1060 |
pr_log_debug(DEBUG2, MOD_SITE_MISC_VERSION |
| 1061 |
": error modifying timestamps for '%s': %s", path, strerror(xerrno)); |
| 1062 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1063 |
|
| 1064 |
errno = xerrno; |
| 1065 |
return PR_ERROR(cmd); |
| 1066 |
} |
| 1067 |
|
| 1068 |
pr_response_add(R_200, _("SITE %s command successful"), |
| 1069 |
(char *) cmd->argv[1]); |
| 1070 |
return PR_HANDLED(cmd); |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
MODRET site_misc_utime_atime_mtime_ctime(cmd_rec *cmd) { |
| 1075 |
register unsigned int i; |
| 1076 |
char *cmd_name, *decoded_path, *path = "", *timestamp; |
| 1077 |
size_t timestamp_len; |
| 1078 |
unsigned int year, month, day, hour, min, sec = 0; |
| 1079 |
time_t parsed_atime, parsed_mtime, parsed_ctime; |
| 1080 |
struct timeval tvs[2]; |
| 1081 |
|
| 1082 |
for (i = 2; i < cmd->argc-4; i++) { |
| 1083 |
path = pstrcat(cmd->tmp_pool, path, *path ? " " : "", cmd->argv[i], NULL); |
| 1084 |
} |
| 1085 |
|
| 1086 |
decoded_path = pr_fs_decode_path2(cmd->tmp_pool, path, |
| 1087 |
FSIO_DECODE_FL_TELL_ERRORS); |
| 1088 |
if (decoded_path == NULL) { |
| 1089 |
int xerrno = errno; |
| 1090 |
|
| 1091 |
pr_log_debug(DEBUG8, "'%s' failed to decode properly: %s", path, |
| 1092 |
strerror(xerrno)); |
| 1093 |
pr_response_add_err(R_550, _("%s: Illegal character sequence in filename"), |
| 1094 |
path); |
| 1095 |
|
| 1096 |
pr_cmd_set_errno(cmd, xerrno); |
| 1097 |
errno = xerrno; |
| 1098 |
return PR_ERROR(cmd); |
| 1099 |
} |
| 1100 |
|
| 1101 |
path = dir_canonical_path(cmd->tmp_pool, decoded_path); |
| 1102 |
if (path == NULL) { |
| 1103 |
int xerrno = EINVAL; |
| 1104 |
|
| 1105 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1106 |
|
| 1107 |
errno = xerrno; |
| 1108 |
return PR_ERROR(cmd); |
| 1109 |
} |
| 1110 |
|
| 1111 |
cmd_name = cmd->argv[0]; |
| 1112 |
cmd->argv[0] = "SITE_UTIME"; |
| 1113 |
if (!dir_check_canon(cmd->tmp_pool, cmd, G_WRITE, path, NULL)) { |
| 1114 |
int xerrno = EPERM; |
| 1115 |
|
| 1116 |
cmd->argv[0] = cmd_name; |
| 1117 |
|
| 1118 |
pr_log_debug(DEBUG4, MOD_SITE_MISC_VERSION |
| 1119 |
": %s command denied by <Limit>", (char *) cmd->argv[0]); |
| 1120 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1121 |
|
| 1122 |
errno = xerrno; |
| 1123 |
return PR_ERROR(cmd); |
| 1124 |
} |
| 1125 |
cmd->argv[0] = cmd_name; |
| 1126 |
|
| 1127 |
if (site_misc_check_filters(cmd, path) < 0) { |
| 1128 |
int xerrno = EPERM; |
| 1129 |
|
| 1130 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1131 |
|
| 1132 |
errno = xerrno; |
| 1133 |
return PR_ERROR(cmd); |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
timestamp = cmd->argv[cmd->argc-4]; |
| 1140 |
timestamp_len = strlen(timestamp); |
| 1141 |
if (timestamp_len != 12 && |
| 1142 |
timestamp_len != 14) { |
| 1143 |
int xerrno = EINVAL; |
| 1144 |
|
| 1145 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 1146 |
": wrong number of digits in timestamp argument '%s' (%lu)", |
| 1147 |
timestamp, (unsigned long) timestamp_len); |
| 1148 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1149 |
|
| 1150 |
errno = xerrno; |
| 1151 |
return PR_ERROR(cmd); |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (site_misc_parsetime(timestamp, timestamp_len, &year, &month, &day, |
| 1155 |
&hour, &min, &sec) < 0) { |
| 1156 |
int xerrno = errno; |
| 1157 |
|
| 1158 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1159 |
|
| 1160 |
errno = xerrno; |
| 1161 |
return PR_ERROR(cmd); |
| 1162 |
} |
| 1163 |
|
| 1164 |
parsed_atime = site_misc_mktime(year, month, day, hour, min, sec); |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
sec = 0; |
| 1171 |
timestamp = cmd->argv[cmd->argc-3]; |
| 1172 |
timestamp_len = strlen(timestamp); |
| 1173 |
if (timestamp_len != 12 && |
| 1174 |
timestamp_len != 14) { |
| 1175 |
int xerrno = EINVAL; |
| 1176 |
|
| 1177 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 1178 |
": wrong number of digits in timestamp argument '%s' (%lu)", |
| 1179 |
timestamp, (unsigned long) timestamp_len); |
| 1180 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1181 |
|
| 1182 |
errno = xerrno; |
| 1183 |
return PR_ERROR(cmd); |
| 1184 |
} |
| 1185 |
|
| 1186 |
if (site_misc_parsetime(timestamp, timestamp_len, &year, &month, &day, |
| 1187 |
&hour, &min, &sec) < 0) { |
| 1188 |
int xerrno = errno; |
| 1189 |
|
| 1190 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1191 |
|
| 1192 |
errno = xerrno; |
| 1193 |
return PR_ERROR(cmd); |
| 1194 |
} |
| 1195 |
|
| 1196 |
parsed_mtime = site_misc_mktime(year, month, day, hour, min, sec); |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
sec = 0; |
| 1203 |
timestamp = cmd->argv[cmd->argc-2]; |
| 1204 |
timestamp_len = strlen(timestamp); |
| 1205 |
if (timestamp_len != 12 && |
| 1206 |
timestamp_len != 14) { |
| 1207 |
int xerrno = EINVAL; |
| 1208 |
|
| 1209 |
pr_log_debug(DEBUG7, MOD_SITE_MISC_VERSION |
| 1210 |
": wrong number of digits in timestamp argument '%s' (%lu)", |
| 1211 |
timestamp, (unsigned long) timestamp_len); |
| 1212 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1213 |
|
| 1214 |
errno = xerrno; |
| 1215 |
return PR_ERROR(cmd); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (site_misc_parsetime(timestamp, timestamp_len, &year, &month, &day, |
| 1219 |
&hour, &min, &sec) < 0) { |
| 1220 |
int xerrno = errno; |
| 1221 |
|
| 1222 |
pr_response_add_err(R_500, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1223 |
|
| 1224 |
errno = xerrno; |
| 1225 |
return PR_ERROR(cmd); |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
parsed_ctime = site_misc_mktime(year, month, day, hour, min, sec); |
| 1233 |
pr_trace_msg("command", 9, |
| 1234 |
"SITE UTIME command sent ctime timestamp of %lu secs", |
| 1235 |
(unsigned long) parsed_ctime); |
| 1236 |
|
| 1237 |
tvs[0].tv_usec = tvs[1].tv_usec = 0; |
| 1238 |
tvs[0].tv_sec = parsed_atime; |
| 1239 |
tvs[1].tv_sec = parsed_mtime; |
| 1240 |
|
| 1241 |
if (pr_fsio_utimes_with_root(path, tvs) < 0) { |
| 1242 |
int xerrno = errno; |
| 1243 |
|
| 1244 |
pr_log_debug(DEBUG2, MOD_SITE_MISC_VERSION |
| 1245 |
": error modifying timestamps for '%s': %s", path, strerror(xerrno)); |
| 1246 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 1247 |
|
| 1248 |
errno = xerrno; |
| 1249 |
return PR_ERROR(cmd); |
| 1250 |
} |
| 1251 |
|
| 1252 |
pr_response_add(R_200, _("SITE %s command successful"), |
| 1253 |
(char *) cmd->argv[1]); |
| 1254 |
return PR_HANDLED(cmd); |
| 1255 |
} |
| 1256 |
|
| 1257 |
MODRET site_misc_utime(cmd_rec *cmd) { |
| 1258 |
if (!site_misc_engine) { |
| 1259 |
return PR_DECLINED(cmd); |
| 1260 |
} |
| 1261 |
|
| 1262 |
if (cmd->argc < 2) { |
| 1263 |
pr_log_debug(DEBUG5, MOD_SITE_MISC_VERSION |
| 1264 |
"%s : wrong number of parameters (%d)", (char *) cmd->argv[0], cmd->argc); |
| 1265 |
return PR_DECLINED(cmd); |
| 1266 |
} |
| 1267 |
|
| 1268 |
if (strncasecmp(cmd->argv[1], "UTIME", 6) == 0) { |
| 1269 |
unsigned char *authenticated; |
| 1270 |
|
| 1271 |
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE); |
| 1272 |
if (authenticated == NULL || |
| 1273 |
*authenticated == FALSE) { |
| 1274 |
pr_response_add_err(R_530, _("Please login with USER and PASS")); |
| 1275 |
errno = EACCES; |
| 1276 |
return PR_ERROR(cmd); |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
if (cmd->argc < 4) { |
| 1286 |
|
| 1287 |
pr_log_debug(DEBUG9, MOD_SITE_MISC_VERSION |
| 1288 |
": SITE UTIME command has wrong number of parameters (%d), ignoring", |
| 1289 |
cmd->argc); |
| 1290 |
return PR_DECLINED(cmd); |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
if (cmd->argc >= 7 && |
| 1298 |
strncasecmp(cmd->argv[cmd->argc-1], "UTC", 4) == 0) { |
| 1299 |
return site_misc_utime_atime_mtime_ctime(cmd); |
| 1300 |
} |
| 1301 |
|
| 1302 |
return site_misc_utime_mtime(cmd); |
| 1303 |
} |
| 1304 |
|
| 1305 |
if (strncasecmp(cmd->argv[1], "HELP", 5) == 0) { |
| 1306 |
pr_response_add(R_214, "UTIME <sp> YYYYMMDDhhmm[ss] <sp> path"); |
| 1307 |
} |
| 1308 |
|
| 1309 |
return PR_DECLINED(cmd); |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
static void site_misc_sess_reinit_ev(const void *event_data, void *user_data) { |
| 1316 |
int res; |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
pr_event_unregister(&site_misc_module, "core.session-reinit", |
| 1321 |
site_misc_sess_reinit_ev); |
| 1322 |
|
| 1323 |
site_misc_engine = TRUE; |
| 1324 |
pr_feat_remove("SITE MKDIR"); |
| 1325 |
pr_feat_remove("SITE RMDIR"); |
| 1326 |
pr_feat_remove("SITE SYMLINK"); |
| 1327 |
pr_feat_remove("SITE UTIME"); |
| 1328 |
|
| 1329 |
res = site_misc_sess_init(); |
| 1330 |
if (res < 0) { |
| 1331 |
pr_session_disconnect(&site_misc_module, |
| 1332 |
PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
static int site_misc_sess_init(void) { |
| 1340 |
config_rec *c; |
| 1341 |
|
| 1342 |
pr_event_register(&site_misc_module, "core.session-reinit", |
| 1343 |
site_misc_sess_reinit_ev, NULL); |
| 1344 |
|
| 1345 |
c = find_config(main_server->conf, CONF_PARAM, "SiteMiscEngine", FALSE); |
| 1346 |
if (c) { |
| 1347 |
site_misc_engine = *((unsigned int *) c->argv[0]); |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (!site_misc_engine) { |
| 1351 |
return 0; |
| 1352 |
} |
| 1353 |
|
| 1354 |
|
| 1355 |
pr_feat_add("SITE MKDIR"); |
| 1356 |
pr_feat_add("SITE RMDIR"); |
| 1357 |
pr_feat_add("SITE SYMLINK"); |
| 1358 |
pr_feat_add("SITE UTIME"); |
| 1359 |
|
| 1360 |
return 0; |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
static conftable site_misc_conftab[] = { |
| 1367 |
{ "SiteMiscEngine", set_sitemiscengine, NULL }, |
| 1368 |
{ NULL } |
| 1369 |
}; |
| 1370 |
|
| 1371 |
static cmdtable site_misc_cmdtab[] = { |
| 1372 |
{ CMD, C_SITE, G_WRITE, site_misc_mkdir, FALSE, FALSE, CL_MISC }, |
| 1373 |
{ CMD, C_SITE, G_WRITE, site_misc_rmdir, FALSE, FALSE, CL_MISC }, |
| 1374 |
{ CMD, C_SITE, G_WRITE, site_misc_symlink, FALSE, FALSE, CL_MISC }, |
| 1375 |
{ CMD, C_SITE, G_WRITE, site_misc_utime, FALSE, FALSE, CL_MISC }, |
| 1376 |
{ 0, NULL } |
| 1377 |
}; |
| 1378 |
|
| 1379 |
module site_misc_module = { |
| 1380 |
NULL, NULL, |
| 1381 |
|
| 1382 |
|
| 1383 |
0x20, |
| 1384 |
|
| 1385 |
|
| 1386 |
"site_misc", |
| 1387 |
|
| 1388 |
|
| 1389 |
site_misc_conftab, |
| 1390 |
|
| 1391 |
|
| 1392 |
site_misc_cmdtab, |
| 1393 |
|
| 1394 |
|
| 1395 |
NULL, |
| 1396 |
|
| 1397 |
|
| 1398 |
NULL, |
| 1399 |
|
| 1400 |
|
| 1401 |
site_misc_sess_init, |
| 1402 |
|
| 1403 |
|
| 1404 |
MOD_SITE_MISC_VERSION |
| 1405 |
}; |