Project

General

Profile

Download (5.9 KB) Statistics
| Branch: | Revision:
1 95b003ff Origo
#
2
# Config settings for mod_auth_tkt CGI scripts
3
# 
4
# Customise as required
5
#
6
7
package AuthTktConfig;
8
9
#use strict;
10
use Tie::DBI;
11
use Digest::MD5 qw(md5 md5_hex md5_base64);
12
use Digest::SHA qw(sha512_base64);
13
use Sys::Syslog qw( :DEFAULT setlogsock);
14
use Net::Subnet;
15
use ConfigReader::Simple;
16 54401133 hq
use Auth::GoogleAuth;
17 4aef7ef6 hq
use Geo::IP;
18 95b003ff Origo
19
# CSS stylesheet to use (optional)
20
our $STYLESHEET = 'tkt.css';
21
22
# Page title (optional)
23
our $TITLE = `cat /etc/stabile/config.cfg | sed -n -e 's/^ENGINENAME=//p'`;
24
chomp $TITLE;
25
$TITLE = $TITLE || 'Stabile';
26
$TITLE = "$TITLE - login";
27
28
# Fixed back location, overriding any set via back cookie or back arg
29
our $FIXED_BACK_LOCATION = '';
30
31
# Default back location, if none set via back cookie or back arg
32
our $DEFAULT_BACK_LOCATION = '/stabile/auth/login';
33
34
# Boolean flag, whether to fallback to HTTP_REFERER for back location
35
our $BACK_REFERER = 1;
36
37
# For autologin, mode to fallback to if autologin fails ('login' or 'guest')
38
our $AUTOLOGIN_FALLBACK_MODE = 'login';
39
40
# Additional cookies to clear on logout e.g. PHPSESSID
41
our @NUKE_COOKIES = qw(steamaccount tktuser);
42
43
# Debug flag
44
our $DEBUG = 0;
45
46
our $COOKIE_BASE = '';
47
$COOKIE_BASE = `cat /etc/stabile/cookiebase` if -e "/etc/stabile/cookiebase";
48
chomp $COOKIE_BASE;
49
$COOKIE_BASE = ".$COOKIE_BASE" unless ($COOKIE_BASE =~ /^\./);
50
51
52
# Username/password validation for login mode
53
#   (modify or point $validate_sub somewhere appropriate).
54
# The validation routine should return a true value (e.g. 1) if the 
55
#   given username/password combination is valid, and a false value
56
#   (e.g. 0) otherwise.
57
# This version uses Apache::Htpasswd and a standard htpasswd file.
58
sub validate
59
{
60 54401133 hq
	my ($username, $password, $totp) = @_;
61 95b003ff Origo
	require Apache::Htpasswd;
62
	my $ht = Apache::Htpasswd->new({ passwdFile => '/etc/apache2/htpasswd-stabile', ReadOnly => 1 });
63
	return $ht->htCheckPassword($username, $password);
64
}
65
66
sub sqlvalidate {
67 54401133 hq
	my ($username, $password, $totp) = @_;
68 95b003ff Origo
	$username = lc $username;
69
	$username = $1 if ($username =~ /(.+)/); # Untaint
70
    my $config = ConfigReader::Simple->new("/etc/stabile/config.cfg",
71
        [qw(DBI_USER DBI_PASSWD)]);
72
    my $dbiuser =  $config->get('DBI_USER') || "irigo";
73
    my $dbipasswd = $config->get('DBI_PASSWD') || "";
74
75
	my %register;
76
	unless (tie %register,'Tie::DBI', {
77
		db=>'mysql:steamregister',
78
		table=>'users',
79
		key=>'username',
80
		autocommit=>0,
81
		CLOBBER=>1,
82
		user=>$dbiuser,
83
		password=>$dbipasswd}) {return 0};
84
85
	my $valid = 0;
86
	my $validip = 0;
87
	my $validuser = 0;
88
89 54401133 hq
	my $u = $register{$username};
90
	my $allowfrom = $u->{'allowfrom'};
91
	my $totpsecret = $u->{'totpsecret'};
92
93 95b003ff Origo
	my $from = $ENV{'REMOTE_ADDR'};
94
	if ($allowfrom) {
95
	    my @allows = split(/,\s*/, $allowfrom);
96
	    foreach my $ip (@allows) {
97
			if ($ip =~ /(\d+\.\d+\.\d+\.\d+\/\d+)/) { # Match a subnet definition
98
				$validip = 1 if (subnet_matcher($1)->($from));
99 04c16f26 hq
			} elsif ( $ip =~ /^\w\w$/ ) { # Match a country code
100 4aef7ef6 hq
				my $geoip = Geo::IP->new(GEOIP_MEMORY_CACHE);
101
				$country = $geoip->country_code_by_addr($from);
102
				$validip = 1 if ($country eq $ip);
103 95b003ff Origo
			} else {
104
				$ip = $1 if ($ip =~ /(\d+\.)0\.0\.0/);
105
				$ip = $1 if ($ip =~ /(\d+\.\d+\.)0\.0/);
106
				$ip = $1 if ($ip =~ /(\d+\.\d+\.\d+\.)0/);
107
				$validip = 1 if ($from =~ /^$ip/);
108
			}
109
	    }
110
	} else {
111
	    $validip = 1;
112
	}
113
	if ($register{$username}) {
114
	    my $privileges = $register{$username}->{'privileges'};
115
	    $validuser = 1 unless ($privileges =~ /d/);
116
	}
117
	if ($validip && $validuser) {
118
        # First check if md5 checksums match
119
        my $upassword = $register{$username}->{'password'};
120 54401133 hq
        if ($password && (
121
			$upassword eq md5_base64($password)
122
			|| $upassword eq sha512_base64($password)
123
			|| ($totp && ($upassword eq $password)) # password is hashed when sent from totp form
124
		)){
125
			if ($totpsecret && $totpsecret ne '--') { # User has 2fa enabled
126
				if ($totp) {
127
					my $auth = Auth::GoogleAuth->new();
128
					if ($auth->verify($totp, 1, $totpsecret)) {
129
						$valid = 1;
130
					} else {
131
						$valid = 2;
132
					}
133
				} else {
134
					$valid = 2;
135
				}
136
			} else {
137
				$valid = 1;
138
			}
139 95b003ff Origo
        # If plaintexts match, then assume we are dealing with a new user and convert the password to it's md5 checksum
140
        } elsif ($password && $register{$username}->{'password'} eq $password) {
141
            $valid = 1;
142
            eval {syslogit('info', "Adding system user $username: " . `/usr/sbin/useradd -m "irigo-$username"`); 1;};
143 104449f5 Origo
			`echo "[User]\nSystemAccount=true" > /var/lib/AccountsService/users/irigo-$username`; # Don't show in login screen
144 95b003ff Origo
    #		eval {`/bin/echo irigo-$username:$password | /usr/sbin/chpasswd`; 1;}; # Doesn't work on Lucid, so we go through the hoops below...
145
            my $np = `/bin/echo irigo-$username:$password | /usr/sbin/chpasswd -S`; # -S option not supported on older versions
146
            $np =~ /irigo-$username:(.+)/;
147
            my $cpass = $1;
148
            eval {`/usr/sbin/usermod -p '$cpass' irigo-$username`; 1;};
149
            $register{$username}->{'password'} = sha512_base64($password);
150
    #        unless(-d "$basedir$username"){
151
    #            umask "0000";
152
    #            mkdir "$basedir$username" or syslogit("info", "Unable to create user dir for $username");
153
    #        }
154
        }
155
		$register{$username}->{'lastlogin'} = $register{$username}->{'curlogin'};
156
		$register{$username}->{'lastloginfrom'} = $register{$username}->{'curloginfrom'};
157
		$register{$username}->{'curlogin'} = time;
158
		$register{$username}->{'curloginfrom'} = $ENV{'REMOTE_ADDR'};
159
	}
160
	return $valid;
161
	untie %register;
162
}
163
164
165
#our $validate_sub = \&validate;
166
our $validate_sub = \&sqlvalidate;
167
168
# For guest mode (if used), setup guest username
169
#   Could use a counter or a random suffix etc.
170
sub guest_user { return 'guest' }
171
our $guest_sub = \&guest_user;
172
173
1;
174
175
sub syslogit {
176
	my ($priority, $msg) = @_;
177
178
	setlogsock('unix');
179
	# $programname is assumed to be a global.  Also log the PID
180
	# and to CONSole if there's a problem.  Use facility 'user'.
181
	openlog("", 'pid,cons', 'user');
182
	syslog($priority, $msg);
183
	closelog();
184
	return 1;
185
}