GeniHRN.pm.in 7.94 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# GENIPUBLIC-COPYRIGHT
4
# Copyright (c) 2009-2010 University of Utah and the Flux Group.
5 6 7 8 9 10
# All rights reserved.
#
package GeniHRN;

use strict;
use Exporter;
11
use Carp;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# References:
#
#   GMOC Proposal: "Use URN as GENI identifiers" version 0.2, Viecco, 2009
#   RFC 2141, "URN Syntax", Moats, 1997
#   RFC 3151, "A URN Namespace for Public Identifiers", Walsh, 2001
#   RFC 3986, "URI Generic Syntax", Berners-Lee, 2005
#   RFC 3987, "Internationalised Resource Identifiers", Duerst, 2005
#   RFC 4343, "DNS Case Insensitivity Clarification", Eastlake, 2006

sub Unescape($)
{
    my ($uri) = @_;

    my $norm = "";
    while( $uri =~ /^([^%]*)%([0-9A-Fa-f]{2})(.*)$/ ) {
	$norm .= $1;
	my $val = hex( $2 );
	# Transform %-encoded sequences back to unreserved characters
	# where possible (see RFC 3986, section 2.3).
	if( $val == 0x2D || $val == 0x2E ||
	    ( $val >= 0x30 && $val <= 0x39 ) ||
	    ( $val >= 0x41 && $val <= 0x5A ) ||
	    $val == 0x5F ||
	    ( $val >= 0x61 && $val <= 0x7A ) ||
	    $val == 0x7E ) {
	    $norm .= chr( $val );
	} else {
	    $norm .= "%" . $2;
	}
	$uri = $3;
    }
    $norm .= $uri;

    return $norm;
}

