patch-dbconf_py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. $OpenBSD: patch-dbconf_py,v 1.1.1.1 2012/07/18 08:25:07 sthen Exp $
  2. sqlite3 support from http://code.google.com/p/yubico-yubiserve/source/list r39
  3. --- dbconf.py.orig Wed Jul 18 01:16:24 2012
  4. +++ dbconf.py Wed Jul 18 01:04:51 2012
  5. @@ -1,4 +1,4 @@
  6. -#!/usr/bin/python
  7. +#!${MODPY_BIN}
  8. import time, random, re, os
  9. from sys import argv
  10. try:
  11. @@ -6,12 +6,16 @@ try:
  12. except ImportError:
  13. pass
  14. try:
  15. + import sqlite3
  16. +except ImportError:
  17. + pass
  18. +try:
  19. import sqlite
  20. except ImportError:
  21. pass
  22. def parseConfigFile(): # Originally I wrote this function to parse PHP configuration files!
  23. - config = open(os.path.dirname(os.path.realpath(__file__)) + '/yubiserve.cfg', 'r').read().splitlines()
  24. + config = open('${SYSCONFDIR}/yubiserve/yubiserve.cfg', 'r').read().splitlines()
  25. keys = {}
  26. for line in config:
  27. match = re.search('(.*?)=(.*);', line)
  28. @@ -54,14 +58,15 @@ if config['yubiDB'] == 'mysql' and (config['yubiMySQLH
  29. print "Cannot continue without any MySQL configuration.\nPlease read README.\n\n"
  30. quit()
  31. try:
  32. - if config['yubiDB'] == 'sqlite':
  33. - con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
  34. + if config['yubiDB'] == 'sqlite3':
  35. + con = sqlite3.connect('/var/db/yubiserve/yubikeys.sqlite3')
  36. + elif config['yubiDB'] == 'sqlite':
  37. + con = sqlite.connect('/var/db/yubiserve/yubikeys.sqlite')
  38. elif config['yubiDB'] == 'mysql':
  39. con = MySQLdb.connect(host=config['yubiMySQLHost'], user=config['yubiMySQLUser'], passwd=config['yubiMySQLPass'], db=config['yubiMySQLName'])
  40. except:
  41. print "There's a problem with the database!\n"
  42. cur = con.cursor()
  43. -
  44. if (len(argv)<2):
  45. print ' == YubiServe Key Management Tool 2.0 ==\n'
  46. print ' -ya <nickname> <publicid> <secretid> <aeskey>\tAdd a new Yubikey'
  47. @@ -84,13 +89,15 @@ else:
  48. if argv[1][0:2] == '-y': # Yubico Yubikey
  49. if (argv[1][2] == 'd') and (len(argv)>2):
  50. nickname = re.escape(argv[2])
  51. - cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "'")
  52. - if (cur.rowcount == 0):
  53. + cur.execute("SELECT count(nickname) FROM yubikeys WHERE nickname = '" + nickname + "'")
  54. + rowcount = cur.fetchone();
  55. + if not rowcount[0]:
  56. print 'Key not found.'
  57. else:
  58. - cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "' AND active = '1'")
  59. - if (cur.rowcount == 1):
  60. - cur.execute("UPDATE yubikeys SET active = '1' WHERE nickname = '" + nickname + "'")
  61. + cur.execute("SELECT count(nickname) FROM yubikeys WHERE nickname = '" + nickname + "' AND active = '1'")
  62. + rowcount = cur.fetchone();
  63. + if rowcount[0]:
  64. + cur.execute("UPDATE yubikeys SET active = '0' WHERE nickname = '" + nickname + "'")
  65. print "Key '" + nickname + "' disabled."
  66. con.commit()
  67. else:
  68. @@ -98,12 +105,14 @@ else:
  69. elif (argv[1][2] == 'e') and (len(argv)>2):
  70. nickname = re.escape(argv[2])
  71. - cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "'")
  72. - if (cur.rowcount == 0):
  73. + cur.execute("SELECT count(nickname) FROM yubikeys WHERE nickname = '" + nickname + "'")
  74. + rowcount = cur.fetchone();
  75. + if not rowcount[0]:
  76. print 'Key not found.'
  77. else:
  78. - cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "' AND active = '1'")
  79. - if (cur.rowcount == 1):
  80. + cur.execute("SELECT count(nickname) FROM yubikeys WHERE nickname = '" + nickname + "' AND active = '0'")
  81. + rowcount = cur.fetchone();
  82. + if rowcount[0]:
  83. cur.execute("UPDATE yubikeys SET active = '1' WHERE nickname = '" + nickname + "'")
  84. print "Key '" + nickname + "' enabled."
  85. con.commit()
  86. @@ -111,8 +120,9 @@ else:
  87. print 'Key is already enabled.'
  88. elif (argv[1][2] == 'k') and (len(argv)>2):
  89. nickname = re.escape(argv[2])
  90. - cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + nickname + "'")
  91. - if (cur.rowcount == 0):
  92. + cur.execute("SELECT count(nickname) FROM yubikeys WHERE nickname = '" + nickname + "'")
  93. + rowcount = cur.fetchone();
  94. + if not rowcount[0]:
  95. print 'Key not found.'
  96. else:
  97. cur.execute("DELETE FROM yubikeys WHERE nickname = '" + nickname + "'")
  98. @@ -121,8 +131,9 @@ else:
  99. elif (argv[1][2] == 'a') and (len(argv)>4):
  100. nickname = re.escape(argv[2])
  101. if ((len(argv[2])<=16) and (len(argv[3]) <= 16) and (len(argv[4]) <= 12) and (len(argv[5])<=32)):
  102. - cur.execute("SELECT * FROM yubikeys WHERE nickname = '" + argv[2] + "' OR publicname = '" + argv[3] + "'")
  103. - if (cur.rowcount == 0):
  104. + cur.execute("SELECT count(nickname) FROM yubikeys WHERE nickname = '" + argv[2] + "' OR publicname = '" + argv[3] + "'")
  105. + rowcount = cur.fetchone();
  106. + if not rowcount[0]:
  107. cur.execute("INSERT INTO yubikeys VALUES ('" + argv[2] + "', '" + argv[3] + "', '" + time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + "', '" + argv[4] + "', '" + argv[5] + "', 1, 1, 1)")
  108. con.commit()
  109. print "Key '" + argv[2] + "' added to database."
  110. @@ -133,13 +144,14 @@ else:
  111. print 'Secretid must be 12 characters max, aeskey must be 32 characters max.\n'
  112. quit()
  113. elif (argv[1][2] == 'l'):
  114. - cur.execute('SELECT nickname, publicname, active FROM yubikeys')
  115. - if cur.rowcount != 0:
  116. - print " " + str(cur.rowcount) + " keys into database:"
  117. + cur.execute('SELECT count(nickname) FROM yubikeys')
  118. + rowcount = cur.fetchone();
  119. + print " %s keys into database:" % (rowcount[0])
  120. + if rowcount[0]:
  121. + cur.execute('SELECT nickname, publicname, active FROM yubikeys')
  122. print '[Nickname]\t\t>> [PublicID]'
  123. - for i in range(0, cur.rowcount):
  124. - (nickname, publicname, active) = cur.fetchone()
  125. - print ' ' + nickname + ' ' * (23-len(nickname)) + ">> " + publicname + ' ' * (21-len(publicname)) + ">> " + active
  126. + for (nickname, publicname, active) in cur:
  127. + print '%-23s >> %-21s >> %s ' % (nickname, publicname, active)
  128. print ''
  129. else:
  130. print 'No keys in database\n'
  131. @@ -148,12 +160,14 @@ else:
  132. elif argv[1][0:2] == '-h':
  133. if (argv[1][2] == 'd') and (len(argv)>2):
  134. nickname = re.escape(argv[2])
  135. - cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "'")
  136. - if (cur.rowcount == 0):
  137. + cur.execute("SELECT count(nickname) FROM oathtokens WHERE nickname = '" + nickname + "'")
  138. + rowcount = cur.fetchone();
  139. + if not rowcount[0]:
  140. print 'Key not found.'
  141. else:
  142. - cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "' AND active = '1'")
  143. - if (cur.rowcount == 1):
  144. + cur.execute("SELECT count(nickname) FROM oathtokens WHERE nickname = '" + nickname + "' AND active = '1'")
  145. + rowcount = cur.fetchone();
  146. + if rowcount[0]:
  147. cur.execute("UPDATE oathtokens SET active = '1' WHERE nickname = '" + nickname + "'")
  148. print "Key '" + nickname + "' disabled."
  149. con.commit()
  150. @@ -162,12 +176,14 @@ else:
  151. elif (argv[1][2] == 'e') and (len(argv)>2):
  152. nickname = re.escape(argv[2])
  153. - cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "'")
  154. - if (cur.rowcount == 0):
  155. + cur.execute("SELECT count(nickname) FROM oathtokens WHERE nickname = '" + nickname + "'")
  156. + rowcount = cur.fetchone();
  157. + if not rowcount[0]:
  158. print 'Key not found.'
  159. else:
  160. - cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "' AND active = '1'")
  161. - if (cur.rowcount == 1):
  162. + cur.execute("SELECT count(nickname) FROM oathtokens WHERE nickname = '" + nickname + "' AND active = '1'")
  163. + rowcount = cur.fetchone();
  164. + if rowcount[0]:
  165. cur.execute("UPDATE oathtokens SET active = '1' WHERE nickname = '" + nickname + "'")
  166. print "Key '" + nickname + "' enabled."
  167. con.commit()
  168. @@ -175,8 +191,9 @@ else:
  169. print 'Key is already enabled.'
  170. elif (argv[1][2] == 'k') and (len(argv)>2):
  171. nickname = re.escape(argv[2])
  172. - cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + nickname + "'")
  173. - if (cur.rowcount == 0):
  174. + cur.execute("SELECT count(nickname) FROM oathtokens WHERE nickname = '" + nickname + "'")
  175. + rowcount = cur.fetchone();
  176. + if not rowcount[0]:
  177. print 'Key not found.'
  178. else:
  179. cur.execute("DELETE FROM oathtokens WHERE nickname = '" + nickname + "'")
  180. @@ -185,8 +202,9 @@ else:
  181. elif (argv[1][2] == 'a') and (len(argv)>3):
  182. nickname = re.escape(argv[2])
  183. if (len(argv[2])<=16) and (len(argv[3]) <= 16) and (len(argv[4]) <= 40):
  184. - cur.execute("SELECT * FROM oathtokens WHERE nickname = '" + argv[2] + "' OR publicname = '" + argv[3] + "'")
  185. - if (cur.rowcount == 0):
  186. + cur.execute("SELECT count(nickname) FROM oathtokens WHERE nickname = '" + argv[2] + "' OR publicname = '" + argv[3] + "'")
  187. + rowcount = cur.fetchone();
  188. + if not rowcount[0]:
  189. cur.execute("INSERT INTO oathtokens VALUES ('" + nickname + "', '" + argv[3] + "', '" + time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + "', '" + argv[4] + "', 1, 1)")
  190. con.commit()
  191. print "Key '" + argv[2] + "' added to database."
  192. @@ -197,14 +215,14 @@ else:
  193. print 'Secret key must be 40 characters max.\n'
  194. quit()
  195. elif (argv[1][2] == 'l'):
  196. - cur.execute('SELECT nickname, publicname FROM oathtokens')
  197. - if cur.rowcount != 0:
  198. - print " " + str(cur.rowcount) + " keys into database:"
  199. + cur.execute('SELECT count(nickname) FROM oathtokens')
  200. + rowcount = cur.fetchone();
  201. + print " %s keys into database:" % (rowcount[0])
  202. + if rowcount[0]:
  203. + cur.execute('SELECT nickname, publicname FROM oathtokens')
  204. print '[Nickname]\t\t>> [PublicID]'
  205. - for i in range(0, cur.rowcount):
  206. - (nickname, publicname) = cur.fetchone()
  207. - print ' ' + nickname + ' ' * (23-len(nickname)) + ">> " + publicname
  208. - print ''
  209. + for (nickname, publicname) in cur:
  210. + print '%-23s >> %-21s >> %s ' % (nickname, publicname)
  211. else:
  212. print 'No keys in database\n'
  213. else:
  214. @@ -212,13 +230,15 @@ else:
  215. elif argv[1][0:2] == '-a':
  216. if (argv[1][2] == 'a') and (len(argv)>2):
  217. nickname = re.escape(argv[2])
  218. - cur.execute("SELECT * FROM apikeys WHERE nickname = '" + nickname + "'")
  219. - if (cur.rowcount != 0):
  220. + cur.execute("SELECT count(nickname) FROM apikeys WHERE nickname = '" + nickname + "'")
  221. + rowcount = cur.fetchone();
  222. + if rowcount[0]:
  223. print 'API Key for this nickname is already present. Remove it or choose another one.\n'
  224. quit()
  225. cur.execute('SELECT id FROM apikeys ORDER BY id DESC LIMIT 1')
  226. - if (cur.rowcount != 0):
  227. - id = cur.fetchone()[0] + 1
  228. + lastid = cur.fetchone()
  229. + if lastid:
  230. + id = lastid[0] + 1
  231. else:
  232. id = 1
  233. api_key = randomChars(20)
  234. @@ -228,22 +248,23 @@ else:
  235. print "Your API Key ID is: " + str(id) + "\n"
  236. elif (argv[1][2] == 'k') and (len(argv)>2):
  237. nickname = re.escape(argv[2])
  238. - cur.execute("SELECT * FROM apikeys WHERE nickname = '" + nickname + "'")
  239. - if (cur.rowcount == 0):
  240. + cur.execute("SELECT count(nickname) FROM apikeys WHERE nickname = '" + nickname + "'")
  241. + rowcount = cur.fetchone();
  242. + if not rowcount[0]:
  243. print "API Key for this nickname Doesn't exists!\n"
  244. quit()
  245. cur.execute("DELETE FROM apikeys WHERE nickname = '" + nickname + "'")
  246. con.commit()
  247. print "API Key for '" + nickname + "' has been deleted.\n"
  248. elif (argv[1][2] == 'l'):
  249. - cur.execute('SELECT nickname FROM apikeys')
  250. - if cur.rowcount != 0:
  251. - print ' ' + str(cur.rowcount) + ' keys into database:'
  252. + cur.execute('SELECT count(nickname) FROM apikeys')
  253. + rowcount = cur.fetchone();
  254. + print " %s keys into database:" % (rowcount[0])
  255. + if rowcount[0]:
  256. + cur.execute('SELECT nickname FROM apikeys')
  257. print '[Nickname]'
  258. - for i in range(0, cur.rowcount):
  259. - nickname = cur.fetchone()[0]
  260. - print ' ' + nickname
  261. - print ''
  262. + for (nickname) in cur:
  263. + print '%-23s' % (nickname)
  264. else:
  265. print 'No keys in database\n'
  266. -
  267. \ No newline at end of file
  268. +