|
|
@@ -1,3 +1,39 @@
|
|
|
<?php
|
|
|
|
|
|
-namespace SshModel;
|
|
|
+namespace SshModel;
|
|
|
+
|
|
|
+function get_ssh_keys($id) {
|
|
|
+ $ini = parse_ini_file('../includes/config.ini');
|
|
|
+ try {
|
|
|
+ $ldapconn = ldap_connect($ini['hostname'], $ini['port']);
|
|
|
+ ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
|
+ ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ die ('ERROR: ' . $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ $filter = "(&(uid=" . $id . ")(objectClass=ldapPublicKey))";
|
|
|
+ $attributes = array("ldapPublicKey");
|
|
|
+
|
|
|
+ if ($ldapconn) {
|
|
|
+ $ldap_bind = ldap_bind($ldapconn, $ini['binddn'], $ini["bindpw"]);
|
|
|
+ if ($ldap_bind) {
|
|
|
+ $res = ldap_search($ldapconn, $ini['basedn'], $filter, $attributes);
|
|
|
+ $info = ldap_get_entries($ldapconn, $res);
|
|
|
+ if ($info["count"] > 0) {
|
|
|
+ $keys = array();
|
|
|
+ // if user has ssh keys
|
|
|
+ if (isset($info[0]["sshpublickey"])) {
|
|
|
+ for ($i = 0; $i < $info[0]["sshpublickey"]["count"]; $i++) {
|
|
|
+ $key = $info[0]["sshpublickey"][$i];
|
|
|
+ $key_name = array();
|
|
|
+ preg_match("/\S+@\S+/", $key, $key_name);
|
|
|
+ $keys[] = [$key_name[0] => $key];
|
|
|
+ }
|
|
|
+ return $keys;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|