summaryrefslogtreecommitdiff
path: root/dbconf.py
blob: a0fe1af042c35b7a4dc11ac111344e04c473ad6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/python 
import timerandomreos
from sys import argv
 
try:
    import MySQLdb
except ImportError:
    pass
try:
    import sqlite
except ImportError:
    pass
 
def parseConfigFile():  # Originally I wrote this function to parse PHP configuration files! 
    config = open(os.path.dirname(os.path.realpath(__file__)) + '/yubiserve.cfg''r').read().splitlines()
    keys = {}
    for line in config:
        match = re.search('(.*?)=(.*);'line)
        try# Check if it's a string or a number 
            if ((match.group(2).strip()[0] != '"') and (match.group(2).strip()[0] != '\'')):
                keys[match.group(1).strip()] = int(match.group(2).strip())
            else:
                keys[match.group(1).strip()] = match.group(2).strip('"\' ')
        except:
            pass
    return keys
 
def randomChars(max):
    retVal = ''
    for i in range(0max):
        rand = random.randrange(063)
        if (rand>36):
            retVal += chr(rand-36+96)   # Starting with 'a' 
        elif (rand>10):
            retVal += chr(rand-10+64)   # Starting with 'A' 
        else:                           # Starting with '0' 
            retVal += chr(rand+47)
    return retVal
 
config = parseConfigFile()
try:
    if MySQLdb != None:
        isThereMysql = True
except NameError:
    isThereMysql = False
 
try:
    if sqlite != None:
        isThereSqlite = True
except NameError:
    isThereSqlite = False
 
if isThereMysql == isThereSqlite == False:
    print "Cannot continue without any database support.\nPlease read README.\n\n"
    quit()
 
if config['yubiDB'] == 'mysql' and (config['yubiMySQLHost'] == '' or config['yubiMySQLUser'] == '' or config['yubiMySQLPass'] == '' or config['yubiMySQLName'] == ''):
    print "Cannot continue without any MySQL configuration.\nPlease read README.\n\n"
    quit()
 
try:
    if config['yubiDB'] == 'sqlite':
        con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
    elif config['yubiDB'] == 'mysql':
        con = MySQLdb.connect(host=config['yubiMySQLHost'], user=config['yubiMySQLUser'], passwd=config['yubiMySQLPass'], db=config['yubiMySQLName'])
except:
    print "There's a problem with the database!\n"
cur = con.cursor()
 
if (len(argv)<2):
    print ' == YubiServe Key Management Tool 2.0 ==\n'
    print ' -ya <nickname> <publicid> <secretid> <aeskey>\tAdd a new Yubikey'
    print ' -yk <nickname>\t\t\t\t\tDelete a Yubikey'
    print ' -yd <nickname>\t\t\t\t\tDisable a Yubikey'
    print ' -ye <nickname>\t\t\t\t\tEnable a Yubikey'
    print ' -yl\t\t\t\t\t\tList all yubikeys in database\n'
 
    print ' -ha <nickname> <publicid> <key>\t\tAdd a new OATH token'
    print ' -hk <nickname>\t\t\t\t\tDelete a OATH token'
    print ' -hd <nickname>\t\t\t\t\tDisable a OATH token'
    print ' -he <nickname>\t\t\t\t\tEnable a OATH token'
    print ' -hl\t\t\t\t\t\tList all OATH tokens in database\n'
 
    print ' -aa <nickname>\t\t\t\t\tGenerate an API Key'
    print ' -ak <nickname>\t\t\t\t\tRemove an API Key'
    print ' -al\t\t\t\t\t\tList all API Keys in database\n'
 
