[Mailman-Users] Change moderator or admin password (random) different for each list they were an admin or moderator for - Mailman 2.1

2021-03-04 Thread Daniel Botting

Hi,

I have been using the withlist command to delete a given administrator 
or moderator from all mailman 2.1 lists.


I also require to change the moderator or admin password for all lists 
that they have been removed from, but have it so that it is a different 
randomly generated password for each list they are removed from and then 
have this emailed to the other moderators or admins who are then left or 
a default address if none are left (I have managed so far that it is 
changed to the same password for each list they have been removed from). 
I am currently at the beginning of my Python learning so this is why I'm 
asking for help.


My script and usage so far can be found below:

Usage:

withlist --all --run script_name.change_administrator_password 
firstname.lastn...@domain.co.uk `pwgen -sB 15 1`


The script is saved at:

/usr/sbin/

Function defined in the script:

import sha

def change_administrator_password(mlist, owner, newpasswd):
mlist.Lock()
try:
if mlist.owner[mlist.owner.index(owner)]:
mlist.password = sha.new(newpasswd).hexdigest()
   print (newpasswd)

except:
  print("password not changed for", (mlist.real_name, owner))
mlist.Save()
mlist.Unlock()

This will go through all lists and change the administrator password to 
a randomly generated one, but as advised above it will set the same 
password (not what I want) I've put a print in for debugging so I can 
see what it was.


Thanks

Daniel

--
Mailman-Users mailing list -- mailman-users@python.org
To unsubscribe send an email to mailman-users-le...@python.org
https://mail.python.org/mailman3/lists/mailman-users.python.org/
Mailman FAQ: http://wiki.list.org/x/AgA3
Security Policy: http://wiki.list.org/x/QIA9
Searchable Archives: https://www.mail-archive.com/mailman-users@python.org/
   https://mail.python.org/archives/list/mailman-users@python.org/


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

2021-03-08 Thread Daniel Botting


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.lastn...@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.lastn...@domain.co.uk

Importing list_mod...
Running list_mod.change_moderator_password2()...
Loading list test4-list1 (unlocked)
('password not changed for', ('Test4-list1', 
'first.lastn...@domain.co.uk'))

Loading list test4-list3 (unlocked)
('password not changed for', ('Test4-list3', 
'first.lastn...@domain.co.uk'))

Loading list mailman (unlocked)
('password not changed for', ('Mailman', 'first.lastn...@domain.co.uk'))
Loading list test4-list2 (unlocked)
('password not changed for', ('Test4-list2', 
'first.lastn...@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

--
Mailman-Users mailing list -- mailman-users@python.org
To unsubscribe send an email to mailman-users-le...@python.org
https://mail.python.org/mailman3/lists/mailman-users.python.org/
Mailman FAQ: http://wiki.list.org/x/AgA3
Security Policy: http://wiki.list.org/x/QIA9
Searchable Archives: https://www.mail-archive.com/mailman-users@python.org/
   https://mail.python.org/archives/list/mailman-users@python.org/


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

2021-03-10 Thread Daniel Botting

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.directem...@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:

['moderat...@domain.co.uk', 'moderat...@domain.co.uk']

Exim4 mainlog file is showing:

2021-03-10 20:24:55 SMTP syntax error in "rcpt 
TO:" H=localhost (box 
fqdn) [127.0.0.1] malformed address: @domain.co.uk> may not follow 


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

--
Mailman-Users mailing list -- mailman-users@python.org
To unsubscribe send an email to mailman-users-le...@python.org
https://mail.python.org/mailman3/lists/mailman-users.python.org/
Mailman FAQ: http://wiki.list.org/x/AgA3
Security Policy: http://wiki.list.org/x/QIA9
Searchable Archives: https://www.mail-archive.com/mailman-users@python.org/
   https://mail.python.org/archives/list/mailman-users@python.org/