-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpassword.py
executable file
·83 lines (73 loc) · 2.31 KB
/
adpassword.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
"""adpasswd.py Command line interface to change Active Directory Passwords via LDAP.
Copyright 2009 Craig Sawyer
email: [email protected]
license: GPLv2 see LICENSE.txt
"""
import os
import sys
import ConfigParser
import getpass
from adinterface import ADInterface #, UserNotFoundException
CONFIG_FILENAME = '.adpasswd.cfg'
class InvalidConfigException(Exception):
pass
def GetConfig():
cf = None
if os.path.exists(CONFIG_FILENAME):
configfile = CONFIG_FILENAME
else:
configfile = os.path.join('/etc', CONFIG_FILENAME)
if not os.path.exists(configfile):
homedrive = None
if os.name == 'posix':
path = os.environ['HOME']
elif os.name == 'nt':
if 'HOMEPATH' in os.environ:
homedrive = os.environ['HOMEDRIVE']
path = os.environ['HOMEPATH']
configfile = os.path.join(homedrive, path, CONFIG_FILENAME)
if os.path.exists(configfile):
fd = open(configfile, 'r')
config = ConfigParser.ConfigParser()
config.readfp(fd)
if config.has_section('ad'):
cf = dict()
for name, value in config.items('ad'):
cf[name] = value
else:
raise InvalidConfigException()
print 'Config file seems misconfigured.. no [ad] section'
else:
print "we need a config file here %s or the cwd. " % (configfile)
print """example config:
[ad]
host: ad.blah.com
port: 636
binddn: cn=Administrator,CN=Users,DC=ad,DC=blah,DC=com
bindpw: changemequickly
searchdn: DC=ad,DC=blah,DC=com
"""
raise InvalidConfigException()
print "No valid config file. Quitting."
return cf
def Main():
user = None
password = None
if len(sys.argv) == 3:
user = sys.argv[1]
password = sys.argv[2]
if len(sys.argv) == 2:
user = sys.argv[1]
password = getpass.getpass()
if user and password:
cf = GetConfig()
print cf
l = ADInterface(cf)
l.changepass(user, password)
else:
print "adpasswd.py: You must specify <username> and (optionally) <password>"
print "usage: adpasswd.py username [password]"
sys.exit(1)
if __name__ == "__main__":
Main()