webui.py 4.6 KB

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