-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.pl
150 lines (124 loc) · 4.7 KB
/
index.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# FILE DISCONTINUED HERE
# UPDATED VERSION AT
# https://gitlab.com/yeupou/calaboose.transit/raw/master/index.pl
#
# | |
# \_V_//
# \/=|=\/
# [=v=]
# __\___/_____
# /..[ _____ ]
# /_ [ [ M /] ]
# /../.[ [ M /@] ]
# <-->[_[ [M /@/] ]
# /../ [.[ [ /@/ ] ]
# _________________]\ /__/ [_[ [/@/ C] ]
# <_________________>>0---] [=\ \@/ C / /
# ___ ___ ]/000o /__\ \ C / /
# \ / /....\ \_/ /
# ....\||/.... [___/=\___/
# . . . . [...] [...]
# . .. . [___/ \___]
# . 0 .. 0 . <---> <--->
# /\/\. . . ./\/\ [..] [..]
#
#!/usr/bin/perl
#
# (c) 2012 Mathieu Roy <yeupou--gnu.org>
# http://yeupou.wordpress.com
#
# 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
# the 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
use strict;
use CGI qw(:standard Link);
print header(-charset => 'UTF-8');
print start_html(-lang => 'fr-FR',
-title => "Accès temporaire");
my $debug = 0;
my $log = "/var/log/nginx/transit.log";
my $passwd = "/etc/nginx/passwd/transit";
my $user = param('user');
if ($user) {
# a user was provided
print h3("Demande d'authentification :");
print "+DBG user set to $user " if $debug;
# only take into account the request if it relates to a valid user...
if (scalar(getpwnam($user)) ne "") {
print "+DBG $user is valid " if $debug;
# and is the user belongs to the group transit
use User::grent;
my $group = getgrnam("transit");
for (@{$group->members}) {
if ($_ eq $user) {
print "+DBG $user belongs to transit " if $debug;
# then set up a password if there's no entry for the current
# user
my $passwd_contains_user = 0;
open(PASSWD, "< $passwd");
while(<PASSWD>){
next unless /^$user:/;
$passwd_contains_user = 1;
last;
}
close(PASSWD);
unless ($passwd_contains_user) {
print "+DBG $user is missing from $passwd " if $debug;
# If we get here, we create a new password and send it by
# mail to the user.
# (assume there is at least a valid alias for the user)
my @chars = (0 .. 9, 'a' .. 'z', 'A' .. 'Z');
my $random = join "", @chars[ map rand @chars, 0 .. 6 ];
my $remote_ip = $ENV{'REMOTE_ADDR'};
# this must be logged
use POSIX qw(strftime);
open(LOG, ">> $log");
print LOG strftime "$remote_ip [%c] access requested and approved, ".$ENV{'HTTP_USER_AGENT'}."\n", localtime;
close(LOG);
# update/create a passwd file
open(PASSWD, ">> $passwd");
print PASSWD "$user:".crypt($random, $user)."\n";
close(PASSWD);
print "+DBG $passwd updated/created ($user:$random) " if $debug;
# build a mail and send it
use Socket;
my $remote;
$remote = gethostbyaddr(inet_aton($remote_ip), AF_INET)
or $remote = $remote_ip;
use Mail::Send;
my $msg = new Mail::Send;
$msg->to($user);
$msg->subject("Accès temporaire");
$msg->add("User-Agent", "calaboose.transit");
my $fh = $msg->open;
print $fh "Bonjour,\n\Sollicité depuis la machine $remote, un nouveau mot de passe temporaire a été crée :\n\n\t\t".$random."\n\n";
$fh->close;
print p("Un message vous a été envoyé à l'instant.");
print "+DBG mail sent " if $debug;
}
}
}
}
# in any case
# do not provide any relevant error message, it's not supposed to give out
# any details of the current system
} else {
# otherwise print a form to allow user to register
print h3("Inconnu...");
print p("Indiquer ici le nom d'utilisateur habituel :");
print start_form(-method=>"POST",-action=>script_name()).textfield(-name=>'user').submit().end_form();
}
print end_html();
# EOF