File Coverage

blib/lib/Digest/UserSID.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Digest::UserSID;
2             require 5.001;
3             ##############################################################################
4             # $Id: UserSID.pm,v 1.5 2001/03/21 17:32:11 unrzc9 Exp $ #
5             #
6             # See the bottom of this file for the POD documentation. Search for the
7             # string '=head'.
8             # You can run this file through either pod2man or pod2html to produce pretty
9             # documentation in manual or html file format (these utilities are part of the
10             # Perl 5 distribution).
11             #
12             # Copyright 1999-2001 Wolfgang Wiese. All rights reserved.
13             # It may be used and modified freely, but I do request that this copyright
14             # notice remain attached to the file. You may modify this module as you
15             # wish, but if you redistribute a modified version, please attach a note
16             # listing the modifications you have made.
17             #
18             ##############################################################################
19             # Last Modified on: $Date: 2001/03/21 17:32:11 $
20             # By: $Author: unrzc9 $
21             # Version: $Revision: 1.5 $
22             ##############################################################################
23 1     1   638 use strict;
  1         2  
  1         32  
24 1     1   1402 use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
  0            
  0            
25              
26             BEGIN {
27             use Exporter ();
28             use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
29             $VERSION = do { my @r = (q$Revision: 1.5 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
30             $UserSID::VERSION = '$Id: UserSID.pm,v 1.5 2001/03/21 17:32:11 unrzc9 Exp $';
31             @ISA = qw(Exporter);
32             @EXPORT = qw(removewebsid getuserbysid makewebsid checkwebsid USID_check USID_add USID_update
33             read new create remove update);
34             %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
35             @EXPORT_OK = qw();
36             }
37             use vars @EXPORT_OK;
38              
39              
40             $UserSID::digest = "hex";
41             # Could be: binary, hex, base64. See the manual of Digest::SHA1 for more.
42             $UserSID::FILE = "/tmp/sid";
43             # Name for the dbm-file in which username, sid and time will be stored
44             $UserSID::MAXSECONDS = 60*30;
45             # Sets the time how long the sid is valid
46             $UserSID::CHECKTIMEONLY = 1;
47             # If this is unlike 0 the routine 'checkwebsid' will check only
48             # for the valid timerange but not of the userclient is valid.
49             # Additionally in 'makewebsid' the used variables for encryption
50             # dont use the typical environment-variables that are used to determine
51             # the client.
52             ##############################################################################
53             # Exported Subroutines
54             ##############################################################################
55             sub removewebsid {
56             my $name = shift;
57             my $sha = shift;
58             my $file = shift || $UserSID::FILE;
59             my $maxseconds = shift || $UserSID::MAXSECONDS;
60              
61             if ((not $name) || (not $sha)) {
62             return 0;
63             }
64             if (not %UserSID::DATA) {
65             &LoadSIDData($file);
66             }
67              
68             if ($UserSID::DATA{$name}) {
69             delete $UserSID::DATA{$name};
70             return &SaveSIDData($file);
71             } else {
72             return 0;
73             }
74              
75             }
76             ##############################################################################
77             sub getuserbysid {
78             my $code = shift;
79             my $file = shift || $UserSID::FILE;
80             my $key;
81             my ($checksha, $checktime);
82             my $found;
83            
84             if (not $code) {
85             return;
86             }
87             if (not %UserSID::DATA) {
88             &LoadSIDData($file);
89             }
90             foreach $key (keys %UserSID::DATA) {
91             ($checksha, $checktime) = split(/\t/,$UserSID::DATA{$key},2);
92             if ($checksha eq $code) {
93             $found = $key;
94             last;
95             }
96             }
97             return $found;
98             }
99             ##############################################################################
100             sub makewebsid {
101             my $name = shift;
102             my $code;
103             my $ip;
104            
105             if (not $name) {
106             return;
107             }
108             if ($UserSID::CHECKTIMEONLY) {
109             $ip = $ENV{'HTTP_X_FORWARDED_FROM'} || $ENV{'REMOTE_ADDR'} || $ENV{'REMOTE_HOST'};
110             $code = $name.$ip;
111             } else {
112             my $referer = &GetSecondaryDN($ENV{'SERVER_NAME'});
113             my $agent = $ENV{'HTTP_USER_AGENT'};
114             $ip = $ENV{'HTTP_X_FORWARDED_FROM'} || $ENV{'REMOTE_ADDR'} || $ENV{'REMOTE_HOST'};
115             $code = $referer.$agent.$ip;
116             }
117              
118             if (not $code) {
119             $code = $<;
120             }
121             return &USID_add($name,$code);
122             }
123             ##############################################################################
124             sub checkwebsid {
125             my $name = shift;
126             my $pass = shift;
127            
128             my $res = &USID_check($name,$pass);
129            
130             if (not $res) {
131             return 0;
132             }
133             if ($UserSID::CHECKTIMEONLY) {
134             return 1;
135             }
136              
137            
138             my $referer = &GetSecondaryDN($ENV{'HTTP_REFERER'});
139             my $agent = $ENV{'HTTP_USER_AGENT'};
140             my $ip = $ENV{'HTTP_X_FORWARDED_FROM'} || $ENV{'REMOTE_ADDR'} || $ENV{'REMOTE_HOST'};
141             my $code = $referer.$agent.$ip;
142             if (not $code) {
143             $code = $<;
144             }
145             $code = &CreateKey($code);
146            
147             if ($code eq $pass) {
148             return 1;
149             } else {
150             return 0;
151             }
152             }
153             ##############################################################################
154             sub USID_add {
155             my $name = shift;
156             my $string = shift;
157             my $file = shift || $UserSID::FILE;
158            
159             if ((not $name) || (not $string)) {
160             return 0;
161             }
162            
163             my $sid = new Digest::UserSID($file);
164             my $res = $sid->create($name,$string);
165             if ($res) {
166             return $sid->{'sha'};
167             } else {
168             return 0;
169             }
170             }
171             ##############################################################################
172             sub USID_check {
173             my $name = shift;
174             my $sha = shift;
175             my $file = shift || $UserSID::FILE;
176             my $maxseconds = shift || $UserSID::MAXSECONDS;
177            
178             if ((not $name) || (not $sha)) {
179             return 0;
180             }
181             if (not %UserSID::DATA) {
182             &LoadSIDData($file);
183             }
184             if ($UserSID::DATA{$name}) {
185             my ($checksha, $checktime) = split(/\t/,$UserSID::DATA{$name},2);
186             if ($checksha eq $sha) {
187             my $nowtime = time;
188             if (($nowtime-$checktime) > $maxseconds) {
189             delete $UserSID::DATA{$name};
190             &SaveSIDData($file);
191             return 0;
192             } else {
193             return 1;
194             }
195             } else {
196             return 0;
197             }
198             } else {
199             return 0;
200             }
201             }
202             ##############################################################################
203             sub USID_update {
204             my $name = shift;
205             my $sha = shift;
206             my $file = shift || $UserSID::FILE;
207             my $maxseconds = shift || $UserSID::MAXSECONDS;
208              
209             if ((not $name) || (not $sha)) {
210             return 0;
211             }
212             if (not %UserSID::DATA) {
213             &LoadSIDData($file);
214             }
215             if ($UserSID::DATA{$name}) {
216             my ($checksha, $checktime) = split(/\t/,$UserSID::DATA{$name},2);
217             if ($checksha eq $sha) {
218             $checktime = time;
219             $UserSID::DATA{$name} = "$checksha\t$checktime";
220             return &SaveSIDData($file);
221             } else {
222             return 0;
223             }
224             } else {
225             return 0;
226             }
227             }
228             ##############################################################################
229             sub read {
230             my $sid = shift;
231             my $user = shift;
232             my $file = shift || $UserSID::FILE;
233              
234             if ((not $sid) || (not $user)) {
235             return 0;
236             }
237             if (not %UserSID::DATA) {
238             &LoadSIDData($file);
239             }
240             if ($UserSID::DATA{$user}) {
241             my ($checksha, $checktime) = split(/\t/,$UserSID::DATA{$user},2);
242             $sid->{'sha'} = $checksha;
243             $sid->{'time'} = $checktime;
244             $sid->{'user'} = $user;
245             return 1;
246             } else {
247             return 0;
248             }
249             }
250             ##############################################################################
251             sub update {
252             my $sid = shift;
253             my $file = shift || $UserSID::FILE;
254            
255             if ((not $sid) || (not $sid->{'user'})) {
256             return 0;
257             }
258             $sid->{'time'} = time;
259             return &AddNewSID($sid);
260             }
261             ##############################################################################
262             sub remove {
263             my $sid = shift;
264             my $file = shift || $UserSID::FILE;
265             my $key;
266            
267             if ((not $sid) || (not $sid->{'user'})) {
268             return 0;
269             }
270            
271             if (not %UserSID::DATA) {
272             &LoadSIDData($file);
273             }
274            
275             delete $UserSID::DATA{$sid->{'user'}};
276             return &SaveSIDData($file);
277              
278             }
279             ##############################################################################
280             sub create {
281             my $sid = shift;
282             my $user = shift;
283             my $string = shift || localtime;
284             my $digest = shift || $UserSID::digest;
285            
286             if ((not $sid) || (not $user)) {
287             return;
288             }
289            
290             $sid->{'user'} = $user;
291             $sid->{'time'} = time;
292             $sid->{'sha'} = &CreateKey($string,$digest);
293              
294             return &AddNewSID($sid);
295             }
296             ##############################################################################
297             sub new {
298             my $that = shift;
299             my $file = shift || $UserSID::FILE;
300             my $class = ref($that) || $that;
301             my $self = { };
302            
303             if (($file ne $UserSID::FILE) || (not %UserSID::DATA)) {
304             &LoadSIDData($file);
305             }
306             bless $self, $class;
307             return $self;
308             }
309              
310             ##############################################################################
311             # Privat Subroutines
312             ##############################################################################
313             sub GetSecondaryDN {
314             my $string = shift || $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'};
315            
316             if (not $string) {
317             return;
318             }
319             $string =~ s/^([a-z]+):\/\///;
320             if ($string =~ /^([^:\/]+)(:[0-9]+)?/i) {
321             $string = $1;
322             }
323             my @domain = split(/\./,$string);
324             my $tld = pop(@domain);
325             my $secondary = pop(@domain);
326             return "$secondary.$tld";
327             }
328             ##############################################################################
329             sub CreateKey {
330             my $string = shift;
331             my $digest = shift || $UserSID::digest;;
332             my $i;
333             my $result;
334            
335             if ($digest =~ /base/i) {
336             $result = sha1_base64($string);
337             } elsif ($digest =~ /hex/) {
338             $result = sha1_hex($string);
339             } else {
340             $result = sha1($string);
341             }
342             while($result =~ /[^a-zA-Z0-9\.\-_]/) {
343             $i++;
344             $i = $i % 9;
345             $result =~ s/[^a-zA-Z0-9\.\-_]/$i/i;
346             }
347            
348             return $result;
349             }
350             ##############################################################################
351             sub AddNewSID {
352             my $sid = shift;
353             my $file = shift || $UserSID::FILE;
354             my $key;
355            
356             if ((not $sid) || (not $sid->{'user'})) {
357             return 0;
358             }
359            
360             if (not %UserSID::DATA) {
361             &LoadSIDData($file);
362             }
363             $UserSID::DATA{$sid->{'user'}} = "$sid->{'sha'}\t$sid->{'time'}";
364             return &SaveSIDData($file);
365             }
366             ##############################################################################
367             sub LoadSIDData {
368             my $file = shift;
369             my %hash;
370             my $lockfile = $file.".pag";
371            
372            
373             dbmopen(%hash,$file,0644);
374             %UserSID::DATA = %hash;
375             dbmclose(%hash);
376            
377             }
378             ##############################################################################
379             sub SaveSIDData {
380             my $file = shift;
381             my %hash = %UserSID::DATA;
382             my $lockfile = $file.".pag";
383            
384             if (not $file) {
385             return 0;
386             }
387            
388             dbmopen(%hash,$file,0644);
389             %hash = %UserSID::DATA;
390             dbmclose(%hash);
391            
392             return 1;
393              
394             }
395             ##############################################################################
396             # EOFunctions
397             ##############################################################################
398             1;
399             __END__