File Coverage

blib/lib/Net/LDAP/posixAccount.pm
Criterion Covered Total %
statement 15 106 14.1
branch 0 54 0.0
condition n/a
subroutine 5 8 62.5
pod 3 3 100.0
total 23 171 13.4


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