Search code examples
regexperliscsi

How can use regex to validate iSCSI target names?


I am trying to craft a regexp to validate iSCSI qualified names. An example of a qualified name is iqn.2011-08.com.example:storage This is example is minimal, I have seen other examples that are more extended.

So far what I have to validate off of it this:

print "Enter a new target name: ";

my $target_name = <STDIN>;

chomp $target_name;

if ($target_name =~ /^iqn\.\d{4}-\d{2}/xmi) {

    print GREEN . "Target name is valid!" . RESET . "\n";

} else {

    print RED . "Target name is not valid!" . RESET . "\n";

}

How can I extend that to work with rest up to the : I am not going to parse after the : becuase it is a description tag.

Is there a limit to how big a domain name can be?


Solution

  • According to RFC3270 (and in turn RFC1035),

    /
       (?(DEFINE)
          (?<IQN_PAT>
             iqn
             \. 
             [0-9]{4}-[0-9]{2}
             \.
             (?&REV_SUBDOMAIN_PAT)
             (?: : .* )?
          )
    
          (?<EUI_PAT>
             eui
             \.
             [0-9A-Fa-f]{16}
          )
    
          (?<REV_SUBDOMAIN_PAT>
             (?&LABEL_PAT) (?: \. (?&LABEL_PAT) )*
          )
    
          (?<LABEL_PAT>
             [A-Za-z] (?: [A-Za-z0-9\-]* [A-Za-z0-9] )?
          )
       )
    
       ^ (?: (?&IQN_PAT) | (?&EUI_PAT) ) \z
    /sx
    

    It's not clear if the eui names accept lowercase hex digits or not. I figured it was safer to allow them.

    If you condense the above, you get /^(?:iqn\.[0-9]{4}-[0-9]{2}(?:\.[A-Za-z](?:[A-Za-z0-9\-]*[A-Za-z0-9])?)+(?::.*)?|eui\.[0-9A-Fa-f]{16})\z/s.

    (By the way, your use /m is wrong, your use of /i is wrong, and \d can match far more than the allowed [0-9].)