3
0

ftpgetput.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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
  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 <getopt.h>
  16. #include "libbb.h"
  17. typedef struct ftp_host_info_s {
  18. const char *user;
  19. const char *password;
  20. struct len_and_sockaddr *lsa;
  21. } ftp_host_info_t;
  22. static smallint verbose_flag;
  23. static smallint do_continue;
  24. static void ftp_die(const char *msg, const char *remote) ATTRIBUTE_NORETURN;
  25. static void ftp_die(const char *msg, const char *remote)
  26. {
  27. /* Guard against garbage from remote server */
  28. const char *cp = remote;
  29. while (*cp >= ' ' && *cp < '\x7f') cp++;
  30. bb_error_msg_and_die("unexpected server response%s%s: %.*s",
  31. msg ? " to " : "", msg ? msg : "",
  32. (int)(cp - remote), remote);
  33. }
  34. static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
  35. {
  36. unsigned n;
  37. if (verbose_flag) {
  38. bb_error_msg("cmd %s %s", s1, s2);
  39. }
  40. if (s1) {
  41. if (s2) {
  42. fprintf(stream, "%s %s\r\n", s1, s2);
  43. } else {
  44. fprintf(stream, "%s\r\n", s1);
  45. }
  46. }
  47. do {
  48. char *buf_ptr;
  49. if (fgets(buf, 510, stream) == NULL) {
  50. bb_perror_msg_and_die("fgets");
  51. }
  52. buf_ptr = strstr(buf, "\r\n");
  53. if (buf_ptr) {
  54. *buf_ptr = '\0';
  55. }
  56. } while (!isdigit(buf[0]) || buf[3] != ' ');
  57. buf[3] = '\0';
  58. n = xatou(buf);
  59. buf[3] = ' ';
  60. return n;
  61. }
  62. static int xconnect_ftpdata(ftp_host_info_t *server, char *buf)
  63. {
  64. char *buf_ptr;
  65. unsigned short port_num;
  66. /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
  67. * Server's IP is N1.N2.N3.N4 (we ignore it)
  68. * Server's port for data connection is P1*256+P2 */
  69. buf_ptr = strrchr(buf, ')');
  70. if (buf_ptr) *buf_ptr = '\0';
  71. buf_ptr = strrchr(buf, ',');
  72. *buf_ptr = '\0';
  73. port_num = xatoul_range(buf_ptr + 1, 0, 255);
  74. buf_ptr = strrchr(buf, ',');
  75. *buf_ptr = '\0';
  76. port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
  77. set_nport(server->lsa, htons(port_num));
  78. return xconnect_stream(server->lsa);
  79. }
  80. static FILE *ftp_login(ftp_host_info_t *server)
  81. {
  82. FILE *control_stream;
  83. char buf[512];
  84. /* Connect to the command socket */
  85. control_stream = fdopen(xconnect_stream(server->lsa), "r+");
  86. if (control_stream == NULL) {
  87. /* fdopen failed - extremely unlikely */
  88. bb_perror_nomsg_and_die();
  89. }
  90. if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
  91. ftp_die(NULL, buf);
  92. }
  93. /* Login to the server */
  94. switch (ftpcmd("USER", server->user, control_stream, buf)) {
  95. case 230:
  96. break;
  97. case 331:
  98. if (ftpcmd("PASS", server->password, control_stream, buf) != 230) {
  99. ftp_die("PASS", buf);
  100. }
  101. break;
  102. default:
  103. ftp_die("USER", buf);
  104. }
  105. ftpcmd("TYPE I", NULL, control_stream, buf);
  106. return control_stream;
  107. }
  108. #if !ENABLE_FTPGET
  109. int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
  110. const char *local_path, char *server_path);
  111. #else
  112. static
  113. int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
  114. const char *local_path, char *server_path)
  115. {
  116. char buf[512];
  117. /* I think 'filesize' usage here is bogus. Let's see... */
  118. //off_t filesize = -1;
  119. #define filesize ((off_t)-1)
  120. int fd_data;
  121. int fd_local = -1;
  122. off_t beg_range = 0;
  123. /* Connect to the data socket */
  124. if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
  125. ftp_die("PASV", buf);
  126. }
  127. fd_data = xconnect_ftpdata(server, buf);
  128. if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) {
  129. //filesize = BB_STRTOOFF(buf + 4, NULL, 10);
  130. //if (errno || filesize < 0)
  131. // ftp_die("SIZE", buf);
  132. } else {
  133. do_continue = 0;
  134. }
  135. if (LONE_DASH(local_path)) {
  136. fd_local = STDOUT_FILENO;
  137. do_continue = 0;
  138. }
  139. if (do_continue) {
  140. struct stat sbuf;
  141. if (lstat(local_path, &sbuf) < 0) {
  142. bb_perror_msg_and_die("lstat");
  143. }
  144. if (sbuf.st_size > 0) {
  145. beg_range = sbuf.st_size;
  146. } else {
  147. do_continue = 0;
  148. }
  149. }
  150. if (do_continue) {
  151. sprintf(buf, "REST %"OFF_FMT"d", beg_range);
  152. if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
  153. do_continue = 0;
  154. } else {
  155. //if (filesize != -1)
  156. // filesize -= beg_range;
  157. }
  158. }
  159. if (ftpcmd("RETR", server_path, control_stream, buf) > 150) {
  160. ftp_die("RETR", buf);
  161. }
  162. /* only make a local file if we know that one exists on the remote server */
  163. if (fd_local == -1) {
  164. if (do_continue) {
  165. fd_local = xopen(local_path, O_APPEND | O_WRONLY);
  166. } else {
  167. fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
  168. }
  169. }
  170. /* Copy the file */
  171. if (filesize != -1) {
  172. if (bb_copyfd_size(fd_data, fd_local, filesize) == -1)
  173. return EXIT_FAILURE;
  174. } else {
  175. if (bb_copyfd_eof(fd_data, fd_local) == -1)
  176. return EXIT_FAILURE;
  177. }
  178. /* close it all down */
  179. close(fd_data);
  180. if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
  181. ftp_die(NULL, buf);
  182. }
  183. ftpcmd("QUIT", NULL, control_stream, buf);
  184. return EXIT_SUCCESS;
  185. }
  186. #endif
  187. #if !ENABLE_FTPPUT
  188. int ftp_send(ftp_host_info_t *server, FILE *control_stream,
  189. const char *server_path, char *local_path);
  190. #else
  191. static
  192. int ftp_send(ftp_host_info_t *server, FILE *control_stream,
  193. const char *server_path, char *local_path)
  194. {
  195. struct stat sbuf;
  196. char buf[512];
  197. int fd_data;
  198. int fd_local;
  199. int response;
  200. /* Connect to the data socket */
  201. if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
  202. ftp_die("PASV", buf);
  203. }
  204. fd_data = xconnect_ftpdata(server, buf);
  205. /* get the local file */
  206. fd_local = STDIN_FILENO;
  207. if (NOT_LONE_DASH(local_path)) {
  208. fd_local = xopen(local_path, O_RDONLY);
  209. fstat(fd_local, &sbuf);
  210. sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
  211. response = ftpcmd(buf, NULL, control_stream, buf);
  212. switch (response) {
  213. case 200:
  214. case 202:
  215. break;
  216. default:
  217. close(fd_local);
  218. ftp_die("ALLO", buf);
  219. break;
  220. }
  221. }
  222. response = ftpcmd("STOR", server_path, control_stream, buf);
  223. switch (response) {
  224. case 125:
  225. case 150:
  226. break;
  227. default:
  228. close(fd_local);
  229. ftp_die("STOR", buf);
  230. }
  231. /* transfer the file */
  232. if (bb_copyfd_eof(fd_local, fd_data) == -1) {
  233. exit(EXIT_FAILURE);
  234. }
  235. /* close it all down */
  236. close(fd_data);
  237. if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
  238. ftp_die("close", buf);
  239. }
  240. ftpcmd("QUIT", NULL, control_stream, buf);
  241. return EXIT_SUCCESS;
  242. }
  243. #endif
  244. #define FTPGETPUT_OPT_CONTINUE 1
  245. #define FTPGETPUT_OPT_VERBOSE 2
  246. #define FTPGETPUT_OPT_USER 4
  247. #define FTPGETPUT_OPT_PASSWORD 8
  248. #define FTPGETPUT_OPT_PORT 16
  249. #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
  250. static const char ftpgetput_longopts[] ALIGN1 =
  251. "continue\0" Required_argument "c"
  252. "verbose\0" No_argument "v"
  253. "username\0" Required_argument "u"
  254. "password\0" Required_argument "p"
  255. "port\0" Required_argument "P"
  256. ;
  257. #endif
  258. int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  259. int ftpgetput_main(int argc, char **argv)
  260. {
  261. /* content-length of the file */
  262. unsigned opt;
  263. const char *port = "ftp";
  264. /* socket to ftp server */
  265. FILE *control_stream;
  266. /* continue previous transfer (-c) */
  267. ftp_host_info_t *server;
  268. #if ENABLE_FTPPUT && !ENABLE_FTPGET
  269. # define ftp_action ftp_send
  270. #elif ENABLE_FTPGET && !ENABLE_FTPPUT
  271. # define ftp_action ftp_receive
  272. #else
  273. int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = ftp_send;
  274. /* Check to see if the command is ftpget or ftput */
  275. if (applet_name[3] == 'g') {
  276. ftp_action = ftp_receive;
  277. }
  278. #endif
  279. /* Set default values */
  280. server = xmalloc(sizeof(*server));
  281. server->user = "anonymous";
  282. server->password = "busybox@";
  283. /*
  284. * Decipher the command line
  285. */
  286. #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
  287. applet_long_options = ftpgetput_longopts;
  288. #endif
  289. opt_complementary = "=3"; /* must have 3 params */
  290. opt = getopt32(argv, "cvu:p:P:", &server->user, &server->password, &port);
  291. argv += optind;
  292. /* Process the non-option command line arguments */
  293. if (opt & FTPGETPUT_OPT_CONTINUE) {
  294. do_continue = 1;
  295. }
  296. if (opt & FTPGETPUT_OPT_VERBOSE) {
  297. verbose_flag = 1;
  298. }
  299. /* We want to do exactly _one_ DNS lookup, since some
  300. * sites (i.e. ftp.us.debian.org) use round-robin DNS
  301. * and we want to connect to only one IP... */
  302. server->lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
  303. if (verbose_flag) {
  304. printf("Connecting to %s (%s)\n", argv[0],
  305. xmalloc_sockaddr2dotted(&server->lsa->sa));
  306. }
  307. /* Connect/Setup/Configure the FTP session */
  308. control_stream = ftp_login(server);
  309. return ftp_action(server, control_stream, argv[1], argv[2]);
  310. }