diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-18 17:32:44 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-18 17:32:44 +0000 |
| commit | 9f57cf6604638f14390effa01b51c8ad979f14cd (patch) | |
| tree | 713ecefd47867bb5d2b16c4ae3e5acd28c1c90e3 | |
| parent | fce4a9454c5399c2ce8ca8e87048331c6e3d98fa (diff) | |
| download | busybox-w32-9f57cf6604638f14390effa01b51c8ad979f14cd.tar.gz busybox-w32-9f57cf6604638f14390effa01b51c8ad979f14cd.tar.bz2 busybox-w32-9f57cf6604638f14390effa01b51c8ad979f14cd.zip | |
ftpd: fix command fetching to not do it in 1-byte reads;
fix command de-escaping. Tested to download files with embeeded \xff and LF.
libbb: tweaks for the above
function old new delta
ftpd_main 2231 2321 +90
xmalloc_fgets_internal 190 222 +32
xmalloc_fgets_str_len - 27 +27
xmalloc_fgets_str 7 23 +16
xmalloc_fgetline_str 10 26 +16
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 4/0 up/down: 181/0) Total: 181 bytes
| -rw-r--r-- | include/libbb.h | 6 | ||||
| -rw-r--r-- | libbb/fgets_str.c | 26 | ||||
| -rw-r--r-- | libbb/read.c | 6 | ||||
| -rw-r--r-- | networking/ftpd.c | 65 |
4 files changed, 81 insertions, 22 deletions
diff --git a/include/libbb.h b/include/libbb.h index bc47ce7ba..8ea493b18 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -594,9 +594,9 @@ extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FA | |||
| 594 | // Reads byte-by-byte. Useful when it is important to not read ahead. | 594 | // Reads byte-by-byte. Useful when it is important to not read ahead. |
| 595 | // Bytes are appended to pfx (which must be malloced, or NULL). | 595 | // Bytes are appended to pfx (which must be malloced, or NULL). |
| 596 | extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; | 596 | extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; |
| 597 | /* Reads block up to *maxsz_p (default: MAX_INT(ssize_t)) */ | 597 | /* Reads block up to *maxsz_p (default: INT_MAX - 4095) */ |
| 598 | extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC; | 598 | extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC; |
| 599 | /* Returns NULL if file can't be opened */ | 599 | /* Returns NULL if file can't be opened (default max size: INT_MAX - 4095) */ |
| 600 | extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; | 600 | extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; |
| 601 | /* Autodetects .gz etc */ | 601 | /* Autodetects .gz etc */ |
| 602 | extern int open_zipped(const char *fname) FAST_FUNC; | 602 | extern int open_zipped(const char *fname) FAST_FUNC; |
| @@ -619,6 +619,8 @@ extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC; | |||
| 619 | extern char *bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) FAST_FUNC; | 619 | extern char *bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) FAST_FUNC; |
| 620 | /* Reads up to (and including) TERMINATING_STRING: */ | 620 | /* Reads up to (and including) TERMINATING_STRING: */ |
| 621 | extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC; | 621 | extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC; |
| 622 | /* Same, with limited max size, and returns the length (excluding NUL): */ | ||
| 623 | extern char *xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p) FAST_FUNC; | ||
| 622 | /* Chops off TERMINATING_STRING from the end: */ | 624 | /* Chops off TERMINATING_STRING from the end: */ |
| 623 | extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string) FAST_FUNC; | 625 | extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string) FAST_FUNC; |
| 624 | /* Reads up to (and including) "\n" or NUL byte: */ | 626 | /* Reads up to (and including) "\n" or NUL byte: */ |
diff --git a/libbb/fgets_str.c b/libbb/fgets_str.c index 8026a15da..3fe61cdc3 100644 --- a/libbb/fgets_str.c +++ b/libbb/fgets_str.c | |||
| @@ -10,7 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | #include "libbb.h" | 11 | #include "libbb.h" |
| 12 | 12 | ||
| 13 | static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off) | 13 | static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off, size_t *maxsz_p) |
| 14 | { | 14 | { |
| 15 | char *linebuf = NULL; | 15 | char *linebuf = NULL; |
| 16 | const int term_length = strlen(terminating_string); | 16 | const int term_length = strlen(terminating_string); |
| @@ -18,6 +18,7 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, | |||
| 18 | int linebufsz = 0; | 18 | int linebufsz = 0; |
| 19 | int idx = 0; | 19 | int idx = 0; |
| 20 | int ch; | 20 | int ch; |
| 21 | size_t maxsz = *maxsz_p; | ||
| 21 | 22 | ||
| 22 | while (1) { | 23 | while (1) { |
| 23 | ch = fgetc(file); | 24 | ch = fgetc(file); |
| @@ -30,6 +31,11 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, | |||
| 30 | if (idx >= linebufsz) { | 31 | if (idx >= linebufsz) { |
| 31 | linebufsz += 200; | 32 | linebufsz += 200; |
| 32 | linebuf = xrealloc(linebuf, linebufsz); | 33 | linebuf = xrealloc(linebuf, linebufsz); |
| 34 | if (idx >= maxsz) { | ||
| 35 | linebuf[idx] = ch; | ||
| 36 | idx++; | ||
| 37 | break; | ||
| 38 | } | ||
| 33 | } | 39 | } |
| 34 | 40 | ||
| 35 | linebuf[idx] = ch; | 41 | linebuf[idx] = ch; |
| @@ -48,6 +54,7 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, | |||
| 48 | /* Grow/shrink *first*, then store NUL */ | 54 | /* Grow/shrink *first*, then store NUL */ |
| 49 | linebuf = xrealloc(linebuf, idx + 1); | 55 | linebuf = xrealloc(linebuf, idx + 1); |
| 50 | linebuf[idx] = '\0'; | 56 | linebuf[idx] = '\0'; |
| 57 | *maxsz_p = idx; | ||
| 51 | return linebuf; | 58 | return linebuf; |
| 52 | } | 59 | } |
| 53 | 60 | ||
| @@ -57,10 +64,23 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, | |||
| 57 | * Return NULL if EOF is reached immediately. */ | 64 | * Return NULL if EOF is reached immediately. */ |
| 58 | char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string) | 65 | char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string) |
| 59 | { | 66 | { |
| 60 | return xmalloc_fgets_internal(file, terminating_string, 0); | 67 | size_t maxsz = INT_MAX - 4095; |
| 68 | return xmalloc_fgets_internal(file, terminating_string, 0, &maxsz); | ||
| 69 | } | ||
| 70 | |||
| 71 | char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p) | ||
| 72 | { | ||
| 73 | size_t maxsz; | ||
| 74 | |||
| 75 | if (!maxsz_p) { | ||
| 76 | maxsz = INT_MAX - 4095; | ||
| 77 | maxsz_p = &maxsz; | ||
| 78 | } | ||
| 79 | return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p); | ||
| 61 | } | 80 | } |
| 62 | 81 | ||
| 63 | char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string) | 82 | char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string) |
| 64 | { | 83 | { |
| 65 | return xmalloc_fgets_internal(file, terminating_string, 1); | 84 | size_t maxsz = INT_MAX - 4095; |
| 85 | return xmalloc_fgets_internal(file, terminating_string, 1, &maxsz); | ||
| 66 | } | 86 | } |
diff --git a/libbb/read.c b/libbb/read.c index 815007c1e..37503e84d 100644 --- a/libbb/read.c +++ b/libbb/read.c | |||
| @@ -141,7 +141,7 @@ char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p) | |||
| 141 | { | 141 | { |
| 142 | char *p; | 142 | char *p; |
| 143 | size_t sz = buf ? strlen(buf) : 0; | 143 | size_t sz = buf ? strlen(buf) : 0; |
| 144 | size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t); | 144 | size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
| 145 | 145 | ||
| 146 | goto jump_in; | 146 | goto jump_in; |
| 147 | while (sz < maxsz) { | 147 | while (sz < maxsz) { |
| @@ -198,7 +198,7 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p) | |||
| 198 | size_t to_read; | 198 | size_t to_read; |
| 199 | struct stat st; | 199 | struct stat st; |
| 200 | 200 | ||
| 201 | to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */ | 201 | to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */ |
| 202 | 202 | ||
| 203 | /* Estimate file size */ | 203 | /* Estimate file size */ |
| 204 | st.st_size = 0; /* in case fstat fails, assume 0 */ | 204 | st.st_size = 0; /* in case fstat fails, assume 0 */ |
| @@ -262,7 +262,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) | |||
| 262 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ | 262 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
| 263 | if (len != (off_t)-1) { | 263 | if (len != (off_t)-1) { |
| 264 | xlseek(fd, 0, SEEK_SET); | 264 | xlseek(fd, 0, SEEK_SET); |
| 265 | size = maxsz_p ? *maxsz_p : INT_MAX; | 265 | size = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
| 266 | if (len < size) | 266 | if (len < size) |
| 267 | size = len; | 267 | size = len; |
| 268 | } | 268 | } |
diff --git a/networking/ftpd.c b/networking/ftpd.c index 6630db710..39a4d1869 100644 --- a/networking/ftpd.c +++ b/networking/ftpd.c | |||
| @@ -972,23 +972,60 @@ cmdio_get_cmd_and_arg(void) | |||
| 972 | 972 | ||
| 973 | free(G.ftp_cmd); | 973 | free(G.ftp_cmd); |
| 974 | len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */ | 974 | len = 8 * 1024; /* Paranoia. Peer may send 1 gigabyte long cmd... */ |
| 975 | G.ftp_cmd = cmd = xmalloc_reads(STDIN_FILENO, NULL, &len); | 975 | G.ftp_cmd = cmd = xmalloc_fgets_str_len(stdin, "\r\n", &len); |
| 976 | if (!cmd) | 976 | if (!cmd) |
| 977 | exit(0); | 977 | exit(0); |
| 978 | 978 | ||
| 979 | /* TODO: de-escape telnet here: 0xff,0xff => 0xff */ | 979 | /* De-escape telnet: 0xff,0xff => 0xff */ |
| 980 | /* RFC959 says that ABOR, STAT, QUIT may be sent even during | 980 | /* RFC959 says that ABOR, STAT, QUIT may be sent even during |
| 981 | * data transfer, and may be preceded by telnet's "Interrupt Process" | 981 | * data transfer, and may be preceded by telnet's "Interrupt Process" |
| 982 | * code (two-byte sequence 255,244) and then by telnet "Synch" code | 982 | * code (two-byte sequence 255,244) and then by telnet "Synch" code |
| 983 | * 255,242 (byte 242 is sent with TCP URG bit using send(MSG_OOB) | 983 | * 255,242 (byte 242 is sent with TCP URG bit using send(MSG_OOB) |
| 984 | * and may generate SIGURG on our side. See RFC854). | 984 | * and may generate SIGURG on our side. See RFC854). |
| 985 | * So far we don't support that (may install SIGURG handler if we'd want to), | 985 | * So far we don't support that (may install SIGURG handler if we'd want to), |
| 986 | * but we need to at least remove 255,xxx pairs. lftp sends those. */ | 986 | * but we need to at least remove 255,xxx pairs. lftp sends those. */ |
| 987 | 987 | /* Then de-escape FTP: NUL => '\n' */ | |
| 988 | /* Trailing '\n' is already stripped, strip '\r' */ | 988 | /* Testing for \xff: |
| 989 | len = strlen(cmd) - 1; | 989 | * Create file named '\xff': echo Hello >`echo -ne "\xff"` |
| 990 | if ((ssize_t)len >= 0 && cmd[len] == '\r') | 990 | * Try to get it: ftpget -v 127.0.0.1 Eff `echo -ne "\xff\xff"` |
| 991 | cmd[len--] = '\0'; | 991 | * (need "\xff\xff" until ftpget applet is fixed to do escaping :) |
| 992 | * Testing for embedded LF: | ||
| 993 | * LF_HERE=`echo -ne "LF\nHERE"` | ||
| 994 | * echo Hello >"$LF_HERE" | ||
| 995 | * ftpget -v 127.0.0.1 LF_HERE "$LF_HERE" | ||
| 996 | */ | ||
| 997 | { | ||
| 998 | int dst, src; | ||
| 999 | |||
| 1000 | /* Strip "\r\n" if it is there */ | ||
| 1001 | if (len != 0 && cmd[len - 1] == '\n') { | ||
| 1002 | len--; | ||
| 1003 | if (len != 0 && cmd[len - 1] == '\r') | ||
| 1004 | len--; | ||
| 1005 | cmd[len] = '\0'; | ||
| 1006 | } | ||
| 1007 | src = strchrnul(cmd, 0xff) - cmd; | ||
| 1008 | /* 99,99% there are neither NULs nor 255s and src == len */ | ||
| 1009 | if (src < len) { | ||
| 1010 | dst = src; | ||
| 1011 | do { | ||
| 1012 | if ((unsigned char)(cmd[src]) == 255) { | ||
| 1013 | src++; | ||
| 1014 | /* 255,xxx - skip 255 */ | ||
| 1015 | if ((unsigned char)(cmd[src]) != 255) { | ||
| 1016 | /* 255,!255 - skip both */ | ||
| 1017 | src++; | ||
| 1018 | continue; | ||
| 1019 | } | ||
| 1020 | /* 255,255 - retain one 255 */ | ||
| 1021 | } | ||
| 1022 | /* NUL => '\n' */ | ||
| 1023 | cmd[dst++] = cmd[src] ? cmd[src] : '\n'; | ||
| 1024 | src++; | ||
| 1025 | } while (src < len); | ||
| 1026 | cmd[dst] = '\0'; | ||
| 1027 | } | ||
| 1028 | } | ||
| 992 | 1029 | ||
| 993 | if (G.verbose > 1) | 1030 | if (G.verbose > 1) |
| 994 | verbose_log(cmd); | 1031 | verbose_log(cmd); |
