Re: Change moderator or admin password (random) different for each list they were an admin or moderator for - Mailman 2.1

On 08/03/2021 17:17, Daniel Botting wrote:
Hi,
First of all thank you to both on this thread, appreciated and the applause :)
I have updated the function as below:
I run -
withlist --all --run list_mod.change_moderator_password2 first.lastname@domain.co.uk
Function:
def change_moderator_password2(mlist, moderator): mlist.Lock() try: if mlist.moderator[mlist.moderator.index(moderator)]: '
newpassword = subprocess.check_output( ["pwgen", "-sB", "15", "1"]).strip()
except: print("password not changed for", (mlist.real_name, moderator)) mlist.Save()
From what I can see this is not working as expected, the moderator address I'm trying to change the password is on the list named 'Test4-list1'.
root@lists-test4:~/scripts# withlist --all --run list_mod.change_moderator_password2 firstname.lastname@domain.co.uk Importing list_mod... Running list_mod.change_moderator_password2()... Loading list test4-list1 (unlocked) ('password not changed for', ('Test4-list1', 'first.lastname@domain.co.uk')) Loading list test4-list3 (unlocked) ('password not changed for', ('Test4-list3', 'first.lastname@domain.co.uk')) Loading list mailman (unlocked) ('password not changed for', ('Mailman', 'first.lastname@domain.co.uk')) Loading list test4-list2 (unlocked) ('password not changed for', ('Test4-list2', 'first.lastname@domain.co.uk')) Unlocking (but not saving) list: test4-list2 Finalizing
Apologies, you'll have to bear with me, I'm slowly learning Python.
Many thanks
Daniel
On 05/03/2021 17:14, Mark Sapiro wrote:
On 3/4/21 10:35 PM, Stephen J. Turnbull wrote:
Hi, Daniel
Mark Sapiro writes: > > You need to > generate a password within the script.
I think
newpassword = subprocess.run(["pwgen", "-sB", "15", "1"], capture_output=True, text=True).stdout
will do the trick (be careful about text=True, though; you may want the default text=False so that stdout will be bytes instead of str). Except that's Python 3 and this is Mailman 2.1 withlist so the script needs to be Python 2. For that we would want
newpassword = subprocess.check_output( ["pwgen", "-sB", "15", "1"]).strip()
The .strip() is to remove a trailing newline.
https://docs.python.org/2.7/library/subprocess.html#subprocess.check_output
-- Daniel Botting Systems Administrator Codethink Ltd. 3rd Floor Dale House, 35 Dale Street, Manchester, M1 2HF United Kingdom
http://www.codethink.co.uk/ We respect your privacy. See https://www.codethink.co.uk/privacy.html

On 3/8/21 9:20 AM, Daniel Botting wrote:
I have updated the function as below:
I run -
withlist --all --run list_mod.change_moderator_password2 first.lastname@domain.co.uk
Function:
def change_moderator_password2(mlist, moderator): mlist.Lock() try: if mlist.moderator[mlist.moderator.index(moderator)]: '
newpassword = subprocess.check_output( ["pwgen", "-sB", "15", "1"]).strip()
except: print("password not changed for", (mlist.real_name, moderator)) mlist.Save()
You need to unlock the list, and you should probably only save it if you succeed. You are also missing a ')' (my fault) and a lot of other stuff.
You want something like this for the script
import subprocess from Mailman.Utils import sha_new
def change_moderator_password2(mlist, moderator): if not mlist.Locked(): mlist.Lock() try: if moderator in mlist.moderator: newpassword = subprocess.check_output( ["pwgen", "-sB", "15", "1"]).strip()) mlist.mod_password = sha_new(newpassword).hexdigest() print('%s moderator password changed to %s' % (mlist.real_name, newpassword)) mlist.Save() else: print('%s not found in %s moderator' % (moderator, mlist.real_name))
except subprocess.CalledProcessError as e:
print("password not changed for %s: %s\n%s" %
(mlist.real_name, moderator, e.output))
finally:
mlist.Unlock()
Apologies, you'll have to bear with me, I'm slowly learning Python.
And the appropriate list for that is tutor@python.org <https://mail.python.org/mailman/listinfo/tutor>
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan

