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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#!/usr/bin/python
#
# Sets password settings (Password complexity, history length,
# minimum password length, the minimum and maximum password age) on a
# Samba4 server
#
# Copyright Jelmer Vernooij 2008
# Copyright Matthias Dieter Wallnoefer 2009
# Released under the GNU GPL version 3 or later
#
import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), "../bin/python"))
import samba.getopt as options
import optparse
import pwd
import ldb
from samba.auth import system_session
from samba.samdb import SamDB
from samba.dcerpc.samr import DOMAIN_PASSWORD_COMPLEX
parser = optparse.OptionParser("pwsettings (show | set <options>)")
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option_group(options.VersionOptions(parser))
credopts = options.CredentialsOptions(parser)
parser.add_option_group(credopts)
parser.add_option("--quiet", help="Be quiet", action="store_true")
parser.add_option("-H", help="LDB URL for database or target server", type=str)
parser.add_option("--complexity",
help="The password complexity (on | off). Default is 'on'", type=str)
parser.add_option("--history-length",
help="The password history length (<integer> | default)", type=str)
parser.add_option("--min-pwd-length",
help="The minimum password length (<integer> | default)", type=str)
parser.add_option("--min-pwd-age",
help="The minimum password age (<integer in days> | default)", type=str)
parser.add_option("--max-pwd-age",
help="The maximum password age (<integer in days> | default)", type=str)
opts, args = parser.parse_args()
#
# print a message if quiet is not set
#
def message(text):
if not opts.quiet:
print text
if len(args) == 0:
parser.print_usage()
sys.exit(1)
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
if opts.H is not None:
url = opts.H
else:
url = lp.get("sam database")
samdb = SamDB(url=url, session_info=system_session(),
credentials=creds, lp=lp)
domain_dn = SamDB.domain_dn(samdb)
res = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
attrs=["pwdProperties", "pwdHistoryLength", "minPwdLength", "minPwdAge",
"maxPwdAge"])
assert(len(res) == 1)
try:
pwd_props = int(res[0]["pwdProperties"][0])
pwd_hist_len = int(res[0]["pwdHistoryLength"][0])
min_pwd_len = int(res[0]["minPwdLength"][0])
# ticks -> days
min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))
max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
except:
if args[0] == "show":
print "ERROR: Password informations missing in your AD domain object!"
print "So no settings can be displayed!"
sys.exit(1)
else:
pwd_props = 0
message("WARNING: Assuming previous password properties 0 (used for password complexity setting)")
if args[0] == "show":
message("Password informations for domain '" + domain_dn + "'")
message("")
if pwd_props & DOMAIN_PASSWORD_COMPLEX != 0:
message("Password complexity: on")
else:
message("Password complexity: off")
message("Password history length: " + str(pwd_hist_len))
message("Minimum password length: " + str(min_pwd_len))
message("Minimum password age (days): " + str(min_pwd_age))
message("Maximum password age (days): " + str(max_pwd_age))
elif args[0] == "set":
if opts.complexity is not None:
if opts.complexity == "on":
pwd_props = pwd_props | DOMAIN_PASSWORD_COMPLEX
m = ldb.Message()
m.dn = ldb.Dn(samdb, domain_dn)
m["pwdProperties"] = ldb.MessageElement(str(pwd_props),
ldb.FLAG_MOD_REPLACE, "pwdProperties")
samdb.modify(m)
message("Password complexity activated!")
elif opts.complexity == "off":
pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)
m = ldb.Message()
m.dn = ldb.Dn(samdb, domain_dn)
m["pwdProperties"] = ldb.MessageElement(str(pwd_props),
ldb.FLAG_MOD_REPLACE, "pwdProperties")
samdb.modify(m)
message("Password complexity deactivated!")
else:
print "ERROR: Wrong argument '" + opts.complexity + "'!"
sys.exit(1)
if opts.history_length is not None:
if opts.history_length == "default":
pwd_hist_len = 24
else:
pwd_hist_len = int(opts.history_length)
m = ldb.Message()
m.dn = ldb.Dn(samdb, domain_dn)
m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),
ldb.FLAG_MOD_REPLACE, "pwdHistoryLength")
samdb.modify(m)
message("Password history length changed!")
if opts.min_pwd_length is not None:
if opts.min_pwd_length == "default":
min_pwd_len = 7
else:
min_pwd_len = int(opts.min_pwd_length)
m = ldb.Message()
m.dn = ldb.Dn(samdb, domain_dn)
m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),
ldb.FLAG_MOD_REPLACE, "minPwdLength")
samdb.modify(m)
message("Minimum password length changed!")
if opts.min_pwd_age is not None:
if opts.min_pwd_age == "default":
min_pwd_age = 0
else:
min_pwd_age = int(opts.min_pwd_age)
# days -> ticks
min_pwd_age = -int(min_pwd_age * (24 * 60 * 60 * 1e7))
m = ldb.Message()
m.dn = ldb.Dn(samdb, domain_dn)
m["minPwdAge"] = ldb.MessageElement(str(min_pwd_age),
ldb.FLAG_MOD_REPLACE, "minPwdAge")
samdb.modify(m)
message("Minimum password age changed!")
if opts.max_pwd_age is not None:
if opts.max_pwd_age == "default":
max_pwd_age = 43
else:
max_pwd_age = int(opts.max_pwd_age)
# days -> ticks
max_pwd_age = -int(max_pwd_age * (24 * 60 * 60 * 1e7))
m = ldb.Message()
m.dn = ldb.Dn(samdb, domain_dn)
m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age),
ldb.FLAG_MOD_REPLACE, "maxPwdAge")
samdb.modify(m)
message("Maximum password age changed!")
message("All changes applied successfully!")
else:
print "ERROR: Wrong argument '" + args[0] + "'!"
sys.exit(1)
|