| 1 |
#!/usr/bin/env python |
|---|
| 2 |
# -*- coding: utf-8 -*- |
|---|
| 3 |
# |
|---|
| 4 |
# Copyright (C) 2006 Edgewall Software |
|---|
| 5 |
# Copyright (C) 2006 Matthew Good <matt@matt-good.net> |
|---|
| 6 |
# All rights reserved. |
|---|
| 7 |
# |
|---|
| 8 |
# This software is licensed as described in the file COPYING, which |
|---|
| 9 |
# you should have received as part of this distribution. The terms |
|---|
| 10 |
# are also available at http://trac.edgewall.org/wiki/TracLicense. |
|---|
| 11 |
# |
|---|
| 12 |
# This software consists of voluntary contributions made by many |
|---|
| 13 |
# individuals. For the exact contribution history, see the revision |
|---|
| 14 |
# history and logs, available at http://trac.edgewall.org/log/. |
|---|
| 15 |
# |
|---|
| 16 |
# Author: Matthew Good <matt@matt-good.net> |
|---|
| 17 |
|
|---|
| 18 |
import errno |
|---|
| 19 |
import fileinput |
|---|
| 20 |
import md5 |
|---|
| 21 |
import sys |
|---|
| 22 |
from optparse import OptionParser |
|---|
| 23 |
from getpass import getpass |
|---|
| 24 |
|
|---|
| 25 |
def ask_pass(): |
|---|
| 26 |
pass1 = getpass('New password: ') |
|---|
| 27 |
pass2 = getpass('Re-type new password: ') |
|---|
| 28 |
if pass1 != pass2: |
|---|
| 29 |
print >>sys.stderr, "They don't match, sorry" |
|---|
| 30 |
sys.exit(1) |
|---|
| 31 |
return pass1 |
|---|
| 32 |
|
|---|
| 33 |
def get_digest(userprefix, password=None): |
|---|
| 34 |
if password == None: |
|---|
| 35 |
password = ask_pass() |
|---|
| 36 |
return make_digest(userprefix, password) |
|---|
| 37 |
|
|---|
| 38 |
def make_digest(userprefix, password): |
|---|
| 39 |
return userprefix + md5.new(userprefix + password).hexdigest() |
|---|
| 40 |
|
|---|
| 41 |
usage = "%prog [-c] [-b] passwordfile realm username" |
|---|
| 42 |
parser = OptionParser(usage=usage) |
|---|
| 43 |
parser.add_option('-c', action='store_true', dest='create', default=False, |
|---|
| 44 |
help='Create a new file') |
|---|
| 45 |
parser.add_option('-b', action='store_true', dest='batch', default=False, |
|---|
| 46 |
help='Batch mode, password on the commandline.') |
|---|
| 47 |
|
|---|
| 48 |
opts, args = parser.parse_args() |
|---|
| 49 |
|
|---|
| 50 |
try: |
|---|
| 51 |
if opts.batch: |
|---|
| 52 |
filename, realm, username, password = args |
|---|
| 53 |
else: |
|---|
| 54 |
filename, realm, username = args |
|---|
| 55 |
password = None |
|---|
| 56 |
except ValueError: |
|---|
| 57 |
parser.error('Wrong number of arguments') |
|---|
| 58 |
|
|---|
| 59 |
prefix = '%s:%s:' % (username, realm) |
|---|
| 60 |
|
|---|
| 61 |
if opts.create: |
|---|
| 62 |
try: |
|---|
| 63 |
f = open(filename, 'w') |
|---|
| 64 |
except EnvironmentError, e: |
|---|
| 65 |
if e.errno == errno.EACCES: |
|---|
| 66 |
print >>sys.stderr, 'Unable to update file', filename |
|---|
| 67 |
sys.exit(1) |
|---|
| 68 |
else: |
|---|
| 69 |
raise |
|---|
| 70 |
try: |
|---|
| 71 |
print >>f, get_digest(prefix, password) |
|---|
| 72 |
finally: |
|---|
| 73 |
f.close() |
|---|
| 74 |
else: |
|---|
| 75 |
try: |
|---|
| 76 |
matched = False |
|---|
| 77 |
for line in fileinput.input(filename, inplace=True): |
|---|
| 78 |
if line.startswith(prefix): |
|---|
| 79 |
if not matched: |
|---|
| 80 |
print get_digest(prefix, password) |
|---|
| 81 |
matched = True |
|---|
| 82 |
else: |
|---|
| 83 |
print line, |
|---|
| 84 |
if not matched: |
|---|
| 85 |
f = open(filename, 'a') |
|---|
| 86 |
try: |
|---|
| 87 |
print >>f, get_digest(prefix, password) |
|---|
| 88 |
finally: |
|---|
| 89 |
f.close() |
|---|
| 90 |
except EnvironmentError, e: |
|---|
| 91 |
if e.errno == errno.ENOENT: |
|---|
| 92 |
print >>sys.stderr, 'Could not open passwd file %s for reading.' \ |
|---|
| 93 |
% filename |
|---|
| 94 |
print >>sys.stderr, 'Use -c option to create a new one.' |
|---|
| 95 |
sys.exit(1) |
|---|
| 96 |
elif e.errno == errno.EACCES: |
|---|
| 97 |
print >>sys.stderr, 'Unable to update file', filename |
|---|
| 98 |
sys.exit(1) |
|---|
| 99 |
else: |
|---|
| 100 |
raise |
|---|