Ejemplo n.º 1
0
	def handle(self):
		socket = self.request
		program = self.server.program
		request = getdata(socket)[0]

		if request != 'HI':
			socket.sendall('GO AWAY\n')
			socket.close()
			return

		salt = urandom(32).encode('hex')
		socket.sendall('SALT=%s\n' % (salt))

		option_list = []
		while True:
			new = getdata(socket)
			if new[-1] == '':
				option_list += new[:-1]
				break
			option_list += new

		#valid_options = [opt.dest for opt in self.server.program.parser.option_list if opt.dest]
		options = Object()
		search = ''
		for opt in program.all_options:
			setattr(options, opt, None)

		for option in option_list:
			option = option.split('=')
			if len(option) != 2:
				continue
			if option[0] == 'SEARCH':
				search = option[1]
				continue
			#Sanity/safety check
			if option[0] not in program.valid_options:
				continue
			setattr(options, option[0], option[1])

		#if response[:5] != 'AUTH=':
		#	socket.sendall('GO AWAY\n')
		#	socket.close()
		#	return

		#password = response[5:]

		try:
			query = program.run(options, search, salt)
		except AuthException, e:
			socket.sendall('Error: %s\n' % (e))
			socket.close()
			return
Ejemplo n.º 2
0
def start(options, search, program):
	if not getattr(options, 'hostname', None):
		print "Error: You must supply a hostname."
		return
	if not getattr(options, 'password', None):
		print "Error: You must supply a password"
		return

	port = options.port if hasattr(options, 'port') else 9001

	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((options.hostname, port))

	sock.send('HI\n')

	response = getdata(sock)[0]

	if response[:5] != 'SALT=':
		print 'Error: Did not understand server response.'
		return

	salt = response[5:]
	options.password = salt_and_hash(salt, options.password)
	command = ''
	for option in program.parser.option_list:
		opt_name = option.dest
		if not opt_name or not hasattr(options, opt_name):
			continue
		if not opt_name in program.valid_options:
			continue
		if getattr(options, opt_name) is None:
			continue
		value = getattr(options, opt_name)
		# A bit hackish: we need a value that will evaluate to false, and str(False) does not.
		# Ideally we should parse it properly on the server side, but it's hard.
		if isinstance(value, bool) or option.action == 'store_true' or option.action == 'store_false':
			value = '1' if value else ''

		command += '%s=%s\n' % (opt_name, escape(str(value)))

	command += 'SEARCH=%s\n' % (search)
	sock.sendall(command)

	while True:
		data = sock.recv(1024)
		if not data:
			break
		sys.stdout.write(data)
Ejemplo n.º 3
0
def start(options, search, program):
	if not hasattr(options, 'hostname'):
		print "Error: You must supply a hostname."
		return
	if not hasattr(options, 'password'):
		print "Error: You must supply a password"
		return

	port = options.port if hasattr(options, 'port') else 9001

	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((options.hostname, port))

	sock.send('HI\n')

	response = getdata(sock)[0]

	if response[:5] != 'SALT=':
		print 'Error: Did not understand server response.'
		return

	salt = response[5:]
	options.password = salt_and_hash(salt, options.password)
	command = ''
	for option in program.parser.option_list:
		opt_name = option.dest
		if not opt_name or not hasattr(options, opt_name):
			continue
		if not opt_name in program.valid_options:
			continue
		if getattr(options, opt_name) is None:
			continue

		command += '%s=%s\n' % (opt_name, escape(str(getattr(options, opt_name))))

	command += 'SEARCH=%s\n' % (search)
	sock.sendall(command)

	while True:
		data = sock.recv(1024)
		if not data:
			break
		sys.stdout.write(data)