Commit b909df74 authored by Christian Robottom's avatar Christian Robottom

Reapply fixes to zeopasswd.py that allow a user to specify the

authentication database details via commandline parameters (in lieu of
the default ZConfig). Also fixed some minor bugs -- missing import,
missing realm, etc.

For no extra charge did some usage cleanups and added a check to
Database to make sure the realm is consistent.
parent 57c663b0
...@@ -59,8 +59,12 @@ class Database: ...@@ -59,8 +59,12 @@ class Database:
""" """
self._users = {} self._users = {}
self.filename = filename self.filename = filename
self.realm = realm
self.load() self.load()
if self.realm and self.realm != realm:
raise ValueError, ("Specified realm %r differs from "
"database realm %r" % (realm or '', self.realm))
else:
self.realm = realm
def save(self, fd=None): def save(self, fd=None):
filename = self.filename filename = self.filename
......
...@@ -16,73 +16,105 @@ ...@@ -16,73 +16,105 @@
usage: python zeopasswd.py [options] username [password] usage: python zeopasswd.py [options] username [password]
-C/--configuration URL -- configuration file or URL Specify either a configuration file:
-d/--delete -- delete user instead of updating password
-C/--configuration -- ZConfig configuration file
or the individual options:
-f/--filename -- authentication database filename
-p/--protocol -- authentication protocol name
-r/--realm -- authentication database realm
Additional options:
-d/--delete -- delete user instead of updating password
""" """
import getopt import getopt
import getpass import getpass
import sys import sys
import os
import ZConfig import ZConfig
import ZEO import ZEO
def usage(msg): def usage(msg):
print msg
print __doc__ print __doc__
print msg
sys.exit(2) sys.exit(2)
def options(args): def options(args):
"""Password-specific options loaded from regular ZEO config file.""" """Password-specific options loaded from regular ZEO config file."""
schema = ZConfig.loadSchema(os.path.join(os.path.dirname(ZEO.__file__),
"schema.xml"))
try: try:
options, args = getopt.getopt(args, "C:", ["configure="]) options, args = getopt.getopt(args, "dr:p:f:C:", ["configure=",
"protocol=",
"filename=",
"realm"])
except getopt.error, msg: except getopt.error, msg:
usage(msg) usage(msg)
config = None config = None
delete = False delete = 0
auth_protocol = None
auth_db = ""
auth_realm = None
for k, v in options: for k, v in options:
if k == '-C' or k == '--configure': if k == '-C' or k == '--configure':
schemafile = os.path.join(os.path.dirname(ZEO.__file__),
"schema.xml")
schema = ZConfig.loadSchema(schemafile)
config, nil = ZConfig.loadConfig(schema, v) config, nil = ZConfig.loadConfig(schema, v)
if k == '-d' or k == '--delete': if k == '-d' or k == '--delete':
delete = True delete = 1
if config is None: if k == '-p' or k == '--protocol':
usage("Must specifiy configuration file") auth_protocol = v
if k == '-f' or k == '--filename':
auth_db = v
if k == '-r' or k == '--realm':
auth_realm = v
if config is not None:
if auth_protocol or auth_db:
usage("Error: Conflicting options; use either -C *or* -p and -f")
auth_protocol = config.zeo.authentication_protocol
auth_db = config.zeo.authentication_database
auth_realm = config.zeo.authentication_realm
elif not (auth_protocol and auth_db):
usage("Error: Must specifiy configuration file or protocol and database")
password = None password = None
if delete: if delete:
if not args: if not args:
usage("Must specify username to delete") usage("Error: Must specify a username to delete")
elif len(args) > 1: elif len(args) > 1:
usage("Too many arguments") usage("Error: Too many arguments")
username = args[0] username = args[0]
else: else:
if not args: if not args:
usage("Must specify username") usage("Error: Must specify a username")
elif len(args) > 2: elif len(args) > 2:
usage("Too many arguments") usage("Error: Too many arguments")
elif len(args) == 1: elif len(args) == 1:
username = args[0] username = args[0]
else: else:
username, password = args username, password = args
return config.zeo, delete, username, password return auth_protocol, auth_db, auth_realm, delete, username, password
def main(args=None): def main(args=None):
options, delete, username, password = options(args) p, auth_db, auth_realm, delete, username, password = options(args)
p = options.authentication_protocol
if p is None: if p is None:
usage("ZEO configuration does not specify authentication-protocol") usage("Error: configuration does not specify auth protocol")
if p == "digest": if p == "digest":
from ZEO.auth.auth_digest import DigestDatabase as Database from ZEO.auth.auth_digest import DigestDatabase as Database
elif p == "srp": elif p == "srp":
from ZEO.auth.auth_srp import SRPDatabase as Database from ZEO.auth.auth_srp import SRPDatabase as Database
if options.authentication_database is None: else:
usage("ZEO configuration does not specify authentication-database") raise ValueError, "Unknown database type %r" % p
db = Database(options.authentication_database) if auth_db is None:
usage("Error: configuration does not specify auth database")
db = Database(auth_db, auth_realm)
if delete: if delete:
db.del_user(username) db.del_user(username)
else: else:
...@@ -92,4 +124,5 @@ def main(args=None): ...@@ -92,4 +124,5 @@ def main(args=None):
db.save() db.save()
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) main(sys.argv[1:])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment