File Coverage

blib/lib/Samba/SIDhelper.pm
Criterion Covered Total %
statement 6 71 8.4
branch 0 20 0.0
condition n/a
subroutine 2 6 33.3
pod 4 4 100.0
total 12 101 11.8


line stmt bran cond sub pod time code
1             package Samba::SIDhelper;
2              
3 1     1   20813 use warnings;
  1         3  
  1         27  
4 1     1   4 use strict;
  1         2  
  1         660  
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             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             sub new {
59 0     0 1   my %args;
60 0 0         if(defined($_[1])){
61 0           %args= %{$_[1]};
  0            
62             };
63              
64 0           my $self = {error=>undef, errorString=>""};
65 0           bless $self;
66              
67 0 0         if (defined($args{sid})) {
68 0           $self->{sid}=$args{sid};
69             }
70              
71 0 0         if (!defined($self->{sid})) {
72 0           my $sid;
73 0 0         if ($args{domain}) {
74 0           $sid=`net getdomainsid`;
75 0 0         if ($? ne '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 0         if ($? ne '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             sub uid2sid{
117 0     0 1   my $self=$_[0];
118 0           my $uid=$_[1];
119              
120 0           $self->errorblank;
121              
122 0 0         if (!defined($uid)) {
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 0         if ($uid !~ /^[0123456789]*$/) {
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             sub gid2sid{
154 0     0 1   my $self=$_[0];
155 0           my $gid=$_[1];
156              
157 0           $self->errorblank;
158              
159 0 0         if (!defined($gid)) {
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 0         if ($gid !~ /^[0123456789]*$/) {
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             sub errorblank{
187 0     0 1   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<< >>
235              
236             =head1 BUGS
237              
238             Please report any bugs or feature requests to C, or through
239             the web interface at L. 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
259              
260             =item * AnnoCPAN: Annotated CPAN documentation
261              
262             L
263              
264             =item * CPAN Ratings
265              
266             L
267              
268             =item * Search CPAN
269              
270             L
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             1; # End of Samba::SIDhelper