diff options
| author | LV-426 <lv-426@taproot.org.il> | 2015-01-13 01:52:35 +0200 |
|---|---|---|
| committer | LV-426 <lv-426@taproot.org.il> | 2015-01-13 01:52:35 +0200 |
| commit | de29536fa1fc8d57c522637250c294027cb4ebef (patch) | |
| tree | 9a7d948bb3f5c45ff2092b3fff97433a7e97bba2 /src | |
| parent | e6ab11939fa2055224243676ee31332ed48b39d5 (diff) | |
mv init_pw_db to rz_cli_tool, improve
Diffstat (limited to 'src')
| -rw-r--r-- | src/server/crypt_util.py | 16 | ||||
| -rw-r--r-- | src/server/rz_cli_tool.py | 54 | ||||
| -rw-r--r-- | src/server/rz_server.py | 15 |
3 files changed, 59 insertions, 26 deletions
diff --git a/src/server/crypt_util.py b/src/server/crypt_util.py index 464fac94..7eec4c57 100644 --- a/src/server/crypt_util.py +++ b/src/server/crypt_util.py @@ -1,23 +1,17 @@ -import pickle import hashlib, uuid -import os import logging +import pickle -log = logging.getLogger('rhizi') -def add_user_login(config, u, p): - htpasswd_path = config.htpasswd_path +log = logging.getLogger('rhizi') - if False == os.path.exists(htpasswd_path): - with open(htpasswd_path, 'wb') as f: - pickle.dump({}, f) +def add_user_login(htpasswd_path, salt, u, p): with open(htpasswd_path, 'rb') as f: data = f.read() pw_db = pickle.loads(data) with open(htpasswd_path, 'wb') as f: - salt = config.secret_key pw_db[u] = hash_pw(str(p), salt) pickle.dump(pw_db, f) @@ -38,9 +32,9 @@ def validate_login(config, u, p): existing_pw_hash = pw_db.get(u) if None == existing_pw_hash: - raise Exception('Not autorhized') + raise Exception('Not authorized') if hash_pw(p, salt) != existing_pw_hash: - raise Exception('Not autorhized') + raise Exception('Not authorized') diff --git a/src/server/rz_cli_tool.py b/src/server/rz_cli_tool.py new file mode 100644 index 00000000..1dea8559 --- /dev/null +++ b/src/server/rz_cli_tool.py @@ -0,0 +1,54 @@ +import argparse +import os +import pickle +import re + +import crypt_util +from rz_server import init_config + + +def init_pw_db(cfg, user_pw_list_file): + htpasswd_path = cfg.htpasswd_path + + if os.path.exists(htpasswd_path): + print('htpasswd_path already exists, aborting: ' + htpasswd_path) + return + + with open(htpasswd_path, 'wb') as f: # init file + pickle.dump({}, f) + + salt = cfg.secret_key + assert len(salt) > 8, 'server-key not found or too short' + print('using config secret_key for salt generation: ' + salt[:3] + '...') + + u_count = 0 + with open(user_pw_list_file, 'r') as f: + for line in f: + if re.match('(^#)|(\s+$)', line): + continue + + kv_arr = line.split(',') + if 2 != len(kv_arr): + raise Exception('failed to parse user,pw line: ' + line) + + user, pw = map(str.strip, kv_arr) + crypt_util.add_user_login(htpasswd_path, salt, user, pw) + print('added user: u: ' + user) + u_count = u_count + 1 + print('htpasswd generated: path: %s, user-count: %d' % (htpasswd_path, u_count)) + +def main(): + p = argparse.ArgumentParser(description='rz-cli tool') + p.add_argument('--config-dir', help='path to Rhizi config dir', default='res/etc') + p.add_argument('--init-htpasswd-db', help='init login htpasswd db', action='store_const', const=True) + p.add_argument('--htpasswd-init-file', help='htpasswd db initialization file in \'user,pw\' format') + + args = p.parse_args() + cfg = init_config(args.config_dir) + + if args.init_htpasswd_db: + init_pw_db(cfg, args.htpasswd_init_file) + exit(0) + +if __name__ == '__main__': + main() diff --git a/src/server/rz_server.py b/src/server/rz_server.py index e8fec231..118cfa16 100644 --- a/src/server/rz_server.py +++ b/src/server/rz_server.py @@ -248,21 +248,10 @@ def init_config(cfg_dir): cfg = Config.init_from_file(cfg_path) return cfg -def init_pw_db(cfg, user_pw_list_file): - if not os.path.exists(user_pw_list_file): - print("error: missing specified htpasswd init file '%s'" % user_pw_list_file) - raise SystemExit - with open(user_pw_list_file) as user_pwd_list_fd: - for line in user_pwd_list_fd: - u, p = [word.strip() for word in line.split(',')] - crypt_util.add_user_login(cfg, u, p) - if __name__ == "__main__": p = argparse.ArgumentParser(description='rhizi-server') p.add_argument('--config-dir', help='path to Rhizi config dir', default='res/etc') - p.add_argument('--init-htpasswd-db', help='init login htpasswd db', action='store_const', const=True) - p.add_argument('--htpasswd-init-file', help='htpasswd db initialization file in \'user,pw\' format') args = p.parse_args() cfg = init_config(args.config_dir) @@ -273,10 +262,6 @@ if __name__ == "__main__": if False == cfg.access_control: log.warn('access control disabled, all-granted access set on all URLs') - if args.init_htpasswd_db: - init_pw_db(cfg, args.htpasswd_init_file) - exit(0) - kernel = RZ_Kernel() webapp = init_webapp(cfg, kernel) ws_srv = init_ws_interface(cfg, kernel, webapp) |
