ftpgetput.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ftpget
  4. *
  5. * Mini implementation of FTP to retrieve a remote file.
  6. *
  7. * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
  8. * Copyright (C) 2002 Glenn McGrath <bug1@iinet.net.au>
  9. *
  10. * Based on wget.c by Chip Rosenthal Covad Communications
  11. * <chip@laserlink.net>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <sys/types.h>
  29. #include <sys/ioctl.h>
  30. #include <sys/time.h>
  31. #include <sys/stat.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <getopt.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <signal.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <netinet/in.h>
  42. #include <netdb.h>
  43. #include <sys/socket.h>
  44. #include <arpa/inet.h>
  45. #include "busybox.h"
  46. typedef struct ftp_host_info_s {
  47. char *user;
  48. char *password;
  49. struct sockaddr_in *s_in;
  50. } ftp_host_info_t;
  51. static char verbose_flag = 0;
  52. static char do_continue = 0;
  53. static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
  54. {
  55. if (verbose_flag) {
  56. bb_error_msg("cmd %s%s", s1, s2);
  57. }
  58. if (s1) {
  59. if (s2) {
  60. fprintf(stream, "%s%s\n", s1, s2);
  61. } else {
  62. fprintf(stream, "%s\n", s1);
  63. }
  64. }
  65. do {
  66. char *buf_ptr;
  67. if (fgets(buf, 510, stream) == NULL) {
  68. bb_perror_msg_and_die("fgets()");
  69. }
  70. buf_ptr = strstr(buf, "\r\n");
  71. if (buf_ptr) {
  72. *buf_ptr = '\0';
  73. }
  74. } while (! isdigit(buf[0]) || buf[3] != ' ');
  75. return atoi(buf);
  76. }
  77. static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
  78. {
  79. char *buf_ptr;
  80. unsigned short port_num;
  81. buf_ptr = strrchr(buf, ',');
  82. *buf_ptr = '\0';
  83. port_num = atoi(buf_ptr + 1);
  84. buf_ptr = strrchr(buf, ',');
  85. *buf_ptr = '\0';
  86. port_num += atoi(buf_ptr + 1) * 256;
  87. server->s_in->sin_port=htons(port_num);
  88. return(xconnect(server->s_in));
  89. }
  90. static FILE *ftp_login(ftp_host_info_t *server)
  91. {
  92. FILE *control_stream;
  93. char buf[512];
  94. /* Connect to the command socket */
  95. control_stream = fdopen(xconnect(server->s_in), "r+");
  96. if (control_stream == NULL) {
  97. bb_perror_msg_and_die("Couldnt open control stream");
  98. }
  99. if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
  100. bb_error_msg_and_die("%s", buf + 4);
  101. }
  102. /* Login to the server */
  103. switch (ftpcmd("USER ", server->user, control_stream, buf)) {
  104. case 230:
  105. break;
  106. case 331:
  107. if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
  108. bb_error_msg_and_die("PASS error: %s", buf + 4);
  109. }
  110. break;
  111. default:
  112. bb_error_msg_and_die("USER error: %s", buf + 4);
  113. }
  114. ftpcmd("TYPE I", NULL, control_stream, buf);
  115. return(control_stream);
  116. }
  117. #ifdef CONFIG_FTPGET
  118. static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
  119. const char *local_path, char *server_path)
  120. {
  121. char buf[512];
  122. off_t filesize = 0;
  123. int fd_data;
  124. int fd_local = -1;
  125. off_t beg_range = 0;
  126. /* Connect to the data socket */
  127. if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
  128. bb_error_msg_and_die("PASV error: %s", buf + 4);
  129. }
  130. fd_data = xconnect_ftpdata(server, buf);
  131. if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
  132. unsigned long value=filesize;
  133. if (safe_strtoul(buf + 4, &value))
  134. bb_error_msg_and_die("SIZE error: %s", buf + 4);
  135. filesize = value;
  136. }
  137. if ((local_path[0] == '-') && (local_path[1] == '\0')) {
  138. fd_local = STDOUT_FILENO;
  139. do_continue = 0;
  140. }
  141. if (do_continue) {
  142. struct stat sbuf;
  143. if (lstat(local_path, &sbuf) < 0) {
  144. bb_perror_msg_and_die("fstat()");
  145. }
  146. if (sbuf.st_size > 0) {
  147. beg_range = sbuf.st_size;
  148. } else {
  149. do_continue = 0;
  150. }
  151. }
  152. if (do_continue) {
  153. sprintf(buf, "REST %ld", (long)beg_range);
  154. if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
  155. do_continue = 0;
  156. } else {
  157. filesize -= beg_range;
  158. }
  159. }
  160. if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
  161. bb_error_msg_and_die("RETR error: %s", buf + 4);
  162. }
  163. /* only make a local file if we know that one exists on the remote server */
  164. if (fd_local == -1) {
  165. if (do_continue) {
  166. fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
  167. } else {
  168. fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
  169. }
  170. }
  171. /* Copy the file */
  172. if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
  173. exit(EXIT_FAILURE);
  174. }
  175. /* close it all down */
  176. close(fd_data);
  177. if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
  178. bb_error_msg_and_die("ftp error: %s", buf + 4);
  179. }
  180. ftpcmd("QUIT", NULL, control_stream, buf);
  181. return(EXIT_SUCCESS);
  182. }
  183. #endif
  184. #ifdef CONFIG_FTPPUT
  185. static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
  186. const char *server_path, char *local_path)
  187. {
  188. struct stat sbuf;
  189. char buf[512];
  190. int fd_data;
  191. int fd_local;
  192. int response;
  193. /* Connect to the data socket */
  194. if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
  195. bb_error_msg_and_die("PASV error: %s", buf + 4);
  196. }
  197. fd_data = xconnect_ftpdata(server, buf);
  198. if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
  199. bb_error_msg_and_die("CWD error: %s", buf + 4);
  200. }
  201. /* get the local file */
  202. if ((local_path[0] == '-') && (local_path[1] == '\0')) {
  203. fd_local = STDIN_FILENO;
  204. } else {
  205. fd_local = bb_xopen(local_path, O_RDONLY);
  206. fstat(fd_local, &sbuf);
  207. sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
  208. response = ftpcmd(buf, NULL, control_stream, buf);
  209. switch (response) {
  210. case 200:
  211. case 202:
  212. break;
  213. default:
  214. close(fd_local);
  215. bb_error_msg_and_die("ALLO error: %s", buf + 4);
  216. break;
  217. }
  218. }
  219. response = ftpcmd("STOR ", local_path, control_stream, buf);
  220. switch (response) {
  221. case 125:
  222. case 150:
  223. break;
  224. default:
  225. close(fd_local);
  226. bb_error_msg_and_die("STOR error: %s", buf + 4);
  227. }
  228. /* transfer the file */
  229. if (bb_copyfd_eof(fd_local, fd_data) == -1) {
  230. exit(EXIT_FAILURE);
  231. }
  232. /* close it all down */
  233. close(fd_data);
  234. if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
  235. bb_error_msg_and_die("error: %s", buf + 4);
  236. }
  237. ftpcmd("QUIT", NULL, control_stream, buf);
  238. return(EXIT_SUCCESS);
  239. }
  240. #endif
  241. #define FTPGETPUT_OPT_CONTINUE 1
  242. #define FTPGETPUT_OPT_VERBOSE 2
  243. #define FTPGETPUT_OPT_USER 4
  244. #define FTPGETPUT_OPT_PASSWORD 8
  245. #define FTPGETPUT_OPT_PORT 16
  246. static const struct option ftpgetput_long_options[] = {
  247. {"continue", 1, NULL, 'c'},
  248. {"verbose", 0, NULL, 'v'},
  249. {"username", 1, NULL, 'u'},
  250. {"password", 1, NULL, 'p'},
  251. {"port", 1, NULL, 'P'},
  252. {0, 0, 0, 0}
  253. };
  254. int ftpgetput_main(int argc, char **argv)
  255. {
  256. /* content-length of the file */
  257. unsigned long opt;
  258. char *port = "ftp";
  259. /* socket to ftp server */
  260. FILE *control_stream;
  261. struct sockaddr_in s_in;
  262. /* continue a prev transfer (-c) */
  263. ftp_host_info_t *server;
  264. int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
  265. /* Check to see if the command is ftpget or ftput */
  266. #ifdef CONFIG_FTPPUT
  267. # ifdef CONFIG_FTPGET
  268. if (bb_applet_name[3] == 'p') {
  269. ftp_action = ftp_send;
  270. }
  271. # else
  272. ftp_action = ftp_send;
  273. # endif
  274. #endif
  275. #ifdef CONFIG_FTPGET
  276. # ifdef CONFIG_FTPPUT
  277. if (bb_applet_name[3] == 'g') {
  278. ftp_action = ftp_recieve;
  279. }
  280. # else
  281. ftp_action = ftp_recieve;
  282. # endif
  283. #endif
  284. /* Set default values */
  285. server = xmalloc(sizeof(ftp_host_info_t));
  286. server->user = "anonymous";
  287. server->password = "busybox@";
  288. verbose_flag = 0;
  289. /*
  290. * Decipher the command line
  291. */
  292. bb_applet_long_options = ftpgetput_long_options;
  293. opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
  294. /* Process the non-option command line arguments */
  295. if (argc - optind != 3) {
  296. bb_show_usage();
  297. }
  298. if (opt & FTPGETPUT_OPT_CONTINUE) {
  299. do_continue = 1;
  300. }
  301. if (opt & FTPGETPUT_OPT_VERBOSE) {
  302. verbose_flag = 1;
  303. }
  304. /* We want to do exactly _one_ DNS lookup, since some
  305. * sites (i.e. ftp.us.debian.org) use round-robin DNS
  306. * and we want to connect to only one IP... */
  307. server->s_in = &s_in;
  308. bb_lookup_host(&s_in, argv[optind]);
  309. s_in.sin_port = bb_lookup_port(port, "tcp", 21);
  310. if (verbose_flag) {
  311. printf("Connecting to %s[%s]:%d\n",
  312. argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
  313. }
  314. /* Connect/Setup/Configure the FTP session */
  315. control_stream = ftp_login(server);
  316. return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
  317. }
  318. /*
  319. Local Variables:
  320. c-file-style: "linux"
  321. c-basic-offset: 4
  322. tab-width: 4
  323. End:
  324. */