Pārlūkot izejas kodu

using bind funcs in ssh model

clement 2 gadi atpakaļ
vecāks
revīzija
c14efb036f
1 mainītis faili ar 23 papildinājumiem un 54 dzēšanām
  1. 23 54
      src/models/ssh.php

+ 23 - 54
src/models/ssh.php

@@ -3,35 +3,26 @@
 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());
-    }
+    global $ini;
 
     $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;
+    $ldap_bind = bind_as_operator();
+    if ($ldap_bind[0]) {
+        $res = ldap_search($ldap_bind[1], $ini['basedn'], $filter, $attributes);
+        $info = ldap_get_entries($ldap_bind[1], $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;
             }
         }
     }
@@ -39,55 +30,33 @@ function get_ssh_keys($id) {
 }
 
 function add_ssh_key($id, $new_key) {
-    $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());
-    }
-
-    if (!$ldapconn) {
-        return false;
-    }
+    global $ini;
 
-    $ldap_bind = @ldap_bind($ldapconn, $ini['binddn'], $ini["bindpw"]);
+    $ldap_bind = bind_as_operator();
 
-    if (!$ldap_bind) {
+    if (!$ldap_bind[0]) {
         return false;
     }
 
     $dn = "uid=" . $id . "," . $ini['basedn'];
     $entry['sshPublicKey'] = $new_key;
-    $res = @ldap_mod_add($ldapconn, $dn, $entry);
+    $res = @ldap_mod_add($ldap_bind[1], $dn, $entry);
 
     return $res;
 }
 
 function del_ssh_key($id, $key) {
-    $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());
-    }
-
-    if (!$ldapconn) {
-        return false;
-    }
+    global $ini;
 
-    $ldap_bind = @ldap_bind($ldapconn, $ini['binddn'], $ini["bindpw"]);
+    $ldap_bind = bind_as_operator();
 
-    if (!$ldap_bind) {
+    if (!$ldap_bind[0]) {
         return false;
     }
 
     $dn = "uid=" . $id . "," . $ini['basedn'];
     $entry["sshPublicKey"] = $key;
-    $res = @ldap_mod_del($ldapconn, $dn, $entry);
+    $res = @ldap_mod_del($ldap_bind[1], $dn, $entry);
 
     return $res;
 }