Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Utility routines. |
| 3 | * |
| 4 | * Copyright (C) 2018 Denys Vlasenko |
| 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 7 | */ |
Denys Vlasenko | 8d94317 | 2018-02-06 17:11:15 +0100 | [diff] [blame] | 8 | //kbuild:lib-$(CONFIG_FTPGET) += parse_pasv_epsv.o |
| 9 | //kbuild:lib-$(CONFIG_FTPPUT) += parse_pasv_epsv.o |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 10 | //kbuild:lib-$(CONFIG_WGET) += parse_pasv_epsv.o |
| 11 | |
| 12 | #include "libbb.h" |
| 13 | |
| 14 | int FAST_FUNC parse_pasv_epsv(char *buf) |
| 15 | { |
| 16 | /* |
| 17 | * PASV command will not work for IPv6. RFC2428 describes |
| 18 | * IPv6-capable "extended PASV" - EPSV. |
| 19 | * |
| 20 | * "EPSV [protocol]" asks server to bind to and listen on a data port |
| 21 | * in specified protocol. Protocol is 1 for IPv4, 2 for IPv6. |
| 22 | * If not specified, defaults to "same as used for control connection". |
| 23 | * If server understood you, it should answer "229 <some text>(|||port|)" |
| 24 | * where "|" are literal pipe chars and "port" is ASCII decimal port#. |
| 25 | * |
| 26 | * There is also an IPv6-capable replacement for PORT (EPRT), |
| 27 | * but we don't need that. |
| 28 | * |
| 29 | * NB: PASV may still work for some servers even over IPv6. |
| 30 | * For example, vsftp happily answers |
| 31 | * "227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual. |
| 32 | */ |
| 33 | char *ptr; |
| 34 | int port; |
| 35 | |
| 36 | if (!ENABLE_FEATURE_IPV6 || buf[2] == '7' /* "227" */) { |
| 37 | /* Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]" |
| 38 | * Server's IP is N1.N2.N3.N4 (we ignore it) |
| 39 | * Server's port for data connection is P1*256+P2 */ |
| 40 | ptr = strrchr(buf, ')'); |
Denys Vlasenko | 06a407c | 2020-07-19 20:59:35 +0200 | [diff] [blame] | 41 | if (!ptr) ptr = strrchr(buf, '\r'); /* for PASV responses not ending with ')' */ |
| 42 | if (!ptr) ptr = strrchr(buf, '\n'); /* for PASV responses not ending with ')' */ |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 43 | if (ptr) *ptr = '\0'; |
| 44 | |
| 45 | ptr = strrchr(buf, ','); |
| 46 | if (!ptr) return -1; |
| 47 | *ptr = '\0'; |
| 48 | port = xatou_range(ptr + 1, 0, 255); |
| 49 | |
| 50 | ptr = strrchr(buf, ','); |
| 51 | if (!ptr) return -1; |
| 52 | *ptr = '\0'; |
| 53 | port += xatou_range(ptr + 1, 0, 255) * 256; |
| 54 | } else { |
| 55 | /* Response is "229 garbage(|||P1|)" |
| 56 | * Server's port for data connection is P1 */ |
| 57 | ptr = strrchr(buf, '|'); |
| 58 | if (!ptr) return -1; |
| 59 | *ptr = '\0'; |
| 60 | |
| 61 | ptr = strrchr(buf, '|'); |
| 62 | if (!ptr) return -1; |
| 63 | *ptr = '\0'; |
| 64 | port = xatou_range(ptr + 1, 0, 65535); |
| 65 | } |
| 66 | |
| 67 | return port; |
| 68 | } |