webui.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. tmplt = """
  36. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  37. <form action="/" method="POST">
  38. <select name="tag" onchange="this.form.submit()">
  39. %for node in nodes:
  40. <option value="{{node[1]}}">{{node[1]}}</option>
  41. %end
  42. </select>
  43. </form>
  44. <p>The open items are as follows:</p>
  45. <table border="1">
  46. %for node in nodes:
  47. <tr>
  48. %for item in node:
  49. <td>{{item}}</td>
  50. %end
  51. </tr>
  52. <tr><td></td><td>edit</td></tr>
  53. %end
  54. </table>
  55. """
  56. login = """
  57. <p>Please enter your database password: <b>
  58. <form action="/auth" method="POST">
  59. Password: <input type="password" name="pwd">
  60. </form>"""
  61. def get_conf(args):
  62. config_dir = os.path.expanduser("~/.pwman")
  63. if not os.path.isdir(config_dir):
  64. os.mkdir(config_dir)
  65. if not os.path.exists(args.cfile):
  66. config.set_defaults(default_config)
  67. else:
  68. config.load(args.cfile)
  69. return config
  70. def set_xsel(config, OSX):
  71. if not OSX:
  72. xselpath = which("xsel")
  73. config.set_value("Global", "xsel", xselpath)
  74. elif OSX:
  75. pbcopypath = which("pbcopy")
  76. config.set_value("Global", "xsel", pbcopypath)
  77. def set_win_colors(config):
  78. if 'win' in sys.platform:
  79. try:
  80. import colorama
  81. colorama.init()
  82. except ImportError:
  83. config.set_value("Global", "colors", 'no')
  84. def set_umask(config):
  85. # set umask before creating/opening any files
  86. try:
  87. umask = config.get_value("Global", "umask")
  88. if re.search(r'^\d{4}$', umask):
  89. os.umask(int(umask))
  90. else:
  91. raise ValueError
  92. except ValueError:
  93. print("Could not determine umask from config!")
  94. sys.exit(2)
  95. def set_db(args):
  96. if args.dbase:
  97. config.set_value("Database", "filename", args.dbase)
  98. config.set_value("Global", "save", "False")
  99. def set_algorithm(args, config):
  100. if args.algo:
  101. config.set_value("Encryption", "algorithm", args.algo)
  102. config.set_value("Global", "save", "False")
  103. def get_conf_options(args, OSX):
  104. config = get_conf(args)
  105. xselpath = config.get_value("Global", "xsel")
  106. if not xselpath:
  107. set_xsel(config, OSX)
  108. set_win_colors(config)
  109. set_db(args)
  110. set_umask(config)
  111. set_algorithm(args, config)
  112. dbtype = config.get_value("Database", "type")
  113. if not dbtype:
  114. print("Could not read the Database type from the config!")
  115. sys.exit(1)
  116. return xselpath, dbtype
  117. @route('/auth', method=['GET', 'POST'])
  118. def is_authenticated():
  119. global AUTHENTICATED
  120. crypto = CryptoEngine.get()
  121. if request.method == 'POST':
  122. key = request.POST.get('pwd', '')
  123. crypto.auth(key)
  124. AUTHENTICATED = True
  125. redirect('/')
  126. else:
  127. return login
  128. @route('/', method=['GET', 'POST'])
  129. def listnodes():
  130. global AUTHENTICATED
  131. _filter = None
  132. OSX = False
  133. args = parser_options().parse_args()
  134. xselpath, dbtype = get_conf_options(args, OSX)
  135. dbver = 0.4
  136. db = pwman.data.factory.create(dbtype, dbver)
  137. db.open()
  138. crypto = CryptoEngine.get()
  139. if not AUTHENTICATED:
  140. redirect('/auth')
  141. if 'POST' in request.method:
  142. _filter = request.POST.get('tag')
  143. if _filter:
  144. db._filtertags = [TagNew(_filter.strip())]
  145. nodeids = db.listnodes()
  146. nodes = db.getnodes(nodeids)
  147. nodesd = [''] * len(nodes)
  148. for idx, node in enumerate(nodes):
  149. tags = node.tags
  150. tags = filter(None, tags)
  151. nodesd[idx] = ('@'.join((node.username, node.url)), ','.join(tags))
  152. output = template(tmplt, nodes=nodesd)
  153. return output
  154. debug(True)
  155. run(reloader=True)