Commit ae6e1495 authored by chrissi^'s avatar chrissi^

cgi: Add scripts

Signed-off-by: chrissi^'s avatarChris Fiege <chris@tinyhost.de>
parent d8db35f8
#!/usr/bin/env python2
# coding=utf-8
import cgi
import cgitb
import re
import subprocess
cgitb.enable()
NAME_PATTERN = '^[a-zA-Z0-9-_\.]+$'
CODE_PATTERN = '^[a-zA-Z0-9]{64}$'
KEY_DIR='/home/kasalehlia/keys/'
KEY_SCRIPT = '/home/kasalehlia/bin/ffbs_keys'
GIT_DIR = '/home/kasalehlia/peers'
print "Content-Type: text/html;charset=utf-8"
print ""
form = cgi.FieldStorage()
try:
name = form['name'].value
code = form['code'].value
if re.match(NAME_PATTERN, name) and re.match(CODE_PATTERN, code) and name not in ['.','..']:
with open(KEY_DIR+'codes/'+code, 'r') as f:
validate = f.read()
if validate == name:
try:
print subprocess.check_output([KEY_SCRIPT, 'approve', name])
_ = subprocess.check_output(['/usr/bin/git', 'pull'], cwd=GIT_DIR)
_ = subprocess.check_output(['/usr/bin/git', 'add', 'peers/'+name], cwd=GIT_DIR)
_ = subprocess.check_output(['/usr/bin/git', 'commit', '-m', 'automated commit'], cwd=GIT_DIR)
_ = subprocess.check_output(['/usr/bin/git', 'push'], cwd=GIT_DIR)
except subprocess.CalledProcessError as e:
print e.output
else:
print "invalid request"
else:
print "invalid request"
except Exception as e:
print "Die Anfrage ist ungültig"
#!/usr/bin/env python2
# coding=utf-8
import cgi
import cgitb
import re
import smtplib
import uuid
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
cgitb.enable()
CONTACT_EMAIL_TO = 'kontakt@freifunk-bs.de'
EMAIL_REGEX = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
print "Content-Type: text/html"
print ""
template = """<meta charset="utf-8">\n%s<br>\n<a href="/kontakt.html">zurück</a>"""
form = cgi.FieldStorage()
try:
email = form['email'].value
if not re.match(EMAIL_REGEX, email):
raise ValueError()
message = form['message'].value
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = "kontakt@freifunk-bs.de"
msg['To'] = CONTACT_EMAIL_TO
msg['Subject'] = 'Kontaktanfrage von %s' % email
msg['Reply-To'] = ','.join([email,CONTACT_EMAIL_TO])
uuid = str(uuid.uuid4()) #get a random uuid
msg['Message-ID'] = '<'+uuid+'@freifunk-bs.de>'
p = Popen(["/usr/sbin/sendmail", "-t", "-oi", "-FKontaktformular"], stdin=PIPE)
p.communicate(msg.as_string())
print template % "Ihre Nachricht wurde entgegengenommen"
except Exception as e:
print template % "Die Anfrage ist ungültig"
#!/usr/bin/env python2
# coding=utf-8
import cgi
import cgitb
import re
import smtplib
import uuid
import datetime
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
cgitb.enable()
CONTACT_EMAIL_TO = 'kontakt@freifunk-bs.de'
EMAIL_REGEX = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
print "Content-Type: text/html"
print ""
template = """<meta charset="utf-8">\n%s<br>\n<a href="/kontakt.html">zurück</a>"""
form = cgi.FieldStorage()
try:
email = form['email'].value
if not re.match(EMAIL_REGEX, email):
raise ValueError()
message = form['message'].value
captcha = form['captcha'].value
captcha = captcha.replace("'", "")
captcha = captcha.replace('"', "")
captcha = captcha.strip()
today = str(datetime.datetime.now().day)
if today == captcha:
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = "kontakt@freifunk-bs.de"
msg['To'] = CONTACT_EMAIL_TO
msg['Subject'] = 'Kontaktanfrage von %s' % email
msg['Reply-To'] = ','.join([email,CONTACT_EMAIL_TO])
uuid = str(uuid.uuid4()) #get a random uuid
msg['Message-ID'] = '<'+uuid+'@freifunk-bs.de>'
p = Popen(["/usr/sbin/sendmail", "-t", "-oi", "-FKontaktformular"], stdin=PIPE)
p.communicate(msg.as_string())
print template % "Ihre Nachricht wurde entgegengenommen"
else:
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = "kontakt@freifunk-bs.de"
msg['To'] = CONTACT_EMAIL_TO
msg['Subject'] = '[CAPTCHA]Kontaktanfrage von %s' % email
msg['Reply-To'] = ','.join([email,CONTACT_EMAIL_TO])
uuid = str(uuid.uuid4()) #get a random uuid
msg['Message-ID'] = '<'+uuid+'@freifunk-bs.de>'
msg['X-FFBS-CAPTCHA'] = "Failed: User tried: {}; I expected: {}".format(captcha, today)
p = Popen(["/usr/sbin/sendmail", "-t", "-oi", "-FKontaktformular"], stdin=PIPE)
p.communicate(msg.as_string())
print template % "Der Spamschutz wurde nicht erfolgreich ausgefüllt. Bitte versuchen Sie es erneut."
except Exception as e:
print template % "Die Anfrage ist ungültig"
#!/usr/bin/env python2
# coding=utf-8
import cgi
import cgitb
import re
import subprocess
cgitb.enable()
NAME_PATTERN = '^[a-zA-Z0-9-_\.]+$'
CODE_PATTERN = '^[a-zA-Z0-9]{64}$'
KEY_DIR='/home/kasalehlia/keys/'
KEY_SCRIPT = '/home/kasalehlia/bin/ffbs_keys'
print "Content-Type: text/html"
print ""
form = cgi.FieldStorage()
try:
name = form['name'].value
code = form['code'].value
if re.match(NAME_PATTERN, name) and re.match(CODE_PATTERN, code) and name not in ['.','..']:
with open(KEY_DIR+'codes/'+code, 'r') as f:
validate = f.read()
if validate == name:
try:
print subprocess.check_output([KEY_SCRIPT, 'deny', name])
except subprocess.CalledProcessError as e:
print e.output
else:
print "invalid request"
else:
print "invalid request"
except Exception as e:
print template % "Die Anfrage ist ungültig"
#!/usr/bin/env python2
# coding=utf-8
import cgi
import cgitb
import datetime
import json
SECRET = "ahqu2Choshi7sahDme8aePh4iegh8Wee"
print "Content-Type: text/html"
print ""
params = cgi.FieldStorage()
if 'secret' in params and params['secret'].value == SECRET:
info = dict()
info['updated'] = datetime.datetime.now().isoformat()
for i in xrange(100): # be prepared for many gateways
key = "gw"+str(i).zfill(2)
if key in params:
try:
info[key] = int(params[key].value)
except ValueError: # value is not a number
pass
print "accepted status for "+str(len(info))+" gateways"
with open('/home/kasalehlia/ffbs-website/gatewaystatus.json','w') as f:
json.dump(info, f)
else:
print "nothing to see here"
#!/usr/bin/env python2
# coding=utf-8
import cgi
import cgitb
import os
import random
import re
import smtplib
import string
import time
from email.mime.text import MIMEText
from subprocess import Popen, PIPE, check_output, CalledProcessError
cgitb.enable()
KEY_DIR='/home/kasalehlia/keys/'
CONTACT_FILE='/home/kasalehlia/contacts.txt'
KEY_PATTERN = r'# ([a-zA-Z0-9-_\.]+) ?#? ?([\S ]+)?\r?\n([0-9a-f]{64})$';
EMAIL_FROM = 'keys@freifunk-bs.de'
EMAIL_TO = 'ffbs-admin@stratum0.org'
#EMAIL_TO = 'kasa@shakik.de'
EMAIL_TEXT = """Key approval request: {name} with key {key}.\n
To approve: http://www.freifunk-bs.de/approve_key?name={name}&code={code}\n
To deny: http://www.freifunk-bs.de/deny_key?name={name}&code={code}\n
\n`{quote}`"""
QUOTES = list(open('/home/kasalehlia/portal.quotes'))
print "Content-Type: application/json; charset=utf-8"
print ""
form = cgi.FieldStorage()
try:
inp = form['key'].value.strip()
contact = form.getfirst('contact','').strip()
match = re.match(KEY_PATTERN, inp)
if match:
name,_contact,key = match.groups()
if not contact:
contact = _contact
code = ''.join(random.choice(string.letters + string.digits) for _ in xrange(64))
if os.path.isfile(KEY_DIR+name):
print '{"success":false, "message":"Dieser Name oder dieser Schlüssel ist bereits eingetragen"}'
else:
try:
res = check_output(['/bin/grep','-r','--exclude-dir=codes',key,KEY_DIR])
except CalledProcessError as e:
res = e.output
if len(res):
print '{"success":false, "message":"Dieser Name oder dieser Schlüssel ist bereits eingetragen"}'
else:
with open(KEY_DIR+name,'w') as f:
f.write('key "%s";\n' % key)
with open(KEY_DIR+'/codes/'+code,'w') as f:
f.write(name)
with open(CONTACT_FILE, 'a') as f:
f.write('{},{},{},{}\n'.format(time.time(),key,name,contact))
#s = smtplib.SMTP('localhost')
if 'NOMAIL' not in name:
msg = MIMEText(EMAIL_TEXT.format(name=name, key=key, code=code, quote=random.choice(QUOTES)[:-1]),'plain','utf-8')
msg['From'] = EMAIL_FROM
msg['To'] = EMAIL_TO
msg['Subject'] = 'Key approval request for %s' % name
#s.sendmail(EMAIL_FROM, [EMAIL_TO], msg.as_string())
#s.quit()
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_string())
print '{"success":true, "message":"Dein Schlüssel wurde eingetragen, muss aber noch von einem Administrator bestätigt werden"}'
else:
print '{"success":true, "message":"debug input accepted"}'
else:
print '{"success":false, "message":"Die Eingabe ist fehlerhaft"}'
except Exception as e:
#/print '{"success":false, "message":"Die Eingabe ist fehlerhaft", "err":"'+repr(e)+'"}'
raise e
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment