[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 <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2016-01-21 16:24:16 +00:00
parent 42c2a6aab7
commit f0e9e55442
3 changed files with 60 additions and 28 deletions

View File

@@ -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 ) );