]> mj.ucw.cz Git - pciids.git/blobdiff - PciIds/Users.pm
update-web: Synchronize mirrors
[pciids.git] / PciIds / Users.pm
index 3f6e38eebdb3bdf4073a34168af61a20ff3aba0e..bf6ced2b2becf4ba9753c4f8991f1013774887be 100644 (file)
@@ -1,3 +1,21 @@
+#      PciIds web database
+#      Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
+#
+#      This program is free software; you can redistribute it and/or modify
+#      it under the terms of the GNU General Public License as published by
+#      he Free Software Foundation; either version 2 of the License, or
+#      (at your option) any later version.
+#
+#      This program is distributed in the hope that it will be useful,
+#      but WITHOUT ANY WARRANTY; without even the implied warranty of
+#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#
+#      GNU General Public License for more details.
+#
+#      You should have received a copy of the GNU General Public License
+#      along with this program; if not, write to the Free Software
+#      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 package PciIds::Users;
 use strict;
 use warnings;
@@ -13,7 +31,16 @@ use Apache2::Connection;
 
 my( %privnames, %privnums );
 
-our @EXPORT = qw(&addUser &emailConfirm &checkConfirmHash &saltedPasswd &genAuthToken &checkAuthToken &hasRight &getRightDefs &genResetHash &changePasswd &pushProfile);
+our @EXPORT = qw(&addUser &emailConfirm &checkConfirmHash &saltedPasswd &genAuthToken &checkAuthToken &hasRight &getRightDefs &genResetHash &changePasswd &pushProfile &emailCheck);
+
+sub emailCheck( $$ ) {
+       my( $email, $tables ) = @_;
+       my $newmail;
+       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
+       return 'Email too long' if length $newmail > 255;
+       return 'An account for this email address already exists' if( ( defined $tables ) && $tables->hasEmail( $newmail ) );
+       return ( undef, $newmail );
+}
 
 sub saltedPasswd( $$ ) {
        my( $email, $passwd ) = @_;
@@ -55,25 +82,23 @@ sub changePasswd( $$$$ ) {
        $tables->changePasswd( $id, $salted );
 }
 
-sub genAuthToken( $$$$ ) {
-       my( $tables, $id, $req, $rights ) = @_;
+sub genAuthToken( $$$$$ ) {
+       my( $tables, $id, $req, $rights, $name ) = @_;
        unless( defined $rights ) {#Just logged in
-               my $from = $req->connection()->remote_ip();
+               my $from = $req->useragent_ip();
                $tables->setLastLog( $id, $from );
                $rights = $tables->rights( $id );
        }
        my $haveRights = scalar @{$rights};
        my $time = time;
-       my $ip = $req->connection()->remote_ip();
-       return "$id:$haveRights:$time:".md5_hex( "$id:$time:$ip:".$config{'authsalt'} );
+       return "$id:$haveRights:$time:".md5_hex( "$id:$time:".$config{'authsalt'} ).":$name";
 }
 
 sub checkAuthToken( $$$ ) {
        my( $tables, $req, $token ) = @_;
-       my( $id, $haveRights, $time, $hex ) = defined( $token ) ? split( /:/, $token ) : ();
-       return ( 0, 0, 0, [], "Not logged in" ) unless( defined $hex );
-       my $ip = $req->connection()->remote_ip();
-       my $expected = md5_hex( "$id:$time:$ip:".$config{'authsalt'} );
+       my( $id, $haveRights, $time, $hex, $name ) = defined( $token ) ? split( /:/, $token ) : ();
+       return ( 0, 0, 0, [], "Not logged in", undef ) unless( defined $hex );
+       my $expected = md5_hex( "$id:$time:".$config{'authsalt'} );
        my $actTime = time;
        my $tokOk = ( $expected eq $hex );
        my $authed = ( $tokOk && ( $time + $config{'authtime'} > $actTime ) );
@@ -87,7 +112,7 @@ sub checkAuthToken( $$$ ) {
                        push @{$rights}, \%r;
                }
        }
-       return ( $authed, $id, $regen, $rights, $authed ? undef : ( $tokOk ? "Login timed out" : "Not logged in x" ) );
+       return ( $authed, $id, $regen, $rights, $authed ? undef : ( $tokOk ? "Login timed out" : "Not logged in" ), $name );
 }
 
 sub hasRight( $$ ) {
@@ -118,7 +143,7 @@ sub pushProfile( $$$$ ) {
 checkConf( [ 'passwdsalt', 'regmailsalt', 'authsalt' ] );
 defConf( { 'authtime' => 2100, 'regenauthtime' => 300 } );
 
-open PRIVS, $directory."/rights" or die "Could not open privilege definitions\n";
+open PRIVS, $directory."cf/rights" or die "Could not open privilege definitions\n";
 foreach( <PRIVS> ) {
        my( $num, $name ) = /^(\d+)\s+(.*)$/ or die "Invalid syntax in privileges\n";
        $privnames{$num} = $name;