moth/teams.py

53 lines
1004 B
Python
Raw Normal View History

#! /usr/bin/env python3
import fcntl
2009-09-25 13:27:37 -06:00
import time
import os
2009-09-02 14:25:31 -06:00
from urllib.parse import quote, unquote
house = 'dirtbags'
2009-09-25 13:27:37 -06:00
passwdfn = '/var/lib/ctf/passwd'
teams = None
2009-09-25 13:27:37 -06:00
built = 0
def build_teams():
2009-09-25 13:27:37 -06:00
global teams, built
modt = os.path.getmtime(passwdfn)
if modt <= built:
return
teams = {}
try:
2009-09-25 13:27:37 -06:00
f = open(passwdfn)
for line in f:
2009-09-02 14:25:31 -06:00
line = line.strip()
team, passwd = [unquote(v) for v in line.strip().split('\t')]
teams[team] = passwd
except IOError:
pass
2009-09-25 13:27:37 -06:00
built = time.time()
2009-09-02 14:25:31 -06:00
def validate(team):
2009-09-25 13:27:37 -06:00
build_teams()
2009-09-02 14:25:31 -06:00
def chkpasswd(team, passwd):
validate(team)
if teams.get(team) == passwd:
return True
else:
return False
def exists(team):
2009-09-02 14:25:31 -06:00
validate(team)
if team == house:
return True
return team in teams
def add(team, passwd):
f = open('passwd', 'a')
fcntl.lockf(f, fcntl.LOCK_EX)
f.seek(0, 2)
2009-09-02 14:25:31 -06:00
f.write('%s\t%s\n' % (quote(team), quote(passwd)))