webui.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. from __future__ import print_function
  21. from bottle import route, run, debug, template, request, get, redirect
  22. import os
  23. import sys
  24. import re
  25. import shutil
  26. from pwman import default_config, which
  27. from pwman import parser_options
  28. from pwman.ui import get_ui_platform
  29. from pwman.ui.tools import CLICallback
  30. from pwman.util.crypto import CryptoEngine
  31. import pwman.util.config as config
  32. import pwman.data.factory
  33. from pwman.data.tags import TagNew
  34. AUTHENTICATED = False
  35. TAGS = None
  36. DB = None
  37. tmplt = """
  38. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  39. <form action="/" method="POST">
  40. <select multiple name="tag" onchange="this.form.submit()">
  41. %for tag in tags:
  42. <option value="{{tag}}">{{tag}}</option>
  43. %end
  44. </select>
  45. </form>
  46. <p>Click on username to view the details:</p>
  47. <table border="1">
  48. %for node in nodes:
  49. <tr>
  50. %#for item in node:
  51. %# <td><a href={{node._id}}><{{item}}</a></td>
  52. <td><a href=/node/{{node._id}}>{{node.username}}@{{node.url}}</a></td>
  53. <td>{{ ', '.join([t.strip() for t in filter(None, node.tags)]) }}</td>
  54. <td>edit</td>
  55. %end
  56. </tr>
  57. %end
  58. </table>
  59. """
  60. edit_node_tmplt = """
  61. <form action="/edit/" method="POST">
  62. Username: <input type="text" name="username"><br>
  63. Password: <input type="password" name="lastname"><br>
  64. Repeat Password: <input type="password" name="lastname"><br>
  65. Notes: <input type="text" name="notes"><br>
  66. Tags: <input type="text" name="tags"><br>
  67. <input type="submit" value="Save edits">
  68. </form>
  69. """
  70. login = """
  71. <p>Please enter your database password: <b>
  72. <form action="/auth" method="POST">
  73. Password: <input type="password" name="pwd">
  74. </form>"""
  75. def get_conf(args):
  76. config_dir = os.path.expanduser("~/.pwman")
  77. if not os.path.isdir(config_dir):
  78. os.mkdir(config_dir)
  79. if not os.path.exists(args.cfile):
  80. config.set_defaults(default_config)
  81. else:
  82. config.load(args.cfile)
  83. return config
  84. def set_xsel(config, OSX):
  85. if not OSX:
  86. xselpath = which("xsel")
  87. config.set_value("Global", "xsel", xselpath)
  88. elif OSX:
  89. pbcopypath = which("pbcopy")
  90. config.set_value("Global", "xsel", pbcopypath)
  91. def set_win_colors(config):
  92. if 'win' in sys.platform:
  93. try:
  94. import colorama
  95. colorama.init()
  96. except ImportError:
  97. config.set_value("Global", "colors", 'no')
  98. def set_umask(config):
  99. # set umask before creating/opening any files
  100. try:
  101. umask = config.get_value("Global", "umask")
  102. if re.search(r'^\d{4}$', umask):
  103. os.umask(int(umask))
  104. else:
  105. raise ValueError
  106. except ValueError:
  107. print("Could not determine umask from config!")
  108. sys.exit(2)
  109. def set_db(args):
  110. if args.dbase:
  111. config.set_value("Database", "filename", args.dbase)
  112. config.set_value("Global", "save", "False")
  113. def set_algorithm(args, config):
  114. if args.algo:
  115. config.set_value("Encryption", "algorithm", args.algo)
  116. config.set_value("Global", "save", "False")
  117. def get_conf_options(args, OSX):
  118. config = get_conf(args)
  119. xselpath = config.get_value("Global", "xsel")
  120. if not xselpath:
  121. set_xsel(config, OSX)
  122. set_win_colors(config)
  123. set_db(args)
  124. set_umask(config)
  125. set_algorithm(args, config)
  126. dbtype = config.get_value("Database", "type")
  127. if not dbtype:
  128. print("Could not read the Database type from the config!")
  129. sys.exit(1)
  130. return xselpath, dbtype
  131. @route('/node/:no')
  132. def view_node(no):
  133. global DB
  134. node = DB.getnodes([no])
  135. tmplt = """
  136. <table border="1">
  137. <tr><td>Username:</td> <td>{{ node.username }}</td></tr>
  138. <tr><td>Password:</td> <td>{{ node.password }}</td></tr>
  139. <tr><td>Url:</td> <td>{{node.url}} </td></tr>
  140. <tr><td>Notes:</td> <td>{{node.notes}}</td></tr>
  141. <tr><td>Tags:</td> <td>{{node.tags}}</td></tr>
  142. </table>
  143. """
  144. output = template(tmplt, node=node[0])
  145. return output
  146. @route('/new', method=['GET', 'POST'])
  147. def new():
  148. pass
  149. @route('/edit/:no', method=['GET', 'POST'])
  150. def edit_node(no):
  151. output = template(edit_node_tmplt,)
  152. return output
  153. @route('/auth', method=['GET', 'POST'])
  154. def is_authenticated():
  155. global AUTHENTICATED
  156. crypto = CryptoEngine.get()
  157. if request.method == 'POST':
  158. key = request.POST.get('pwd', '')
  159. crypto.auth(key)
  160. AUTHENTICATED = True
  161. redirect('/')
  162. else:
  163. return login
  164. @route('/', method=['GET', 'POST'])
  165. def listnodes():
  166. global AUTHENTICATED, TAGS, DB
  167. _filter = None
  168. OSX = False
  169. args = parser_options().parse_args()
  170. xselpath, dbtype = get_conf_options(args, OSX)
  171. dbver = 0.4
  172. DB = pwman.data.factory.create(dbtype, dbver)
  173. DB.open()
  174. crypto = CryptoEngine.get()
  175. if not AUTHENTICATED:
  176. redirect('/auth')
  177. if 'POST' in request.method:
  178. _filter = request.POST.get('tag')
  179. if _filter:
  180. DB._filtertags = [TagNew(_filter.strip())]
  181. if _filter == 'None':
  182. DB._filtertags = []
  183. nodeids = DB.listnodes()
  184. nodes = DB.getnodes(nodeids)
  185. nodesd = [''] * len(nodes)
  186. for idx, node in enumerate(nodes):
  187. ntags = [t.strip() for t in filter(None, node.tags)]
  188. nodesd[idx] = ('@'.join((node.username, node.url)),
  189. ', '.join(ntags))
  190. if not TAGS:
  191. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  192. TAGS.sort()
  193. TAGS.insert(0, 'None')
  194. print(len(TAGS))
  195. output = template(tmplt, nodes=nodes, tags=TAGS)
  196. return output
  197. debug(True)
  198. run(reloader=True)