/
deploy_keyring.php
126 lines (104 loc) · 4.72 KB
/
deploy_keyring.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
<?php
require_once('inc/global.inc.php');
include('Mail.php');
include('Mail/mime.php');
$smarty->assign('title','nSKM - Keyring Deployment Process');
if (isset($_POST['id_keyring'])) $id_keyring = $_POST["id_keyring"];
elseif (isset($_GET['id_keyring'])) $id_keyring=$_GET['id_keyring'];
else $id_keyring = "";
if (isset($_POST['step'])) {$step = $_POST["step"];} else {$step = "";}
if (isset($_GET['step'])) {$step2 = $_GET["step"];} else {$step2 = "";}
if (isset($_GET['clean'])) {$clean = $_GET['clean'];} else {$clean = "";}
if (isset($_GET['create_user'])) $create_user=$_GET['create_user']; else $create_user='';
$output1='';
$output2='';
$id_run=rand();
$mysql_link=$GLOBALS['mysql_link'];
if (empty($id_keyring))
{
// If no keyring specify, display keyrings' list
$keyrings = get_all_keyrings();
$smarty->assign("keyrings",$keyrings);
$smarty->display('deploy_keyring.tpl');
}
else
{
if ($step2==2)
{
$keyring_name = get_keyring_name($id_keyring);
$result = mysqli_query($mysql_link, "SELECT * FROM `hak` where `id_keyring` = '$id_keyring' ORDER BY `id_host`" )
or die (mysqli_error()."<br>Couldn't execute query: $query");
$nr = $result->num_rows;
$output='';
while( $row = mysqli_fetch_array( $result ))
{
// Affecting values
$id_host = $row['id_host'];
$id_account = $row['id_account'];
$hostname = get_host_name($id_host);
$ssh_port = get_host_ssh_port($id_host);
$account_name = get_account_name($id_account);
$hosts[$id_host]['accounts'][$id_account]['name'] = $account_name;
$hosts[$id_host]['name'] = $hostname;
$hosts[$id_host]['ssh_port'] = $ssh_port;
$smarty->assign('keyring_name',$keyring_name);
list($res_conn,$mess_conn)= test_connection($hostname,$clean,$ssh_port);
// If connection works go on
if ($res_conn)
{
// Clean Known-hosts if needed
if ($clean==1)
{
$output_clean= ssh_clean_known_hosts_file($hostname,get_host_ip($id_host));
$hosts[$id_host]['accounts'][$id_account]['result_clean']=$output_clean;
}
$output1=prepare_authorizedkey_file($id_host,$id_account,$id_run);
$hosts[$id_host]['accounts'][$id_account]['result1']=$mess_conn.$output1;
$output2=deploy_authorizedkey_file($id_host,$id_account,$id_run,$create_user);
$hosts[$id_host]['accounts'][$id_account]['result2']=$output2;
} else {
$hosts[$id_host]['accounts'][$id_account]['result1']=$mess_conn.$output1;
$hosts[$id_host]['accounts'][$id_account]['result2']=$output2;
}
}
//We delete the private key file
$priv_key=$home_of_webserver_account."/.ssh/id_rsa";
if (file_exists($priv_key))
unlink($priv_key);
$smarty->assign('hosts',$hosts);
$smarty->display('deploy_keyring_done.tpl');
// Sending mail
if ($SKM_SEND_MAIL)
{
$message='';
$emailuser = $email_to;
foreach ($hosts AS $host) {
$message.= "<h4>Deploying Keyring <b><i>$keyring_name</i></b> on ".$host['name']."</h4>";
foreach ($host['accounts'] AS $account) {
if ($clean==1) {$message.= $output_clean;}
$message.= $account['result1'];
$message.= $account['result2'];
$message.= "<br>";
}
}
$mime = new Mail_mime();
$mime->setHTMLBody($message);
// Ajout des images
preg_match_all('@(.*)images/(.*).gif(.*)@i',$message,$matches);
$img=array_unique($matches[2]);
foreach ($img as $key=>$value) {
$mime->addHTMLImage("images/$value.gif","image/gif");
}
$hdrs = array(
'From' => "SKM <".$email_from.">",
'Subject' => "SKM: Deploying Keyring $keyring_name"
);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail',"-f $email_from");
$mail->send($emailuser, $hdrs, $body);
}
}
else header("Location:decrypt_key.php?action=deploy_keyring&id_keyring=$id_keyring");
}
?>