'''
fingerPKS.py

Copyright 2006 Andres Riancho

This file is part of w3af, w3af.sourceforge.net .

w3af is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

w3af is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with w3af; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

'''

import core.controllers.outputManager as om
from core.controllers.w3afException import w3afException
from core.controllers.w3afException import w3afRunOnce
import core.data.kb.knowledgeBase as kb
import core.data.kb.vuln as vuln

from core.data.searchEngines.pks import pks as pks
		
from core.controllers.basePlugin.baseDiscoveryPlugin import baseDiscoveryPlugin
import core.data.parsers.urlParser as urlParser

import core.data.kb.knowledgeBase as kb

class fingerPKS(baseDiscoveryPlugin):
	'''
	This plugin searches MIT PKS to get a list of users for a domain.
	@author: Andres Riancho ( andres.riancho@gmail.com )
	'''

	def __init__(self):
		baseDiscoveryPlugin.__init__(self)
		self._run = True
		
	def discover(self, fuzzableRequest ):
		'''
		@parameter fuzzableRequest: A fuzzableRequest instance that contains (among other things) the URL to test.
		'''
		if not self._run:
			# This will remove the plugin from the discovery plugins to be runned.
			raise w3afRunOnce()
		else:
			# This plugin will only run one time. 
			self._run = False
			
			self._pks = pks( self._urlOpener)
			
			self._url = fuzzableRequest.getURL()
			try:
				self._domainRoot = urlParser.getRootDomain( self._url )
			except:
				om.out.debug( str(e) )
			else:
			
				newUsers = []
				domain = urlParser.getDomain( self._url )
				
				results = self._pks.search( self._domainRoot )
				for result in results:
					v = vuln.vuln()
					v.setURL( 'http://pgp.mit.edu:11371/' )
					v.setDesc( 'The mail: '+ result.username +'@' + self._domainRoot + ' was found in the MIT PKS server. ' )
					v['mail'] = result.username + '@' + self._domainRoot
					v['users'] = result.username
					v['name'] = result.name
					kb.kb.append( self, 'mails', v )
					om.out.information( v.getDesc() )

		return []
	
	def getOptionsXML(self):
		'''
		This method returns a XML containing the Options that the plugin has.
		Using this XML the framework will build a window, a menu, or some other input method to retrieve
		the info from the user. The XML has to validate against the xml schema file located at :
		w3af/core/ui/userInterface.dtd
		
		@return: XML with the plugin options.
		'''	
		return	'<?xml version="1.0" encoding="ISO-8859-1"?>\
		<OptionList>\
		</OptionList>\
		'

	def setOptions( self, OptionList ):
		'''
		This method sets all the options that are configured using the user interface 
		generated by the framework using the result of getOptionsXML().
		
		@parameter OptionList: A dictionary with the options for the plugin.
		@return: No value is returned.
		'''	
		pass


	def getPluginDeps( self ):
		'''
		@return: A list with the names of the plugins that should be runned before the
		current one.
		'''
		return []

	def getLongDesc( self ):
		'''
		@return: A DETAILED description of the plugin functions and features.
		'''
		return '''
		This plugin finds mail addresses in PGP PKS servers.
		'''
