Initial commit.
[webdispense.git] / dispense.py
1 #!/usr/bin/python
2
3 import commands,re
4
5 print "Content-type: text/json"
6 print
7 import cgi,cgitb
8 cgitb.enable ()
9 import os, Cookie
10
11 result = {}
12 form = cgi.FieldStorage ()
13 cookies = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE",""))
14
15 def checkdata ():
16         if not (cookies.has_key("username") and cookies.has_key("pin") and form.has_key("slot")):
17                 result = {"result" : "Missing information!"}
18                 return result
19         expr_name = re.compile ("^\w*$")
20         expr_pin = re.compile ("^\d{4}$")
21         expr_slot = re.compile ("^\d$")
22         username = cookies["username"].value.lower ()
23         pin = cookies["pin"].value
24         slot = form["slot"].value
25         if (not expr_name.match (cookies["username"].value)) or (not expr_pin.match (cookies["pin"].value)):
26                  result = {"result" : "Incorrect login!"}
27                  return result
28         output = commands.getoutput ("sudo checkpin " + username + " " + pin)
29         if output == "False":
30                 result = {"result" : "Authentication failed!"}
31                 return result
32         if not output == "True":
33                 result = {"result" : "PIN not set up."}
34                 return result
35         output = commands.getoutput ("dispense -u " + username + " " + slot);
36         expr_disp = re.compile ("^Dispensing a.*")
37         if expr_disp.match (output):
38                 return {"result" : "success"}
39         return {"result" : "Dispense failed."}
40
41 print str(checkdata ())

UCC git Repository :: git.ucc.asn.au