File Coverage

blib/lib/Net/LDAP/posixAccount.pm
Criterion Covered Total %
statement 18 108 16.6
branch 0 52 0.0
condition n/a
subroutine 6 9 66.6
pod 3 3 100.0
total 27 172 15.7


line stmt bran cond sub pod time code
1             package Net::LDAP::posixAccount;
2              
3 2     2   370075 use warnings;
  2         3  
  2         141  
4 2     2   8 use strict;
  2         4  
  2         64  
5 2     2   1227 use Net::LDAP::Entry;
  2         329604  
  2         60  
6 2     2   934 use Sys::User::UIDhelper;
  2         1036  
  2         58  
7 2     2   728 use Sys::Group::GIDhelper;
  2         935  
  2         60  
8 2     2   11 use base 'Error::Helper';
  2         4  
  2         895  
9              
10             =head1 NAME
11              
12             Net::LDAP::posixAccount - Creates new Net::LDAP::Entry objects for a posixAccount entry.
13              
14             =head1 VERSION
15              
16             Version 1.0.0
17              
18             =cut
19              
20             our $VERSION = '1.0.0';
21              
22             =head1 SYNOPSIS
23              
24             use Net::LDAP::posixAccount;
25              
26             # Initiates the module with a base DN of 'ou=users,dc=foo'.
27             my $foo = Net::LDAP::posixAccount->new(baseDN=>'ou=user,dc=foo');
28              
29             # create the user vvelox with a gid of 404 and a uid of 404
30             my $entry = $foo->create(name=>'vvelox', gid=>'404', uid=>'404');
31              
32             # add it using $ldap, a previously created Net::LDAP object
33             $entry->update($ldap);
34              
35             =head1 METHODS
36              
37             =head2 new
38              
39             This initiates the module. It accepts one arguement, a hash. Please See below
40             for accepted values.
41              
42             - baseDN :: This is a required value and is the base that the entry will
43             be created under.
44              
45             - topless :: This is a perl boolean value. If this is set to true, the
46             objectClass top is not present.
47              
48             =cut
49              
50             sub new {
51 0     0 1   my ( $blank, %args ) = @_;
52              
53             #returns undef if the baseDN is not set
54 0           my $self = {
55             perror => undef,
56             error => undef,
57             errorLine => undef,
58             errorFilename => undef,
59             errorString => "",
60             errorExtra => {
61             all_errors_fatal => 1,
62             flags => {
63             1 => 'missing_name',
64             2 => 'missing_uid',
65             3 => 'missing_gid',
66             4 => 'invalid_value',
67             5 => 'missing_baseDN',
68             6 => 'invalid_baseDN',
69             },
70             fatal_flags => {},
71             perror_not_fatal => 0,
72             },
73             baseDN => undef,
74             topless => undef,
75             };
76 0           bless $self;
77              
78             #if it is defined it sets the topless setting to what ever it is
79 0 0         if ( defined( $args{topless} ) ) {
80 0           $self->{topless} = $args{topless};
81             }
82              
83 0 0         if ( !defined( $args{baseDN} ) ) {
84 0           $self->{error} = 5;
85 0           $self->{errorString} = 'baseDN is not defined';
86 0           $self->{perror} = 1;
87 0           $self->warn;
88 0           return $self;
89             }
90              
91             # check to see if the base DN looks legit
92 0 0         if ( $args{baseDN} !~ /^(?:(?:[A-Za-z0-9]+=[^,]+),\s*)*(?:[A-Za-z0-9]+=[^,]+)$/ ) {
93 0           $self->{error} = 6;
94 0           $self->{errorString} = 'baseDN, "' . $args{baseDN} . '", does not appear to be a valid DN';
95 0           $self->{perror} = 1;
96 0           $self->warn;
97 0           return $self;
98             }
99              
100 0           $self->{baseDN} = $args{baseDN};
101              
102 0           return $self;
103             } ## end sub new
104              
105             =head2 create
106              
107             Creates a new Net::LDAP::Entry object.
108              
109             - name :: The name of the user.
110              
111             - cn :: What the common name should be for a user. This defaults to the username if it is not defined.
112              
113             - uid ::This is the UID number of a user. If set to 'AUTO', Sys::User::UIDhelper will be used.
114              
115             - gid :: This is GID number of a user. If set to 'AUTO', Sys::Group::GIDhelper will be used.
116              
117             - gecos :: This is the GECOS field for a user. If it is not defined, the name is used.
118              
119             - loginShell This is the login shell for the user.
120             - default :: /sbin/nologin
121              
122             - home ::This is the home directory of a user.
123             - default :: /home/$name
124              
125             - primary :: This is the attribute that will be used for when creating the entry.
126             'uid', 'uidNumber', or 'cn' are the accepted value. The default is 'uid'.
127              
128             - description :: This is the LDAP description field. If it is not defined, it is set to gecos.
129              
130             - minUID :: This is the min UID that will be used if 'uid' is set to 'AUTO'.
131             - default :: 1001
132              
133             - maxUID This is the max UID that will be used if 'uid' is set to 'AUTO'.
134             - default :: 64000
135              
136             - minGID :: This is the min GID that will be used if 'gid' is set to 'AUTO'.
137             - default :: 1001
138              
139             - maxGID :: This is the max GID that will be used if 'gid' is set to 'AUTO'.
140             - default :: 64000
141              
142             =cut
143              
144             sub create {
145 0     0 1   my ( $self, %args ) = @_;
146              
147 0           $self->errorblank;
148              
149             #error if name is not defined
150 0 0         if ( !defined( $args{name} ) ) {
151 0           $self->{error} = 1;
152 0           $self->{errorString} = 'name not defined';
153 0           $self->warn;
154             }
155              
156             #set CN to name if it is not defined
157 0 0         if ( !defined( $args{cn} ) ) {
158 0           $args{cn} = $args{name};
159             }
160              
161             #error if uid is not defined
162 0 0         if ( !defined( $args{uid} ) ) {
163 0           $self->{error} = 2;
164 0           $self->{errorString} = 'uid not defined';
165 0           $self->warn;
166             }
167              
168             #handles choosing the UID if it is set to AUTO
169 0 0         if ( $args{uid} eq 'AUTO' ) {
170             #sets the minUID if it is not defined
171 0 0         if ( !defined( $args{minUID} eq '1001' ) ) {
172 0           $args{uid} = '1001';
173             }
174              
175             #sets the maxUID if it is not defined
176 0 0         if ( !defined( $args{minUID} ) ) {
177 0           $args{uid} = '64000';
178             }
179              
180             #creates it
181             my $uidhelper = Sys::User::UIDhelper->new(
182             min => $args{minUID},
183             max => $args{maxUID}
184 0           );
185             #gets the first free one
186 0           $args{uid} = $uidhelper->firstfree();
187             } ## end if ( $args{uid} eq 'AUTO' )
188              
189             #error if gid is not defined
190 0 0         if ( !defined( $args{gid} ) ) {
191 0           $self->{error} = 3;
192 0           $self->{errorString} = 'gid not defined';
193 0           $self->warn;
194             }
195              
196             #handles choosing the GID if it is set to AUTO
197 0 0         if ( $args{gid} eq 'AUTO' ) {
198             #sets the minUID if it is not defined
199 0 0         if ( !defined( $args{minGID} eq '1001' ) ) {
200 0           $args{uid} = '1001';
201             }
202              
203             #sets the maxUID if it is not defined
204 0 0         if ( !defined( $args{minGID} ) ) {
205 0           $args{uid} = '64000';
206             }
207              
208             #creates it
209             my $gidhelper = Sys::Group::GIDhelper->new(
210             min => $args{minGID},
211             max => $args{maxGID}
212 0           );
213             #gets the first free one
214 0           $args{gid} = $gidhelper->firstfree();
215             } ## end if ( $args{gid} eq 'AUTO' )
216              
217             #set gecos to name if it is not defined
218 0 0         if ( !defined( $args{gecos} ) ) {
219 0 0         if ( defined( $args{description} ) ) {
220 0           $args{gecos} = $args{description};
221             } else {
222 0           $args{gecos} = $args{name};
223             }
224             }
225              
226             #sets the description field
227 0 0         if ( !defined( $args{description} ) ) {
228 0 0         if ( defined( $args{gecos} ) ) {
229 0           $args{description} = $args{gecos};
230             }
231             }
232              
233             #sets the loginShell to '/sbin/nologin' if it is not defined
234 0 0         if ( !defined( $args{loginShell} ) ) {
235 0           $args{loginShell} = '/sbin/nologin';
236             }
237              
238             #sets the home if it is not specified
239 0 0         if ( !defined( $args{home} ) ) {
240 0           $args{loginShell} = '/home/' . $args{name};
241             }
242              
243             #set primary if it is not defined
244 0 0         if ( !defined( $args{primary} ) ) {
245 0           $args{primary} = 'uid';
246             }
247              
248             #
249 0           my @primary = ( 'uid', 'cn', 'uidNumber' );
250 0           my $dn = undef;
251 0           my $primaryInt = 0;
252 0           while ( defined( $primary[$primaryInt] ) ) {
253             #when a match is found, use it to begin forming the the DN
254 0 0         if ( $args{primary} eq $primary[$primaryInt] ) {
255 0           $dn = $args{primary} . '=';
256             }
257 0           $primaryInt++;
258             }
259              
260             #error if none is matched
261 0 0         if ( !defined($dn) ) {
262 0           $self->{error} = 4;
263 0           $self->{errorString} = 'primary is a invalid value';
264 0           $self->warn;
265             }
266              
267             #forms the DN if it is using the UID
268 0 0         if ( $args{primary} eq 'uid' ) {
269 0           $dn = $dn . $args{name};
270             }
271              
272             #forms the DN if it is using the uidNumber
273 0 0         if ( $args{primary} eq 'uidNumber' ) {
274 0           $dn = $dn . $args{uid};
275             }
276              
277             #forms the DN if it is using the CN
278 0 0         if ( $args{primary} eq 'cn' ) {
279 0           $dn = $dn . $args{cn};
280             }
281              
282             #full forms the DN
283 0           $dn = $dn . ',' . $self->{baseDN};
284              
285             #creates a new object
286 0           my $entry = Net::LDAP::Entry->new;
287              
288             #sets the dn
289 0           $entry->dn($dn);
290              
291             #adds top if it is not topless
292 0 0         if ( !$args{topless} ) {
293 0           $entry->add( objectClass => ['top'] );
294             }
295              
296             #adds the various attributes
297             $entry->add(
298             objectClass => [ 'account', 'posixAccount' ],
299             uidNumber => [ $args{uid} ],
300             gidNumber => [ $args{gid} ],
301             uid => [ $args{name} ],
302             homeDirectory => [ $args{home} ],
303             gecos => [ $args{gecos} ],
304             loginShell => [ $args{loginShell} ],
305             cn => [ $args{cn} ],
306 0           description => [ $args{description} ]
307             );
308              
309 0           return $entry;
310             } ## end sub create
311              
312             =head2 errorBlank
313              
314             A internal function user for clearing an error.
315              
316             =cut
317              
318             #blanks the error flags
319             sub errorBlank {
320 0     0 1   my $self = $_[0];
321              
322             #error handling
323 0           $self->{error} = undef;
324 0           $self->{errorString} = "";
325              
326 0           return 1;
327             }
328              
329             =head1 Error Codes/Flags
330              
331             L is used and all errors are considered fatal.
332              
333             =head2 1/missing_name
334              
335             'name' not defined.
336              
337             =head2 2/missing_uid
338              
339             'uid' not defined.
340              
341             =head2 3/missing_gid
342              
343             'gid' not defined.
344              
345             =head2 4/invalid_value
346              
347             The primary value is a invalid value.
348              
349             =head2 5/missing_baseDN
350              
351             Missing baseDN.
352              
353             =head2 6/invalid_baseDN
354              
355             The specified base DN does does not appear to be a DN.
356              
357             Checked via the regex below.
358              
359             ^(?:(?:[A-Za-z0-9]+=[^,]+),\s*)*(?:[A-Za-z0-9]+=[^,]+)$
360              
361             =head1 AUTHOR
362              
363             Zane C. Bowers, C<< >>
364              
365             =head1 BUGS
366              
367             Please report any bugs or feature requests to C, or through
368             the web interface at L. I will be notified, and then you'll
369             automatically be notified of progress on your bug as I make changes.
370              
371              
372              
373              
374             =head1 SUPPORT
375              
376             You can find documentation for this module with the perldoc command.
377              
378             perldoc Net::LDAP::posixAccount
379              
380              
381             You can also look for information at:
382              
383             =over 4
384              
385             =item * RT: CPAN's request tracker
386              
387             L
388              
389             =item * AnnoCPAN: Annotated CPAN documentation
390              
391             L
392              
393             =item * CPAN Ratings
394              
395             L
396              
397             =item * Search CPAN
398              
399             L
400              
401             =back
402              
403              
404             =head1 ACKNOWLEDGEMENTS
405              
406              
407             =head1 COPYRIGHT & LICENSE
408              
409             Copyright 2008 Zane C. Bowers, all rights reserved.
410              
411             This program is free software; you can redistribute it and/or modify it
412             under the same terms as Perl itself.
413              
414              
415             =cut
416              
417             1; # End of Net::LDAP::posixAccount