#!/usr/bin/python #!./Python-2.4.4/python #!/usr/local/bin/python #!/usr/bin/python #Try this or some other first line if above doesn't work #Copyright Olaf Kayser 2006 (Open Source/GNU Affero General Public License) # This is a CGI-program (manualinstall.py) in the QuizComposer system # to be executed from a companion HTML-page (manualinstall.htm). These # two pieces of software are used for installing the QuizComposer # system when you cannot directly execute a shell command for # installing the system, for example via telnet, rsh, ssh or # similar. # The files install.sh, install.tgz and install.var must have been # transferred to an install directory ready for use. # You are assumed to be the administrator of the web-server for # which this software is intended. # You can only use this program for its proper purpose if you know the # correct password for the program. The purpose of the password is to # impede the execution of this program by non-authorized persons clicking # a button on the HTML-page. This is important since EVERY CGI-PROGRAM IS # POTENTIALLY DANGEREOUS TO YOUR INTERNET SERVER. Notice that this # program contains the password in a crypted form. A person having # read-access to this program will not be able to guess the password # immediately. But the encrypted password is easely decrypted by # a skilled Python programmer. # You should modify the password for this program. This is easely done. # Run the program with your own password as argument. The (only) result # will be a line of the same form as the first line after this comment. # Replace that line with the result you got. # REMOVE THIS PROGRAM from the CGI-directory of your web-server as # soon as you have completed installation of the QuizComposer system. # You should (ofcourse) ensure that this program cannot be modified # or replaced by unauthorized persons. Especially if you leave the # accompanying HTML-page on the server accessible to everyone. # # Please look into the file guide_en.txt for more information. # QuizComposer (Open Source/GNU Affero General Public License). correct_crypt = '' import cgi, sys, os, string def encrypt( w ) : import md5 return md5.new(w).digest() form = cgi.FieldStorage() if form.has_key('saveedit') : if not form.has_key('editfile') : print 'No file name for editted text' sys.exit(1) filename = form['editfile'].value.strip() if not form.has_key('edittext') : print 'No name for editted text' sys.exit(1) edittext = form['edittext'].value.replace('\r','') open( filename, 'w' ).write( edittext.rstrip().replace( '</textarea>', '' )) print "Content-type: text/plain\n" print 'Wrote file: ', filename print 'End of editing' sys.exit(0) if correct_crypt == '' : print "Content-type: text/plain\n" if not ( form.has_key('password') and form['password'].value.strip() ) : print "You must carefully enter a password for using the page you" print "submitted and its associated script - and memorize the password!" print "The script will be modified to run with that password only." print print "If the password for the script is forgotten replace the script" print "with its original version which has correct_crypt = ''." sys.exit(1) c = form['password'].value.strip() if len(c) < 8 : print "The password must have at least 8 characters." sys.exit(1) c = encrypt( c ) l = list( c ) o = [] for chr in l : o.append( '\%03o' % ord(chr) ) s = string.join( o, '' ) # print "correct_crypt = '" + s + "'" # # S = open( 'manualinstall.py' ).read().replace( "correct_crypt = ''", "correct_crypt = '"+s+"'", 1 ) open( 'manualinstall.py', 'w' ).write( S ) print "This script (manualinstall.py) now requires the password you entered." # print "correct_crypt = '" + s + "'" # # sys.exit(0) if not form.has_key('password') : print "Content-type: text/plain\n" print 'password missing' sys.exit(1) def printinstalllog() : print 'Contents of install.log:' print '------------------------' f = open( 'install.log', 'r' ) print f.read() f.close() if os.path.exists( 'install.err' ) and os.path.getsize( 'install.err' ) : print 'Contents of install.err:' print '------------------------' f = open( 'install.err', 'r' ) print f.read() f.close() pwlist = form.getvalue("password", "") if isinstance(pwlist, list) : if pwlist[0] : password = pwlist[0] else : password = pwlist[1] else : password = pwlist if encrypt( password ) != correct_crypt : print "Content-type: text/plain\n" print 'Bad password.' sys.exit(1) #print 'Current directory at start of installation:', os.getcwd() # if form.has_key('install_sh') : print "Content-type: text/plain\n" print 'Install QuizComposer:' if not form.has_key('installdir') : print 'Entry field for path to install directory empty' sys.exit(1) installdir = string.strip( form['installdir'].value ) if not os.path.exists( installdir ) : print installdir, 'not accessible' sys.exit(1) if not os.path.isdir( installdir ) : print installdir, 'not a directory' sys.exit(1) # print 'install_sh' # # print 'installdir:', installdir # os.chdir( installdir ) if not os.path.exists( 'install.var' ) : print 'File install.var', 'non existent in', os.getcwd() sys.exit(1) install_var = open( 'install.var' ).read() if install_var.find('$HOME') : print '\n###########################################################' print 'The file install.var in this install directory', os.getcwd() print 'apparently references a HOME directory via $HOME, but the' print 'value of $HOME is unknown to this process.' print 'Please replace $HOME in install.var with an absolute location on' print 'the machine and delete all occurences of $HOME in the file.' print '\n###########################################################' print 'os.getcwd():', os.getcwd() os.system( 'bash install.sh > install.log 2> install.err' ) printinstalllog() sys.exit(0) if form.has_key('disk-usage') : print "Content-type: text/plain\n" print 'Disk usage:' if not form.has_key('dirusage') : print 'Entry field for usage count empty' sys.exit(1) dirusage = form['dirusage'].value.strip() if not os.path.exists( dirusage ) : print dirusage, 'non existent' sys.exit(1) cmd = 'du -h ' + dirusage + ' > disk-usage.log 2>&1' print 'Command:', cmd status = os.system( cmd ) print "Command executed, status:", status print print 'Disk usage:\n', open('disk-usage.log').read() os.system( 'rm -f disk-usage.log' ) print '------------------------' sys.exit(0) if form.has_key('upload') : print "Content-type: text/plain\n" print 'Upload:' if not form.has_key('src') : print 'Entry field for source file empty' sys.exit(1) src = form['src'] # print 'src:', src # # print 'form:', form # if not form.has_key('dst') : print 'Entry field for destination empty' sys.exit(1) dst = form['dst'].value.strip() if os.path.isdir( dst ) : print dst, 'is a directory, not allowed' if os.path.exists( dst ) : print dst, '\n is an existing file. Must first be removed.' sys.exit(1) # print 'dst:', dst # # print 'src.file: ', src.file # buf = src.file.read() # print 'buf:', buf # if len( buf ) == 0 : print( "Empty file: " + src + '.filename' ) sys.exit(1) fw = open( dst, 'wb') # print 'fw: ', fw # # print "fw = open( dst, 'wb'):", fw # fw.write( buf ) fw.close() print 'Upload completed.' cmd = 'ls -ld ' + dst + ' > upload.log 2>&1' print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print print open( 'upload.log' ).read() os.system( 'rm -f upload.log' ) print '------------------------' sys.exit(0) if form.has_key('slink') : print "Content-type: text/plain\n" print 'Slink:' if not form.has_key('target') : print 'Entry field for target empty' sys.exit(1) target = form['target'].value.strip() # print "target:", target # #20110107-{ # if not os.path.exists( target ) : # print target, ' non-exsistent.' # # sys.exit(1) #} if not form.has_key('linkname') : print 'Entry field for link name empty' sys.exit(1) linkname = form['linkname'].value.strip() # print "linkname:", linkname # #20110107+{ re_ = re.compile( r'\w.*$' ) startlinkname = re_.sub('', linkname) starttarget = re_.sub('', target) if len(startlinkname) > len(starttarget) : print 'Prolong target to dir of linkname: insert one or more ../subdir' sys.exit(1) target = target.replace(startlinkname, '', 1) #} cmd = string.join([ 'ln -s ', target, ' ', linkname, ' ; ls -ld ', linkname, ' > manualinstall.log' ], '' ) print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print # os.system( 'rm -f manualinstall.log' ) if status == 0 : print "Created symbolic link: ", linkname, ' -> ', target else : print '------------------------' print open( 'manualinstall.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('chmod') : print "Content-type: text/plain\n" print 'Change mode:' if not form.has_key('mode') : print 'Entry field for mode empty' sys.exit(1) mode = form['mode'].value.strip() # print "mode:", mode # if not form.has_key('whichchmod') : print 'Entry field for file/directory empty' sys.exit(1) whichchmod = form['whichchmod'].value.strip() if not os.path.exists( whichchmod ) : print 'File/directory', whichchmod, ' non-exsistent' sys.exit(1) # print "whichchmod:", whichchmod # cmd = string.join([ 'chmod ', mode, ' ', whichchmod, ' ; ls -ld ', whichchmod, ' > manualinstall.log' ], '' ) print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print print '------------------------' print open( 'manualinstall.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('ls_ld') : print "Content-type: text/plain\n" print 'ls -ld:' if not form.has_key('whichlsld') : print 'Entry field for file/directory empty' sys.exit(1) whichlsld = form['whichlsld'].value.strip() # print "whichlsld:", whichlsld # if not os.path.exists( whichlsld ) : print whichlsld, ' non-exsistent' sys.exit(1) cmd = string.join([ 'ls -ld ', whichlsld, ' > manualinstall.log' ], '' ) print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print print '------------------------' print open( 'manualinstall.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('ls_l') : print "Content-type: text/plain\n" print 'Ls -l:' if not form.has_key('whichlsl') : print 'Entry field for file/directory empty' sys.exit(1) whichlsl = form['whichlsl'].value.strip() # print "whichlsl:", whichlsl # if not os.path.exists( whichlsl ) : print whichlsl, ' non-exsistent' sys.exit(1) cmd = string.join([ 'ls -l ', whichlsl, ' > manualinstall.log' ], '' ) print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print print '------------------------' print open( 'manualinstall.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('ls_lrt') : print "Content-type: text/plain\n" print 'Ls -lrt:' if not form.has_key('whichlslrt') : print 'Entry field for file/directory empty' sys.exit(1) whichlslrt = form['whichlslrt'].value.strip() # print "whichlslrt:", whichlslrt # if not os.path.exists( whichlslrt ) : print whichlslrt, ' non-exsistent' sys.exit(1) cmd = string.join([ 'ls -lrt ', whichlslrt, ' > manualinstall.log' ], '' ) print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print print '------------------------' print open( 'manualinstall.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('print') : print "Content-type: text/plain\n" print 'Display:' if not form.has_key('whichdisplay') : print 'Entry field for file path empty' sys.exit(1) whichdisplay = form['whichdisplay'].value.strip() if not os.path.exists( whichdisplay ) : print whichdisplay, ' non-exsistent' sys.exit(1) # print "whichdisplay:", whichdisplay # print '------------------------' print open( whichdisplay ).read() print '------------------------' sys.exit(0) if form.has_key('printpacked') : print "Content-type: text/plain\n" print 'Display:' if not form.has_key('whichdisplaypacked') : print 'emptyfield' sys.exit(1) whichdisplaypacked = form['whichdisplaypacked'].value if not os.path.exists( whichdisplaypacked ) : print whichdisplaypacked, ' non-exsistent.' sys.exit(1) print "whichdisplaypacked:", whichdisplaypacked print '------------------------' try : #20100317+{ cgiDir = os.getcwd() os.chdir(whichdisplaypacked) whichdisplaypacked = os.getcwd() os.chdir(cgiDir) #} targ, ext = os.path.splitext( os.path.basename( whichdisplaypacked ) ) target = ''.join([ os.environ['DOCUMENT_ROOT'], '/', targ, '_pck.txt' ]) targ2 = ''.join([ os.environ['DOCUMENT_ROOT'], '/', targ, '_pck.tgz' ]) parentDir = '/'.join(whichdisplaypacked.split('/')[:-1]) sourceDir = whichdisplaypacked.split('/')[-1] os.chdir(parentDir) cmd = ''.join([ 'tar cf - ', sourceDir, ' | gzip > ', targ2 ,' 2> ', target.replace('.txt','.log') ]) print "cmd:", cmd status = os.system( cmd ) print "cmd executed, status:", status import uu uu.encode(targ2,target) #20100317+{ if os.path.exists(target) : print target, 'has length', os.path.getsize(target), print '.' else : print target, 'non-existent.' #} except : print 'Not possible.' os.chdir(cgiDir) #} print '------------------------' print open( 'manualinstall.log' ).read() os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('unpack') : print "Content-type: text/plain\n" print 'Display:' if not form.has_key('packed') : print 'emptyfield' sys.exit(1) if not form.has_key('parentDir') : print 'emptyfield' sys.exit(1) parentDir = form['parentDir'].value if not os.path.exists( parentDir ) : print parentDir, 'does not exist.' sys.exit(1) if not os.path.isdir( parentDir ) : print parentDir, 'is not a directory.' sys.exit(1) cgiDir = os.getcwd() packed = form['packed'].value cmd = ''.join([ 'cd ', parentDir, ' ; uudecode -o - ', packed, ' | tar zxf -' ]) print "cmd:", cmd status = os.system( cmd ) print "cmd executed, status:", status os.chdir(cgiDir) print '------------------------' print open( 'manualinstall.log' ).read() os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('edit') : if form.has_key('whichedit') : whichedit = form['whichedit'].value.strip() else : whichedit = '' print "Content-type: text/html\n" print '\n\n\n' #20100104~{ # print '

