#!/usr/bin/python
# 
# July 2010, Anthony R. Thompson, loosely based on change_pw
#
# Copyright (C) 2001-2010 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

"""Show which, if any, level of access a password provides.

Given a list name and password, say whether the password matches the
site admin password, the list admin password, and/or the list
moderator password.

Usage: which_pw <listname> <password>
"""

import sys
sys.path.append('/usr/lib/mailman/')  # INSTALL; Mailman directory location
from Mailman import Errors
from Mailman import MailList
from Mailman import Utils
from Mailman import mm_cfg

if len(sys.argv) < 3:
    print >> sys.stderr, __doc__
    sys.exit(1)
listname = sys.argv[1]
password = sys.argv[2]

def valid_site_admin_pass(sapass):
    return (Utils.sha.new(sapass).hexdigest() ==
            Utils.get_global_password(True))

def valid_list_pass(mlist, passwd):
    return (mlist.password == Utils.sha.new(passwd).hexdigest())

def valid_mod_pass(mlist, passwd):
    return (mlist.mod_password == Utils.sha.new(passwd).hexdigest())

def main():
    try:
        mlist = MailList.MailList(listname, lock=0)
    except Errors.MMUnknownListError:
        print >> sys.stderr, 'No such list "%s"' % (listname)
        sys.exit(1)

    print "Testing password: %s" % (password)

    if valid_site_admin_pass(password):
        print "Matches site admin password"
    else:
        print "Doesn't match site admin password"

    if valid_list_pass(mlist, password):
        print "Matches list password for %s" % (listname)
    else:
        print "Doesn't match list password for %s" % (listname)

    if valid_mod_pass(mlist, password):
        print "Matches moderator password for %s" % (listname)
    else:
        print "Doesn't match moderator password for %s" % (listname)

if __name__ == '__main__':
    main()
