ftpgetput.c 8.1 KB

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