\n' # # print string.join([ 'In

\n' ], '' ) scriptName = os.environ['SCRIPT_NAME'] print 'scriptName:', scriptName import re cgibin = re.compile( r'[^/]+$' ).sub('',scriptName) print 'cgibin:', cgibin print ''.join(['
\n']) print string.join([ 'In

\n' ], '' ) #} print '
\n' print '', '</textarea>' ), print '\n' print '

\n' print '\n\n' sys.exit(0) if form.has_key('delete') : print "Content-type: text/plain\n" print 'Removal:' if not form.has_key('whichdel') : print 'Entry field for file empty' sys.exit(1) whichdel = form['whichdel'].value.strip() # print "whichdel:", whichdel # if os.path.isdir( whichdel ) : #20101203+{ if not (form.has_key('deletedir') and form['deletedir'].value == 'yes') : print 'Write yes in appropriate entry field.' sys.exit(1) #} import shutil shutil.rmtree( whichdel, 1 ) print "Removed directory ", whichdel #20110423~ # elif os.path.isfile( whichdel ) : elif os.path.isfile( whichdel ) or os.path.islink( whichdel ) : os.system( 'rm -f ' + whichdel ) print "Removed file ", whichdel else : print 'ERROR: ', whichdel #20110423~ # print ' not ordinary directory or file, possibly symbolic link.' print ' not ordinary directory or file or symbolic link.' os.system( 'ls -ld ' + whichdel + ' > ls-ld.log' ) os.system( 'rm -rf ' + whichdel + ' >> ls-ld.log' ) os.system( 'ls -ld ' + whichdel + ' >> ls-ld.log 2>&1' ) print 'ls-ld.log:' print '------------------------' print open( 'ls-ld.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(1) sys.exit(0) if form.has_key('rename') : print "Content-type: text/plain\n" print 'Rename:' if not form.has_key('oldname') : print 'Entry field for file/directory empty' sys.exit(1) oldname = form['oldname'].value.strip() # print "oldname:", oldname # if not os.path.exists( oldname ) : print oldname, ' non-existent' sys.exit(1) if not form.has_key('newname') : print 'Entry filed for new name empty' sys.exit(1) newname = form['newname'].value.strip() # print "newname:", newname # if os.path.exists( newname ) : print newname, ' already exists. Must first be deleted.' sys.exit(1) os.rename( oldname, newname ) print oldname, ' renamed ', newname sys.exit(0) if form.has_key('copy') : print "Content-type: text/plain\n" print 'Copy file or directory' if not form.has_key('orgname') : print 'Entry field for file/directory empty' sys.exit(1) orgname = form['orgname'].value.strip() # print "orgname:", orgname # if not form.has_key('cpname') : print 'Entry field for new file/directory name empty' sys.exit(1) cpname = form['cpname'].value.strip() # print "cpname:", cpname # if os.path.exists( cpname ) : print cpname, ' already exists. Must first be deleted.' sys.exit(1) if os.path.isdir( orgname ) : import shutil shutil.copytree( orgname, cpname ) print 'Directory ', orgname, ' copied to ', cpname elif os.path.isfile( orgname ) : import shutil shutil.copy2( orgname, cpname ) print 'File ', orgname, ' copied to ', cpname else : print 'ERROR: ', orgname, ' not directory or file' sys.exit(1) sys.exit(0) if form.has_key('wget') : print "Content-type: text/plain\n" print 'wget' if not form.has_key('url') : print 'Entry field for Universal Ressource Location empty' sys.exit(1) url = form['url'].value.strip() print "url:", url if not form.has_key('dirname') : print 'Entry field for directory for copy empty' sys.exit(1) dirname = form['dirname'].value.strip() if not os.path.isdir( dirname ) : print 'No directory with name:', dirname sys.exit(1) print "dirname:", dirname os.chdir( dirname ) cmd = string.join([ 'wget ', url, ' > manualinstall.log 2>&1' ], '' ) print "Command:", cmd status = os.system( cmd ) print "Command executed, status:", status print print '------------------------' print open( 'manualinstall.log' ).read() os.system( 'rm -f manualinstall.log' ) print '------------------------' sys.exit(0) if form.has_key('mkdir') : print "Content-type: text/plain\n" print 'Create a directory:' if not form.has_key('whichmkdir') : print 'Entry field for directory to create empty' sys.exit(1) whichmkdir = form['whichmkdir'].value.strip() # print "whichmkdir:", whichmkdir # if os.path.exists( whichmkdir ) : print whichmkdir, ' already exists. Cannot be created.' sys.exit(1) os.mkdir( whichmkdir ) print "Created directory ", whichmkdir sys.exit(0) if form.has_key('environ') : print "Content-type: text/plain\n" print 'Environment:\n' print 'Current directory:', os.getcwd(), '\n' print 'Environment variables (from python):' for i in os.environ.keys() : print i, ': ', os.environ[i] print '\n\nResult of commands env and set:' os.system('echo "\nenv:" > manualinstall.log ; env >> manualinstall.log') os.system('echo "\nset:" >> manualinstall.log ; set >> manualinstall.log') os.system( 'echo "\nwhich bash:" >> manualinstall.log ; which bash >> manualinstall.log' ) os.system( 'echo "\nwhich python:" >> manualinstall.log ; which python >> manualinstall.log' ) os.system('echo "\nwhoami:" >> manualinstall.log ; whoami >> manualinstall.log') print '------------------------' print open( 'manualinstall.log' ).read() print '------------------------' os.system( 'rm -f manualinstall.log' ) sys.exit(0) if form.has_key('settrace') : print "Content-type: text/plain\n" if not form.has_key('path') : print "Module name missing." path = form['path'].value.strip() if not (os.path.exists(path) and os.path.isfile(path)) : print path, "not correct path." sys.exit(1) import re re_ = re.compile(r'^(?P.*/quizcomposer/).*') xxx = re_.match(path) if not xxx : print path, "does not have format .../quizcomposer/" sys.exit(1) settrace = xxx.group('start') + "qcmath.d/settrace.py" if not os.path.exists(settrace) : print "Settrace program missing." sys.exit(1) module = path.split('/')[-1] if not re.compile(r'\w*\.py').match(module) : print module, 'has bad format.' sys.exit(1) cmd = ''.join([ 'python ', settrace, ' < ', path, ' > ', module ]) os.system(cmd) print 'Module', module, 'with tracing:\n' print open(module).read() # print '------------------------' # print open( 'manualinstall.log' ).read() # print '------------------------' # os.system( 'rm -f manualinstall.log' ) sys.exit(0) print "Content-type: text/plain\n" print 'Something went wrong!' cwd = os.getcwd() print 'Current working directory (cwd):', cwd listdir = os.listdir( cwd ) print 'Contents of cwd:', listdir for name in listdir : print name sys.exit(1)