File Coverage

blib/lib/Net/LDAP/SID.pm
Criterion Covered Total %
statement 34 37 91.8
branch 5 8 62.5
condition n/a
subroutine 9 10 90.0
pod 3 3 100.0
total 51 58 87.9


line stmt bran cond sub pod time code
1             package Net::LDAP::SID;
2 2     2   25404 use strict;
  2         4  
  2         44  
3 2     2   7 use warnings;
  2         1  
  2         38  
4 2     2   5 use Carp;
  2         4  
  2         751  
5              
6             # for reference
7             # https://lists.samba.org/archive/linux/2005-September/014301.html
8             # https://froosh.wordpress.com/2005/10/21/hex-sid-to-decimal-sid-translation/
9             # https://blogs.msdn.microsoft.com/oldnewthing/20040315-00/?p=40253
10             # http://www.selfadsi.org/ads-attributes/user-objectSid.htm
11              
12             =head1 NAME
13              
14             Net::LDAP::SID - Active Directory Security Identifier manipulation
15              
16             =cut
17              
18             our $VERSION = '0.001';
19              
20             =head1 SYNOPSIS
21              
22             my $sid = Net::LDAP::SID->new( $binary );
23             # or
24             my $sid = Net::LDAP::SID->new( $string );
25              
26             print $sid->as_string;
27             print $sid->as_binary;
28              
29             =head1 METHODS
30              
31             =head2 new
32              
33             Constructor. Can pass either the binary or string representation of the SID.
34              
35             =cut
36              
37             sub new {
38 6     6 1 947 my $class = shift;
39 6 50       13 my $bin_or_str = shift or confess "binary or string required";
40 6         7 my $self = bless {}, $class;
41 6         9 $self->_build($bin_or_str);
42 6 50       12 $self->_debug() if $ENV{'LDAP_DEBUG'};
43 6         12 return $self;
44             }
45              
46             sub _debug {
47 0     0   0 my $self = shift;
48              
49 0         0 warn "SID binary = " . join( '\\', unpack '(H2)*', $self->{binary} );
50 0         0 warn "SID string = $self->{string}";
51             }
52              
53             sub _build {
54 6     6   7 my ( $self, $bin_or_string ) = @_;
55 6 100       14 if ( substr( $bin_or_string, 0, 1 ) eq 'S' ) {
56 3         6 $self->_build_from_string($bin_or_string);
57             }
58             else {
59 3         5 $self->_build_from_binary($bin_or_string);
60             }
61             }
62              
63             # SID binary format
64             # byte[0] - revision level
65             # byte[1] - count of sub authorities
66             # byte[2-8] - 48 bit authority (big-endian)
67             # and then count x 32 bit sub authorities (little-endian)
68              
69             my $THIRTY_TWO_BITS = 4294967296;
70             my $PACK_TEMPLATE = 'C C n N V*';
71              
72             sub _build_from_string {
73 3     3   2 my ( $self, $string ) = @_;
74              
75 3         10 my ( undef, $revision_level, $authority, @sub_authorities ) = split /-/,
76             $string;
77 3         4 my $sub_authority_count = scalar @sub_authorities;
78              
79 3         6 my $auth_id_16 = int( $authority / $THIRTY_TWO_BITS );
80 3         3 my $auth_id_32 = $authority - ( $auth_id_16 * $THIRTY_TWO_BITS );
81              
82 3         19 $self->{binary} = pack $PACK_TEMPLATE, $revision_level,
83             $sub_authority_count, $auth_id_16, $auth_id_32,
84             @sub_authorities;
85 3         6 $self->{string} = $string;
86             }
87              
88             sub _build_from_binary {
89 3     3   3 my ( $self, $binary ) = @_;
90 3         12 my ( $revision_level, $sub_authority_count,
91             $auth_id_16, $auth_id_32, @sub_authorities )
92             = unpack $PACK_TEMPLATE, $binary;
93              
94 3 50       7 confess "Invalid SID binary: $binary"
95             if $sub_authority_count != scalar @sub_authorities;
96              
97 3         5 my $authority = ( $auth_id_16 * $THIRTY_TWO_BITS ) + $auth_id_32;
98              
99 3         10 $self->{string} = join '-', 'S', $revision_level, $authority,
100             @sub_authorities;
101 3         7 $self->{binary} = $binary;
102             }
103              
104             =head2 as_string
105              
106             Returns string representation of SID.
107              
108             =head2 as_binary
109              
110             Returns binary representation of SID.
111              
112             =cut
113              
114 6     6 1 14 sub as_string { shift->{string} }
115 6     6 1 14 sub as_binary { shift->{binary} }
116              
117             =head1 AUTHOR
118              
119             Peter Karman, C<< >>
120              
121             =head1 BUGS
122              
123             Please report any bugs or feature requests to C, or through
124             the web interface at L. I will be notified, and then you'll
125             automatically be notified of progress on your bug as I make changes.
126              
127             =head1 SUPPORT
128              
129             You can find documentation for this module with the perldoc command.
130              
131             perldoc Net::LDAP::SID
132              
133              
134             You can also look for information at:
135              
136             =over 4
137              
138             =item * RT: CPAN's request tracker (report bugs here)
139              
140             L
141              
142             =item * AnnoCPAN: Annotated CPAN documentation
143              
144             L
145              
146             =item * CPAN Ratings
147              
148             L
149              
150             =item * Search CPAN
151              
152             L
153              
154             =back
155              
156             =head1 LICENSE AND COPYRIGHT
157              
158             Copyright 2016 Peter Karman.
159              
160             This program is free software; you can redistribute it and/or modify it
161             under the terms of the the Artistic License (2.0). You may obtain a
162             copy of the full license at:
163              
164             L
165              
166             Any use, modification, and distribution of the Standard or Modified
167             Versions is governed by this Artistic License. By using, modifying or
168             distributing the Package, you accept this license. Do not use, modify,
169             or distribute the Package, if you do not accept this license.
170              
171             If your Modified Version has been derived from a Modified Version made
172             by someone other than you, you are nevertheless required to ensure that
173             your Modified Version complies with the requirements of this license.
174              
175             This license does not grant you the right to use any trademark, service
176             mark, tradename, or logo of the Copyright Holder.
177              
178             This license includes the non-exclusive, worldwide, free-of-charge
179             patent license to make, have made, use, offer to sell, sell, import and
180             otherwise transfer the Package with respect to any patent claims
181             licensable by the Copyright Holder that are necessarily infringed by the
182             Package. If you institute patent litigation (including a cross-claim or
183             counterclaim) against any party alleging that the Package constitutes
184             direct or contributory patent infringement, then this Artistic License
185             to you shall terminate on the date that such litigation is filed.
186              
187             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
188             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
189             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
190             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
191             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
192             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
193             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
194             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195              
196             =cut
197              
198             1; # End of Net::LDAP::SID