Skip to content

Commit

Permalink
Augment mailpasswd so that it can be used by root and scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
melloc committed Aug 21, 2013
1 parent 6ba306c commit d03e3e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 17 additions & 11 deletions mailpasswd
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,41 @@
from manageldap import *
import argparse, getpass, ldap

# mailpasswd [-m --modify] [-r room_no] [-w work_ph] [-h home_ph] [-o other]
# mailpasswd [-m --modify] [-s --stdin] [-u --user]
parser = argparse.ArgumentParser(description="Retrieve (or modify) the invoking user's mail password.")
parser.add_argument('-m','--modify',action='store_true',help="Change the user's mail password.")
parser.add_argument('-s','--stdin',action='store_true',help="Read the user's mail password from stdin.")
parser.add_argument('-u','--user',type=str,help="The user we should bind as.",default="")
args = parser.parse_args()


def printMailpasswd(conn, dn):
a = conn.search_s(dn, ldap.SCOPE_BASE)[0][1]
print "Your mail password is: %s" % a["userPassword"][0]

def modifyMailpasswd(conn, dn):
passwd = getpass.getpass("New mail password: ")
again = getpass.getpass("New mail password again: ")
if passwd != again:
print "Passwords do not match."
return
def modifyMailpasswd(conn, dn, stdin=False):
if stdin:
passwd = raw_input()
else:
passwd = getpass.getpass("New mail password: ")
again = getpass.getpass("New mail password again: ")
if passwd != again:
print "Passwords do not match."
return

conn.modify_s(dn,[(ldap.MOD_REPLACE, 'userPassword', passwd)])

def mailpasswd(change):
binddn = getBindDn()
def mailpasswd(user, change, stdin=False):
binddn = getBindDn(user=user)
uid = getUsername(binddn)
dn = "cn=%s,ou=mailpasswd,%s" % (uid, basedn)
conn = getConnection(binddn, server)
if conn:
try:
modifyMailpasswd(conn, dn) if change else printMailpasswd(conn, dn)
modifyMailpasswd(conn, dn, stdin=stdin) if change else printMailpasswd(conn, dn)
finally:
conn.unbind()
else:
print "mailpasswd couldn't complete. See above for errors. If the problem persists, please contact `root@techhouse.org'."

mailpasswd(args.modify)
mailpasswd(args.user, args.modify, stdin=args.stdin)
4 changes: 2 additions & 2 deletions manageldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def getNextId(database="passwd"):
highest = awk.communicate()[0]
return highest.strip()

def getBindDn():
def getBindDn(user=""):
"""Return a DN for binding as the current logged in user.
This function assumes that the uid 'user' maps to the DN uid=user,ou=People
beneath the base DN."""
username = getpass.getuser()
username = user or getpass.getuser()
binddn = "uid=%s,ou=People,%s" % (username,basedn)
return binddn

Expand Down

0 comments on commit d03e3e4

Please sign in to comment.