s4-repl: add a debug to make it easier to monitor replication
[Samba/aatanasov.git] / source4 / setup / pwsettings
blobcd9c07dfb54aac4c02ed701a77db386d5718f9ee
1 #!/usr/bin/python
3 # Sets password settings (Password complexity, history length,
4 # minimum password length, the minimum and maximum password age) on a
5 # Samba4 server
7 # Copyright Jelmer Vernooij 2008
8 # Copyright Matthias Dieter Wallnoefer 2009
9 # Copyright Andrew Kroeger 2009
10 # Released under the GNU GPL version 3 or later
12 import os, sys
14 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), "../bin/python"))
16 import samba.getopt as options
17 import optparse
18 import pwd
19 import ldb
21 from samba.auth import system_session
22 from samba.samdb import SamDB
23 from samba.dcerpc.samr import DOMAIN_PASSWORD_COMPLEX
25 parser = optparse.OptionParser("pwsettings (show | set <options>)")
26 sambaopts = options.SambaOptions(parser)
27 parser.add_option_group(sambaopts)
28 parser.add_option_group(options.VersionOptions(parser))
29 credopts = options.CredentialsOptions(parser)
30 parser.add_option_group(credopts)
31 parser.add_option("--quiet", help="Be quiet", action="store_true")
32 parser.add_option("-H", help="LDB URL for database or target server", type=str)
33 parser.add_option("--complexity",
34 help="The password complexity (on | off | default). Default is 'on'", type=str)
35 parser.add_option("--history-length",
36 help="The password history length (<integer> | default). Default is 24.", type=str)
37 parser.add_option("--min-pwd-length",
38 help="The minimum password length (<integer> | default). Default is 7.", type=str)
39 parser.add_option("--min-pwd-age",
40 help="The minimum password age (<integer in days> | default). Default is 0.", type=str)
41 parser.add_option("--max-pwd-age",
42 help="The maximum password age (<integer in days> | default). Default is 43.", type=str)
44 opts, args = parser.parse_args()
47 # print a message if quiet is not set
49 def message(text):
50 if not opts.quiet:
51 print text
53 if len(args) == 0:
54 parser.print_usage()
55 sys.exit(1)
57 lp = sambaopts.get_loadparm()
59 creds = credopts.get_credentials(lp)
61 if opts.H is not None:
62 url = opts.H
63 else:
64 url = lp.get("sam database")
66 samdb = SamDB(url=url, session_info=system_session(),
67 credentials=creds, lp=lp)
69 domain_dn = SamDB.domain_dn(samdb)
70 res = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
71 attrs=["pwdProperties", "pwdHistoryLength", "minPwdLength", "minPwdAge",
72 "maxPwdAge"])
73 assert(len(res) == 1)
74 try:
75 pwd_props = int(res[0]["pwdProperties"][0])
76 pwd_hist_len = int(res[0]["pwdHistoryLength"][0])
77 min_pwd_len = int(res[0]["minPwdLength"][0])
78 # ticks -> days
79 min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))
80 max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
81 except:
82 if args[0] == "show":
83 print "ERROR: Password informations missing in your AD domain object!"
84 print "So no settings can be displayed!"
85 sys.exit(1)
86 else:
87 print "ERROR: Could not retrieve password properties (used for password complexity setting)"
88 sys.exit(1)
90 if args[0] == "show":
91 message("Password informations for domain '" + domain_dn + "'")
92 message("")
93 if pwd_props & DOMAIN_PASSWORD_COMPLEX != 0:
94 message("Password complexity: on")
95 else:
96 message("Password complexity: off")
97 message("Password history length: " + str(pwd_hist_len))
98 message("Minimum password length: " + str(min_pwd_len))
99 message("Minimum password age (days): " + str(min_pwd_age))
100 message("Maximum password age (days): " + str(max_pwd_age))
102 elif args[0] == "set":
104 msgs = []
105 m = ldb.Message()
106 m.dn = ldb.Dn(samdb, domain_dn)
108 if opts.complexity is not None:
109 if opts.complexity == "on" or opts.complexity == "default":
110 pwd_props = pwd_props | DOMAIN_PASSWORD_COMPLEX
111 msgs.append("Password complexity activated!")
112 elif opts.complexity == "off":
113 pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)
114 msgs.append("Password complexity deactivated!")
115 else:
116 print "ERROR: Wrong argument '" + opts.complexity + "'!"
117 sys.exit(1)
119 m["pwdProperties"] = ldb.MessageElement(str(pwd_props),
120 ldb.FLAG_MOD_REPLACE, "pwdProperties")
122 if opts.history_length is not None:
123 if opts.history_length == "default":
124 pwd_hist_len = 24
125 else:
126 pwd_hist_len = int(opts.history_length)
128 if pwd_hist_len < 0 or pwd_hist_len > 24:
129 print "ERROR: Password history length must be in the range of 0 to 24!"
130 sys.exit(1)
132 m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),
133 ldb.FLAG_MOD_REPLACE, "pwdHistoryLength")
134 msgs.append("Password history length changed!")
136 if opts.min_pwd_length is not None:
137 if opts.min_pwd_length == "default":
138 min_pwd_len = 7
139 else:
140 min_pwd_len = int(opts.min_pwd_length)
142 if min_pwd_len < 0 or min_pwd_len > 14:
143 print "ERROR: Minimum password length must be in the range of 0 to 14!"
144 sys.exit(1)
146 m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),
147 ldb.FLAG_MOD_REPLACE, "minPwdLength")
148 msgs.append("Minimum password length changed!")
150 if opts.min_pwd_age is not None:
151 if opts.min_pwd_age == "default":
152 min_pwd_age = 0
153 else:
154 min_pwd_age = int(opts.min_pwd_age)
156 if min_pwd_age < 0 or min_pwd_age > 998:
157 print "ERROR: Minimum password age must be in the range of 0 to 998!"
158 sys.exit(1)
160 # days -> ticks
161 min_pwd_age_ticks = -int(min_pwd_age * (24 * 60 * 60 * 1e7))
163 m["minPwdAge"] = ldb.MessageElement(str(min_pwd_age_ticks),
164 ldb.FLAG_MOD_REPLACE, "minPwdAge")
165 msgs.append("Minimum password age changed!")
167 if opts.max_pwd_age is not None:
168 if opts.max_pwd_age == "default":
169 max_pwd_age = 43
170 else:
171 max_pwd_age = int(opts.max_pwd_age)
173 if max_pwd_age < 0 or max_pwd_age > 999:
174 print "ERROR: Maximum password age must be in the range of 0 to 999!"
175 sys.exit(1)
177 # days -> ticks
178 max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))
180 m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age_ticks),
181 ldb.FLAG_MOD_REPLACE, "maxPwdAge")
182 msgs.append("Maximum password age changed!")
184 if max_pwd_age > 0 and min_pwd_age >= max_pwd_age:
185 print "ERROR: Maximum password age (%d) must be greater than minimum password age (%d)!" % (max_pwd_age, min_pwd_age)
186 sys.exit(1)
188 samdb.modify(m)
190 msgs.append("All changes applied successfully!")
192 message("\n".join(msgs))
193 else:
194 print "ERROR: Wrong argument '" + args[0] + "'!"
195 sys.exit(1)