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