forked from nilsteampassnet/TeamPass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh.php
268 lines (246 loc) · 10.2 KB
/
ssh.php
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
*
* @package ssh.php
* @author Nils Laumaillé <[email protected]>
* @version 2.1.27
* @copyright 2009-2019 Nils Laumaillé
* @license GNU GPL-3.0
* @link https://www.teampass.net
*
* This library 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.
*/
require_once('./sources/SecureHandler.php');
session_start();
if (!isset($_SESSION['CPM']) || $_SESSION['CPM'] != 1 ||
!isset($_SESSION['user_id']) || empty($_SESSION['user_id']) ||
!isset($_SESSION['key']) || empty($_SESSION['key']) || !isset($_GET['id'])
|| empty($_GET['key']) || $_GET['key'] != $_SESSION['key']
) {
die('Hacking attempt...');
}
// Load config
if (file_exists('../includes/config/tp.config.php')) {
include_once '../includes/config/tp.config.php';
} elseif (file_exists('./includes/config/tp.config.php')) {
include_once './includes/config/tp.config.php';
} else {
throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
}
$SETTINGS['enable_server_password_change'] = 1;
/* do checks */
require_once $SETTINGS['cpassman_dir'].'/includes/config/include.php';
require_once $SETTINGS['cpassman_dir'].'/sources/checks.php';
if (!checkUser($_SESSION['user_id'], $_SESSION['key'], "home") || !isset($SETTINGS['enable_server_password_change']) || $SETTINGS['enable_server_password_change'] != 1) {
$_SESSION['error']['code'] = ERR_NOT_ALLOWED; //not allowed page
include $SETTINGS['cpassman_dir'].'/error.php';
exit();
}
include $SETTINGS['cpassman_dir'].'/includes/language/'.$_SESSION['user_language'].'.php';
include $SETTINGS['cpassman_dir'].'/includes/config/settings.php';
require_once $SETTINGS['cpassman_dir'].'/sources/main.functions.php';
header("Content-type: text/html; charset=utf-8");
header("Cache-Control: no-cache, no-store, must-revalidate");
// connect to DB
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php';
$pass = defuse_return_decrypted($pass);
DB::$host = $server;
DB::$user = $user;
DB::$password = $pass;
DB::$dbName = $database;
DB::$port = $port;
DB::$encoding = $encoding;
DB::$error_handler = true;
$link = mysqli_connect($server, $user, $pass, $database, $port);
$link->set_charset($encoding);
// check user's token
$dataUser = DB::queryfirstrow(
"SELECT key_tempo
FROM ".prefix_table("users")."
WHERE id=%i",
$_SESSION['user_id']
);
if ($dataUser['key_tempo'] !== $_GET['key']) {
$_SESSION['error']['code'] = ERR_NOT_ALLOWED; //not allowed page
include $SETTINGS['cpassman_dir'].'/error.php';
exit();
}
// get data about item
$dataItem = DB::queryfirstrow(
"SELECT label, login, pw, pw_iv, url, auto_update_pwd_frequency
FROM ".prefix_table("items")."
WHERE id=%i",
$_GET['id']
);
// decrypt password
$oldPwClear = cryption(
$dataItem['pw'],
"",
"decrypt"
);
echo '
<div id="tabs">
<ul>
<li><a href="#tabs-1">'.$LANG['ssh_one_shot_change'].'</a></li>
<li><a href="#tabs-2">'.$LANG['ssh_scheduled_change'].'</a></li>
</ul>
<div id="tabs-1">
<div>
<label for="ausp_ssh_root">'.$LANG['ssh_user'].':</label>
<input type="text" id="ausp_ssh_root" class="menu_250 text ui-widget-content ui-corner-all" style="padding:3px;" value="'.$dataItem['login'].'" />
</div>
<div>
<label for="ausp_ssh_pwd">'.$LANG['ssh_pwd'].':</label>
<input type="password" id="ausp_ssh_pwd" class="menu_250 text ui-widget-content ui-corner-all" style="padding:3px;" value="'.$oldPwClear['string'].'" />
</div>
<div>
<label for="ausp_pwd">'.$LANG['index_new_pw'].':</label>
<input type="text" id="ausp_pwd" class="menu_250 text ui-widget-content ui-corner-all" style="padding:3px;" />
<i id="ausp_but_generate" class="fa fa-refresh fa-border fa-sm tip" style="cursor:pointer;padding:3px;" title="'.htmlentities(strip_tags($LANG['click_to_generate']), ENT_QUOTES).'"></i>
<i id="ausp_pwd_loader" style="display:none;margin-left:5px;" class="fa fa-cog fa-spin"></i>
</div>
<hr>
<div id="dialog_auto_update_server_pwd_status" style="margin:15px 0 15px 0;">'.$LANG['auto_update_server_password_info'].'</div>
<div id="dialog_auto_update_server_pwd_info" style="text-align:center;padding:5px;display:none;margin-top:10px;" class="ui-state-error ui-corner-all"></div>
<hr>
<a href="#" id="but_one_shot" class="button" onclick="start_one_shot_change()">'.$LANG['admin_action_db_backup_start_tip'].'</a>
</div>
<div id="tabs-2">
<div style="margin-bottom:10px;">'.$LANG['ssh_password_frequency_change_info'].'</div>
<label for="ausp_cron_freq">'.$LANG['ssh_password_frequency_change'].':</label>
<select id="ssh_freq">
<option value="0">0</option>
<option value="1"', isset($dataItem['auto_update_pwd_frequency']) && $dataItem['auto_update_pwd_frequency'] == 1 ? "selected" : "", '>1</option>
<option value="2"', isset($dataItem['auto_update_pwd_frequency']) && $dataItem['auto_update_pwd_frequency'] == 2 ? "selected" : "", '>2</option>
<option value="3"', isset($dataItem['auto_update_pwd_frequency']) && $dataItem['auto_update_pwd_frequency'] == 3 ? "selected" : "", '>3</option>
<option value="4"', isset($dataItem['auto_update_pwd_frequency']) && $dataItem['auto_update_pwd_frequency'] == 4 ? "selected" : "", '>4</option>
</select>
<div id="cronned_task_error" style="text-align:center;padding:5px;display:none;margin-top:10px;" class="ui-corner-all"></div>
<hr>
<a href="#" id="but_cronned_task" class="button" onclick="save_cronned_task()">'.$LANG['save_button'].'</a>
</div>
</div>
';
?>
<script type="text/javascript">
function save_cronned_task()
{
$("#cronned_task_error").hide();
$.post(
"sources/utils.queries.php",
{
type : "server_auto_update_password_frequency",
id : $('#selected_items').val(),
freq : $('#ssh_freq').val(),
key : "<?php echo $_SESSION['key']; ?>"
},
function(data) {
if (data[0].error != "") {
$("#cronned_task_error")
.html("Error: "+data[0].error)
.show()
.removeClass( "ui-state-focus" )
.addClass( "ui-state-error" );
} else {
$("#cronned_task_error")
.html("<?php echo $LANG['alert_message_done']; ?>")
.show()
.removeClass( "ui-state-error" )
.addClass( "ui-state-focus" );
}
},
"json"
);
}
function start_one_shot_change()
{
// check if new password is set
if($("#ausp_pwd").val() == "") {
$("#dialog_auto_update_server_pwd_info").html('<i class="fa fa-warning"></i> <?php echo $LANG['error_new_pwd_missing']; ?>').show();
return false;
}
// check if new password is set
if($("#ausp_ssh_root").val() == "" || $("#ausp_ssh_pwd").val() == "") {
$("#dialog_auto_update_server_pwd_info").html('<i class="fa fa-warning"></i> <?php echo $LANG['error_ssh_credentials_missing']; ?>').show();
return false;
}
// show progress
$("#dialog_auto_update_server_pwd_status").html('<i class="fa fa-cog fa-spin"></i> <?php echo $LANG['please_wait']; ?> ... ').attr("class","").show();
$("#dialog_auto_update_server_pwd_info").html("").hide();
//prepare data
var data = '{"currentId":"'+$('#selected_items').val() + '", '+
'"new_pwd":"'+$('#ausp_pwd').val()+'", '+
'"ssh_root":"'+$('#ausp_ssh_root').val()+'", '+
'"ssh_pwd":"'+$('#ausp_ssh_pwd').val()+'", '+
'"user_id":"<?php echo $_SESSION['user_id']; ?>"}';
$.post(
"sources/utils.queries.php",
{
type : "server_auto_update_password",
data : prepareExchangedData(data, "encode", "<?php echo $_SESSION['key']; ?>"),
key : "<?php echo $_SESSION['key']; ?>"
},
function(data) {
data = prepareExchangedData(data , "decode", "<?php echo $_SESSION['key']; ?>");
//check if format error
if (data.error != "") {
$("#dialog_auto_update_server_pwd_info").html("Error: "+data.error).show();
$("#dialog_auto_update_server_pwd_status").html("<?php echo $LANG['auto_update_server_password_info']; ?>");
} else {
// tbc
$("#dialog_auto_update_server_pwd_status").html("done "+data.text);
// change password in item form
$('#edit_pw1').val($('#ausp_pwd').val());
$("#hid_pw").val($('#ausp_pwd').val());
// change quick password
new ClipboardJS("#menu_button_copy_pw, #button_quick_pw_copy", {
text: function() {
return unsanitizeString($('#edit_pw1').val());
}
});
$("#button_quick_pw_copy").show();
}
}
);
}
function generate_pw()
{
$("#ausp_pwd_loader").show();
$.post(
"sources/main.queries.php",
{
type : "generate_a_password",
size : 12,
secure : false,
symbols : true,
capitalize : true,
numerals : true
},
function(data) {
data = prepareExchangedData(data, "decode", "<?php echo $_SESSION['key']; ?>");
if (data.error == "true") {
$("#dialog_auto_update_server_pwd_info").html(data.error_msg).show();
} else {
$("#ausp_pwd").val(data.key);
}
$("#ausp_pwd_loader").hide();
}
);
}
$(function() {
$("#tabs").tabs();
$(".button")
.button()
.click(function(event) {
event.preventDefault();
});
// generate new pw at opening
generate_pw();
// button to generate
$("#ausp_but_generate").click(function() {
generate_pw();
});
});
</script>