File Coverage

blib/lib/Passwd/DB.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Passwd::DB;
2              
3 1     1   2353 use DB_File;
  0            
  0            
4             use strict;
5             use Carp;
6             use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
7              
8             require Exporter;
9             require AutoLoader;
10              
11             @ISA = qw(Exporter AutoLoader);
12             # Items to export into callers namespace by default. Note: do not export
13             # names by default without a very good reason. Use EXPORT_OK instead.
14             # Do not simply export all your public functions/methods/constants.
15             @EXPORT = qw(getpwnam getpwuid);
16              
17             @EXPORT_OK = qw(mgetpwnam setpwinfo rmpwnam init_db modpwinfo);
18              
19             $VERSION = '1.03';
20              
21             my %_Create_Flags = (
22             'freebsd' => 0x200,
23             'linux' => 0100,
24             'solaris' => 0x100,
25             'hpux' => => 0400, # same as 0x100
26             );
27             my %_DB_Global = ();
28             $_DB_Global{'Database'} = $ENV{'PWDDatabase'};
29              
30             if (defined($_Create_Flags{$})) {
31             $_DB_Global{'CFlags'} = $_Create_Flags{$};
32             } else {
33             $_DB_Global{'CFlags'} = 0x100;
34             }
35              
36             # Preloaded methods go here.
37              
38             sub new {
39             my $class = shift;
40             my $self = {};
41            
42             bless $self, $class;
43             if ((scalar(@_) >= 3) || (scalar(@_) < 1)) {
44             croak "$class: $class->new('/path/to/database_file' [, 'create'])";
45             }
46             $self->{'CLASS'} = $class;
47             $self->init_db(@_);
48             return $self;
49             }
50              
51             sub init_db ($;$) {
52             my ($self, $db, $create) = @_;
53             if (ref($self)) {
54             $self->{'Database'} = $db;
55             } else {
56             if ($self eq 'Passwd::DB') {
57             $_DB_Global{'Database'} = $db;
58             } else {
59             $_DB_Global{'Database'} = $self;
60             $create = $db;
61             $db = $self;
62             }
63             }
64             $create =~ tr/A-Z/-z/;
65             if ($create eq 'create' || $create == 1) {
66             _create_db($db);
67             }
68             # print "Using PasswordDB functions on $Database\n";
69             }
70              
71             sub _create_db ($) {
72             my ($db) = @_;
73             my (%dbm);
74            
75             tie (%dbm, 'DB_File', $db, $_DB_Global{'CFlags'}|2, 0640, $DB_HASH) or do {
76             croak "Couldn't create $db : $!";
77             };
78             untie (%dbm);
79             }
80              
81             sub getpwnam ($) {
82             my ($self, $login) = @_;
83             my (%dbm, @info, $db);
84              
85             if (ref($self)) {
86             $db = $self->{'Database'};
87             } else {
88             $login = $self;
89             $db = $_DB_Global{'Database'};
90             }
91              
92             tie (%dbm, 'DB_File', $db, 0, 0400, $DB_HASH) or do {
93             croak "Couldn't access $db : $!";
94             };
95             if (!defined($dbm{$login})) {
96             untie %dbm;
97             return;
98             }
99             @info = (split(':',$dbm{$login}));
100             if (scalar(@info) == 7) {
101             splice (@info,4,0, "");
102             splice (@info,4,0, "");
103             untie %dbm;
104             return @info;
105             }
106             untie %dbm;
107             return;
108             }
109              
110             sub getpwuid ($) {
111             my ($self, $uid) = @_;
112             my (%dbm, @info, $key, $db);
113              
114             if (ref($self)) {
115             $db = $self->{'Database'};
116             } else {
117             $uid = $self;
118             $db = $_DB_Global{'Database'};
119             }
120              
121             tie (%dbm, 'DB_File', $db, 0, 0400, $DB_HASH) or do {
122             croak "Couldn't access $db : $!";
123             };
124             foreach $key (keys %dbm) {
125             @info = (split(':',$dbm{$key}));
126             if ($info[2] == $uid) {
127             if (scalar(@info) == 7) {
128             splice (@info,4,0, qw(0 0));
129             }
130             untie %dbm;
131             return (@info);
132             }
133             }
134             untie %dbm;
135             return;
136             }
137              
138             sub mgetpwnam ($) { # same as getpwnam without quota and comment
139             my ($self, $login) = @_;
140             my (%dbm, @info, $db);
141              
142             if (ref($self)) {
143             $db = $self->{'Database'};
144             } else {
145             $login = $self;
146             $db = $_DB_Global{'Database'};
147             }
148              
149             tie (%dbm, 'DB_File', $db, 0, 0400, $DB_HASH) or do {
150             croak "Couldn't access $db : $!";
151             };
152             if (!defined($dbm{$login})) {
153             untie %dbm;
154             return;
155             }
156             @info = (split(':',$dbm{$login}));
157             untie %dbm;
158             if (scalar(@info) == 7) {
159             return (@info);
160             }
161             return;
162             }
163             sub modpwinfo (@) {
164             my ($self, @info) = @_;
165             my (%dbm, $loginfo, $db, $size, $err);
166              
167             if (ref($self)) {
168             $db = $self->{'Database'};
169             $size = scalar(@info);
170             } else {
171             $size = unshift @info, $self;
172             $db = $_DB_Global{'Database'};
173             }
174              
175             if ($size != 7) {
176             croak "Incorrect number of arguments for modpwinfo";
177             }
178             tie (%dbm, 'DB_File', $db, 2, 0600, $DB_HASH) or do {
179             croak "Couldn't access $db : $!";
180             };
181             if (!defined($dbm{$info[0]})) {
182             return 2;
183             }
184             $loginfo = join(':',@info);
185             $dbm{$info[0]} = $loginfo;
186             $err = (!defined($dbm{$info[0]}));
187             untie %dbm;
188              
189             return $err;
190             }
191              
192             sub setpwinfo (@) {
193             my ($self, @info) = @_;
194             my (%dbm, $loginfo, $db, $size, $err);
195              
196             if (ref($self)) {
197             $db = $self->{'Database'};
198             $size = scalar(@info);
199             } else {
200             $size = unshift @info, $self;
201             $db = $_DB_Global{'Database'};
202             }
203              
204             if ($size != 7) {
205             croak "Incorrect number of arguments for setpwinfo";
206             }
207             tie (%dbm, 'DB_File', $db, 2, 0600, $DB_HASH) or do {
208             croak "Couldn't access $db : $!";
209             };
210             $loginfo = join(':',@info);
211             $dbm{$info[0]} = $loginfo;
212             $err = (!defined($dbm{$info[0]}));
213             untie %dbm;
214              
215             return $err;
216             }
217              
218             sub rmpwnam ($) {
219             my ($self, $login) = @_;
220             my (%dbm, $db);
221              
222             if (ref($self)) {
223             $db = $self->{'Database'};
224             } else {
225             $login = $self;
226             $db = $_DB_Global{'Database'};
227             }
228              
229             tie (%dbm, 'DB_File', $db, 2, 0600, $DB_HASH) or do {
230             croak "Couldn't access $db : $!";
231             };
232             my $err = delete $dbm{$login};
233             untie %dbm;
234              
235             if (!defined($err)) {
236             return 1;
237             }
238             return 0;
239             }
240            
241              
242             # Autoload methods go after =cut, and are processed by the autosplit program.
243              
244             1;
245             __END__