-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathchangesshkey.php
More file actions
185 lines (160 loc) · 6.42 KB
/
Copy pathchangesshkey.php
File metadata and controls
185 lines (160 loc) · 6.42 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
#==============================================================================
# LTB Self Service Password
#
# Copyright (C) 2009 Clement OUDOT
# Copyright (C) 2009 LTB-project.org
#
# 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.
#
# GPL License: http://www.gnu.org/licenses/gpl.txt
#
#==============================================================================
# This page is called to change sshPublicKey
#==============================================================================
# POST parameters
#==============================================================================
# Initiate vars
$result = "";
$login = "";
$password = "";
$sshkey = "";
$ldap = "";
$userdn = "";
$mail = "";
if (isset($_POST["password"]) and $_POST["password"]) { $password = $_POST["password"]; }
else { $result = "passwordrequired"; }
if (isset($_POST["sshkey"]) and $_POST["sshkey"]) { $sshkey = $_POST["sshkey"]; }
else { $result = "sshkeyrequired"; }
if (isset($_REQUEST["login"]) and $_REQUEST["login"]) { $login = $_REQUEST["login"]; }
else { $result = "loginrequired"; }
if (! isset($_REQUEST["login"]) and ! isset($_POST["password"]) and ! isset($_POST["sshkey"]))
{ $result = "emptysshkeychangeform"; }
# Check the entered username for characters that our installation doesn't support
if ( $result === "" ) {
$result = check_username_validity($login,$login_forbidden_chars);
}
#==============================================================================
# Check reCAPTCHA
#==============================================================================
if ( $result === "" && $use_recaptcha ) {
$result = check_recaptcha($recaptcha_privatekey, $recaptcha_request_method, $_POST['g-recaptcha-response'], $login);
}
#==============================================================================
# Check password
#==============================================================================
if ( $result === "" ) {
# Connect to LDAP
$ldap = ldap_connect($ldap_url);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if ( $ldap_starttls && !ldap_start_tls($ldap) ) {
$result = "ldaperror";
error_log("LDAP - Unable to use StartTLS");
} else {
# Bind
if ( isset($ldap_binddn) && isset($ldap_bindpw) ) {
$bind = ldap_bind($ldap, $ldap_binddn, $ldap_bindpw);
} else {
$bind = ldap_bind($ldap);
}
$errno = ldap_errno($ldap);
if ( $errno ) {
$result = "ldaperror";
error_log("LDAP - Bind error $errno (".ldap_error($ldap).")");
} else {
# Search for user
$ldap_filter = str_replace("{login}", $login, $ldap_filter);
$search = ldap_search($ldap, $ldap_base, $ldap_filter);
$errno = ldap_errno($ldap);
if ( $errno ) {
$result = "ldaperror";
error_log("LDAP - Search error $errno (".ldap_error($ldap).")");
} else {
# Get user DN
$entry = ldap_first_entry($ldap, $search);
$userdn = ldap_get_dn($ldap, $entry);
if( !$userdn ) {
$result = "badcredentials";
error_log("LDAP - User $login not found");
} else {
# Get user email for notification
if ( $notify_on_sshkey_change ) {
$mailValues = ldap_get_values($ldap, $entry, $mail_attribute);
if ( $mailValues["count"] > 0 ) {
$mail = $mailValues[0];
}
}
# Bind with old password
$bind = ldap_bind($ldap, $userdn, $password);
$errno = ldap_errno($ldap);
if ( ($errno == 49) && $ad_mode ) {
if ( ldap_get_option($ldap, 0x0032, $extended_error) ) {
error_log("LDAP - Bind user extended_error $extended_error (".ldap_error($ldap).")");
$extended_error = explode(', ', $extended_error);
if ( strpos($extended_error[2], '773') or strpos($extended_error[0], 'NT_STATUS_PASSWORD_MUST_CHANGE') ) {
error_log("LDAP - Bind user password needs to be changed");
$errno = 0;
}
if ( ( strpos($extended_error[2], '532') or strpos($extended_error[0], 'NT_STATUS_ACCOUNT_EXPIRED') ) and $ad_options['change_expired_password'] ) {
error_log("LDAP - Bind user password is expired");
$errno = 0;
}
unset($extended_error);
}
}
if ( $errno ) {
$result = "badcredentials";
error_log("LDAP - Bind user error $errno (".ldap_error($ldap).")");
} else {
# Rebind as Manager if needed
if ( $who_change_sshkey == "manager" ) {
$bind = ldap_bind($ldap, $ldap_binddn, $ldap_bindpw);
}
}}}}}
}
#==============================================================================
# Change sshPublicKey
#==============================================================================
if ( $result === "" ) {
$result = change_sshkey($ldap, $userdn, $change_sshkey_attribute, $sshkey);
}
if ( $result === "sshkeychanged") {
# Notify password change
if ($mail and $notify_on_sshkey_change) {
$data = array( "login" => $login, "mail" => $mail, "sshkey" => $sshkey);
if ( !send_mail($mailer, $mail, $mail_from, $mail_from_name, $messages["changesshkeysubject"], $messages["changesshkeymessage"].$mail_signature, $data) ) {
error_log("Error while sending change email to $mail (user $login)");
}
}
}
# Render associated template
echo $twig->render('changesshkey.twig', array(
'result' => $result,
'show_help' => $show_help,
'login' => $login,
'recaptcha_publickey' => $recaptcha_publickey,
'recaptcha_theme' => $recaptcha_theme,
'recaptcha_type' => $recaptcha_type,
'recaptcha_size' => $recaptcha_size,
'lang' => $lang,
'background_image' => $background_image,
'show_menu' => $show_menu,
'logo' => $logo,
'dependency_check_results' => $dependency_check_results,
'use_questions' => $use_questions,
'use_tokens' => $use_tokens,
'use_sms' => $use_sms,
'change_sshkey' => $change_sshkey,
'use_totp' => $use_totp,
'action' => $action,
'source' => $source,
));