sub IsValid($)
{
    my ($hrn) = @_;

57 58 59 60 61
    if (!defined($hrn)) {
	carp("GeniHRN::IsValid: hrn is undefined");
	return 0;
    }

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    # Reject %00 sequences (see RFC 3986, section 7.3).
    return undef if $hrn =~ /%00/;

    # We accept ANY other %-encoded octet (following RFC 3987, section 5.3.2.3
    # in favour of RFC 2141, section 5, which specifies the opposite).
    $hrn = Unescape( $hrn );

    # The "urn" prefix is case-insensitive (see RFC 2141, section 2).
    # The "publicid" NID is case-insensitive (see RFC 2141, section 3).
    # The "IDN" specified by Viecco is believed to be case-sensitive (no
    #   authoritative reference known).
    # We regard Viecco's optional resource-type specifier as being
    #   mandatory: partly to avoid ambiguity between resource type
    #   namespaces, and partly to avoid ambiguity between a resource-type
    #   and a resource-name containing (escaped) whitespace.
77
    return $hrn =~ m'^[uU][rR][nN]:[pP][uU][bB][lL][iI][cC][iI][dD]:IDN\+[A-Za-z0-9.-]+(?::[A-Za-z0-9.-]+)*\+\w+\+(?:[-!$()*,./0-9:=@A-Z_a-z]|(?:%[0-9A-Fa-f][0-9A-Fa-f]))+$';
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
# Perform RFC 3151 transcription (from a string of legal public identifier
# characters to a URN (sub)string).
sub Transcribe($)
{
    my ($str) = @_;

    # Perform whitespace normalisation (see RFC 3151, section 1.1).
    $str =~ s/^[ \t\r\n]*//;
    $str =~ s/[ \t\r\n]*$//;
    # The replacement with a space is arbitrary and temporary; the space
    # will later be replaced with a '+' below (we can't directly use a '+'
    # yet, because we want to treat literal '+'s in the input differently).
    $str =~ s/[ \t\r\n]+/ /g;

    # The order here is critical: the intent is that from now on, at most
    # one transformation will apply to any character.
    $str =~ s/%/%25/g;
    # '% characters have been escaped; it is now unambiguous to translate
    # sequences that will contain '%'s.
    $str =~ s/#/%23/g;
    $str =~ s/'/%27/g;
    $str =~ s/\+/%2B/g;
    $str =~ s/;/%3B/g;
    $str =~ s/\?/%3F/g;
    # '+' characters have been escaped; it is now safe to translate ' ' to '+'.
    $str =~ s/ /+/g;
    # ';' characters have been escaped; it is now safe to translate '::' to
    # ';'.
    $str =~ s/::/;/g;
    # '::' sequences have been translated; any remaining ':' character must
    # have been a singleton, and can now be escaped.
    $str =~ s/:/%3A/g;
    # All ':' characters have been escaped; we can now translate '//' to ':'.
    $str =~ s|//|:|g;
    # '//' sequences have been translated; any remaining '/' character must
    # have been a singleton, and can now be escaped.
    $str =~ s|/|%2F|g;

    return $str;
}

# Perform RFC 3151 inverse transcription (from a URN (sub)string to a
# (partial) public identifier).
sub Untranscribe($)
{
    my ($str) = @_;

    # Do this in exactly the opposite order to Transcribe, for exactly
    # the same reason.
    $str =~ s|%2F|/|gi;
    $str =~ s|:|//|g;
    $str =~ s/%3A/:/gi;
    $str =~ s/;/::/g;
    $str =~ s/\+/ /g;
    $str =~ s/%3F/?/gi;
    $str =~ s/%3B/;/gi;
    $str =~ s/%2B/+/gi;
    $str =~ s/%27/'/gi;
    $str =~ s/%23/#/gi;
    $str =~ s/%25/%/gi;

    # Note that whitespace normalisation is inherently lossy, so we couldn't
    # undo it even if we wanted to: all leading and trailing whitespace is
    # irretrievably gone, and all internal whitespace sequences have collapsed
    # to single space characters.
    return $str;
}

148 149 150 151 152 153 154
# Break a URN into (sub-)authority, type, and ID components.  There
# might be further structure in the authority part, but we'll ignore
# that for now.
sub Parse($)
{
    my ($hrn) = @_;

155 156 157 158
    if (!defined($hrn)) {
	carp("GeniHRN::Parse: hrn is undefined");
	return 0;
    }
159 160 161 162 163 164
    return undef if !IsValid( $hrn );

    $hrn = Unescape( $hrn );

    $hrn =~ /^[^+]*\+([^+]+)\+([^+]+)\+(.+)$/;

165
    return ($1, $2, Untranscribe( $3 ));
166 167
}

168 169 170 171 172
# Generate a ProtoGENI URN.  Note that this is a little bit more
# restrictive than the general GENI naming scheme requires: we don't
# currently apply transcription to the authority or type fields,
# though it would be easy enough to add if anybody were perverse
# enough to want it.
173 174 175 176 177 178 179
sub Generate($$$)
{
    my ($authority, $type, $id) = @_;

    # Assume that any sub-authorities are already encoded (see
    # RFC 3151, section 2).  We don't currently handle sub-authorities,
    # so this is irrelevant for now.
180 181 182 183 184 185

    # Apply case normalisation to the authority; see RFC 3987, section
    # 5.3.2.1.  According to section 5.3.3, we are supposed to go
    # further and perform RFC 3490 ToASCII UseSTD3ASCIIRules and
    # AllowUnassigned and RFC 3491 Nameprep validation to interpret IRIs,
    # but quite frankly I think I've done more than enough RFC chasing already.
186 187 188
    $authority =~ tr/A-Z/a-z/;
    return undef if $authority !~ /^[-.0-9A-Za-z:]+$/;
    return undef if $type !~ /^[-.0-9A-Z_a-z~]+$/;
189
    return undef if $id !~ m{^[-\t\n\r !#$%'()*+,./0-9:;=?\@A-Z_a-z]+$};
190

191 192
    return "urn:publicid:IDN+" . $authority . "+" . $type . "+" .
        Transcribe( $id );
193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

# Apply scheme-based (and other) normalisations to a URN (see RFC 3987,
# section 5.3).  This is conformant to RFC 2141, section 5 (we recognise
# all of those lexical equivalences, and introduce additional ones as
# is permitted).  We do not perform deep interpretation of the URN, so
# this procedure can and should be applied to foreign (non-ProtoGENI) URNs.
sub Normalise($)
{
    my ($hrn) = @_;

    return undef if !IsValid( $hrn );

    my ($authority, $type, $id) = Parse( $hrn );
207
    return Generate( $authority, $type, $id );
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}

sub Equal($$)
{
    my ($hrn0, $hrn1) = @_;

    return undef if !IsValid( $hrn0 ) || !IsValid( $hrn1 );

    my $norm0 = Normalise( $hrn0 );
    my $norm1 = Normalise( $hrn1 );

    return $norm0 eq $norm1;
}

sub Authoritative($$)
{
    my ($hrn, $authority) = @_;

    $authority =~ tr/A-Z/a-z/;
    my @hrn = Parse( $hrn );
    $hrn[ 0 ] =~ tr/A-Z/a-z/;

    return $hrn[ 0 ] eq $authority;
}

233 234 235 236 237 238 239 240 241 242 243 244 245
# Helper functions to make special cases slightly less messy:

# Generate an interface URN given a node and an interface ID on that node.
# This will probably fail horribly if the node ends and/or the interface
# begins with a '/' character, but anybody who does that probably deserves
# what they get.
sub GenerateInterface($$$)
{
    my ($authority,$node,$interface) = @_;

    return Generate( $authority, "interface", $node . "//" . $interface );
}

246 247 248 249 250
# Undo the GenerateInterface into a authority/name/interface triplet.
sub ParseInterface($)
{
    my ($urn) = @_;

251 252 253 254
    if (!defined($urn)) {
	carp("GeniHRN::ParseInterface: urn is undefined");
	return 0;
    }
255 256 257 258 259 260 261 262 263
    my ($authority,$type,$id) = Parse( $urn );

    return undef if $type ne "interface";

    return undef unless $id =~ m{(.*)//(.*)};

    return ( $authority, $1, $2 );
}

264 265
# _Always_ make sure that this 1 is at the end of the file...
1;