Advertisement
Guest User

Untitled

a guest
May 24th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 64.34 KB | None | 0 0
  1.  
  2. #include "mystring.h"
  3. #include "login.h"
  4. #include "logging.h"
  5. #include "dirlist.h"
  6. #include "options.h"
  7. #include "main.h"
  8. #include "targzip.h"
  9. #include "cwd.h"
  10. #include "bftpdutmp.h"
  11. #include "md5.h"
  12.  
  13. int state = STATE_CONNECTED;
  14. char user[USERLEN + 1];
  15. struct sockaddr_in sa;
  16. char pasv = 0;
  17. int sock;
  18. int pasvsock;
  19. char *philename = NULL;
  20. unsigned long offset = 0;
  21. short int xfertype = TYPE_BINARY;
  22. int ratio_send = 1, ratio_recv = 1;
  23. /* long unsigned bytes_sent = 0, bytes_recvd = 0; */
  24. double bytes_sent = 0.0, bytes_recvd = 0.0;
  25. int epsvall = 0;
  26. int xfer_bufsize;
  27.  
  28. void control_printf(char success, char *format, ...)
  29. {
  30. char buffer[MAX_STRING_LENGTH];
  31. va_list val;
  32. va_start(val, format);
  33. vsnprintf(buffer, sizeof(buffer), format, val);
  34. va_end(val);
  35. fprintf(stderr, "%s\r\n", buffer);
  36. replace(buffer, "\r", "", MAX_STRING_LENGTH);
  37. bftpd_statuslog(3, success, "%s", buffer);
  38. }
  39.  
  40. void new_umask()
  41. {
  42. int um;
  43. unsigned long get_um;
  44. char *foo = config_getoption("UMASK");
  45. if (!foo[0])
  46. um = 022;
  47. else
  48. {
  49. get_um = strtoul(foo, NULL, 8);
  50. if (get_um <= INT_MAX)
  51. um = get_um;
  52. else
  53. {
  54. bftpd_log("Error with umask value. Setting to 022.\n", 0);
  55. um = 022;
  56. }
  57. }
  58. umask(um);
  59. }
  60.  
  61. void prepare_sock(int sock)
  62. {
  63. int on = 1;
  64. #ifdef TCP_NODELAY
  65. setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &on, sizeof(on));
  66. #endif
  67. #ifdef TCP_NOPUSH
  68. setsockopt(sock, IPPROTO_TCP, TCP_NOPUSH, (void *) &on, sizeof(on));
  69. #endif
  70. #ifdef SO_REUSEADDR
  71. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on));
  72. #endif
  73. #ifdef SO_REUSEPORT
  74. setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *) &on, sizeof(on));
  75. #endif
  76. #ifdef SO_SNDBUF
  77. on = 65536;
  78. setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &on, sizeof(on));
  79. #endif
  80. }
  81.  
  82. int dataconn()
  83. {
  84. struct sockaddr foo;
  85. struct sockaddr_in local;
  86. socklen_t namelen = sizeof(foo);
  87. int curuid = geteuid();
  88.  
  89. memset(&foo, 0, sizeof(foo));
  90. memset(&local, 0, sizeof(local));
  91.  
  92. if (pasv) {
  93. sock = accept(pasvsock, (struct sockaddr *) &foo, (socklen_t *) &namelen);
  94. if (sock == -1) {
  95. control_printf(SL_FAILURE, "425-Unable to accept data connection.\r\n425 %s.",
  96. strerror(errno));
  97. return 1;
  98. }
  99. close(pasvsock);
  100. prepare_sock(sock);
  101. } else {
  102. sock = socket(AF_INET, SOCK_STREAM, 0);
  103. prepare_sock(sock);
  104. local.sin_addr.s_addr = name.sin_addr.s_addr;
  105. local.sin_family = AF_INET;
  106. if (!strcasecmp(config_getoption("DATAPORT20"), "yes")) {
  107. seteuid(0);
  108. local.sin_port = htons(20);
  109. }
  110. if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0) {
  111. control_printf(SL_FAILURE, "425-Unable to bind data socket.\r\n425 %s.",
  112. strerror(errno));
  113. return 1;
  114. }
  115. if (!strcasecmp(config_getoption("DATAPORT20"), "yes"))
  116. seteuid(curuid);
  117. sa.sin_family = AF_INET;
  118. if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
  119. control_printf(SL_FAILURE, "425-Unable to establish data connection.\r\n"
  120. "425 %s.", strerror(errno));
  121. return 1;
  122. }
  123. }
  124. control_printf(SL_SUCCESS, "150 %s data connection established.",
  125. xfertype == TYPE_BINARY ? "BINARY" : "ASCII");
  126. return 0;
  127. }
  128.  
  129. void init_userinfo()
  130. {
  131. #ifndef NO_GETPWNAM
  132. struct passwd *temp = getpwnam(user);
  133. if (temp) {
  134. userinfo.pw_name = strdup(temp->pw_name);
  135. userinfo.pw_passwd = strdup(temp->pw_passwd);
  136. userinfo.pw_uid = temp->pw_uid;
  137. userinfo.pw_gid = temp->pw_gid;
  138. userinfo.pw_gecos = strdup(temp->pw_gecos);
  139. userinfo.pw_dir = strdup(temp->pw_dir);
  140. userinfo.pw_shell = strdup(temp->pw_shell);
  141. userinfo_set = 1;
  142. }
  143. #endif
  144. }
  145.  
  146. void command_user(char *username)
  147. {
  148. char *alias;
  149. if (state) {
  150. control_printf(SL_FAILURE, "503 Username already given.");
  151. return;
  152. }
  153. mystrncpy(user, username, sizeof(user) - 1);
  154. userinfo_set = 1; /* Dirty! */
  155. alias = (char *) config_getoption("ALIAS");
  156. userinfo_set = 0;
  157. if (alias[0] != '\0')
  158. mystrncpy(user, alias, sizeof(user) - 1);
  159. init_userinfo();
  160. userinfo_set = 1; /* just in case we missed it */
  161. #ifdef DEBUG
  162. bftpd_log("Trying to log in as %s.\n", user);
  163. #endif
  164. expand_groups();
  165. if (!strcasecmp(config_getoption("ANONYMOUS_USER"), "yes"))
  166. {
  167. state = STATE_USER;
  168. control_printf(SL_SUCCESS, "331 Password please.");
  169. /* bftpd_login(""); */
  170. }
  171. else {
  172. state = STATE_USER;
  173. control_printf(SL_SUCCESS, "331 Password please.");
  174. }
  175. }
  176.  
  177. void command_pass(char *password)
  178. {
  179. if (state > STATE_USER) {
  180. control_printf(SL_FAILURE, "503 Already logged in.");
  181. return;
  182. }
  183. if (bftpd_login(password)) {
  184. bftpd_log("Login as user '%s' failed.\n", user);
  185. control_printf(SL_FAILURE, "530 Login incorrect.");
  186. // exit(0);
  187. state = STATE_CONNECTED;
  188. return;
  189. }
  190. }
  191.  
  192. void command_pwd(char *params)
  193. {
  194. char *my_cwd = NULL;
  195.  
  196. my_cwd = bftpd_cwd_getcwd();
  197. if (my_cwd)
  198. {
  199. control_printf(SL_SUCCESS, "257 \"%s\" is the current working directory.", my_cwd);
  200. free(my_cwd);
  201. }
  202. }
  203.  
  204.  
  205. void command_type(char *params)
  206. {
  207. if ((*params == 'A') || (*params == 'a')) {
  208. control_printf(SL_SUCCESS, "200 Transfer type changed to ASCII");
  209. xfertype = TYPE_ASCII;
  210. } else if ((*params == 'I') || (*params == 'i')) {
  211. control_printf(SL_SUCCESS, "200 Transfer type changed to BINARY");
  212. xfertype = TYPE_BINARY;
  213. } else
  214. control_printf(SL_FAILURE, "500 Type '%c' not supported.", *params);
  215. }
  216.  
  217. void command_port(char *params) {
  218. unsigned long a0, a1, a2, a3, p0, p1, addr;
  219. if (epsvall) {
  220. control_printf(SL_FAILURE, "500 EPSV ALL has been called.");
  221. return;
  222. }
  223. sscanf(params, "%lu,%lu,%lu,%lu,%lu,%lu", &a0, &a1, &a2, &a3, &p0, &p1);
  224. addr = htonl((a0 << 24) + (a1 << 16) + (a2 << 8) + a3);
  225. if((addr != remotename.sin_addr.s_addr) &&( strncasecmp(config_getoption("ALLOW_FXP"), "yes", 3))) {
  226. control_printf(SL_FAILURE, "500 The given address is not yours.");
  227. return;
  228. }
  229. sa.sin_addr.s_addr = addr;
  230. sa.sin_port = htons((p0 << 8) + p1);
  231. if (pasv) {
  232. close(sock);
  233. pasv = 0;
  234. }
  235. control_printf(SL_SUCCESS, "200 PORT %lu.%lu.%lu.%lu:%lu OK",
  236. a0, a1, a2, a3, (p0 << 8) + p1);
  237. }
  238.  
  239. void command_eprt(char *params) {
  240. char delim;
  241. int af;
  242. char addr[51];
  243. char foo[20];
  244. int port;
  245. if (epsvall) {
  246. control_printf(SL_FAILURE, "500 EPSV ALL has been called.");
  247. return;
  248. }
  249. if (strlen(params) < 5) {
  250. control_printf(SL_FAILURE, "500 Syntax error.");
  251. return;
  252. }
  253. delim = params[0];
  254. sprintf(foo, "%c%%i%c%%50[^%c]%c%%i%c", delim, delim, delim, delim, delim);
  255. if (sscanf(params, foo, &af, addr, &port) < 3) {
  256. control_printf(SL_FAILURE, "500 Syntax error.");
  257. return;
  258. }
  259. if (af != 1) {
  260. control_printf(SL_FAILURE, "522 Protocol unsupported, use (1)");
  261. return;
  262. }
  263. sa.sin_addr.s_addr = inet_addr(addr);
  264. if ((sa.sin_addr.s_addr != remotename.sin_addr.s_addr) && (strncasecmp(config_getoption("ALLOW_FXP"), "yes", 3))) {
  265. control_printf(SL_FAILURE, "500 The given address is not yours.");
  266. return;
  267. }
  268. sa.sin_port = htons(port);
  269. if (pasv) {
  270. close(sock);
  271. pasv = 0;
  272. }
  273. control_printf(SL_FAILURE, "200 EPRT %s:%i OK", addr, port);
  274. }
  275.  
  276. void command_pasv(char *foo)
  277. {
  278. int a1, a2, a3, a4;
  279. socklen_t namelen;
  280. struct sockaddr_in localsock;
  281. char *my_override_ip;
  282.  
  283. if (epsvall) {
  284. control_printf(SL_FAILURE, "500 EPSV ALL has been called.");
  285. return;
  286. }
  287. pasvsock = socket(AF_INET, SOCK_STREAM, 0);
  288. sa.sin_addr.s_addr = INADDR_ANY;
  289. sa.sin_family = AF_INET;
  290.  
  291. if (!config_getoption("PASSIVE_PORTS") || !strlen(config_getoption("PASSIVE_PORTS"))) {
  292. /* bind to any port */
  293. sa.sin_port = 0;
  294. if (bind(pasvsock, (struct sockaddr *) &sa, sizeof(sa)) == -1)
  295. {
  296. control_printf(SL_FAILURE, "425-Error: Unable to bind data socket.\r\n425 %s", strerror(errno));
  297. return;
  298. }
  299. }
  300.  
  301. else {
  302. int i = 0, success = 0, port;
  303. for (;;) {
  304. port = int_from_list(config_getoption("PASSIVE_PORTS"), i++);
  305. if (port < 0)
  306. break;
  307. sa.sin_port = htons(port);
  308. if (bind(pasvsock, (struct sockaddr *) &sa, sizeof(sa)) == 0) {
  309. success = 1;
  310. #ifdef DEBUG
  311. bftpd_log("Passive mode: Successfully bound port %d\n", port);
  312. #endif
  313. break;
  314. }
  315. } /* end of for loop */
  316. if (!success) {
  317. control_printf(SL_FAILURE, "425 Error: Unable to bind data socket.");
  318. return;
  319. }
  320. prepare_sock(pasvsock);
  321. } /* end of else using list of ports */
  322.  
  323. if (listen(pasvsock, 1)) {
  324. control_printf(SL_FAILURE, "425-Error: Unable to make socket listen.\r\n425 %s",
  325. strerror(errno));
  326. return;
  327. }
  328. namelen = sizeof(localsock);
  329. getsockname(pasvsock, (struct sockaddr *) &localsock, (socklen_t *) &namelen);
  330.  
  331. /* see if we should over-ride the IP address sent to the client */
  332. my_override_ip = config_getoption("OVERRIDE_IP");
  333. if (my_override_ip[0])
  334. {
  335. sscanf( my_override_ip, "%i.%i.%i.%i",
  336. &a1, &a2, &a3, &a4);
  337. }
  338. else /* noraml, no over-ride */
  339. {
  340. sscanf((char *) inet_ntoa(name.sin_addr), "%i.%i.%i.%i",
  341. &a1, &a2, &a3, &a4);
  342. }
  343.  
  344. control_printf(SL_SUCCESS, "227 Entering Passive Mode (%i,%i,%i,%i,%i,%i)", a1, a2, a3, a4,
  345. ntohs(localsock.sin_port) >> 8, ntohs(localsock.sin_port) & 0xFF);
  346. pasv = 1;
  347. }
  348.  
  349. void command_epsv(char *params)
  350. {
  351. struct sockaddr_in localsock;
  352. socklen_t namelen;
  353. int af;
  354. if (params[0]) {
  355. if (!strncasecmp(params, "ALL", 3))
  356. epsvall = 1;
  357. else {
  358. if (sscanf(params, "%i", &af) < 1) {
  359. control_printf(SL_FAILURE, "500 Syntax error.");
  360. return;
  361. } else {
  362. if (af != 1) {
  363. control_printf(SL_FAILURE, "522 Protocol unsupported, use (1)");
  364. return;
  365. }
  366. }
  367. }
  368. }
  369. pasvsock = socket(AF_INET, SOCK_STREAM, 0);
  370. sa.sin_addr.s_addr = INADDR_ANY;
  371. sa.sin_port = 0;
  372. sa.sin_family = AF_INET;
  373. if (bind(pasvsock, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
  374. control_printf(SL_FAILURE, "500-Error: Unable to bind data socket.\r\n425 %s",
  375. strerror(errno));
  376. return;
  377. }
  378. if (listen(pasvsock, 1)) {
  379. control_printf(SL_FAILURE, "500-Error: Unable to make socket listen.\r\n425 %s",
  380. strerror(errno));
  381. return;
  382. }
  383. namelen = sizeof(localsock);
  384. getsockname(pasvsock, (struct sockaddr *) &localsock, (socklen_t *) &namelen);
  385. control_printf(SL_SUCCESS, "229 Entering extended passive mode (|||%i|)",
  386. ntohs(localsock.sin_port));
  387. pasv = 1;
  388. }
  389.  
  390. char test_abort(char selectbefore, int file, int sock)
  391. {
  392. char str[256];
  393. fd_set rfds;
  394. struct timeval tv;
  395. char *result;
  396.  
  397. if (selectbefore) {
  398. tv.tv_sec = 0;
  399. tv.tv_usec = 0;
  400. FD_ZERO(&rfds);
  401. FD_SET(fileno(stdin), &rfds);
  402. if (!select(fileno(stdin) + 1, &rfds, NULL, NULL, &tv))
  403. return 0;
  404. }
  405.  
  406. result = fgets(str, sizeof(str), stdin);
  407. if ( (result) && (strstr(str, "ABOR")) ) {
  408. control_printf(SL_SUCCESS, "426 Transfer aborted.");
  409. close(file);
  410. close(sock);
  411. control_printf(SL_SUCCESS, "226 Aborted.");
  412. bftpd_log("Client aborted file transmission.\n");
  413. alarm(control_timeout);
  414. return 1;
  415. }
  416. return 0;
  417. }
  418.  
  419. void command_allo(char *foo)
  420. {
  421. command_noop(foo);
  422. }
  423.  
  424.  
  425. /* This function allows the storage of multiple files on the server. */
  426. void command_mput(char *filenames)
  427. {
  428. char filename[MAXCMD]; /* single filename */
  429. int from_index, to_index; /* position in "filenames" and "filename" */
  430.  
  431. from_index = 0; /* start at begining of filenames */
  432. memset(filename, 0, MAXCMD); /* clear filename */
  433. to_index = 0;
  434.  
  435. /* go until we find a NULL character */
  436. while ( filenames[from_index] > 0)
  437. {
  438. /* copy filename until we hit a space */
  439. if (filenames[from_index] == ' ')
  440. {
  441. /* got a full filename */
  442. command_stor(filename);
  443. /* clear filename and reset to_index */
  444. to_index = 0;
  445. memset(filename, 0, MAXCMD);
  446.  
  447. while (filenames[from_index] == ' ')
  448. from_index++; /* goto next position */
  449. }
  450.  
  451. /* if we haven't hit a space, then copy the letter */
  452. else
  453. {
  454. filename[to_index] = filenames[from_index];
  455. to_index++;
  456. from_index++;
  457. /* if the next character is a NULL, then this is the end of the filename */
  458. if (! filenames[from_index])
  459. {
  460. command_stor(filename); /* get the file */
  461. to_index = 0; /* reset filename index */
  462. memset(filename, 0, MAXCMD); /* clear filename buffer */
  463. from_index++; /* goto next character */
  464. }
  465. }
  466.  
  467. /* if the buffer is getting too big, then stop */
  468. if (to_index > (MAXCMD - 2) )
  469. {
  470. bftpd_log("Error: Filename in '%s' too long.\n", filenames);
  471. return;
  472. }
  473.  
  474. } /* end of while */
  475.  
  476. }
  477.  
  478.  
  479.  
  480. void do_stor(char *filename, int flags)
  481. {
  482. char *buffer;
  483. int fd, i, max;
  484. fd_set rfds;
  485. struct timeval tv;
  486. char *p, *pp;
  487. char *mapped = bftpd_cwd_mappath(filename);
  488.  
  489. int my_buffer_size; /* total transfer buffer size divided by number of clients */
  490. int num_clients = 1; /* number of clients connected to the server */
  491. int new_num_clients = 1;
  492. int xfer_delay;
  493. int attempt_gzip = FALSE;
  494. unsigned long get_value;
  495. int change_buffer_size = FALSE;
  496. int stdin_fileno;
  497. int write_result;
  498. #ifdef HAVE_ZLIB_H
  499. gzFile my_zip_file = NULL;
  500. #endif
  501.  
  502. if (pre_write_script)
  503. run_script(pre_write_script, mapped);
  504.  
  505. #ifdef HAVE_ZLIB_H
  506. if (! strcmp( config_getoption("GZ_UPLOAD"), "yes") )
  507. {
  508. attempt_gzip = TRUE;
  509. strcat(mapped, ".gz");
  510. }
  511. else
  512. attempt_gzip = FALSE;
  513. #endif
  514.  
  515. /* See if we should delay between data transfers */
  516. get_value = strtoul( config_getoption("XFER_DELAY"), NULL, 0);
  517. if (get_value <= INT_MAX)
  518. xfer_delay = get_value;
  519. else
  520. {
  521. bftpd_log("Error getting xfer_delay in do_stor().\n", 0);
  522. xfer_delay = 0;
  523. }
  524.  
  525. /* Check to see if the file exists and if we can over-write
  526. it, if it does. -- Jesse */
  527. fd = open(mapped, O_RDONLY);
  528. if (fd >= 0) /* file exists */
  529. {
  530. /* close the file */
  531. close(fd);
  532. /* check if we can over-write it */
  533. if ( !strcasecmp( config_getoption("ALLOWCOMMAND_DELE"), "no") )
  534. {
  535. bftpd_log("Not allowed to over-write '%s'.\n", filename);
  536. control_printf(SL_FAILURE,
  537. "553 Error: Remote file is write protected.");
  538.  
  539. free(mapped);
  540. close(sock);
  541. return;
  542. }
  543. }
  544.  
  545. if (! attempt_gzip)
  546. {
  547. fd = open(mapped, flags, 00666);
  548. /*
  549. do this below
  550. if (mapped)
  551. free(mapped);
  552. */
  553. if (fd == -1) {
  554. bftpd_log("Error: '%s' while trying to store file '%s'.\n",
  555. strerror(errno), filename);
  556. control_printf(SL_FAILURE, "553 Error: %s.", strerror(errno));
  557.  
  558. close(fd); /* make sure it is not open */
  559. if (post_write_script)
  560. run_script(post_write_script, mapped);
  561. free(mapped);
  562. return;
  563. }
  564. }
  565.  
  566. #ifdef HAVE_ZLIB_H
  567. if ( attempt_gzip )
  568. {
  569. my_zip_file = gzopen(mapped, "wb+");
  570. if (mapped)
  571. {
  572. free(mapped);
  573. mapped = NULL;
  574. }
  575. if (! my_zip_file)
  576. {
  577. control_printf(SL_FAILURE, "553 Error: An error occured creating compressed file.");
  578. close(sock);
  579. close(fd);
  580. return;
  581. }
  582. }
  583. #endif
  584.  
  585. bftpd_log("Client is storing file '%s'.\n", filename);
  586. if (dataconn())
  587. {
  588. close(fd);
  589. if (post_write_script)
  590. run_script(post_write_script, mapped);
  591. if (mapped)
  592. free(mapped);
  593. return;
  594. }
  595.  
  596.  
  597. /* decide if the transfer buffer size should change. */
  598. if (! strcasecmp( config_getoption("CHANGE_BUFSIZE"), "yes") )
  599. change_buffer_size = TRUE;
  600.  
  601. /* Figure out how big the transfer buffer should be.
  602. This will be the total size divided by the number of clients connected.
  603. -- Jesse
  604. */
  605. if (change_buffer_size)
  606. {
  607. num_clients = bftpdutmp_usercount("*");
  608. my_buffer_size = get_buffer_size(num_clients);
  609. }
  610. else
  611. my_buffer_size = xfer_bufsize;
  612.  
  613. alarm(0);
  614. buffer = malloc(xfer_bufsize);
  615. /* Check to see if we are out of memory. -- Jesse */
  616. if (! buffer)
  617. {
  618. bftpd_log("Unable to create buffer to receive file.\n", 0);
  619. control_printf(SL_FAILURE, "553 Error: An unknown error occured on the server.");
  620. if (fd >= 0)
  621. close(fd);
  622. close(sock);
  623. if (mapped)
  624. free(mapped);
  625. return;
  626. }
  627.  
  628. lseek(fd, offset, SEEK_SET);
  629. offset = 0;
  630. /* Do not use the whole buffer, because a null byte has to be
  631. * written after the string in ASCII mode. */
  632. stdin_fileno = fileno(stdin);
  633. max = (sock > stdin_fileno ? sock : stdin_fileno) + 1;
  634. for (;;) /* start receiving loop */
  635. {
  636. FD_ZERO(&rfds);
  637. FD_SET(sock, &rfds);
  638. FD_SET( stdin_fileno, &rfds);
  639.  
  640. tv.tv_sec = data_timeout;
  641. tv.tv_usec = 0;
  642. if (!select(max, &rfds, NULL, NULL, &tv)) {
  643. close(sock);
  644. close(fd);
  645. control_printf(SL_FAILURE, "426 Kicked due to data transmission timeout.");
  646. bftpd_log("Kicked due to data transmission timeout.\n");
  647. /* Before we exit, let's remove our entry in the log file. -- Jesse */
  648. if (post_write_script)
  649. run_script(post_write_script, mapped);
  650.  
  651. bftpdutmp_end();
  652. // Update_Send_Recv(user, bytes_sent, bytes_recvd);
  653. exit(0);
  654. }
  655. if (FD_ISSET(stdin_fileno, &rfds)) {
  656. test_abort(0, fd, sock);
  657. if (buffer)
  658. free(buffer);
  659. close(fd);
  660. if (post_write_script)
  661. run_script(post_write_script, mapped);
  662. free(mapped);
  663. return;
  664. }
  665.  
  666. if (!((i = recv(sock, buffer, my_buffer_size - 1, 0))))
  667. break;
  668. bytes_recvd += i;
  669. if (xfertype == TYPE_ASCII) {
  670. buffer[i] = '\0';
  671. /* on ASCII stransfer, strip character 13 */
  672. p = pp = buffer;
  673. while (*p) {
  674. if ((unsigned char) *p == 13)
  675. p++;
  676. else
  677. *pp++ = *p++;
  678. }
  679. *pp++ = 0;
  680. i = strlen(buffer);
  681. } // end of if ASCII type transfer
  682.  
  683. #ifdef HAVE_ZLIB_H
  684. if (my_zip_file)
  685. gzwrite( my_zip_file, buffer, i );
  686. #endif
  687. if(! attempt_gzip)
  688. {
  689. write_result = write(fd, buffer, i);
  690. if (write_result == -1)
  691. break;
  692. }
  693.  
  694. /* Check to see if our bandwidth usage should change. -- Jesse */
  695. if (change_buffer_size)
  696. {
  697. new_num_clients = bftpdutmp_usercount("*");
  698. if (new_num_clients != num_clients)
  699. {
  700. num_clients = new_num_clients;
  701. my_buffer_size = get_buffer_size(num_clients);
  702. }
  703. }
  704.  
  705. /* check for transfer delay */
  706. if ( xfer_delay )
  707. {
  708. struct timeval wait_time;
  709.  
  710. wait_time.tv_sec = 0;
  711. wait_time.tv_usec = xfer_delay;
  712. select( 0, NULL, NULL, NULL, &wait_time);
  713. }
  714.  
  715.  
  716. } // end of for loop, reading
  717.  
  718. free(buffer);
  719. #ifdef HAVE_ZLIB_H
  720. gzclose(my_zip_file);
  721. #else
  722. close(fd);
  723. #endif
  724.  
  725. close(sock);
  726. alarm(control_timeout);
  727. offset = 0;
  728. control_printf(SL_SUCCESS, "226 File transmission successful.");
  729. bftpd_log("File transmission successful.\n");
  730. if (post_write_script)
  731. run_script(post_write_script, mapped);
  732.  
  733. if (mapped)
  734. free(mapped);
  735. // Update_Send_Recv(user, bytes_sent, bytes_recvd);
  736. }
  737.  
  738. void command_stor(char *filename)
  739. {
  740. do_stor(filename, O_CREAT | O_WRONLY | O_TRUNC);
  741. }
  742.  
  743. void command_appe(char *filename)
  744. {
  745. do_stor(filename, O_CREAT | O_WRONLY | O_APPEND);
  746. }
  747.  
  748.  
  749.  
  750.  
  751. /* Send multpile files to the client. */
  752. void command_mget(char *filenames)
  753. {
  754. char filename[MAXCMD]; /* single filename */
  755. int from_index, to_index; /* position in "filenames" and "filename" */
  756.  
  757. from_index = 0; /* start at begining of filenames */
  758. memset(filename, 0, MAXCMD); /* clear filename */
  759. to_index = 0;
  760.  
  761. /* go until we find a NULL character */
  762. while ( filenames[from_index] > 0)
  763. {
  764. /* copy filename until we hit a space */
  765. if (filenames[from_index] == ' ')
  766. {
  767. /* got a full filename */
  768. command_retr(filename);
  769. /* clear filename and reset to_index */
  770. to_index = 0;
  771. memset(filename, 0, MAXCMD);
  772.  
  773. while (filenames[from_index] == ' ')
  774. from_index++; /* goto next position */
  775. }
  776.  
  777. /* if we haven't hit a space, then copy the letter */
  778. else
  779. {
  780. filename[to_index] = filenames[from_index];
  781. to_index++;
  782. from_index++;
  783. /* if the next character is a NULL, then this is the end of the filename */
  784. if (! filenames[from_index])
  785. {
  786. command_retr(filename); /* send the file */
  787. to_index = 0; /* reset filename index */
  788. memset(filename, 0, MAXCMD); /* clear filename buffer */
  789. from_index++; /* goto next character */
  790. }
  791. }
  792.  
  793. /* if the buffer is getting too big, then stop */
  794. if (to_index > (MAXCMD - 2) )
  795. {
  796. bftpd_log("Error: Filename in '%s' too long.\n", filenames);
  797. return;
  798. }
  799.  
  800. } /* end of while */
  801.  
  802. }
  803.  
  804. void command_retr(char *filename)
  805. {
  806. int num_clients = 1;
  807. int new_num_clients = 1; /* number of connectiosn to the server */
  808. int my_buffer_size; /* size of the transfer buffer to use */
  809. char *mapped = NULL;
  810. char *buffer;
  811. int xfer_delay;
  812. struct timeval wait_time;
  813. unsigned long get_value;
  814. ssize_t send_status;
  815. int change_buffer_size = FALSE;
  816.  
  817. #if (defined(WANT_GZIP) || defined(HAVE_ZLIB_H))
  818. gzFile gzfile;
  819. #endif
  820. int phile;
  821. int i, whattodo = DO_NORMAL;
  822. struct stat statbuf;
  823. #if (defined(WANT_TAR) && defined(WANT_GZIP))
  824. int filedes[2];
  825. #endif
  826. #if (defined(WANT_TAR) || defined(WANT_GZIP))
  827. char *foo;
  828. #endif
  829. #ifdef WANT_TAR
  830. char *argv[4];
  831. #endif
  832.  
  833. get_value = strtoul( config_getoption("XFER_DELAY"), NULL, 0);
  834. if (get_value <= INT_MAX)
  835. xfer_delay = get_value;
  836. else
  837. {
  838. bftpd_log("Error getting XFER_DELAY in command_retr().\n", 0);
  839. xfer_delay = 0;
  840. }
  841.  
  842. mapped = bftpd_cwd_mappath(filename);
  843. if (! mapped)
  844. {
  845. bftpd_log("Memory error in sending file.\n", 0);
  846. control_printf(SL_FAILURE, "553 An unknown error occured on the server.", 9);
  847. return;
  848. }
  849.  
  850. if (! strcasecmp( config_getoption("CHANGE_BUFSIZE"), "yes") )
  851. change_buffer_size = TRUE;
  852.  
  853. phile = open(mapped, O_RDONLY);
  854. if (phile == -1) { // failed to open a file
  855. #if (defined(WANT_TAR) && defined(WANT_GZIP))
  856. if ((foo = strstr(filename, ".tar.gz")))
  857. if (strlen(foo) == 7) {
  858. whattodo = DO_TARGZ;
  859. *foo = '\0';
  860. }
  861. #endif
  862. #ifdef WANT_TAR
  863. if ((foo = strstr(filename, ".tar")))
  864. if (strlen(foo) == 4) {
  865. whattodo = DO_TARONLY;
  866. *foo = '\0';
  867. }
  868. #endif
  869. #ifdef WANT_GZIP
  870. if ((foo = strstr(filename, ".gz")))
  871. if (strlen(foo) == 3) {
  872. whattodo = DO_GZONLY;
  873. *foo = '\0';
  874. }
  875. #endif
  876. if (whattodo == DO_NORMAL) {
  877. bftpd_log("Error: '%s' while trying to receive file '%s'.\n",
  878. strerror(errno), filename);
  879. control_printf(SL_FAILURE, "553 Error: %s.", strerror(errno));
  880. if (mapped)
  881. free(mapped);
  882. return;
  883. }
  884. }
  885.  
  886. #ifdef HAVE_ZLIB_H
  887. else // we did open a file
  888. {
  889. char *my_temp;
  890. char *zip_option;
  891.  
  892. my_temp = strstr(filename, ".gz");
  893. zip_option = config_getoption("GZ_DOWNLOAD");
  894. if (my_temp)
  895. {
  896. if ( ( strlen(my_temp) == 3) && (! strcasecmp(zip_option, "yes") ) )
  897. whattodo = DO_GZUNZIP;
  898. }
  899. }
  900. #endif
  901.  
  902. stat(mapped, (struct stat *) &statbuf);
  903. if (S_ISDIR(statbuf.st_mode)) {
  904. control_printf(SL_FAILURE, "550 Error: Is a directory.");
  905. if (mapped)
  906. free(mapped);
  907. return;
  908. }
  909.  
  910. if ((((statbuf.st_size - offset) * ratio_send) / ratio_recv > bytes_recvd
  911. - bytes_sent) && (strcmp((char *) config_getoption("RATIO"), "none"))) {
  912. bftpd_log("Error: 'File too big (ratio)' while trying to receive file "
  913. "'%s'.\n", filename);
  914. control_printf(SL_FAILURE, "553 File too big. Send at least %lf bytes first.",
  915. (double) (((statbuf.st_size - offset) * ratio_send) / ratio_recv)
  916. - bytes_recvd);
  917. if (mapped)
  918. free(mapped);
  919. return;
  920. }
  921. bftpd_log("Client is receiving file '%s'.\n", filename);
  922. switch (whattodo) {
  923. #if (defined(WANT_TAR) && defined(WANT_GZIP))
  924. case DO_TARGZ:
  925. close(phile);
  926. if (dataconn()) {
  927. if (mapped)
  928. free(mapped);
  929. return;
  930. }
  931. alarm(0);
  932. pipe(filedes);
  933. if (fork()) {
  934. buffer = malloc(xfer_bufsize);
  935. /* check to make sure alloc worked */
  936. if (! buffer)
  937. {
  938. if (mapped)
  939. free(mapped);
  940. bftpd_log("Memory error in sending file.\n", 0);
  941. control_printf(SL_FAILURE, "553 An unknown error occured on the server.", 9);
  942. return;
  943. }
  944.  
  945. /* find the size of the transfer buffer divided by number of connections */
  946. if (change_buffer_size)
  947. {
  948. num_clients = bftpdutmp_usercount("*");
  949. my_buffer_size = get_buffer_size(num_clients);
  950. }
  951. else
  952. my_buffer_size = xfer_bufsize;
  953.  
  954. close(filedes[1]);
  955. gzfile = gzdopen(sock, "wb");
  956. while ((i = read(filedes[0], buffer, my_buffer_size))) {
  957. gzwrite(gzfile, buffer, i);
  958. test_abort(1, phile, sock);
  959.  
  960. /* check for a change in number of connections */
  961. if (change_buffer_size)
  962. {
  963. new_num_clients = bftpdutmp_usercount("*");
  964. if (new_num_clients != num_clients)
  965. {
  966. num_clients = new_num_clients;
  967. my_buffer_size = get_buffer_size(num_clients);
  968. }
  969. }
  970. /* pause between transfers */
  971. if (xfer_delay)
  972. {
  973. wait_time.tv_sec = 0;
  974. wait_time.tv_usec = xfer_delay;
  975. select( 0, NULL, NULL, NULL, &wait_time);
  976. }
  977. } // end of while
  978. free(buffer);
  979. gzclose(gzfile);
  980. wait(NULL); /* Kill the zombie */
  981. } else {
  982. stderr = devnull;
  983. close(filedes[0]);
  984. close(fileno(stdout));
  985. dup2(filedes[1], fileno(stdout));
  986. setvbuf(stdout, NULL, _IONBF, 0);
  987. argv[0] = "tar";
  988. argv[1] = "cf";
  989. argv[2] = "-";
  990. argv[3] = mapped;
  991. exit(pax_main(4, argv));
  992. }
  993. break;
  994. #endif
  995. #ifdef WANT_TAR
  996. case DO_TARONLY:
  997. if (dataconn()) {
  998. if (mapped)
  999. free(mapped);
  1000. return;
  1001. }
  1002. alarm(0);
  1003. if (fork())
  1004. wait(NULL);
  1005. else {
  1006. stderr = devnull;
  1007. dup2(sock, fileno(stdout));
  1008. argv[0] = "tar";
  1009. argv[1] = "cf";
  1010. argv[2] = "-";
  1011. argv[3] = mapped;
  1012. exit(pax_main(4, argv));
  1013. }
  1014. break;
  1015. #endif
  1016. #ifdef WANT_GZIP
  1017. case DO_GZONLY:
  1018. if (mapped)
  1019. {
  1020. free(mapped);
  1021. mapped = NULL;
  1022. }
  1023. if ((phile = open(mapped, O_RDONLY)) < 0) {
  1024. control_printf(SL_FAILURE, "553 Error: %s.", strerror(errno));
  1025. return;
  1026. }
  1027. if (dataconn()) {
  1028. if (mapped)
  1029. free(mapped);
  1030. return;
  1031. }
  1032. alarm(0);
  1033. buffer = malloc(xfer_bufsize);
  1034. /* check for alloc error */
  1035. if (! buffer)
  1036. {
  1037. bftpd_log("Memory error while sending file.", 0);
  1038. control_printf(SL_FAILURE, "553 An unknown error occured on the server.", 0);
  1039. if (phile) close(phile);
  1040. return;
  1041. }
  1042.  
  1043. /* check buffer size based on number of connections */
  1044. if (change_buffer_size)
  1045. {
  1046. num_clients = bftpdutmp_usercount("*");
  1047. my_buffer_size = get_buffer_size(num_clients);
  1048. }
  1049. else
  1050. my_buffer_size = xfer_bufsize;
  1051.  
  1052. /* Use "wb9" for maximum compression, uses more CPU time... */
  1053. gzfile = gzdopen(sock, "wb");
  1054. while ((i = read(phile, buffer, my_buffer_size))) {
  1055. gzwrite(gzfile, buffer, i);
  1056. test_abort(1, phile, sock);
  1057. if (change_buffer_size)
  1058. {
  1059. new_num_clients = bftpdutmp_usercount("*");
  1060. if ( new_num_clients != num_clients )
  1061. {
  1062. num_clients = new_num_clients;
  1063. my_buffer_size = get_buffer_size(num_clients);
  1064. }
  1065. }
  1066. /* pause between transfers */
  1067. if (xfer_delay)
  1068. {
  1069. wait_time.tv_sec = 0;
  1070. wait_time.tv_usec = xfer_delay;
  1071. select( 0, NULL, NULL, NULL, &wait_time);
  1072. }
  1073. }
  1074. free(buffer);
  1075. close(phile);
  1076. gzclose(gzfile);
  1077. break;
  1078. #endif
  1079.  
  1080. #ifdef HAVE_ZLIB_H
  1081. case DO_GZUNZIP:
  1082. if ( dataconn() )
  1083. return;
  1084.  
  1085. gzfile = gzdopen(phile, "rb");
  1086. if (! gzfile)
  1087. {
  1088. close(phile);
  1089. bftpd_log("Memory error while sending file.", 0);
  1090. control_printf(SL_FAILURE, "553 An unknown error occured on the server.", 0);
  1091. return;
  1092. }
  1093.  
  1094. alarm(0);
  1095. buffer = malloc(xfer_bufsize);
  1096. if (! buffer)
  1097. {
  1098. close(phile);
  1099. gzclose(gzfile);
  1100. bftpd_log("Memory error while sending file.", 0);
  1101. control_printf(SL_FAILURE, "553 An unknown error occured on the server.", 0);
  1102. return;
  1103. }
  1104.  
  1105. /* check buffer size based on number of connections */
  1106. if (change_buffer_size)
  1107. {
  1108. num_clients = bftpdutmp_usercount("*");
  1109. my_buffer_size = get_buffer_size(num_clients);
  1110. }
  1111. else
  1112. my_buffer_size = xfer_bufsize;
  1113.  
  1114. i = gzread(gzfile, buffer, my_buffer_size);
  1115. while ( i )
  1116. {
  1117. write(sock, buffer, i);
  1118. // test_abort(1, phile, sock);
  1119.  
  1120. if (change_buffer_size)
  1121. {
  1122. new_num_clients = bftpdutmp_usercount("*");
  1123. if ( new_num_clients != num_clients )
  1124. {
  1125. num_clients = new_num_clients;
  1126. my_buffer_size = get_buffer_size(num_clients);
  1127. }
  1128. }
  1129.  
  1130. /* pause between transfers */
  1131. if (xfer_delay)
  1132. {
  1133. wait_time.tv_sec = 0;
  1134. wait_time.tv_usec = xfer_delay;
  1135. select( 0, NULL, NULL, NULL, &wait_time);
  1136. }
  1137.  
  1138. i = gzread(gzfile, buffer, my_buffer_size);
  1139. } // end of while not end of file
  1140.  
  1141. free(buffer);
  1142. close(phile);
  1143. gzclose(gzfile);
  1144. break; // send file and unzip on the fly
  1145. #endif
  1146.  
  1147. case DO_NORMAL:
  1148. /* used to be commented out */
  1149. if (mapped)
  1150. {
  1151. free(mapped);
  1152. mapped = NULL;
  1153. }
  1154. if (dataconn())
  1155. return;
  1156. alarm(0);
  1157. lseek(phile, offset, SEEK_SET);
  1158. offset = 0;
  1159. buffer = malloc(xfer_bufsize * 2 + 1);
  1160. /* make sure buffer was created */
  1161. if (! buffer)
  1162. {
  1163. control_printf(SL_FAILURE, "553 An unknown error occured.");
  1164. bftpd_log("Memory error while trying to send file.", 0);
  1165. close(sock);
  1166. close(phile);
  1167. return;
  1168. }
  1169. if (change_buffer_size)
  1170. {
  1171. num_clients = bftpdutmp_usercount("*");
  1172. my_buffer_size = get_buffer_size(num_clients);
  1173. }
  1174. else
  1175. my_buffer_size = xfer_bufsize;
  1176.  
  1177. while ((i = read(phile, buffer, my_buffer_size))) {
  1178. if (test_abort(1, phile, sock)) {
  1179. free(buffer);
  1180. return;
  1181. }
  1182.  
  1183. if (xfertype == TYPE_ASCII) {
  1184. buffer[i] = '\0';
  1185. i += replace(buffer, "\n", "\r\n", xfer_bufsize);
  1186. }
  1187. send_status = send(sock, buffer, i, 0);
  1188. // check for dropped connection
  1189. if (send_status < 0)
  1190. {
  1191. free(buffer);
  1192. close(phile);
  1193. close(sock);
  1194. alarm(control_timeout);
  1195. control_printf(SL_SUCCESS, "426 Transfer aborted.");
  1196. control_printf(SL_SUCCESS, "226 Aborted.");
  1197. bftpd_log("File transmission interrupted. Send failed.\n");
  1198. return;
  1199. }
  1200.  
  1201. bytes_sent += i;
  1202.  
  1203. if (change_buffer_size)
  1204. {
  1205. new_num_clients = bftpdutmp_usercount("*");
  1206. my_buffer_size = get_buffer_size(num_clients);
  1207. }
  1208.  
  1209. /* pause between transfers */
  1210. if (xfer_delay)
  1211. {
  1212. wait_time.tv_sec = 0;
  1213. wait_time.tv_usec = xfer_delay;
  1214. select( 0, NULL, NULL, NULL, &wait_time);
  1215. }
  1216.  
  1217. } // end of while
  1218. free(buffer);
  1219. }
  1220.  
  1221. close(phile);
  1222. close(sock);
  1223. offset = 0;
  1224. alarm(control_timeout);
  1225. control_printf(SL_SUCCESS, "226 File transmission successful.");
  1226. bftpd_log("File transmission of '%s' successful.\n", filename);
  1227. if (mapped) free(mapped);
  1228. // Update_Send_Recv(user, bytes_sent, bytes_recvd);
  1229. }
  1230.  
  1231. void do_dirlist(char *dirname, char verbose)
  1232. {
  1233. int show_hidden = FALSE;
  1234. FILE *datastream;
  1235.  
  1236. if (dirname[0] != '\0') {
  1237. /* check for show hidden files flag */
  1238. if ( (dirname[0] == '-' ) && (dirname[1] == 'a') )
  1239. {
  1240. show_hidden = TRUE;
  1241. while ((dirname[0] != ' ') && (dirname[0] != '\0'))
  1242. dirname++;
  1243. if (dirname[0] != '\0')
  1244. dirname++;
  1245. }
  1246. /* skip other arguments */
  1247. else if (dirname[0] == '-') {
  1248. while ((dirname[0] != ' ') && (dirname[0] != '\0'))
  1249. dirname++;
  1250. if (dirname[0] != '\0')
  1251. dirname++;
  1252. }
  1253. }
  1254. if (dataconn())
  1255. return;
  1256. alarm(0);
  1257. datastream = fdopen(sock, "w");
  1258. if (dirname[0] == '\0')
  1259. dirlist("*", datastream, verbose, show_hidden);
  1260. else {
  1261. char *mapped = bftpd_cwd_mappath(dirname);
  1262. dirlist(mapped, datastream, verbose, show_hidden);
  1263. free(mapped);
  1264. }
  1265. fclose(datastream);
  1266. alarm(control_timeout);
  1267. control_printf(SL_SUCCESS, "226 Directory list has been submitted.");
  1268. }
  1269.  
  1270. void command_list(char *dirname)
  1271. {
  1272. do_dirlist(dirname, 1);
  1273. }
  1274.  
  1275. void command_nlst(char *dirname)
  1276. {
  1277. do_dirlist(dirname, 0);
  1278. }
  1279.  
  1280. void command_syst(char *params)
  1281. {
  1282. control_printf(SL_SUCCESS, "215 UNIX Type: L8");
  1283. }
  1284.  
  1285. void command_mdtm(char *filename)
  1286. {
  1287. struct stat statbuf;
  1288. struct tm *filetime;
  1289. char *fullfilename = bftpd_cwd_mappath(filename);
  1290. if (!stat(fullfilename, (struct stat *) &statbuf)) {
  1291. filetime = gmtime((time_t *) & statbuf.st_mtime);
  1292. control_printf(SL_SUCCESS, "213 %04i%02i%02i%02i%02i%02i",
  1293. filetime->tm_year + 1900, filetime->tm_mon + 1,
  1294. filetime->tm_mday, filetime->tm_hour, filetime->tm_min,
  1295. filetime->tm_sec);
  1296. } else {
  1297. control_printf(SL_FAILURE, "550 Error while determining the modification time: %s",
  1298. strerror(errno));
  1299. }
  1300. free(fullfilename);
  1301. }
  1302.  
  1303. void command_cwd(char *dir)
  1304. {
  1305. if (bftpd_cwd_chdir(dir)) {
  1306. bftpd_log("Error: '%s' while changing directory to '%s'.\n",
  1307. strerror(errno), dir);
  1308. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1309. } else {
  1310. bftpd_log("Changed directory to '%s'.\n", dir);
  1311. control_printf(SL_SUCCESS, "250 OK");
  1312. }
  1313. }
  1314.  
  1315. void command_cdup(char *params)
  1316. {
  1317. bftpd_log("Changed directory to '..'.\n");
  1318. bftpd_cwd_chdir("..");
  1319. control_printf(SL_SUCCESS, "250 OK");
  1320. }
  1321.  
  1322. void command_dele(char *filename)
  1323. {
  1324. struct stat sbuf;
  1325. char *mapped = bftpd_cwd_mappath(filename);
  1326. if (! mapped)
  1327. {
  1328. control_printf(SL_FAILURE, "451 Error: Unable to perform delete..");
  1329. return;
  1330. }
  1331. if (pre_write_script)
  1332. run_script(pre_write_script, mapped);
  1333.  
  1334. /*
  1335. if (unlink(mapped)) {
  1336. bftpd_log("Error: '%s' while trying to delete file '%s'.\n",
  1337. strerror(errno), filename);
  1338. */
  1339. if ( lstat(mapped, &sbuf) == -1 ) {
  1340. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1341. } else {
  1342. /*
  1343. bftpd_log("Deleted file '%s'.\n", filename);
  1344. control_printf(SL_SUCCESS, "200 OK");
  1345. */
  1346. if (S_ISDIR(sbuf.st_mode))
  1347. {
  1348. bftpd_log("Error: '%s' while trying to delete folder '%s' with DELE.\n", strerror(errno), filename);
  1349. control_printf(SL_FAILURE, "550 %s: Is a directory", filename);
  1350. }
  1351. else
  1352. {
  1353. if (unlink(mapped))
  1354. {
  1355. bftpd_log("Error: '%s' while trying to delete file '%s'.\n", strerror(errno), filename);
  1356. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1357. }
  1358. else
  1359. {
  1360. bftpd_log("Deleted file '%s'.\n", filename);
  1361. control_printf(SL_SUCCESS, "200 OK");
  1362. }
  1363. }
  1364. }
  1365.  
  1366. if (post_write_script)
  1367. run_script(post_write_script, mapped);
  1368.  
  1369. free(mapped);
  1370. }
  1371.  
  1372. void command_mkd(char *dirname)
  1373. {
  1374. char *mapped = bftpd_cwd_mappath(dirname);
  1375. if (! mapped)
  1376. {
  1377. control_printf(SL_FAILURE, "451 Error: Unable to create directory.");
  1378. return;
  1379. }
  1380. if (pre_write_script)
  1381. run_script(pre_write_script, mapped);
  1382.  
  1383. if (mkdir(mapped, 0777)) {
  1384. bftpd_log("Error: '%s' while trying to create directory '%s'.\n",
  1385. strerror(errno), dirname);
  1386. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1387. } else {
  1388. bftpd_log("Created directory '%s'.\n", dirname);
  1389. control_printf(SL_SUCCESS, "257 \"%s\" has been created.", dirname);
  1390. }
  1391.  
  1392. if (post_write_script)
  1393. run_script(post_write_script, mapped);
  1394.  
  1395. free(mapped);
  1396. }
  1397.  
  1398. void command_rmd(char *dirname)
  1399. {
  1400. char *mapped = bftpd_cwd_mappath(dirname);
  1401. if (! mapped)
  1402. {
  1403. control_printf(SL_FAILURE, "451 Error: Unable to remove directory.");
  1404. return;
  1405. }
  1406.  
  1407. if (pre_write_script)
  1408. run_script(pre_write_script, mapped);
  1409.  
  1410. if (rmdir(mapped)) {
  1411. /*
  1412. bftpd_log("Error: '%s' while trying to remove directory '%s'.\n", strerror(errno), dirname);
  1413. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1414. */
  1415. if (errno == ENOTEMPTY)
  1416. {
  1417. bftpd_log("Error: '%s' while trying to remove directory '%s': not empty.\n", strerror(errno), dirname);
  1418. control_printf(SL_FAILURE, "550 %s: Directory not empty", dirname);
  1419. }
  1420. else
  1421. {
  1422. bftpd_log("Error: '%s' while trying to remove directory '%s'.\n", strerror(errno), dirname);
  1423. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1424. }
  1425.  
  1426. } else {
  1427. bftpd_log("Removed directory '%s'.\n", dirname);
  1428. control_printf(SL_SUCCESS, "250 OK");
  1429. }
  1430.  
  1431. if (post_write_script)
  1432. run_script(post_write_script, mapped);
  1433. free(mapped);
  1434. }
  1435.  
  1436. void command_noop(char *params)
  1437. {
  1438. control_printf(SL_SUCCESS, "200 OK");
  1439. }
  1440.  
  1441. void command_rnfr(char *oldname)
  1442. {
  1443. FILE *file;
  1444. char *mapped = bftpd_cwd_mappath(oldname);
  1445. if (! mapped)
  1446. {
  1447. control_printf(SL_FAILURE, "451 Error: Unable to locate file.");
  1448. return;
  1449. }
  1450.  
  1451. if ((file = fopen(mapped, "r"))) {
  1452. fclose(file);
  1453. if (philename)
  1454. free(philename);
  1455. philename = mapped;
  1456. state = STATE_RENAME;
  1457. control_printf(SL_SUCCESS, "350 File exists, ready for destination name");
  1458. } else {
  1459. free(mapped);
  1460. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1461. }
  1462. }
  1463.  
  1464. void command_rnto(char *newname)
  1465. {
  1466. char *mapped = bftpd_cwd_mappath(newname);
  1467. if ( (! mapped) || (! philename) )
  1468. {
  1469. control_printf(SL_FAILURE, "451 Error: Unable to rename file.");
  1470. return;
  1471. }
  1472.  
  1473. if (pre_write_script)
  1474. run_script(pre_write_script, mapped);
  1475.  
  1476. if (rename(philename, mapped)) {
  1477. bftpd_log("Error: '%s' while trying to rename '%s' to '%s'.\n",
  1478. strerror(errno), philename, mapped);
  1479. // strerror(errno), philename, bftpd_cwd_mappath(newname));
  1480. control_printf(SL_FAILURE, "451 Error: %s.", strerror(errno));
  1481. } else {
  1482. bftpd_log("Successfully renamed '%s' to '%s'.\n", // philename, bftpd_cwd_mappath(newname));
  1483. philename, mapped);
  1484. control_printf(SL_SUCCESS, "250 OK");
  1485. state = STATE_AUTHENTICATED;
  1486. }
  1487.  
  1488. if (post_write_script)
  1489. run_script(post_write_script, mapped);
  1490.  
  1491. free(philename);
  1492. free(mapped);
  1493. philename = NULL;
  1494. }
  1495.  
  1496. void command_rest(char *params)
  1497. {
  1498. offset = strtoul(params, NULL, 10);
  1499. control_printf(SL_SUCCESS, "350 Restarting at offset %i.", offset);
  1500. }
  1501.  
  1502. void command_size(char *filename)
  1503. {
  1504. struct stat statbuf;
  1505. char *mapped = bftpd_cwd_mappath(filename);
  1506. if (! mapped)
  1507. {
  1508. control_printf(SL_FAILURE, "451 Error: Unable to locate file.");
  1509. return;
  1510. }
  1511.  
  1512. if (!stat(mapped, &statbuf)) {
  1513. control_printf(SL_SUCCESS, "213 %i", (int) statbuf.st_size);
  1514. } else {
  1515. control_printf(SL_FAILURE, "550 Error: %s.", strerror(errno));
  1516. }
  1517. free(mapped);
  1518. }
  1519.  
  1520. void command_quit(char *params)
  1521. {
  1522. control_printf(SL_SUCCESS, "221 %s", config_getoption("QUIT_MSG"));
  1523. /* Make sure we log user out. -- Jesse <slicer69@hotmail.com> */
  1524. bftpdutmp_end();
  1525. // Update_Send_Recv(user, bytes_sent, bytes_recvd);
  1526. exit(0);
  1527. }
  1528.  
  1529. void command_stat(char *filename)
  1530. {
  1531. char *mapped = bftpd_cwd_mappath(filename);
  1532. if (! mapped)
  1533. {
  1534. control_printf(SL_FAILURE, "451 Error: Unable to discover status.");
  1535. return;
  1536. }
  1537.  
  1538. control_printf(SL_SUCCESS, "213-Status of %s:", filename);
  1539. bftpd_stat(mapped, stderr);
  1540. control_printf(SL_SUCCESS, "213 End of Status.");
  1541. free(mapped);
  1542. }
  1543.  
  1544. /* SITE commands */
  1545.  
  1546. void command_chmod(char *params)
  1547. {
  1548. int permissions;
  1549. char *mapped;
  1550. char *my_string;
  1551.  
  1552. if (!strchr(params, ' ')) {
  1553. control_printf(SL_FAILURE, "550 Usage: SITE CHMOD <permissions> <filename>");
  1554. return;
  1555. }
  1556. my_string = strdup(strchr(params, ' ') + 1);
  1557. if (! my_string)
  1558. {
  1559. control_printf(SL_FAILURE, "550: An error occured on the server trying to CHMOD.");
  1560. return;
  1561. }
  1562. /* mapped = bftpd_cwd_mappath(strdup(strchr(params, ' ') + 1)); */
  1563. mapped = bftpd_cwd_mappath(my_string);
  1564. free(my_string);
  1565.  
  1566. if (pre_write_script)
  1567. run_script(pre_write_script, mapped);
  1568.  
  1569. *strchr(params, ' ') = '\0';
  1570. sscanf(params, "%o", &permissions);
  1571. if (chmod(mapped, permissions))
  1572. control_printf(SL_FAILURE, "Error: %s.", strerror(errno));
  1573. else {
  1574. bftpd_log("Changed permissions of '%s' to '%o'.\n", mapped,
  1575. permissions);
  1576. control_printf(SL_SUCCESS, "200 CHMOD successful.");
  1577. }
  1578. if (post_write_script)
  1579. run_script(post_write_script, mapped);
  1580.  
  1581. free(mapped);
  1582. }
  1583.  
  1584. void command_chown(char *params)
  1585. {
  1586. char foo[MAXCMD + 1], owner[MAXCMD + 1], group[MAXCMD + 1],
  1587. filename[MAXCMD + 1], *mapped;
  1588. int uid, gid;
  1589. if (!strchr(params, ' ')) {
  1590. control_printf(SL_FAILURE, "550 Usage: SITE CHOWN <owner>[.<group>] <filename>");
  1591. return;
  1592. }
  1593. sscanf(params, "%[^ ] %s", foo, filename);
  1594. if (strchr(foo, '.'))
  1595. sscanf(foo, "%[^.].%s", owner, group);
  1596. else {
  1597. strcpy(owner, foo);
  1598. group[0] = '\0';
  1599. }
  1600. if (!sscanf(owner, "%i", &uid)) /* Is it a number? */
  1601. if (((uid = mygetpwnam(owner, passwdfile))) < 0) {
  1602. control_printf(SL_FAILURE, "550 User '%s' not found.", owner);
  1603. return;
  1604. }
  1605. if (!sscanf(group, "%i", &gid))
  1606. if (((gid = mygetpwnam(group, groupfile))) < 0) {
  1607. control_printf(SL_FAILURE, "550 Group '%s' not found.", group);
  1608. return;
  1609. }
  1610. mapped = bftpd_cwd_mappath(filename);
  1611. if (pre_write_script)
  1612. run_script(pre_write_script, mapped);
  1613.  
  1614. if (chown(mapped, uid, gid))
  1615. control_printf(SL_FAILURE, "550 Error: %s.", strerror(errno));
  1616. else {
  1617. bftpd_log("Changed owner of '%s' to UID %i GID %i.\n", filename, uid,
  1618. gid);
  1619. control_printf(SL_SUCCESS, "200 CHOWN successful.");
  1620. }
  1621. if (post_write_script)
  1622. run_script(post_write_script, mapped);
  1623. free(mapped);
  1624. }
  1625.  
  1626.  
  1627.  
  1628.  
  1629. /*
  1630. Send the md5sum check of a given file to the
  1631. client.
  1632. */
  1633. void command_md5(char *philename)
  1634. {
  1635. FILE *myphile;
  1636. md5_t my_md5;
  1637. char buffer[1024];
  1638. int bytes_read = 0;
  1639. char md5_result[16];
  1640. char output_string[64];
  1641.  
  1642. if (! philename)
  1643. {
  1644. control_printf(SL_FAILURE, "550 File not found.");
  1645. return;
  1646. }
  1647.  
  1648. philename++;
  1649. myphile = fopen(philename, "r");
  1650. if (! myphile)
  1651. {
  1652. control_printf(SL_FAILURE, "550 Unable to open file `%s'.", philename);
  1653. return;
  1654. }
  1655.  
  1656. md5_init(&my_md5);
  1657. bytes_read = fread(buffer, sizeof(char), 1024, myphile);
  1658. while (bytes_read > 0)
  1659. {
  1660. md5_process(&my_md5, buffer, bytes_read);
  1661. bytes_read = fread(buffer, sizeof(char), 1024, myphile);
  1662. }
  1663.  
  1664. md5_finish(&my_md5, md5_result);
  1665. md5_sig_to_string(md5_result, output_string, sizeof(output_string) );
  1666. control_printf(SL_SUCCESS, "200 MD5 finger print: %s.", output_string);
  1667. fclose(myphile);
  1668. }
  1669.  
  1670.  
  1671.  
  1672. void command_site(char *str)
  1673. {
  1674. const struct command subcmds[] = {
  1675. {"chmod ", NULL, command_chmod, STATE_AUTHENTICATED},
  1676. {"chown ", NULL, command_chown, STATE_AUTHENTICATED},
  1677. {"md5", NULL, command_md5, STATE_AUTHENTICATED},
  1678. {NULL, NULL, 0}
  1679. };
  1680. int i;
  1681. if (!strcasecmp(config_getoption("ENABLE_SITE"), "no")) {
  1682. control_printf(SL_FAILURE, "550 SITE commands are disabled.");
  1683. return;
  1684. }
  1685. for (i = 0; subcmds[i].name; i++) {
  1686. if (!strncasecmp(str, subcmds[i].name, strlen(subcmds[i].name))) {
  1687. cutto(str, strlen(subcmds[i].name));
  1688. subcmds[i].function(str);
  1689. return;
  1690. }
  1691. }
  1692.  
  1693. /* see if the user needs help */
  1694. if (! strcasecmp(str, "help") )
  1695. {
  1696. control_printf(SL_SUCCESS, "211-Possible usages for SITE command:");
  1697. control_printf(SL_SUCCESS, "211-site chmod <mask> <filename>");
  1698. control_printf(SL_SUCCESS, "211-site chown <owner> <filename>");
  1699. control_printf(SL_SUCCESS, "211 site md5 <filename>");
  1700. return;
  1701. }
  1702. control_printf(SL_FAILURE, "550 Unknown command: 'SITE %s'.", str);
  1703. }
  1704.  
  1705. void command_auth(char *type)
  1706. {
  1707. control_printf(SL_FAILURE, "550 Not implemented yet\r\n");
  1708. }
  1709.  
  1710. /* Command parsing */
  1711.  
  1712. const struct command commands[] = {
  1713. {"USER", "<sp> username", command_user, STATE_CONNECTED, 0},
  1714. {"PASS", "<sp> password", command_pass, STATE_USER, 0},
  1715. {"XPWD", "(returns cwd)", command_pwd, STATE_AUTHENTICATED, 1},
  1716. {"PWD", "(returns cwd)", command_pwd, STATE_AUTHENTICATED, 0},
  1717. {"TYPE", "<sp> type-code (A or I)", command_type, STATE_AUTHENTICATED, 0},
  1718. {"PORT", "<sp> h1,h2,h3,h4,p1,p2", command_port, STATE_AUTHENTICATED, 0},
  1719. {"EPRT", "<sp><d><net-prt><d><ip><d><tcp-prt><d>", command_eprt, STATE_AUTHENTICATED, 1},
  1720. {"PASV", "(returns address/port)", command_pasv, STATE_AUTHENTICATED, 0},
  1721. {"EPSV", "(returns address/post)", command_epsv, STATE_AUTHENTICATED, 1},
  1722. {"ALLO", "<sp> size", command_allo, STATE_AUTHENTICATED, 1},
  1723. {"STOR", "<sp> pathname", command_stor, STATE_AUTHENTICATED, 0},
  1724. {"APPE", "<sp> pathname", command_appe, STATE_AUTHENTICATED, 1},
  1725. {"RETR", "<sp> pathname", command_retr, STATE_AUTHENTICATED, 0},
  1726. {"LIST", "[<sp> pathname]", command_list, STATE_AUTHENTICATED, 0},
  1727. {"NLST", "[<sp> pathname]", command_nlst, STATE_AUTHENTICATED, 0},
  1728. {"SYST", "(returns system type)", command_syst, STATE_CONNECTED, 0},
  1729. {"MDTM", "<sp> pathname", command_mdtm, STATE_AUTHENTICATED, 1},
  1730. {"XCWD", "<sp> pathname", command_cwd, STATE_AUTHENTICATED, 1},
  1731. {"CWD", "<sp> pathname", command_cwd, STATE_AUTHENTICATED, 0},
  1732. {"XCUP", "(up one directory)", command_cdup, STATE_AUTHENTICATED, 1},
  1733. {"CDUP", "(up one directory)", command_cdup, STATE_AUTHENTICATED, 0},
  1734. {"DELE", "<sp> pathname", command_dele, STATE_AUTHENTICATED, 0},
  1735. {"XMKD", "<sp> pathname", command_mkd, STATE_AUTHENTICATED, 1},
  1736. {"MKD", "<sp> pathname", command_mkd, STATE_AUTHENTICATED, 0},
  1737. {"XRMD", "<sp> pathname", command_rmd, STATE_AUTHENTICATED, 1},
  1738. {"RMD", "<sp> pathname", command_rmd, STATE_AUTHENTICATED, 0},
  1739. {"NOOP", "(no operation)", command_noop, STATE_AUTHENTICATED, 0},
  1740. {"OPTS", "<sp> string <sp> val", command_opts, STATE_AUTHENTICATED, 0},
  1741. {"RNFR", "<sp> pathname", command_rnfr, STATE_AUTHENTICATED, 0},
  1742. {"RNTO", "<sp> pathname", command_rnto, STATE_RENAME, 0},
  1743. {"REST", "<sp> byte-count", command_rest, STATE_AUTHENTICATED, 1},
  1744. {"SIZE", "<sp> pathname", command_size, STATE_AUTHENTICATED, 1},
  1745. {"QUIT", "(close control connection)", command_quit, STATE_CONNECTED, 0},
  1746. {"HELP", "[<sp> command]", command_help, STATE_AUTHENTICATED, 0},
  1747. {"STAT", "<sp> pathname", command_stat, STATE_AUTHENTICATED, 0},
  1748. {"SITE", "<sp> string", command_site, STATE_AUTHENTICATED, 0},
  1749. {"FEAT", "(returns list of extensions)", command_feat, STATE_AUTHENTICATED, 1},
  1750. /* {"AUTH", "<sp> authtype", command_auth, STATE_CONNECTED, 0},
  1751. {"ADMIN_LOGIN", "(admin)", command_adminlogin, STATE_CONNECTED, 0},*/
  1752. {"MGET", "<sp> pathname", command_mget, STATE_AUTHENTICATED, 0},
  1753. {"MPUT", "<sp> pathname", command_mput, STATE_AUTHENTICATED, 0},
  1754. {NULL, NULL, NULL, 0, 0}
  1755. };
  1756.  
  1757. void command_feat(char *params)
  1758. {
  1759. int i;
  1760. control_printf(SL_SUCCESS, "211-Extensions supported:");
  1761. for (i = 0; commands[i].name; i++)
  1762. if (commands[i].showinfeat)
  1763. control_printf(SL_SUCCESS, " %s", commands[i].name);
  1764. control_printf(SL_SUCCESS, " UTF8");
  1765. control_printf(SL_SUCCESS, "211 End");
  1766. }
  1767.  
  1768. void command_opts(char *params)
  1769. {
  1770. if (! strcasecmp(params, "utf8 on") )
  1771. control_printf(SL_SUCCESS, "200 UTF8 ON");
  1772. else
  1773. control_printf(SL_SUCCESS, "550 %s not implemented", params);
  1774. }
  1775.  
  1776.  
  1777. void command_help(char *params)
  1778. {
  1779. int i;
  1780. if (params[0] == '\0') {
  1781. control_printf(SL_SUCCESS, "214-The following commands are recognized.");
  1782. for (i = 0; commands[i].name; i++)
  1783. control_printf(SL_SUCCESS, "214-%s", commands[i].name);
  1784. control_printf(SL_SUCCESS, "214 End of help");
  1785. } else {
  1786. for (i = 0; commands[i].name; i++)
  1787. if (!strcasecmp(params, commands[i].name))
  1788. control_printf(SL_SUCCESS, "214 Syntax: %s", commands[i].syntax);
  1789. }
  1790. }
  1791.  
  1792. int parsecmd(char *str)
  1793. {
  1794. int i;
  1795. char *p, *pp, confstr[64]; /* strlen("ALLOWCOMMAND_XXXX") + 1 == 18 */
  1796. p = pp = str; /* Remove garbage in the string */
  1797. while (*p)
  1798. if ((unsigned char) *p < 32)
  1799. p++;
  1800. else
  1801. *pp++ = *p++;
  1802. *pp++ = 0;
  1803. for (i = 0; commands[i].name; i++) { /* Parse command */
  1804. if (!strncasecmp(str, commands[i].name, strlen(commands[i].name))) {
  1805. sprintf(confstr, "ALLOWCOMMAND_%s", commands[i].name);
  1806. if (!strcasecmp(config_getoption(confstr), "no")) {
  1807. control_printf(SL_FAILURE, "550 The command '%s' is disabled.",
  1808. commands[i].name);
  1809. return 1;
  1810. }
  1811. cutto(str, strlen(commands[i].name));
  1812. p = str;
  1813. while ((*p) && ((*p == ' ') || (*p == '\t')))
  1814. p++;
  1815. memmove(str, p, strlen(str) - (p - str) + 1);
  1816. if (state >= commands[i].state_needed) {
  1817. commands[i].function(str);
  1818. return 0;
  1819. } else {
  1820. switch (state) {
  1821. case STATE_CONNECTED: {
  1822. control_printf(SL_FAILURE, "503 USER expected.");
  1823. return 1;
  1824. }
  1825. case STATE_USER: {
  1826. control_printf(SL_FAILURE, "503 PASS expected.");
  1827. return 1;
  1828. }
  1829. case STATE_AUTHENTICATED: {
  1830. control_printf(SL_FAILURE, "503 RNFR before RNTO expected.");
  1831. return 1;
  1832. }
  1833. }
  1834. }
  1835. }
  1836. }
  1837. control_printf(SL_FAILURE, "500 Unknown command: \"%s\"", str);
  1838. return 0;
  1839. }
  1840.  
  1841.  
  1842. int get_buffer_size(int num_connections)
  1843. {
  1844. int buffer_size;
  1845.  
  1846. if (num_connections < 1)
  1847. num_connections = 1;
  1848.  
  1849. buffer_size = xfer_bufsize / num_connections;
  1850. if ( buffer_size < 2)
  1851. buffer_size = 2;
  1852.  
  1853. return buffer_size;
  1854. }
  1855.  
  1856.  
  1857.  
  1858. /*
  1859. This function forks and runs a script. On success it
  1860. returns TRUE, if an error occures, it returns FALSE.
  1861. */
  1862. int run_script(char *script, char *path)
  1863. {
  1864. pid_t process_id;
  1865. char *command_args[] = { script, path, NULL } ;
  1866. /* sighandler_t save_quit, save_int, save_chld; */
  1867. sig_t save_quit, save_int, save_chld;
  1868.  
  1869. /* save original signal handler values */
  1870. save_quit = signal(SIGQUIT, SIG_IGN);
  1871. save_int = signal(SIGINT, SIG_IGN);
  1872. save_chld = signal(SIGCHLD, SIG_DFL);
  1873.  
  1874. process_id = fork();
  1875. /* check for failure */
  1876. if (process_id < 0)
  1877. {
  1878. signal(SIGQUIT, save_quit);
  1879. signal(SIGINT, save_int);
  1880. signal(SIGCHLD, save_chld);
  1881. return FALSE;
  1882. }
  1883.  
  1884. /* child process */
  1885. if (process_id == 0)
  1886. {
  1887. signal(SIGQUIT, SIG_DFL);
  1888. signal(SIGINT, SIG_DFL);
  1889. signal(SIGCHLD, SIG_DFL);
  1890.  
  1891. execv(script, command_args);
  1892. bftpd_log("Error trying to run script: %s\n", script);
  1893. exit(127);
  1894. }
  1895.  
  1896. /* parent process */
  1897. do
  1898. {
  1899. // process_id = wait4(process_id, NULL, 0, NULL);
  1900. process_id = waitpid(process_id, NULL, 0);
  1901. } while ( (process_id == -1) && (errno == EINTR) );
  1902.  
  1903. signal(SIGQUIT, save_quit);
  1904. signal(SIGINT, save_int);
  1905. signal(SIGCHLD, save_chld);
  1906.  
  1907. return TRUE;
  1908. }
  1909.  
  1910.  
  1911. void Force_Update_Sent_Recv_Log()
  1912. {
  1913. Update_Send_Recv(user, bytes_sent, bytes_recvd);
  1914. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement