From f0e9e55442023c2f18e62cf74fe9098e0a6f5347 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 21 Jan 2016 16:24:16 +0000 Subject: [tftp] Mangle initial slash on TFTP URIs TFTP URIs are intrinsically problematic, since: - TFTP servers may use either normal slashes or backslashes as a directory separator, - TFTP servers allow filenames to be specified using relative paths (with no initial directory separator), - TFTP filenames present in a DHCP filename field may use special characters such as "?" or "#" that prevent parsing as a generic URI. As of commit 7667536 ("[uri] Refactor URI parsing and formatting"), we have directly constructed TFTP URIs from DHCP next-server and filename pairs, avoiding the generic URI parser. This eliminated the problems related to special characters, but indirectly made it impossible to parse a "tftp://..." URI string into a TFTP URI with a non-absolute path. Re-introduce the convention of requiring an extra slash in a "tftp://..." URI string in order to specify a TFTP URI with an initial slash in the filename. For example: tftp://192.168.0.1/boot/pxelinux.0 => RRQ "boot/pxelinux.0" tftp://192.168.0.1//boot/pxelinux.0 => RRQ "/boot/pxelinux.0" This is ugly, but there seems to be no other sensible way to provide the ability to specify all possible TFTP filenames. A side-effect of this change is that format_uri() will no longer add a spurious initial "/" when formatting a relative URI string. This improves the console output when fetching an image specified via a relative URI. Signed-off-by: Michael Brown --- src/net/udp/tftp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/net/udp') diff --git a/src/net/udp/tftp.c b/src/net/udp/tftp.c index f3cb34342..4255472ee 100644 --- a/src/net/udp/tftp.c +++ b/src/net/udp/tftp.c @@ -325,7 +325,7 @@ void tftp_set_mtftp_port ( unsigned int port ) { * @ret rc Return status code */ static int tftp_send_rrq ( struct tftp_request *tftp ) { - const char *path = tftp->uri->path; + const char *path = ( tftp->uri->path + 1 /* skip '/' */ ); struct tftp_rrq *rrq; size_t len; struct io_buffer *iobuf; @@ -1067,6 +1067,8 @@ static int tftp_core_open ( struct interface *xfer, struct uri *uri, return -EINVAL; if ( ! uri->path ) return -EINVAL; + if ( uri->path[0] != '/' ) + return -EINVAL; /* Allocate and populate TFTP structure */ tftp = zalloc ( sizeof ( *tftp ) ); -- cgit v1.2.3-55-g7522