From a3ec4535cb5f699022683b8678a93b5ab1a076a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= <anarcat@koumbit.org>
Date: Thu, 29 Nov 2012 17:43:24 -0500
Subject: [PATCH] fix IP allocation code so at least node save works

---
 modules/hosting/server/hosting.ip.inc         | 14 +++++------
 .../web_server/ssl/hosting_ssl.nodeapi.inc    | 24 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/modules/hosting/server/hosting.ip.inc b/modules/hosting/server/hosting.ip.inc
index 271bcb8d..0d9ccc22 100644
--- a/modules/hosting/server/hosting.ip.inc
+++ b/modules/hosting/server/hosting.ip.inc
@@ -96,16 +96,16 @@ function hosting_ip_delete_revision($node) {
  */
 function hosting_ip_allocate($cert, $site) {
   // make sure the IP is not allocated while we pick ours
-  db_query("LOCK TABLES {hosting_ssl_cert_ips} WRITE");
-  $platform = node_load($node->platform);
+  db_query("LOCK TABLES {hosting_ssl_cert_ips} WRITE, {hosting_ip_addresses} WRITE");
+  $platform = node_load($site->platform);
   $server = node_load($platform->web_server);
   // guess the next available IP
-  $ip = db_result(db_query("SELECT server.ip_address FROM hosting_ip_addresses AS server
-                             LEFT JOIN hosting_ssl_cert_ips AS certs ON server.id = certs.ip_address
-                             WHERE certs.ip_address IS NULL AND nid = %d LIMIT 1;", $node->nid));
+  $ip = db_result(db_query("SELECT hosting_ip_addresses.id FROM {hosting_ip_addresses}
+                             LEFT JOIN {hosting_ssl_cert_ips} ON hosting_ip_addresses.id = hosting_ssl_cert_ips.ip_address
+                             WHERE hosting_ssl_cert_ips.ip_address IS NULL AND nid = %d LIMIT 1;", $server->nid));
   if ($ip) {
-    db_query("INSERT INTO {hosting_ssl_cert_ips} (cid, ip_address) VALUES (%d, %d)", $cert->nid, $ip);
+    db_query("INSERT INTO {hosting_ssl_cert_ips} (cid, ip_address) VALUES (%d, %d)", $cert->cid, $ip);
   }
-  db_query("UNLOCK TABLES {hosting_ssl_cert_ips}");
+  db_query("UNLOCK TABLES");
   return $ip;
 }
\ No newline at end of file
diff --git a/modules/hosting/web_server/ssl/hosting_ssl.nodeapi.inc b/modules/hosting/web_server/ssl/hosting_ssl.nodeapi.inc
index ba6efdfe..813c91e3 100644
--- a/modules/hosting/web_server/ssl/hosting_ssl.nodeapi.inc
+++ b/modules/hosting/web_server/ssl/hosting_ssl.nodeapi.inc
@@ -119,7 +119,7 @@ function hosting_ssl_get_key($cid) {
 }
 
 function hosting_ssl_get_ip($cid) {
-  return db_result(db_query("SELECT ips.ip_address FROM {hosting_ssl_cert_ips} cert INNER JOIN {hosting_ip_addresses} ips ON cert.cid = ips.cid WHERE cid=%d", $cid));
+  return db_result(db_query("SELECT ips.ip_address FROM {hosting_ssl_cert_ips} cert INNER JOIN {hosting_ip_addresses} ips ON cert.ip_address = ips.id WHERE cid=%d", $cid));
 }
 
 function hosting_ssl_output_key($cid) {
@@ -239,35 +239,39 @@ function hosting_ssl_save_key($node) {
     return 0;
   }
 
-  // we only save the new key if it's a string
-  if (!is_numeric($node->key)) {
-    $result = db_query("SELECT * FROM {hosting_ssl_cert} WHERE ssl_key = '%s'", $node->key);
+  $client = hosting_get_client($node->client);
+
+  $ssl_key = $node->ssl_key_new;
+  if ($ssl_key) {
+    $result = db_query("SELECT * FROM {hosting_ssl_cert} WHERE ssl_key = '%s'", $ssl_key);
     if ($obj = db_fetch_object($result)) {
       // update
       if ($node->client != null) {
-        $obj->client = $node->client;
+        $obj->client = $client->nid;
       }
       drupal_write_record("hosting_ssl_cert", $obj, 'cid');
+      $node->ssl_key = $obj->cid;
     }
     else {
       // insert
       $obj = new stdClass();
-      $obj->ssl_key = $node->key;
-      $obj->client = $node->client;
+      $obj->ssl_key = $ssl_key;
+      $obj->client = $client->nid;
       $obj->status = 0;
       drupal_write_record("hosting_ssl_cert", $obj);
-      if (!hosting_ip_allocate($cert, $node)) {
-        form_set_error('ssl_key_new', t("Unable to allocate IP address for certificate, disabling SSL"));
+      if (!hosting_ip_allocate($obj, $node)) {
+        form_set_error('ssl_key_new', t("Unable to allocate IP address for certificate, disabling SSL. Allocate more IP addresses to this server then try to enable SSL again."));
         db_query("DELETE FROM {hosting_ssl_cert} WHERE cid = %d", $obj->cid);
         $obj->cid = FALSE;
         $node->ssl_enabled = HOSTING_SSL_DISABLED;
       }
+      $node->ssl_key = $obj->cid;
     }
 
     return $obj->cid;
   }
 
-  return $node->key;
+  return $node->ssl_key;
 }
 
 /**
-- 
GitLab