8 use Digest::MD5 qw(md5_base64 md5_hex);#TODO Some better algorithm?
12 use Apache2::Connection;
14 my( %privnames, %privnums );
16 our @EXPORT = qw(&addUser &emailConfirm &checkConfirmHash &saltedPasswd &genAuthToken &checkAuthToken &hasRight &getRightDefs &genResetHash &changePasswd &pushProfile &emailCheck);
18 sub emailCheck( $$ ) {
19 my( $email, $tables ) = @_;
21 return 'Does not look like an email address' unless ( ( $newmail ) = ( $email =~ /^([^,? "'`;<>]+@[^@,?\/ "'`;<>]+)$/ ) );#make sure the mail is not only reasonable looking, but safe to work with too
22 return 'Email too long' if length $newmail > 255;
23 return 'An account for this email address already exists' if( ( defined $tables ) && $tables->hasEmail( $newmail ) );
24 return ( undef, $newmail );
27 sub saltedPasswd( $$ ) {
28 my( $email, $passwd ) = @_;
29 my $salt = $config{'passwdsalt'};
30 return md5_base64( "$email:$passwd:$salt" );
33 sub genResetHash( $$$$ ) {
34 my( $id, $email, $login, $passwd ) = @_;
35 my $salt = $config{'regmailsalt'};
36 return md5_hex( "$id:$email:$login:$passwd:$salt" );
39 sub emailConfirm( $ ) {
41 my $salt = $config{'regmailsalt'};
42 return md5_hex( $email.$salt );
45 sub checkConfirmHash( $$ ) {
46 my( $email, $hash ) = @_;
47 return 0 unless( ( defined $email ) && ( defined $hash ) );
48 my( $expected ) = emailConfirm( $email );
49 return ( $expected eq $hash );
53 my( $tables, $name, $email, $passwd ) = @_;
54 my $salted = saltedPasswd( $email, $passwd );
55 tlog( "Creating user $email" . ( ( defined $name ) ? " - $name" : '' ) );
56 my $id = $tables->addUser( $name, $email, $salted );
57 tlog( "User ($email) id: $id" );
61 sub changePasswd( $$$$ ) {
62 my( $tables, $id, $passwd, $email ) = @_;
63 my $salted = saltedPasswd( $email, $passwd );
64 $tables->changePasswd( $id, $salted );
67 sub genAuthToken( $$$$$ ) {
68 my( $tables, $id, $req, $rights, $name ) = @_;
69 unless( defined $rights ) {#Just logged in
70 my $from = $req->connection()->remote_ip();
71 $tables->setLastLog( $id, $from );
72 $rights = $tables->rights( $id );
74 my $haveRights = scalar @{$rights};
76 my $ip = $req->connection()->remote_ip();
77 return "$id:$haveRights:$time:".md5_hex( "$id:$time:$ip:".$config{'authsalt'} ).":$name";
80 sub checkAuthToken( $$$ ) {
81 my( $tables, $req, $token ) = @_;
82 my( $id, $haveRights, $time, $hex, $name ) = defined( $token ) ? split( /:/, $token ) : ();
83 return ( 0, 0, 0, [], "Not logged in", undef ) unless( defined $hex );
84 my $ip = $req->connection()->remote_ip();
85 my $expected = md5_hex( "$id:$time:$ip:".$config{'authsalt'} );
87 my $tokOk = ( $expected eq $hex );
88 my $authed = ( $tokOk && ( $time + $config{'authtime'} > $actTime ) );
89 my $regen = $authed && ( $time + $config{'regenauthtime'} < $actTime );
92 foreach( @{$tables->rights( $id )} ) {
95 $r{'name'} = $privnames{$r{'id'}};
99 return ( $authed, $id, $regen, $rights, $authed ? undef : ( $tokOk ? "Login timed out" : "Not logged in" ), $name );
103 my( $rights, $name ) = @_;
104 foreach( @{$rights} ) {
105 return 1 if( $_->{'name'} eq $name );
111 return ( \%privnums, \%privnames );
114 sub pushProfile( $$$$ ) {
115 my( $tables, $id, $oldData, $data ) = @_;
116 my( $email, $passwd ) = ( $data->{'email'}, $data->{'current_password'} );
117 if( ( defined $passwd ) && ( $passwd ne '' ) ) {
118 my $salted = saltedPasswd( $email, $passwd );
119 $tables->setEmail( $id, $email, $salted );
121 $data->{'login'} = undef if ( defined $data->{'login'} ) && ( $data->{'login'} eq '' );
122 $data->{'xmpp'} = undef if ( defined $data->{'xmpp'} ) && ( $data->{'xmpp'} eq '' );
123 $tables->pushProfile( $id, $data->{'login'}, $data->{'xmpp'}, $data->{'email_time'}, $data->{'xmpp_time'} );
124 changePasswd( $tables, $id, $data->{'password'}, $email ) if ( defined $data->{'password'} ) && ( $data->{'password'} ne '' );
127 checkConf( [ 'passwdsalt', 'regmailsalt', 'authsalt' ] );
128 defConf( { 'authtime' => 2100, 'regenauthtime' => 300 } );
130 open PRIVS, $directory."/rights" or die "Could not open privilege definitions\n";
132 my( $num, $name ) = /^(\d+)\s+(.*)$/ or die "Invalid syntax in privileges\n";
133 $privnames{$num} = $name;
134 $privnums{$name} = $num;