| 1 | #+############################################################################## | ||||||||||||||||||||
| 2 | # # | ||||||||||||||||||||
| 3 | # File: No/Worries/DN.pm # | ||||||||||||||||||||
| 4 | # # | ||||||||||||||||||||
| 5 | # Description: Distinguished Names handling without worries # | ||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||
| 7 | #-############################################################################## | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | # module definition | ||||||||||||||||||||
| 11 | # | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package No::Worries::DN; | ||||||||||||||||||||
| 14 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | our $VERSION = "1.7"; | |||||||||||||||||||
| 17 | 2 | our $REVISION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # | ||||||||||||||||||||
| 20 | # used modules | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | use No::Worries::Die qw(dief); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 2 | ▶ | use No::Worries::Export qw(export_control); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 2 | ▶ | use Params::Validate qw(validate_pos :types); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # | ||||||||||||||||||||
| 28 | # constants | ||||||||||||||||||||
| 29 | # | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | ▶ | use constant FORMAT_RFC2253 => "rfc2253"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 2 | ▶ | use constant FORMAT_JAVA => "java"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 2 | ▶ | use constant FORMAT_OPENSSL => "openssl"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # | ||||||||||||||||||||
| 36 | # global variables | ||||||||||||||||||||
| 37 | # | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | our( | ||||||||||||||||||||
| 40 | 2 | %_Map, # map of known attribute types | |||||||||||||||||||
| 41 | $_TypeRE, # regexp for a valid attribute type | ||||||||||||||||||||
| 42 | $_ValueRE, # regexp for a valid attribute value | ||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 2 | foreach my $type (qw(Email emailAddress EMAILADDRESS)) { | |||||||||||||||||||
| 46 | 6 | $_Map{$type} = $type; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 2 | foreach my $type (qw(C CN DC L O OU ST)) { | |||||||||||||||||||
| 50 | 14 | $_Map{$type} = $_Map{lc($type)} = $type; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 2 | $_TypeRE = join("|", keys(%_Map)); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 2 | $_ValueRE = "[" . | |||||||||||||||||||
| 56 | "0-9a-zA-Z" . # alphanumerical | ||||||||||||||||||||
| 57 | "\\x20" . # space | ||||||||||||||||||||
| 58 | "\\x27" . # quote | ||||||||||||||||||||
| 59 | "\\x28" . # left parenthesis | ||||||||||||||||||||
| 60 | "\\x29" . # right parenthesis | ||||||||||||||||||||
| 61 | "\\x2d" . # dash | ||||||||||||||||||||
| 62 | "\\x2e" . # dot | ||||||||||||||||||||
| 63 | "\\x2f" . # slash | ||||||||||||||||||||
| 64 | "\\x3a" . # colon | ||||||||||||||||||||
| 65 | "\\x40" . # at sign | ||||||||||||||||||||
| 66 | "\\x5f" . # underscore | ||||||||||||||||||||
| 67 | "\\xa0-\\xff" . # some high-bit characters that may come from ISO-8859-1 | ||||||||||||||||||||
| 68 | "]+"; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # | ||||||||||||||||||||
| 71 | # parse a string containing a DN and return an array reference | ||||||||||||||||||||
| 72 | # | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 12 | ▶ | sub dn_parse ($) { | ||||||||||||||||||
|
Subroutine
dn_parse: called CC=10 SCAR=24.2
Pod
documented
| |||||||||||||||||||||
| 75 | 12 | my($string) = @_; | |||||||||||||||||||
| 76 | 12 | my($sep, @list, @dn); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 12 | validate_pos(@_, { type => SCALAR }); | |||||||||||||||||||
| 79 | 11 | ▶ | if ($string =~ m/^(\/[a-z]+=[^=]*){3,}$/i) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 80 | 3 | $sep = "/"; | |||||||||||||||||||
| 81 | } elsif ($string =~ m/^[a-z]+=[^=]*(,[a-z]+=[^=]*){2,}$/i) { | ||||||||||||||||||||
| 82 | 1 | $sep = ","; | |||||||||||||||||||
| 83 | } elsif ($string =~ m/^[a-z]+=[^=]*(, [a-z]+=[^=]*){2,}$/i) { | ||||||||||||||||||||
| 84 | 4 | $sep = ", "; | |||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||
| 86 | 3 | dief("unexpected DN: %s", $string); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | 8 | @list = split(/$sep/, $string); | |||||||||||||||||||
| 89 | 8 | ▶ | shift(@list) if $sep eq "/"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 8 | @dn = (); | |||||||||||||||||||
| 91 | 8 | foreach my $attr (@list) { | |||||||||||||||||||
| 92 | 37 | ▶ | if ($attr =~ /^($_TypeRE)=($_ValueRE)$/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 93 | # type=value | ||||||||||||||||||||
| 94 | 37 | push(@dn, "$_Map{$1}=$2"); | |||||||||||||||||||
| 95 | ▶ | } elsif (@dn and $attr =~ /^($_ValueRE)$/) { | |||||||||||||||||||
|
Condition0%0/3
@dn and $attr =~ /^($No::Worries::DN::_ValueRE)$/
MC/DC0%0/2
@dn and $attr =~ /^($No::Worries::DN::_ValueRE)$/
@dn
$attr =~ /^($No::Worries::DN::_ValueRE)$/
Truth table0%0/3
A and B
A= @dnB= $attr =~ /^($No::Worries::DN::_ValueRE)$/
| |||||||||||||||||||||
| 96 | # value only, assumed to come from previous attribute | ||||||||||||||||||||
| 97 | 0 | $dn[-1] .= $sep . $attr; | |||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||
| 99 | 0 | dief("invalid DN: %s", $string); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 8 | ▶ | @dn = reverse(@dn) if $sep eq "/"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 8 | return(\@dn); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # | ||||||||||||||||||||
| 107 | # convert the given parsed DN into a string | ||||||||||||||||||||
| 108 | # | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 4 | ▶ | sub dn_string ($$) { | ||||||||||||||||||
|
Subroutine
dn_string: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 111 | 4 | my($dn, $format) = @_; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 4 | validate_pos(@_, { type => ARRAYREF }, { type => SCALAR }); | |||||||||||||||||||
| 114 | 3 | ▶ | return(join(",", @{ $dn })) if $format eq FORMAT_RFC2253; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 2 | ▶ | return(join(", ", @{ $dn })) if $format eq FORMAT_JAVA; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 1 | ▶ | return(join("/", "", reverse(@{ $dn }))) if $format eq FORMAT_OPENSSL; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | dief("unsupported DN format: %s", $format); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # | ||||||||||||||||||||
| 121 | # export control | ||||||||||||||||||||
| 122 | # | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 1 | ▶ | sub import : method { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 125 | 1 | my($pkg, %exported); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | $pkg = shift(@_); | |||||||||||||||||||
| 128 | 1 | grep($exported{$_}++, map("dn_$_", qw(parse string))); | |||||||||||||||||||
| 129 | 1 | export_control(scalar(caller()), $pkg, \%exported, @_); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 2 | 1; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | __DATA__ | ||||||||||||||||||||