forked from sciurus/gente
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gente.pl
executable file
·123 lines (103 loc) · 3.15 KB
/
gente.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
#!/usr/bin/env perl
use Mojolicious::Lite;
use Net::LDAP;
use Net::LDAP::Extension::SetPassword;
app->log->level('fatal'); # will set level again, below, after parsing config
my $config = plugin 'JSONConfig' => { file => 'gente.json' };
# if someone is using mod_rewrite to hide the script file name
# generate urls that reflect that
hook before_dispatch => sub {
my $self = shift;
my $base = $self->req->env->{SCRIPT_URI};
if ($base) {
$self->req->url->base( Mojo::URL->new($base) );
}
};
get '/' => sub {
my $self = shift;
$self->stash( title => $config->{title} );
$self->render('form');
};
post '/' => sub {
my $self = shift;
my $server = $config->{server};
my $timeout = $config->{timeout};
my $dn = $config->{dn};
my $cafile = $config->{cafile};
$self->stash( title => $config->{title} );
my $username = $self->param('username');
my $old = $self->param('old');
my $new = $self->param('new');
my $error;
my $result;
my $ldap = Net::LDAP->new( $server, timeout => $timeout );
if ( not $ldap ) {
$error = "Unable to connect to $server";
$self->app->log->error($error);
$result = 'An internal error occured';
$self->render( 'feedback', status => 500, result => $result );
return;
}
$self->app->log->debug('LDAP Connected');
my $mesg = $ldap->start_tls( verify => 'require', cafile => $cafile );
if ( $mesg->code ) {
$error = "Unable to start TLS to $server using $cafile";
$self->app->log->error($error);
$result = 'An internal error occured';
$self->render( 'feedback', status => 500, result => $result );
return;
}
$self->app->log->debug('TLS Enabled');
$mesg = $ldap->bind( "uid=$username,$dn", password => $old );
if ( $mesg->code ) {
$error = "Unable to bind as $username. Server says " . $mesg->error;
$self->app->log->info($error);
$result =
'Unable to change your password. Maybe your old password is not correct? Try again or get help.';
$self->render( 'feedback', result => $result );
return;
}
$self->app->log->debug('User Bound');
$mesg = $ldap->set_password( oldpasswd => $old, newpasswd => $new );
if ( $mesg->code ) {
$error =
"Unable to change password as $username. Server says " . $mesg->error;
$self->app->log->info($error);
$result = 'Unable to change your password. Try again or get help.';
}
else {
$self->app->log->debug('Password Changed');
$result = 'Your password was successfully changed';
}
$self->render( 'feedback', result => $result );
};
app->secrets($config->{secret});
app->log->level($config->{log_level});
app->start;
__DATA__
@@ form.html.ep
% layout 'default';
<%= form_for '/' => (method => 'post') => begin %>
Username:
<%= input_tag 'username' %>
<br>
Old Password:
<%= input_tag 'old', type => 'password' %>
<br>
New Password:
<%= input_tag 'new', type => 'password' %>
<br>
<%= submit_button %>
<% end %>
@@ feedback.html.ep
% layout 'default';
<p> <%= $result %> </p>
<p> <%= link_to 'Back to the form' => '/' %> </p>
@@ layouts/default.html.ep
<!doctype html><html>
<head><title><%= title %></title></head>
<body>
<h1> <%= title %> </h1>
<%= content %>
</body>
</html>