webui.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, redirect, static_file
  22. from pwman.util.crypto import CryptoEngine
  23. import pwman.data.factory
  24. from pwman.data.tags import TagNew
  25. from pwman import parser_options, get_conf_options
  26. from daemon import Daemon
  27. from pkg_resources import resource_filename
  28. import itertools
  29. import argparse
  30. import sys
  31. from os.path import expanduser, join
  32. templates_path = [resource_filename('pwman', 'ui/templates')]
  33. statics = [resource_filename('pwman', 'ui/templates/static')][0]
  34. AUTHENTICATED = False
  35. TAGS = None
  36. DB = None
  37. # BUG: Error: SQLite: Incorrect number of bindings supplied.
  38. # The current statement uses 2, and there are 1 supplied.
  39. # When issuing multiple times filter
  40. # WEB GUI shows multiple tags as one tag!
  41. def require_auth(fn):
  42. def check_auth(**kwargs):
  43. if AUTHENTICATED:
  44. return fn(**kwargs)
  45. else:
  46. redirect("/auth")
  47. return check_auth
  48. @route('/node/:no')
  49. @require_auth
  50. def view_node(no):
  51. global DB
  52. node = DB.getnodes([no])
  53. output = template("view.tpl", node=node[0], template_lookup=templates_path)
  54. return output
  55. def submit_node(id, request):
  56. # create new\update node based on request.params.items()
  57. redirect('/')
  58. @route('/new/', method=['GET', 'POST'])
  59. @route('/edit/:no', method=['GET', 'POST'])
  60. @require_auth
  61. def edit_node(no=None):
  62. global DB
  63. if 'POST' in request.method:
  64. submit_node(no, request)
  65. if no:
  66. node = DB.getnodes([no])[0]
  67. else:
  68. class Node(object):
  69. def __init__(self):
  70. self._id = None
  71. self.username = ''
  72. self.password = ''
  73. self.url = ''
  74. self.notes = ''
  75. self.tags = ''
  76. node = Node()
  77. output = template('edit.tpl', node=node,
  78. template_lookup=templates_path)
  79. return output
  80. @route('/forget', method=['GET', 'POST'])
  81. def forget():
  82. global AUTHENTICATED
  83. AUTHENTICATED = False
  84. enc = CryptoEngine.get()
  85. enc.forget()
  86. redirect('/auth')
  87. @route('/auth', method=['GET', 'POST'])
  88. def is_authenticated():
  89. global AUTHENTICATED
  90. crypto = CryptoEngine.get(dbver=0.5)
  91. if request.method == 'POST':
  92. key = request.POST.get('pwd', '')
  93. crypto.auth(key)
  94. AUTHENTICATED = True
  95. redirect('/')
  96. else:
  97. return template("login.tpl", template_lookup=templates_path)
  98. @route('/', method=['GET', 'POST'])
  99. @require_auth
  100. def listnodes(apply=['require_login']):
  101. global AUTHENTICATED, TAGS, DB
  102. _filter = None
  103. if 'POST' in request.method:
  104. _filter = request.POST.get('tag')
  105. if _filter:
  106. DB._filtertags = [TagNew(_filter.strip())]
  107. if _filter == 'None':
  108. DB._filtertags = []
  109. nodeids = DB.listnodes()
  110. nodes = DB.getnodes(nodeids)
  111. nodesd = [''] * len(nodes)
  112. for idx, node in enumerate(nodes):
  113. ntags = [t.strip() for t in filter(None, node.tags)]
  114. nodesd[idx] = ('@'.join((node.username, node.url)),
  115. ', '.join(ntags))
  116. if not TAGS:
  117. t = [node.tags for node in nodes]
  118. t1 = list(itertools.chain.from_iterable(t))
  119. TAGS = list(set(t1))
  120. TAGS.sort()
  121. TAGS.insert(0, 'None')
  122. html_nodes = template("main.tpl", nodes=nodes, tags=TAGS,
  123. template_lookup=[resource_filename('pwman',
  124. 'ui/templates')])
  125. return html_nodes
  126. @route('/static/<filepath:path>')
  127. def server_static(filepath):
  128. return static_file(filepath, root=statics)
  129. class Pwman3WebDaemon(Daemon):
  130. def startd(self):
  131. """
  132. Start the daemon
  133. """
  134. # Check for a pidfile to see if the daemon already runs
  135. self.exit_running()
  136. #if not os.path.exists(self.pidfile):
  137. # Start the daemon
  138. self.daemonize()
  139. # after self.daemonize()
  140. # all output is redirected
  141. print(open(self.pidfile).read())
  142. self.run()
  143. def run(self):
  144. global AUTHENTICATED, TAGS, DB
  145. OSX = False
  146. sys.argv = []
  147. args = parser_options().parse_args()
  148. xselpath, dbtype = get_conf_options(args, OSX)
  149. dbver = 0.5
  150. DB = pwman.data.factory.create(dbtype, dbver)
  151. DB.open(dbver=0.5)
  152. print(dir(DB))
  153. CryptoEngine.get(dbver=0.5)
  154. print(pwman.config._conf)
  155. debug(True)
  156. run(port=9030)
  157. if __name__ == '__main__':
  158. parser = argparse.ArgumentParser(
  159. description="Start the webui of pwman3",
  160. usage='%(prog)s start|stop|restart|status [-p|-d]\n',
  161. )
  162. parser.add_argument('-D', '--NoDaemon',
  163. help='Do not fork, start in foreground',
  164. action="store_true", default=False)
  165. ext_usage = ("{prog} start - starts the webui."
  166. "{prog} stop - stops the webui."
  167. "{prog} status - check if the process is already running."
  168. "".format(prog=parser.prog))
  169. if len(sys.argv) == 1:
  170. parser.print_help()
  171. sys.exit(1)
  172. action = sys.argv[1]
  173. if action not in ['start', 'stop', 'restart', 'status']:
  174. parser.print_help()
  175. print(ext_usage)
  176. sys.exit(1)
  177. sys.argv = sys.argv[1:]
  178. path = join(expanduser("~"), '.pwman')
  179. daemon = Pwman3WebDaemon(parser.prog, join(path, '%s.pid' % parser.prog),
  180. noisy=True)
  181. args = parser.parse_args()
  182. if action == 'status':
  183. running = daemon.check_proc()
  184. if running:
  185. print("%s already runs with pid: %d" % (parser.prog, running))
  186. if action == 'start' and args.NoDaemon:
  187. try:
  188. daemon.start()
  189. except KeyboardInterrupt:
  190. daemon.set_level("auto")
  191. daemon.stop()
  192. if action == 'start' and not args.NoDaemon:
  193. daemon.startd()
  194. if action == "restart":
  195. daemon.restart()
  196. if action == "stop":
  197. daemon.stop()