Ver Fonte

forgot files

clement há 2 anos atrás
pai
commit
2c5a8a1b55

+ 4 - 1
src/controllers/ssh.php

@@ -9,11 +9,14 @@ function form_ssh() {
         exit();
     }
 
-    if (!$_SESSION['SSH_KEYS']) {
+    if (!$_SESSION['IS_SSH']) {
         header ('Location: home');
         exit();
     }
 
+    $ssh_keys = SshModel\get_ssh_keys($_SESSION['user']);
+    $_SESSION["SSH_KEYS"] = $ssh_keys;
+
     $template = get_view("sshhomepage");
 
     echo $template->render();

+ 1 - 1
src/controllers/user.php

@@ -117,7 +117,7 @@ function auth() {
     }
 
     $_SESSION['user'] = $id;
-    $_SESSION['SSH_KEYS'] = $auth_res[1]['SSH_KEYS'];
+    $_SESSION['IS_SSH'] = $auth_res[1]['IS_SSH'];
     header('Location: home');
     exit();
 }

+ 37 - 1
src/models/ssh.php

@@ -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;
+}

+ 3 - 3
src/models/user.php

@@ -29,12 +29,12 @@ function get_user_infos($id) {
     $info = ldap_get_entries($ldapconn, $res);
 
     if ($info["count"] > 0) {
-        $ssh_keys = true;
+        $is_ssh = true;
     } else {
-        $ssh_keys = false;
+        $is_ssh = false;
     }
 
-    return array("SSH_KEYS" => $ssh_keys);
+    return array("IS_SSH" => $is_ssh);
 }
 
 function auth($id, $passwd) {

+ 1 - 1
templates/home.html.twig

@@ -8,7 +8,7 @@
         <ul>
             <li><a href="changepassword">Change password</a></li>
             <li><a href="signout">Sign out</a></li>
-            {% if session.SSH_KEYS %}
+            {% if session.IS_SSH %}
             <li><a href="ssh">Manage SSH Keys</a></li>
             {% endif %}
         </ul>

+ 3 - 1
templates/sshhomepage.html.twig

@@ -1 +1,3 @@
-It works!
+{% for key_name in session.SSH_KEYS.0|keys %}
+    {{ key_name }} : {{ session.SSH_KEYS.0[key_name] }}
+{% endfor %}