>From 47dc3e32e6b47bd80620cfbc54e7590193dd0c1a Mon Sep 17 00:00:00 2001
From: Daniel Miller <bonsaiviking () gmail com>
Date: Tue, 31 Jul 2012 16:42:27 -0500
Subject: [PATCH 5/5] Make smbauth.lua use host, not nmap, registry

---
 nselib/smbauth.lua |   42 +++++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 25 deletions(-)

diff --git a/nselib/smbauth.lua b/nselib/smbauth.lua
index c40ec50..7003323 100644
--- a/nselib/smbauth.lua
+++ b/nselib/smbauth.lua
@@ -106,11 +106,11 @@ local ACCOUNT_TYPES = {
 }
 
 local function account_exists(host, username, domain)
-	if(nmap.registry[host.ip] == nil or nmap.registry[host.ip]['smbaccounts'] == nil) then
+	if(host.registry['smbaccounts'] == nil) then
 		return false
 	end
 
-	for i, j in pairs(nmap.registry[host.ip]['smbaccounts']) do
+	for i, j in pairs(host.registry['smbaccounts']) do
 		if(j['username'] == username and j['domain'] == domain) then
 			return true
 		end
@@ -121,13 +121,13 @@ end
 
 function next_account(host, num)
 	if(num == nil) then
-		if(nmap.registry[host.ip]['smbindex'] == nil) then
-			nmap.registry[host.ip]['smbindex'] = 1
+		if(host.registry['smbindex'] == nil) then
+			host.registry['smbindex'] = 1
 		else
-			nmap.registry[host.ip]['smbindex'] = nmap.registry[host.ip]['smbindex'] + 1
+			host.registry['smbindex'] = host.registry['smbindex'] + 1
 		end
 	else
-		nmap.registry[host.ip]['smbindex'] = num
+		host.registry['smbindex'] = num
 	end
 end
 
@@ -166,11 +166,8 @@ function add_account(host, username, domain, password, password_hash, hash_type,
 		return
 	end
 
-	if(nmap.registry[host.ip] == nil) then
-		nmap.registry[host.ip] = {}
-	end
-	if(nmap.registry[host.ip]['smbaccounts'] == nil) then
-		nmap.registry[host.ip]['smbaccounts'] = {}
+	if(host.registry['smbaccounts'] == nil) then
+		host.registry['smbaccounts'] = {}
 	end
 
 	-- Determine the type of account, if it wasn't given
@@ -205,10 +202,10 @@ function add_account(host, username, domain, password, password_hash, hash_type,
 	new_entry['account_type']  = account_type
 
 	-- Insert the new entry into the table
-	table.insert(nmap.registry[host.ip]['smbaccounts'], new_entry)
+	table.insert(host.registry['smbaccounts'], new_entry)
 
 	-- Sort the table based on the account type (we want anonymous at the end, administrator at the front)
-	table.sort(nmap.registry[host.ip]['smbaccounts'], function(a,b) return a['account_type'] > b['account_type'] end)
+	table.sort(host.registry['smbaccounts'], function(a,b) return a['account_type'] > b['account_type'] end)
 
 	-- Print a debug message
 	stdnse.print_debug(1, "SMB: Added account '%s' to account list", username)
@@ -216,7 +213,7 @@ function add_account(host, username, domain, password, password_hash, hash_type,
 	-- Reset the credentials
 	next_account(host, 1)
 
---	io.write("\n\n" .. nsedebug.tostr(nmap.registry[host.ip]['smbaccounts']) .. "\n\n")
+--	io.write("\n\n" .. nsedebug.tostr(host.registry['smbaccounts']) .. "\n\n")
 end
 
 ---Retrieve the current set of credentials set in the registry. If these fail, <code>next_credentials</code> should be
@@ -226,12 +223,12 @@ end
 --@return (result, username, domain, password, password_hash, hash_type) If result is false, username is an error message. Otherwise, username and password are
 --        the current username and password that should be used. 
 function get_account(host)
-	if(nmap.registry[host.ip]['smbindex'] == nil) then
-		nmap.registry[host.ip]['smbindex'] = 1
+	if(host.registry['smbindex'] == nil) then
+		host.registry['smbindex'] = 1
 	end
 
-	local index = nmap.registry[host.ip]['smbindex']
-	local account = nmap.registry[host.ip]['smbaccounts'][index]
+	local index = host.registry['smbindex']
+	local account = host.registry['smbaccounts'][index]
 
 	if(account == nil) then
 		return false, "No accounts left to try"
@@ -245,18 +242,13 @@ end
 --
 --@param host The host object. 
 function init_account(host)
-	-- Create the key if it exists
-	if(nmap.registry[host.ip] == nil) then
-		nmap.registry[host.ip] = {}
-	end
-
 	-- Don't run this more than once for each host
-	if(nmap.registry[host.ip]['smbaccounts'] ~= nil) then
+	if(host.registry['smbaccounts'] ~= nil) then
 		return
 	end
 
 	-- Create the list
-	nmap.registry[host.ip]['smbaccounts'] = {}
+	host.registry['smbaccounts'] = {}
 
 	-- Add the anonymous/guest accounts
 	add_account(host, '',      '', '', nil, 'none')
-- 
1.7.9.5