Hi Mark,
Thank you, much appreciated.
I've got it working and progressed a little further ;)
["pwgen", "-sB", "15", "1"]).strip()) I had to remove the very end closing bracket, it was causing a syntax error.
I then encountered a Errno 2 error no such file, which I tracked down to the fact this was a new test server due to mail not being received to lists and I didn't have pwgen installed.
I then managed to get it to email me either directly for each occurence of the password being changed if I just put in my email address directly or if one moderator is set, the issue comes that if I have more than one moderator it's mangling the message:
print('%s moderator password changed to %s' % (mlist.real_name, newpassword))
print(mlist.moderator) sender = 'root@host fqdn' receivers = [mlist.moderator] #receivers = ['my.directemail@domain.co.uk']
message = """ Daniel on list-test4 """
try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print ("Successfully sent email")
except SMTPException: print ("Error: unable to send email")
mlist.Save()
If it can't send email because of this issue the traceback advises (been trying to figure this out as well):
NameError: global name 'SMTPException' is not defined
I did some debugging:
print.moderator displays:
['moderator1@domain.co.uk', 'moderator2@domain.co.uk']
Exim4 mainlog file is showing:
2021-03-10 20:24:55 SMTP syntax error in "rcpt TO:<moderator1@domain.co.ukmoderator2@domain.co.uk>" H=localhost (box fqdn) [127.0.0.1] malformed address: @domain.co.uk> may not follow <moderator1@domain.co.ukmoderator2
I've been trying to work out to get a space to be added, as that looks like the issue to me?
Finally in regards to pythontutor, very helpful thank you. My thirteen year old son is also taking an interest in Python recently after doing a bit at school, we are learning together :)
Many thanks
Daniel
On 08/03/2021 18:04, Mark Sapiro wrote:
You want something like this for the script
import subprocess from Mailman.Utils import sha_new
def change_moderator_password2(mlist, moderator): if not mlist.Locked(): mlist.Lock() try: if moderator in mlist.moderator: newpassword = subprocess.check_output( ["pwgen", "-sB", "15", "1"]).strip()) mlist.mod_password = sha_new(newpassword).hexdigest() print('%s moderator password changed to %s' % (mlist.real_name, newpassword)) mlist.Save() else: print('%s not found in %s moderator' % (moderator, mlist.real_name))
except subprocess.CalledProcessError as e: print("password not changed for %s: %s\n%s" % (mlist.real_name, moderator, e.output)) finally: mlist.Unlock()
-- Daniel Botting Systems Administrator Codethink Ltd. 3rd Floor Dale House, 35 Dale Street, Manchester, M1 2HF United Kingdom
http://www.codethink.co.uk/ We respect your privacy. See https://www.codethink.co.uk/privacy.html

On 3/10/21 12:39 PM, Daniel Botting wrote:
Hi Mark,
Thank you, much appreciated.
I've got it working and progressed a little further ;)
["pwgen", "-sB", "15", "1"]).strip()) I had to remove the very end closing bracket, it was causing a syntax error.
That was my mistake, sorry.
I then encountered a Errno 2 error no such file, which I tracked down to the fact this was a new test server due to mail not being received to lists and I didn't have pwgen installed.
I then managed to get it to email me either directly for each occurence of the password being changed if I just put in my email address directly or if one moderator is set, the issue comes that if I have more than one moderator it's mangling the message:
print('%s moderator password changed to %s' % (mlist.real_name, newpassword))
print(mlist.moderator) sender = 'root@host fqdn' receivers = [mlist.moderator]
mlist.moderator is already a list so [mlist.moderator] is a list of lists.
#receivers = ['my.directemail@domain.co.uk']
message = """ Daniel on list-test4 """
try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print ("Successfully sent email")
except SMTPException: print ("Error: unable to send email")
mlist.Save()
If it can't send email because of this issue the traceback advises (been trying to figure this out as well):
NameError: global name 'SMTPException' is not defined
You have to import it. You also need to import smtplib. If you do
import smtplib
then you can do try: smtpObj = smtplib.SMTP('localhost') ... except smtplib.SMTPException: ...
Or if you also do
from smtplib import SMTPException
you can write the except clause as
except SMTPException:
There is no magic by which Python knows the names of things other than builtins (see sections 2 - 6 at <https://docs.python.org/2.7/library/index.html>). You have to import them or define them.
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan
participants (2)
-
Daniel Botting
-
Mark Sapiro