[Zope] password

Jonathan Desp Jonathan Desp" <jonathandesp@atomasoft.com
Sat, 29 Jul 2000 18:20:38 -0400


Hi,

the program zpasswd prompt.

they said:

If this program is called without command-line options, it will prompt
for all necessary information.  The available options are:

    -u / --username=
    Set the username to be used for the superuser

    -p / --password=
    Set the password

    -e / --encoding=
    Set the encryption/encoding rules.  Defaults to SHA-1. OPTIONAL

    -d / --domains=
    Set the domain names that the user user can log in from.  Defaults to
    any. OPTIONAL.

    Filename is not option, and should be the name of the file to store the
    information in.

__________________

What I should write in this source code from zpasswd ? -->

"""Zope password change system"""

__version__='$Revision: 1.8 $ '[11:-2]

import sys, string, sha, binascii, whrandom, getopt, getpass, os

try:
    from crypt import crypt
except ImportError:
    crypt = None

def generate_salt():
    """Generate a salt value for the crypt function."""
    salt_choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
                   "abcdefghijklmnopqrstuvwxyz" \
                   "0123456789./"
    return whrandom.choice(salt_choices)+whrandom.choice(salt_choices)

def generate_passwd(password, encoding):
    encoding=string.upper(encoding)
    if encoding == 'SHA':
        pw = '{SHA}' + binascii.b2a_base64(sha.new(password).digest())[:-1]
    elif encoding == 'CRYPT':
        pw = '{CRYPT}' + crypt(password, generate_salt())
    elif encoding == 'CLEARTEXT':
        pw = password

    return pw

def write_access(home, user='', group=''):
    import whrandom
    pw_choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
                 "abcdefghijklmnopqrstuvwxyz" \
                 "0123456789!"

    ac_path=os.path.join(home, 'access')
    if not os.path.exists(ac_path):
        print '-'*78
        print 'creating default access file'
        acfile=open(ac_path, 'w')
        pw = ''
        for i in range(8):
            pw = pw + whrandom.choice(pw_choices)
        acfile.write('superuser:' + generate_passwd(pw, 'SHA'))
        acfile.close()
        os.system('chmod 644 access')

        print """Note:
        The super user name and password are 'superuser'
        and '%s'.

        You can change the superuser name and password with the
        zpasswd script.  To find out more, type:

        %s zpasswd.py
        """ % (pw, sys.executable)

        import do; do.ch(ac_path, user, group)

def main(argv):
    short_options = ':u:p:e:d:'
    long_options = ['username=',
                    'password=',
                    'encoding=',
                    'domains=']

    usage = """%s [options] filename

If this program is called without command-line options, it will prompt
for all necessary information.  The available options are:

    -u / --username=
    Set the username to be used for the superuser

    -p / --password=
    Set the password

    -e / --encoding=
    Set the encryption/encoding rules.  Defaults to SHA-1. OPTIONAL

    -d / --domains=
    Set the domain names that the user user can log in from.  Defaults to
    any. OPTIONAL.

    Filename is not option, and should be the name of the file to store the
    information in.

Copyright (C) 1999 Digital Creations, Inc.
""" % argv[0]

    try:
        if len(argv) < 2:
            raise "CommandLineError"

        optlist, args = getopt.getopt(sys.argv[1:], short_options,
long_options)

        if len(args) != 1:
            raise "CommandLineError"

        access_file = open(args[0], 'w')

        if len(optlist) > 0:
            # Set the sane defaults
            username = 'superuser'
            encoding = 'SHA'
            domains = ''

            for opt in optlist:
                if (opt[0] == '-u') or (opt[0] == '--username'):
                    username = opt[1]
                elif (opt[0] == '-p') or (opt[0] == '--password'):
                    password = opt[1]
                elif (opt[0] == '-e') or (opt[0] == '--encoding'):
                    encoding = opt[1]
                elif (opt[0] == '-d') or (opt[0] == '--domains'):
                    domains = ":" + opt[1]

            # Verify that we got what we need
            if not username or not password:
                raise "CommandLineError"

            access_file.write(username + ':' +
                              generate_passwd(password, encoding) +
                              domains)

        else:
            # Run through the prompts
            while 1:
                username = raw_input("Username: ")
                if username != '':
                    break

            while 1:
                password = getpass.getpass("Password: ")
                verify = getpass.getpass("Vefify password: ")
                if verify == password:
                    break
                else:
                    password = verify = ''
                    print "Password mismatch, please try again..."

            while 1:
                print """
Please choose a format from:

SHA - SHA-1 hashed password
CRYPT - UNIX-style crypt password
CLEARTEXT - no protection.
"""
                encoding = raw_input("Encoding: ")
                if encoding != '':
                    break

            domains = raw_input("Domain restrictions: ")
            if domains: domains = ":" + domains

            access_file.write(username + ":" +
                      generate_passwd(password, encoding) +
                      domains)

    except "CommandLineError":
        sys.stderr.write(usage)
        sys.exit(1)


# If called from the command line
if __name__=='__main__': main(sys.argv)




Very truly yours,

<><><><><><><><><><><><><><>
Jonathan Desp
Chairman and CEO, Atomasoft Inc.
Matter will become software
http://www.atomasoft.com
<><><><><><><><><><><><><><>