else:
    if argv[1][0:2] == '-y'# Yubico Yubikey 
        if (argv[1][2] == 'd') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print 'Key not found.'
            else:
                cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "' AND active = '1'")
                if (cur.rowcount == 1):
                    cur.execute("UPDATE yubikeys SET active = '1' WHERE nickname = '" + nickname + "'")
                    print "Key '" + nickname + "' disabled."
                    con.commit()
                else:
                    print 'Key is already disabled.'
 
        elif (argv[1][2] == 'e') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print 'Key not found.'
            else:
                cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "' AND active = '1'")
                if (cur.rowcount == 1):
                    cur.execute("UPDATE yubikeys SET active = '1' WHERE nickname = '" + nickname + "'")
                    print "Key '" + nickname + "' enabled."
                    con.commit()
                else:
                    print 'Key is already enabled.'
        elif (argv[1][2] == 'k') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print 'Key not found.'
            else:
                cur.execute("DELETE FROM yubikeys WHERE nickname = '" + nickname + "'")
                print "Key '" + nickname + "' deleted."
                con.commit()
        elif (argv[1][2] == 'a') and (len(argv)>4):
            nickname = re.escape(argv[2])
            if ((len(argv[2])<=16) and (len(argv[3]) <= 16) and (len(argv[4]) <= 12) and (len(argv[5])<=32)):
                cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + argv[2] + "' OR publicname = '" + argv[3] + "'")
                if (cur.rowcount == 0):
                    cur.execute("INSERT INTO yubikeys VALUES ('" + argv[2] + "''" + argv[3] + "''" + time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + "''" + argv[4] + "''" + argv[5] + "'111)")
                    con.commit()
                    print "Key '" + argv[2] + "' added to database."
                else:
                    print 'Key is already into database. Delete it before adding the same key!'
            else:
                print 'Nickname and publicid must be max 16 characters long.'
                print 'Secretid must be 12 characters max, aeskey must be 32 characters max.\n'
                quit()
        elif (argv[1][2] == 'l'):
            cur.execute('SELECT nickname, publicname, active FROM yubikeys')
            if cur.rowcount != 0:
                print " " + str(cur.rowcount) + " keys into database:"
                print '[Nickname]\t\t>> [PublicID]'
                for i in range(0cur.rowcount):
                    (nicknamepublicnameactive) = cur.fetchone()
                    print ' ' + nickname + ' ' * (23-len(nickname)) + ">> " + publicname + ' ' * (21-len(publicname)) + ">> " + active
                print ''
            else:
                print 'No keys in database\n'
        else:
            print 'Not enough parameters. Try looking at ' + argv[0] + ' --help'
    elif argv[1][0:2] == '-h':
        if (argv[1][2] == 'd') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print 'Key not found.'
            else:
                cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "' AND active = '1'")
                if (cur.rowcount == 1):
                    cur.execute("UPDATE oathtokens SET active = '1' WHERE nickname = '" + nickname + "'")
                    print "Key '" + nickname + "' disabled."
                    con.commit()
                else:
                    print 'Key is already disabled.'
 
        elif (argv[1][2] == 'e') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print 'Key not found.'
            else:
                cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "' AND active = '1'")
                if (cur.rowcount == 1):
                    cur.execute("UPDATE oathtokens SET active = '1' WHERE nickname = '" + nickname + "'")
                    print "Key '" + nickname + "' enabled."
                    con.commit()
                else:
                    print 'Key is already enabled.'
        elif (argv[1][2] == 'k') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print 'Key not found.'
            else:
                cur.execute("DELETE FROM oathtokens WHERE nickname = '" + nickname + "'")
                print "Key '" + nickname + "' deleted."
                con.commit()
        elif (argv[1][2] == 'a') and (len(argv)>3):
            nickname = re.escape(argv[2])
            if (len(argv[2])<=16) and (len(argv[3]) <= 16) and (len(argv[4]) <= 40):
                cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + argv[2] + "' OR publicname = '" + argv[3] + "'")
                if (cur.rowcount == 0):
                    cur.execute("INSERT INTO oathtokens VALUES ('" + nickname + "''" + argv[3] + "''" + time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + "''" + argv[4] + "'11)")
                    con.commit()
                    print "Key '" + argv[2] + "' added to database."
                else:
                    print 'Key is already into database. Delete it before adding the same key!'
            else:
                print 'Nickname and publicid must be max 16 characters long.'
                print 'Secret key must be 40 characters max.\n'
                quit()
        elif (argv[1][2] == 'l'):
            cur.execute('SELECT nickname, publicname FROM oathtokens')
            if cur.rowcount != 0:
                print " " + str(cur.rowcount) + " keys into database:"
                print '[Nickname]\t\t>> [PublicID]'
                for i in range(0cur.rowcount):
                    (nicknamepublicname) = cur.fetchone()
                    print ' ' + nickname + ' ' * (23-len(nickname)) + ">> " + publicname
                print ''
            else:
                print 'No keys in database\n'
        else:
            print 'Not enough parameters. Try looking at ' + argv[0] + ' --help'
    elif argv[1][0:2] == '-a':
        if (argv[1][2] == 'a') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM apikeys WHERE nickname = '" + nickname + "'")
            if (cur.rowcount != 0):
                print 'API Key for this nickname is already present. Remove it or choose another one.\n'
                quit()
            cur.execute('SELECT id FROM apikeys ORDER BY id DESC LIMIT 1')
            if (cur.rowcount != 0):
                id = cur.fetchone()[0] + 1
            else:
                id = 1
            api_key = randomChars(20)
            cur.execute("INSERT INTO apikeys VALUES ('" + nickname + "''" + api_key + "''" + str(id) + "')")
            con.commit()
            print "New API Key for '" + nickname + "': '" + api_key.encode('base64').strip() + "'"
            print "Your API Key ID is: " + str(id) + "\n"
        elif (argv[1][2] == 'k') and (len(argv)>2):
            nickname = re.escape(argv[2])
            cur.execute("SELECT * FROM apikeys WHERE nickname = '" + nickname + "'")
            if (cur.rowcount == 0):
                print "API Key for this nickname Doesn't exists!\n"
                quit()
            cur.execute("DELETE FROM apikeys WHERE nickname = '" + nickname + "'")
            con.commit()
            print "API Key for '" + nickname + "' has been deleted.\n"
        elif (argv[1][2] == 'l'):
            cur.execute('SELECT nickname FROM apikeys')
            if cur.rowcount != 0:
                print ' ' + str(cur.rowcount) + ' keys into database:'
                print '[Nickname]'
                for i in range(0cur.rowcount):
                    nickname = cur.fetchone()[0]
                    print ' ' + nickname
                print ''
            else:
                print 'No keys in database\n'