Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * ftpget |
| 4 | * |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 5 | * Mini implementation of FTP to retrieve a remote file. |
| 6 | * |
| 7 | * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com> |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 8 | * Copyright (C) 2002 Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 9 | * |
| 10 | * Based on wget.c by Chip Rosenthal Covad Communications |
| 11 | * <chip@laserlink.net> |
| 12 | * |
Rob Landley | 6f03722 | 2005-11-08 00:52:31 +0000 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 17 | #include <getopt.h> |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 18 | |
| 19 | typedef struct ftp_host_info_s { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 20 | char *user; |
| 21 | char *password; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 22 | struct sockaddr_in *s_in; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 23 | } ftp_host_info_t; |
| 24 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 25 | static char verbose_flag = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 26 | static char do_continue = 0; |
| 27 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 28 | static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf) |
| 29 | { |
| 30 | if (verbose_flag) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | bb_error_msg("cmd %s%s", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | if (s1) { |
| 35 | if (s2) { |
Paul Fox | 146e83a | 2005-07-19 21:26:57 +0000 | [diff] [blame] | 36 | fprintf(stream, "%s%s\r\n", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 37 | } else { |
Paul Fox | 146e83a | 2005-07-19 21:26:57 +0000 | [diff] [blame] | 38 | fprintf(stream, "%s\r\n", s1); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 41 | do { |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 42 | char *buf_ptr; |
| 43 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 44 | if (fgets(buf, 510, stream) == NULL) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 45 | bb_perror_msg_and_die("fgets"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 46 | } |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 47 | buf_ptr = strstr(buf, "\r\n"); |
| 48 | if (buf_ptr) { |
| 49 | *buf_ptr = '\0'; |
| 50 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 51 | } while (!isdigit(buf[0]) || buf[3] != ' '); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 53 | return xatou(buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 56 | static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 57 | { |
| 58 | char *buf_ptr; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 59 | unsigned short port_num; |
| 60 | |
| 61 | buf_ptr = strrchr(buf, ','); |
| 62 | *buf_ptr = '\0'; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 63 | port_num = xatoul_range(buf_ptr + 1, 0, 255); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 64 | |
| 65 | buf_ptr = strrchr(buf, ','); |
| 66 | *buf_ptr = '\0'; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 67 | port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 69 | server->s_in->sin_port = htons(port_num); |
Denis Vlasenko | 1457915 | 2006-10-26 01:09:46 +0000 | [diff] [blame^] | 70 | return xconnect_tcp_v4(server->s_in); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static FILE *ftp_login(ftp_host_info_t *server) |
| 74 | { |
| 75 | FILE *control_stream; |
| 76 | char buf[512]; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 77 | |
| 78 | /* Connect to the command socket */ |
Denis Vlasenko | 1457915 | 2006-10-26 01:09:46 +0000 | [diff] [blame^] | 79 | control_stream = fdopen(xconnect_tcp_v4(server->s_in), "r+"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 80 | if (control_stream == NULL) { |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 81 | bb_perror_msg_and_die("cannot open control stream"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | bb_error_msg_and_die("%s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Login to the server */ |
| 89 | switch (ftpcmd("USER ", server->user, control_stream, buf)) { |
| 90 | case 230: |
| 91 | break; |
| 92 | case 331: |
| 93 | if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | bb_error_msg_and_die("PASS error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 95 | } |
| 96 | break; |
| 97 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | bb_error_msg_and_die("USER error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | ftpcmd("TYPE I", NULL, control_stream, buf); |
| 102 | |
| 103 | return(control_stream); |
| 104 | } |
| 105 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 106 | #if !ENABLE_FTPGET |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 107 | int ftp_receive(ftp_host_info_t *server, FILE *control_stream, |
| 108 | const char *local_path, char *server_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 109 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 110 | static |
| 111 | int ftp_receive(ftp_host_info_t *server, FILE *control_stream, |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 112 | const char *local_path, char *server_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 113 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 114 | char buf[512]; |
| 115 | off_t filesize = 0; |
| 116 | int fd_data; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 117 | int fd_local = -1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 118 | off_t beg_range = 0; |
| 119 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 120 | /* Connect to the data socket */ |
| 121 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 123 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 124 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 125 | |
| 126 | if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 127 | if (SAFE_STRTOOFF(buf + 4, &filesize)) |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 128 | bb_error_msg_and_die("SIZE error: %s", buf + 4); |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 129 | } else { |
| 130 | filesize = -1; |
| 131 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 134 | if ((local_path[0] == '-') && (local_path[1] == '\0')) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 135 | fd_local = STDOUT_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 136 | do_continue = 0; |
| 137 | } |
| 138 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 139 | if (do_continue) { |
| 140 | struct stat sbuf; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 141 | if (lstat(local_path, &sbuf) < 0) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 142 | bb_perror_msg_and_die("lstat"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 143 | } |
| 144 | if (sbuf.st_size > 0) { |
| 145 | beg_range = sbuf.st_size; |
| 146 | } else { |
| 147 | do_continue = 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (do_continue) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 152 | sprintf(buf, "REST "OFF_FMT, beg_range); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 153 | if (ftpcmd(buf, NULL, control_stream, buf) != 350) { |
| 154 | do_continue = 0; |
| 155 | } else { |
| 156 | filesize -= beg_range; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 161 | bb_error_msg_and_die("RETR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 164 | /* only make a local file if we know that one exists on the remote server */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 165 | if (fd_local == -1) { |
| 166 | if (do_continue) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 167 | fd_local = xopen(local_path, O_APPEND | O_WRONLY); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 168 | } else { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 169 | fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 170 | } |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 173 | /* Copy the file */ |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 174 | if (filesize != -1) { |
| 175 | if (-1 == bb_copyfd_size(fd_data, fd_local, filesize)) |
| 176 | exit(EXIT_FAILURE); |
| 177 | } else { |
| 178 | if (-1 == bb_copyfd_eof(fd_data, fd_local)) |
| 179 | exit(EXIT_FAILURE); |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 180 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 181 | |
| 182 | /* close it all down */ |
| 183 | close(fd_data); |
| 184 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 185 | bb_error_msg_and_die("ftp error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 186 | } |
| 187 | ftpcmd("QUIT", NULL, control_stream, buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 188 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 189 | return(EXIT_SUCCESS); |
| 190 | } |
| 191 | #endif |
| 192 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 193 | #if !ENABLE_FTPPUT |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 194 | int ftp_send(ftp_host_info_t *server, FILE *control_stream, |
| 195 | const char *server_path, char *local_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 196 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 197 | static |
| 198 | int ftp_send(ftp_host_info_t *server, FILE *control_stream, |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 199 | const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 200 | { |
| 201 | struct stat sbuf; |
| 202 | char buf[512]; |
| 203 | int fd_data; |
| 204 | int fd_local; |
| 205 | int response; |
| 206 | |
| 207 | /* Connect to the data socket */ |
| 208 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 209 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 210 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 211 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 212 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 213 | /* get the local file */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 214 | if ((local_path[0] == '-') && (local_path[1] == '\0')) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 215 | fd_local = STDIN_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 216 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 217 | fd_local = xopen(local_path, O_RDONLY); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 218 | fstat(fd_local, &sbuf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 219 | |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 220 | sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size); |
| 221 | response = ftpcmd(buf, NULL, control_stream, buf); |
| 222 | switch (response) { |
| 223 | case 200: |
| 224 | case 202: |
| 225 | break; |
| 226 | default: |
| 227 | close(fd_local); |
| 228 | bb_error_msg_and_die("ALLO error: %s", buf + 4); |
| 229 | break; |
| 230 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 231 | } |
Rob Landley | 6f03722 | 2005-11-08 00:52:31 +0000 | [diff] [blame] | 232 | response = ftpcmd("STOR ", server_path, control_stream, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 233 | switch (response) { |
| 234 | case 125: |
| 235 | case 150: |
| 236 | break; |
| 237 | default: |
| 238 | close(fd_local); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 239 | bb_error_msg_and_die("STOR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* transfer the file */ |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 243 | if (bb_copyfd_eof(fd_local, fd_data) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 244 | exit(EXIT_FAILURE); |
| 245 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 246 | |
| 247 | /* close it all down */ |
| 248 | close(fd_data); |
| 249 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 250 | bb_error_msg_and_die("error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 251 | } |
| 252 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 253 | |
| 254 | return(EXIT_SUCCESS); |
| 255 | } |
| 256 | #endif |
| 257 | |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 258 | #define FTPGETPUT_OPT_CONTINUE 1 |
| 259 | #define FTPGETPUT_OPT_VERBOSE 2 |
| 260 | #define FTPGETPUT_OPT_USER 4 |
| 261 | #define FTPGETPUT_OPT_PASSWORD 8 |
| 262 | #define FTPGETPUT_OPT_PORT 16 |
| 263 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 264 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 265 | static const struct option ftpgetput_long_options[] = { |
| 266 | {"continue", 1, NULL, 'c'}, |
| 267 | {"verbose", 0, NULL, 'v'}, |
| 268 | {"username", 1, NULL, 'u'}, |
| 269 | {"password", 1, NULL, 'p'}, |
| 270 | {"port", 1, NULL, 'P'}, |
| 271 | {0, 0, 0, 0} |
| 272 | }; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 273 | #else |
| 274 | #define ftpgetput_long_options 0 |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 275 | #endif |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 276 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 277 | int ftpgetput_main(int argc, char **argv) |
| 278 | { |
| 279 | /* content-length of the file */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 280 | unsigned opt; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 281 | char *port = "ftp"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 282 | |
| 283 | /* socket to ftp server */ |
| 284 | FILE *control_stream; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 285 | struct sockaddr_in s_in; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 286 | |
| 287 | /* continue a prev transfer (-c) */ |
| 288 | ftp_host_info_t *server; |
| 289 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 290 | int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 291 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 292 | /* Check to see if the command is ftpget or ftput */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 293 | if (ENABLE_FTPPUT && (!ENABLE_FTPGET || applet_name[3] == 'p')) { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 294 | ftp_action = ftp_send; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 295 | } |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 296 | if (ENABLE_FTPGET && (!ENABLE_FTPPUT || applet_name[3] == 'g')) { |
Bernhard Reutner-Fischer | e0387a6 | 2006-06-07 13:31:59 +0000 | [diff] [blame] | 297 | ftp_action = ftp_receive; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 298 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 299 | |
| 300 | /* Set default values */ |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 301 | server = xmalloc(sizeof(ftp_host_info_t)); |
| 302 | server->user = "anonymous"; |
| 303 | server->password = "busybox@"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 304 | verbose_flag = 0; |
| 305 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 306 | /* |
| 307 | * Decipher the command line |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 308 | */ |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 309 | if (ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS) |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 310 | applet_long_options = ftpgetput_long_options; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 311 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 312 | opt = getopt32(argc, argv, "cvu:p:P:", &server->user, &server->password, &port); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 313 | |
| 314 | /* Process the non-option command line arguments */ |
| 315 | if (argc - optind != 3) { |
| 316 | bb_show_usage(); |
| 317 | } |
| 318 | |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 319 | if (opt & FTPGETPUT_OPT_CONTINUE) { |
| 320 | do_continue = 1; |
| 321 | } |
| 322 | if (opt & FTPGETPUT_OPT_VERBOSE) { |
| 323 | verbose_flag = 1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 326 | /* We want to do exactly _one_ DNS lookup, since some |
| 327 | * sites (i.e. ftp.us.debian.org) use round-robin DNS |
| 328 | * and we want to connect to only one IP... */ |
| 329 | server->s_in = &s_in; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 330 | bb_lookup_host(&s_in, argv[optind]); |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 331 | s_in.sin_port = bb_lookup_port(port, "tcp", 21); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 332 | if (verbose_flag) { |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 333 | printf("Connecting to %s[%s]:%d\n", |
| 334 | argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Connect/Setup/Configure the FTP session */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 338 | control_stream = ftp_login(server); |
| 339 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 340 | return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2])); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 341 | } |