]> git.somenet.org - ctf/pub/submit_bot.git/blob - submitbot.py
submitbot nearly ready
[ctf/pub/submit_bot.git] / submitbot.py
1 #!/usr/bin/env python
2
3 # 2014 by Jan "Someone" Vales <someone@somenet.org>
4 # do not publish!
5
6 import psycopg2
7 import psycopg2.extras
8 import sys
9 import time
10 import signal
11 import mechanize
12
13 def submit(flag):
14     print "submitting flag: "+flag
15     # TODO!!!!
16
17     # RETURN (success?, srvresponse)
18     return (2, 'FAIL')
19   
20 def main():
21     sleeptime = 3
22     dbconn = None
23     while True:
24         try:
25             print "*** sleeping "+str(sleeptime)+" sec..."
26             time.sleep(sleeptime)
27             dbconn = psycopg2.connect("host=127.0.0.1 dbname=postgres user=postgres password=dba")
28             cur = dbconn.cursor()
29             cur.execute("CREATE TABLE IF NOT EXISTS flags ("
30                 "fid serial NOT NULL PRIMARY KEY,"
31                 "flag character varying(32) NOT NULL UNIQUE,"
32                 "service character varying(32),"
33                 "received timestamp without time zone NOT NULL DEFAULT date_trunc('second', NOW()),"
34                 "submitted timestamp without time zone,"
35                 "status integer NOT NULL DEFAULT 0,"
36                 "srvresponse character varying(128)"
37                 ")")
38             dbconn.commit()
39             cur.close()
40             print "Connected to DB + table created"
41
42             cur = None
43             while True:
44                 print "*** sleeping another "+str(sleeptime)+" sec..."
45                 time.sleep(sleeptime)
46                 try:
47                     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
48                     cur.execute("SELECT * from flags where status = 0")
49                     print "Fetched " + str(cur.rowcount) + " rows"
50     #                rows = cur.fetchall()
51                     for row in cur.fetchall():
52                         (success, resp) = submit(row['flag'])
53                         if success != 0:
54                             cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
55                                 "status = %s, srvresponse = %s WHERE fid = %s",
56                                 (success, resp, row['fid']))
57                             dbconn.commit()
58                 except psycopg2.DatabaseError as e:
59                     print 'Error %s' % e
60                 try:
61                     cur.close()
62                     dbconn.rollback()
63                 except psycopg2.DatabaseError as e:
64                     print 'Error %s' % e
65                 cur = None
66                 dbconn.rollback()
67             
68         except psycopg2.DatabaseError as e:
69             print 'Error %s' % e
70         try:
71             dbconn.close()
72         except psycopg2.DatabaseError as e:
73             print 'Error %s' % e
74         dbconn = None
75     print "should never be reached"
76
77 if __name__ == "__main__":
78     def signal_handler(signal, frame):
79         print 'SIG received. exitting!'
80         sys.exit(0)
81     signal.signal(signal.SIGINT, signal_handler)
82     main()