-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmailpasswd
executable file
·57 lines (50 loc) · 2.3 KB
/
mailpasswd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/env python
from manageldap import *
import argparse, getpass, ldap
# 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('-c','--create',action='store_true',help="Create a new entry for the user.")
parser.add_argument('-d','--delete',action='store_true',help="Delete the existing entry for the user.")
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, stdin=False, create=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."
exit(1)
if create:
cn = getUsername(dn)
print "Creating new user: %s" % cn
conn.add_s(dn,[ ('objectClass', ['simpleSecurityObject', 'organizationalRole']),
('cn', [cn]),
('userPassword', [passwd]) ])
print "Deleted %s" % dn
else:
conn.modify_s(dn,[(ldap.MOD_REPLACE, 'userPassword', passwd)])
def mailpasswd(user, change, stdin=False, create=False, delete=False):
binddn = getBindDn(user=user)
uid = getUsername(binddn)
dn = "cn=%s,ou=mailpasswd,%s" % (uid, basedn)
conn = getConnection(binddn, server)
if conn:
try:
if delete:
conn.delete_s(dn)
elif change or create:
modifyMailpasswd(conn, dn, stdin=stdin, create=create)
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.user, args.modify, stdin=args.stdin, create=args.create, delete=args.delete)