blib/lib/Samba/SIDhelper.pm

filter: Statementstmt 10.9% 8 / 73 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 100% 4 / 4 totaltotal 13.5% 14 / 103 CC 11 SCAR 33.8
CC
11
Cov
9%
new
56.0
uid2sid
12.0
gid2sid
12.0
CRAP 29.2
↑ Summary
1 package Samba::SIDhelper;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Samba::SIDhelper - Create SIDs based on G/UIDs.
9  
10 =head1 VERSION
11  
12 Version 0.0.0
13  
14 =cut
15  
16 2 our $VERSION = '0.0.0';
17  
18  
19 =head1 SYNOPSIS
20  
21 use Samba::SIDhelper;
22  
23 my $sidhelper = Samba::SIDhelper->new({sid=>'S-1-5-21-1234-56789-10111213'});
24  
25 my $sid=$sidhelper->uid2sid('1002');
26 if ($sidhelper){
27 print "Error!\n";
28 }
29  
30 $sid=$sidhelper->gid2sid('1002');
31 if ($sidhelper){
32 print "Error!\n";
33 }
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 =head3 args hash
40  
41 =head4 sid
42  
43 If this is specified, this base SID will be used instead of trying
44 to automatically figure out what to use.
45  
46 =head4 domain
47  
48 If this is set to 1, it will try to use get the domain SID instead
49 of the local SID.
50  
51 my $sidhelper->new({sid=>'S-1-5-21-1234-56789-10111213'});
52 if($sidhelper->{error}){
53 print "Error!\n";
54 }
55  
56 =cut
57  
58 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
59 0 my %args;
60 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
61 0 %args= %{$_[1]};
62 };
63  
64 0 my $self = {error=>undef, errorString=>""};
65 0 bless $self;
66  
67 0 ▶ if (defined($args{sid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"sid"}) 0 0 0
68 0 $self->{sid}=$args{sid};
69 }
70  
71 0 ▶ if (!defined($self->{sid})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sid"}) 0 0 0
72 0 my $sid;
73 0 ▶ if ($args{domain}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'domain'}) { } 0 0 0
74 0 $sid=`net getdomainsid`;
75 0 ▶ if ($? ne '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
76 0 $self->{error}=1;
77 0 $self->{errorString}='"net getdomainsid" exited with a non-zero';
78 0 warn('Samba-SIDhelper new:1: '.$self->{errorString});
79 0 return $self;
80 }
81 }else {
82 0 $sid=`net getlocalsid`;
83 0 ▶ if ($? ne '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
84 0 $self->{error}=2;
85 0 $self->{errorString}='"net getdomainsid" exited with a non-zero';
86 0 warn('Samba-SIDhelper new:2: '.$self->{errorString});
87 0 return $self;
88 }
89 }
90  
91 0 chomp($sid);
92  
93 0 my @sidA=split(/\:/, $sid);
94
95 0 $sid=$sidA[1];
96  
97 0 $sid=~s/ //g;
98  
99 0 $self->{sid}=$sid;
100 }
101  
102 0 return $self;
103 }
104  
105 =head2 uid2sid
106  
107 Convert a UID to SID.
108  
109 my $sid=$sidhelper->uid2sid('1002');
110 if ($sidhelper){
111 print "Error!\n";
112 }
113  
114 =cut
115  
116 0 ▶sub uid2sid{
Subroutine uid2sid: not called CC=3 SCAR=24.8
Pod documented
117 0 my $self=$_[0];
118 0 my $uid=$_[1];
119  
120 0 $self->errorblank;
121  
122 0 ▶ if (!defined($uid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid) 0 0 0
123 0 $self->{error}=3;
124 0 $self->{errorString}='No UID specified';
125 0 warn('Samba-SIDhelper uid2sid:3: '.$self->{errorString});
126 0 return undef;
127 }
128  
129 0 ▶ if ($uid !~ /^[0123456789]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uid =~ /^[0123456789]*$/) 0 0 0
130 0 $self->{error}=5;
131 0 $self->{errorString}='UID is not numeric';
132 0 warn('Samba-SIDhelper uid2sid:5: '.$self->{errorString});
133 0 return undef;
134 }
135  
136 0 $uid=$uid*2;
137 0 $uid=$uid+1000;
138  
139 0 return $self->{sid}.'-'.$uid;
140 }
141  
142 =head2 gid2sid
143  
144 Convert a GID to SID.
145  
146 my $sid=$sidhelper->gid2sid('1002');
147 if ($sidhelper){
148 print "Error!\n";
149 }
150  
151 =cut
152  
153 0 ▶sub gid2sid{
Subroutine gid2sid: not called CC=3 SCAR=24.8
Pod documented
154 0 my $self=$_[0];
155 0 my $gid=$_[1];
156  
157 0 $self->errorblank;
158  
159 0 ▶ if (!defined($gid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gid) 0 0 0
160 0 $self->{error}=4;
161 0 $self->{errorString}='No GID specified';
162 0 warn('Samba-SIDhelper gid2sid:4: '.$self->{errorString});
163 0 return undef;
164 }
165  
166 0 ▶ if ($gid !~ /^[0123456789]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gid =~ /^[0123456789]*$/) 0 0 0
167 0 $self->{error}=5;
168 0 $self->{errorString}='GID is not numeric';
169 0 warn('Samba-SIDhelper gid2sid:5: '.$self->{errorString});
170 0 return undef;
171 }
172  
173 0 $gid=$gid*2;
174 0 $gid=$gid+1001;
175  
176 0 return $self->{sid}.'-'.$gid;
177 }
178  
179 =head2 errorblank
180  
181 This is a internal function and should not be called.
182  
183 =cut
184  
185 #blanks the error flags
186 0 ▶sub errorblank{
Subroutine errorblank: not called CC=1 SCAR=6.9
Pod documented
187 0 my $self=$_[0];
188  
189 0 $self->{error}=undef;
190 0 $self->{errorString}="";
191  
192 0 return 1;
193 };
194  
195 =head1 ERROR CODES
196  
197 =head2 1
198  
199 "net getdomainsid" exited with a non-zero.
200  
201 =head2 2
202  
203 "net getlocalsid" exited with a non-zero.
204  
205 =head2 3
206  
207 No UID specified.
208  
209 =head2 4
210  
211 No UID specified.
212  
213 =head2 5
214  
215 Non-numeric value for UID or GID.
216  
217 =head1 SID DISCOVERY
218  
219 This requires Samba to be installed. The command net is used, which requires this
220 being ran as root.
221  
222 =head1 CONVERSION METHOD
223  
224 This uses the method from smbldap-tools.
225  
226 $sid=$uid*2+1000
227 $sid=$gid*2+1001
228  
229 This method means both both user and group info is can be stored in the same space. Groups are always
230 odd, while users are always even.
231  
232 =head1 AUTHOR
233  
234 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
235  
236 =head1 BUGS
237  
238 Please report any bugs or feature requests to C<bug-samba-sidhelper at rt.cpan.org>, or through
239 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Samba-SIDhelper>. I will be notified, and then you'll
240 automatically be notified of progress on your bug as I make changes.
241  
242  
243  
244  
245 =head1 SUPPORT
246  
247 You can find documentation for this module with the perldoc command.
248  
249 perldoc Samba::SIDhelper
250  
251  
252 You can also look for information at:
253  
254 =over 4
255  
256 =item * RT: CPAN's request tracker
257  
258 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Samba-SIDhelper>
259  
260 =item * AnnoCPAN: Annotated CPAN documentation
261  
262 L<http://annocpan.org/dist/Samba-SIDhelper>
263  
264 =item * CPAN Ratings
265  
266 L<http://cpanratings.perl.org/d/Samba-SIDhelper>
267  
268 =item * Search CPAN
269  
270 L<http://search.cpan.org/dist/Samba-SIDhelper/>
271  
272 =back
273  
274  
275 =head1 ACKNOWLEDGEMENTS
276  
277  
278 =head1 COPYRIGHT & LICENSE
279  
280 Copyright 2009 Zane C. Bowers, all rights reserved.
281  
282 This program is free software; you can redistribute it and/or modify it
283 under the same terms as Perl itself.
284  
285  
286 =cut
287  
288 2 1; # End of Samba::SIDhelper
↑ Summary