From 00da74400cf4cd3006f266784500f1fe9e2c8f4a Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 15 Aug 2024 17:30:01 +0000 Subject: [PATCH] password-changer fixes: * fix `--chpw-no` which did nothing * print list of users with unchanged passwords by default * more granular verbosity levels --- copyparty/__main__.py | 3 +-- copyparty/authsrv.py | 42 ++++++++++++++++++++++++++++-------------- copyparty/svchub.py | 6 ++++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index fd02cffb..373c894c 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -1072,8 +1072,7 @@ def add_chpw(ap): ap2.add_argument("--chpw-no", metavar="U,U,U", type=u, action="append", help="do not allow password-changes for this comma-separated list of usernames") ap2.add_argument("--chpw-db", metavar="PATH", type=u, default=db_path, help="where to store the passwords database (if you run multiple copyparty instances, make sure they use different DBs)") ap2.add_argument("--chpw-len", metavar="N", type=int, default=8, help="minimum password length") - ap2.add_argument("--chpw-v", action="store_true", help="verbose (when loading: list status of each user)") - ap2.add_argument("--chpw-q", action="store_true", help="quiet (when loading: don't print summary)") + ap2.add_argument("--chpw-v", metavar="LVL", type=int, default=2, help="verbosity of summary on config load [\033[32m0\033[0m] = nothing at all, [\033[32m1\033[0m] = number of users, [\033[32m2\033[0m] = list users with default-pw, [\033[32m3\033[0m] = list all users") def add_zeroconf(ap): diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index aa11605f..6c4b50c5 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -2101,11 +2101,18 @@ class AuthSrv(object): if uname == "*" or uname not in self.defpw: return False, "not logged in" + if uname in self.args.chpw_no: + return False, "not allowed for this account" + if len(pw) < self.args.chpw_len: t = "minimum password length: %d characters" return False, t % (self.args.chpw_len,) hpw = self.ah.hash(pw) if self.ah.on else pw + + if hpw == self.acct[uname]: + return False, "that's already your password my dude" + if hpw in self.iacct: return False, "password is taken" @@ -2141,12 +2148,13 @@ class AuthSrv(object): with open(ap, "r", encoding="utf-8") as f: pwdb = json.load(f) - u404 = set() + useen = set() urst = set() uok = set() for usr, orig, mod in pwdb: + useen.add(usr) if usr not in acct: - u404.add(usr) + # previous user, no longer known continue if acct[usr] != orig: urst.add(usr) @@ -2154,33 +2162,39 @@ class AuthSrv(object): uok.add(usr) acct[usr] = mod - if self.args.chpw_q: + if not self.args.chpw_v: return + for usr in acct: + if usr not in useen: + urst.add(usr) + for zs in uok: urst.discard(zs) - if not self.args.chpw_v: - t = "chpw: %d loaded, %d default, %d ignored" - self.log(t % (len(uok), len(urst), len(u404))) + if self.args.chpw_v == 1 or (self.args.chpw_v == 2 and not urst): + t = "chpw: %d changed, %d unchanged" + self.log(t % (len(uok), len(urst))) + return + + elif self.args.chpw_v == 2: + t = "chpw: %d changed" % (len(uok)) + if urst: + t += ", \033[0munchanged:\033[35m %s" % (", ".join(list(urst))) + + self.log(t, 6) return msg = "" if uok: - t = "\033[0mloaded: \033[32m%s" + t = "\033[0mchanged: \033[32m%s" msg += t % (", ".join(list(uok)),) if urst: - t = "%s\033[0mdefault: \033[35m%s" + t = "%s\033[0munchanged: \033[35m%s" msg += t % ( ", " if msg else "", ", ".join(list(urst)), ) - if u404: - t = "%s\033[0mignored: \033[35m%s" - msg += t % ( - ", " if msg else "", - ", ".join(list(u404)), - ) self.log("chpw: " + msg, 6) diff --git a/copyparty/svchub.py b/copyparty/svchub.py index 75e8a014..023dbd12 100644 --- a/copyparty/svchub.py +++ b/copyparty/svchub.py @@ -213,6 +213,12 @@ class SvcHub(object): self.log("root", t, 1) raise Exception(t) + noch = set() + for zs in args.chpw_no or []: + zsl = [x.strip() for x in zs.split(",")] + noch.update([x for x in zsl if x]) + args.chpw_no = noch + bri = "zy"[args.theme % 2 :][:1] ch = "abcdefghijklmnopqrstuvwx"[int(args.theme / 2)] args.theme = "{0}{1} {0} {1}".format(ch, bri)