#! /usr/bin/python
#
# August 2010, Anthony R. Thompson, check a password for a given user
# on a given list; based heavily on change_pw.
#
# Since this is 95% the same code, it might just be better if
# change_pw took a -m flag to set the moderator password instead of
# the list password, but this script is an interim step allowing one
# to set the moderator password from the command line or shell script.
#
# Copyright (C) 2001-2007 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.
#

"""Check the password for a member on a list(s).

Given a list name(s), member (subscriber address), and password, check
whether the given password matches the member's password on the
list(s).  If the member is not subscribed, return an error.

Usage: check_member_pw [options]

Options:

    --all / -a
        Check the member password for all lists.

    --domain=domain
    -d domain
        Check the member password for all lists in the virtual
        domain `domain'.  It is okay to give multiple -d options.

    --listname=listname
    -l listname
        Check the member password only for the named list.  It is
        okay to give multiple -l options.

    --password=password
    -p password
        Use the supplied plain text password `newpassword' as the
        member password for the member and lists that are being
        checked (as specified by the -a, -l, and -m options).

    --member=member
    -m member
        Check the member password for the specified address on one or
        more lists (as specified by the -a, -l, and -p options).

    --help / -h
        Print this help message and exit.
"""

import sys
import sha
import getopt

# This next line might not be necessary if this script is put in the
# same directory as the change_pw script
sys.path.append('/usr/lib/mailman/')  # INSTALL; Mailman directory location

from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman import Errors
from Mailman import Message
from Mailman import i18n

_ = i18n._

SPACE = ' '



def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, _(__doc__)
    if msg:
        print >> fd, msg
    sys.exit(code)



_listcache = {}

def openlist(listname):
    missing = []
    mlist = _listcache.get(listname, missing)
    if mlist is missing:
        try:
            mlist = MailList.MailList(listname, lock=0)
        except Errors.MMListError, e:
            usage(1, _('No such list "%(listname)s"\n%(e)s'))
        _listcache[listname] = mlist
    return mlist



def main():
    # Parse options
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'ad:l:p:m:h',
            ['all', 'domain=', 'listname=', 'password=', 'member=', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    # defaults
    listnames = {}
    domains = {}
    password = None
    member = None

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-a', '--all'):
            for name in Utils.list_names():
                listnames[name] = 1
        elif opt in ('-d', '--domain'):
            domains[arg] = 1
        elif opt in ('-l', '--listname'):
            listnames[arg.lower()] = 1
        elif opt in ('-p', '--password'):
            password = arg
        elif opt in ('-m', '--member'):
            member = arg

    if args:
        strargs = SPACE.join(args)
        usage(1, _('Bad arguments: %(strargs)s'))

    if password is not None:
        if not password:
            usage(1, _('You must specify a password to check'))
        shapassword = sha.new(password).hexdigest()

    if member is not None:
        if not member:
            usage(1, _('You must specify a member to check'))

    if domains:
        for name in Utils.list_names():
            mlist = openlist(name)
            if domains.has_key(mlist.host_name):
                listnames[name] = 1

    if not listnames:
        print >> sys.stderr, _('Nothing to do.')
        sys.exit(0)

    # Check the member password on the lists
    for listname in listnames.keys():
        mlist = openlist(listname)
        mlist.Lock()
        try:
            if not mlist.isMember(member):
                print _('No such member %(member)s on %(listname)s')
            else:
                if password == mlist.getMemberPassword(member):
                    print _('Password match for %(member)s on %(listname)s')
                else:
                    print _('Password mismatch for %(member)s on %(listname)s')
        finally:
            mlist.Unlock()



if __name__ == '__main__':
    main()
