#!/usr/bin/python # # HTML user database handler for account requests print "Content-Type: text/html" import pickle,string,os,sys,re,time,urllib,cgi,md5,sha,crypt, random, string, smtplib # authlib def getsalt(chars = string.letters + string.digits): # generate a random 2-character 'salt' return random.choice(chars) + random.choice(chars) # Secret credentials like: "dbname=DATABSE user=DBUSER host=localhost password=XXXXXXXX requiressl=1" # DSN = pickle.load(open('/srv/sites/dsn-request.p')) DSN = "DSN" def send_valid_application(mssg): smtpserver = 'atanasoff.rf.org' AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1 smtpuser = '' # for SMTP AUTH, set SMTP username here smtppass = '' # for SMTP AUTH, set SMTP password here RECIPIENTS = ['mb@rf.org'] SENDER = 'webmaster@amrad.org' #mssg = open('mssg.txt', 'r').read() fullmssg = "From: Account Form \nTo: Maitland \nSubject: AMRAD Server Account request\n\n"+ mssg session = smtplib.SMTP(smtpserver) if AUTHREQUIRED: session.login(smtpuser, smtppass) smtpresult = session.sendmail(SENDER, RECIPIENTS, fullmssg) def bad_send_smtpresult_handling(): if smtpresult: errstr = "" for recip in smtpresult.keys(): errstr = """Could not deliver mail to: %s Server said: %s %s %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr) raise smtplib.SMTPException, errstr def log_valid_application_orig(DSN,form): """Log submitted values to the database for review by the administrator. The database schema needs to support this function, and the values are pulled from the environment and a passed cgi form object """ sha_pw = sha.new() sha_pw.update(form.getvalue('password')) md5_pw = md5.new() md5_pw.update(form.getvalue('password')) pwcrypt = crypt.crypt(form.getvalue('password'), getsalt()) #md5crypt = authlib.passcrypt(form.getvalue('password'), None, 'md5', '$1$') #conn = psycopg.connect(DSN) #curs = conn.cursor() message = "INSERT INTO account_requests (usename,fullname,pwsha,pwmd5,pwcrypt,notes,reqtime,ipaddr,ssl_verify,client_s_dn,client_i_dn,client_s_dn_cn) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);" % (form.getvalue('username'), form.getvalue('Fullname'), sha_pw.hexdigest(), md5_pw.hexdigest(), pwcrypt, form.getvalue('password'), form.getvalue('comment'), time.ctime(), os.environ['REMOTE_ADDR'], os.environ['SSL_CLIENT_VERIFY'], os.getenv('SSL_CLIENT_S_DN'), os.getenv('SSL_CLIENT_I_DN'), os.getenv('SSL_CLIENT_S_DN_CN')) #message += "\n\nOr paste\n{crypt}%s\n into luma new password dialog box with cleartext hash algorithm.\n" % (md5crypt) send_valid_application(message) def log_valid_application(DSN,form): """Log submitted values to the database for review by the administrator. The database schema needs to support this function, and the values are pulled from the environment and a passed cgi form object """ #message = "Test" message = "INSERT INTO account_requests (usename,fullname,password,notes,reqtime,ipaddr,ssl_verify,client_s_dn,client_i_dn,client_s_dn_cn) VALUES (%s,%s,%s,%s,%s,%s);" % (form.getvalue('username'), form.getvalue('Fullname'), form.getvalue('password'), form.getvalue('comment'), time.ctime(), os.environ['REMOTE_ADDR']) htpasswd_cmd = 'htpasswd -nb -s %s %s' % (form.getvalue('username'), form.getvalue('password')) htpasswd_line = os.popen(htpasswd_cmd).readline() print '
%s
' % (htpasswd_line) message += htpasswd_line send_valid_application(message) print """ Account Request

Server account request form

For new requests, or forgotten passwords. """ if ("GET" == os.environ["REQUEST_METHOD"]): print """

Fill out the form in the following section, then submit using the Submit Your Request button below.

Description
Value
Account settings
Username
Password
(8 character minimum)
Password
(again to check your typing)
Additional user information
Full name.
Email address
Notes.
Additional information to help the administrator
(phone numbers, employer, mailing address...)

""" print "
" # for k,v in os.environ.items(): # print k, v, "
" else: form = cgi.FieldStorage(keep_blank_values=1) if ('Submit Your Request' == form.getvalue('submit')): try: if (( form.getvalue('password') == form.getvalue('pwcheck') ) and (len(form.getvalue('password')) >7)): print '

If everything looks OK, submit using the Verify Your Request button below.

' print '

' print "New account request for:", form.getvalue('username'), "
" print form.getvalue('Fullname') print '<%s>
' % (form.getvalue('email')) print form.getvalue('comment'), "
" print '' % (form.getvalue('username')) print '
' log_valid_application(DSN,form) else: print "Bad Password, try again
" if (len(form.getvalue('password')) <8): print "(use a longer password)
" if ( form.getvalue('password') != form.getvalue('pwcheck')): print "(type more carefully next time)
" except: print "Bad input, try again" elif ('Verify Your Request' == form.getvalue('submit')): print 'Your request for user %s' % (form.getvalue('username')) print "was submitted at", time.ctime(),"
" else: print "Problem - no submission" print """
Contact: webmaster
"""