webui.py 4.6 KB

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